class HDB
Attributes
connection[R]
resultTable[R]
sth[R]
table[R]
Public Class Methods
HDB(connectionName = "default")
click to toggle source
# File lib/hdb/hdb.rb, line 336 def self.HDB(connectionName = "default") hdb = HSharedData.instance().value("hdb-#{connectionName}") return (hdb) ? hdb : self.newHDB(connectionName) end
destroy(connectionName = "default")
click to toggle source
# File lib/hdb/hdb.rb, line 343 def self.destroy(connectionName = "default") self.HDB(connectionName).closeConnection() HSharedData.instance().setValue(nil, "hdb-#{connectionName}") end
new(host, port, dbname, user, password, timezone, connectionName = "default", connector = "Pg")
click to toggle source
# File lib/hdb/hdb.rb, line 82 def initialize(host, port, dbname, user, password, timezone, connectionName = "default", connector = "Pg") @host = host @port = port @dbname = dbname @user = user @password = password @timezone = timezone @connectionName = connectionName @connection = nil @select = nil @from = nil @where = nil @orderBy = nil @direction = nil @limit = nil @offset = nil @resultTable = nil @table = nil end
newHDB(connectionName = "default")
click to toggle source
# File lib/hdb/hdb.rb, line 306 def self.newHDB(connectionName = "default") params = hc.value(connectionName) host = params["host"] port = params["port"] dbname = params["dbname"] user = params["user"] password = params["password"] timezone = params["timezone"] connector = params["connector"] connector = connector.split('-') if (connector[0] == "hdbi") hdb = HDBI.new(host, port, dbname, user, password, timezone, connectionName, connector[1]) elsif (connector[0] == "hpgsql") hdb = HPgSql.new(host, port, dbname, user, password, timezone, connectionName) elsif (connector[0] == "hmysql") hdb = HMySql.new(host, port, dbname, user, password, timezone, connectionName) elsif (connector[0] == "hmysql2") hdb = HMySql2.new(host, port, dbname, user, password, timezone, connectionName) end hdb.openConnection() HSharedData.instance().setValue(hdb, "hdb-#{connectionName}") return hdb end
Public Instance Methods
closeConnection()
click to toggle source
# File lib/hdb/hdb.rb, line 174 def closeConnection() self.disconnect() if @connection @connection = nil end
columnCount()
click to toggle source
# File lib/hdb/hdb.rb, line 286 def columnCount() return self.fieldNameList.length end
data(row, idOrFieldName)
click to toggle source
# File lib/hdb/hdb.rb, line 292 def data(row, idOrFieldName) return (idOrFieldName.class == Fixnum) ? @table[row][@table[row].keys[idOrFieldName]] : @table[row][idOrFieldName.to_s] end
delete(tableName, where = "TRUE", operator = "AND")
click to toggle source
# File lib/hdb/hdb.rb, line 271 def delete(tableName, where = "TRUE", operator = "AND") where = self.quote(where) if where.class == Hash where = where.join(" #{operator} ") if(where.class == Array || where.class == Hash) self.execute("DELETE FROM \"#{tableName}\" WHERE #{where}") return self.rowsAffected() # rows affected end
direction(direction)
click to toggle source
# File lib/hdb/hdb.rb, line 136 def direction(direction) @direction = direction return self end
execute(queryStr = self.queryStr, pageSize: "all", page: 0)
click to toggle source
# File lib/hdb/hdb.rb, line 182 def execute(queryStr = self.queryStr, pageSize: "all", page: 0) limit = "LIMIT #{pageSize}" offset = "OFFSET #{page} * #{pageSize}" if(pageSize == "all") limit = offset = "" end queryStr += " #{limit} #{offset}" t1 = Time.now hl << queryStr.hight_yellow result = self._execute(queryStr) hl << "... msecs. #{((Time.now - t1) * 1000).round(2)}".hight_cyan return result end
from(from)
click to toggle source
# File lib/hdb/hdb.rb, line 117 def from(from) from = from.hjoin(', ') if(from.class == Array) @from = from return self end
insert(tableName, values)
click to toggle source
# File lib/hdb/hdb.rb, line 247 def insert(tableName, values) return self.execute("INSERT INTO \"#{tableName}\" #{self.insertValues(values)} RETURNING id").data(0,"id") end
insertValues(args)
click to toggle source
# File lib/hdb/hdb.rb, line 239 def insertValues(args) return "(id) values(default)" unless(args) args = self.quote(args) return "(#{args.keys.hjoin(', ')}) values(#{args.values.join(', ')})" end
limit(limit)
click to toggle source
# File lib/hdb/hdb.rb, line 140 def limit(limit) @limit = limit return self end
method_missing(methodeName, *args)
click to toggle source
# File lib/hdb/hdb.rb, line 107 def method_missing(methodeName, *args) return eval("HODB.new($1, @connectionName).find_by_#{$2}(*args)") if methodeName.to_s =~ /^find_on_(.+)_by_(.+)$/ end
normalizeWhereFormat(where, operator = 'AND')
click to toggle source
# File lib/hdb/hdb.rb, line 122 def normalizeWhereFormat(where, operator = 'AND') where = self.quote(where) if where.class == Hash where = where.join(" #{operator} ") if(where.class == Array || where.class == Hash) return where end
offset(offset)
click to toggle source
# File lib/hdb/hdb.rb, line 144 def offset(offset) @offset = offset return self end
openConnection()
click to toggle source
# File lib/hdb/hdb.rb, line 160 def openConnection() hl << "connectionName: #{@connectionName}".red hl << "host: #{@host}".red hl << "port: #{@port}".red hl << "db: #{@dbname}".red hl << "user: #{@user}".red hl << "password: #{@password}".red hl << "timezone: #{@timezone}".red @connection = self.connect() unless @connection end
orderBy(orderBy)
click to toggle source
# File lib/hdb/hdb.rb, line 131 def orderBy(orderBy) orderBy = orderBy.hjoin(', ') if(orderBy.class == Array) @orderBy = orderBy return self end
primitiveType?(value)
click to toggle source
# File lib/hdb/hdb.rb, line 208 def primitiveType?(value) return (value.class == String or value.class == Fixnum or value.class == TrueClass or value.class == FalseClass) end
queryStr()
click to toggle source
# File lib/hdb/hdb.rb, line 149 def queryStr() where = "WHERE #{@where}" if(@where) orderBy = "ORDER BY #{@orderBy}" if(@orderBy) limit = "LIMIT #{@limit}" if(@limit) offset = "OFFSET #{@offset}" if(@offset) return "SELECT #{@select} FROM #{@from} #{where} #{orderBy} #{@direction} #{limit} #{offset}" end
quote(args)
click to toggle source
quota tutti i valori tranne quelli che iniziano con # quindi se nella mia query devo richiamare una funzione basta farla precedere dal cancelletto
# File lib/hdb/hdb.rb, line 219 def quote(args) return 'NULL' if args == nil return self.quoteValue(args) if self.primitiveType?(args) result = Hash.new() args.each do |key, value| result[key] = self.quoteValue(args[key]) end return result end
Also aliased as: q
quoteValue(value)
click to toggle source
# File lib/hdb/hdb.rb, line 200 def quoteValue(value) return 'NULL' if value == nil value = value.to_s.gsub("'", "''") return (value[0] != '#') ? "'#{value}'" : value[1, value.size - 1] end
rowCount()
click to toggle source
# File lib/hdb/hdb.rb, line 280 def rowCount() return self.rowsAffected() end
select(select)
click to toggle source
posso passare anche un array di campi
# File lib/hdb/hdb.rb, line 112 def select(select) select = select.hjoin(', ') if(select.class == Array) @select = select return self end
show()
click to toggle source
# File lib/hdb/hdb.rb, line 298 def show() @table.each do |row| p row end end
toClassName(tableName)
click to toggle source
# File lib/hdb/hdb.rb, line 235 def toClassName(tableName) return tableName.hcapitalize() end
update(tableName, values, where = "TRUE", operator = "AND")
click to toggle source
# File lib/hdb/hdb.rb, line 263 def update(tableName, values, where = "TRUE", operator = "AND") where = where.join(" #{operator} ") if(where.class == Array || where.class == Hash) self.execute("UPDATE \"#{tableName}\" SET #{self.updateValues(values)} WHERE #{where}") return self.rowsAffected() end
updateValues(args)
click to toggle source
# File lib/hdb/hdb.rb, line 253 def updateValues(args) return "" unless(args) args = self.quote(args) result = [] args.each { |key, value| result << "\"#{key}\" = #{value}" } return result.join(', ') end
where(where, operator = 'AND')
click to toggle source
# File lib/hdb/hdb.rb, line 127 def where(where, operator = 'AND') @where = self.normalizeWhereFormat(where, operator) return self end