Class: DB::MariaDB::Native::Connection
- Inherits:
-
FFI::Pointer
- Object
- FFI::Pointer
- DB::MariaDB::Native::Connection
- Defined in:
- lib/db/mariadb/native/connection.rb
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Class Method Summary collapse
Instance Method Summary collapse
- #affected_rows ⇒ Object
- #check_error!(message) ⇒ Object
- #close ⇒ Object
-
#discard_results ⇒ Object
Silently discard any results that application didn’t read.
- #escape(value) ⇒ Object
- #free_result ⇒ Object
- #info ⇒ Object
-
#initialize(address, io, types, **options) ⇒ Connection
constructor
A new instance of Connection.
- #insert_id ⇒ Object
- #more_results? ⇒ Boolean
- #next_result(types: @types) ⇒ Object
- #send_query(statement) ⇒ Object
- #status ⇒ Object
- #wait_for(status) ⇒ Object
Constructor Details
#initialize(address, io, types, **options) ⇒ Connection
Returns a new instance of Connection.
96 97 98 99 100 101 102 103 |
# File 'lib/db/mariadb/native/connection.rb', line 96 def initialize(address, io, types, **) super(address) @io = io @result = nil @types = types end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
105 106 107 |
# File 'lib/db/mariadb/native/connection.rb', line 105 def types @types end |
Class Method Details
.connect(host: 'localhost', username: nil, password: nil, database: nil, port: 0, unix_socket: nil, client_flags: 0, compression: false, types: DEFAULT_TYPES, **options) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/db/mariadb/native/connection.rb', line 55 def self.connect(host: 'localhost', username: nil, password: nil, database: nil, port: 0, unix_socket: nil, client_flags: 0, compression: false, types: DEFAULT_TYPES, **) pointer = Native.mysql_init(nil) Native.(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 = ::IO.new(Native.mysql_get_socket(pointer), "r+", autoclose: false) 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, **) end |
Instance Method Details
#affected_rows ⇒ Object
189 190 191 |
# File 'lib/db/mariadb/native/connection.rb', line 189 def affected_rows Native.mysql_affected_rows(self) end |
#check_error!(message) ⇒ Object
115 116 117 118 119 |
# File 'lib/db/mariadb/native/connection.rb', line 115 def check_error!() if Native.mysql_errno(self) != 0 raise Error, "#{message}: #{Native.mysql_error(self)}!" end end |
#close ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/db/mariadb/native/connection.rb', line 133 def close self.free_result Native.mysql_close(self) @io.close end |
#discard_results ⇒ Object
Silently discard any results that application didn’t read.
182 183 184 185 186 187 |
# File 'lib/db/mariadb/native/connection.rb', line 182 def discard_results while result = self.get_result end return nil end |
#escape(value) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/db/mariadb/native/connection.rb', line 141 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 ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/db/mariadb/native/connection.rb', line 125 def free_result if @result Native.mysql_free_result(@result) @result = nil end end |
#info ⇒ Object
197 198 199 |
# File 'lib/db/mariadb/native/connection.rb', line 197 def info Native.mysql_info(self) end |
#insert_id ⇒ Object
193 194 195 |
# File 'lib/db/mariadb/native/connection.rb', line 193 def insert_id Native.mysql_insert_id(self) end |
#more_results? ⇒ Boolean
171 172 173 |
# File 'lib/db/mariadb/native/connection.rb', line 171 def more_results? Native.mysql_more_results(self) == 1 end |
#next_result(types: @types) ⇒ Object
175 176 177 178 179 |
# File 'lib/db/mariadb/native/connection.rb', line 175 def next_result(types: @types) if result = self.get_result return Result.new(self, types, result) end end |
#send_query(statement) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/db/mariadb/native/connection.rb', line 152 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 ⇒ Object
121 122 123 |
# File 'lib/db/mariadb/native/connection.rb', line 121 def status Native.mysql_stat(self) end |
#wait_for(status) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/db/mariadb/native/connection.rb', line 107 def wait_for(status) if status & MYSQL_WAIT_READ @io.wait_readable elsif status & MYSQL_WAIT_WRITE @io.wait_writable end end |