Module: Roma::Storage::SQLite3_Ext

Defined in:
lib/roma/storage/sqlite3_storage.rb

Instance Method Summary collapse

Instance Method Details

#create_tableObject



46
47
48
49
50
51
# File 'lib/roma/storage/sqlite3_storage.rb', line 46

def create_table
  sql = "create table t_roma ( " + 
    "key TEXT PRIMARY KEY," +
    "val BLOB);"
  self.execute( sql )
end

#eachObject



40
41
42
43
44
# File 'lib/roma/storage/sqlite3_storage.rb', line 40

def each
  self.execute("select * from t_roma"){ |r|
    yield r[0],r[1]
  }
end

#get(k) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/roma/storage/sqlite3_storage.rb', line 19

def get(k)
  if RUBY_VERSION >= "1.9.1"
    k = k.encode("ascii-8bit") if k.encoding != Encoding::ASCII_8BIT
  end
  r = self.execute("select * from t_roma where key=?",k)
  return nil if r.length==0
  r[0][1]
end

#out(k) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/roma/storage/sqlite3_storage.rb', line 28

def out(k)
  if RUBY_VERSION >= "1.9.1"
    k = k.encode("ascii-8bit") if k.encoding != Encoding::ASCII_8BIT
  end
  return nil if get(k) == nil
  self.execute("delete from t_roma where key=?",k)
end

#put(k, v) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/roma/storage/sqlite3_storage.rb', line 8

def put(k,v)
  if RUBY_VERSION >= "1.9.1"
    k = k.encode("ascii-8bit") if k.encoding != Encoding::ASCII_8BIT
  end
  if self.execute("select count(*) from t_roma where key=?",k)[0][0].to_i==0
    self.execute("insert into t_roma values (?,?)",k,SQLite3::Blob.new(v))
  else
    self.execute("update t_roma set val=? where key=?",SQLite3::Blob.new(v),k)
  end
end

#rnumObject



36
37
38
# File 'lib/roma/storage/sqlite3_storage.rb', line 36

def rnum
  self.execute("select count(*) from t_roma")[0][0].to_i
end

#syncObject



60
61
62
# File 'lib/roma/storage/sqlite3_storage.rb', line 60

def sync
  true
end

#tablesObject



53
54
55
56
57
58
# File 'lib/roma/storage/sqlite3_storage.rb', line 53

def tables
  sql = "SELECT name FROM " +
    "sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master " +
    "WHERE type='table' ORDER BY name;"
  self.execute( sql ).flatten
end