Class: Baza::Driver::Mysql

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

Defined Under Namespace

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

Instance Attribute Summary collapse

Attributes inherited from BaseSqlDriver

#baza, #cols, #indexes, #sep_col, #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

#escape, #escape_column, #insert, #insert_multi, #transaction

Constructor Details

#initialize(baza) ⇒ Mysql

Returns a new instance of Mysql.



22
23
24
25
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/drivers/mysql.rb', line 22

def initialize(baza)
  super

  @opts = @baza.opts

  require "monitor"
  @mutex = Monitor.new

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

    reconnect
  end
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



16
17
18
# File 'lib/baza/drivers/mysql.rb', line 16

def conn
  @conn
end

Class Method Details

.from_object(args) ⇒ Object



18
19
20
# File 'lib/baza/drivers/mysql.rb', line 18

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.



50
51
52
# File 'lib/baza/drivers/mysql.rb', line 50

def clean
  tables.clean if tables
end

#closeObject

Closes the connection threadsafe.



109
110
111
# File 'lib/baza/drivers/mysql.rb', line 109

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

#destroyObject

Destroyes the connection.



114
115
116
117
118
119
120
121
122
# File 'lib/baza/drivers/mysql.rb', line 114

def destroy
  @conn = nil
  @baza = 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.



99
100
101
# File 'lib/baza/drivers/mysql.rb', line 99

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

#last_idObject

Returns the last inserted ID for the connection.



104
105
106
# File 'lib/baza/drivers/mysql.rb', line 104

def last_id
  @mutex.synchronize { return @conn.insert_id.to_i }
end

#query(str) ⇒ Object

Executes a query and returns the result.



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

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.



91
92
93
94
95
96
# File 'lib/baza/drivers/mysql.rb', line 91

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.



55
56
57
58
59
60
61
# File 'lib/baza/drivers/mysql.rb', line 55

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

#supports_multiple_databases?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/baza/drivers/mysql.rb', line 124

def supports_multiple_databases?
  true
end