Module: Suture::Wrap::Sqlite

Defined in:
lib/suture/wrap/sqlite.rb

Constant Summary collapse

SCHEMA_VERSION =
2

Class Method Summary collapse

Class Method Details

.delete(db, table, where_clause, bind_params) ⇒ Object



53
54
55
# File 'lib/suture/wrap/sqlite.rb', line 53

def self.delete(db, table, where_clause, bind_params)
  db.execute("delete from #{table} #{where_clause}", bind_params)
end

.init(location) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/suture/wrap/sqlite.rb', line 8

def self.init(location)
  full_path = File.join(Dir.getwd, location)
  FileUtils.mkdir_p(File.dirname(full_path))
  SQLite3::Database.new(full_path).tap do |db|
    db.execute <<-SQL
      create table if not exists suture_schema_info (
        version integer unique
      );
    SQL
    db.execute("insert or ignore into suture_schema_info values (?)", [SCHEMA_VERSION])
    actual_schema_version = db.execute("select * from suture_schema_info").first[0]
    if SCHEMA_VERSION != actual_schema_version
      raise Suture::Error::SchemaVersion.new(SCHEMA_VERSION, actual_schema_version)
    end

    db.execute <<-SQL
      create table if not exists observations (
        id integer primary key,
        name varchar(255) not null,
        args clob not null,
        result clob,
        error clob,
        unique(name, args)
      );
    SQL
  end
end

.insert(db, table, cols, vals) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/suture/wrap/sqlite.rb', line 36

def self.insert(db, table, cols, vals)
  sql = <<-SQL
    insert into #{table}
      (#{cols.join(", ")})
    values
      (#{vals.size.times.map { "?" }.join(", ")})
  SQL
  db.execute(sql, vals)
end

.select(db, table, where_clause, bind_params) ⇒ Object



46
47
48
49
50
51
# File 'lib/suture/wrap/sqlite.rb', line 46

def self.select(db, table, where_clause, bind_params)
  db.execute(
    "select * from #{table} #{where_clause} order by id asc",
    bind_params
  )
end