Class: HDB

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

Direct Known Subclasses

HDBI, HMySql, HMySql2, HPgSql

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, dbname, user, password, timezone, connectionName = "default", connector = "Pg") ⇒ HDB

Returns a new instance of HDB.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methodeName, *args) ⇒ Object



107
108
109
# 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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



80
81
82
# File 'lib/hdb/hdb.rb', line 80

def connection
  @connection
end

#resultTableObject (readonly)

Returns the value of attribute resultTable.



80
81
82
# File 'lib/hdb/hdb.rb', line 80

def resultTable
  @resultTable
end

#sthObject (readonly)

Returns the value of attribute sth.



80
81
82
# File 'lib/hdb/hdb.rb', line 80

def sth
  @sth
end

#tableObject (readonly)

Returns the value of attribute table.



80
81
82
# File 'lib/hdb/hdb.rb', line 80

def table
  @table
end

Class Method Details

.destroy(connectionName = "default") ⇒ Object



343
344
345
346
347
348
# File 'lib/hdb/hdb.rb', line 343

def self.destroy(connectionName = "default")
  
  self.HDB(connectionName).closeConnection()
  HSharedData.instance().setValue(nil, "hdb-#{connectionName}")

end

.HDB(connectionName = "default") ⇒ Object



336
337
338
339
340
341
# 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

.newHDB(connectionName = "default") ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# 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

Instance Method Details

#closeConnectionObject



174
175
176
177
178
179
# File 'lib/hdb/hdb.rb', line 174

def closeConnection()
  
  self.disconnect() if @connection
  @connection = nil

end

#columnCountObject



286
287
288
289
290
# File 'lib/hdb/hdb.rb', line 286

def columnCount()

  return self.fieldNameList.length

end

#data(row, idOrFieldName) ⇒ Object



292
293
294
295
296
# 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") ⇒ Object



271
272
273
274
275
276
277
278
# 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) ⇒ Object



136
137
138
139
# File 'lib/hdb/hdb.rb', line 136

def direction(direction)
  @direction = direction
  return self
end

#execute(queryStr = self.queryStr, pageSize: "all", page: 0) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# 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) ⇒ Object



117
118
119
120
121
# 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) ⇒ Object



247
248
249
250
251
# 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) ⇒ Object



239
240
241
242
243
244
245
# 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) ⇒ Object



140
141
142
143
# File 'lib/hdb/hdb.rb', line 140

def limit(limit)
  @limit = limit
  return self
end

#normalizeWhereFormat(where, operator = 'AND') ⇒ Object



122
123
124
125
126
# 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) ⇒ Object



144
145
146
147
# File 'lib/hdb/hdb.rb', line 144

def offset(offset)
  @offset = offset
  return self
end

#openConnectionObject



160
161
162
163
164
165
166
167
168
169
170
171
# 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) ⇒ Object



131
132
133
134
135
# File 'lib/hdb/hdb.rb', line 131

def orderBy(orderBy)
  orderBy = orderBy.hjoin(', ') if(orderBy.class == Array)
  @orderBy = orderBy
  return self
end

#primitiveType?(value) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
# 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

#queryStrObject



149
150
151
152
153
154
155
156
157
# 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) ⇒ Object Also known as: q

quota tutti i valori tranne quelli che iniziano con # quindi se nella mia query devo richiamare una funzione basta farla precedere dal cancelletto



219
220
221
222
223
224
225
226
227
228
229
230
231
# 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

#quoteValue(value) ⇒ Object



200
201
202
203
204
205
206
# 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

#rowCountObject



280
281
282
283
284
# File 'lib/hdb/hdb.rb', line 280

def rowCount()

  return self.rowsAffected()

end

#select(select) ⇒ Object

posso passare anche un array di campi



112
113
114
115
116
# File 'lib/hdb/hdb.rb', line 112

def select(select)
  select = select.hjoin(', ') if(select.class == Array)
  @select = select
  return self
end

#showObject



298
299
300
301
302
303
304
# File 'lib/hdb/hdb.rb', line 298

def show()

  @table.each do |row|
    p row
  end

end

#toClassName(tableName) ⇒ Object



235
236
237
# File 'lib/hdb/hdb.rb', line 235

def toClassName(tableName)
  return tableName.hcapitalize()
end

#update(tableName, values, where = "TRUE", operator = "AND") ⇒ Object



263
264
265
266
267
268
269
# 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) ⇒ Object



253
254
255
256
257
258
259
260
261
# 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') ⇒ Object



127
128
129
130
# File 'lib/hdb/hdb.rb', line 127

def where(where, operator = 'AND')
  @where = self.normalizeWhereFormat(where, operator)
  return self
end