Class: OrientdbClient::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/orientdb_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, http_client:, client:) ⇒ Node

Returns a new instance of Node.



143
144
145
146
147
148
149
150
151
# File 'lib/orientdb_client.rb', line 143

def initialize(host:, port:, http_client:, client:)
  @host = host
  @port = port
  @http_client = http_client
  @client = client
  @connected = false
  @database = nil
  @debug = false
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



140
141
142
# File 'lib/orientdb_client.rb', line 140

def database
  @database
end

#debug=(value) ⇒ Object (writeonly)

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



141
142
143
# File 'lib/orientdb_client.rb', line 141

def debug=(value)
  @debug = value
end

Instance Method Details

#alter_property(class_name, property_name, field, value) ⇒ Object



212
213
214
# File 'lib/orientdb_client.rb', line 212

def alter_property(class_name, property_name, field, value)
  command("ALTER PROPERTY #{class_name}.#{property_name} #{field} #{value}")
end

#command(sql) ⇒ Object



227
228
229
230
# File 'lib/orientdb_client.rb', line 227

def command(sql)
  r = request(:post, "command/#{@database}/sql/#{CGI::escape(sql)}")
  parse_response(r)
end

#connect(database) ⇒ Object



153
154
155
156
157
158
# File 'lib/orientdb_client.rb', line 153

def connect(database)
  request(:get, "connect/#{database}")
  @connected = true
  @database = database
  true
end

#connected?Boolean

Returns:

  • (Boolean)


247
248
249
# File 'lib/orientdb_client.rb', line 247

def connected?
  @connected == true
end

#create_class(name, options) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/orientdb_client.rb', line 193

def create_class(name, options)
  sql = "CREATE CLASS #{name}"
  sql << " EXTENDS #{options[:extends]}" if options.key?(:extends)
  sql << " CLUSTER #{options[:cluster]}" if options.key?(:cluster)
  sql << ' ABSTRACT' if options.key?(:abstract)
  command(sql)
end

#create_database(name, storage, type, options) ⇒ Object



166
167
168
169
# File 'lib/orientdb_client.rb', line 166

def create_database(name, storage, type, options)
  r = request(:post, "database/#{name}/#{storage}/#{type}", options)
  parse_response(r)
end

#create_property(class_name, property_name, type, options) ⇒ Object



205
206
207
208
209
210
# File 'lib/orientdb_client.rb', line 205

def create_property(class_name, property_name, type, options)
  command("CREATE PROPERTY #{class_name}.#{property_name} #{type}")
  options.each do |k, v|
    alter_property(class_name, property_name, k, v)
  end
end

#delete_database(name, options) ⇒ Object



171
172
173
174
# File 'lib/orientdb_client.rb', line 171

def delete_database(name, options)
  r = request(:delete, "database/#{name}", options)
  parse_response(r)
end

#disconnectObject



160
161
162
163
164
# File 'lib/orientdb_client.rb', line 160

def disconnect
  request(:get, 'disconnect') rescue UnauthorizedError
  @connected = false
  true
end

#drop_class(name) ⇒ Object



201
202
203
# File 'lib/orientdb_client.rb', line 201

def drop_class(name)
  command("DROP CLASS #{name}")
end

#get_class(name) ⇒ Object



232
233
234
235
236
237
# File 'lib/orientdb_client.rb', line 232

def get_class(name)
  r = request(:get, "class/#{@database}/#{name}")
  parse_response(r)
rescue IllegalArgumentException
  raise NotFoundError
end

#get_database(name, options) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/orientdb_client.rb', line 176

def get_database(name, options)
  r = request(:get, "database/#{name}", options)
  r = parse_response(r)
rescue UnauthorizedError => e
  # Attempt to get not-found db, when connected, will return 401 error.
  if connected?
    raise NotFoundError.new("Database #{name} not found, or you are not authorized to access it.", 401)
  else
    raise e
  end
end

#has_class?(name) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
242
243
244
245
# File 'lib/orientdb_client.rb', line 239

def has_class?(name)
  if get_class(name)
    return true
  end
rescue NotFoundError
  return false
end

#list_databasesObject



188
189
190
191
# File 'lib/orientdb_client.rb', line 188

def list_databases
  r = request(:get, 'listDatabases')
  parse_response(r)['databases']
end

#query(sql, options) ⇒ Object



216
217
218
# File 'lib/orientdb_client.rb', line 216

def query(sql, options)
  parse_response(query_unparsed(sql, options))['result']
end

#query_unparsed(sql, options) ⇒ Object



220
221
222
223
224
225
# File 'lib/orientdb_client.rb', line 220

def query_unparsed(sql, options)
  limit = limit_string(options)
  request(:get, "query/#{@database}/sql/#{CGI::escape(sql)}#{limit}")
rescue NegativeArraySizeException
  raise NotFoundError
end