Class: Audrey::Query::Q0

Inherits:
Audrey::Query show all
Defined in:
lib/audrey/query/q0.rb

Overview

Audrey::Query::Q0

Defined Under Namespace

Modules: Defined Classes: Fields, Iterator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p_db) ⇒ Q0


initialize



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/audrey/query/q0.rb', line 14

def initialize(p_db)
	# check that we got a Audrey object
	if not p_db.is_a?(Audrey)
		raise 'db-not-audrey: ' + pdb.class.to_s
	end
	
	@db = p_db
	@engine = @db.engine
	@aclass = nil
	@fields = Audrey::Query::Q0::Fields.new()
end

Instance Attribute Details

#aclassObject

Returns the value of attribute aclass.



7
8
9
# File 'lib/audrey/query/q0.rb', line 7

def aclass
  @aclass
end

#dbObject (readonly)

Returns the value of attribute db.



5
6
7
# File 'lib/audrey/query/q0.rb', line 5

def db
  @db
end

#fieldsObject

Returns the value of attribute fields.



6
7
8
# File 'lib/audrey/query/q0.rb', line 6

def fields
  @fields
end

#limitObject

Returns the value of attribute limit.



8
9
10
# File 'lib/audrey/query/q0.rb', line 8

def limit
  @limit
end

#offsetObject

Returns the value of attribute offset.



9
10
11
# File 'lib/audrey/query/q0.rb', line 9

def offset
  @offset
end

Instance Method Details

#aclassesObject


aclasses



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/audrey/query/q0.rb', line 157

def aclasses
	# $tm.hrm
	
	# early exit: no aclass
	if not @aclass
		return nil
	end
	
	# initialize
	fcs = [*@aclass]
	
	# early exit: aclass is an empty array
	# NOTE: An empty array would mean that not records are returned, so
	# we'll interpret it to mean all records.
	if not fcs.any?
		return nil
	end
	
	# initialize
	rv = {}
	
	# build aclasses
	fcs.each do |aclass|
		aclass.aclasses.each do |desc|
			rv[desc] = nil
		end
	end
	
	# return
	return rv.keys
end

#cloneObject


clone



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/audrey/query/q0.rb', line 196

def clone
	# $tm.hrm
	rv = self.class.new(@db)
	
	# aclass
	if @aclass.is_a?(Array)
		rv.aclass = @aclass.clone
	else
		rv.aclass = @aclass
	end
	
	# fields
	rv.fields = @fields.clone
	
	# return
	return rv
end

#countObject


count



91
92
93
# File 'lib/audrey/query/q0.rb', line 91

def count
	return @engine.q0(self).count
end

#definedObject


defined



146
147
148
# File 'lib/audrey/query/q0.rb', line 146

def defined
	return Audrey::Query::Q0::Defined
end

#each(opts = {}) ⇒ Object


each



33
34
35
36
37
38
39
40
41
# File 'lib/audrey/query/q0.rb', line 33

def each(opts={})
	# $tm.hrm
	equery = @engine.q0(self)
	
	# loop through equery results
	equery.each(opts) do |pk|
		yield @db.object_from_pk(pk)
	end
end

#firstObject


first returns the first record in the query, or nil if the query didn’t find anything



131
132
133
134
135
136
137
# File 'lib/audrey/query/q0.rb', line 131

def first
	each() do |obj|
		return obj
	end
	
	return nil
end

#iteratorObject


iterator



221
222
223
224
# File 'lib/audrey/query/q0.rb', line 221

def iterator
	# $tm.hrm
	return Audrey::Query::Q0::Iterator.new(self, @engine.q0(self).iterator)
end

#sampleObject


sample



50
51
52
53
54
55
56
# File 'lib/audrey/query/q0.rb', line 50

def sample
	# $tm.hrm
	cl = self.clone()
	cl.limit = 1
	cl.offset = rand(self.count)
	return cl.first
end

#samples(count) ⇒ Object


samples KLUDGE: This routine could be more efficient by not creating an object out of every sample until it’s determined that we don’t already have the object.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/audrey/query/q0.rb', line 68

def samples(count)
	rv = {}
	
	# loop until we get enough objects
	while rv.length < count
		obj = sample()
		
		if not rv[obj.audrey.pk]
			rv[obj.audrey.pk] = obj
		end
	end
	
	# return
	return rv.values
end

#sum(fname) ⇒ Object


sum



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/audrey/query/q0.rb', line 102

def sum(fname)
	total = 0
	
	# optimize the query by cloning and add Defined
	qopt = self.clone
	qopt.fields.add 'calls', qopt.defined
	
	# loop through records
	qopt.run do |record|
		field = record.audrey[fname]
		
		if field.is_a?(Numeric)
			total += field
		end
	end
	
	# return
	return total
end