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

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
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rest/change.rb', line 108

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. internal using while updating records


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rest/change.rb', line 82

def patch_record rid	    # :nodoc:   (used by #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 RestClient::InternalServerError => e
	sentence=  JSON.parse( e.response)['errors'].last['content']
 logger.error{sentence}
 logger.error{ e.backtrace.map {|l| "  #{l}\n"}.join  }
 logger.error{e.message.to_s}
    end
  else
	  logger.error{"PATCH 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 data are returned as Hash.

First the cached object is retrieved
Its modified by the parameters provided
and then patched

better would be: update the cached object and patch that.

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rest/change.rb', line 31

def update record, attributes , version=0   
  r = ActiveOrient::Model.autoload_object record.rid 
  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
  # It's assumed, that the version 
  # if the patch is not successfull no string is returned and thus no record is fetched
  # JSON.parse(result) if result.is_a?(String)
  if result.is_a?(String)
     JSON.parse(result) # return value
  else
    logger.error{ "REST::Update was not successfull" }
     nil   # returnvalue
  end
end

#update_records(o_class, set: {}, where: {}, remove: nil) ⇒ Object

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.

# todo: clear the rid-cache


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rest/change.rb', line 64

def update_records o_class, set:{}, where: {}, remove: nil
	logger.progname = 'RestChange#UpdateRecords'
	count =  execute do 
		if set.present?
			"UPDATE #{classname(o_class)} SET #{generate_sql_list(set)} #{compose_where(where)}"
		elsif remove.present?
			"UPDATE #{classname(o_class)} remove #{remove} #{compose_where(where)}"
		end 
	end &.first
rescue Exception => e
	logger.error{e.message}
	nil


end