Class: Arcade::MatchStatement

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MatchStatement.



97
98
99
100
101
102
103
104
105
# File 'lib/match.rb', line 97

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



120
121
122
123
# File 'lib/match.rb', line 120

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

#compile(&b) ⇒ Object



125
126
127
# File 'lib/match.rb', line 125

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



114
115
116
117
# File 'lib/match.rb', line 114

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"


163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/match.rb', line 163

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



107
108
109
# File 'lib/match.rb', line 107

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