Class: ActiveOrient::Query
Instance Method Summary collapse
-
#execute_queries(reset: true, transaction: true) ⇒ Object
All predefined queries are send to the database.
-
#get_documents(o_class, **args) ⇒ Object
calls ActiveOrient::ActiveOrient#GetDocuments stores the query in the query-stack and saves the result in the record-Array.
- #reset_queries ⇒ Object
- #reset_records ⇒ Object (also: #reset_results)
Methods inherited from Model
#add_item_to_property, all, autoload_object, #classname, count, create, create_edge, create_link, create_linkset, create_properties, create_property, #delete, first, #from_orient, get, get_documents, get_properties, #is_edge?, last, #link, #nested_under_indifferent_access, new_document, orientdb_class, #query, query_database, #reload!, #remove_item_from_property, #rid, #riid, superClass, #to_orient, #update, update_or_create, #version, where
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_riid, find, get_riid, has_many, has_one, #initialize, remove_riid, serialize, store_riid, #to_model, #update_attribute
Constructor Details
This class inherits a constructor from ActiveOrient::Base
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}” }
57 58 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 57 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_documents(o_class, **args) ⇒ Object
calls ActiveOrient::ActiveOrient#GetDocuments stores the query in the query-stack and saves the result in the record-Array
returns the count of assigned records
23 24 25 26 27 28 29 30 |
# File 'lib/query.rb', line 23 def get_documents o_class , **args query = OrientSupport::OrientQuery.new class_name(o_class), args self.queries << query.compose count= 0 orientdb.get_documents( o_class , query: query.compose ).each{|c| records << c; count+=1 } count end |
#reset_queries ⇒ Object
13 14 15 |
# File 'lib/query.rb', line 13 def reset_queries self.queries = [] end |
#reset_records ⇒ Object Also known as: reset_results
8 9 10 |
# File 'lib/query.rb', line 8 def reset_records self.records= [] end |