Class: Baza::Driver::Mysql2
- Inherits:
-
MysqlBaseDriver
- Object
- BaseSqlDriver
- MysqlBaseDriver
- Baza::Driver::Mysql2
- Defined in:
- lib/baza/driver/mysql2.rb
Defined Under Namespace
Classes: Column, Columns, Commands, Database, Databases, Index, Indexes, Result, Table, Tables, User, Users
Constant Summary
Constants inherited from BaseSqlDriver
BaseSqlDriver::SELECT_ARGS_ALLOWED_KEYS
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#conns ⇒ Object
readonly
Returns the value of attribute conns.
Attributes inherited from BaseSqlDriver
#cols, #db, #indexes, #sep_col, #sep_database, #sep_index, #sep_table, #sep_val, #tables
Class Method Summary collapse
-
.from_object(args) ⇒ Object
Helper to enable automatic registering of database using Baza::Db.from_object.
Instance Method Summary collapse
-
#clean ⇒ Object
Cleans the wref-map holding the tables.
-
#close ⇒ Object
Closes the connection threadsafe.
-
#destroy ⇒ Object
Destroyes the connection.
-
#escape(string) ⇒ Object
Escapes a string to be safe to use in a query.
-
#initialize(db) ⇒ Mysql2
constructor
A new instance of Mysql2.
-
#query(str) ⇒ Object
Executes a query and returns the result.
-
#query_ubuf(str, _args = nil, &_blk) ⇒ Object
Executes an unbuffered query and returns the result that can be used to access the data.
-
#reconnect ⇒ Object
Respawns the connection to the MySQL-database.
Methods inherited from MysqlBaseDriver
args, #insert_multi, #transaction
Methods inherited from BaseSqlDriver
#count, #delete, #escape_column, #escape_database, #escape_index, #escape_table, #insert, #insert_multi, #select, #single, #sql_make_where, #sqlval, #supports_multiple_databases?, #transaction
Constructor Details
#initialize(db) ⇒ Mysql2
Returns a new instance of Mysql2.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/baza/driver/mysql2.rb', line 26 def initialize(db) super @opts = @db.opts require "monitor" @mutex = Monitor.new if @opts[:encoding] @encoding = @opts[:encoding] else @encoding = "utf8" end if @db.opts.key?(:port) @port = @db.opts[:port].to_i else @port = 3306 end reconnect end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
6 7 8 |
# File 'lib/baza/driver/mysql2.rb', line 6 def conn @conn end |
#conns ⇒ Object (readonly)
Returns the value of attribute conns.
6 7 8 |
# File 'lib/baza/driver/mysql2.rb', line 6 def conns @conns end |
Class Method Details
.from_object(args) ⇒ Object
Helper to enable automatic registering of database using Baza::Db.from_object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/baza/driver/mysql2.rb', line 9 def self.from_object(args) if args[:object].class.name == "Mysql2::Client" return { type: :success, args: { type: :mysql2, conn: args[:object], query_args: { symbolize_keys: true } } } end nil end |
Instance Method Details
#clean ⇒ Object
Cleans the wref-map holding the tables.
50 51 52 |
# File 'lib/baza/driver/mysql2.rb', line 50 def clean tables.clean if tables end |
#close ⇒ Object
Closes the connection threadsafe.
131 132 133 |
# File 'lib/baza/driver/mysql2.rb', line 131 def close @mutex.synchronize { @conn.close } end |
#destroy ⇒ Object
Destroyes the connection.
136 137 138 139 140 141 142 143 |
# File 'lib/baza/driver/mysql2.rb', line 136 def destroy @conn = nil @db = nil @mutex = nil @encoding = nil @query_args = nil @port = nil end |
#escape(string) ⇒ Object
Escapes a string to be safe to use in a query.
126 127 128 |
# File 'lib/baza/driver/mysql2.rb', line 126 def escape(string) @conn.escape(string.to_s) end |
#query(str) ⇒ Object
Executes a query and returns the result.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/baza/driver/mysql2.rb', line 92 def query(str) str = str.to_s str = str.force_encoding("UTF-8") if @encoding == "utf8" && str.respond_to?(:force_encoding) tries = 0 begin tries += 1 @mutex.synchronize do return Baza::Driver::Mysql2::Result.new(self, @conn.query(str, @query_args)) end rescue => e if tries <= 3 if e. == "MySQL server has gone away" || e. == "closed MySQL connection" || e. == "Can't connect to local MySQL server through socket" sleep 0.5 reconnect retry elsif e..include?("No operations allowed after connection closed") || e. == "This connection is still waiting for a result, try again once you have the result" || e. == "Lock wait timeout exceeded; try restarting transaction" reconnect retry end end raise e end end |
#query_ubuf(str, _args = nil, &_blk) ⇒ Object
Executes an unbuffered query and returns the result that can be used to access the data.
119 120 121 122 123 |
# File 'lib/baza/driver/mysql2.rb', line 119 def query_ubuf(str, _args = nil, &_blk) @mutex.synchronize do return Baza::Driver::Mysql2::Result.new(self, @conn.query(str, @query_args.merge(stream: true))) end end |
#reconnect ⇒ Object
Respawns the connection to the MySQL-database.
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 |
# File 'lib/baza/driver/mysql2.rb', line 55 def reconnect @mutex.synchronize do require "mysql2" unless ::Object.const_defined?("Mysql2") args = { host: @db.opts[:host], username: @db.opts[:user], password: @db.opts[:pass], database: @db.opts[:db], port: @port, symbolize_keys: true, cache_rows: false } # Symbolize keys should also be given here, else table-data wont be symbolized for some reason - knj. @query_args = {symbolize_keys: true} @query_args[:cast] = false unless @db.opts[:type_translation] @query_args.merge!(@db.opts[:query_args]) if @db.opts[:query_args] pos_args = [:as, :async, :cast_booleans, :database_timezone, :application_timezone, :cache_rows, :connect_flags, :cast] pos_args.each do |key| args[key] = @db.opts[key] if @db.opts.key?(key) end args[:as] = :array if @db.opts[:conn] @conn = @db.opts[:conn] else @conn = ::Mysql2::Client.new(args) end query("SET NAMES '#{esc(@encoding)}'") if @encoding end end |