Class: Og::SqliteConnection

Inherits:
Connection show all
Defined in:
lib/og/adapters/sqlite.rb

Overview

The SQLite connection.

Instance Attribute Summary

Attributes inherited from Connection

#db, #store

Instance Method Summary collapse

Methods inherited from Connection

#count, #create_table, #delete, #drop_table, #insert, #load, #load_all, #load_by_name, #load_by_oid, #save, #select, #select_one, #transaction, #update, #update_properties

Constructor Details

#initialize(db) ⇒ SqliteConnection

Returns a new instance of SqliteConnection.



177
178
179
180
# File 'lib/og/adapters/sqlite.rb', line 177

def initialize(db)
	@store = SQLite3::Database.new("#{db.config[:database]}.db")
	super
end

Instance Method Details

#closeObject



182
183
184
185
# File 'lib/og/adapters/sqlite.rb', line 182

def close
	@store.close
	super
end

#commitObject



216
217
218
# File 'lib/og/adapters/sqlite.rb', line 216

def commit
	@store.commit
end

#exec(sql) ⇒ Object



202
203
204
205
206
207
208
209
210
# File 'lib/og/adapters/sqlite.rb', line 202

def exec(sql)
	Logger.debug sql if $DBG
	begin
		@store.query(sql).close 
	rescue => ex
		Logger.error "DB error #{ex}, [#{sql}]"
		Logger.error ex.backtrace.join("\n")
	end
end

#prepare(sql) ⇒ Object



187
188
189
# File 'lib/og/adapters/sqlite.rb', line 187

def prepare(sql)
	@store.prepare(sql)
end

#query(sql) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/og/adapters/sqlite.rb', line 191

def query(sql)
	Logger.debug sql if $DBG
	begin
		return @store.query(sql)
	rescue => ex
		Logger.error "DB error #{ex}, [#{sql}]"
		Logger.error ex.backtrace.join("\n")
		return nil
	end
end

#read_all(res, klass) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/og/adapters/sqlite.rb', line 240

def read_all(res, klass)
	return [] unless valid_res?(res)
	objects = []

	res.each do |row|
		obj = klass.new
		obj.og_read(row)
		objects << obj
	end
	
	res.close
	return objects
end

#read_int(res, idx = 0) ⇒ Object



254
255
256
257
258
# File 'lib/og/adapters/sqlite.rb', line 254

def read_int(res, idx = 0)
	val = res.next[idx].to_i
	res.close
	return val
end

#read_one(res, klass) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/og/adapters/sqlite.rb', line 228

def read_one(res, klass)
	return nil unless valid_res?(res)
	row = res.next
	return nil unless row

	obj = klass.new
	obj.og_read(row)

	res.close
	return obj
end

#rollbackObject



220
221
222
# File 'lib/og/adapters/sqlite.rb', line 220

def rollback
	@store.rollback
end

#startObject



212
213
214
# File 'lib/og/adapters/sqlite.rb', line 212

def start
	@store.transaction
end

#valid_res?(res) ⇒ Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/og/adapters/sqlite.rb', line 224

def valid_res?(res)
	return !(res.nil?)
end