class Irc::Bot::Registry::SqliteAccessor

Public Class Methods

new(filename) click to toggle source
# File lib/rbot/registry/sqlite.rb, line 15
def initialize(filename)
  super filename + '.db'
end

Public Instance Methods

[](key) click to toggle source
# File lib/rbot/registry/sqlite.rb, line 37
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
[]=(key,value) click to toggle source
# File lib/rbot/registry/sqlite.rb, line 50
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
clear() click to toggle source
# File lib/rbot/registry/sqlite.rb, line 105
def clear
  return unless dbexists?
  registry.execute('DELETE FROM data')
end
Also aliased as: truncate
delete(key) click to toggle source
# File lib/rbot/registry/sqlite.rb, line 87
def delete(key)
  return default unless dbexists?
  begin
    value = self[key]
    registry.execute('DELETE FROM data WHERE key = ?', key.to_s)
    value if registry.changes > 0
  rescue
    nil
  end
end
each(&block) click to toggle source
# File lib/rbot/registry/sqlite.rb, line 59
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
Also aliased as: each_pair
each_pair(&block)
Alias for: each
has_key?(key) click to toggle source
# File lib/rbot/registry/sqlite.rb, line 70
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
Also aliased as: include?, member?, key?
has_value?(value) click to toggle source
# File lib/rbot/registry/sqlite.rb, line 80
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
include?(key)
Alias for: has_key?
key?(key)
Alias for: has_key?
keys() click to toggle source

returns a list of your keys

# File lib/rbot/registry/sqlite.rb, line 99
def keys
  return [] unless dbexists?
  res = registry.execute('SELECT key FROM data')
  res.map { |row| row.first }
end
length() click to toggle source

returns the number of keys in your registry namespace

# File lib/rbot/registry/sqlite.rb, line 113
def length
  return 0 unless dbexists?
  res = registry.get_first_row('SELECT COUNT(key) FROM data')
  res.first
end
Also aliased as: size
member?(key)
Alias for: has_key?
optimize() click to toggle source
# File lib/rbot/registry/sqlite.rb, line 32
def optimize
  return unless @registry
  @registry.execute('VACUUM')
end
registry() click to toggle source
# File lib/rbot/registry/sqlite.rb, line 19
def registry
  super
  unless @registry
    @registry = SQLite3::Database.new(@filename)
    begin
      @registry.execute('SELECT COUNT(*) FROM data')
    rescue
      @registry.execute('CREATE TABLE data (key PRIMARY KEY, value)')
    end
  end
  @registry
end
size()
Alias for: length
truncate()
Alias for: clear