Class: SequelSqlBare

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

Direct Known Subclasses

SequelSqlWrapper

Instance Method Summary collapse

Methods inherited from SqlWrapper

#except_primary_key, #quote_column, #quote_table

Constructor Details

#initialize(db) ⇒ SequelSqlBare

Returns a new instance of SequelSqlBare.



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

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

Instance Method Details

#column_names(tbl) ⇒ Object



47
48
49
# File 'lib/coopy/sequel_sql_wrapper.rb', line 47

def column_names(tbl)
  columns(tbl).map{|x| x[0]}
end

#columns(tbl) ⇒ Object



42
43
44
45
# File 'lib/coopy/sequel_sql_wrapper.rb', line 42

def columns(tbl)
  sync_table(tbl)
  @db.schema(@tname)
end

#delete(tbl, cols, vals) ⇒ Object



28
29
30
31
# File 'lib/coopy/sequel_sql_wrapper.rb', line 28

def delete(tbl,cols,vals)
  sync_table(tbl)
  @t.filter(enhash(cols,vals)).delete
end

#enhash(cols, vals) ⇒ Object



19
20
21
# File 'lib/coopy/sequel_sql_wrapper.rb', line 19

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

#fetch(sql, names) ⇒ Object



61
62
63
64
65
# File 'lib/coopy/sequel_sql_wrapper.rb', line 61

def fetch(sql,names)
  @db.fetch(sql) do |row|
    yield names.map{|n| row[n]}
  end
end

#index(tbl) ⇒ Object



56
57
58
59
# File 'lib/coopy/sequel_sql_wrapper.rb', line 56

def index(tbl)
  key = primary_key(tbl)
  @t.select(*key)
end

#insert(tbl, cols, vals) ⇒ Object



23
24
25
26
# File 'lib/coopy/sequel_sql_wrapper.rb', line 23

def insert(tbl,cols,vals)
  sync_table(tbl)
  @t.insert(enhash(cols,vals))
end

#primary_key(tbl) ⇒ Object



51
52
53
54
# File 'lib/coopy/sequel_sql_wrapper.rb', line 51

def primary_key(tbl)
  cols = columns(tbl)
  cols.select{|x| x[1][:primary_key]}.map{|x| x[0]}
end

#sync_table(tbl) ⇒ Object



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

def sync_table(tbl)
  tbl = @tname if tbl.nil?
  tbl = @db.tables[0] if tbl.nil?
  return @t if tbl==@tname
  @tname = tbl
  @t = @db[tbl]
end

#transaction(&block) ⇒ Object



38
39
40
# File 'lib/coopy/sequel_sql_wrapper.rb', line 38

def transaction(&block)
  @db.transaction(&block)
end

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



33
34
35
36
# File 'lib/coopy/sequel_sql_wrapper.rb', line 33

def update(tbl,set_cols,set_vals,cond_cols,cond_vals)
  sync_table(tbl)
  @t.filter(enhash(cond_cols,cond_vals)).update(enhash(set_cols,set_vals))
end