Class: DbPlugin
- Inherits:
-
Rubko::Plugin
- Object
- Rubko::Plugin
- DbPlugin
- Defined in:
- lib/rubko/plugins/db.rb
Constant Summary collapse
- TypArray =
PostgreSQL array OIDs and coresponding data type
{ 1000 => 16, # bool 1001 => 17, # bytea 1002 => 18, # char 1003 => 19, # name 1016 => 20, # int8 1005 => 21, # int2 1006 => 22, # int2vector 1007 => 23, # int4 1008 => 24, # regproc 1009 => 25, # text 1028 => 26, # oid 1010 => 27, # tid 1011 => 28, # xid 1012 => 29, # cid 1013 => 30, # oidvector 199 => 114, # json 143 => 142, # xml 1017 => 600, # point 1018 => 601, # lseg 1019 => 602, # path 1020 => 603, # box 1027 => 604, # polygon 629 => 628, # line 651 => 650, # cidr 1021 => 700, # float4 1022 => 701, # float8 1023 => 702, # abstime 1024 => 703, # reltime 1025 => 704, # tinterval 719 => 718, # circle 791 => 790, # money 1040 => 829, # macaddr 1041 => 869, # inet 1034 => 1033, # aclitem 1014 => 1042, # bpchar 1015 => 1043, # varchar 1182 => 1082, # date 1183 => 1083, # time 1115 => 1114, # timestamp 1185 => 1184, # timestamptz 1187 => 1186, # interval 1270 => 1266, # timetz 1561 => 1560, # bit 1563 => 1562, # varbit 1231 => 1700, # numeric 2201 => 1790, # refcursor 2207 => 2202, # regprocedure 2208 => 2203, # regoper 2209 => 2204, # regoperator 2210 => 2205, # regclass 2211 => 2206, # regtype 2287 => 2249, # record 1263 => 2275, # cstring 2951 => 2950, # uuid 2949 => 2970, # txid_snapshot 3643 => 3614, # tsvector 3645 => 3615, # tsquery 3644 => 3642, # gtsvector 3735 => 3734, # regconfig 3770 => 3769, # regdictionary 3905 => 3904, # int4range 3907 => 3906, # numrange 3909 => 3908, # tsrange 3911 => 3910, # tstzrange 3913 => 3912, # daterange 3927 => 3926, # int8range }
- Unpack =
{ 16 => 'C', # bool 21 => 's>', # int2 23 => 'l>', # int4 20 => 'q>', # int8 26 => 'L>', # oid 700 => 'g', # float4 701 => 'G', # float8 829 => 'C*', # macaddr 1082 => 'l>', # date 1114 => 'q>', # timestamp 1184 => 'q>', # timestamptz 1186 => 'q>l>l>', # interval 1700 => 's>*', # numeric 1007 => 'l>*' }
Instance Attribute Summary collapse
-
#affectedRows ⇒ Object
readonly
Returns the value of attribute affectedRows.
-
#db ⇒ Object
Returns the value of attribute db.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#pool ⇒ Object
Returns the value of attribute pool.
-
#port ⇒ Object
Returns the value of attribute port.
-
#user ⇒ Object
Returns the value of attribute user.
Attributes included from Rubko::Base
Instance Method Summary collapse
- #castNumeric(value) ⇒ Object
- #column(query, *args) ⇒ Object
- #connect ⇒ Object
- #escape(param, type = nil) ⇒ Object
- #finalize ⇒ Object
- #handle ⇒ Object
- #init ⇒ Object
- #inTransaction ⇒ Object
- #poolSize ⇒ Object
- #prepare(query, *args) ⇒ Object
- #raw(query, *args) ⇒ Object
- #release ⇒ Object
- #result(query, *args, &block) ⇒ Object
- #resultHash(key, query, *args) ⇒ Object
- #resultMultiHash(key, query, *args) ⇒ Object
- #rollback ⇒ Object
- #row(query, *args) ⇒ Object
- #transaction(mode = nil) ⇒ Object
- #value(query, *args) ⇒ Object
Methods inherited from Rubko::Plugin
Methods included from Rubko::Base
#camelize, #httpGet, #initialize, #jsonParse, #loadController, #loadFile, #loadModel, #loadPlugin, #loadView, #method_missing, #uncamelize
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Rubko::Base
Instance Attribute Details
#affectedRows ⇒ Object (readonly)
Returns the value of attribute affectedRows.
27 28 29 |
# File 'lib/rubko/plugins/db.rb', line 27 def affectedRows @affectedRows end |
#db ⇒ Object
Returns the value of attribute db.
26 27 28 |
# File 'lib/rubko/plugins/db.rb', line 26 def db @db end |
#host ⇒ Object
Returns the value of attribute host.
26 27 28 |
# File 'lib/rubko/plugins/db.rb', line 26 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
26 27 28 |
# File 'lib/rubko/plugins/db.rb', line 26 def password @password end |
#pool ⇒ Object
Returns the value of attribute pool.
26 27 28 |
# File 'lib/rubko/plugins/db.rb', line 26 def pool @pool end |
#port ⇒ Object
Returns the value of attribute port.
26 27 28 |
# File 'lib/rubko/plugins/db.rb', line 26 def port @port end |
#user ⇒ Object
Returns the value of attribute user.
26 27 28 |
# File 'lib/rubko/plugins/db.rb', line 26 def user @user end |
Instance Method Details
#castNumeric(value) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rubko/plugins/db.rb', line 154 def castNumeric(value) _, fac, sign, scale, *digits = value return Float::NAN if sign == -16384 sign = (sign == 0 ? '': '-') value = digits.map { |digit| '%04d' % digit } if scale != 0 require 'bigdecimal' BigDecimal.new sign + value.insert(fac+1, '.').join else ( sign + value.join + '0000'*fac ).to_i end end |
#column(query, *args) ⇒ Object
273 274 275 276 277 278 279 |
# File 'lib/rubko/plugins/db.rb', line 273 def column(query, *args) result = raw query, *args return [] if result.nfields == 0 result.column_values(0).map { |value| cast value, result.ftype(0) } end |
#connect ⇒ Object
29 30 31 |
# File 'lib/rubko/plugins/db.rb', line 29 def connect PG.connect host: host, port: port, user: user, password: password, dbname: db end |
#escape(param, type = nil) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/rubko/plugins/db.rb', line 222 def escape(param, type = nil) case param when Array '(' + param.map { |element| escape element }.join(',') + ')' when String if param.encoding.name == 'ASCII-8BIT' "'" + handle.escape_bytea(param) + "'" else "'" + handle.escape_string(param) + "'" end when Symbol handle.quote_ident param.to_s when Integer, Float, TimeInterval, FalseClass, TrueClass param.to_s else 'NULL' end end |
#finalize ⇒ Object
348 349 350 351 352 |
# File 'lib/rubko/plugins/db.rb', line 348 def finalize unless release handle.finish unless handle.finished? end end |
#handle ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rubko/plugins/db.rb', line 14 def handle unless @handle config if @handle.nil? if pool @sig = [host, port, user, password, db] @handle = ( pool[:db, *@sig] ||= [] ).pop end @handle = connect unless @handle end @handle end |
#init ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/rubko/plugins/db.rb', line 4 def init @host = '' @port = 5432 @user = 'postgres' @password = '' @db = 'postgres' @pool = nil end |
#inTransaction ⇒ Object
344 345 346 |
# File 'lib/rubko/plugins/db.rb', line 344 def inTransaction handle.transaction_status == 2 end |
#poolSize ⇒ Object
42 43 44 |
# File 'lib/rubko/plugins/db.rb', line 42 def poolSize pool.keys(:db, *@sig).size end |
#prepare(query, *args) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/rubko/plugins/db.rb', line 243 def prepare(query, *args) hash = args[-1].kind_of?(Hash) && args.pop || {} hash.merge! Hash[(0...args.size).zip args] i = -1 query.gsub(/[[:alpha:]]+/i) { |match| ( match =~ /[[:upper:]]/ && match =~ /[[:lower:]]/ ) ? escape(match.to_sym) : match }.gsub(/\?(\w+)?\??([b])?/) { |match| key = $1 || i += 1 key = key.to_sym if key.respond_to? :to_sym key = key.to_s unless hash.has_key? key hash.has_key?(key) && escape(hash[key], $2) || match }.tap { |out| log.sql out.gsub(/\s+/, ' ') } end |
#raw(query, *args) ⇒ Object
259 260 261 |
# File 'lib/rubko/plugins/db.rb', line 259 def raw(query, *args) handle.exec prepare(query, *args), [], 1 end |
#release ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/rubko/plugins/db.rb', line 33 def release return true unless @handle if pool && handle.transaction_status == 0 pool[:db, *@sig].push @handle @handle = false return true end end |
#result(query, *args, &block) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/rubko/plugins/db.rb', line 281 def result(query, *args, &block) result = raw(query, *args) if block result.each { |row| yield castHash row, result } else result.map { |row| castHash row, result } end end |
#resultHash(key, query, *args) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/rubko/plugins/db.rb', line 294 def resultHash(key, query, *args) ret = {} result = raw query, *args result.each { |row| row = castHash row, result [*key].reduce(ret) { |v, k| bucket = row.delete k.to_sym v[bucket] ||= {} }.replace row } ret end |
#resultMultiHash(key, query, *args) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/rubko/plugins/db.rb', line 308 def resultMultiHash(key, query, *args) ret = {} result = raw query, *args result.each { |row| row = castHash row, result last = [*key].last [*key].reduce(ret) { |v, k| bucket = row.delete k.to_sym if k.equal? last # is the last element ( v[bucket] ||= [] ) << row else v[bucket] ||= {} end } } ret end |
#rollback ⇒ Object
340 341 342 |
# File 'lib/rubko/plugins/db.rb', line 340 def rollback raw 'ROLLBACK' end |
#row(query, *args) ⇒ Object
268 269 270 271 |
# File 'lib/rubko/plugins/db.rb', line 268 def row(query, *args) result = raw query, *args castHash result[0], result if result.ntuples > 0 end |
#transaction(mode = nil) ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/rubko/plugins/db.rb', line 327 def transaction(mode = nil) begin raw 'BEGIN' + (' ISOLATION LEVEL '+mode if mode).to_s yield raw 'COMMIT' if inTransaction rescue => e p e puts e.backtrace rollback puts 'Transaction rolled back.' end end |
#value(query, *args) ⇒ Object
263 264 265 266 |
# File 'lib/rubko/plugins/db.rb', line 263 def value(query, *args) result = raw query, *args cast(result.getvalue(0, 0), result.ftype(0)) if result.ntuples > 0 end |