Class: KnjDB_mysql
Defined Under Namespace
Classes: Columns, Indexes, Tables
Instance Attribute Summary collapse
-
#cols ⇒ Object
Returns the value of attribute cols.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#conns ⇒ Object
readonly
Returns the value of attribute conns.
-
#escape_col ⇒ Object
readonly
Returns the value of attribute escape_col.
-
#escape_table ⇒ Object
readonly
Returns the value of attribute escape_table.
-
#escape_val ⇒ Object
readonly
Returns the value of attribute escape_val.
-
#indexes ⇒ Object
Returns the value of attribute indexes.
-
#knjdb ⇒ Object
readonly
Returns the value of attribute knjdb.
-
#tables ⇒ Object
Returns the value of attribute tables.
Instance Method Summary collapse
- #clean ⇒ Object
-
#close ⇒ Object
Closes the connection threadsafe.
-
#destroy ⇒ Object
Destroyes the connection.
-
#esc_col(string) ⇒ Object
(also: #esc_table)
Escapes a string to be safe to use as a column in a query.
-
#escape(string) ⇒ Object
(also: #esc)
An alternative to the MySQL framework’s escape.
-
#escape_alternative(string) ⇒ Object
Escapes a string to be safe to use in a query.
-
#initialize(knjdb_ob) ⇒ KnjDB_mysql
constructor
A new instance of KnjDB_mysql.
- #insert_multi(tablename, arr_hashes) ⇒ Object
-
#java_mysql_resultset_killer(id) ⇒ Object
This method handels the closing of statements and results for the Java MySQL-mode.
-
#lastID ⇒ Object
Returns the last inserted ID for the connection.
-
#query(str) ⇒ Object
Executes a query and returns the result.
-
#query_ubuf(str) ⇒ Object
Executes an unbuffered query and returns the result that can be used to access the data.
- #reconnect ⇒ Object
Constructor Details
#initialize(knjdb_ob) ⇒ KnjDB_mysql
Returns a new instance of KnjDB_mysql.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 5 def initialize(knjdb_ob) @knjdb = knjdb_ob @opts = @knjdb.opts @encoding = @opts[:encoding] @escape_table = "`" @escape_col = "`" @escape_val = "'" @esc_table = "`" @esc_col = "`" @mutex = Mutex.new if @knjdb.opts.key?(:port) @port = @knjdb.opts[:port].to_i else @port = 3306 end @java_rs_data = {} @subtype = @knjdb.opts[:subtype] @subtype = "mysql" if @subtype.to_s.length <= 0 self.reconnect end |
Instance Attribute Details
#cols ⇒ Object
Returns the value of attribute cols.
3 4 5 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 3 def cols @cols end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
2 3 4 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 2 def conn @conn end |
#conns ⇒ Object (readonly)
Returns the value of attribute conns.
2 3 4 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 2 def conns @conns end |
#escape_col ⇒ Object (readonly)
Returns the value of attribute escape_col.
2 3 4 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 2 def escape_col @escape_col end |
#escape_table ⇒ Object (readonly)
Returns the value of attribute escape_table.
2 3 4 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 2 def escape_table @escape_table end |
#escape_val ⇒ Object (readonly)
Returns the value of attribute escape_val.
2 3 4 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 2 def escape_val @escape_val end |
#indexes ⇒ Object
Returns the value of attribute indexes.
3 4 5 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 3 def indexes @indexes end |
#knjdb ⇒ Object (readonly)
Returns the value of attribute knjdb.
2 3 4 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 2 def knjdb @knjdb end |
#tables ⇒ Object
Returns the value of attribute tables.
3 4 5 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 3 def tables @tables end |
Instance Method Details
#clean ⇒ Object
38 39 40 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 38 def clean self.tables.clean if self.tables end |
#close ⇒ Object
Closes the connection threadsafe.
267 268 269 270 271 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 267 def close @mutex.synchronize do @conn.close end end |
#destroy ⇒ Object
Destroyes the connection.
274 275 276 277 278 279 280 281 282 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 274 def destroy @conn = nil @knjdb = nil @mutex = nil @subtype = nil @encoding = nil @query_args = nil @port = nil end |
#esc_col(string) ⇒ Object Also known as: esc_table
Escapes a string to be safe to use as a column in a query.
239 240 241 242 243 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 239 def esc_col(string) string = string.to_s raise "Invalid column-string: #{string}" if string.index(@escape_col) != nil return string end |
#escape(string) ⇒ Object Also known as: esc
An alternative to the MySQL framework’s escape. This is copied from the Ruby/MySQL framework at: www.tmtm.org/en/ruby/mysql/
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 226 def escape(string) return string.to_s.gsub(/([\0\n\r\032\'\"\\])/) do case $1 when "\0" then "\\0" when "\n" then "\\n" when "\r" then "\\r" when "\032" then "\\Z" else "\\" + $1 end end end |
#escape_alternative(string) ⇒ Object
Escapes a string to be safe to use in a query.
212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 212 def escape_alternative(string) case @subtype when "mysql" return @conn.escape_string(string.to_s) when "mysql2" return @conn.escape(string.to_s) when "java" return self.escape(string) else raise "Unknown subtype: '#{@subtype}'." end end |
#insert_multi(tablename, arr_hashes) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 284 def insert_multi(tablename, arr_hashes) sql = "INSERT INTO `#{self.esc_table(tablename)}` (" first = true arr_hashes.first.keys.each do |col_name| sql << "," if !first first = false if first sql << "`#{self.esc_col(col_name)}`" end sql << ") VALUES (" first = true arr_hashes.each do |hash| if first first = false else sql << "),(" end first_key = true hash.each do |key, val| if first_key first_key = false else sql << "," end sql << "'#{self.escape(val)}'" end end sql << ")" self.query(sql) end |
#java_mysql_resultset_killer(id) ⇒ Object
This method handels the closing of statements and results for the Java MySQL-mode.
29 30 31 32 33 34 35 36 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 29 def java_mysql_resultset_killer(id) data = @java_rs_data[id] return nil if !data data[:res].close data[:stmt].close @java_rs_data.delete(id) end |
#lastID ⇒ Object
Returns the last inserted ID for the connection.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 249 def lastID case @subtype when "mysql" @mutex.synchronize do return @conn.insert_id end when "mysql2" @mutex.synchronize do return @conn.last_id end when "java" data = self.query("SELECT LAST_INSERT_ID() AS id").fetch return data[:id] if data.key?(:id) raise "Could not figure out last inserted ID." end end |
#query(str) ⇒ Object
Executes a query and returns the result.
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 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 101 def query(str) str = str.to_s str = str.force_encoding("UTF-8") if @encoding == "utf8" and str.respond_to?(:force_encoding) tries = 0 begin tries += 1 @mutex.synchronize do case @subtype when "mysql" return KnjDB_mysql_result.new(self, @conn.query(str)) when "mysql2" return KnjDB_mysql2_result.new(@conn.query(str, @query_args)) when "java" stmt = conn.create_statement if str.match(/^\s*(delete|update|create|drop|insert\s+into|alter)\s+/i) begin stmt.execute(str) ensure stmt.close end return nil else begin res = stmt.execute_query(str) ret = KnjDB_java_mysql_result.new(@knjdb, @opts, res) #Save reference to result and statement, so we can close them when they are garbage collected. @java_rs_data[ret.__id__] = {:res => res, :stmt => stmt} ObjectSpace.define_finalizer(ret, self.method("java_mysql_resultset_killer")) return ret rescue => e res.close if res stmt.close raise e end end else raise "Unknown subtype: '#{@subtype}'." end end rescue => e if tries < 3 if e. == "MySQL server has gone away" or e. == "closed MySQL connection" or e. == "Can't connect to local MySQL server through socket" sleep 0.5 self.reconnect retry elsif e. == "This connection is still waiting for a result, try again once you have the result" sleep 0.1 retry elsif e.to_s.index("No operations allowed after connection closed") != nil self.reconnect retry end end print str raise e end end |
#query_ubuf(str) ⇒ Object
Executes an unbuffered query and returns the result that can be used to access the data.
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 205 206 207 208 209 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 166 def query_ubuf(str) @mutex.synchronize do case @subtype when "mysql" @conn.query_with_result = false return KnjDB_mysql_unbuffered_result.new(@conn, @opts, @conn.query(str)) when "mysql2" raise "MySQL2 does not support unbuffered queries yet! Waiting for :stream..." when "java" if str.match(/^\s*(delete|update|create|drop|insert\s+into)\s+/i) stmt = @conn.createStatement begin stmt.execute(str) ensure stmt.close end return nil else stmt = @conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY) stmt.setFetchSize(java.lang.Integer::MIN_VALUE) begin res = stmt.executeQuery(str) ret = KnjDB_java_mysql_result.new(@knjdb, @opts, res) #Save reference to result and statement, so we can close them when they are garbage collected. @java_rs_data[ret.__id__] = {:res => res, :stmt => stmt} ObjectSpace.define_finalizer(ret, self.method("java_mysql_resultset_killer")) return ret rescue => e res.close if res stmt.close raise e end end raise "Not implemented yet." else raise "Unknown subtype: '#{@subtype}'" end end end |
#reconnect ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 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 95 96 97 98 |
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 42 def reconnect case @subtype when "mysql" @conn = Mysql.real_connect(@knjdb.opts[:host], @knjdb.opts[:user], @knjdb.opts[:pass], @knjdb.opts[:db], @port) when "mysql2" require "rubygems" require "mysql2" args = { :host => @knjdb.opts[:host], :username => @knjdb.opts[:user], :password => @knjdb.opts[:pass], :database => @knjdb.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.merge!(@knjdb.opts[:query_args]) if @knjdb.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] = @knjdb.opts[key] if @knjdb.opts.key?(key) end tries = 0 begin tries += 1 @conn = Mysql2::Client.new(args) rescue => e if tries <= 3 if e. == "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)" sleep 1 retry end end raise e end when "java" if !@jdbc_loaded require "java" require "/usr/share/java/mysql-connector-java.jar" if File.exists?("/usr/share/java/mysql-connector-java.jar") import "com.mysql.jdbc.Driver" @jdbc_loaded = true end @conn = java.sql::DriverManager.getConnection("jdbc:mysql://#{@knjdb.opts[:host]}:#{@port}/#{@knjdb.opts[:db]}?user=#{@knjdb.opts[:user]}&password=#{@knjdb.opts[:pass]}&populateInsertRowWithDefaultValues=true&zeroDateTimeBehavior=round") query("SET SQL_MODE = ''") else raise "Unknown subtype: #{@subtype}" end self.query("SET NAMES '#{self.esc(@encoding)}'") if @encoding end |