Class: ActiveOrient::Query

Inherits:
Model show all
Defined in:
lib/query.rb

Overview

this is depreciated and not maintained anymore

Constant Summary

Constants included from OrientDB

OrientDB::DocumentDatabase, OrientDB::DocumentDatabasePool, OrientDB::DocumentDatabasePooled, OrientDB::GraphDatabase, OrientDB::IndexType, OrientDB::OClassImpl, OrientDB::OTraverse, OrientDB::PropertyImpl, OrientDB::RemoteStorage, OrientDB::SQLCommand, OrientDB::SQLSynchQuery, OrientDB::Schema, OrientDB::SchemaProxy, OrientDB::SchemaType, OrientDB::ServerAdmin, OrientDB::User, OrientDB::UsingJava

Instance Attribute Summary

Attributes inherited from Model

#metadata

Attributes inherited from Base

#metadata

Instance Method Summary collapse

Methods inherited from Model

autoload_object, delete_class, #document, #to_ary

Methods included from ModelClass

#add_edge_link, #all, #alter_property, #classname, #count, #create, #create_index, #create_properties, #create_property, #custom_where, #delete_property, #delete_record, #delete_records, #first, #get, #get_model_class, #get_properties, #last, #match, #naming_convention, #orientdb_class, #print_class_properties, #query_database, #remove, #require_model_file, #update_all, #update_or_create_records, #upsert, #where

Methods included from ModelRecord

#add_item_to_property, classname, #find, #from_orient, #increment_version, #is_edge?, #method_missing, #query, #reload!, #remove, #remove_item_from_property, #remove_position_from_property, #rid, #rrid, #save, #set_item_to_property, #to_or, #update, #update_attribute, #update_attributes, #update_item_property, #version, #version=

Methods included from BaseProperties

#==, #content_attributes, #default_attributes, #set_attribute_defaults, #to_human, #update_missing

Methods inherited from Base

#[], #[]=, attr_accessible, attr_protected, #attributes, #attributes=, belongs_to, display_rid, #document, get_rid, has_many, has_one, #initialize, #my_metadata, remove_rid, reset_rid_store, serialize, store_rid, #to_model, #update_attribute

Constructor Details

This class inherits a constructor from ActiveOrient::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ModelRecord

Instance Method Details

#execute_queries(reset: true, transaction: true) ⇒ Object

All predefined queries are send to the database.

The result is stored in the records.
Unknown Records are of Type ActiveOrient::Model::Myquery, uses ActiveOrient::Orientdb.execute which tries to autosuggest the ActiveOrient::Model::{Class}

example: Multible Records
  ach = ActiveOrient::Query.new
  ach.queries << 'create class Contracts ABSTRACT'
  ach.queries << 'create property Contracts.details link'
  ach.queries << 'create class Stocks extends Contracts'
  result = ach.execute_queries transaction: false

example: Batch
  q = ActiveOrient::Query.new
  q.queries << [
  "select expand( contracts ) from Openinterest"
  "let con = select expand( contracts ) from Openinterest;",
  "let sub = select from Subcategories where contracts in $con;",
  "let cat = select from Categories where subcategories in $sub;",
  "let ind = select from Industries where categories in $cat;",
  "SELECT expand(unionall) FROM (SELECT unionall( $con, $cat))"
  ]
  q.execute_queries.each{|x| puts "X #{x.inspect}" }


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/query.rb', line 59

def execute_queries reset: true, transaction: true
  reset_records if reset
  begin
    orientdb.execute( transaction: transaction ) do
      result = queries.map do |q|
# command: words are seperated by one space only, thus squeeze multible spaces
        sql_cmd = -> (command) {{type: "cmd", language: "sql", command: command.squeeze(' ') }}
        batch_cmd = -> (command_array){{type: "script", language: "sql", script: command_array}}
        case q
        when String
         sql_cmd[q]
        when Hash
         q
        when Array
         batch_cmd[q]
       else
         nil
        end # case
      end.compact
 # save the result in records
      result.each{|y| records << y}
    end # block
  rescue RestClient::InternalServerError => e
    puts e.inspect
  end
end

#get_records(o_class, **args) ⇒ Object Also known as: get_documents

Calls ActiveOrient::ActiveOrient#GetRecords

Stores the query in the query-stack and saves the result in the record-Array

Returns the count of assigned records


25
26
27
28
29
30
31
# File 'lib/query.rb', line 25

def get_records o_class , **args
  query = OrientSupport::OrientQuery.new classname(o_class), args
 self.queries << query.compose
  count = 0
  orientdb.get_records(o_class, query: query.compose).each{|c| records << c; count += 1}
  count
end

#reset_queriesObject



14
15
16
# File 'lib/query.rb', line 14

def reset_queries
  self.queries = []
end

#reset_recordsObject Also known as: reset_results



9
10
11
# File 'lib/query.rb', line 9

def reset_records
  self.records= []
end