Class: Baza::Driver::MysqlJava
- Inherits:
-
JdbcDriver
- Object
- BaseSqlDriver
- JdbcDriver
- Baza::Driver::MysqlJava
- Defined in:
- lib/baza/drivers/mysql_java.rb
Defined Under Namespace
Classes: Column, Columns, Index, Indexes, Table, Tables
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
#baza, #cols, #indexes, #sep_col, #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
-
#close ⇒ Object
Closes the connection threadsafe.
-
#destroy ⇒ Object
Destroyes the connection.
-
#initialize(baza) ⇒ MysqlJava
constructor
A new instance of MysqlJava.
-
#insert_multi(tablename, arr_hashes, args = nil) ⇒ Object
Inserts multiple rows in a table.
-
#last_id ⇒ Object
Returns the last inserted ID for the connection.
-
#reconnect ⇒ Object
Respawns the connection to the MySQL-database.
- #transaction ⇒ Object
Methods inherited from JdbcDriver
#query, #query_ubuf, #result_set_killer
Methods inherited from BaseSqlDriver
#escape, #escape_column, #insert, #supports_multiple_databases?
Constructor Details
#initialize(baza) ⇒ MysqlJava
Returns a new instance of MysqlJava.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/baza/drivers/mysql_java.rb', line 35 def initialize(baza) super @opts = @baza.opts if @opts[:encoding] @encoding = @opts[:encoding] else @encoding = "utf8" end if @baza.opts.key?(:port) @port = @baza.opts[:port].to_i else @port = 3306 end @java_rs_data = {} reconnect end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
18 19 20 |
# File 'lib/baza/drivers/mysql_java.rb', line 18 def conn @conn end |
#conns ⇒ Object (readonly)
Returns the value of attribute conns.
18 19 20 |
# File 'lib/baza/drivers/mysql_java.rb', line 18 def conns @conns end |
Class Method Details
.from_object(args) ⇒ Object
Helper to enable automatic registering of database using Baza::Db.from_object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/baza/drivers/mysql_java.rb', line 21 def self.from_object(args) if args[:object].class.name == "Java::ComMysqlJdbc::JDBC4Connection" return { type: :success, args: { type: :mysql_java, conn: args[:object] } } end nil end |
Instance Method Details
#close ⇒ Object
Closes the connection threadsafe.
80 81 82 |
# File 'lib/baza/drivers/mysql_java.rb', line 80 def close @mutex.synchronize { @conn.close } end |
#destroy ⇒ Object
Destroyes the connection.
85 86 87 88 89 90 91 92 |
# File 'lib/baza/drivers/mysql_java.rb', line 85 def destroy @conn = nil @baza = nil @mutex = nil @encoding = nil @query_args = nil @port = nil end |
#insert_multi(tablename, arr_hashes, args = nil) ⇒ Object
Inserts multiple rows in a table. Can return the inserted IDs if asked to in arguments.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/baza/drivers/mysql_java.rb', line 95 def insert_multi(tablename, arr_hashes, args = nil) sql = "INSERT INTO `#{tablename}` (" first = true if args && args[:keys] keys = args[:keys] elsif arr_hashes.first.is_a?(Hash) keys = arr_hashes.first.keys else raise "Could not figure out keys." end keys.each do |col_name| sql << "," unless first first = false if first sql << "`#{escape_column(col_name)}`" end sql << ") VALUES (" first = true arr_hashes.each do |hash| if first first = false else sql << "),(" end first_key = true if hash.is_a?(Array) hash.each do |val| if first_key first_key = false else sql << "," end sql << @baza.sqlval(val) end else hash.each do |_key, val| if first_key first_key = false else sql << "," end sql << @baza.sqlval(val) end end end sql << ")" return sql if args && args[:return_sql] query_no_result_set(sql) if args && args[:return_id] first_id = last_id raise "Invalid ID: #{first_id}" if first_id.to_i <= 0 ids = [first_id] 1.upto(arr_hashes.length - 1) do |count| ids << first_id + count end ids_length = ids.length arr_hashes_length = arr_hashes.length raise "Invalid length (#{ids_length}, #{arr_hashes_length})." if ids_length != arr_hashes_length return ids else return nil end end |
#last_id ⇒ Object
Returns the last inserted ID for the connection.
73 74 75 76 77 |
# File 'lib/baza/drivers/mysql_java.rb', line 73 def last_id data = query("SELECT LAST_INSERT_ID() AS id").fetch return data[:id].to_i if data[:id] raise "Could not figure out last inserted ID." end |
#reconnect ⇒ Object
Respawns the connection to the MySQL-database.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/baza/drivers/mysql_java.rb', line 57 def reconnect @mutex.synchronize do if @baza.opts[:conn] @jdbc_loaded = true @conn = @baza.opts[:conn] else com.mysql.jdbc.Driver @conn = java.sql::DriverManager.getConnection("jdbc:mysql://#{@baza.opts[:host]}:#{@port}/#{@baza.opts[:db]}?user=#{@baza.opts[:user]}&password=#{@baza.opts[:pass]}&populateInsertRowWithDefaultValues=true&zeroDateTimeBehavior=round&characterEncoding=#{@encoding}&holdResultsOpenOverStatementClose=true") end query_no_result_set("SET SQL_MODE = ''") query_no_result_set("SET NAMES '#{esc(@encoding)}'") if @encoding end end |
#transaction ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/baza/drivers/mysql_java.rb', line 171 def transaction query_no_result_set("START TRANSACTION") begin yield @baza query_no_result_set("COMMIT") rescue query_no_result_set("ROLLBACK") raise end end |