Class: Lafcadio::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/lafcadio/query/Query.rb,
lib/lafcadio/query.rb,
lib/lafcadio/query/In.rb,
lib/lafcadio/query/Max.rb,
lib/lafcadio/query/Not.rb,
lib/lafcadio/query/Like.rb,
lib/lafcadio/query/Link.rb,
lib/lafcadio/query/Equals.rb,
lib/lafcadio/query/Compare.rb,
lib/lafcadio/query/Condition.rb,
lib/lafcadio/query/CompoundCondition.rb

Overview

:nodoc:

Direct Known Subclasses

Max

Defined Under Namespace

Classes: Compare, CompoundCondition, Condition, DomainObjectImpostor, Equals, In, Inferrer, Like, Link, Max, Not, ObjectFieldImpostor

Constant Summary collapse

ASC =
1
DESC =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objectType, pkIdOrCondition = nil) ⇒ Query

Returns a new instance of Query.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lafcadio/query/Query.rb', line 9

def initialize(objectType, pkIdOrCondition = nil)
	@objectType = objectType
	( @condition, @orderBy, @limit ) = [ nil, nil, nil ]
	if pkIdOrCondition
		if pkIdOrCondition.class <= Condition
			@condition = pkIdOrCondition
		else
			@condition = Query::Equals.new(objectType.sqlPrimaryKeyName,
					pkIdOrCondition, objectType)
		end
	end
	@orderByOrder = ASC
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



6
7
8
# File 'lib/lafcadio/query/Query.rb', line 6

def condition
  @condition
end

#limitObject

Returns the value of attribute limit.



7
8
9
# File 'lib/lafcadio/query/Query.rb', line 7

def limit
  @limit
end

#objectTypeObject (readonly)

Returns the value of attribute objectType.



6
7
8
# File 'lib/lafcadio/query/Query.rb', line 6

def objectType
  @objectType
end

#orderByObject

Returns the value of attribute orderBy.



7
8
9
# File 'lib/lafcadio/query/Query.rb', line 7

def orderBy
  @orderBy
end

#orderByOrderObject

Returns the value of attribute orderByOrder.



7
8
9
# File 'lib/lafcadio/query/Query.rb', line 7

def orderByOrder
  @orderByOrder
end

Class Method Details

.And(*conditions) ⇒ Object



92
# File 'lib/lafcadio/query.rb', line 92

def self.And( *conditions ); CompoundCondition.new( *conditions ); end

.Or(*conditions) ⇒ Object



94
95
96
97
# File 'lib/lafcadio/query.rb', line 94

def self.Or( *conditions )
	conditions << CompoundCondition::OR
	CompoundCondition.new( *conditions)
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/lafcadio/query/Query.rb', line 25

def eql?( other )
	other.class <= Query && other.toSql == toSql
end

#fieldsObject



64
65
66
# File 'lib/lafcadio/query/Query.rb', line 64

def fields
	'*'
end

#hashObject



23
# File 'lib/lafcadio/query/Query.rb', line 23

def hash; toSql.hash; end

#limitClauseObject



80
81
82
# File 'lib/lafcadio/query/Query.rb', line 80

def limitClause
	"limit #{ @limit.begin }, #{ @limit.end - @limit.begin + 1 }" if @limit
end

#orderClauseObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lafcadio/query/Query.rb', line 68

def orderClause
	if @orderBy
		clause = "order by #{ @orderBy } "
		if @orderByOrder == ASC
			clause += 'asc'
		else
			clause += 'desc'
		end
		clause
	end
end

#sqlPrimaryKeyField(objectType) ⇒ Object



60
61
62
# File 'lib/lafcadio/query/Query.rb', line 60

def sqlPrimaryKeyField(objectType)
	"#{ objectType.tableName }.#{ objectType.sqlPrimaryKeyName }"
end

#tablesObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/lafcadio/query/Query.rb', line 29

def tables
	tableNames = []
	anObjectType = objectType
	until(DomainObject.abstractSubclasses.index(anObjectType) != nil ||
			anObjectType == DomainObject)
		tableNames.unshift anObjectType.tableName
		anObjectType = anObjectType.superclass
	end
	tableNames.join ', '
end

#toSqlObject



84
85
86
87
88
89
90
# File 'lib/lafcadio/query/Query.rb', line 84

def toSql
	clauses = [ "select #{ fields }", "from #{ tables }" ]
	clauses << whereClause if whereClause
	clauses << orderClause if orderClause
	clauses << limitClause if limitClause
	clauses.join ' '
end

#whereClauseObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lafcadio/query/Query.rb', line 40

def whereClause
	whereClauses = []
	anObjectType = objectType
	superclass = anObjectType.superclass
	until(DomainObject.abstractSubclasses.index(superclass) != nil ||
			superclass == DomainObject)
		joinClause = "#{ sqlPrimaryKeyField(superclass) } = " +
				"#{ sqlPrimaryKeyField(anObjectType) }"
		whereClauses.unshift joinClause
		anObjectType = superclass
		superclass = superclass.superclass
	end
	whereClauses << @condition.toSql if @condition
	if whereClauses.size > 0
		"where #{ whereClauses.join(' and ') }"
	else
		nil
	end
end