class OrientSupport::OrientQuery

Public Class Methods

mk_simple_setter(*m) click to toggle source
# File lib/support/orientquery.rb, line 488
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) click to toggle source
# File lib/support/orientquery.rb, line 500
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
new(**args) { |: 'and'| ... } click to toggle source
# File lib/support/orientquery.rb, line 334
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

Public Instance Methods

compose(destination: :batch) click to toggle source

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.

# File lib/support/orientquery.rb, line 390
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, limit ].compact.join(' ')
        elsif kind.to_sym == :update!
                [ 'update', target, set,  where, limit, misc ].compact.join(' ')
        elsif kind.to_sym == :create
                [ "CREATE VERTEX", target, set ].compact.join(' ')
        #    [ kind, target, set,  return_statement ,where,  limit, misc ].compact.join(' ')
        elsif kind.to_sym == :upsert 
                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
Also aliased as: to_s
connect_with(in_or_out, via: nil) click to toggle source

connects by adding {in_or_out}('edgeClass')

# File lib/support/orientquery.rb, line 590
def connect_with in_or_out, via: nil
         argument = " #{in_or_out}(#{via.to_or if via.present?})"
end
distinct(d) click to toggle source
# File lib/support/orientquery.rb, line 482
def distinct d
        @q[:projection] << "distinct " +  generate_sql_list( d ){ ' as ' }
        self
end
execute(reduce: false) { |x| ... } click to toggle source

returns nil if the query was not sucessfully executed

# File lib/support/orientquery.rb, line 625
def execute(reduce: false)
        #puts "Compose: #{compose}"
        result = V.orientdb.execute{ compose }
        return nil unless result.is_a?(::Array)
        result =  result.map{|x| yield x } if block_given?
        return  result.first if reduce && result.size == 1
        ## standard case: return Array
        OrientSupport::Array.new( work_on: resolve_target, work_with: result.orient_flatten)   
end
expand(item) click to toggle source
# File lib/support/orientquery.rb, line 584
            def expand item
                    @q[:projection] =[ " expand ( #{item.to_s} )" ]
                    self
end
from(arg = nil) click to toggle source

from can either be a Databaseclass to operate on or a Subquery providing data to query further

# File lib/support/orientquery.rb, line 444
def from arg = nil
        if arg.present?
                @q[:database] =  arg
                self # return query-object
        elsif  @q[:database].present? # read from
                "from #{ target }"
        end
end
group(value = nil) click to toggle source
# File lib/support/orientquery.rb, line 568
      def group value = nil
                    if value.present?
    @q[:group] << value
                    self
                    elsif @q[:group].present?
                     "group by #{@q[:group].join(', ')}"
                    end
end
Also aliased as: group_by
group_by(value = nil)
Alias for: group
kind(value=nil) click to toggle source
# File lib/support/orientquery.rb, line 376
def kind value=nil
        if value.present?
                @q[:kind] = value
                self
        else
        @q[:kind]
        end
end
let(value = nil) click to toggle source
# File lib/support/orientquery.rb, line 523
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_orient
                                                                                                                                end }"
                                end
                        end
                end.join(', ')
        end
end
nodes(in_or_out = :out, via: nil, where: nil, expand: true) click to toggle source

adds a connection

in_or_out:  :out --->  outE('edgeClass').in[where-condition] 
            :in  --->  inE('edgeClass').out[where-condition]
# File lib/support/orientquery.rb, line 597
def nodes in_or_out = :out, via: nil, where: nil, expand: true
         condition = where.present? ?  "[ #{generate_sql_list(where)} ]" : ""
         start =  if in_or_out  == :in
                                                        'inE'
                                                elsif in_or_out ==  :out
                                                        'outE'
                                                else
                                                        "both"
                                                end
         the_end =  if in_or_out == :in 
                                                                '.out' 
                                                        elsif in_or_out == :out
                                                                '.in'
                                                        else
                                                                ''
                                                        end
         argument = " #{start}(#{[via].flatten.map(&:to_or).join(',') if via.present?})#{the_end}#{condition} "

         if expand.present?
                 send :expand, argument
         else
                 @q[:projection]  << argument 
         end
         self
end
order(value = nil) click to toggle source
# File lib/support/orientquery.rb, line 454
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
Also aliased as: order_by
order_by(value = nil)
Alias for: order
resolve_target() click to toggle source
# File lib/support/orientquery.rb, line 635
def resolve_target
        if @q[:database].is_a? OrientSupport::OrientQuery
                @q[:database].resolve_target
        else
                @q[:database]
        end
end
target(arg = nil) click to toggle source
# File lib/support/orientquery.rb, line 416
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 ActiveOrient::Model   # a single record
                                                                the_argument.rrid
                                                        when self.class              # result of a query
                                                                ' ( '+ the_argument.compose + ' ) '
                                                        when Class
                                                                the_argument.ref_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() click to toggle source
# File lib/support/orientquery.rb, line 412
def to_or
        compose.to_or
end
to_s(destination: :batch)
Alias for: compose