Class: Og::SqliteStore
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, #count, #delete_all, #enable_logging, #find, #find_one, #join, #load, #managed_tables, #prepare_statement, #reload, #select, #select_one, #type_cast, #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_boolean, #parse_date, #parse_float, #parse_int, #parse_timestamp, #quote, #quote_array, #table, #tableize, #timestamp
Methods inherited from Store
#count, create, #delete, #find, for_name, #force_save!, #insert, #load, #reload, #save, #transaction, #update, #update_properties
Constructor Details
#initialize(options) ⇒ SqliteStore
Initialize the Sqlite store. This store provides a default name.
63
64
65
66
|
# File 'lib/og/store/sqlite.rb', line 63
def initialize(options)
super
@conn = SQLite3::Database.new(self.class.db_filename(options))
end
|
Class Method Details
.db_filename(options) ⇒ Object
46
47
48
49
|
# File 'lib/og/store/sqlite.rb', line 46
def self.db_filename(options)
options[:name] ||= 'data'
"#{options[:name]}.db"
end
|
.destroy(options) ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/og/store/sqlite.rb', line 51
def self.destroy(options)
begin
FileUtils.rm(db_filename(options))
super
rescue Object
Logger.info "Cannot drop '#{options[:name]}'!"
end
end
|
Instance Method Details
#close ⇒ Object
68
69
70
71
|
# File 'lib/og/store/sqlite.rb', line 68
def close
@conn.close
super
end
|
#commit ⇒ Object
102
103
104
105
|
# File 'lib/og/store/sqlite.rb', line 102
def commit
@transaction_nesting -= 1
@conn.commit if @transaction_nesting < 1
end
|
#enchant(klass, manager) ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/og/store/sqlite.rb', line 73
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
90
91
92
93
94
95
|
# File 'lib/og/store/sqlite.rb', line 90
def exec(sql)
Logger.debug sql if $DBG
@conn.query(sql).close
rescue => ex
handle_sql_exception(ex, sql)
end
|
#last_insert_rowid ⇒ Object
112
113
114
|
# File 'lib/og/store/sqlite.rb', line 112
def last_insert_rowid
conn.query("SELECT last_insert_rowid()").first_value.to_i
end
|
#list_fields(table) ⇒ Object
126
127
128
129
130
131
132
|
# File 'lib/og/store/sqlite.rb', line 126
def list_fields(table)
rset = @conn.query("pragma table_info(#{table})")
columns = rset.columns
rows = rset.entries
idx = columns.index('name')
rows.map { |r| r[idx] }
end
|
#list_tables ⇒ Object
121
122
123
124
|
# File 'lib/og/store/sqlite.rb', line 121
def list_tables
rset = @conn.query("select name from sqlite_master where type = 'table'")
rset.map { |r| r[0] }
end
|
#query(sql) ⇒ Object
83
84
85
86
87
88
|
# File 'lib/og/store/sqlite.rb', line 83
def query(sql)
Logger.debug sql if $DBG
return @conn.query(sql)
rescue Exception => ex
handle_sql_exception(ex, sql)
end
|
#read_one(res, klass, options = nil) ⇒ Object
Deserialize one object from the ResultSet.
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/og/store/sqlite.rb', line 136
def read_one(res, klass, options = nil)
return nil if res.blank?
if options and join_relations = options[:include]
join_relations = [join_relations].flatten.collect do |n|
klass.relation(n)
end
end
res_row = res.next
klass = Og::Entity::entity_from_string(res_row[0]) if klass.schema_inheritance?
obj = klass.og_allocate(res_row, 0)
if options and options[:select]
read_row(obj, res, res_row, 0)
else
obj.og_read(res_row)
read_join_relations(obj, res_row, 0, join_relations) if join_relations
end
return obj
ensure
res.close
end
|
#rollback ⇒ Object
107
108
109
110
|
# File 'lib/og/store/sqlite.rb', line 107
def rollback
@transaction_nesting -= 1
@conn.rollback if @transaction_nesting < 1
end
|
#sql_update(sql) ⇒ Object
116
117
118
119
|
# File 'lib/og/store/sqlite.rb', line 116
def sql_update(sql)
exec(sql)
@conn.changes
end
|
#start ⇒ Object
97
98
99
100
|
# File 'lib/og/store/sqlite.rb', line 97
def start
@conn.transaction if @transaction_nesting < 1
@transaction_nesting += 1
end
|