Class: OrientSupport::OrientQuery

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/support.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support

#compose_where, #generate_sql_list

Constructor Details

#initialize(**args) ⇒ OrientQuery

Returns a new instance of OrientQuery.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/support.rb', line 155

def initialize  **args
	@projection = []
	@misc = []
	@let  = []
	@where = []
	@order = []
	@kind = 'select'
	args.each do | k,v|
	  case k
	   when :projection
	    @projection << v
	   when :let
	    @let << v
	   when :order
	    @order << v
	   when :where
	    @where << v
	   when :kind
@kind = v
	  else
	    self.send k, v
	  end
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arg, &b) ⇒ Object



180
181
182
# File 'lib/support.rb', line 180

def method_missing method, *arg, &b
	@misc << method.to_s << " " << arg.map(&:to_s).join(' ')
end

Instance Attribute Details

#letObject

Returns the value of attribute let.



259
260
261
# File 'lib/support.rb', line 259

def let
  @let
end

#orderObject

Returns the value of attribute order.



343
344
345
# File 'lib/support.rb', line 343

def order
  @order
end

#projectionObject

Returns the value of attribute projection.



290
291
292
# File 'lib/support.rb', line 290

def projection
  @projection
end

#whereObject

Call where without a parameter to request the saved where-string

to create the where-part of the query a string, a hash or an Array is supported

where: “r > 9” –> where r > 9 where: 9, b: ‘s’ –> where a = 9 and b = ‘s’ where:[{ a: 2} , ‘b > 3’,{ c: ‘ufz’ }] –> where a = 2 and b > 3 and c = ‘ufz’



253
254
255
# File 'lib/support.rb', line 253

def where
  @where
end

Instance Method Details

#compose(destination: :batch) ⇒ Object Also known as: to_s

Output the compiled query parmeter: destination (rest, batch ) If the query is submitted via the REST-Interface (as get-command), the limit parameter is extracted.



196
197
198
199
200
201
202
# File 'lib/support.rb', line 196

def compose( destination: :batch )
	if destination == :rest
	[ @kind, projection_s,  from, let_s, where_s , subquery,  misc, order_s , group_by, unwind, skip ].compact.join(' ')
else
	[ @kind, projection_s,  from, let_s, where_s , subquery,  misc, order_s , group_by, limit, unwind, skip ].compact.join(' ')
end
end

#database_classObject



233
234
235
236
237
238
239
240
241
# File 'lib/support.rb', line 233

def database_class
	if @database.present?
	  @database
	elsif @from.is_a? OrientQuery
	  @from.database_class
	else
	  nil
	end
end

#database_class=(arg) ⇒ Object



227
228
229
230
231
232
# File 'lib/support.rb', line 227

def database_class= arg
	@database =  arg if @database.present?
	if @from.is_a? OrientQuery
	  @from.database_class= arg
	end
end

#distinct(d) ⇒ Object Also known as: distinct=



275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/support.rb', line 275

def distinct d
	@projection << case d
 when String, Symbol
     "distinct( #{d.to_s} )"  
 when Array
     "distinct( #{d.first} ) as #{d.last}"
 when Hash
     "distinct( #{d.first.first} ) as #{d.first.last}"
 else
   ""
 end 
	compose  # return the hole query
end

#from(arg = nil) ⇒ Object Also known as: from=

from can either be a Databaseclass to operate on or a Subquery providing data to query further



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/support.rb', line 207

def from arg=nil
if arg.present?
  @database = case arg 
	      when Class
		arg.new.classname
	      when ActiveOrient::Model
		classname
	      when String
		arg
	      when Symbol
		arg
	      when OrientQuery
		' ( '+ arg.to_s + ' ) '
	      end
  compose  # return the complete query
else # read from
  "from " <<  @database.to_s  unless @database.nil?
  end
end

#get_limitObject

The Rest-Interface needs to separate the limit-value. This Method extracts the number, usage in REST::get_documents



324
325
326
# File 'lib/support.rb', line 324

def get_limit
	@limit.nil? ? -1 : @limit.split(' ').last.to_i
end

#group_by(g = nil) ⇒ Object



327
328
329
330
331
# File 'lib/support.rb', line 327

def group_by g=nil
	@group = "group by  #{g.to_s}" if g.present?
	# only a string is allowed
	@group  # return_value
end

#let_sObject



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/support.rb', line 260

def let_s
	unless @let.empty?
	  "let " << @let.map do |s|
	   case s
	  when Hash
	    s.map{ |x,y| "$#{x} = ( #{y} )"}.join( ', ')
	  when Array
	    s.join(',  ')
	  else
	    s
	  end
	  end.join(', ')
	end
end

#limit(l = nil) ⇒ Object Also known as: limit=

select_string = (“select ” + select_string + distinct_string + ‘ from ’ + class_name(o_class) ).squeeze(‘ ’) where_string = compose_where( where )



313
314
315
316
317
# File 'lib/support.rb', line 313

def limit l=nil
	@limit = "limit  #{l.to_s}" if l.present?
	# only a string is allowed
	@limit  # return_value
end

#miscObject



184
185
186
# File 'lib/support.rb', line 184

def misc
	@misc.join(' ') unless @misc.empty?
end

#order_sObject



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/support.rb', line 345

def order_s 
	unless @order.empty?
	 # the [@order] is nessesary to enable query.order= "..." oder query.order= { a: :b }  
	  "order by " << [@order].flatten.map do | o |
	    case o
	    when  Hash 
 o.map{ |x,y| "#{x} #{y}" }.join( " " )
	    else
 o.to_s
	    end  # case
	  end.join(', ')
	else
	  ''
	end
end

#projection_sObject



291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/support.rb', line 291

def projection_s

	  @projection.map do | s |
		case s
			  when Hash
 s.map{ |x,y| "#{x} as #{y}"}.join( ', ')
			  when Array
 s.join(', ')
			  else
 s
			  end
	  end.join( ', ' )

end

#skip(n = nil) ⇒ Object



338
339
340
341
# File 'lib/support.rb', line 338

def skip n=nil
	@skip= n if n.present?
	"skip #{@skip}" if @skip.present?
end

#subqueryObject



188
189
190
# File 'lib/support.rb', line 188

def subquery
	nil
end

#unwind(u = nil) ⇒ Object

return_value



332
333
334
335
336
# File 'lib/support.rb', line 332

def unwind u=nil
	@unwind = "unwind  #{u.to_s}" if u.present?
	# only a string is allowed
	@unwind  # return_value
end

#where_sObject



254
255
256
# File 'lib/support.rb', line 254

def where_s
	compose_where @where
end