Class: Lafcadio::DomainObjectSqlMaker

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ DomainObjectSqlMaker

Returns a new instance of DomainObjectSqlMaker.



7
# File 'lib/lafcadio/objectStore/DomainObjectSqlMaker.rb', line 7

def initialize(obj); @obj = obj; end

Instance Attribute Details

#bindValuesObject (readonly)

Returns the value of attribute bindValues.



5
6
7
# File 'lib/lafcadio/objectStore/DomainObjectSqlMaker.rb', line 5

def bindValues
  @bindValues
end

Instance Method Details

#deleteSql(objectType) ⇒ Object



48
49
50
51
# File 'lib/lafcadio/objectStore/DomainObjectSqlMaker.rb', line 48

def deleteSql(objectType)
	"delete from #{ objectType.tableName} " +
			"where #{ objectType.sqlPrimaryKeyName }=#{ @obj.pkId }"
end

#getNameValuePairs(objectType) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lafcadio/objectStore/DomainObjectSqlMaker.rb', line 21

def getNameValuePairs(objectType)
	require 'lafcadio/util/QueueHash'
	nameValues = []
	objectType.classFields.each { |field|
		value = @obj.send(field.name)
		unless field.dbWillAutomaticallyWrite
			nameValues << field.nameForSQL
			nameValues <<(field.valueForSQL(value))
		end
		if field.bind_write?
			@bindValues << value
		end
	}
	QueueHash.new( *nameValues )
end

#insertSQL(objectType) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lafcadio/objectStore/DomainObjectSqlMaker.rb', line 9

def insertSQL(objectType)
	fields = objectType.classFields
	nameValuePairs = getNameValuePairs(objectType)
	if objectType.isBasedOn?
		nameValuePairs[objectType.sqlPrimaryKeyName] = 'LAST_INSERT_ID()'
	end
	fieldNameStr = nameValuePairs.keys.join ", "
	fieldValueStr = nameValuePairs.values.join ", "
	"insert into #{ objectType.tableName}(#{fieldNameStr}) " +
			"values(#{fieldValueStr})"
end

#sqlStatementsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lafcadio/objectStore/DomainObjectSqlMaker.rb', line 53

def sqlStatements
	statements = []
	if @obj.errorMessages.size > 0
		raise DomainObjectInitError, @obj.errorMessages, caller
	end
	@obj.class.selfAndConcreteSuperclasses.each { |objectType|
		@bindValues = []
		if @obj.pkId == nil
			statement = insertSQL(objectType)
		else
			if @obj.delete
				statement = deleteSql(objectType)
			else
				statement = updateSQL(objectType)
			end
		end
		statements << [statement, @bindValues]
		}
	statements.reverse
end

#updateSQL(objectType) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/lafcadio/objectStore/DomainObjectSqlMaker.rb', line 37

def updateSQL(objectType)
	nameValueStrings = []
	nameValuePairs = getNameValuePairs(objectType)
	nameValuePairs.each { |key, value|
		nameValueStrings << "#{key}=#{ value }"
	}
	allNameValues = nameValueStrings.join ', '
	"update #{ objectType.tableName} set #{allNameValues} " +
			"where #{ objectType.sqlPrimaryKeyName}=#{@obj.pkId}"
end