Class: EventMachine::EmailServer::Sqlite3EmailStore
- Inherits:
-
AbstractEmailStore
- Object
- AbstractEmailStore
- EventMachine::EmailServer::Sqlite3EmailStore
- Defined in:
- lib/eventmachine/email_server/sqlite3.rb
Instance Method Summary collapse
- #count ⇒ Object
- #delete_by_field(field, value) ⇒ Object
- #delete_email(email) ⇒ Object
- #delete_id(id) ⇒ Object
- #delete_user(uid) ⇒ Object
- #emails_by_field(field, value) ⇒ Object
- #emails_by_userid(uid) ⇒ Object
-
#initialize(sqlite3, tablename = "emails") ⇒ Sqlite3EmailStore
constructor
A new instance of Sqlite3EmailStore.
- #save_email(email) ⇒ Object
Constructor Details
#initialize(sqlite3, tablename = "emails") ⇒ Sqlite3EmailStore
Returns a new instance of Sqlite3EmailStore.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/eventmachine/email_server/sqlite3.rb', line 72 def initialize(sqlite3, tablename = "emails") @class = Email @fields = "'"+@class.members.map {|x| x.to_s}.join("', '")+"'" @tablename = tablename if sqlite3.class == SQLite3::Database @db = sqlite3 else @db = SQLite3::Database.new(sqlite3) end if @db.table_info(tablename).length == 0 fields = @fields.gsub(/'id'/, 'id integer primary key autoincrement') @db.execute("CREATE TABLE #{@tablename} (#{fields})") end end |
Instance Method Details
#count ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/eventmachine/email_server/sqlite3.rb', line 148 def count sql = "SELECT COUNT(*) FROM #{@tablename}" rs = @db.execute(sql) c = 0 rs.each do |row| c = row[0] end c end |
#delete_by_field(field, value) ⇒ Object
135 136 137 138 |
# File 'lib/eventmachine/email_server/sqlite3.rb', line 135 def delete_by_field(field, value) sql = "DELETE FROM #{@tablename} WHERE #{field} = ?" @db.execute(sql, value) end |
#delete_email(email) ⇒ Object
129 130 131 132 133 |
# File 'lib/eventmachine/email_server/sqlite3.rb', line 129 def delete_email(email) if email.id delete_by_field("id", email.id) end end |
#delete_id(id) ⇒ Object
140 141 142 |
# File 'lib/eventmachine/email_server/sqlite3.rb', line 140 def delete_id(id) delete_by_field("id", id) end |
#delete_user(uid) ⇒ Object
144 145 146 |
# File 'lib/eventmachine/email_server/sqlite3.rb', line 144 def delete_user(uid) delete_by_field("uid", uid) end |
#emails_by_field(field, value) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/eventmachine/email_server/sqlite3.rb', line 87 def emails_by_field(field, value) sql = "SELECT * FROM #{@tablename} WHERE #{quote(field)}='#{quote(value.to_s)}'" rs = @db.execute(sql) return nil unless rs emails = Array.new rs.each do |row| emails << Email.new(*row) end emails end |
#emails_by_userid(uid) ⇒ Object
98 99 100 |
# File 'lib/eventmachine/email_server/sqlite3.rb', line 98 def emails_by_userid(uid) emails_by_field("uid", uid) end |
#save_email(email) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/eventmachine/email_server/sqlite3.rb', line 107 def save_email(email) if email.id # I'm being too crafty here.. this is bad style args = (@class.members - [:id]).map{|f| email.send(f)} args << email.send(:id) @db.execute("UPDATE #{@tablename} SET " + (@class.members - [:id]).map { |field| "#{field} = ?"}.join(", ") + " WHERE id = ?", *args) else email.id = "NULL" args = (@class.members).map{|f| email.send(f)} qs = args.map{|x| "'#{quote(x.to_s)}'"}.join(",").gsub(/'NULL'/, "NULL") sql = "INSERT INTO #{@tablename} (#{@fields}) VALUES (#{qs})" @db.execute(sql) rs = @db.execute("SELECT last_insert_rowid()") rs.each do |row| email.id = *row end end email.id end |