forked from QueueClassic/queue_classic
/
durable_array.rb
51 lines (40 loc) · 1.12 KB
/
durable_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
49
50
51
module QC
class DurableArray
def initialize(database)
@database = database
@table_name = @database.table_name
@top_boundary = @database.top_boundary
end
def <<(details)
execute("INSERT INTO #{@table_name} (details) VALUES ($1);", JSON.dump(details))
@database.notify if ENV["QC_LISTENING_WORKER"] == "true"
end
def count
execute("SELECT COUNT(*) FROM #{@table_name}")[0]["count"].to_i
end
def delete(job)
execute("DELETE FROM #{@table_name} WHERE id = $1;", job.id)
job
end
def search_details_column(q)
find_many { ["SELECT * FROM #{@table_name} WHERE details LIKE $1;", "%#{q}%"] }
end
def first
find_one { ["SELECT * FROM lock_head($1, $2);", @table_name, @top_boundary] }
end
def each
execute("SELECT * FROM #{@table_name} ORDER BY id ASC;").each do |r|
yield Job.new(r)
end
end
def find_one(&blk)
find_many(&blk).pop
end
def find_many
execute(*yield).map { |r| Job.new(r) }
end
def execute(sql, *params)
@database.execute(sql, *params)
end
end
end