Class: Lamed::MySQL

Inherits:
Mysql
  • Object
show all
Defined in:
lib/lamed/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ MySQL

Returns a new instance of MySQL.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lamed/model.rb', line 42

def initialize(params = {})
  @params        = params
  @host          = params[:host]      || '127.0.0.1'
  @port          = (params[:port]     || 3306).to_i
  @user          = params[:username]  || 'root'
  @password      = params[:password]  || 'pwd'
  @database      = params[:database]
  @read_host     = params[:read_host]
  @read_username = params[:read_user]
  @read_password = params[:read_password]
  @read_database = params[:read_password]
  @read_port     = params[:read_port]
  self.connect
end

Instance Attribute Details

#db_connObject (readonly)

Returns the value of attribute db_conn.



40
41
42
# File 'lib/lamed/model.rb', line 40

def db_conn
  @db_conn
end

#db_conn_readObject (readonly)

Returns the value of attribute db_conn_read.



40
41
42
# File 'lib/lamed/model.rb', line 40

def db_conn_read
  @db_conn_read
end

#paramsObject (readonly)

Returns the value of attribute params.



40
41
42
# File 'lib/lamed/model.rb', line 40

def params
  @params
end

#statusObject (readonly)

Returns the value of attribute status.



40
41
42
# File 'lib/lamed/model.rb', line 40

def status
  @status
end

Class Method Details

.query(query_string) ⇒ Object



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
# File 'lib/lamed/model.rb', line 67

def self.query(query_string)
  n = 0							# track how many times the system had to reconnect to the db
  begin
    # Test to see if the query starts with a select which would mean it was a read query
    if query_string.split[0].upcase == "SELECT"
      res = $db_conn_read.query(query_string)
    else
      res = $db_conn.query(query_string)
    end
  rescue Mysql::Error => e
    case e.to_s
      when 'MySQL server has gone away'
        MySQL.new($DB_OPTIONS)
        n += 1
        retry
      when 'Lost connection to MySQL server during query'
        MySQL.new($DB_OPTIONS) 
        n += 1
        retry
      else
        # Don't know what to do because of an unknown error so to play it safe we'll just break instead looping endlessly.
        raise "ERROR: #{e.to_s} Not sure what this error is from #{@host}."
    end
  end
  return res
end

Instance Method Details

#connectObject



57
58
59
60
61
62
63
64
65
# File 'lib/lamed/model.rb', line 57

def connect
  $db_conn = Mysql.real_connect(@host, @user, @password, @database, @port)
  # Check to make sure there is a read db in the configs
  if @read_host
    $db_conn_read = Mysql.real_connect(@read_host, @read_username, @read_password, @read_database, @read_port)
  else
    $db_conn_read = $db_conn
  end
end