Class: HMySql2

Inherits:
HDB
  • Object
show all
Defined in:
lib/hdb/hmysql2.rb

Instance Attribute Summary

Attributes inherited from HDB

#connection, #resultTable, #sth, #table

Instance Method Summary collapse

Methods inherited from HDB

HDB, #closeConnection, #columnCount, #data, #delete, destroy, #direction, #execute, #from, #insertValues, #limit, #method_missing, newHDB, #normalizeWhereFormat, #offset, #openConnection, #orderBy, #primitiveType?, #queryStr, #quote, #quoteValue, #rowCount, #select, #show, #toClassName, #update, #updateValues, #where

Constructor Details

#initialize(host, port, dbname, user, password, timezone) ⇒ HMySql2

Returns a new instance of HMySql2.



32
33
34
35
36
37
# File 'lib/hdb/hmysql2.rb', line 32

def initialize(host, port, dbname, user, password, timezone)

  super(host, port, dbname, user, password, timezone, "hmysql2")
  @result = nil

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HDB

Instance Method Details

#_execute(queryStr = self.queryStr) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hdb/hmysql2.rb', line 55

def _execute(queryStr = self.queryStr) 

  @result = @connection.query(queryStr)
  
  return self unless @result
  
  @resultTable = HFieldTable.new()
  self.fieldNameList().each { |fieldName| @resultTable.addFieldName(fieldName) }
  @resultTable.makeCaption()
  @result.each_with_index do |row, i|  
    row.each do |fieldName, fieldValue|
      @resultTable.setDataByFieldName(i, fieldName, fieldValue.to_s)
    end
  end

  return self

end

#connectObject



39
40
41
42
43
44
45
46
# File 'lib/hdb/hmysql2.rb', line 39

def connect()

  @connection = Mysql2::Client.new(host: @host, username: @user, password: @password, database: @dbname, port: @port.to_i)
  #hl << "Server version: #{self.execute("SHOW server_version").dataByFieldIndex(0,0).to_s}"
  self.execute("SET time_zone = '#{@timezone}'") if @timezone
  return @connection

end

#disconnectObject



48
49
50
51
52
53
# File 'lib/hdb/hmysql2.rb', line 48

def disconnect()

  @connection.close()
  @connection = nil

end

#fieldNameListObject



74
75
76
77
78
79
80
81
82
# File 'lib/hdb/hmysql2.rb', line 74

def fieldNameList()

  fieldList = HList.new()
  
  @result.fields.each { |fieldName| fieldList << fieldName }
  
  return fieldList

end

#insert(tableName, values) ⇒ Object



88
89
90
91
92
93
# File 'lib/hdb/hmysql2.rb', line 88

def insert(tableName, values)

  self.execute("INSERT INTO #{tableName} #{self.insertValues(values)}")
  return @connection.last_id

end

#rowsAffectedObject



84
85
86
# File 'lib/hdb/hmysql2.rb', line 84

def rowsAffected
  return @connection.affected_rows()
end