Class: Coopy::DbiSqlWrapper

Inherits:
SqlWrapper show all
Defined in:
lib/coopy/dbi_sql_wrapper.rb

Instance Method Summary collapse

Methods inherited from SqlWrapper

#except_primary_key, #quote_column

Constructor Details

#initialize(db) ⇒ DbiSqlWrapper

Returns a new instance of DbiSqlWrapper.



5
6
7
8
9
# File 'lib/coopy/dbi_sql_wrapper.rb', line 5

def initialize(db)
  @db = db
  @t = nil
  @qt = nil
end

Instance Method Details

#column_names(tbl) ⇒ Object



71
72
73
# File 'lib/coopy/dbi_sql_wrapper.rb', line 71

def column_names(tbl)
  columns(tbl).map{|c| c[:name]}
end

#columns(tbl) ⇒ Object



66
67
68
69
# File 'lib/coopy/dbi_sql_wrapper.rb', line 66

def columns(tbl)
  tbl = complete_table(tbl)
  @db.columns(tbl)
end

#complete_table(tbl) ⇒ Object



11
12
13
14
15
16
# File 'lib/coopy/dbi_sql_wrapper.rb', line 11

def complete_table(tbl)
  return tbl unless tbl.nil?
  return @t unless @t.nil?
  @t = @db.tables[0]
  @t
end

#delete(tbl, cols, vals) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/coopy/dbi_sql_wrapper.rb', line 35

def delete(tbl,cols,vals)
  tbl = quote_table(tbl)
  template = cols.map{|c| @db.quote(c) + ' = ?'}.join(" AND ")
  template = "DELETE FROM #{tbl} WHERE #{template}"
  stmt = @db.prepare(template)
  stmt.execute(*vals)
  stmt.finish
end

#enhash(cols, vals) ⇒ Object



75
76
77
# File 'lib/coopy/dbi_sql_wrapper.rb', line 75

def enhash(cols,vals)
  Hash[*cols.map{|c| c.to_sym}.zip(vals).flatten]
end

#fetch(sql, names) ⇒ Object



79
80
81
82
83
# File 'lib/coopy/dbi_sql_wrapper.rb', line 79

def fetch(sql,names)
  @db.select_all(sql) do |row|
    yield row
  end
end

#insert(tbl, cols, vals) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/coopy/dbi_sql_wrapper.rb', line 26

def insert(tbl,cols,vals)
  tbl = quote_table(tbl)
  template = cols.map{|x| '?'}.join(",")
  template = "INSERT INTO #{tbl} VALUES(#{template})"
  stmt = @db.prepare(template)
  stmt.execute(*vals)
  stmt.finish
end

#primary_key(tbl) ⇒ Object



85
86
87
88
# File 'lib/coopy/dbi_sql_wrapper.rb', line 85

def primary_key(tbl)
  # don't seem to have this information? oy.
  [column_names(tbl)[0]]
end

#quote_table(tbl) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/coopy/dbi_sql_wrapper.rb', line 18

def quote_table(tbl)
  return @db.quote(tbl) unless tbl.nil?
  return @qt unless @qt.nil?
  @t = @db.tables[0]
  @qt = @db.quote(@t)
  @qt
end

#transaction(&block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/coopy/dbi_sql_wrapper.rb', line 55

def transaction(&block)
  @db["AutoCommit"]=false
  begin
    block.call
    @db.commit
  rescue Exception => e
    @db.rollback
    raise e
  end
end

#update(tbl, set_cols, set_vals, cond_cols, cond_vals) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/coopy/dbi_sql_wrapper.rb', line 44

def update(tbl,set_cols,set_vals,cond_cols,cond_vals)
  tbl = quote_table(tbl)
  conds = cond_cols.map{|c| @db.quote(c) + ' = ?'}.join(" AND ")
  sets = set_cols.map{|c| @db.quote(c) + ' = ?'}.join(", ")
  template = "UPDATE #{@qt} SET #{sets} WHERE #{conds}"
  v = set_vals + cond_vals
  stmt = @db.prepare(template)
  stmt.execute(*v)
  stmt.finish
end