Class: Mysql
- Inherits:
-
Object
- Object
- Mysql
- Defined in:
- lib/mysql.rb,
lib/mysql/charset.rb
Overview
MySQL connection class.
Defined Under Namespace
Classes: Charset, Field, Result, ResultBase, Stmt
Constant Summary collapse
- VERSION =
Version number of this library
'3.0.1'
- MYSQL_UNIX_PORT =
UNIX domain socket filename
"/tmp/mysql.sock"
- MYSQL_TCP_PORT =
TCP socket port number
3306
- DEFAULT_OPTS =
{ host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze
Instance Attribute Summary collapse
- #connect_timeout ⇒ Numeric?
- #database ⇒ String?
- #flags ⇒ Integer?
- #get_server_public_key ⇒ Boolean
- #host ⇒ String?
- #init_command ⇒ String?
- #load_data_local_dir ⇒ String?
- #local_infile ⇒ Boolean
- #password ⇒ String?
- #port ⇒ Integer, ...
- #read_timeout ⇒ Numeric?
-
#socket ⇒ String?
Socket filename.
-
#ssl_mode ⇒ String, Integer
1 or “disabled” / 2 or “preferred” / 3 or “required”.
- #username ⇒ String?
- #write_timeout ⇒ Numeric?
Class Method Summary collapse
-
.connect(*args, **opts) ⇒ Mysql
Make Mysql object and connect to mysqld.
-
.escape_string(str) ⇒ String
Escape special character in string.
-
.quote ⇒ String
Escape special character in string.
Instance Method Summary collapse
-
#affected_rows ⇒ Integer
Number of affected records by insert/update/delete.
-
#autocommit(flag) ⇒ Mysql
Set autocommit mode.
-
#character_set_name ⇒ String
Charset name.
-
#charset ⇒ Mysql::Charset
Character set of MySQL connection.
-
#charset=(cs) ⇒ Object
Set charset of MySQL connection.
-
#close ⇒ Mysql
Disconnect from mysql.
-
#close! ⇒ Mysql
Disconnect from mysql without QUIT packet.
-
#commit ⇒ Mysql
Commit transaction.
-
#connect(*args, **opts) ⇒ Mysql
Connect to mysqld.
-
#errno ⇒ Integer
Last error number.
-
#error ⇒ String
Last error message.
-
#escape_string(str) ⇒ Object
(also: #quote)
Escape special character in MySQL.
-
#field_count ⇒ Integer
Number of columns for last query.
-
#host_info ⇒ String
Connection type.
-
#info ⇒ String
Information for last query.
-
#initialize(*args, **opts) ⇒ Mysql
constructor
A new instance of Mysql.
-
#insert_id ⇒ Integer
Latest auto_increment value.
-
#kill(pid) ⇒ Mysql
Kill query.
-
#more_results ⇒ Boolean
(also: #more_results?)
True if multiple queries are specified and unexecuted queries exists.
-
#next_result ⇒ Boolean
execute next query if multiple queries are specified.
- #parse_args(args, opts) ⇒ Object
-
#ping ⇒ Mysql
Check whether the connection is available.
-
#prepare(str) ⇒ Mysql::Stmt
Parse prepared-statement.
-
#query(str, &block) ⇒ Object
Execute query string.
-
#refresh(op) ⇒ Mysql
Flush tables or caches.
-
#reload ⇒ Mysql
Reload grant tables.
-
#rollback ⇒ Mysql
Rollback transaction.
-
#select_db(db) ⇒ Mysql
Select default database.
-
#server_info ⇒ String
Server version.
-
#server_version ⇒ Integer
Server version.
-
#set_server_option(opt) ⇒ Mysql
Set server option.
-
#shutdown(level = 0) ⇒ Mysql
shutdown server.
-
#sqlstate ⇒ String
Sqlstate for last error.
-
#stat ⇒ String
Statistics message.
-
#store_result ⇒ Mysql::Result
Get all data for last query.
-
#thread_id ⇒ Integer
Thread ID.
-
#warning_count ⇒ Integer
Number of warnings for previous query.
Constructor Details
#initialize(uri, **opts) ⇒ Mysql #initialize(host, username, password, database, port, socket, flags, **opts) ⇒ Mysql #initialize(host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: nil, **opts) ⇒ Mysql
Returns a new instance of Mysql.
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/mysql.rb', line 139 def initialize(*args, **opts) @fields = nil @protocol = nil @sqlstate = "00000" @host_info = nil @last_error = nil @result_exist = false @opts = DEFAULT_OPTS.dup parse_args(args, opts) end |
Instance Attribute Details
#connect_timeout ⇒ Numeric?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#database ⇒ String?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#flags ⇒ Integer?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#get_server_public_key ⇒ Boolean
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#host ⇒ String?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#init_command ⇒ String?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#load_data_local_dir ⇒ String?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#local_infile ⇒ Boolean
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#password ⇒ String?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#port ⇒ Integer, ...
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#read_timeout ⇒ Numeric?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#socket ⇒ String?
Returns socket filename.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#ssl_mode ⇒ String, Integer
Returns 1 or “disabled” / 2 or “preferred” / 3 or “required”.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#username ⇒ String?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
#write_timeout ⇒ Numeric?
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mysql.rb', line 56 DEFAULT_OPTS = { host: nil, username: nil, password: nil, database: nil, port: nil, socket: nil, flags: 0, charset: nil, connect_timeout: nil, read_timeout: nil, write_timeout: nil, init_command: nil, local_infile: nil, load_data_local_dir: nil, ssl_mode: SSL_MODE_PREFERRED, get_server_public_key: false, }.freeze |
Class Method Details
.connect(*args, **opts) ⇒ Mysql
Make Mysql object and connect to mysqld. parameter is same as arguments for #initialize.
82 83 84 |
# File 'lib/mysql.rb', line 82 def connect(*args, **opts) self.new(*args, **opts).connect end |
.escape_string(str) ⇒ String
Escape special character in string.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mysql.rb', line 89 def escape_string(str) str.gsub(/[\0\n\r\\\'\"\x1a]/) do |s| case s when "\0" then "\\0" when "\n" then "\\n" when "\r" then "\\r" when "\x1a" then "\\Z" else "\\#{s}" end end end |
.quote ⇒ String
Escape special character in string.
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mysql.rb', line 100 def escape_string(str) str.gsub(/[\0\n\r\\\'\"\x1a]/) do |s| case s when "\0" then "\\0" when "\n" then "\\n" when "\r" then "\\r" when "\x1a" then "\\Z" else "\\#{s}" end end end |
Instance Method Details
#affected_rows ⇒ Integer
Returns number of affected records by insert/update/delete.
306 307 308 |
# File 'lib/mysql.rb', line 306 def affected_rows @protocol ? @protocol.affected_rows : 0 end |
#autocommit(flag) ⇒ Mysql
Set autocommit mode
486 487 488 489 |
# File 'lib/mysql.rb', line 486 def autocommit(flag) query "set autocommit=#{flag ? 1 : 0}" self end |
#character_set_name ⇒ String
Returns charset name.
259 260 261 |
# File 'lib/mysql.rb', line 259 def character_set_name @protocol.charset.name end |
#charset ⇒ Mysql::Charset
Returns character set of MySQL connection.
242 243 244 |
# File 'lib/mysql.rb', line 242 def charset @opts[:charset] end |
#charset=(cs) ⇒ Object
Set charset of MySQL connection.
248 249 250 251 252 253 254 255 256 |
# File 'lib/mysql.rb', line 248 def charset=(cs) charset = cs.is_a?(Charset) ? cs : Charset.by_name(cs) if @protocol @protocol.charset = charset query "SET NAMES #{charset.name}" end @opts[:charset] = charset cs end |
#close ⇒ Mysql
Disconnect from mysql.
214 215 216 217 218 219 220 |
# File 'lib/mysql.rb', line 214 def close if @protocol @protocol.quit_command @protocol = nil end return self end |
#close! ⇒ Mysql
Disconnect from mysql without QUIT packet.
224 225 226 227 228 229 230 |
# File 'lib/mysql.rb', line 224 def close! if @protocol @protocol.close @protocol = nil end return self end |
#commit ⇒ Mysql
Commit transaction
471 472 473 474 |
# File 'lib/mysql.rb', line 471 def commit query 'commit' self end |
#connect(*args, **opts) ⇒ Mysql
Connect to mysqld. parameter is same as arguments for #initialize.
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mysql.rb', line 153 def connect(*args, **opts) parse_args(args, opts) if @opts[:flags] & CLIENT_COMPRESS != 0 warn 'unsupported flag: CLIENT_COMPRESS' if $VERBOSE @opts[:flags] &= ~CLIENT_COMPRESS end @protocol = Protocol.new(@opts) @protocol.authenticate @host_info = (@opts[:host].nil? || @opts[:host] == "localhost") ? 'Localhost via UNIX socket' : "#{@opts[:host]} via TCP/IP" query @opts[:init_command] if @opts[:init_command] return self end |
#errno ⇒ Integer
Returns last error number.
264 265 266 |
# File 'lib/mysql.rb', line 264 def errno @last_error ? @last_error.errno : 0 end |
#error ⇒ String
Returns last error message.
269 270 271 |
# File 'lib/mysql.rb', line 269 def error @last_error && @last_error.error end |
#escape_string(str) ⇒ Object Also known as: quote
Escape special character in MySQL.
return [String]
236 237 238 |
# File 'lib/mysql.rb', line 236 def escape_string(str) self.class.escape_string str end |
#field_count ⇒ Integer
Returns number of columns for last query.
279 280 281 |
# File 'lib/mysql.rb', line 279 def field_count @fields.size end |
#host_info ⇒ String
Returns connection type.
284 285 286 |
# File 'lib/mysql.rb', line 284 def host_info @host_info end |
#info ⇒ String
Returns information for last query.
301 302 303 |
# File 'lib/mysql.rb', line 301 def info @protocol && @protocol. end |
#insert_id ⇒ Integer
Returns latest auto_increment value.
311 312 313 |
# File 'lib/mysql.rb', line 311 def insert_id @protocol ? @protocol.insert_id : 0 end |
#kill(pid) ⇒ Mysql
Kill query.
323 324 325 326 327 |
# File 'lib/mysql.rb', line 323 def kill(pid) check_connection @protocol.kill_command pid self end |
#more_results ⇒ Boolean Also known as: more_results?
Returns true if multiple queries are specified and unexecuted queries exists.
391 392 393 |
# File 'lib/mysql.rb', line 391 def more_results @protocol.server_status & SERVER_MORE_RESULTS_EXISTS != 0 end |
#next_result ⇒ Boolean
execute next query if multiple queries are specified.
398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/mysql.rb', line 398 def next_result return false unless more_results check_connection @fields = nil nfields = @protocol.get_result if nfields @fields = @protocol.retr_fields nfields @result_exist = true end return true end |
#parse_args(args, opts) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/mysql.rb', line 166 def parse_args(args, opts) case args[0] when URI uri = args[0] when /\Amysql:\/\// uri = URI.parse(args[0]) when String @opts[:host], user, passwd, dbname, port, socket, flags = *args @opts[:username] = user if user @opts[:password] = passwd if passwd @opts[:database] = dbname if dbname @opts[:port] = port if port @opts[:socket] = socket if socket @opts[:flags] = flags if flags when Hash # skip when nil # skip end if uri host = uri.hostname.to_s host = URI.decode_www_form_component(host) if host.start_with?('/') @opts[:socket] = host host = '' end @opts[:host] = host @opts[:username] = URI.decode_www_form_component(uri.user.to_s) @opts[:password] = URI.decode_www_form_component(uri.password.to_s) @opts[:database] = uri.path.sub(/\A\/+/, '') @opts[:port] = uri.port opts = URI.decode_www_form(uri.query).to_h.transform_keys(&:intern).merge(opts) if uri.query opts[:flags] = opts[:flags].to_i if opts[:flags] end if args.last.kind_of? Hash opts = opts.merge(args.last) end @opts.update(opts) end |
#ping ⇒ Mysql
Check whether the connection is available.
428 429 430 431 432 |
# File 'lib/mysql.rb', line 428 def ping check_connection @protocol.ping_command self end |
#prepare(str) ⇒ Mysql::Stmt
Parse prepared-statement.
413 414 415 416 417 |
# File 'lib/mysql.rb', line 413 def prepare(str) st = Stmt.new @protocol st.prepare str st end |
#query(str) ⇒ Mysql::Result? #query(str) {|Mysql::Result| ... } ⇒ self
Execute query string.
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/mysql.rb', line 341 def query(str, &block) check_connection @fields = nil begin nfields = @protocol.query_command str if nfields @fields = @protocol.retr_fields nfields @result_exist = true end if block while true block.call store_result if @fields break unless next_result end return self end return @fields ? store_result : nil rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end end |
#refresh(op) ⇒ Mysql
Flush tables or caches.
437 438 439 440 441 |
# File 'lib/mysql.rb', line 437 def refresh(op) check_connection @protocol.refresh_command op self end |
#reload ⇒ Mysql
Reload grant tables.
445 446 447 |
# File 'lib/mysql.rb', line 445 def reload refresh Mysql::REFRESH_GRANT end |
#rollback ⇒ Mysql
Rollback transaction
478 479 480 481 |
# File 'lib/mysql.rb', line 478 def rollback query 'rollback' self end |
#select_db(db) ⇒ Mysql
Select default database
451 452 453 454 |
# File 'lib/mysql.rb', line 451 def select_db(db) query "use #{db}" self end |
#server_info ⇒ String
Returns server version.
289 290 291 292 |
# File 'lib/mysql.rb', line 289 def server_info check_connection @protocol.server_info end |
#server_version ⇒ Integer
Returns server version.
295 296 297 298 |
# File 'lib/mysql.rb', line 295 def server_version check_connection @protocol.server_version end |
#set_server_option(opt) ⇒ Mysql
Set server option.
384 385 386 387 388 |
# File 'lib/mysql.rb', line 384 def set_server_option(opt) check_connection @protocol.set_option_command opt self end |
#shutdown(level = 0) ⇒ Mysql
shutdown server.
458 459 460 461 462 |
# File 'lib/mysql.rb', line 458 def shutdown(level=0) check_connection @protocol.shutdown_command level self end |
#sqlstate ⇒ String
Returns sqlstate for last error.
274 275 276 |
# File 'lib/mysql.rb', line 274 def sqlstate @last_error ? @last_error.sqlstate : "00000" end |
#stat ⇒ String
Returns statistics message.
465 466 467 |
# File 'lib/mysql.rb', line 465 def stat @protocol ? @protocol.statistics_command : 'MySQL server has gone away' end |
#store_result ⇒ Mysql::Result
Get all data for last query.
367 368 369 370 371 372 373 |
# File 'lib/mysql.rb', line 367 def store_result check_connection raise ClientError, 'invalid usage' unless @result_exist res = Result.new @fields, @protocol @result_exist = false res end |
#thread_id ⇒ Integer
Returns Thread ID.
376 377 378 379 |
# File 'lib/mysql.rb', line 376 def thread_id check_connection @protocol.thread_id end |
#warning_count ⇒ Integer
Returns number of warnings for previous query.
316 317 318 |
# File 'lib/mysql.rb', line 316 def warning_count @protocol ? @protocol.warning_count : 0 end |