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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/support.rb', line 181

def initialize  **args
  @projection = []
  @misc  = []
  @let   = []
  @where = []
  @order = []
  @aliases = []
  @match_statements = []
  @class =  nil
  @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
	when :start
	  @match_statements[0] = MatchStatement.new **v
#  @match_statements[1] = MatchConnection.new
	when :connection
	  @match_statements[1] = MatchConnection.new **v
	when :return
	  @aliases << 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



216
217
218
# File 'lib/support.rb', line 216

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.



176
177
178
# File 'lib/support.rb', line 176

def let
  @let
end

#match_statementsObject

Returns the value of attribute match_statements.



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

def match_statements
  @match_statements
end

#orderObject

Returns the value of attribute order.



178
179
180
# File 'lib/support.rb', line 178

def order
  @order
end

#projectionObject

Returns the value of attribute projection.



177
178
179
# File 'lib/support.rb', line 177

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: {a: 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'


175
176
177
# File 'lib/support.rb', line 175

def where
  @where
end

Instance Method Details

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

Output the compiled query

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


284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/support.rb', line 284

def compose(destination: :batch)
  if @kind == :match
	unless @match_statements.empty?
	match_query =  @kind.to_s.upcase + " "+ @match_statements[0].compose_simple 
	match_query << @match_statements[1..-1].map( &:compose ).join
	match_query << " RETURN "<< (@match_statements.map( &:as ).compact | @aliases).join(', ')
	end
  elsif 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

#connect(direction, edge_class: nil, count: 1, as: nil) ⇒ Object

(only if kind == :match): connect

Add a connection to the match-query

A Match-Query alwas has an Entry-Stratement and maybe other Statements. They are connected via “ -> ” (outE), “<-” (inE) or “–” (both).

The connection method adds a connection to the statement-stack.

Parameters:

direction: :in, :out, :both
edge_class: to restrict the Query on a certain Edge-Class
count: To repeat the connection
as:  Includes a micro-statement to finalize the Match-Query
     as: defines a output-variablet, which is used later in the return-statement

The method returns the OrientSupport::MatchConnection object, which can be modified further. It is compiled by calling compose



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

def connect direction, edge_class: nil, count: 1, as: nil
  direction= :both unless [ :in, :out].include? direction
  match_statements << m = OrientSupport::MatchConnection.new( direction: direction, count: count, as: as)
  m
end

#database_classObject



325
326
327
328
329
330
331
332
333
# File 'lib/support.rb', line 325

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

#database_class=(arg) ⇒ Object



335
336
337
338
339
340
# File 'lib/support.rb', line 335

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=



362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/support.rb', line 362

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=



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/support.rb', line 304

def from arg = nil
  if arg.present?
	@database = case arg
  when ActiveOrient::Model
    arg.rrid
  when OrientQuery
    ' ( '+ arg.to_s + ' ) '
  else
    if arg.to_s.rid?
			arg
    else
			ORD.classname(arg)
    end
  end
	compose  # return the complete query
  else # read from
	"from #{@database}" unless @database.nil?
  end
end

#get_limitObject



397
398
399
# File 'lib/support.rb', line 397

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

#group_by(g = nil) ⇒ Object



401
402
403
404
405
# File 'lib/support.rb', line 401

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



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

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

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



390
391
392
393
394
# File 'lib/support.rb', line 390

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

#miscObject



220
221
222
# File 'lib/support.rb', line 220

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

#order_sObject



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/support.rb', line 418

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|
	  #puts "CLASS : "+o.class.to_s
	  #puts o.to_s
	  case o
	  when String, Symbol, Array
 o.to_s
	  else
 o.map{|x,y| "#{x} #{y}"}.join(" ")
	  end  # case
	end.join(', ')
  else
	''
  end # unless
end

#projection_sObject



377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/support.rb', line 377

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

#skip(n = nil) ⇒ Object



413
414
415
416
# File 'lib/support.rb', line 413

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

#statement(match_class = nil, **args) ⇒ Object

(only if kind == :match): statement

A Match Query consists of a simple start-statement ( classname and where-condition ), a connection followd by other Statement-connection-pairs. It performs a sub-query starting at the given entry-point.

Statement adds a statement to the statement-stack. Statement returns the created OrientSupport::MatchStatement-record for further modifications. It is compiled by calling »compose«.

OrientSupport::OrientQuery collects any “as”-directive for inclusion in the return-statement

Parameter (all optional)

Class: classname, :where: {}, while: {}, as: string, maxdepth: >0 ,


274
275
276
277
# File 'lib/support.rb', line 274

def statement match_class= nil, **args
  match_statements << s = OrientSupport::MatchStatement.new( mattch_class, args )
  s
end

#subqueryObject



224
225
226
# File 'lib/support.rb', line 224

def subquery
  nil
end

#unwind(u = nil) ⇒ Object



407
408
409
410
411
# File 'lib/support.rb', line 407

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

#where_sObject



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

def where_s
 compose_where @where
end