A Store that persists objects into an Sqlite3 database.
As well as the usual options to the constructor, you can also pass a :busy_timeout option which defines how quickly to retry a query, should the database be locked. The default value is 50ms. The retry will currently continue until successful.
To read documentation about the methods, consult the documentation for SqlStore and Store.
- close
- commit
- db_filename
- destroy_db
- exec_statement
- last_insert_id
- new
- primary_key_type
- query_statement
- rollback
- sql_update
- start
- table_info
Initialize the Sqlite store. This store provides a default name.
[ show source ]
# File lib/og/adapter/sqlite.rb, line 32
32: def initialize(options)
33: super
34: @busy_timeout = (options[:busy_timeout] || 50)/1000
35: @conn = SQLite3::Database.new(db_filename(options))
36: end
[ show source ]
# File lib/og/adapter/sqlite.rb, line 38
38: def close
39: @conn.close
40: super
41: end
[ show source ]
# File lib/og/adapter/sqlite.rb, line 90
90: def commit
91: @transaction_nesting -= 1
92: @conn.commit if @transaction_nesting < 1
93: end
Override if needed.
[ show source ]
# File lib/og/adapter/sqlite.rb, line 45
45: def db_filename(options)
46: options[:name] ||= 'data'
47: if options[:name] == ':memory:' || options[:name] == :memory
48: ':memory:'
49: else
50: "#{options[:name]}.db"
51: end
52: end
[ show source ]
# File lib/og/adapter/sqlite.rb, line 54
54: def destroy_db(options)
55: FileUtils.rm_f(db_filename(options))
56: super
57: end
[ show source ]
# File lib/og/adapter/sqlite.rb, line 78
78: def exec_statement(sql)
79: return @conn.query(sql).close
80: rescue SQLite3::BusyException
81: sleep(@busy_timeout)
82: retry
83: end
[ show source ]
# File lib/og/adapter/sqlite.rb, line 105
105: def last_insert_id(klass = nil)
106: query("SELECT last_insert_rowid()").first_value.to_i
107: end
The type used for default primary keys.
[ show source ]
# File lib/og/adapter/sqlite.rb, line 61
61: def primary_key_type
62: 'integer PRIMARY KEY'
63: end
SQLite send back a BusyException if the database is locked. Currently we keep sending the query until success or the universe implodes. Note that the SQLite3 ruby library provides a busy_timeout, and busy_handler facility, but I couldn’t get the thing to work.
[ show source ]
# File lib/og/adapter/sqlite.rb, line 71
71: def query_statement(sql)
72: return @conn.query(sql)
73: rescue SQLite3::BusyException
74: sleep(@busy_timeout)
75: retry
76: end
[ show source ]
# File lib/og/adapter/sqlite.rb, line 95
95: def rollback
96: @transaction_nesting -= 1
97: @conn.rollback if @transaction_nesting < 1
98: end
[ show source ]
# File lib/og/adapter/sqlite.rb, line 100
100: def sql_update(sql)
101: exec(sql)
102: @conn.changes
103: end
[ show source ]
# File lib/og/adapter/sqlite.rb, line 85
85: def start
86: @conn.transaction if @transaction_nesting < 1
87: @transaction_nesting += 1
88: end
Returns the Sqlite information of a table within the database or nil if it doesn’t exist. Mostly for internal usage.
[ show source ]
# File lib/og/adapter/sqlite.rb, line 112
112: def table_info(table)
113: r = query_statement("SELECT name FROM sqlite_master WHERE type='table' AND name='#{self.class.escape(table.to_s)}'");
114: return r && r.blank? ? nil : r.next
115: end