/usr/lib/ruby/vendor_ruby/moneta/adapters/sqlite.rb is in ruby-moneta 0.7.20-2.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | require 'sqlite3'
module Moneta
module Adapters
# Sqlite3 backend
# @api public
class Sqlite
include Defaults
include IncrementSupport
supports :create
attr_reader :backend
# @param [Hash] options
# @option options [String] :file Database file
# @option options [String] :table ('moneta') Table name
# @option options [Fixnum] :busy_timeout (1000) Sqlite timeout if database is busy
# @option options [::Sqlite3::Database] :backend Use existing backend instance
def initialize(options = {})
table = options[:table] || 'moneta'
@backend = options[:backend] ||
begin
raise ArgumentError, 'Option :file is required' unless options[:file]
::SQLite3::Database.new(options[:file])
end
@backend.busy_timeout(options[:busy_timeout] || 1000)
@backend.execute("create table if not exists #{table} (k blob not null primary key, v blob)")
@stmts =
[@exists = @backend.prepare("select exists(select 1 from #{table} where k = ?)"),
@select = @backend.prepare("select v from #{table} where k = ?"),
@replace = @backend.prepare("replace into #{table} values (?, ?)"),
@delete = @backend.prepare("delete from #{table} where k = ?"),
@clear = @backend.prepare("delete from #{table}"),
@create = @backend.prepare("insert into #{table} values (?, ?)")]
end
# (see Proxy#key?)
def key?(key, options = {})
@exists.execute!(key).first.first.to_i == 1
end
# (see Proxy#load)
def load(key, options = {})
rows = @select.execute!(key)
rows.empty? ? nil : rows.first.first
end
# (see Proxy#store)
def store(key, value, options = {})
@replace.execute!(key, value)
value
end
# (see Proxy#delete)
def delete(key, options = {})
value = load(key, options)
@delete.execute!(key)
value
end
# (see Proxy#increment)
def increment(key, amount = 1, options = {})
@backend.transaction(:exclusive) { return super }
end
# (see Proxy#clear)
def clear(options = {})
@clear.execute!
self
end
# (see Default#create)
def create(key, value, options = {})
@create.execute!(key,value)
true
rescue SQLite3::ConstraintException
# If you know a better way to detect whether an insert-ignore
# suceeded, please tell me.
@create.reset!
false
end
# (see Proxy#close)
def close
@stmts.each {|s| s.close }
@backend.close
nil
end
end
end
end
|