Module: OrientDB::DocumentDatabaseMixin

Included in:
DocumentDatabase, DocumentDatabasePooled
Defined in:
lib/orientdb/database.rb

Instance Method Summary collapse

Instance Method Details

#all(sql_query = nil) ⇒ Object



24
25
26
27
# File 'lib/orientdb/database.rb', line 24

def all(sql_query = nil)
  sql_query = prepare_sql_query sql_query
  query sql_query
end

#all_in_class(klass_name) ⇒ Object



95
96
97
# File 'lib/orientdb/database.rb', line 95

def all_in_class(klass_name)
  browse_class(klass_name.to_s).map
end

#all_in_cluster(cluster_name) ⇒ Object



99
100
101
# File 'lib/orientdb/database.rb', line 99

def all_in_cluster(cluster_name)
  browse_cluster(cluster_name.to_s).map
end

#create_class(klass_name, fields = {}) ⇒ Object



63
64
65
# File 'lib/orientdb/database.rb', line 63

def create_class(klass_name, fields = {})
  OrientDB::OClassImpl.create self, klass_name.to_s, fields
end

#drop_class(klass_name) ⇒ Object



79
80
81
# File 'lib/orientdb/database.rb', line 79

def drop_class(klass_name)
  schema.drop_class(klass_name) if schema.exists_class(klass_name)
end

#find_by_rid(rid) ⇒ Object



34
35
36
# File 'lib/orientdb/database.rb', line 34

def find_by_rid(rid)
  first "SELECT FROM #{rid}"
end

#find_by_rids(*rids) ⇒ Object



38
39
40
# File 'lib/orientdb/database.rb', line 38

def find_by_rids(*rids)
  all "SELECT FROM [#{rids.map{|x| x.to_s}.join(', ')}]"
end

#first(sql_query = nil) ⇒ Object



29
30
31
32
# File 'lib/orientdb/database.rb', line 29

def first(sql_query = nil)
  sql_query = prepare_sql_query(sql_query).setLimit(1)
  query(sql_query).first
end

#get_class(klass_name) ⇒ Object



59
60
61
# File 'lib/orientdb/database.rb', line 59

def get_class(klass_name)
  schema.get_class klass_name.to_s
end

#get_or_create_class(klass_name, fields = {}) ⇒ Object



67
68
69
# File 'lib/orientdb/database.rb', line 67

def get_or_create_class(klass_name, fields = {})
  get_class(klass_name) || create_class(klass_name, fields)
end

#migrate_class(klass_name, fields = {}) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/orientdb/database.rb', line 71

def migrate_class(klass_name, fields = {})
  klass = get_or_create_class klass_name
  fields.each do |name, options|
    type = options.delete :type
    klass.add name, type, options
  end
end

#prepare_sql_command(command) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/orientdb/database.rb', line 11

def prepare_sql_command(command)
  return if command.nil?
  return command.to_sql_command if command.respond_to?(:to_sql_command)
  case command
    when OrientDB::SQLCommand
      command
    when String
      OrientDB::SQLCommand.new command
    else
      raise "Unknown command type"
  end
end

#prepare_sql_query(query) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/orientdb/database.rb', line 42

def prepare_sql_query(query)
  return if query.nil?
  return query.to_sql_query if query.respond_to?(:to_sql_query)
  case query
    when OrientDB::SQLSynchQuery
      query
    when String
      OrientDB::SQLSynchQuery.new(query)
    else
      raise "Unknown query type"
  end
end

#quote(value) ⇒ Object



103
104
105
# File 'lib/orientdb/database.rb', line 103

def quote(value)
  SQL::Query.quote value
end

#recreate_class(klass_name, fields = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/orientdb/database.rb', line 83

def recreate_class(klass_name, fields = {})
  #run_command("DELETE FROM '#{klass_name}'") rescue nil
  root_class = get_class(klass_name)
  schema.classes.each do |possible_sub_class|
    if possible_sub_class.is_sub_class_of(root_class) && root_class.name != possible_sub_class.name
      drop_class(possible_sub_class.name)
    end
  end
  drop_class(klass_name) rescue nil
  create_class klass_name, fields
end

#run_command(sql_command = nil) ⇒ Object Also known as: cmd



4
5
6
7
# File 'lib/orientdb/database.rb', line 4

def run_command(sql_command = nil)
  sql_command = prepare_sql_command sql_command
  command(sql_command).execute(true)
end

#schemaObject



55
56
57
# File 'lib/orientdb/database.rb', line 55

def schema
  .schema
end