-
Notifications
You must be signed in to change notification settings - Fork 49
/
array.rb
48 lines (34 loc) · 1.04 KB
/
array.rb
1
2
3
4
5
6
7
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Picky
module Backends
class SQLite
class Array < Basic
def create_table
db.execute 'create table key_value (key varchar(255), value text);'
end
def size
result = db.execute 'SELECT COUNT(*) FROM key_value'
result.first.first.to_i
end
def []= key, array
unless array.empty?
db.execute 'INSERT OR REPLACE INTO key_value (key,value) VALUES (?,?)',
key.to_s,
MultiJson.encode(array)
end
DirectlyManipulable.make self, array, key
array
end
def [] key
res = db.execute "SELECT value FROM key_value WHERE key = ? LIMIT 1",
key.to_s
array = res.blank? ? [] : MultiJson.decode(res.first.first)
DirectlyManipulable.make self, array, key
array
end
def delete key
db.execute "DELETE FROM key_value WHERE key = (?)", key.to_s
end
end
end
end
end