Class: Arcade::Query
- Inherits:
-
Object
- Object
- Arcade::Query
- Includes:
- Support::Model, Support::Sql
- Defined in:
- lib/init.rb,
lib/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)
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.
-
#connect_with(in_or_out, via: nil) ⇒ Object
connects by adding in_or_out(‘edgeClass’).
-
#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 can either be a Databaseclass 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
adds a connection in_or_out: :out —> outE(‘edgeClass’).in :in —> inE(‘edgeClass’).out.
- #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 |
# File 'lib/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 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'
40 41 42 43 44 45 46 47 |
# File 'lib/query.rb', line 40 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
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/query.rb', line 172 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
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/query.rb', line 184 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
228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/query.rb', line 228 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
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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/query.rb', line 71 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 |
#connect_with(in_or_out, via: nil) ⇒ Object
connects by adding in_or_out(‘edgeClass’)
315 316 317 |
# File 'lib/query.rb', line 315 def connect_with in_or_out, via: nil argument = " #{in_or_out}(#{via.to_or if via.present?})" end |
#database_class ⇒ Object
:nodoc:
158 159 160 |
# File 'lib/query.rb', line 158 def database_class # :nodoc: @q[:database] end |
#database_class=(arg) ⇒ Object
:nodoc:
162 163 164 |
# File 'lib/query.rb', line 162 def database_class= arg # :nodoc: @q[:database] = arg end |
#db ⇒ Object
45 46 47 |
# File 'lib/init.rb', line 45 def db Init.db end |
#distinct(d) ⇒ Object
166 167 168 169 |
# File 'lib/query.rb', line 166 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
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/query.rb', line 350 def execute(reduce: false, autoload: true ) # unless projection.nil? || projection.empty? result = db.execute { 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
309 310 311 312 |
# File 'lib/query.rb', line 309 def item @q[:projection] =[ " expand ( #{item.to_s} )" ] self end |
#from(arg = nil) ⇒ Object
from can either be a Databaseclass to operate on or a Subquery providing data to query further
128 129 130 131 132 133 134 135 |
# File 'lib/query.rb', line 128 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:
305 306 307 |
# File 'lib/query.rb', line 305 def get_limit # :nodoc: @q[:limit].nil? ? -1 : @q[:limit].to_i end |
#group(value = nil) ⇒ Object Also known as: group_by
293 294 295 296 297 298 299 300 |
# File 'lib/query.rb', line 293 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
57 58 59 60 61 62 63 64 |
# File 'lib/query.rb', line 57 def kind value=nil if value.present? @q[:kind] = value self else @q[:kind] end end |
#let(value = nil) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/query.rb', line 250 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
241 242 243 244 245 246 247 248 249 |
# File 'lib/query.rb', line 241 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:
49 50 51 |
# File 'lib/query.rb', line 49 def misc # :nodoc: @q[:misc].join(' ') unless @q[:misc].empty? end |
#nodes(in_or_out = :out, via: nil, where: nil, expand: false) ⇒ Object
adds a connection
in_or_out: :out ---> outE('edgeClass').in[where-condition]
:in ---> inE('edgeClass').out[where-condition]
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/query.rb', line 322 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? start = if in_or_out.is_a? Symbol in_or_out.to_s elsif in_or_out.is_a? String in_or_out else "both" end argument = " #{start}(#{via})#{condition} " if .present? send :expand, argument else @q[:projection] << argument end self end |
#order(value = nil) ⇒ Object Also known as: order_by
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/query.rb', line 138 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:
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/query.rb', line 275 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
345 346 347 |
# File 'lib/query.rb', line 345 def query db.query compose end |
#resolve_target ⇒ Object
372 373 374 375 376 377 378 |
# File 'lib/query.rb', line 372 def resolve_target if @q[:database].is_a? Query @q[:database].resolve_target else @q[:database] end end |
#subquery ⇒ Object
:nodoc:
53 54 55 |
# File 'lib/query.rb', line 53 def subquery # :nodoc: nil end |
#target(arg = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/query.rb', line 100 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 else if the_argument.to_s.rid? # a string with "#ab:cd" the_argument else # a database-class-name the_argument.to_s end end else raise "cannot complete until a target is specified" end end |
#to_or ⇒ Object
96 97 98 |
# File 'lib/query.rb', line 96 def to_or compose.to_or end |
#where(value = nil) ⇒ Object
:nodoc:
215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/query.rb', line 215 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:
207 208 209 210 211 212 213 214 |
# File 'lib/query.rb', line 207 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 |