class HDBI
Public Class Methods
new(host, port, dbname, user, password, timezone, connectionName, connector = "Pg")
click to toggle source
Calls superclass method
HDB::new
# 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
test()
click to toggle source
# File lib/hdb/hdbi_test.rb, line 23 def self.test end
test0()
click to toggle source
# 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
test1()
click to toggle source
# 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
test2()
click to toggle source
# 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
test3()
click to toggle source
finish isn't required
# 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
test4()
click to toggle source
# 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
test5()
click to toggle source
# 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
test6()
click to toggle source
# 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
testmysql()
click to toggle source
# 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
testx()
click to toggle source
# 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
testxx()
click to toggle source
# 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
Public Instance Methods
_execute(queryStr = self.queryStr)
click to toggle source
# 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
connect()
click to toggle source
# 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
disconnect()
click to toggle source
# File lib/hdb/hdbi.rb, line 24 def disconnect() @connection.disconnect() @connection = nil end
fieldNameList()
click to toggle source
# File lib/hdb/hdbi.rb, line 49 def fieldNameList() fieldList = HList.new() @sth.column_names.each { |fieldName| fieldList << fieldName } return fieldList end
rowsAffected()
click to toggle source
# File lib/hdb/hdbi.rb, line 59 def rowsAffected return @sth.rows # rows affected end