Class: Og::SqliteStore

Inherits:
SqlStore show all
Defined in:
lib/og/store/sqlite.rb

Overview

A Store that persists objects into an Sqlite3 database. To read documentation about the methods, consult the documentation for SqlStore and Store.

Instance Attribute Summary

Attributes inherited from SqlStore

#conn

Attributes inherited from Store

#options, #transaction_nesting

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SqlStore

#aggregate, #delete_all, #enable_logging, #find, #find_one, #join, #load, #managed_tables, #reload, #select, #select_one, #unjoin, #unmanaged_tables, #update, #update_by_sql, #update_properties

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_date, #parse_float, #parse_int, #parse_timestamp, #quote, #table, #tableize, #timestamp

Methods inherited from Store

#count, create, #delete, #find, for_name, #insert, #load, #reload, #save, #transaction, #update, #update_properties

Constructor Details

#initialize(options) ⇒ SqliteStore

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



60
61
62
63
# File 'lib/og/store/sqlite.rb', line 60

def initialize(options)
  super
  @conn = SQLite3::Database.new(self.class.db_filename(options))
end

Class Method Details

.db_filename(options) ⇒ Object

Override if needed.



43
44
45
46
# File 'lib/og/store/sqlite.rb', line 43

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

.destroy(options) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/og/store/sqlite.rb', line 48

def self.destroy(options)
  begin
    FileUtils.rm(db_filename(options))  
    super
  rescue Object
    Logger.info "Cannot drop '#{options[:name]}'!"
  end
end

Instance Method Details

#closeObject



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

def close
  @conn.close
  super
end

#commitObject



99
100
101
102
# File 'lib/og/store/sqlite.rb', line 99

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

#enchant(klass, manager) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/og/store/sqlite.rb', line 70

def enchant(klass, manager)
  if klass.ann.self.primary_key.symbol == :oid
    unless klass.properties.include? :oid
      klass.property :oid, Fixnum, :sql => 'integer PRIMARY KEY'
    end
  end

  super
end

#exec(sql) ⇒ Object



87
88
89
90
91
92
# File 'lib/og/store/sqlite.rb', line 87

def exec(sql)
  Logger.debug sql if $DBG
  @conn.query(sql).close
rescue => ex
  handle_sql_exception(ex, sql)
end

#last_insert_rowidObject



109
110
111
# File 'lib/og/store/sqlite.rb', line 109

def last_insert_rowid
  conn.query("SELECT last_insert_rowid()").first_value.to_i
end

#query(sql) ⇒ Object



80
81
82
83
84
85
# File 'lib/og/store/sqlite.rb', line 80

def query(sql)
  Logger.debug sql if $DBG
  return @conn.query(sql)
rescue => ex
  handle_sql_exception(ex, sql)
end

#rollbackObject



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

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

#sql_update(sql) ⇒ Object



113
114
115
116
# File 'lib/og/store/sqlite.rb', line 113

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

#startObject



94
95
96
97
# File 'lib/og/store/sqlite.rb', line 94

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