Class: Sparrow::Queues::Sqlite
- Inherits:
-
Object
- Object
- Sparrow::Queues::Sqlite
- Includes:
- Miscel
- Defined in:
- lib/sparrow/queues/sqlite.rb
Instance Attribute Summary collapse
-
#count_pop ⇒ Object
Returns the value of attribute count_pop.
-
#count_push ⇒ Object
Returns the value of attribute count_push.
-
#db ⇒ Object
Returns the value of attribute db.
-
#queue_name ⇒ Object
Returns the value of attribute queue_name.
Instance Method Summary collapse
- #all ⇒ Object
- #clear! ⇒ Object
- #count ⇒ Object
-
#initialize(queue_name) ⇒ Sqlite
constructor
A new instance of Sqlite.
- #insert(values) ⇒ Object
- #pop ⇒ Object
- #push(value) ⇒ Object
Methods included from Miscel
#base_dir, #log_path, #logger, #options, #options=, #pid_dir
Constructor Details
#initialize(queue_name) ⇒ Sqlite
Returns a new instance of Sqlite.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sparrow/queues/sqlite.rb', line 12 def initialize(queue_name) self.queue_name = queue_name self.count_pop = 0 self.count_push = 0 db_exists = File.exists?(db_path) self.db = SQLite3::Database.new( db_path ) if !db_exists self.db.execute_batch <<-SQL CREATE TABLE queues ( id INTEGER PRIMARY KEY, data VARCHAR(255) ); PRAGMA default_synchronous=OFF; PRAGMA count_changes=OFF; SQL end end |
Instance Attribute Details
#count_pop ⇒ Object
Returns the value of attribute count_pop.
9 10 11 |
# File 'lib/sparrow/queues/sqlite.rb', line 9 def count_pop @count_pop end |
#count_push ⇒ Object
Returns the value of attribute count_push.
10 11 12 |
# File 'lib/sparrow/queues/sqlite.rb', line 10 def count_push @count_push end |
#db ⇒ Object
Returns the value of attribute db.
8 9 10 |
# File 'lib/sparrow/queues/sqlite.rb', line 8 def db @db end |
#queue_name ⇒ Object
Returns the value of attribute queue_name.
7 8 9 |
# File 'lib/sparrow/queues/sqlite.rb', line 7 def queue_name @queue_name end |
Instance Method Details
#all ⇒ Object
52 53 54 |
# File 'lib/sparrow/queues/sqlite.rb', line 52 def all db.execute("SELECT * FROM queues").collect {|v| v[1] } end |
#clear! ⇒ Object
48 49 50 |
# File 'lib/sparrow/queues/sqlite.rb', line 48 def clear! db.execute("DELETE FROM queues") end |
#count ⇒ Object
44 45 46 |
# File 'lib/sparrow/queues/sqlite.rb', line 44 def count db.get_first_value("SELECT count(*) FROM queues").to_i end |
#insert(values) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/sparrow/queues/sqlite.rb', line 56 def insert(values) db.transaction do |d| values.each do |v| db.execute("INSERT INTO queues (data) VALUES (?)", v) end end end |
#pop ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/sparrow/queues/sqlite.rb', line 36 def pop id, value = db.get_first_row("SELECT * FROM queues LIMIT 1;") return unless id and value db.execute("DELETE FROM queues WHERE id = ?", id) self.count_pop += 1 value end |
#push(value) ⇒ Object
30 31 32 33 34 |
# File 'lib/sparrow/queues/sqlite.rb', line 30 def push(value) db.execute("INSERT INTO queues (data) VALUES (?);", value) self.count_push += 1 value end |