Module: RestChange

Included in:
ActiveOrient::OrientDB
Defined in:
lib/rest/change.rb

Instance Method Summary collapse

Instance Method Details

#alter_property(o_class, property:, attribute: "DEFAULT", alteration:) ⇒ Object

Used to add restriction or other properties to the Property of a Class.

See http://orientdb.com/docs/2.1/SQL-Alter-Property.html


85
86
87
88
89
90
91
92
93
94
95
96
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
# File 'lib/rest/change.rb', line 85

def alter_property o_class, property:, attribute: "DEFAULT", alteration:  # :nodoc: because untested
  logger.progname = 'RestChange#AlterProperty'
  begin
    attribute.to_s! unless attribute.is_a? String
    attribute.capitalize_first_letter
    case attribute
    when "LINKEDCLASS", "LINKEDTYPE", "NAME", "REGEX", "TYPE", "REGEX", "COLLATE", "CUSTOM"
      unless alteration.is_a? String
        logger.error{"#{alteration} should be a String."}
        return 0
      end
    when "MIN", "MAX"
      unless alteration.is_a? Integer
        logger.error{"#{alteration} should be an Integer."}
        return 0
      end
    when "MANDATORY", "NOTNULL", "READONLY"
      unless alteration.is_a? TrueClass or alteration.is_a? FalseClass
        logger.error{"#{alteration} should be an Integer."}
        return 0
      end
    when "DEFAULT"
    else
      logger.error{"Wrong attribute."}
      return 0
    end

    name_class = classname(o_class)
    execute name_class, transaction: false do # To execute commands
      [{ type: "cmd",
        language: 'sql',
        command: "ALTER PROPERTY #{name_class}.#{property} #{attribute} #{alteration}"}]
    end
  rescue Exception => e
    logger.error{e.message}
  end
end

#change_database(name) ⇒ Object

Changes the working-database to name



7
8
9
10
11
# File 'lib/rest/change.rb', line 7

def change_database name
  @classes = []
  @database = name
  ActiveOrient.database = name
end

#patch_record(rid) ⇒ Object Also known as: patch_document

Lazy Updating of the given Record.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rest/change.rb', line 62

def patch_record rid	    # :nodoc:   (used by Model#update )
  logger.progname = 'RestChange#PatchRecord'
  content = yield
  if content.is_a? Hash
    begin
      @res["/document/#{ActiveOrient.database}/#{rid}"].patch content.to_orient.to_json
    rescue Exception => e
      logger.error{e.message}
    end
  else
	  logger.error{"FAILED: The Block must provide an Hash with properties to be updated"}
  end
end

#update(record, attributes, version = 0) ⇒ Object

Convient update of the dataset by calling sql-patch

The argument record can be specified as ActiveOrient::Model-instance or as rid-string( #0:0 )

called from ModelRecord#update

if the update was successful, the updated ActiveOrient::Model-record is returned.


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rest/change.rb', line 25

def update record, attributes , version=0   # :nodoc: 
  r = if record.is_a?(String) && record.rid?
    ActiveOrient::Model.autoload record 
	else
 record
	end
  return(false) unless r.is_a?(ActiveOrient::Model)
  version = r.version if version.zero?
  result = patch_record(r.rid) do
    attributes.merge({'@version' => version, '@class' => r.class.ref_name })
  end
  # returns a new instance of ActiveOrient::Model and updates any reference on rid
  # if the patch is not successfull no string is returned and thus no record is fetched
 #   puts JSON.parse(result) if result.is_a?(String)
  ActiveOrient::Model.orientdb_class(name: r.class.ref_name, superclass: :find_ME ).new(JSON.parse(result))  if result.is_a?(String)
end

#update_records(o_class, set:, where: {}) ⇒ Object Also known as: update_documents

Example:

ORD.update_documents classname, set: {:symbol => 'TWR'}, where: {con_id: 340}

Replaces the symbol to TWR in each record where the con_id is 340

Both set and where take multiple attributes

Returns the JSON-Response.



54
55
56
57
# File 'lib/rest/change.rb', line 54

def update_records o_class, set:, where: {}
  url = "UPDATE #{classname(o_class)} SET #{generate_sql_list(set)} #{compose_where(where)}"
  response = @res[URI.encode("/command/#{ActiveOrient.database}/sql/" << url)].post ''
end