Class: Irc::Bot::Registry::SqliteAccessor

Inherits:
AbstractAccessor show all
Defined in:
/home/apoc/projects/ruby/rbot/lib/rbot/registry/sqlite.rb

Instance Attribute Summary

Attributes inherited from AbstractAccessor

#filename, #recovery

Instance Method Summary (collapse)

Methods inherited from AbstractAccessor

#close, #create_folders, #dbexists?, #default, #each_key, #each_value, #flush, get_impl, #index, #restore, #set_default, #store, #sub_registry, #to_a, #to_hash, #values

Constructor Details

- (SqliteAccessor) initialize(filename)

Returns a new instance of SqliteAccessor



15
16
17
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry/sqlite.rb', line 15

def initialize(filename)
  super filename + '.db'
end

Instance Method Details

- (Object) [](key)



37
38
39
40
41
42
43
44
45
46
47
48
# File '/home/apoc/projects/ruby/rbot/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

- (Object) []=(key, value)



50
51
52
53
54
55
56
57
# File '/home/apoc/projects/ruby/rbot/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

- (Object) clear Also known as: truncate



105
106
107
108
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry/sqlite.rb', line 105

def clear
  return unless dbexists?
  registry.execute('DELETE FROM data')
end

- (Object) delete(key)



87
88
89
90
91
92
93
94
95
96
# File '/home/apoc/projects/ruby/rbot/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

- (Object) each(&block) Also known as: each_pair



59
60
61
62
63
64
65
66
# File '/home/apoc/projects/ruby/rbot/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

- (Boolean) has_key?(key) Also known as: include?, member?, key?

Returns:

  • (Boolean)


70
71
72
73
74
# File '/home/apoc/projects/ruby/rbot/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

- (Boolean) has_value?(value)

Returns:

  • (Boolean)


80
81
82
83
84
85
# File '/home/apoc/projects/ruby/rbot/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

- (Object) keys

returns a list of your keys



99
100
101
102
103
# File '/home/apoc/projects/ruby/rbot/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

- (Object) length Also known as: size

returns the number of keys in your registry namespace



113
114
115
116
117
# File '/home/apoc/projects/ruby/rbot/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

- (Object) optimize



32
33
34
35
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry/sqlite.rb', line 32

def optimize
  return unless @registry
  @registry.execute('VACUUM')
end

- (Object) registry



19
20
21
22
23
24
25
26
27
28
29
30
# File '/home/apoc/projects/ruby/rbot/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