class DB::MariaDB::Native::Connection

Public Class Methods

connect(wrapper: IO, host: 'localhost', username: nil, password: nil, database: nil, port: 0, unix_socket: nil, client_flags: 0, compression: false, types: DEFAULT_TYPES, **options) click to toggle source
# File lib/db/mariadb/native/connection.rb, line 76
def self.connect(wrapper: IO, host: 'localhost', username: nil, password: nil, database: nil, port: 0, unix_socket: nil, client_flags: 0, compression: false, types: DEFAULT_TYPES, **options)
        pointer = Native.mysql_init(nil)
        Native.mysql_options(pointer, MYSQL_OPT_NONBLOCK, nil)
        
        # if protocol
        #  Native.mysql_options(pointer, MYSQL_OPT_PROTOCOL, FFI::MemoryPointer.new(:uint, protocol))
        # end
        
        client_flags |= CLIENT_MULTI_STATEMENT | CLIENT_MULTI_RESULTS
        
        if compression
                client_flags |= CLIENT_COMPRESSION
        end
        
        result = FFI::MemoryPointer.new(:pointer)
        
        status = Native.mysql_real_connect_start(result, pointer, host, username, password, database, port, unix_socket, client_flags);
        
        io = wrapper.new(Native.mysql_get_socket(pointer), "r+")
        
        if status > 0
                while status > 0
                        if status & MYSQL_WAIT_READ
                                io.wait_readable
                        elsif status & MYSQL_WAIT_WRITE
                                io.wait_writable
                        else
                                io.wait_any
                        end
                        
                        status = Native.mysql_real_connect_cont(result, pointer, status)
                end
        end
        
        if result.read_pointer.null?
                raise Error, "Could not connect: #{Native.mysql_error(pointer)}!"
        end
        
        return self.new(pointer, io, types, **options)
end
new(address, io, types, **options) click to toggle source
Calls superclass method
# File lib/db/mariadb/native/connection.rb, line 117
def initialize(address, io, types, **options)
        super(address)
        
        @io = io
        @result = nil
        
        @types = types
end

Public Instance Methods

affected_rows() click to toggle source
# File lib/db/mariadb/native/connection.rb, line 208
def affected_rows
        Native.mysql_affected_rows(self)
end
check_error!(message) click to toggle source
# File lib/db/mariadb/native/connection.rb, line 134
def check_error!(message)
        if Native.mysql_errno(self) != 0
                raise Error, "#{message}: #{Native.mysql_error(self)}!"
        end
end
close() click to toggle source
# File lib/db/mariadb/native/connection.rb, line 152
def close
        self.free_result
        
        Native.mysql_close(self)
        
        @io.close
end
discard_results() click to toggle source

Silently discard any results that application didn't read.

# File lib/db/mariadb/native/connection.rb, line 201
def discard_results
        while result = self.get_result
        end
        
        return nil
end
escape(value) click to toggle source
# File lib/db/mariadb/native/connection.rb, line 160
def escape(value)
        value = value.to_s
        
        maximum_length = value.bytesize * 2 + 1
        out = FFI::MemoryPointer.new(:char, maximum_length)
        
        Native.mysql_real_escape_string(self, out, value, value.bytesize)
        
        return out.read_string
end
free_result() click to toggle source
# File lib/db/mariadb/native/connection.rb, line 144
def free_result
        if @result
                Native.mysql_free_result(@result)
                
                @result = nil
        end
end
info() click to toggle source
# File lib/db/mariadb/native/connection.rb, line 216
def info
        Native.mysql_info(self)
end
insert_id() click to toggle source
# File lib/db/mariadb/native/connection.rb, line 212
def insert_id
        Native.mysql_insert_id(self)
end
more_results?() click to toggle source

@returns [Boolean] If there are more results.

# File lib/db/mariadb/native/connection.rb, line 190
def more_results?
        Native.mysql_more_results(self) == 1
end
next_result(types: @types) click to toggle source
# File lib/db/mariadb/native/connection.rb, line 194
def next_result(types: @types)
        if result = self.get_result
                return Result.new(self, types, result)
        end
end
send_query(statement) click to toggle source
# File lib/db/mariadb/native/connection.rb, line 171
def send_query(statement)
        self.free_result
        
        error = FFI::MemoryPointer.new(:int)
        
        status = Native.mysql_real_query_start(error, self, statement, statement.bytesize)
        
        while status != 0
                self.wait_for(status)
                
                status = Native.mysql_real_query_cont(error, self, status)
        end
        
        if error.read_int != 0
                raise Error, "Could not send query: #{Native.mysql_error(self)}!"
        end
end
status() click to toggle source
# File lib/db/mariadb/native/connection.rb, line 140
def status
        Native.mysql_stat(self)
end
wait_for(status) click to toggle source
# File lib/db/mariadb/native/connection.rb, line 126
def wait_for(status)
        if status & MYSQL_WAIT_READ
                @io.wait_readable
        elsif status & MYSQL_WAIT_WRITE
                @io.wait_writable
        end
end

Protected Instance Methods

get_result() click to toggle source
# File lib/db/mariadb/native/connection.rb, line 221
def get_result
        if @result
                self.free_result
                
                # Successful and there are no more results:
                return if Native.mysql_next_result(self) == -1
                
                check_error!("Get result")
        end
        
        @result = Native.mysql_use_result(self)
        
        if @result.null?
                check_error!("Get result")
                
                return nil
        else
                return @result
        end
end