orientdb 0.0.12-jruby → 0.0.14-jruby

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.12
1
+ 0.0.14
data/bin/orientdb_console CHANGED
@@ -11,7 +11,7 @@ if ARGV.include?('test:db')
11
11
  puts ">> TEST_DB PATH : #{TEST_DB_PATH}"
12
12
 
13
13
  require 'fileutils'
14
- FileUtils.remove_dir TEST_DB_PATH
14
+ FileUtils.remove_dir "#{TEMP_DIR}/databases" rescue nil
15
15
  FileUtils.mkdir_p TEST_DB_PATH
16
16
  DB = OrientDB::DocumentDatabase.new("local:#{TEST_DB_PATH}/test").create
17
17
  end
data/lib/orientdb.rb CHANGED
@@ -25,6 +25,5 @@ require 'orientdb/storage'
25
25
  require 'orientdb/database'
26
26
  require 'orientdb/record'
27
27
  require 'orientdb/document'
28
- require 'orientdb/sql_query'
29
28
  require 'orientdb/sql'
30
29
  require 'orientdb/oclass'
@@ -6,13 +6,14 @@ module OrientDB
6
6
  command(sql_command).execute(true)
7
7
  end
8
8
 
9
- def prepare_sql_command(sql_command)
10
- return if sql_command.nil?
11
- case sql_command
9
+ def prepare_sql_command(command)
10
+ return if command.nil?
11
+ return command.to_sql_command if command.respond_to?(:to_sql_command)
12
+ case command
12
13
  when OrientDB::SQLCommand
13
- sql_command
14
+ command
14
15
  when String
15
- OrientDB::SQLCommand.new sql_command
16
+ OrientDB::SQLCommand.new command
16
17
  else
17
18
  raise "Unknown command type"
18
19
  end
@@ -28,15 +29,22 @@ module OrientDB
28
29
  query(sql_query).first
29
30
  end
30
31
 
31
- def prepare_sql_query(sql_query)
32
- return if sql_query.nil?
33
- case sql_query
32
+ def find_by_rid(rid)
33
+ first "SELECT FROM #{rid}"
34
+ end
35
+
36
+ def find_by_rids(*rids)
37
+ all "SELECT FROM [#{rids.map{|x| x.to_s}.join(', ')}]"
38
+ end
39
+
40
+ def prepare_sql_query(query)
41
+ return if query.nil?
42
+ return query.to_sql_query if query.respond_to?(:to_sql_query)
43
+ case query
34
44
  when OrientDB::SQLSynchQuery
35
- sql_query
36
- when OrientDB::SQL::Query
37
- sql_query.to_sql_synch_query
45
+ query
38
46
  when String
39
- OrientDB::SQLSynchQuery.new(sql_query)
47
+ OrientDB::SQLSynchQuery.new(query)
40
48
  else
41
49
  raise "Unknown query type"
42
50
  end
@@ -62,7 +62,7 @@ module OrientDB
62
62
  end
63
63
 
64
64
  def inspect
65
- props = properties.map { |x| "#{x.name}=#{x.type.to_s.downcase}#{x.is_indexed? ? '(idx)' : ''}" }.join(' ')
65
+ props = properties.map { |x| "#{x.name}=#{x.type.name}#{x.is_indexed? ? '(idx)' : ''}" }.join(' ')
66
66
  "#<OrientDB::OClass:" + name +
67
67
  (getSuperClass ? ' super=' + getSuperClass.name : '') +
68
68
  (props.empty? ? '' : ' ' + props) +
@@ -83,14 +83,12 @@ module OrientDB
83
83
  klass = db.get_class name
84
84
  else
85
85
  if use_cluster
86
- cluster = db.storage.get_cluster use_cluster
87
- klass = db.schema.create_class name, cluster
86
+ klass = db.schema.create_class name, use_cluster
88
87
  elsif add_cluster && !db.storage.cluster_names.include?(name.downcase)
89
88
  cluster = db.storage.add_cluster name.downcase, STORAGE_TYPES[:physical]
90
89
  klass = db.schema.create_class name, cluster
91
90
  else
92
91
  klass = db.schema.create_class name
93
- cluster = db.storage.get_cluster klass.cluster_ids.first rescue nil
94
92
  end
95
93
  end
96
94
 
@@ -1,10 +1,6 @@
1
1
  module OrientDB
2
2
 
3
3
  class RecordList
4
- def jruby_value
5
- map
6
- end
7
-
8
4
  def inspect
9
5
  "#<OrientDB::RecordList:#{toString}>"
10
6
  end
data/lib/orientdb/sql.rb CHANGED
@@ -1,267 +1,18 @@
1
- require 'orientdb/sql_ext'
1
+ module OrientDB::SQL
2
+ class SQLSynchQuery
2
3
 
3
- module OrientDB
4
- module SQL
5
-
6
- class ConditionExpression
7
-
8
- attr_reader :conditions
9
-
10
- def initialize(type)
11
- @type = type
12
- @conditions = []
13
- end
14
-
15
- def type
16
- @type.to_s.upcase.gsub('_', ' ')
17
- end
18
-
19
- def add(*conds)
20
- conds.each do |cond|
21
- case cond
22
- when ConditionExpression
23
- conditions << cond.to_s
24
- when Hash
25
- cond.each { |k, v| conditions << "#{k} = #{Query.quote(v)}" }
26
- when Array
27
- cond.each { |x| conditions << x.to_s }
28
- else
29
- conditions << cond.to_s
30
- end
31
- end
32
- end
33
-
34
- def clear
35
- @conditions = []
36
- end
37
-
38
- def conditions_str
39
- conditions.join(' AND ')
40
- end
41
-
42
- def parens_conditions_str
43
- conditions.size > 1 ? "(#{conditions_str})" : conditions_str
44
- end
45
-
46
- def to_s
47
- "#{type} #{parens_conditions_str} "
48
- end
4
+ def inspect
5
+ %{#<OrientDB::SQLSynchQuery:#{name} text="#{text}">}
49
6
  end
50
7
 
51
- class Query
52
-
53
- attr_reader :projections, :targets, :conditions, :order, :limit, :lower_range, :upper_range, :plan
54
-
55
- def initialize
56
- @projections = []
57
- @targets = []
58
- @conditions = []
59
- @order = []
60
- @limit = nil
61
- @lower_range = nil
62
- @upper_range = nil
63
- @plan = nil
64
- end
65
-
66
- def select(*args)
67
- args.each do |arg|
68
- case arg
69
- when String, Symbol, Integer
70
- arg = select_single_string(arg)
71
- @projections << arg
72
- when Hash
73
- arg.each { |k, v| @projections << "#{k} AS #{v}" }
74
- when Array
75
- if arg.size == 2
76
- @projections << "#{arg.first} AS #{arg.last}"
77
- else
78
- arg.each { |x| @projections << select_single_string(x) }
79
- end
80
- end
81
- end
82
- self
83
- end
84
-
85
- alias :columns :select
86
-
87
- def select!(*args)
88
- @projections = []
89
- select *args
90
- end
91
-
92
- def from(*args)
93
- args.each { |x| @targets << x.to_s }
94
- self
95
- end
96
-
97
- def from!(*args)
98
- @targets = []
99
- from *args
100
- end
101
-
102
- def where(*args)
103
- @conditions << ConditionExpression.new(:and) if @conditions.empty?
104
- @conditions.last.add *args
105
- self
106
- end
107
-
108
- def where!(*args)
109
- @conditions = []
110
- where *args
111
- end
112
-
113
- def and(*args)
114
- @conditions << ConditionExpression.new(:and)
115
- @conditions.last.add *args
116
- self
117
- end
118
-
119
- def or(*args)
120
- @conditions << ConditionExpression.new(:or)
121
- @conditions.last.add *args
122
- self
123
- end
124
-
125
- def and_not(*args)
126
- @conditions << ConditionExpression.new(:and_not)
127
- @conditions.last.add *args
128
- self
129
- end
130
-
131
- def or_not(*args)
132
- @conditions << ConditionExpression.new(:or_not)
133
- @conditions.last.add *args
134
- self
135
- end
8
+ alias :to_s :inspect
136
9
 
137
- def order(*args)
138
- args.each do |arg|
139
- case arg
140
- when Hash
141
- arg.each { |k, v| @order << "#{k} #{order_direction_for(v)}" }
142
- when Array
143
- case arg.size
144
- when 2
145
- @order << "#{arg[0]} #{order_direction_for(arg[1])}"
146
- else
147
- arg.each { |x| @order << x.to_s }
148
- end
149
- else
150
- @order << arg.to_s
151
- end
152
- end
153
- self
154
- end
155
-
156
- def order!(*args)
157
- @order = []
158
- order *args
159
- end
160
-
161
- def limit(max_records)
162
- @limit = max_records.to_s.to_i
163
- self
164
- end
165
-
166
- alias :limit! :limit
167
-
168
- def range(lower_rid, upper_rid = nil)
169
- @lower_range = lower_rid.to_s
170
- @upper_range = upper_rid ? upper_rid.to_s : nil
171
- self
172
- end
173
-
174
- alias :range! :range
175
-
176
- def to_s
177
- (select_sql + target_sql + conditions_sql + order_sql + limit_sql + range_sql).strip
178
- end
179
-
180
- def to_sql_synch_query
181
- OrientDB::SQLSynchQuery.new to_s
182
- end
183
-
184
- def self.quote(value)
185
- case value
186
- when Numeric, Symbol
187
- value.to_s
188
- when String
189
- quote_string(value)
190
- when Array
191
- "[" + value.map { |x| quote(x) }.join(", ") + "]"
192
- when Regexp
193
- quote_regexp(value)
194
- end
195
- end
196
-
197
- def self.quote_string(str)
198
- str = str.dup
199
- return str if str[0, 1] == "'" && str[-1, 1] == "'"
200
- last_pos = 0
201
- while (pos = str.index("'", last_pos))
202
- str.insert(pos, "\\") if pos > 0 && str[pos - 1, 1] != "\\"
203
- last_pos = pos + 1
204
- end
205
- "'#{str}'"
206
- end
207
-
208
- def self.quote_regexp(regexp)
209
- regexp = regexp.inspect
210
- left_index = regexp.index('/') + 1
211
- right_index = regexp.rindex('/') - 1
212
- str = regexp[left_index..right_index]
213
- "'#{str}'"
214
- end
215
-
216
- private
217
-
218
- def select_sql
219
- str = @projections.empty? ? '' : @projections.map { |x| x.to_s }.join(', ') + ' '
220
- "SELECT #{str}"
221
- end
222
-
223
- def target_sql
224
- case @targets.size
225
- when 0
226
- "FROM "
227
- when 1
228
- "FROM #{@targets.first} "
229
- else
230
- "FROM [#{@targets.map { |x| x.to_s }.join(", ")}] "
231
- end
232
- end
233
-
234
- def conditions_sql
235
- case @conditions.size
236
- when 0
237
- ''
238
- when 1
239
- "WHERE #{@conditions.first.conditions_str} "
240
- else
241
- "WHERE #{@conditions.first.parens_conditions_str} #{@conditions[1..-1].map { |x| x.to_s }.join('')}"
242
- end
243
- end
244
-
245
- def order_sql
246
- @order.empty? ? '' : "ORDER BY #{@order.map { |x| x.to_s }.join(', ')} "
247
- end
248
-
249
- def limit_sql
250
- @limit.nil? ? '' : "LIMIT #{@limit} "
251
- end
252
-
253
- def range_sql
254
- @lower_range.nil? ? '' : "RANGE #{@lower_range}#{@upper_range ? ", #{@upper_range}" : ''} "
255
- end
256
-
257
- def select_single_string(arg)
258
- arg.to_s.split('___').join(' AS ').split('__').join('.')
259
- end
260
-
261
- def order_direction_for(value)
262
- value.to_s.strip.downcase == 'desc' ? 'DESC' : 'ASC'
263
- end
264
-
265
- end
266
10
  end
267
- end
11
+ end
12
+
13
+ require 'orientdb/sql/common'
14
+ require 'orientdb/sql/ext'
15
+ require 'orientdb/sql/query'
16
+ require 'orientdb/sql/insert'
17
+ require 'orientdb/sql/update'
18
+ require 'orientdb/sql/delete'
@@ -0,0 +1,217 @@
1
+ module OrientDB::SQL
2
+ module UtilsMixin
3
+
4
+ def select_single_string(arg)
5
+ arg.to_s.split('___').join(' AS ').split('__').join('.')
6
+ end
7
+
8
+ def field_name(name)
9
+ name.to_s.split('__').join('.')
10
+ end
11
+
12
+ def quote(value)
13
+ case value
14
+ when Numeric, Symbol
15
+ value.to_s
16
+ when String
17
+ quote_string(value)
18
+ when Array
19
+ "[" + value.map { |x| quote(x) }.join(", ") + "]"
20
+ when Regexp
21
+ quote_regexp(value)
22
+ end
23
+ end
24
+
25
+ def quote_string(str)
26
+ str = str.dup
27
+ return str if str[0, 1] == "'" && str[-1, 1] == "'"
28
+ last_pos = 0
29
+ while (pos = str.index("'", last_pos))
30
+ str.insert(pos, "\\") if pos > 0 && str[pos - 1, 1] != "\\"
31
+ last_pos = pos + 1
32
+ end
33
+ "'#{str}'"
34
+ end
35
+
36
+ def quote_regexp(regexp)
37
+ regexp = regexp.inspect
38
+ left_index = regexp.index('/') + 1
39
+ right_index = regexp.rindex('/') - 1
40
+ str = regexp[left_index..right_index]
41
+ "'#{str}'"
42
+ end
43
+
44
+ end
45
+
46
+ module ClassClusterParametersMixin
47
+
48
+ def oclass(new_oclass)
49
+ @oclass = new_oclass.to_s
50
+ self
51
+ end
52
+
53
+ alias :oclass! :oclass
54
+
55
+ def cluster(new_cluster)
56
+ @cluster = new_cluster.to_s
57
+ self
58
+ end
59
+
60
+ alias :cluster! :cluster
61
+
62
+ private
63
+
64
+ def target_sql(command)
65
+ command = command.to_s.upcase.gsub('_', ' ')
66
+ if @oclass
67
+ "#{command} #{@oclass} "
68
+ elsif @cluster
69
+ "#{command} cluster:#{@cluster} "
70
+ else
71
+ raise "Missing oclass or cluster"
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ module FieldsValuesParametersMixin
78
+
79
+ def fields(*args)
80
+ args.each do |arg|
81
+ case arg
82
+ when String, Symbol, Integer
83
+ @fields << field_name(arg)
84
+ when Hash
85
+ arg.each { |k, v| @fields << field_name(k); @values << quote(v) }
86
+ when Array
87
+ arg.each { |x| @fields << field_name(x) }
88
+ end
89
+ end
90
+ self
91
+ end
92
+
93
+ def fields!(*args)
94
+ @fields = []
95
+ @values = []
96
+ fields *args
97
+ end
98
+
99
+ def values(*args)
100
+ args.each do |arg|
101
+ case arg
102
+ when String, Symbol, Integer
103
+ arg = quote(arg)
104
+ @values << arg
105
+ when Hash
106
+ arg.each { |k, v| @fields << field_name(k); @values << quote(v) }
107
+ when Array
108
+ arg.each { |x| @values << quote(x) }
109
+ end
110
+ end
111
+ self
112
+ end
113
+
114
+ def values!(*args)
115
+ @fields = []
116
+ @values = []
117
+ values *args
118
+ end
119
+
120
+ end
121
+
122
+ module ConditionsParametersMixin
123
+ def where(*args)
124
+ @conditions << ConditionExpression.new(:and) if @conditions.empty?
125
+ @conditions.last.add *args
126
+ self
127
+ end
128
+
129
+ def where!(*args)
130
+ @conditions = []
131
+ where *args
132
+ end
133
+
134
+ def and(*args)
135
+ @conditions << ConditionExpression.new(:and)
136
+ @conditions.last.add *args
137
+ self
138
+ end
139
+
140
+ def or(*args)
141
+ @conditions << ConditionExpression.new(:or)
142
+ @conditions.last.add *args
143
+ self
144
+ end
145
+
146
+ def and_not(*args)
147
+ @conditions << ConditionExpression.new(:and_not)
148
+ @conditions.last.add *args
149
+ self
150
+ end
151
+
152
+ def or_not(*args)
153
+ @conditions << ConditionExpression.new(:or_not)
154
+ @conditions.last.add *args
155
+ self
156
+ end
157
+
158
+ private
159
+
160
+ def conditions_sql
161
+ case @conditions.size
162
+ when 0
163
+ ''
164
+ when 1
165
+ "WHERE #{@conditions.first.conditions_str} "
166
+ else
167
+ "WHERE #{@conditions.first.parens_conditions_str} #{@conditions[1..-1].map { |x| x.to_s }.join('')}"
168
+ end
169
+ end
170
+
171
+ end
172
+
173
+ class ConditionExpression
174
+
175
+ attr_reader :conditions
176
+
177
+ def initialize(type)
178
+ @type = type
179
+ @conditions = []
180
+ end
181
+
182
+ def type
183
+ @type.to_s.upcase.gsub('_', ' ')
184
+ end
185
+
186
+ def add(*conds)
187
+ conds.each do |cond|
188
+ case cond
189
+ when ConditionExpression
190
+ conditions << cond.to_s
191
+ when Hash
192
+ cond.each { |k, v| conditions << "#{k} = #{Query.quote(v)}" }
193
+ when Array
194
+ cond.each { |x| conditions << x.to_s }
195
+ else
196
+ conditions << cond.to_s
197
+ end
198
+ end
199
+ end
200
+
201
+ def clear
202
+ @conditions = []
203
+ end
204
+
205
+ def conditions_str
206
+ conditions.join(' AND ')
207
+ end
208
+
209
+ def parens_conditions_str
210
+ conditions.size > 1 ? "(#{conditions_str})" : conditions_str
211
+ end
212
+
213
+ def to_s
214
+ "#{type} #{parens_conditions_str} "
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,23 @@
1
+ module OrientDB::SQL
2
+ class Delete
3
+
4
+ include OrientDB::SQL::UtilsMixin
5
+ include OrientDB::SQL::ClassClusterParametersMixin
6
+ include OrientDB::SQL::ConditionsParametersMixin
7
+
8
+ def initialize
9
+ @oclass = nil
10
+ @cluster = nil
11
+ @conditions = []
12
+ end
13
+
14
+ def to_s
15
+ (target_sql(:delete_from) + conditions_sql).strip
16
+ end
17
+
18
+ def to_sql_command
19
+ OrientDB::SQLCommand.new to_s
20
+ end
21
+
22
+ end
23
+ end
@@ -59,32 +59,34 @@ module OrientDB
59
59
  end
60
60
 
61
61
  module ConditionalExtension
62
+ include OrientDB::SQL::UtilsMixin
63
+
62
64
  def like(value)
63
- "#{to_s} LIKE #{Query.quote(value)}"
65
+ "#{to_s} LIKE #{quote(value)}"
64
66
  end
65
67
 
66
68
  def eq(value)
67
- "#{to_s} = #{Query.quote(value)}"
69
+ "#{to_s} = #{quote(value)}"
68
70
  end
69
71
 
70
72
  def lt(value)
71
- "#{to_s} < #{Query.quote(value)}"
73
+ "#{to_s} < #{quote(value)}"
72
74
  end
73
75
 
74
76
  def lte(value)
75
- "#{to_s} <= #{Query.quote(value)}"
77
+ "#{to_s} <= #{quote(value)}"
76
78
  end
77
79
 
78
80
  def gt(value)
79
- "#{to_s} > #{Query.quote(value)}"
81
+ "#{to_s} > #{quote(value)}"
80
82
  end
81
83
 
82
84
  def gte(value)
83
- "#{to_s} >= #{Query.quote(value)}"
85
+ "#{to_s} >= #{quote(value)}"
84
86
  end
85
87
 
86
88
  def ne(value)
87
- "#{to_s} <> #{Query.quote(value)}"
89
+ "#{to_s} <> #{quote(value)}"
88
90
  end
89
91
 
90
92
  def is_null
@@ -96,35 +98,37 @@ module OrientDB
96
98
  end
97
99
 
98
100
  def in(*values)
99
- "#{to_s} IN #{Query.quote(values)}"
101
+ "#{to_s} IN #{quote(values)}"
100
102
  end
101
103
 
102
104
  def contains(field, value)
103
- "#{to_s} contains (#{field} = #{Query.quote(value)})"
105
+ "#{to_s} contains (#{field} = #{quote(value)})"
104
106
  end
105
107
 
106
108
  def contains_all(field, value)
107
- "#{to_s} containsAll (#{field} = #{Query.quote(value)})"
109
+ "#{to_s} containsAll (#{field} = #{quote(value)})"
108
110
  end
109
111
 
110
112
  def contains_key(value)
111
- "#{to_s} containsKey #{Query.quote(value)}"
113
+ "#{to_s} containsKey #{quote(value)}"
112
114
  end
113
115
 
114
116
  def contains_value(value)
115
- "#{to_s} containsValue #{Query.quote(value)}"
117
+ "#{to_s} containsValue #{quote(value)}"
116
118
  end
117
119
 
118
120
  def contains_text(value)
119
- "#{to_s} containsText #{Query.quote(value)}"
121
+ "#{to_s} containsText #{quote(value)}"
120
122
  end
121
123
 
122
124
  def matches(value)
123
- "#{to_s} matches #{Query.quote(value)}"
125
+ "#{to_s} matches #{quote(value)}"
124
126
  end
125
127
  end
126
128
 
127
129
  module FieldOperatorExtension
130
+ include OrientDB::SQL::UtilsMixin
131
+
128
132
  # Avoided overriding the native method
129
133
  def odb_length
130
134
  "#{to_s}.length()"
@@ -145,29 +149,29 @@ module OrientDB
145
149
 
146
150
  # Avoided overriding the native method
147
151
  def odb_left(length)
148
- "#{to_s}.left(#{Query.quote(length)})"
152
+ "#{to_s}.left(#{quote(length)})"
149
153
  end
150
154
 
151
155
  # Avoided overriding the native method
152
156
  def odb_right(length)
153
- "#{to_s}.right(#{Query.quote(length)})"
157
+ "#{to_s}.right(#{quote(length)})"
154
158
  end
155
159
 
156
160
  def sub_string(start, length = nil)
157
- "#{to_s}.subString(#{Query.quote(start)}#{length ? ", #{Query.quote(length)}" : ''})"
161
+ "#{to_s}.subString(#{quote(start)}#{length ? ", #{quote(length)}" : ''})"
158
162
  end
159
163
 
160
164
  def char_at(pos)
161
- "#{to_s}.charAt(#{Query.quote(pos)})"
165
+ "#{to_s}.charAt(#{quote(pos)})"
162
166
  end
163
167
 
164
168
  def index_of(string, start = nil)
165
- "#{to_s}.indexOf(#{Query.quote(string)}#{start ? ", #{Query.quote(start)}" : ''})"
169
+ "#{to_s}.indexOf(#{quote(string)}#{start ? ", #{quote(start)}" : ''})"
166
170
  end
167
171
 
168
172
  # Avoided overriding the native method
169
173
  def odb_format(frmt)
170
- "#{to_s}.format(#{Query.quote(frmt)})"
174
+ "#{to_s}.format(#{quote(frmt)})"
171
175
  end
172
176
 
173
177
  # Avoided overriding the native method
@@ -201,6 +205,7 @@ module OrientDB
201
205
  end
202
206
 
203
207
  module BundledFunctionExtension
208
+ include OrientDB::SQL::UtilsMixin
204
209
 
205
210
  # Avoided overriding the native method
206
211
  def odb_count
@@ -233,7 +238,7 @@ module OrientDB
233
238
 
234
239
  # Avoided overriding the native method
235
240
  def odb_format_str(*args)
236
- "format('#{to_s}', #{args.map{|x| Query.quote(x)}.join(', ')})"
241
+ "format('#{to_s}', #{args.map{|x| quote(x)}.join(', ')})"
237
242
  end
238
243
  end
239
244
  end
@@ -0,0 +1,37 @@
1
+ module OrientDB::SQL
2
+ class Insert
3
+
4
+ include OrientDB::SQL::UtilsMixin
5
+ include OrientDB::SQL::ClassClusterParametersMixin
6
+ include OrientDB::SQL::FieldsValuesParametersMixin
7
+
8
+ def initialize
9
+ @oclass = nil
10
+ @cluster = nil
11
+ @fields = []
12
+ @values = []
13
+ end
14
+
15
+ def to_s
16
+ (target_sql(:insert_into) + fields_sql + values_sql).strip
17
+ end
18
+
19
+ def to_sql_command
20
+ OrientDB::SQLCommand.new to_s
21
+ end
22
+
23
+ private
24
+
25
+ def fields_sql
26
+ raise "Missing fields" if @fields.empty?
27
+ "(#{@fields.join(', ')}) "
28
+ end
29
+
30
+ def values_sql
31
+ raise "Missing values" if @values.empty?
32
+ raise "Unbalanced fields & values" unless @values.size == @fields.size
33
+ "VALUES (#{@values.join(', ')}) "
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,138 @@
1
+ module OrientDB::SQL
2
+ class Query
3
+ include OrientDB::SQL::UtilsMixin
4
+ include OrientDB::SQL::ConditionsParametersMixin
5
+ extend OrientDB::SQL::UtilsMixin
6
+
7
+ attr_reader :projections, :targets, :conditions, :order, :limit, :lower_range, :upper_range, :plan
8
+
9
+ def initialize
10
+ @projections = []
11
+ @targets = []
12
+ @conditions = []
13
+ @order = []
14
+ @limit = nil
15
+ @lower_range = nil
16
+ @upper_range = nil
17
+ @plan = nil
18
+ end
19
+
20
+ def select(*args)
21
+ args.each do |arg|
22
+ case arg
23
+ when String, Symbol, Integer
24
+ arg = select_single_string(arg)
25
+ @projections << arg
26
+ when Hash
27
+ arg.each { |k, v| @projections << "#{k} AS #{v}" }
28
+ when Array
29
+ if arg.size == 2
30
+ @projections << "#{arg.first} AS #{arg.last}"
31
+ else
32
+ arg.each { |x| @projections << select_single_string(x) }
33
+ end
34
+ end
35
+ end
36
+ self
37
+ end
38
+
39
+ alias :columns :select
40
+
41
+ def select!(*args)
42
+ @projections = []
43
+ select *args
44
+ end
45
+
46
+ def from(*args)
47
+ args.each { |x| @targets << x.to_s }
48
+ self
49
+ end
50
+
51
+ def from!(*args)
52
+ @targets = []
53
+ from *args
54
+ end
55
+
56
+ def order(*args)
57
+ args.each do |arg|
58
+ case arg
59
+ when Hash
60
+ arg.each { |k, v| @order << "#{k} #{order_direction_for(v)}" }
61
+ when Array
62
+ case arg.size
63
+ when 2
64
+ @order << "#{arg[0]} #{order_direction_for(arg[1])}"
65
+ else
66
+ arg.each { |x| @order << x.to_s }
67
+ end
68
+ else
69
+ @order << arg.to_s
70
+ end
71
+ end
72
+ self
73
+ end
74
+
75
+ def order!(*args)
76
+ @order = []
77
+ order *args
78
+ end
79
+
80
+ def limit(max_records)
81
+ @limit = max_records.to_s.to_i
82
+ self
83
+ end
84
+
85
+ alias :limit! :limit
86
+
87
+ def range(lower_rid, upper_rid = nil)
88
+ @lower_range = lower_rid.to_s
89
+ @upper_range = upper_rid ? upper_rid.to_s : nil
90
+ self
91
+ end
92
+
93
+ alias :range! :range
94
+
95
+ def to_s
96
+ (select_sql + target_sql + conditions_sql + order_sql + limit_sql + range_sql).strip
97
+ end
98
+
99
+ def to_sql_query
100
+ OrientDB::SQLSynchQuery.new to_s
101
+ end
102
+
103
+ private
104
+
105
+ def select_sql
106
+ str = @projections.empty? ? '' : @projections.map { |x| x.to_s }.join(', ') + ' '
107
+ "SELECT #{str}"
108
+ end
109
+
110
+ def target_sql
111
+ case @targets.size
112
+ when 0
113
+ "FROM "
114
+ when 1
115
+ "FROM #{@targets.first} "
116
+ else
117
+ "FROM [#{@targets.map { |x| x.to_s }.join(", ")}] "
118
+ end
119
+ end
120
+
121
+ def order_sql
122
+ @order.empty? ? '' : "ORDER BY #{@order.map { |x| x.to_s }.join(', ')} "
123
+ end
124
+
125
+ def limit_sql
126
+ @limit.nil? ? '' : "LIMIT #{@limit} "
127
+ end
128
+
129
+ def range_sql
130
+ @lower_range.nil? ? '' : "RANGE #{@lower_range}#{@upper_range ? ", #{@upper_range}" : ''} "
131
+ end
132
+
133
+ def order_direction_for(value)
134
+ value.to_s.strip.downcase == 'desc' ? 'DESC' : 'ASC'
135
+ end
136
+
137
+ end
138
+ end
@@ -0,0 +1,57 @@
1
+ module OrientDB::SQL
2
+ class Update
3
+
4
+ include OrientDB::SQL::UtilsMixin
5
+ include OrientDB::SQL::ClassClusterParametersMixin
6
+ include OrientDB::SQL::FieldsValuesParametersMixin
7
+ include OrientDB::SQL::ConditionsParametersMixin
8
+
9
+ def initialize
10
+ @oclass = nil
11
+ @cluster = nil
12
+ @action = "SET"
13
+ @fields = []
14
+ @values = []
15
+ @conditions = []
16
+ end
17
+
18
+ def action(new_action)
19
+ @action = new_action.to_s.upcase
20
+ self
21
+ end
22
+
23
+ alias :action! :action
24
+
25
+ def to_s
26
+ (target_sql(:update) + fields_sql + conditions_sql).strip
27
+ end
28
+
29
+ def to_sql_command
30
+ OrientDB::SQLCommand.new to_s
31
+ end
32
+
33
+ private
34
+
35
+ def fields_sql
36
+ raise "Missing fields" if @fields.empty?
37
+ str = "#{@action} "
38
+ if @action == "REMOVE" && @values.empty?
39
+ str += @fields.join(', ')
40
+ else
41
+ raise "Missing values" if @values.empty?
42
+ raise "Unbalanced fields & values" unless @values.size == @fields.size
43
+ ary = []
44
+ @fields.each_with_index do |field, idx|
45
+ ary << "#{field} = #{@values[idx]}"
46
+ end
47
+ str += ary.join(", ")
48
+ end
49
+ str + ' '
50
+ end
51
+
52
+ def values_sql
53
+ "(#{@values.join(', ')})"
54
+ end
55
+
56
+ end
57
+ end
@@ -1,13 +0,0 @@
1
- module OrientDB
2
-
3
- class SqlQuery
4
-
5
- def inspect
6
- %{#<OrientDB::SqlQuery:#{name} text="#{text}">}
7
- end
8
-
9
- alias :to_s :inspect
10
-
11
- end
12
-
13
- end
data/orientdb.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{orientdb}
8
- s.version = "0.0.12"
8
+ s.version = "0.0.14"
9
9
  s.platform = %q{jruby}
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version=
12
12
  s.authors = ["Adrian Madrid"]
13
- s.date = %q{2011-01-20}
13
+ s.date = %q{2011-01-25}
14
14
  s.default_executable = %q{orientdb_console}
15
15
  s.description = %q{Simple JRuby wrapper for the OrientDB.}
16
16
  s.email = ["aemadrid@gmail.com"]
@@ -45,7 +45,12 @@ Gem::Specification.new do |s|
45
45
  "lib/orientdb/record.rb",
46
46
  "lib/orientdb/schema.rb",
47
47
  "lib/orientdb/sql.rb",
48
- "lib/orientdb/sql_ext.rb",
48
+ "lib/orientdb/sql/common.rb",
49
+ "lib/orientdb/sql/delete.rb",
50
+ "lib/orientdb/sql/ext.rb",
51
+ "lib/orientdb/sql/insert.rb",
52
+ "lib/orientdb/sql/query.rb",
53
+ "lib/orientdb/sql/update.rb",
49
54
  "lib/orientdb/sql_query.rb",
50
55
  "lib/orientdb/storage.rb",
51
56
  "lib/orientdb/user.rb",
@@ -81,10 +81,13 @@ describe "OrientDB" do
81
81
  end
82
82
 
83
83
  it "should find rows by simple field values" do
84
- DB.first("SELECT * FROM employee WHERE name = 'Mark'").should == @e1
85
84
  DB.first('SELECT * FROM employee WHERE age = 37').should == @e2
86
85
  end
87
86
 
87
+ it "should find rows by simple field values" do
88
+ DB.find_by_rid(@e3.rid).rid.should == @e3.rid
89
+ end
90
+
88
91
  it "should find rows by values in arrays" do
89
92
  qry = DB.prepare_sql_query "SELECT * FROM #{@oclass} WHERE 'admin' IN groups"
90
93
  DB.all(qry).map { |x| x.name }.sort.should == [@e1, @e2, @e4].map { |x| x.name }.sort
data/spec/sql_spec.rb CHANGED
@@ -4,13 +4,7 @@ describe "OrientDB" do
4
4
 
5
5
  describe "SQL" do
6
6
 
7
- describe "Query" do
8
-
9
- it "should do a blank query" do
10
- @q = OrientDB::SQL::Query.new
11
- @q.should be_a_kind_of OrientDB::SQL::Query
12
- @q.to_s.should == 'SELECT FROM'
13
- end
7
+ describe "Common" do
14
8
 
15
9
  describe "quote" do
16
10
  it "should quote numeric values correctly" do
@@ -49,6 +43,44 @@ describe "OrientDB" do
49
43
  end
50
44
  end
51
45
 
46
+ describe "select_single_string" do
47
+ it "should split extend and alias strings correctly" do
48
+ OrientDB::SQL::Query.select_single_string('a__b___c').should == 'a.b AS c'
49
+ end
50
+
51
+ it "should split extend strings correctly" do
52
+ OrientDB::SQL::Query.select_single_string('a__b').should == 'a.b'
53
+ end
54
+
55
+ it "should split extended strings correctly" do
56
+ OrientDB::SQL::Query.select_single_string('a__b__c').should == 'a.b.c'
57
+ end
58
+
59
+ it "should split alias strings correctly" do
60
+ OrientDB::SQL::Query.select_single_string('a___c').should == 'a AS c'
61
+ end
62
+ end
63
+
64
+ describe "field_name" do
65
+ it "should split simple names correctly" do
66
+ OrientDB::SQL::Query.field_name(:a__b).should == 'a.b'
67
+ end
68
+
69
+ it "should split longer names correctly" do
70
+ OrientDB::SQL::Query.field_name(:a__b__c).should == 'a.b.c'
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ describe "Query" do
77
+
78
+ it "should do a blank query" do
79
+ @q = OrientDB::SQL::Query.new
80
+ @q.should be_a_kind_of OrientDB::SQL::Query
81
+ @q.to_s.should == 'SELECT FROM'
82
+ end
83
+
52
84
  describe "SELECT" do
53
85
 
54
86
  before :each do
@@ -661,6 +693,132 @@ describe "OrientDB" do
661
693
  end
662
694
  end
663
695
  end
696
+ end
697
+
698
+ describe "INSERT" do
699
+
700
+ before :each do
701
+ @q = OrientDB::SQL::Insert.new
702
+ end
703
+
704
+ it "should handle basic oclass inserts" do
705
+ @q.oclass('Person').values(:name => "Martin").to_s.should == "INSERT INTO Person (name) VALUES ('Martin')"
706
+ end
707
+
708
+ it "should handle basic oclass inserts" do
709
+ @q.cluster('person').values(:name => "Martin").to_s.should == "INSERT INTO cluster:person (name) VALUES ('Martin')"
710
+ end
711
+
712
+ it "should handle complex oclass inserts" do
713
+ @q.oclass('Person').fields(:name => "Martin").values(:age => 39).to_s.should == "INSERT INTO Person (name, age) VALUES ('Martin', 39)"
714
+ end
715
+
716
+ it "should handle array oclass inserts" do
717
+ @q.oclass('Person').fields(:name, :age).values("Martin", 39).to_s.should == "INSERT INTO Person (name, age) VALUES ('Martin', 39)"
718
+ end
719
+
720
+ end
721
+
722
+ describe "UPDATE" do
723
+
724
+ before :each do
725
+ @q = OrientDB::SQL::Update.new
726
+ end
664
727
 
728
+ it "should handle basic oclass SET update" do
729
+ @q.oclass('Person').values(:name => "Martin").to_s.should == "UPDATE Person SET name = 'Martin'"
730
+ end
731
+
732
+ it "should handle basic cluster SET update" do
733
+ @q.cluster('person').values(:name => "Martin").to_s.should == "UPDATE cluster:person SET name = 'Martin'"
734
+ end
735
+
736
+ it "should handle complex oclass SET updates" do
737
+ @q.oclass('Person').fields(:name => "Martin").values(:age => 39).to_s.should == "UPDATE Person SET name = 'Martin', age = 39"
738
+ end
739
+
740
+ it "should handle array oclass REMOVE a field updates" do
741
+ @q.oclass('Person').action(:remove).fields(:name).to_s.should == "UPDATE Person REMOVE name"
742
+ end
743
+
744
+ it "should handle array oclass REMOVE many fields updates" do
745
+ @q.oclass('Person').action(:remove).fields(:name, :age).to_s.should == "UPDATE Person REMOVE name, age"
746
+ end
747
+
748
+ it "should handle basic oclass REMOVE a value from a collection update" do
749
+ @q.oclass('Person').action(:remove).values(:name => "Martin").to_s.should == "UPDATE Person REMOVE name = 'Martin'"
750
+ end
751
+
752
+ it "should handle basic oclass ADD a value into a collection update" do
753
+ @q.oclass('Person').action(:add).values(:name => "Martin").to_s.should == "UPDATE Person ADD name = 'Martin'"
754
+ end
755
+
756
+ it "should handle basic oclass ADD many values into a collection update" do
757
+ @q.oclass('Person').action(:add).values(:name => "Martin").values(:age => 39).to_s.should == "UPDATE Person ADD name = 'Martin', age = 39"
758
+ end
759
+
760
+ it "should handle simple hashes for conditions" do
761
+ @q.oclass('Person').values(:age => 39).where(:a => 1).to_s.should == 'UPDATE Person SET age = 39 WHERE a = 1'
762
+ end
763
+
764
+ it "should handle simple arrays for conditions" do
765
+ @q.oclass('Person').values(:age => 39).where(['a > 1', 'b < 3', 'c = 5']).to_s.should == 'UPDATE Person SET age = 39 WHERE a > 1 AND b < 3 AND c = 5'
766
+ end
767
+
768
+ it "should handle concatenation for conditions" do
769
+ @q.oclass('Person').values(:age => 39).where(:a => 1).where(:b => 2).to_s.should == 'UPDATE Person SET age = 39 WHERE a = 1 AND b = 2'
770
+ end
771
+
772
+ it "should handle overriding for conditions" do
773
+ @q.oclass('Person').values(:age => 39).where(:a => 1).where!(:b => 2).to_s.should == 'UPDATE Person SET age = 39 WHERE b = 2'
774
+ end
775
+
776
+ it "should handle simple joined AND conditions for conditions" do
777
+ @q.oclass('Person').values(:age => 39).where(:a => 1).and(:b => 2).to_s.should == 'UPDATE Person SET age = 39 WHERE a = 1 AND b = 2'
778
+ end
779
+
780
+ it "should handle simple joined OR conditions for conditions" do
781
+ @q.oclass('Person').values(:age => 39).where(:a => 1).or(:b => 2).to_s.should == 'UPDATE Person SET age = 39 WHERE a = 1 OR b = 2'
782
+ end
783
+ end
784
+
785
+ describe "DELETE" do
786
+
787
+ before :each do
788
+ @q = OrientDB::SQL::Delete.new
789
+ end
790
+
791
+ it "should handle basic oclass" do
792
+ @q.oclass('Person').to_s.should == "DELETE FROM Person"
793
+ end
794
+
795
+ it "should handle basic cluster" do
796
+ @q.cluster('person').to_s.should == "DELETE FROM cluster:person"
797
+ end
798
+
799
+ it "should handle simple hashes for conditions" do
800
+ @q.oclass('Person').where(:a => 1).to_s.should == 'DELETE FROM Person WHERE a = 1'
801
+ end
802
+
803
+ it "should handle simple arrays for conditions" do
804
+ @q.oclass('Person').where(['a > 1', 'b < 3', 'c = 5']).to_s.should == 'DELETE FROM Person WHERE a > 1 AND b < 3 AND c = 5'
805
+ end
806
+
807
+ it "should handle concatenation for conditions" do
808
+ @q.oclass('Person').where(:a => 1).where(:b => 2).to_s.should == 'DELETE FROM Person WHERE a = 1 AND b = 2'
809
+ end
810
+
811
+ it "should handle overriding for conditions" do
812
+ @q.oclass('Person').where(:a => 1).where!(:b => 2).to_s.should == 'DELETE FROM Person WHERE b = 2'
813
+ end
814
+
815
+ it "should handle simple joined AND conditions for conditions" do
816
+ @q.oclass('Person').where(:a => 1).and(:b => 2).to_s.should == 'DELETE FROM Person WHERE a = 1 AND b = 2'
817
+ end
818
+
819
+ it "should handle simple joined OR conditions for conditions" do
820
+ @q.oclass('Person').where(:a => 1).or(:b => 2).to_s.should == 'DELETE FROM Person WHERE a = 1 OR b = 2'
821
+ end
665
822
  end
823
+
666
824
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: orientdb
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.12
5
+ version: 0.0.14
6
6
  platform: jruby
7
7
  authors:
8
8
  - Adrian Madrid
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-01-20 00:00:00 -07:00
13
+ date: 2011-01-25 00:00:00 -07:00
14
14
  default_executable: orientdb_console
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -71,7 +71,12 @@ files:
71
71
  - lib/orientdb/record.rb
72
72
  - lib/orientdb/schema.rb
73
73
  - lib/orientdb/sql.rb
74
- - lib/orientdb/sql_ext.rb
74
+ - lib/orientdb/sql/common.rb
75
+ - lib/orientdb/sql/delete.rb
76
+ - lib/orientdb/sql/ext.rb
77
+ - lib/orientdb/sql/insert.rb
78
+ - lib/orientdb/sql/query.rb
79
+ - lib/orientdb/sql/update.rb
75
80
  - lib/orientdb/sql_query.rb
76
81
  - lib/orientdb/storage.rb
77
82
  - lib/orientdb/user.rb