Class: HDBI

Inherits:
HDB
  • Object
show all
Defined in:
lib/hdb/hdbi.rb,
lib/hdb/hdbi_test.rb

Instance Attribute Summary

Attributes inherited from HDB

#connection, #resultTable, #sth, #table

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HDB

HDB, #closeConnection, #columnCount, #data, #delete, destroy, #direction, #execute, #from, #insert, #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, connectionName, connector = "Pg") ⇒ HDBI

Returns a new instance of HDBI.



7
8
9
10
11
12
13
# File 'lib/hdb/hdbi.rb', line 7

def initialize(host, port, dbname, user, password, timezone, connectionName, connector = "Pg")

  super(host, port, dbname, user, password, timezone, connectionName, connector)
  @connector = connector
  @sth = nil

end

Dynamic Method Handling

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

Class Method Details

.testObject



23
24
# File 'lib/hdb/hdbi_test.rb', line 23

def self.test
end

.test0Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hdb/hdbi_test.rb', line 27

def self.test0

  hdb = HDB.new('localhost', '5432', 'quickorder', 'quickorder', 'quickorder')
  hdb.openConnection()
  resultTable = hdb.select([:id, :name]).from(:cookbook).where(["id > 0", "id < 200"]).orderBy([:id, :name]).execute
  resultTable = hdb.select("id, name").from("cookbook").where("id > 0 and id < 200").execute
  hdb.show()
  resultTable = hdb.select([:id, :name]).from(:cookbook).where(["id > 0", "id < 200"], "or").orderBy([:id, :name]).execute(pageSize: 10, page: 2)
  hdb.toSqlTable().show()
  id = hdb.insert(:recipe_types, {name: "'nome"})
  p "delete: " + hdb.delete(:recipe_types, {id: id, name: '10'}).to_s
  hdb.select(:name).from(:recipe_types).execute
  hdb.show()
  hdb.closeConnection

end

.test1Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hdb/hdbi_test.rb', line 45

def self.test1

  dbh = DBI.connect("dbi:Pg:quickorder:localhost", "quickorder", "quickorder")
  sth = dbh.execute("select * from cookbook")
  p sth.column_names
  sth.fetch do |row|
    #p row['name'] # or row.by_field("name")
    #p row[3]      # or row.by_index(3)
    row.each_with_name do |fieldValue, fieldName|
      p "  #{fieldName}: #{fieldValue}"
    end
    p "======================="
  end
  sth.finish

end

.test2Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hdb/hdbi_test.rb', line 62

def self.test2

  dbh = DBI.connect("dbi:Pg:quickorder:localhost", "quickorder", "quickorder")
  sth = dbh.prepare("select * from cookbook where id = ?")
  sth.execute(1)
  p sth.column_names
  sth.fetch do |row|
    #p row['name'] # or row.by_field("name")
    #p row[3]      # or row.by_index(3)
    row.each_with_name do |fieldValue, fieldName|
      p "  #{fieldName}: #{fieldValue}"
    end
    p "======================="
  end
  sth.finish

end

.test3Object

finish isn’t required



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hdb/hdbi_test.rb', line 82

def self.test3

  dbh = DBI.connect("dbi:Pg:quickorder:localhost", "quickorder", "quickorder")
  rows = dbh.select_all("select * from cookbook") # con select_all o select_one non e' necessario finish
  rows.each do |row|
    row.each_with_name do |fieldValue, fieldName|
      p "  #{fieldName}: #{fieldValue}"
    end
    p "======================="
  end

end

.test4Object



96
97
98
99
100
101
# File 'lib/hdb/hdbi_test.rb', line 96

def self.test4

  dbh = DBI.connect("dbi:Pg:quickorder:localhost", "quickorder", "quickorder")
  dbh.do("insert into recipe_types(name, position, department) values(?, ?, ?)", 'hypersonic', 1, nil)

end

.test5Object



103
104
105
106
107
108
109
110
# File 'lib/hdb/hdbi_test.rb', line 103

def self.test5

  dbh = DBI.connect("dbi:Pg:quickorder:localhost", "quickorder", "quickorder")
  sth = dbh.prepare("insert into recipe_types(name, position, department) values(?, ?, ?)")
  sth.execute('hypersonic', 1, nil)
  sth.finish

end

.test6Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hdb/hdbi_test.rb', line 112

def self.test6

  dbh = DBI.connect("dbi:Pg:quickorder:localhost", "quickorder", "quickorder")
  sth = dbh.execute("select * from cookbook")
  rows = sth.fetch_all

  printf "Number of rows affected: %d\n", sth.rows
  printf "Number of columns: %d\n", sth.column_names.size

  rows.each do |row|
    row.each_with_name do |fieldValue, fieldName|
      p "  #{fieldName}: #{fieldValue}"
    end
    p "======================="
  end
  sth.finish

end

.testmysqlObject



16
17
18
19
20
21
# File 'lib/hdb/hdbi_test.rb', line 16

def self.testmysql
  id = hdb("quickorder").insert(:city_table, {name: "Wien"})
  puts "id: #{id}"
  resultTable = hdb("quickorder").select([:id, :name]).from(:city_table).where(["id > 0", "id < 20"]).orderBy([:id, :name]).execute
  resultTable.show
end

.testxObject



6
7
8
9
# File 'lib/hdb/hdbi_test.rb', line 6

def self.testx
  resultTable = hdb.select([:id, :name]).from(:recipe_types).where(["id > 0", "id < 20000"]).orderBy([:id, :name]).execute
  resultTable.show
end

.testxxObject



11
12
13
14
# File 'lib/hdb/hdbi_test.rb', line 11

def self.testxx
  resultTable = hdb("quickorder_pg").select([:id, :name]).from(:recipe_types).where(["id > 0", "id < 20000"]).orderBy([:id, :name]).execute
  resultTable.show
end

Instance Method Details

#_execute(queryStr = self.queryStr) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hdb/hdbi.rb', line 31

def _execute(queryStr = self.queryStr) 

  @sth = @connection.execute(queryStr)
  result = @sth.fetch_all
  @resultTable = HFieldTable.new()
  self.fieldNameList().each { |fieldName| @resultTable.addFieldName(fieldName) }
  @resultTable.makeCaption()
  result.each_with_index do |row, i|  
    row.each_with_name do |fieldValue, fieldName|
      @resultTable.setDataByFieldName(i, fieldName, fieldValue)
    end
  end
  @sth.finish
  @sth = nil
  return self

end

#connectObject



15
16
17
18
19
20
21
22
# File 'lib/hdb/hdbi.rb', line 15

def connect()

  @connection = DBI.connect("dbi:#{@connector}:#{@dbname}:#{@host}", @user, @password)
  hl << "Server version: #{self.execute("SHOW server_version").firstData.to_s}"
  self.execute("SET TIME ZONE '#{@timezone}'") if @timezone
  return @connection

end

#disconnectObject



24
25
26
27
28
29
# File 'lib/hdb/hdbi.rb', line 24

def disconnect()

  @connection.disconnect()
  @connection = nil 

end

#fieldNameListObject



49
50
51
52
53
54
55
56
57
# File 'lib/hdb/hdbi.rb', line 49

def fieldNameList()

  fieldList = HList.new()
  
  @sth.column_names.each { |fieldName| fieldList << fieldName }
  
  return fieldList

end

#rowsAffectedObject



59
60
61
# File 'lib/hdb/hdbi.rb', line 59

def rowsAffected
  return @sth.rows # rows affected
end