-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStore.rb
49 lines (39 loc) · 1.63 KB
/
Store.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
require 'mysql2'
class Store
attr :conn
def initialize()
host = Properties['db']['host']
user = Properties['db']['user']
password = Properties['db']['password']
database = Properties['db']['database']
@conn = Mysql2::Client.new(:host => host, :username => user, :password => password, :database => database)
end
def delete()
conn.query("delete from top_fav");
end
def queue(batch, seq, photo_id)
puts "queueing #{photo_id}"
conn.query("insert into top_fav (batch, seq, photo_id) values ('#{batch}', '#{seq}', '#{photo_id}')");
end
def hasPhoto?(photo_id)
results = conn.query("select * from posted_entries where photo_id = '#{photo_id}'")
return results.any?
end
def persist(photo_id, photo_url, tweet, batch)
conn.query("insert into posted_entries (photo_id, photo_url, tweet, tweet_date, batch, enabled_flag) values ('#{photo_id}', '#{photo_url}', '#{tweet}', now(), '#{batch}', true)");
end
def hasQueue?(batch)
results = conn.query("select * from top_fav where batch = '#{batch}'")
return results.any?
end
def pop(batch)
results = conn.query("select min(seq) as top from top_fav where batch = '#{batch}'")
top = results.first["top"]
puts "top #{top}"
results = conn.query("select photo_id from top_fav where seq = #{top} and batch = '#{batch}'")
photo_id = results.first["photo_id"]
puts "photo #{photo_id}"
conn.query("delete from top_fav where seq = #{top} and batch = '#{batch}'")
return photo_id
end
end