Class: OrientSupport::OrientQuery

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/support/orientquery.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
215
216
# File 'lib/support/orientquery.rb', line 181

def initialize  **args
    @projection = []
    @misc  = []
    @let   = []
    @where = []
    @order = []
    @aliases = []
    @match_statements = []
    @class =  nil
  @return = nil
  @db = 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

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'


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

def method_missing method, *arg, &b   # :nodoc: 
  @misc << method.to_s <<  generate_sql_list(arg) 
      self.to_s  # return compiled result
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



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

def db
  @db
end

#letObject

Returns the value of attribute let.



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

def let
  @let
end

#match_statementsObject

Returns the value of attribute match_statements.



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

def match_statements
  @match_statements
end

#orderObject

Returns the value of attribute order.



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

def order
  @order
end

#projectionObject

Returns the value of attribute projection.



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

def projection
  @projection
end

#whereObject

Returns the value of attribute where.



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

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  via the REST-Interface (as get-command), the limit parameter is extracted.


295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/support/orientquery.rb', line 295

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 @kind.to_sym == :update
    return_statement = "return after " + ( @aliases.empty? ?  "$this" : @aliases.first.to_s)
    [ @kind, @database, misc, where_s, return_statement ].compact.join(' ')
  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



262
263
264
265
266
# File 'lib/support/orientquery.rb', line 262

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

:nodoc:



341
342
343
344
345
346
347
348
349
# File 'lib/support/orientquery.rb', line 341

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

#database_class=(arg) ⇒ Object

:nodoc:



351
352
353
354
355
356
# File 'lib/support/orientquery.rb', line 351

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

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



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

def distinct d
      @projection <<  case d
      when String, Symbol
"distinct #{d.to_s} "
      else
dd= d.to_a.flatten
"distinct  #{dd.first.to_s}  as #{dd.last}"
      end
      compose
end

#expand(item) ⇒ Object



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

def expand item
      @projection =[ " expand ( #{item.to_s} )" ]
      compose
end

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



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/support/orientquery.rb', line 318

def from arg = nil
  if arg.present?
    @database = case arg
                when ActiveOrient::Model   # a single record
                  arg.rrid
                when OrientQuery        # result of a query
                  ' ( '+ arg.to_s + ' ) '
                when Class
                  arg.ref_name
                else
                  if arg.to_s.rid?   # a string with "#ab:cd"
                    arg
                  else     # a database-class-name
                    arg.to_s  
                  end
                end
    compose  # return the complete query
  else # read from
    "from #{@database}" unless @database.nil?
  end
end

#get_limitObject

:nodoc:



410
411
412
# File 'lib/support/orientquery.rb', line 410

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

#group_by(g = nil) ⇒ Object



434
435
436
437
438
# File 'lib/support/orientquery.rb', line 434

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

#let_sObject

:nodoc:



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

def let_s           # :nodoc:
 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=



403
404
405
406
407
# File 'lib/support/orientquery.rb', line 403

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

#miscObject

:nodoc:



230
231
232
233
# File 'lib/support/orientquery.rb', line 230

def misc   # :nodoc:
  @misc.join(' ') unless @misc.empty?
  #    self.to_s  # return compiled result
end

#nodes(in_or_out = :out, via: nil, where: nil, expand: true) ⇒ Object



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

def nodes in_or_out = :out, via:  nil, where: nil, expand: true
   condition = where.present? ?  "[ #{generate_sql_list(where)} ]" : ""
   start =  in_or_out 
   the_end =  in_or_out == :in ? :out : :in
   argument = " #{start}E(#{via.to_or if via.present?}).#{the_end}#{condition} "

   if expand.present?
     send :expand, argument
   else
   @projection  << argument 
   end
   compose
end

#order_sObject

:nodoc:



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/support/orientquery.rb', line 451

def order_s    # :nodoc:
  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 String, Symbol, Array
 o.to_s
    else
 o.map{|x,y| "#{x} #{y}"}.join(" ")
    end  # case
  end.join(', ')
  else
  ''
  end # unless
end

#projection_sObject

:nodoc:



390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/support/orientquery.rb', line 390

def projection_s   # :nodoc:
 @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



446
447
448
449
# File 'lib/support/orientquery.rb', line 446

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 ,


285
286
287
288
# File 'lib/support/orientquery.rb', line 285

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

#subqueryObject

:nodoc:



235
236
237
# File 'lib/support/orientquery.rb', line 235

def subquery  # :nodoc: 
  nil
end

#unwind(u = nil) ⇒ Object



440
441
442
443
444
# File 'lib/support/orientquery.rb', line 440

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

#where_sObject

:nodoc:



358
359
360
# File 'lib/support/orientquery.rb', line 358

def where_s        # :nodoc:
 compose_where @where
end