Class: OrientSupport::MatchStatement

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

Instance Method Summary collapse

Methods included from Support

#as, #compose_where, #generate_sql_list, #where, #while_s

Constructor Details

#initialize(match_class, as: 0, **args) ⇒ MatchStatement

Returns a new instance of MatchStatement.



199
200
201
202
203
204
205
206
207
# File 'lib/support/orientquery.rb', line 199

def initialize match_class, as: 0,  **args
  reduce_class = ->(c){ c.is_a?(Class) ? c.ref_name : c.to_s }

  @q =  MatchSAttributes.new( reduce_class[match_class],  # class
            as.respond_to?(:zero?) && as.zero? ?  reduce_class[match_class].pluralize : as  ,      
            args[ :where ])

  @query_stack = [ self ]
end

Instance Method Details

#<<(connection) ⇒ Object



222
223
224
225
# File 'lib/support/orientquery.rb', line 222

def << connection
  @query_stack << connection
  self  # return MatchStatement
end

#compile(&b) ⇒ Object



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

def compile &b
   "match " + @query_stack.map( &:to_s ).join + return_statement( &b )
end

#compose_simpleObject Also known as: to_s

used for the first compose-statement of a compose-query



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

def compose_simple
    where_statement = where.is_a?(String) && where.size <3 ?  nil :  "where: ( #{ generate_sql_list( @q[:where] ) })"
  '{'+ [ "class: #{@q[:match_class]}",  as , where_statement].compact.join(', ') + '}'
end

#execute(as: :hash, &b) ⇒ Object

executes the standard-case. returns

* as: :hash   : an array of  hashes
* as: :array  : an array of hash-values 
* as  :flatten: a simple array of hash-values

The optional block is used to customize the output. All previously defiend »as«-Statements are provided though the control variable.

Background A match query “Match aaa, as: ‘aa’ return aa ”

returns [ aa: { result of the query, a Vertex or a value-item }, aa: {}…}, …] ] (The standard case)

A match query “Match aaa, as: ‘aa’ return aa.name ”

returns [ aa.name: { name }, aa.name: { name }., …] ]

Now, execute( as: :flatten){ “aa.name” } returns

[name1, name2 ,. ...]

Return statements (examples from orientdb.org/docs/3.0.x/sql/SQL-Match.html)

"person.name as name, friendship.since as since, friend.name as friend"

" person.name + \" is a friend of \" + friend.name as friends"

"$matches"
"$elements"
"$paths"
"$pathElements"


266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/support/orientquery.rb', line 266

def execute as: :hash, &b 
  r = V.db.execute{ compile &b }
  case as
  when :hash
    r
  when :array
   r.map{|y| y.values}
  when :flatten
   r.map{|y| y.values}.orient_flatten 
  else
    raise ArgumentError, "Specify parameter «as:» with :hash, :array, :flatten"
 end
end

#match_aliasObject



209
210
211
# File 'lib/support/orientquery.rb', line 209

def match_alias
  "as: #{@q[:as]}"
end