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: Importer, 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


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
546
547
548
549
550
551
552
553
554
555
# File 'lib/audrey/engine/sqlite3.rb', line 515

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


461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/audrey/engine/sqlite3.rb', line 461

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, bclass, fclass)
		values(?, (select partition from objects where pk=?), 'Audrey::Object::Hash', '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


959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
# File 'lib/audrey/engine/sqlite3.rb', line 959

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


899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# File 'lib/audrey/engine/sqlite3.rb', line 899

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


794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/audrey/engine/sqlite3.rb', line 794

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


1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# File 'lib/audrey/engine/sqlite3.rb', line 1019

def changed_objects
	# $tm.hrm
	
	# sql to add parents
	if not @preps['changed_objects']
		sql = <<~'SQL'
		select *
			from objects
		where
			changed and
			custom_class(fclass)
		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


815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/audrey/engine/sqlite3.rb', line 815

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


777
778
779
780
781
782
783
784
785
# File 'lib/audrey/engine/sqlite3.rb', line 777

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


835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
# File 'lib/audrey/engine/sqlite3.rb', line 835

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


870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
# File 'lib/audrey/engine/sqlite3.rb', line 870

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
327
328
329
330
331
332
333
334
335
336
337
# 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, fclass, scalar, xt|
		# $tm.hrm
		
		# translate scalars to objects for storage in JSON
		if converter = Audrey::AUDREY_SCALAR_TO_RUBY[fclass]
			scalar = converter.call(scalar)
		end
		
		# initialize
		rv = {}
		rv['fc'] = fclass
		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
	#-----------------------------------------------------------------------
	
	
	#-----------------------------------------------------------------------
	# fclass_fco, fclass_isolate
	#
	@dbh.create_function('fclass_fco', 1) do |func, fclass|
		func.result = Audrey::Util.fclass_fco(fclass) ? 1 : 0
	end
	
	@dbh.create_function('fclass_isolate', 1) do |func, fclass|
		func.result = Audrey::Util.fclass_isolate(fclass) ? 1 : 0
	end
	#
	# fclass_fco, fclass_isolate
	#-----------------------------------------------------------------------
	
	
	#-----------------------------------------------------------------------
	# graph_field
	#
	@dbh.create_function('graph_field', 2) do |func, fclass, hkey|
		func.result = Audrey::Util.graph_field?(fclass, hkey) ? 1 : 0
	end
	#
	# graph_field
	#-----------------------------------------------------------------------
	
	
	#-----------------------------------------------------------------------
	# change_to_true
	#
	# @dbh.create_function('change_to_true', 1) do |func, fclass|
	# 	func.result = 1
	# end
	#
	# change_to_true
	#-----------------------------------------------------------------------
	
	
	#-----------------------------------------------------------------------
	# custom_class
	#
	@dbh.create_function('custom_class', 1) do |func, fclass|
		func.result = Audrey::Util.custom_class?(fclass) ? 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


437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/audrey/engine/sqlite3.rb', line 437

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


489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/audrey/engine/sqlite3.rb', line 489

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


725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/audrey/engine/sqlite3.rb', line 725

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


634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/audrey/engine/sqlite3.rb', line 634

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)

564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/audrey/engine/sqlite3.rb', line 564

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


751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/audrey/engine/sqlite3.rb', line 751

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

#import_csv(fclass, path, opts = {}) ⇒ Object


import_csv


1007
1008
1009
1010
# File 'lib/audrey/engine/sqlite3.rb', line 1007

def import_csv(fclass, path, opts={})
	# $tm.hrm
	return Audrey::Engine::SQLite3::Importer::CSV.new(self, fclass, path, opts={})
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


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
419
420
421
422
423
424
425
426
427
428
# File 'lib/audrey/engine/sqlite3.rb', line 374

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, bclass, fclass, scalar)
			values(:pk, :partition, :bclass, :fclass, :scalar)
			SQL
			
			@preps['insert_scalar'] = @dbh.prepare(sql)
		end
		
		# insert record
		simple_exec(
			@preps['insert_scalar'],
			'pk' => pk,
			'partition' => @partition,
			'bclass' => dfn['fclass'].bclass.to_s,
			'fclass' => dfn['fclass'].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,
			'bclass' => dfn['fclass'].bclass.to_s,
			'fclass' => dfn['fclass'].to_s
		);
		
	# if custom object
	elsif object.is_a?(Audrey::Object::Custom)
		simple_exec(
			prepare_insert_collection(),
			'pk' => pk,
			'partition' => @partition,
			'bclass' => object.class.bclass.to_s,
			'fclass' => object.class.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)

929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
# File 'lib/audrey/engine/sqlite3.rb', line 929

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


346
347
348
349
350
351
352
353
# File 'lib/audrey/engine/sqlite3.rb', line 346

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


995
996
997
998
# File 'lib/audrey/engine/sqlite3.rb', line 995

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


362
363
364
365
# File 'lib/audrey/engine/sqlite3.rb', line 362

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


690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
# File 'lib/audrey/engine/sqlite3.rb', line 690

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


665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'lib/audrey/engine/sqlite3.rb', line 665

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


596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/audrey/engine/sqlite3.rb', line 596

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