Class: Arcade::Query
- Inherits:
-
Object
- Object
- Arcade::Query
- Includes:
- Support::Model, Support::Sql
- Defined in:
- lib/arcade/init.rb,
lib/arcade/query.rb
Overview
Provides method ‘db` to every Query-Object
Class Method Summary collapse
Instance Method Summary collapse
- #as(a = nil) ⇒ Object
-
#compose(destination: :batch) ⇒ Object
(also: #to_s)
—————— compose————————————— Output the compiled query Parameter: destination (rest, batch ) If the query is submitted via the REST-Interface (as get-command), the limit parameter is extracted.
-
#database_class ⇒ Object
:nodoc:.
-
#database_class=(arg) ⇒ Object
:nodoc:.
- #db ⇒ Object
- #distinct(d) ⇒ Object
-
#execute(reduce: false, autoload: true) ⇒ Object
returns nil if the query was not sucessfully executed.
- #expand(item) ⇒ Object
-
#from(arg = nil) ⇒ Object
——————– from ————————————— arg can either be a Database class to operate on or a Subquery providing data to query further.
-
#get_limit ⇒ Object
:nodoc:.
- #group(value = nil) ⇒ Object (also: #group_by)
-
#initialize(**args) ⇒ Query
constructor
A new instance of Query.
- #kind(value = nil) ⇒ Object
- #let(value = nil) ⇒ Object
-
#map(value = nil) ⇒ Object
update_map - update an embedded map.
-
#method_missing(method, *arg, &b) ⇒ Object
where: “r > 9” –> where r > 9 where: 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’.
-
#misc ⇒ Object
:nodoc:.
-
#nodes(in_or_out = :out, via: nil, where: nil, expand: false) ⇒ Object
:inE —> inE(‘edgeClass’).outV() :outE —> outE(‘edgeClass’).inV().
- #order(value = nil) ⇒ Object (also: #order_by)
-
#projection(value = nil) ⇒ Object
:nodoc:.
- #query ⇒ Object
- #resolve_target ⇒ Object
-
#subquery ⇒ Object
:nodoc:.
- #target(arg = nil) ⇒ Object
- #to_or ⇒ Object
-
#where(value = nil) ⇒ Object
:nodoc:.
-
#while_s(value = nil) ⇒ Object
:nodoc:.
Methods included from Support::Model
#_allocate_model, #resolve_edge_name
Methods included from Support::Sql
#compose_where, #generate_sql_list
Constructor Details
#initialize(**args) ⇒ Query
Returns a new instance of Query.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/arcade/query.rb', line 17 def initialize **args @q = QueryAttributes.new args[:kind] || 'select' , [], # :projection [], # :where , [], # :let , [], # :order, [], # :while, [] , # misc '', # class '', # return [], # aliases '', # database [], #set, [], # remove [] # group by args.each{|k,v| send k, v} @fill = block_given? ? yield : 'and' 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'
41 42 43 44 45 46 47 48 |
# File 'lib/arcade/query.rb', line 41 def method_missing method, *arg, &b # :nodoc: if method ==:while || method=='while' while_s arg.first else @q[:misc] << method.to_s << generate_sql_list(arg) end self end |
Class Method Details
.mk_simple_setter(*m) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/arcade/query.rb', line 174 def mk_simple_setter *m m.each do |def_m| define_method( def_m ) do | value=nil | if value.present? @q[def_m] = value self elsif @q[def_m].present? "#{def_m.to_s} #{generate_sql_list(@q[def_m]){' ,'}}" end end end end |
.mk_std_setter(*m) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/arcade/query.rb', line 186 def mk_std_setter *m m.each do |def_m| define_method( def_m ) do | value = nil | if value.present? @q[def_m] << case value when String value when ::Hash value.map{|k,v| "#{k} = #{v.to_or}"}.join(", ") else raise "Only String or Hash allowed in #{def_m} statement" end self elsif @q[def_m].present? "#{def_m.to_s} #{@q[def_m].join(',')}" end # branch end # def_method end # each end |
Instance Method Details
#as(a = nil) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/arcade/query.rb', line 230 def as a=nil if a @q[:as] = a # subsequent calls overwrite previous entries else if @q[:as].blank? nil else "as: #{ @q[:as] }" end end end |
#compose(destination: :batch) ⇒ Object Also known as: to_s
—————— compose—————————————
Output the compiled query
Parameter: destination (rest, batch )
If the query is submitted via the REST-Interface (as get-command), the limit parameter is extracted.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/arcade/query.rb', line 70 def compose(destination: :batch) if kind.to_sym == :update return_statement = "return after " + ( @q[:aliases].empty? ? "$current" : @q[:aliases].first.to_s) [ 'update', target, set, remove, return_statement, where ].compact.join(' ') elsif kind.to_sym == :"update_map" [ "update", target, map, where, misc ].compact.join(' ') elsif kind.to_sym == :update! [ 'update', target, set, where, misc ].compact.join(' ') # elsif kind.to_sym == :create # relict of orientdb # [ "CREATE VERTEX", target, set ].compact.join(' ') # [ kind, target, set, return_statement ,where, limit, misc ].compact.join(' ') elsif kind.to_sym == :upsert set( generate_sql_list( @q[:where] ){ @fill || 'and' } ) if set.nil? return_statement = "return after " + ( @q[:aliases].empty? ? "$current" : @q[:aliases].first.to_s) [ "update", target, set,"upsert", return_statement, where, limit, misc ].compact.join(' ') #[ kind, where, return_statement ].compact.join(' ') elsif destination == :rest [ kind, projection, from, let, where, subquery, misc, order, group_by, unwind, skip].compact.join(' ') else [ kind, projection, from, let, where, subquery, while_s, misc, order, group_by, limit, unwind, skip].compact.join(' ') end end |
#database_class ⇒ Object
:nodoc:
160 161 162 |
# File 'lib/arcade/query.rb', line 160 def database_class # :nodoc: @q[:database] end |
#database_class=(arg) ⇒ Object
:nodoc:
164 165 166 |
# File 'lib/arcade/query.rb', line 164 def database_class= arg # :nodoc: @q[:database] = arg end |
#db ⇒ Object
48 49 50 |
# File 'lib/arcade/init.rb', line 48 def db Init.db end |
#distinct(d) ⇒ Object
168 169 170 171 |
# File 'lib/arcade/query.rb', line 168 def distinct d @q[:projection] << "distinct " + generate_sql_list( d ){ ' as ' } self end |
#execute(reduce: false, autoload: true) ⇒ Object
returns nil if the query was not sucessfully executed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/arcade/query.rb', line 361 def execute(reduce: false, autoload: true ) # unless projection.nil? || projection.empty? result = db.transmit { compose } return nil unless result.is_a?(Array) block_given? ? result.map{|x| yield x } : result # return result.first if reduce && result.size == 1 ## case select count(*) from ... --> [{ :count => n }] projection is set ## case update ... after $current --> [{ :$current => n}] projection is not set, but result is an integer # separate key from values and get model-files # if !@q[:projection].empty? && result.first.is_a?(Hash) && result.first.values.is_a?( Array ) # if reduce # result.first.values.map{|x| allocate_model x, autoload} # else # result.map{|_,m| allocate_model m, autoload } # end # eloe # result.map{|y| allocate_model y, autoload } # end ## standard case: return Array #result.arcade_flatten end |
#expand(item) ⇒ Object
311 312 313 314 |
# File 'lib/arcade/query.rb', line 311 def item @q[:projection] =[ " expand ( #{item.to_s} )" ] self end |
#from(arg = nil) ⇒ Object
——————– from —————————————
arg can either be a Database class to operate on or a Subquery providing data to query further
130 131 132 133 134 135 136 137 |
# File 'lib/arcade/query.rb', line 130 def from arg = nil if arg.present? @q[:database] = arg self # return query-object elsif @q[:database].present? # read from "from #{ target }" end end |
#get_limit ⇒ Object
:nodoc:
307 308 309 |
# File 'lib/arcade/query.rb', line 307 def get_limit # :nodoc: @q[:limit].nil? ? -1 : @q[:limit].to_i end |
#group(value = nil) ⇒ Object Also known as: group_by
295 296 297 298 299 300 301 302 |
# File 'lib/arcade/query.rb', line 295 def group value = nil if value.present? @q[:group] << value self elsif @q[:group].present? "group by #{@q[:group].join(', ')}" end end |
#kind(value = nil) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/arcade/query.rb', line 58 def kind value=nil if value.present? @q[:kind] = value self else @q[:kind] end end |
#let(value = nil) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/arcade/query.rb', line 252 def let value = nil if value.present? @q[:let] << value self elsif @q[:let].present? "let " << @q[:let].map do |s| case s when String s when ::Hash s.map do |x,y| # if the symbol: value notation of Hash is used, add "$" to the key x = "$#{x.to_s}" unless x.is_a?(String) && x[0] == "$" "#{x} = #{ case y when self.class "(#{y.compose})" else y.to_db end }" end end end.join(', ') end end |
#map(value = nil) ⇒ Object
update_map - update an embedded map
243 244 245 246 247 248 249 250 251 |
# File 'lib/arcade/query.rb', line 243 def map value=nil if value.present? @q[:map] = value self elsif @q[:map].present? # only one pair is supported "set #{@q[:map].keys.first} = #{@q[:map].values.first.to_or}" end end |
#misc ⇒ Object
:nodoc:
50 51 52 |
# File 'lib/arcade/query.rb', line 50 def misc # :nodoc: @q[:misc].join(' ') unless @q[:misc].empty? end |
#nodes(in_or_out = :out, via: nil, where: nil, expand: false) ⇒ Object
:inE —> inE(‘edgeClass’).outV()
:outE ---> outE('edgeClass')[where-condition].inV()
via: Edge-Class
where: Condition to be applied on the targed vertex (in_or_out = :in, :out, :both)
or on the intermitted edge (in_or_out = :inE, :outE, :bothE)
Condition is inserted as "in_or_out[ condition ]"
Attention: ranges have to be included as array, ie [ 2..4 ]
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/arcade/query.rb', line 331 def nodes in_or_out = :out, via: nil, where: nil, expand: false condition = where.present? ? "[ #{generate_sql_list(where)} ]" : "" via = resolve_edge_name(via) unless via.nil? argument = if in_or_out.to_s[-1] == 'E' case in_or_out.to_s[0..-2] when 'in' "inE(#{via})#{condition}.outV()" when 'out' "outE(#{via})#{condition}.inV()" end else "#{in_or_out.to_s}(#{via})#{condition}" end if .present? send :expand, argument else @q[:projection] << argument end self end |
#order(value = nil) ⇒ Object Also known as: order_by
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/arcade/query.rb', line 140 def order value = nil if value.present? @q[:order] << value self elsif @q[:order].present? "order by " << @q[:order].compact.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(value = nil) ⇒ Object
:nodoc:
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/arcade/query.rb', line 277 def projection value= nil # :nodoc: if value.present? @q[:projection] << value self elsif @q[:projection].present? @q[:projection].compact.map do | s | case s when ::Array s.join(', ') when String, Symbol s.to_s when ::Hash s.map{ |x,y| "#{x} as #{y}"}.join( ', ') end end.join( ', ' ) end end |
#query ⇒ Object
356 357 358 |
# File 'lib/arcade/query.rb', line 356 def query db.query compose end |
#resolve_target ⇒ Object
383 384 385 386 387 388 389 |
# File 'lib/arcade/query.rb', line 383 def resolve_target if @q[:database].is_a? Query @q[:database].resolve_target else @q[:database] end end |
#subquery ⇒ Object
:nodoc:
54 55 56 |
# File 'lib/arcade/query.rb', line 54 def subquery # :nodoc: nil end |
#target(arg = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/arcade/query.rb', line 99 def target arg = nil if arg.present? @q[:database] = arg self # return query-object elsif @q[:database].present? the_argument = @q[:database] case @q[:database] when Base # a single record the_argument.rid when self.class # result of a query ' ( '+ the_argument.compose + ' ) ' when Class the_argument.database_name when Arcade::Match '(' + the_argument.to_s + ')' when String if the_argument.to_s.rid? # a string with "#ab:cd" the_argument else '(' + the_argument + ')' end else # a database-class-name the_argument.to_s end else raise "cannot complete until a target is specified" end end |
#to_or ⇒ Object
95 96 97 |
# File 'lib/arcade/query.rb', line 95 def to_or compose.to_or end |
#where(value = nil) ⇒ Object
:nodoc:
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/arcade/query.rb', line 217 def where value=nil # :nodoc: if value.present? if value.is_a?( Hash ) && value.size >1 value.each {| a, b | where( {a => b} ) } else @q[:where] << value end self elsif @q[:where].present? "where #{ generate_sql_list( @q[:where] ){ @fill || 'and' } }" end end |
#while_s(value = nil) ⇒ Object
:nodoc:
209 210 211 212 213 214 215 216 |
# File 'lib/arcade/query.rb', line 209 def while_s value=nil # :nodoc: if value.present? @q[:while] << value self elsif @q[:while].present? "while #{ generate_sql_list( @q[:while] ) }" end end |