Class: Baza::Driver::Mysql

Inherits:
MysqlBaseDriver show all
Defined in:
lib/baza/driver/mysql.rb

Defined Under Namespace

Classes: Column, Columns, Commands, Database, Databases, Index, Indexes, Result, Sqlspecs, Table, Tables, UnbufferedResult, User, Users

Constant Summary

Constants inherited from BaseSqlDriver

BaseSqlDriver::SELECT_ARGS_ALLOWED_KEYS

Instance Attribute Summary collapse

Attributes inherited from BaseSqlDriver

#cols, #db, #indexes, #sep_col, #sep_database, #sep_index, #sep_table, #sep_val, #tables

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MysqlBaseDriver

args, #insert_multi, #transaction

Methods inherited from BaseSqlDriver

#count, #delete, #escape, #escape_column, #escape_database, #escape_index, #escape_table, #insert, #insert_multi, #select, #single, #sql_make_where, #sqlval, #transaction

Constructor Details

#initialize(db) ⇒ Mysql

Returns a new instance of Mysql.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/baza/driver/mysql.rb', line 10

def initialize(db)
  super

  @opts = @db.opts

  require "monitor"
  @mutex = Monitor.new

  if db.opts[:conn]
    @conn = db.opts[:conn]
  else
    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
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



4
5
6
# File 'lib/baza/driver/mysql.rb', line 4

def conn
  @conn
end

Class Method Details

.from_object(args) ⇒ Object



6
7
8
# File 'lib/baza/driver/mysql.rb', line 6

def self.from_object(args)
  raise "Mysql does not support auth extraction" if args[:object].class.name == "Mysql"
end

Instance Method Details

#cleanObject

Cleans the wref-map holding the tables.



38
39
40
# File 'lib/baza/driver/mysql.rb', line 38

def clean
  tables.clean if tables
end

#closeObject

Closes the connection threadsafe.



92
93
94
# File 'lib/baza/driver/mysql.rb', line 92

def close
  @mutex.synchronize { @conn.close }
end

#destroyObject

Destroyes the connection.



97
98
99
100
101
102
103
104
105
# File 'lib/baza/driver/mysql.rb', line 97

def destroy
  @conn = nil
  @db = nil
  @mutex = nil
  @subtype = nil
  @encoding = nil
  @query_args = nil
  @port = nil
end

#escape_alternative(string) ⇒ Object

Escapes a string to be safe to use in a query.



87
88
89
# File 'lib/baza/driver/mysql.rb', line 87

def escape_alternative(string)
  @conn.escape_string(string.to_s)
end

#query(str) ⇒ Object

Executes a query and returns the result.



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
# File 'lib/baza/driver/mysql.rb', line 52

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::Mysql::Result.new(self, @conn.query(str))
    end
  rescue => e
    if tries <= 3
      if e.message == "MySQL server has gone away" || e.message == "closed MySQL connection" || e.message == "Can't connect to local MySQL server through socket"
        sleep 0.5
        reconnect
        retry
      elsif e.message.include?("No operations allowed after connection closed") || e.message == "This connection is still waiting for a result, try again once you have the result" || e.message == "Lock wait timeout exceeded; try restarting transaction"
        reconnect
        retry
      end
    end

    raise e
  end
end

#query_ubuf(str) ⇒ Object

Executes an unbuffered query and returns the result that can be used to access the data.



79
80
81
82
83
84
# File 'lib/baza/driver/mysql.rb', line 79

def query_ubuf(str)
  @mutex.synchronize do
    @conn.query_with_result = false
    return Baza::Driver::Mysql::UnbufferedResult.new(@conn, @opts, @conn.query(str))
  end
end

#reconnectObject

Respawns the connection to the MySQL-database.



43
44
45
46
47
48
49
# File 'lib/baza/driver/mysql.rb', line 43

def reconnect
  @mutex.synchronize do
    require "mysql" unless ::Object.const_defined?(:Mysql)
    @conn = ::Mysql.real_connect(@db.opts[:host], @db.opts[:user], @db.opts[:pass], @db.opts[:db], @port)
    query("SET NAMES '#{esc(@encoding)}'") if @encoding
  end
end

#supports_multiple_databases?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/baza/driver/mysql.rb', line 107

def supports_multiple_databases?
  true
end