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.



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
217
218
# File 'lib/support.rb', line 185

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



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

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.



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

def let
  @let
end

#match_statementsObject

Returns the value of attribute match_statements.



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

def match_statements
  @match_statements
end

#orderObject

Returns the value of attribute order.



182
183
184
# File 'lib/support.rb', line 182

def order
  @order
end

#projectionObject

Returns the value of attribute projection.



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

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'


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

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.


288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/support.rb', line 288

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



255
256
257
258
259
# File 'lib/support.rb', line 255

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



331
332
333
334
335
336
337
338
339
# File 'lib/support.rb', line 331

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

#database_class=(arg) ⇒ Object



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

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=



368
369
370
371
372
373
374
375
376
377
# File 'lib/support.rb', line 368

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  # return the hole query
end

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



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/support.rb', line 308

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 databas-class-name
      arg.to_s  
    end
  end
  compose  # return the complete query
  else # read from
  "from #{@database}" unless @database.nil?
  end
end

#get_limitObject



400
401
402
# File 'lib/support.rb', line 400

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

#group_by(g = nil) ⇒ Object



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

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



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/support.rb', line 352

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=



393
394
395
396
397
# File 'lib/support.rb', line 393

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

#miscObject



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

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

#order_sObject



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

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

#projection_sObject



380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/support.rb', line 380

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



416
417
418
419
# File 'lib/support.rb', line 416

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 ,


278
279
280
281
# File 'lib/support.rb', line 278

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

#subqueryObject



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

def subquery
  nil
end

#unwind(u = nil) ⇒ Object



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

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

#where_sObject



348
349
350
# File 'lib/support.rb', line 348

def where_s
 compose_where @where
end