Class: Og::SqliteAdapter

Inherits:
SqlStore show all
Extended by:
SqlUtils
Includes:
SqlUtils
Defined in:
lib/og/adapter/sqlite.rb

Overview

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.

Instance Attribute Summary

Attributes inherited from SqlStore

#conn, #typemap

Attributes inherited from Store

#ogmanager

Instance Method Summary collapse

Methods included from SqlUtils

blob, build_join_name, create_join_table_sql, date, escape, join_class_ordering, join_object_ordering, join_table, join_table_index, join_table_info, join_table_key, join_table_keys, ordered_join_table_keys, parse_blob, parse_boolean, parse_date, parse_float, parse_int, parse_timestamp, quote, quote_array, table, tableize, timestamp

Methods inherited from SqlStore

#aggregate, #count, #create_db, #create_field_map, #create_table, #delete_all, #drop_table, #enchant, #eval_og_allocate, #eval_og_create_schema, #eval_og_delete, #eval_og_insert, #eval_og_read, #eval_og_update, #exec, #field_for_attribute, #field_sql_for_attribute, #fields_for_class, #find, #find_one, #force_primary_key, #handle_sql_exception, #insert_sql, #join, #load, #pk_field, #prepare_statement, #read_all, #read_attr, #read_field, #read_join_relations, #read_one, #read_row, #reload, #resolve_limit_options, #resolve_options, #select, #select_one, #serializable_attributes_for_class, #sql_indices_for_class, #sql_type_for_class, #table_exists?, #type_cast, #unjoin, #update, #update_by_sql, #update_condition, #write_attr

Methods inherited from Store

#count, #delete, #delete_all, #enchant, #find, #force_save!, #insert, #load, #reload, #save, #transaction, #update, #update_attributes

Constructor Details

#initialize(options) ⇒ SqliteAdapter

Initialize the Sqlite store. This store provides a default name.



32
33
34
35
36
# File 'lib/og/adapter/sqlite.rb', line 32

def initialize(options)
  super
  @busy_timeout = (options[:busy_timeout] || 50)/1000
  @conn = SQLite3::Database.new(db_filename(options))
end

Instance Method Details

#closeObject



38
39
40
41
# File 'lib/og/adapter/sqlite.rb', line 38

def close
  @conn.close
  super
end

#commitObject



82
83
84
85
# File 'lib/og/adapter/sqlite.rb', line 82

def commit
  @transaction_nesting -= 1
  @conn.commit if @transaction_nesting < 1
end

#db_filename(options) ⇒ Object

Override if needed.



45
46
47
48
49
50
51
52
# File 'lib/og/adapter/sqlite.rb', line 45

def db_filename(options)
  options[:name] ||= 'data'
  if options[:name] == ':memory:'
    ':memory:'
  else
    "#{options[:name]}.db"
  end
end

#destroy_db(options) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/og/adapter/sqlite.rb', line 54

def destroy_db(options)
  begin
    FileUtils.rm(db_filename(options))  
    super
  rescue Errno::ENOENT => ex # FIXME: Lookup Win32/Linux/BSD error
    Logger.info "Cannot drop '#{options[:name]}'!"
  end
end

#exec_statement(sql) ⇒ Object



73
74
75
# File 'lib/og/adapter/sqlite.rb', line 73

def exec_statement(sql)
  query(sql).close()
end

#last_insert_id(klass = nil) ⇒ Object



97
98
99
# File 'lib/og/adapter/sqlite.rb', line 97

def last_insert_id(klass = nil)
  query("SELECT last_insert_rowid()").first_value.to_i
end

#primary_key_typeObject

The type used for default primary keys.



65
66
67
# File 'lib/og/adapter/sqlite.rb', line 65

def primary_key_type
  'integer PRIMARY KEY'
end

#query(sql) ⇒ Object

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.



113
114
115
116
117
118
119
120
# File 'lib/og/adapter/sqlite.rb', line 113

def query(sql)
  begin
    return @conn.query(sql)
  rescue SQLite3::BusyException
    sleep(@busy_timeout)
    retry
  end
end

#query_statement(sql) ⇒ Object



69
70
71
# File 'lib/og/adapter/sqlite.rb', line 69

def query_statement(sql)
  return query(sql)
end

#rollbackObject



87
88
89
90
# File 'lib/og/adapter/sqlite.rb', line 87

def rollback
  @transaction_nesting -= 1
  @conn.rollback if @transaction_nesting < 1
end

#sql_update(sql) ⇒ Object



92
93
94
95
# File 'lib/og/adapter/sqlite.rb', line 92

def sql_update(sql)
  exec(sql)
  @conn.changes
end

#startObject



77
78
79
80
# File 'lib/og/adapter/sqlite.rb', line 77

def start
  @conn.transaction if @transaction_nesting < 1
  @transaction_nesting += 1
end

#table_info(table) ⇒ Object

Returns the Sqlite information of a table within the database or nil if it doesn’t exist. Mostly for internal usage.



104
105
106
107
# File 'lib/og/adapter/sqlite.rb', line 104

def table_info(table)
  r = query_statement("SELECT name FROM sqlite_master WHERE type='table' AND name='#{self.class.escape(table.to_s)}'");
  return r && r.blank? ? nil : r.next
end