Class: Buzzard::DBI::Wrapper
- Inherits:
-
Object
- Object
- Buzzard::DBI::Wrapper
- Defined in:
- lib/buzzard/dbi.rb
Instance Method Summary collapse
- #_maybe_normalize(hash) ⇒ Object
- #_maybe_normalize_all(arr) ⇒ Object
- #_normalize(hash) ⇒ Object
- #_symbolic_keys? ⇒ Boolean
- #all(sql, *params, &block) ⇒ Object
- #exec(sql, *params) ⇒ Object
-
#initialize(app, dbh) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #one(sql, *params) ⇒ Object
- #row(sql, *params) ⇒ Object
Constructor Details
#initialize(app, dbh) ⇒ Wrapper
Returns a new instance of Wrapper.
25 26 27 28 |
# File 'lib/buzzard/dbi.rb', line 25 def initialize(app, dbh) @app = app @dbh = dbh end |
Instance Method Details
#_maybe_normalize(hash) ⇒ Object
85 86 87 |
# File 'lib/buzzard/dbi.rb', line 85 def _maybe_normalize(hash) _symbolic_keys? ? _normalize(hash) : hash end |
#_maybe_normalize_all(arr) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/buzzard/dbi.rb', line 89 def _maybe_normalize_all(arr) if _symbolic_keys? r = [] arr.each_with_index do |hash, index| r[index] = _normalize(hash) end r else arr end end |
#_normalize(hash) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/buzzard/dbi.rb', line 101 def _normalize(hash) if !hash.nil? && hash.kind_of?(Hash) r = {} hash.each_pair do |key,value| key = key.to_s if !key.kind_of?(Symbol) key = key.to_sym if key.kind_of?(String) r[key] = value end r end end |
#_symbolic_keys? ⇒ Boolean
77 78 79 80 81 82 83 |
# File 'lib/buzzard/dbi.rb', line 77 def _symbolic_keys? if @app.settings.respond_to?(:dbi_symbolic_keys) @app.settings.dbi_symbolic_keys else true end end |
#all(sql, *params, &block) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/buzzard/dbi.rb', line 55 def all(sql, *params, &block) sth = @dbh.prepare(sql) res = [] begin sth.execute(*params) while row=sth.fetch_hash do if block yield _maybe_normalize(row) else res << row end end ensure sth.finish end _maybe_normalize_all(res) end |
#exec(sql, *params) ⇒ Object
73 74 75 |
# File 'lib/buzzard/dbi.rb', line 73 def exec(sql, *params) @dbh.do(sql, *params) end |
#one(sql, *params) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/buzzard/dbi.rb', line 30 def one(sql, *params) sth = @dbh.prepare(sql) res = nil begin sth.execute(*params) row = sth.fetch res = row.shift if row ensure sth.finish end res end |
#row(sql, *params) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/buzzard/dbi.rb', line 43 def row(sql, *params) sth = @dbh.prepare(sql) res = nil begin sth.execute(*params) res = sth.fetch_hash ensure sth.finish end _maybe_normalize(res) end |