Class: Audrey::Engine::SQLite3

Inherits:
Audrey::Engine show all
Extended by:
Forwardable
Defined in:
lib/audrey/engine/sqlite3.rb

Overview

Audrey::Engine::SQLite3

Defined Under Namespace

Modules: Init Classes: Query, Transaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p_path, p_mode, opts = {}) ⇒ SQLite3


initialize



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/audrey/engine/sqlite3.rb', line 19

def initialize(p_path, p_mode, opts={})
	# default options
	opts = {'immediate_commit'=>true}.merge(opts)
	
	# set some properties
	@path = p_path
	@immediate_commit = opts['immediate_commit'] ? true : false
	@preps = {}
	@mode = p_mode
	
	# KLUDGE: For now, default to partition 'm'
	@partition = 'm'
	
	# lock file
	set_lock()
	
	# if file exists
	existed = File.exist?(@path)
	
	# open database handle
	@dbh = get_dbh()
	
	# custom functions
	custom_functions()
	
	# create and structure database if the file doesn't exist
	if not existed
		require 'audrey/engine/sqlite3/init'
		Audrey::Engine::SQLite3::Init.build @dbh
	end
	
	# transactions
	@transactions = []
	
	# initial transaction
	if @immediate_commit
		@transactions.push Audrey::Engine::SQLite3::Transaction::AutoCommit.new(self)
	else
		@transactions.push Audrey::Engine::SQLite3::Transaction::RC.new(self)
	end
	
	# create stuff just for this session
	self.class.session_tables @dbh
end

Instance Attribute Details

#dbhObject (readonly)

Returns the value of attribute dbh.



8
9
10
# File 'lib/audrey/engine/sqlite3.rb', line 8

def dbh
  @dbh
end

#partitionObject

Returns the value of attribute partition.



12
13
14
# File 'lib/audrey/engine/sqlite3.rb', line 12

def partition
  @partition
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/audrey/engine/sqlite3.rb', line 10

def path
  @path
end

#prepsObject (readonly)

Returns the value of attribute preps.



11
12
13
# File 'lib/audrey/engine/sqlite3.rb', line 11

def preps
  @preps
end

#transactionsObject (readonly)

Returns the value of attribute transactions.



9
10
11
# File 'lib/audrey/engine/sqlite3.rb', line 9

def transactions
  @transactions
end

Instance Method Details

#add_relationship(parent_pk, child_pk, opts = {}) ⇒ Object


add_relationship



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/audrey/engine/sqlite3.rb', line 505

def add_relationship(parent_pk, child_pk, opts={})
	# ensure prepared statement
	if not @preps['add_relationship']
		sql = <<~SQL
		insert into relationships(pk, parent, child, ord, hkey)
			values(
				:pk,
				:parent,
				:child,
		
				case
					when :ord is null then
						coalesce((select max(ord) from relationships where parent=parent), 0) + 1
					else :ord
				end,
		:hkey)
		SQL
		
		@preps['add_relationship'] = @dbh.prepare(sql)
	end
	
	# uuid for new relationship record
	pk = Audrey::Util.uuid
	
	# execute
	begin
		@preps['add_relationship'].execute(
			sql,
			'pk'=>pk,
			'parent'=>parent_pk,
			'child'=>child_pk,
			'ord'=>opts['ord'],
			'hkey'=>opts['hkey']
		);
	ensure
		@preps['add_relationship'].reset!
	end
	
	# return
	return pk
end

#add_xt(obj_pk, xt_pk) ⇒ Object


add_xt



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/audrey/engine/sqlite3.rb', line 451

def add_xt(obj_pk, xt_pk)
	# $tm.hrm
	
	# prepare insert statement if necessary
	if not @preps['insert_xt']
		sql = <<~SQL
		insert into objects(pk, partition, hsa, aclass)
		values(?, (select partition from objects where pk=?), 'h', 'xt')
		SQL
		
		@preps['insert_xt'] ||= @dbh.prepare(sql)
	end
	
	# create xt
	simple_exec @preps['insert_xt'], xt_pk, obj_pk
	
	# associate record
	@preps['add_xt'] ||= @dbh.prepare('update objects set xt=:xtpk where pk=:pk and partition=:partition')
	simple_exec @preps['add_xt'], 'xtpk'=>xt_pk, 'pk'=>obj_pk, 'partition'=>@partition
end

#ancestors(child_pk) ⇒ Object


ancestors



949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/audrey/engine/sqlite3.rb', line 949

def ancestors(child_pk)
	# sql to add parents
	if not @preps['ancestors']
		sql = <<~'SQL'
		select *
			from objects where pk in (
				with recursive
					ancestors(c) as (
						values(:child_pk)
						union
						select parent from relationships, ancestors
						where relationships.child=ancestors.c
					)
				select parent from relationships
					where relationships.child in ancestors
		);
		SQL
		
		@preps['ancestors'] = @dbh.prepare(sql)
	end
	
	# loop through results
	@preps['ancestors'].execute('child_pk'=>child_pk).each do |row|
		yield row
	end
ensure
	@preps['ancestors'] and @preps['ancestors'].reset!
end

#array_each(parent_pk) ⇒ Object


array_each



889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# File 'lib/audrey/engine/sqlite3.rb', line 889

def array_each(parent_pk)
	# $tm.hrm
	
	# ensure statement handle
	if not @preps['array_each']
		sql = <<~SQL
		select r.ord as ord, o.*
		from relationships r, objects o
		where r.parent=:parent and o.pk=r.child
		order by r.ord
		SQL
		
		@preps['array_each'] = @dbh.prepare(sql)
	end
	
	# loop through records
	@preps['array_each'].execute('parent'=>parent_pk).each_hash do |row|
		yield row
	end
ensure
	@preps['array_each'] and @preps['array_each'].reset!
end

#changed_get(pk) ⇒ Object


changed_get



784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'lib/audrey/engine/sqlite3.rb', line 784

def changed_get(pk)
	# $tm.hrm
	
	# prepare
	@preps['changed_get'] ||= @dbh.prepare('select changed from objects where pk=:pk')
	
	# execute
	@preps['changed_get'].execute('pk'=>pk).each_hash do |row|
		return row['changed']
	end
ensure
	@preps['changed_get'] and @preps['changed_get'].reset!
end

#changed_objectsObject


changed_objects



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/audrey/engine/sqlite3.rb', line 997

def changed_objects
	# $tm.hrm
	
	# sql to add parents
	if not @preps['changed_objects']
		sql = <<~'SQL'
		select *
			from objects
		where
			changed and
			custom_class(aclass)
		order by
			changed
		SQL
		
		@preps['changed_objects'] = @dbh.prepare(sql)
	end
	
	# loop through results
	@preps['changed_objects'].execute().each do |row|
		yield row
	end
ensure
	@preps['changed_objects'] and @preps['changed_objects'].reset!
end

#changed_set(pk, bool) ⇒ Object


changed_set



805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/audrey/engine/sqlite3.rb', line 805

def changed_set(pk, bool)
	# $tm.hrm
	
	# set to changed
	if bool
		@preps['changed_set_true'] ||= @dbh.prepare('update base_objects set changed=current_timestamp where pk=:pk')	
		simple_exec @preps['changed_set_true'], 'pk'=>pk
	else
		@preps['changed_set_false'] ||= @dbh.prepare('update base_objects set changed=null where pk=:pk')	
		simple_exec @preps['changed_set_false'], 'pk'=>pk
	end
end

#clear_collection(parent_pk) ⇒ Object


clear_collection



767
768
769
770
771
772
773
774
775
# File 'lib/audrey/engine/sqlite3.rb', line 767

def clear_collection(parent_pk)
	# $tm.hrm
	
	# prepare
	@preps['clear_collection'] ||= @dbh.prepare('delete from relationships where parent=:parent')
	
	# execute
	simple_exec @preps['clear_collection'], 'parent'=>parent_pk
end

#close(opts = {}) ⇒ Object


close



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/audrey/engine/sqlite3.rb', line 190

def close(opts={})
	# $tm.hrm
	
	# close prepared statements
	@preps.keys.each do |key|
		stmt = @preps[key]
		
		if stmt and (not stmt.closed?)
			stmt.close
		end
		
		@preps.delete key
	end
	
	# purge
	if opts['purge']
		purge()
	end
	
	# close database handle
	if not @dbh.closed?
		@dbh.close
	end
	
	# unlock file
	if @lock
		@lock.flock File::LOCK_UN
		@lock.close
	end
end

#collection_child_pks(parent_pk) ⇒ Object


collection_child_pks



825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'lib/audrey/engine/sqlite3.rb', line 825

def collection_child_pks(parent_pk)
	# $tm.hrm
	
	# prepare
	if not @preps['collection_child_pks']
		sql = <<~'SQL'
		select child
		from relationships
		where parent=:parent
		SQL
		
		@preps['collection_child_pks'] ||= @dbh.prepare(sql)
	end
	
	# init
	rv = []
	
	# get count
	@preps['collection_child_pks'].execute(parent_pk).each_hash do |row|
		rv.push row['child']
	end
	
	# return
	return rv
ensure
	@preps['collection_child_pks'] and @preps['collection_child_pks'].reset!
end

#collection_length(parent_pk) ⇒ Object


collection_length



860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
# File 'lib/audrey/engine/sqlite3.rb', line 860

def collection_length(parent_pk)
	# $tm.hrm
	
	# prepare
	if not @preps['collection_length']
		sql = <<~'SQL'
		select count(*) as count
		from relationships
		where parent=:parent
		SQL
		
		@preps['collection_length'] ||= @dbh.prepare(sql)
	end
	
	# get count
	@preps['collection_length'].execute(parent_pk).each_hash do |row|
		return row['count']
	end
ensure
	@preps['collection_length'] and @preps['collection_length'].reset!
end

#commitObject


commit, rollback



92
93
94
# File 'lib/audrey/engine/sqlite3.rb', line 92

def commit
	return @transactions[0].commit
end

#custom_functionsObject


custom_functions



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/audrey/engine/sqlite3.rb', line 228

def custom_functions()
	#-----------------------------------------------------------------------
	# partition
	#
	@dbh.create_function('current_partition', 0) do |func|
		func.result = @partition
	end
	#
	# partition
	#-----------------------------------------------------------------------
	
	
	#-----------------------------------------------------------------------
	# insert_object_journal
	#
	@dbh.create_function('insert_object_journal', 4 ) do |func, partition, root, aclass, scalar, xt|
		# $tm.hrm
		
		# translate scalars to objects for storage in JSON
		if converter = Audrey::AUDREY_SCALAR_TO_RUBY[aclass]
			scalar = converter.call(scalar)
		end
		
		# initialize
		rv = {}
		rv['fc'] = aclass
		rv['pt'] = partition
		
		# scalar
		if rv['s'].is_a?(String)
			rv['s'] = scalar.force_encoding('UTF-8')
		end
		
		# xt
		if xt
			rv['xt'] = xt
		end
		
		# root
		if root == 1
			rv['root'] = true
		end
		
		# set return result
		func.result = JSON.generate(rv)
	end
	#
	# insert_object_journal
	#-----------------------------------------------------------------------
	
	
	#-----------------------------------------------------------------------
	# aclass_fco, aclass_isolate
	#
	@dbh.create_function('aclass_fco', 1) do |func, aclass|
		func.result = Audrey::Util.aclass_fco(aclass) ? 1 : 0
	end
	
	@dbh.create_function('aclass_isolate', 1) do |func, aclass|
		func.result = Audrey::Util.aclass_isolate(aclass) ? 1 : 0
	end
	#
	# aclass_fco, aclass_isolate
	#-----------------------------------------------------------------------
	
	
	#-----------------------------------------------------------------------
	# graph_field
	#
	@dbh.create_function('graph_field', 2) do |func, aclass, hkey|
		func.result = Audrey::Util.graph_field?(aclass, hkey) ? 1 : 0
	end
	#
	# graph_field
	#-----------------------------------------------------------------------
	
	
	#-----------------------------------------------------------------------
	# custom_class
	#
	@dbh.create_function('custom_class', 1) do |func, aclass|
		func.result = Audrey::Util.custom_class?(aclass) ? 1 : 0
	end
	#
	# custom_class
	#-----------------------------------------------------------------------
	
	
	#-----------------------------------------------------------------------
	# debug
	#
	@dbh.create_function('debug', 1) do |func, msg|
		puts msg
		func.result = 1
	end
	#
	# debug
	#-----------------------------------------------------------------------
end

#delete_object(pk) ⇒ Object


delete_object



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/audrey/engine/sqlite3.rb', line 427

def delete_object(pk)
	# $tm.hrm
	
	# ensure statement handle
	if not @preps['delete_object']
		sql = <<~'SQL'
		delete from objects
		where pk=:pk and partition=:partition
		SQL
		
		@preps['delete_object'] = @dbh.prepare(sql)
	end
	
	# execute
	simple_exec @preps['delete_object'], 'pk' => pk, 'partition' => @partition
end

#get_dbhObject


get_dbh



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/audrey/engine/sqlite3.rb', line 71

def get_dbh
	# get database handle
	rv = ::SQLite3::Database.new(@path)
	
	# enforce referential integrity
	rv.execute 'pragma foreign_keys=on'
	
	# results as hash
	rv.results_as_hash = true
	
	# return
	return rv
end

#get_xt(obj_pk) ⇒ Object


get_xt



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/audrey/engine/sqlite3.rb', line 479

def get_xt(obj_pk)
	# prepare statement if necessary
	@preps['existing_xt'] ||= @dbh.prepare('select xt from objects where pk=:pk and partition=:partition')
	
	# search
	@preps['existing_xt'].execute('pk'=>obj_pk, 'partition'=>@partition).each do |row|
		return row['xt']
	end
	
	# create
	xt_pk = Audrey::Util.uuid
	add_xt obj_pk, xt_pk
	
	# return
	return xt_pk
ensure
	@preps['existing_xt'] and @preps['existing_xt'].reset!
end

#hash_each(parent_pk) ⇒ Object


hash_each



715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# File 'lib/audrey/engine/sqlite3.rb', line 715

def hash_each(parent_pk)
	# $tm.hrm
	rv = nil
	
	# loop through keys
	hash_keys(parent_pk).each do |key|
		if block_given?
			yield key, row_hash_by_key(parent_pk, key)
		else
			rv ||= []
			rv.push [key, row_hash_by_key(parent_pk, key)]
		end
	end
	
	# return
	return rv
end

#hash_element_delete(parent_pk, key) ⇒ Object


hash_element_delete



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/audrey/engine/sqlite3.rb', line 624

def hash_element_delete(parent_pk, key)
	# $tm.hrm
	
	# get record
	record = row_hash_by_key(parent_pk, key)
	
	# execute
	if record
		if not @preps['delete_hash_key']
			sql = <<~'SQL'
			delete from relationships
			where parent=:parent and hkey=:hkey
			SQL
			
			@preps['delete_hash_key'] = @dbh.prepare(sql)
		end
		
		simple_exec @preps['delete_hash_key'], 'parent'=>parent_pk, 'hkey'=>key
	end
	
	# return
	return record
end

#hash_has_key?(parent_pk, key) ⇒ Boolean


hash_has_key?

Returns:

  • (Boolean)


554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/audrey/engine/sqlite3.rb', line 554

def hash_has_key?(parent_pk, key)
	# $tm.hrm
	# puts "parent_pk: #{parent_pk}"
	# puts "key: #{key}"
	
	# prepare
	if not @preps['hash_has_key']
		sql = <<~'SQL'
		select count(*) as count
		from relationships
		where parent=:parent and hkey=:hkey
		SQL
		
		@preps['hash_has_key'] ||= @dbh.prepare(sql)
	end
	
	# get count
	@preps['hash_has_key'].execute('parent'=>parent_pk, 'hkey'=>key).each_hash do |row|
		return row['count'] > 0
	end
ensure
	@preps['hash_has_key'] and @preps['hash_has_key'].reset!
end

#hash_keys(parent_pk) ⇒ Object


hash_keys



741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/audrey/engine/sqlite3.rb', line 741

def hash_keys(parent_pk)
	# $tm.hrm
	rv = []
	
	# prepare
	@preps['hash_keys'] ||= @dbh.prepare('select hkey from relationships where parent=:parent order by ord')
	
	# loop through records
	@preps['hash_keys'].execute('parent'=>parent_pk).each_hash do |row|
		rv.push row['hkey']
	end
	
	# return
	return rv
	
ensure
	@preps['hash_keys'] and @preps['hash_keys'].reset!
end

#in_transaction?Boolean


in_transaction?

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
# File 'lib/audrey/engine/sqlite3.rb', line 107

def in_transaction?
	# loop through transactions
	@transactions.each do |tr|
		tr.is_a?(Audrey::Engine::SQLite3::Transaction::RC) and return true
	end
	
	# else not in transaction
	return false
end

#insert_object(object, opts = {}) ⇒ Object


insert_object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/audrey/engine/sqlite3.rb', line 363

def insert_object(object, opts={})
	# $tm.hrm
	
	# pk
	pk = opts['pk'] || Audrey::Util.uuid
	
	# if scalar
	if dfn = Audrey::Engine::SQLite3::RUBY_SCALAR_TO_SQLITE[object.class]
		if not @preps['insert_scalar']
			sql = <<~'SQL'
			insert into objects(pk, partition, hsa, aclass, scalar)
			values(:pk, :partition, 's', :aclass, :scalar)
			SQL
			
			@preps['insert_scalar'] = @dbh.prepare(sql)
		end
		
		# insert record
		simple_exec(
			@preps['insert_scalar'],
			'pk' => pk,
			'partition' => @partition,
			'aclass' => dfn['aclass'].to_s,
			'scalar' => dfn['to_db'].call(object)
		);
		
	# if hash or array
	elsif dfn = Audrey::RUBY_OBJECT_TO_AUDREY[object.class] and dfn['nclass']
		simple_exec(
			prepare_insert_collection(),
			'pk' => pk,
			'partition' => @partition,
			'hsa' => dfn['aclass'].hsa,
			'aclass' => dfn['aclass'].to_s
		);
		
	# if custom object
	elsif object.is_a?(Audrey::Object::Custom)
		oclass = object.class
		
		simple_exec(
			prepare_insert_collection(),
			'pk' => pk,
			'partition' => @partition,
			'hsa' => oclass.hsa,
			'aclass' => oclass.to_s,
		);
		
	# else unknown
	else
		raise 'unknown-object-class: ' + object.class.to_s
	end
	
	# return pk
	return pk
end

#object_exists?(pk) ⇒ Boolean


object_exists?

Returns:

  • (Boolean)


919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/audrey/engine/sqlite3.rb', line 919

def object_exists?(pk)
	# $tm.hrm
	
	# ensure statement handle
	if not @preps['object_exists']
		sql = <<~'SQL'
		select count(*) as count
		from objects
		where
		pk=:pk and
		partition=:partition
		SQL
		
		@preps['object_exists'] = @dbh.prepare(sql)
	end
	
	@preps['object_exists'].execute('pk'=>pk, 'partition'=>@partition).each do |row|
		return row['count'] > 0
	end
ensure
	@preps['object_exists'] and @preps['object_exists'].reset!
end

#purgeObject


purge



335
336
337
338
339
340
341
342
# File 'lib/audrey/engine/sqlite3.rb', line 335

def purge
	# $tm.hrm
	
	if @mode.write
		sql = 'delete from objects where pk in (select pk from fco_not_traced)'
		@dbh.execute sql
	end
end

#q0(p_fquery) ⇒ Object


q0



985
986
987
988
# File 'lib/audrey/engine/sqlite3.rb', line 985

def q0(p_fquery)
	require 'audrey/engine/sqlite3/query/q0'
	return Audrey::Engine::SQLite3::Query::Q0.new(self, p_fquery)
end

#reset_savepoints(ftr) ⇒ Object


reset_savepoints



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/audrey/engine/sqlite3.rb', line 169

def reset_savepoints(ftr)
	found = false
	
	@transactions.each do |tr|
		if tr == ftr
			found = true
		end
		
		if found
			tr.start
		end
	end
end

#rollbackObject



96
97
98
# File 'lib/audrey/engine/sqlite3.rb', line 96

def rollback
	return @transactions[0].rollback
end

#root_pkObject


root_pk



351
352
353
354
# File 'lib/audrey/engine/sqlite3.rb', line 351

def root_pk
	sql = 'select pk from objects where root and partition=:partition'
	return @dbh.get_first_value(sql, 'partition'=>@partition)
end

#row_by_array_index(parent_pk, idx) ⇒ Object


row_by_array_index



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/audrey/engine/sqlite3.rb', line 680

def row_by_array_index(parent_pk, idx)
	# $tm.hrm
	
	# ensure statement handle
	if not @preps['row_by_array_index']
		sql = <<~SQL
		select r.ord as ord, o.*
		from relationships r, objects o
		where r.parent=:parent and o.pk=r.child
		order by r.ord
		limit 1
		offset :offset
		SQL
		
		@preps['row_by_array_index'] = @dbh.prepare(sql)
	end
	
	# get record
	@preps['row_by_array_index'].execute('parent'=>parent_pk, 'offset'=>idx).each_hash do |row|
		return self.class.set_row_scalar(row.to_h)
	end
	
	# else return nil
	return nil
ensure
	@preps['row_by_array_index'] and @preps['row_by_array_index'].reset!
end

#row_by_pk(pk) ⇒ Object


row_by_pk



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/audrey/engine/sqlite3.rb', line 655

def row_by_pk(pk)
	# $tm.hrm
	
	# ensure prepared statement
	@preps['row_by_pk'] ||= @dbh.prepare('select * from objects where pk=:pk and partition=:partition')
	
	# get record
	@preps['row_by_pk'].execute('pk'=>pk, 'partition'=>@partition).each_hash do |row|
		return self.class.set_row_scalar(row.to_h)
	end
	
	# didn't find the object, so return nil
	return nil
	
ensure
	@preps['row_by_pk'] and @preps['row_by_pk'].reset!
end

#row_hash_by_key(parent_pk, hkey) ⇒ Object


row_hash_by_key



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/audrey/engine/sqlite3.rb', line 586

def row_hash_by_key(parent_pk, hkey)
	# $tm.hrm
	
	# ensure statement handle
	if not @preps['row_hash_by_key']
		sql = <<~SQL
		select * from objects
		where pk =
			(select child from relationships where parent=:parent and hkey=:hkey) and
			partition=:partition
		SQL
		
		@preps['row_hash_by_key'] = @dbh.prepare(sql)
	end
	
	# params
	params = {}
	params['parent'] = parent_pk
	params['hkey'] = hkey
	params['partition'] = @partition
	
	# get record
	@preps['row_hash_by_key'].execute(params).each_hash do |row|
		return self.class.set_row_scalar(row.to_h)
	end
	
ensure
	@preps['row_hash_by_key'] and @preps['row_hash_by_key'].reset!
end

#transaction(opts = {}) ⇒ Object


transaction



124
125
126
127
128
129
130
131
132
133
134
135
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
# File 'lib/audrey/engine/sqlite3.rb', line 124

def transaction(opts={})
	# $tm.hrm
	tr = Audrey::Engine::SQLite3::Transaction::RC.new(self)
	@transactions.push tr
	
	# block
	if block_given?
		begin
			# yield
			yield tr
			
			# autocommit if necessary
			if opts['autocommit']
				tr.commit
			end
		
		# exit transaction
		rescue Audrey::Exception::TransactionExit => e
			unless e.tr == tr
				raise e
			end
			
			# return out of this method
			return nil
		ensure
			tr.terminate
			@transactions.pop
		end
		
		# return nil
		return nil
	
	# non-block
	else
		return tr
	end
end