summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias H <apoc@sixserv.org>2014-03-06 12:46:47 +0100
committerMatthias H <apoc@sixserv.org>2014-03-06 12:46:47 +0100
commitbaf92378d7511983ae24cc41c7e9a86e73923bf4 (patch)
tree650d436a747ba7ede23c62fd149580476599399c
parentd642da9348926e418655685b92f0b68c11b710f1 (diff)
[registry] added sqlite3 database adapter
-rw-r--r--lib/rbot/registry.rb13
-rw-r--r--lib/rbot/registry/sqlite.rb116
2 files changed, 121 insertions, 8 deletions
diff --git a/lib/rbot/registry.rb b/lib/rbot/registry.rb
index 0eefc9da..4fd5f77c 100644
--- a/lib/rbot/registry.rb
+++ b/lib/rbot/registry.rb
@@ -220,7 +220,7 @@ class Registry
# like Hash#each
def each(&block)
return nil unless dbexists?
- registry.each do |key|
+ registry.each_key do |key|
block.call(key, self[key])
end
end
@@ -268,7 +268,10 @@ class Registry
# delete a key from the registry
def delete(key)
return default unless dbexists?
- return registry.delete(key.to_s)
+ value = registry.delete(key.to_s)
+ if value
+ restore(value)
+ end
end
# returns a list of your keys
@@ -277,12 +280,6 @@ class Registry
return registry.keys
end
- # just like Hash#has_both?
- def has_both?(key, value)
- return false unless dbexists?
- registry.has_key?(key.to_s) and registry.has_value?(store(value))
- end
-
# Return an array of all associations [key, value] in your namespace
def to_a
return [] unless dbexists?
diff --git a/lib/rbot/registry/sqlite.rb b/lib/rbot/registry/sqlite.rb
new file mode 100644
index 00000000..89e7d78c
--- /dev/null
+++ b/lib/rbot/registry/sqlite.rb
@@ -0,0 +1,116 @@
+#-- vim:sw=2:et
+#++
+#
+# :title: Sqlite3 registry implementation
+#
+
+require 'sqlite3'
+
+module Irc
+class Bot
+class Registry
+
+ class SqliteAccessor < AbstractAccessor
+
+ def initialize(filename)
+ super filename + '.db'
+ end
+
+ def registry
+ super
+ unless @registry
+ @registry = SQLite3::Database.new(@filename)
+ begin
+ @registry.execute('SELECT COUNT(*) FROM data')
+ rescue
+ @registry.execute('CREATE TABLE data (key string, value blob)')
+ end
+ end
+ @registry
+ end
+
+ def flush
+ end
+
+ def optimize
+ end
+
+ def [](key)
+ if dbexists?
+ begin
+ value = @registry.get_first_row('SELECT value FROM data WHERE key = ?', key.to_s)
+ return restore(value.first)
+ rescue
+ return default
+ end
+ else
+ return default
+ end
+ end
+
+ def []=(key,value)
+ value = SQLite3::Blob.new(store(value))
+ if has_key? key
+ registry.execute('UPDATE data SET value = ? WHERE key = ?', value, key.to_s)
+ else
+ registry.execute('INSERT INTO data VALUES (?, ?)', key.to_s, value)
+ end
+ end
+
+ def each(&block)
+ return nil unless dbexists?
+ res = registry.execute('SELECT * FROM data')
+ res.each do |row|
+ key, value = row
+ block.call(key, restore(value))
+ end
+ end
+
+ def has_key?(key)
+ return nil unless dbexists?
+ res = registry.get_first_row('SELECT COUNT(*) FROM data WHERE key = ?', key.to_s)
+ return res.first > 0
+ end
+
+ def has_value?(value)
+ return nil unless dbexists?
+ value = SQLite3::Blob.new(store(value))
+ res = registry.get_first_row('SELECT COUNT(*) FROM data WHERE value = ?', value)
+ return res.first > 0
+ end
+
+ def delete(key)
+ return default unless dbexists?
+ begin
+ registry.execute('DELETE FROM data WHERE key = ?', key.to_s)
+ registry.changes > 0
+ rescue
+ false
+ end
+ end
+
+ # returns a list of your keys
+ def keys
+ return [] unless dbexists?
+ res = registry.execute('SELECT key FROM data')
+ res.map { |row| row.first }
+ end
+
+ def clear
+ return unless dbexists?
+ registry.execute('DELETE FROM data')
+ end
+
+ # returns the number of keys in your registry namespace
+ def length
+ return 0 unless dbexists?
+ res = registry.get_first_row('SELECT COUNT(key) FROM data')
+ res.first
+ end
+
+ end
+
+end # Registry
+end # Bot
+end # Irc
+