class OrientSupport::MatchStatement

Public Class Methods

new(match_class, as: 0, **args) click to toggle source
# 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

Public Instance Methods

<<(connection) click to toggle source
# File lib/support/orientquery.rb, line 222
def << connection
        @query_stack << connection
        self  # return MatchStatement
end
compile(&b) click to toggle source
# File lib/support/orientquery.rb, line 227
           def compile &b
"match " + @query_stack.map( &:to_s ).join + return_statement( &b )
           end
compose_simple() click to toggle source

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

# 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
Also aliased as: to_s
execute(as: :hash, &b) click to toggle source

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 {class 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 {class 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"
# 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_alias() click to toggle source
# File lib/support/orientquery.rb, line 209
def match_alias
        "as: #{@q[:as]}"
end
to_s()

def compose

'{'+ [ "class: #{@q[:match_class]}", 
                "as: #{@as}" , where, while_s, 
                        @maxdepth >0 ? "maxdepth: #{maxdepth}": nil  ].compact.join(', ')+'}'

end

Alias for: compose_simple

Private Instance Methods

return_statement() { |resolve_as[]| ... } click to toggle source
  return_statement

summarizes defined as-statements ready to be included as last parameter
in the match-statement-stack

They can be modified through a block.

i.e

t= TestQuery.match(  where: {a: 9, b: 's'}, as: nil ) << E.connect("<-", as: :test) 
t.return_statement{|y| "#{y.last}.name"} 

=>> " return  test.name"

return_statement is always called through compile

t.compile{|y| "#{y.last}.name"}
# File lib/support/orientquery.rb, line 308
def return_statement
        resolve_as = ->{             @query_stack.map{|s| s.as.split(':').last unless s.as.nil? }.compact }
        " return " + statement = if block_given? 
                                                                a= yield resolve_as[] 
                                                                a.is_a?(Array) ? a.join(', ') :  a
                                                        else
                                                                resolve_as[].join(', ')
                                                        end

        
end