diff options
Diffstat (limited to 'lib/rbot/journal/postgres.rb')
-rw-r--r-- | lib/rbot/journal/postgres.rb | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/lib/rbot/journal/postgres.rb b/lib/rbot/journal/postgres.rb index 57c539a7..65c67eb9 100644 --- a/lib/rbot/journal/postgres.rb +++ b/lib/rbot/journal/postgres.rb @@ -53,9 +53,14 @@ module Journal [m.id, m.topic, m.timestamp, JSON.generate(m.payload)]) end - def find(query, limit=100, offset=0) - sql, params = query_to_sql(query) - sql = 'SELECT * FROM journal WHERE ' + sql + ' LIMIT %d OFFSET %d' % [limit.to_i, offset.to_i] + def find(query=nil, limit=100, offset=0) + if query + sql, params = query_to_sql(query) + sql = 'SELECT * FROM journal WHERE ' + sql + ' LIMIT %d OFFSET %d' % [limit.to_i, offset.to_i] + else + sql = 'SELECT * FROM journal LIMIT %d OFFSET %d' % [limit.to_i, offset.to_i] + params = [] + end res = @conn.exec_params(sql, params) res.map do |row| timestamp = DateTime.strptime(row['timestamp'], '%Y-%m-%d %H:%M:%S%z') @@ -65,13 +70,29 @@ module Journal end # returns the number of messages that match the query - def count(query) - sql, params = query_to_sql(query) - sql = 'SELECT COUNT(*) FROM journal WHERE ' + sql + def count(query=nil) + if query + sql, params = query_to_sql(query) + sql = 'SELECT COUNT(*) FROM journal WHERE ' + sql + else + sql = 'SELECT COUNT(*) FROM journal' + params = [] + end res = @conn.exec_params(sql, params) res[0]['count'].to_i end + def remove(query=nil) + if query + sql, params = query_to_sql(query) + sql = 'DELETE FROM journal WHERE ' + sql + else + sql = 'DELETE FROM journal;' + params = [] + end + res = @conn.exec_params(sql, params) + end + def drop @conn.exec('DROP TABLE journal;') rescue nil end |