Class: Mysql

Inherits:
Gloo::Core::Obj
  • Object
show all
Defined in:
lib/mysql.rb

Constant Summary collapse

KEYWORD =
'mysql'.freeze
KEYWORD_SHORT =
'mysql'.freeze
HOST =
'host'.freeze
DB =
'database'.freeze
USER =
'username'.freeze
PASSWD =
'password'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.doc_dataObject

Get the object's documentation data.



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/mysql.rb', line 257

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'A MySQL database connection.',
    :children => [
      'host (string) — The database server host.',
      'database (string) — The name of the database.',
      'username (string) — The username with which to connect.',
      "password (string) — The user's password."
    ],
    :messages => [
      'verify — Verify that the database connection can be established.'
    ],
    :examples => <<~EXAMPLES.strip
      mysql [can] :
        on_load [script] :
          run mysql.get_passwd
          run mysql.sql
        db [mysql] :
          host : localhost
          database : my_database
          username : my_user
          password [alias] : mysql.get_passwd.result
        sql [query] :
          database [alias] : mysql.db
          sql : SELECT first, last, phone FROM people
        get_passwd [ask] :
          prompt [string] : Database Password?
          result [string] :
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



78
79
80
# File 'lib/mysql.rb', line 78

def self.messages
  return super + [ 'verify' ]
end

.short_typenameObject

The short name of the object type.



41
42
43
# File 'lib/mysql.rb', line 41

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



34
35
36
# File 'lib/mysql.rb', line 34

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:

  • (Boolean)


54
55
56
# File 'lib/mysql.rb', line 54

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



63
64
65
66
67
68
69
# File 'lib/mysql.rb', line 63

def add_default_children
  fac = @engine.factory
  fac.create_string HOST, nil, self
  fac.create_string DB, nil, self
  fac.create_string USER, nil, self
  fac.create_string PASSWD, nil, self
end

#get_clientObject

Get the client object. It might be cached, so check first. If it is not cached, create a new one.



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

def get_client
  app = @engine.running_app

  client = app.db_client_for_obj( self ) if app 

  if client && client.ping
    @engine.log.debug "Connection is established and active."
    return client
  elsif client
    @engine.log.debug "Connection is established but NOT active.  Reconnecting."
  else
    @engine.log.debug "Opening a new Connection."
  end

  h = {
    host: host_value,
    database: db_value,
    username: user_value,
    password: passwd_value
  }
  client = Mysql2::Client.new( h )

  app.cache_db_client( self, client ) if app
  
  return client
end

#get_query_result(result) ⇒ Object

Based on the result set, build a QueryResult object.



170
171
172
# File 'lib/mysql.rb', line 170

def get_query_result( result )
  return QueryResult.new result[0], result[1], @engine
end

#msg_verifyObject

SSH to the host and execute the command, then update result.



85
86
87
88
89
# File 'lib/mysql.rb', line 85

def msg_verify
  return unless connects?

  @engine.heap.it.set_to true
end

#query(sql, params = nil) ⇒ Object

Open a connection and execute the SQL statement. Return the resulting data.



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

def query( sql, params = nil )
  client = get_client

  heads = []
  data = []
  begin
    if params
      pst = client.prepare( sql )
      rs = pst.execute( *params, :as => :array )
      if rs
        rs.each do |row|
          arr = []
          row.each do |o| 
            arr << o
          end
          data << arr
        end
      end
    else
      rs = client.query( sql, :as => :array ) 
      if rs
        rs.each do |row|
          data << row
        end
      end
    end

    heads = rs.fields if rs
  rescue => e
    @engine.log_exception e
  end

  return [ heads, data ]
end