Module: RestCreate
- Included in:
- ActiveOrient::OrientDB
- Defined in:
- lib/rest/create.rb
Instance Method Summary collapse
-
#create_database(type: 'plocal', database:) ⇒ Object
Creates a database with the given name and switches to this database as working-database.
-
#create_index(o_class, name:, on: :automatic, type: :unique) ⇒ Object
Used to create an index.
-
#create_properties(o_class, all_properties, &b) ⇒ Object
Creates properties .
-
#create_property(o_class, field, index: nil, **args, &b) ⇒ Object
Create a single property.
-
#create_record(o_class, attributes: {}, cache: true, silence: true) ⇒ Object
(also: #create_document)
Creates a Record in the Database and returns this as ActiveOrient::Model-Instance.
-
#upsert(o_class, set:, where:) ⇒ Object
update or insert one record is implemented as upsert.
Instance Method Details
#create_database(type: 'plocal', database:) ⇒ Object
Creates a database with the given name and switches to this database as working-database. Types are either ‘plocal’ or ‘memory’
Returns the name of the working-database
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rest/create.rb', line 11 def create_database type: 'plocal', database: logger.progname = 'RestCreate#CreateDatabase' old_d = ActiveOrient.database ActiveOrient.database_classes = {} ActiveOrient.database = database begin response = ActiveOrient.db_pool.checkout do | conn | conn["database/#{ActiveOrient.database}/#{type}"].post "" end if response.code == 200 logger.info{"Database #{ActiveOrient.database} successfully created and stored as working database"} else logger.error{"Database #{ActiveOrient.database} was NOT created. Working Database is still #{ActiveOrient.database}"} ActiveOrient.database = old_d end rescue RestClient::InternalServerError => e logger.error{"Database #{ActiveOrient.database} was NOT created. Working Database is still #{ActiveOrient.database}"} ActiveOrient.database = old_d end ActiveOrient.database # return_value end |
#create_index(o_class, name:, on: :automatic, type: :unique) ⇒ Object
Used to create an index
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/rest/create.rb', line 268 def create_index o_class, name:, on: :automatic, type: :unique logger.progname = 'RestCreate#CreateIndex' c = classname o_class if execute( transaction: false, tolerated_error_code: /found duplicated key/) do if on == :automatic "CREATE INDEX #{c}.#{name} #{type.to_s.upcase}" elsif on.is_a? Array "CREATE INDEX #{name} ON #{c}(#{on.join(', ')}) #{type.to_s.upcase}" else "CREATE INDEX #{name} ON #{c}(#{on.to_s}) #{type.to_s.upcase}" end end logger.info{"Index on #{c} based on #{name} created."} else logger.error {"index #{name}.#{type} on #{c} NOT created"} end end |
#create_properties(o_class, all_properties, &b) ⇒ Object
Creates properties
and (if defined in the provided block) associates an index
create_properties(classname or class, properties as hash){index}
The default-case
create_properties(:my_high_sophisticated_database_class,
con_id: {type: :integer},
details: {type: :link, linked_class: 'Contracts'}) do
contract_idx: :notunique
end
A composite index
create_properties(:my_high_sophisticated_database_class,
con_id: {type: :integer},
symbol: {type: :string}) do
{name: 'indexname',
on: [:con_id, :details] # default: all specified properties
type: :notunique # default: :unique
}
end
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/rest/create.rb', line 195 def create_properties o_class, all_properties, &b logger.progname = 'RestCreate#CreateProperties' all_properties_in_a_hash = Hash.new #WithIndifferentAccess.new all_properties.each{|field, args| all_properties_in_a_hash.merge! translate_property_hash(field, args)} count, response = 0, nil # puts "all_properties_in_a_hash #{all_properties_in_a_hash.to_json}" if all_properties_in_a_hash.is_a?(Hash) begin response = ActiveOrient.db_pool.checkout do | conn | conn["/property/#{ActiveOrient.database}/#{classname(o_class)}"].post all_properties_in_a_hash.to_json end # puts response.inspect # response.body.to_i returns response.code, only to_f.to_i returns the correct value count= response.body.to_f.to_i if response.code == 201 rescue RestClient::InternalServerError => e logger.progname = 'RestCreate#CreateProperties' response = JSON.parse(e.response)['errors'].pop = response['content'].split(':').last logger.error{"Properties in #{classname(o_class)} were NOT created"} logger.error{"The Error was: #{response['content'].split(':').last}"} nil end end ### index if block_given?# && count == all_properties_in_a_hash.size index = yield if index.is_a?(Hash) if index.size == 1 create_index o_class, name: index.keys.first, on: all_properties_in_a_hash.keys, type: index.values.first else index_hash = {type: :unique, on: all_properties_in_a_hash.keys}.merge index create_index o_class, name: index_hash[:name], on: index_hash[:on], type: index_hash[:type] end end end count # return_value end |
#create_property(o_class, field, index: nil, **args, &b) ⇒ Object
Create a single property.
Supported types: orientdb.com/docs/last/SQL-Create-Property.html
If an index is to be specified, it’s defined in the optional block
create_property(class, field){:unique | :notunique}
--> creates an automatic-Index on the given field
create_property(class, field){{»name« => :unique | :notunique | :full_text}}
--> creates a manual index
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/rest/create.rb', line 247 def create_property o_class, field, index: nil, **args, &b logger.progname = 'RestCreate#CreateProperty' args= { type: :string} if args.blank? # the default case c = create_properties o_class, {field => args} if index.nil? && block_given? index = yield end if index.present? if index.is_a?(String) || index.is_a?(Symbol) create_index o_class, name: field, type: index elsif index.is_a? Hash bez = index.keys.first create_index o_class, name: bez, type: index[bez], on: [field] end end end |
#create_record(o_class, attributes: {}, cache: true, silence: true) ⇒ Object Also known as: create_document
Creates a Record in the Database and returns this as ActiveOrient::Model-Instance
Creates a Record with the attributes provided in the attributes-hash e.g.
create_record @classname, attributes: {con_id: 343, symbol: 'EWTZ'}
Puts the database-response into the cache by default
Argument: silence
if silence is specified, no Error-Messages are raised. Instead
* if a record failes to be created, because an index-error occured, it is replaced by that specified in the database response
97 98 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 127 128 129 130 131 132 |
# File 'lib/rest/create.rb', line 97 def create_record o_class, attributes: {}, cache: true, silence: true # use Model#create instead logger.progname = 'RestCreate#CreateRecord' attributes = yield if attributes.empty? && block_given? # @class must not quoted! Quote only attributes(strings) post_argument = {'@class' => classname(o_class)}.merge(attributes.to_orient) response = nil begin response = ActiveOrient.db_pool.checkout do | conn | conn["/document/#{ActiveOrient.database}"].post post_argument.to_json end data = JSON.parse(response.body) the_object = ActiveOrient::Model.orientdb_class(name: data['@class']).new data ## return_value if cache ActiveOrient::Base.store_rid( the_object ) else the_object end rescue RestClient::InternalServerError => e sentence= JSON.parse( e.response)['errors'].last['content'] if sentence =~ /found duplicated key/ raise "Duplicate Key" unless silence rid = sentence.split("#").last logger.info{ "found duplicated Key --> loaded #{rid} instead of creating "} ## reading database content -- maybe update attributes? get_record rid else response = JSON.parse(e.response)['errors'].pop logger.error{response['content'].split(':')[1..-1].join(':')} logger.error{"No Object allocated"} nil # return_value end rescue Errno::EADDRNOTAVAIL => e sleep(2) retry end end |
#upsert(o_class, set:, where:) ⇒ Object
update or insert one record is implemented as upsert. The where-condition is merged into the set-attributes if its a hash.
Otherwise it’s taken unmodified.
The method returns the included or the updated dataset
## to do # yield works for updated and for inserted datasets # upsert ( ) do | what, record | # if what == :insert # do stuff with insert # if what == :update # do stuff with update # end # returns nil if no insert and no update was made, ie. the dataset is identical to the given attributes
160 161 162 163 164 165 166 167 168 |
# File 'lib/rest/create.rb', line 160 def upsert o_class, set: , where: # use Model#Upsert instead logger.progname = 'RestCreate#Upsert' specify_return_value = "return after @rid" # set.merge! where if where.is_a?( Hash ) # copy where attributes to set command = "Update #{classname(o_class)} set #{generate_sql_list( set ){','}} upsert #{specify_return_value} #{compose_where where}" result = execute( tolerated_error_code: /found duplicated key/){ command } # puts "result #{result.inspect}" result =result.pop if result.is_a? Array end |