Class: Lafcadio::ObjectStore::CommitSqlStatementsAndBinds

Inherits:
Array
  • Object
show all
Defined in:
lib/lafcadio/objectStore.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ CommitSqlStatementsAndBinds

Returns a new instance of CommitSqlStatementsAndBinds.



477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/lafcadio/objectStore.rb', line 477

def initialize( obj )
	@obj = obj
	reversed = []
	@obj.class.self_and_concrete_superclasses.each { |domain_class|
		statement_bind_value_pairs( domain_class ).reverse.each do |pair|
			reversed << pair
		end
	}
	reversed.reverse.each do |statement, binds|
		self << [ statement, binds ]
	end
end

Instance Attribute Details

#bind_valuesObject (readonly)

Returns the value of attribute bind_values.



475
476
477
# File 'lib/lafcadio/objectStore.rb', line 475

def bind_values
  @bind_values
end

Instance Method Details

#delete_sql(domain_class) ⇒ Object



490
491
492
493
# File 'lib/lafcadio/objectStore.rb', line 490

def delete_sql( domain_class )
	"delete from #{ domain_class.table_name} " +
			"where #{ domain_class.sql_primary_key_name }=#{ @obj.pk_id }"
end

#get_name_value_pairs(domain_class) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/lafcadio/objectStore.rb', line 495

def get_name_value_pairs( domain_class )
	nameValues = []
	domain_class.class_fields.each { |field|
		unless field.instance_of?( PrimaryKeyField )
			value = @obj.send(field.name)
			unless field.db_will_automatically_write?
				nameValues << field.db_field_name
				nameValues << field.value_for_sql( value )
			end
			if field.bind_write?
				@bind_values << value
			end
		end
	}
	QueueHash.new( *nameValues )
end

#insert_sql(domain_class) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/lafcadio/objectStore.rb', line 512

def insert_sql( domain_class )
	fields = domain_class.class_fields
	nameValuePairs = get_name_value_pairs( domain_class )
	if domain_class.is_child_domain_class?
		pair = DbBridge.last_inserted_pk_id_pair domain_class.superclass
		nameValuePairs[domain_class.sql_primary_key_name] = pair.first
	end
	fieldNameStr = nameValuePairs.keys.join ", "
	fieldValueStr = nameValuePairs.values.join ", "
	"insert into #{ domain_class.table_name}(#{fieldNameStr}) " +
			"values(#{fieldValueStr})"
end

#statement_bind_value_pairs(domain_class) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/lafcadio/objectStore.rb', line 525

def statement_bind_value_pairs( domain_class )
	@bind_values = []
	if @obj.pk_id == nil
		statement = insert_sql( domain_class )
		pairs = [ [statement, @bind_values] ]
		if domain_class.is_child_domain_class? and ObjectStore.db_type == 'Pg'
			setval_sql = "select setval( '#{ domain_class.postgres_pk_id_seq }', currval('#{ domain_class.superclass.postgres_pk_id_seq }') )"
			pairs << [ setval_sql, @bind_values ]
		end
		pairs
	else
		if @obj.delete
			statement = delete_sql( domain_class )
		else
			statement = update_sql( domain_class)
		end
		[ [statement, @bind_values] ]
	end
end

#update_sql(domain_class) ⇒ Object



545
546
547
548
549
550
551
552
553
554
# File 'lib/lafcadio/objectStore.rb', line 545

def update_sql( domain_class )
	nameValueStrings = []
	nameValuePairs = get_name_value_pairs( domain_class )
	nameValuePairs.each { |key, value|
		nameValueStrings << "#{key}=#{ value }"
	}
	allNameValues = nameValueStrings.join ', '
	"update #{ domain_class.table_name} set #{allNameValues} " +
			"where #{ domain_class.sql_primary_key_name}=#{@obj.pk_id}"
end