Class: Orientdb4r::Client

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/orientdb4r/client.rb

Direct Known Subclasses

BinClient, RestClient

Constant Summary collapse

DEFAULT_SERVER_VERSION =

Server version used if no concrete version identified.

'1.0.0--'
SERVER_VERSION_PATTERN =

# Regexp to validate format of providet version.

/^\d+\.\d+\.\d+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#blank?, #compare_versions, #random_string, #verify_and_sanitize_options, #verify_options

Constructor Details

#initializeClient

Constructor.



32
33
34
35
# File 'lib/orientdb4r/client.rb', line 32

def initialize
  @nodes = []
  @connected = false
end

Instance Attribute Details

#connection_libraryObject (readonly)

type of connection library [:restclient, :excon]



17
18
19
# File 'lib/orientdb4r/client.rb', line 17

def connection_library
  @connection_library
end

#databaseObject (readonly)

connection parameters



13
14
15
# File 'lib/orientdb4r/client.rb', line 13

def database
  @database
end

#lb_strategyObject (readonly)

object implementing a LB strategy



28
29
30
# File 'lib/orientdb4r/client.rb', line 28

def lb_strategy
  @lb_strategy
end

#load_balancingObject (readonly)

type of load balancing [:sequence, :round_robin]



19
20
21
# File 'lib/orientdb4r/client.rb', line 19

def load_balancing
  @load_balancing
end

#nodesObject (readonly)

nodes responsible for communication with a server



26
27
28
# File 'lib/orientdb4r/client.rb', line 26

def nodes
  @nodes
end

#passwordObject (readonly)

connection parameters



13
14
15
# File 'lib/orientdb4r/client.rb', line 13

def password
  @password
end

#proxyObject (readonly)

proxy for remote communication



21
22
23
# File 'lib/orientdb4r/client.rb', line 21

def proxy
  @proxy
end

#server_versionObject (readonly)

version loaded from server



15
16
17
# File 'lib/orientdb4r/client.rb', line 15

def server_version
  @server_version
end

#userObject (readonly)

connection parameters



13
14
15
# File 'lib/orientdb4r/client.rb', line 13

def user
  @user
end

Instance Method Details

#class_exists?(name) ⇒ Boolean

Checks existence of a given class.

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
# File 'lib/orientdb4r/client.rb', line 180

def class_exists?(name)
  rslt = true
  begin
    get_class name
  rescue OrientdbError
    rslt = false
  end
  rslt
end

#command(sql) ⇒ Object

Executes a command against the database.

Raises:

  • (NotImplementedError)


121
122
123
# File 'lib/orientdb4r/client.rb', line 121

def command(sql)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#connect(options) ⇒ Object

Connects client to the server.

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/orientdb4r/client.rb', line 42

def connect options
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#connected?Boolean

Gets flag whenever the client is connected or not.

Returns:

  • (Boolean)


56
57
58
# File 'lib/orientdb4r/client.rb', line 56

def connected?
  @connected
end

#create_class(name, options = {}) ⇒ Object

Creates a new class in the schema.

Raises:

  • (ArgumentError)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/orientdb4r/client.rb', line 130

def create_class(name, options={})
  raise ArgumentError, "class name is blank" if blank?(name)
  opt_pattern = { :extends => :optional , :cluster => :optional, :force => false, :properties => :optional }
  verify_options(options, opt_pattern)

  sql = "CREATE CLASS #{name}"
  sql << " EXTENDS #{options[:extends]}" if options.include? :extends
  sql << " CLUSTER #{options[:cluster]}" if options.include? :cluster

  drop_class name if options[:force]

  command sql

  # properties given?
  if options.include? :properties
    props = options[:properties]
    raise ArgumentError, 'properties have to be an array' unless props.is_a? Array

    props.each do |prop|
      raise ArgumentError, 'property definition has to be a hash' unless prop.is_a? Hash
      prop_name = prop.delete :property
      prop_type = prop.delete :type
      create_property(name, prop_name, prop_type, prop)
    end
  end

  if block_given?
    proxy = Orientdb4r::Utils::Proxy.new(self, name)
    def proxy.property(property, type, options={})
      self.target.send :create_property, self.context, property, type, options
    end
    def proxy.link(property, type, linked_class, options={})
      raise ArgumentError, "type has to be a linked-type, given=#{type}" unless type.to_s.start_with? 'link'
      options[:linked_class] = linked_class
      self.target.send :create_property, self.context, property, type, options
    end
    yield proxy
  end
end

#create_database(options) ⇒ Object

Creates a new database. You can provide an additional authentication to the server with ‘database.create’ resource or the current one will be used.

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/orientdb4r/client.rb', line 76

def create_database(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#create_document(doc) ⇒ Object

Create a new document. Returns the Record-id assigned.

Raises:

  • (NotImplementedError)


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

def create_document(doc)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#create_property(clazz, property, type, options = {}) ⇒ Object

Creates a new property in the schema. You need to create the class before.

Raises:

  • (ArgumentError)


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/orientdb4r/client.rb', line 215

def create_property(clazz, property, type, options={})
  raise ArgumentError, "class name is blank" if blank?(clazz)
  raise ArgumentError, "property name is blank" if blank?(property)
  opt_pattern = {
    :mandatory => :optional , :notnull => :optional, :min => :optional, :max => :optional,
    :regexp =>  :optional, :custom => :optional, :linked_class => :optional
  }
  verify_options(options, opt_pattern)

  cmd = "CREATE PROPERTY #{clazz}.#{property} #{type.to_s}"
  # link?
  if [:link, :linklist, :linkset, :linkmap].include? type.to_s.downcase.to_sym
    raise ArgumentError, "defined linked-type, but not linked-class" unless options.include? :linked_class
    cmd << " #{options[:linked_class]}"
  end
  command cmd

  # ALTER PROPERTY ...
  options.delete :linked_class # it's not option for ALTER
  unless options.empty?
    options.each do |k,v|
      command "ALTER PROPERTY #{clazz}.#{property} #{k.to_s.upcase} #{v}"
    end
  end
end

#database_exists?(options) ⇒ Boolean

Checks existence of a given database. Client has not to be connected to see databases suitable to connect.

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
# File 'lib/orientdb4r/client.rb', line 92

def database_exists?(options)
  rslt = true
  begin
    get_database options
  rescue OrientdbError
    rslt = false
  end
  rslt
end

#delete_database(options) ⇒ Object

Drops a database. Requires additional authentication to the server.

Raises:

  • (NotImplementedError)


106
107
108
# File 'lib/orientdb4r/client.rb', line 106

def delete_database(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#delete_document(rid) ⇒ Object

Deletes an existing document.

Raises:

  • (NotImplementedError)


268
269
270
# File 'lib/orientdb4r/client.rb', line 268

def delete_document(rid)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#disconnectObject

Disconnects client from the server.

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/orientdb4r/client.rb', line 49

def disconnect
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#drop_class(name, options = {}) ⇒ Object

Removes a class from the schema.

Raises:

  • (ArgumentError)


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/orientdb4r/client.rb', line 193

def drop_class(name, options={})
  raise ArgumentError, "class name is blank" if blank?(name)

  # :mode=>:strict forbids to drop a class that is a super class for other one
  opt_pattern = { :mode => :nil }
  verify_options(options, opt_pattern)
  if :strict == options[:mode]
    response = call_server(:method => :get, :uri => "connect/#{@database}") # TODO there cannot be REST
    connect_info = process_response response
    children = connect_info['classes'].select { |i| i['superClass'] == name }
    unless children.empty?
      raise OrientdbError, "class is super-class, cannot be deleted, name=#{name}"
    end
  end

  command "DROP CLASS #{name}"
end

#get_class(name) ⇒ Object

Gets informations about requested class.

Raises:

  • (NotImplementedError)


173
174
175
# File 'lib/orientdb4r/client.rb', line 173

def get_class(name)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#get_database(options) ⇒ Object

Retrieves all the information about a database. Client has not to be connected to see databases suitable to connect.

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/orientdb4r/client.rb', line 84

def get_database(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#get_document(rid) ⇒ Object

Retrieves a document by given ID.

Raises:

  • (NotImplementedError)


254
255
256
# File 'lib/orientdb4r/client.rb', line 254

def get_document(rid)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#query(sql, options) ⇒ Object

Executes a query against the database.

Raises:

  • (NotImplementedError)


114
115
116
# File 'lib/orientdb4r/client.rb', line 114

def query(sql, options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#server(options = {}) ⇒ Object

Retrieve information about the connected OrientDB Server. Enables additional authentication to the server with an account that can access the ‘server.info’ resource.

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/orientdb4r/client.rb', line 65

def server(options={})
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#update_document(doc) ⇒ Object

Updates an existing document.

Raises:

  • (NotImplementedError)


261
262
263
# File 'lib/orientdb4r/client.rb', line 261

def update_document(doc)
  raise NotImplementedError, 'this should be overridden by concrete client'
end