orientdb 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.document +5 -0
  2. data/Gemfile +8 -0
  3. data/Gemfile.lock +32 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +88 -0
  6. data/Rakefile +57 -0
  7. data/bin/orientdb_console +23 -0
  8. data/lib/jars/blueprints-core-2.2.0-SNAPSHOT.jar +0 -0
  9. data/lib/jars/blueprints-orient-graph-2.2.0-SNAPSHOT.jar +0 -0
  10. data/lib/jars/orient-commons-1.2.0.jar +0 -0
  11. data/lib/jars/orientdb-client-1.2.0.jar +0 -0
  12. data/lib/jars/orientdb-core-1.2.0.jar +0 -0
  13. data/lib/jars/orientdb-distributed-1.2.0.jar +0 -0
  14. data/lib/jars/orientdb-enterprise-1.2.0.jar +0 -0
  15. data/lib/jars/orientdb-graphdb-1.2.0.jar +0 -0
  16. data/lib/jars/orientdb-server-1.2.0.jar +0 -0
  17. data/lib/jars/orientdb-tools-1.2.0.jar +0 -0
  18. data/lib/jars/pipes-2.0.0-SNAPSHOT.jar +0 -0
  19. data/lib/orientdb.rb +29 -0
  20. data/lib/orientdb/constants.rb +57 -0
  21. data/lib/orientdb/database.rb +167 -0
  22. data/lib/orientdb/document.rb +78 -0
  23. data/lib/orientdb/ext.rb +13 -0
  24. data/lib/orientdb/oclass.rb +141 -0
  25. data/lib/orientdb/property.rb +37 -0
  26. data/lib/orientdb/record.rb +18 -0
  27. data/lib/orientdb/rid.rb +46 -0
  28. data/lib/orientdb/schema.rb +33 -0
  29. data/lib/orientdb/sql.rb +18 -0
  30. data/lib/orientdb/sql/common.rb +247 -0
  31. data/lib/orientdb/sql/delete.rb +23 -0
  32. data/lib/orientdb/sql/ext.rb +249 -0
  33. data/lib/orientdb/sql/insert.rb +37 -0
  34. data/lib/orientdb/sql/query.rb +138 -0
  35. data/lib/orientdb/sql/update.rb +57 -0
  36. data/lib/orientdb/storage.rb +51 -0
  37. data/lib/orientdb/version.rb +3 -0
  38. data/orientdb.gemspec +95 -0
  39. data/spec/database_spec.rb +111 -0
  40. data/spec/document_spec.rb +99 -0
  41. data/spec/graph_spec.rb +39 -0
  42. data/spec/orientdb_spec.rb +10 -0
  43. data/spec/spec.opts +7 -0
  44. data/spec/spec_basic_helper.rb +25 -0
  45. data/spec/spec_helper.rb +68 -0
  46. data/spec/sql_spec.rb +839 -0
  47. data/spec/tinkerpop_graph_spec.rb +32 -0
  48. metadata +165 -0
@@ -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
@@ -0,0 +1,249 @@
1
+ module OrientDB
2
+ module SQL
3
+
4
+ def monkey_patched
5
+ @monkey_patched ||= []
6
+ end
7
+
8
+ module_function :monkey_patched
9
+
10
+ def monkey_patched?(name)
11
+ monkey_patched.include? name.to_s.downcase.to_sym
12
+ end
13
+
14
+ module_function :monkey_patched?
15
+
16
+ # Extend strings and/or symbols to create queries easier
17
+ def monkey_patch!(*args)
18
+ args = %w{ symbol string } if args.empty?
19
+ args.each do |arg|
20
+ case arg.to_s.downcase.to_sym
21
+ when :symbol
22
+ if monkey_patched?(:symbol)
23
+ puts "Symbol has already been monkey patched!"
24
+ false
25
+ else
26
+ Symbol.send :include, OrderExtension
27
+ Symbol.send :include, ConditionalExtension
28
+ Symbol.send :include, FieldOperatorExtension
29
+ Symbol.send :include, BundledFunctionExtension
30
+ monkey_patched << :symbol
31
+ true
32
+ end
33
+ when :string
34
+ if monkey_patched?(:string)
35
+ puts "String has already been monkey patched!"
36
+ false
37
+ else
38
+ String.send :include, OrderExtension
39
+ String.send :include, ConditionalExtension
40
+ String.send :include, FieldOperatorExtension
41
+ String.send :include, BundledFunctionExtension
42
+ monkey_patched << :string
43
+ true
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ module_function :monkey_patch!
50
+
51
+ module OrderExtension
52
+ def asc
53
+ "#{to_s} ASC"
54
+ end
55
+
56
+ def desc
57
+ "#{to_s} DESC"
58
+ end
59
+ end
60
+
61
+ module ConditionalExtension
62
+ include OrientDB::SQL::UtilsMixin
63
+
64
+ def lit
65
+ OrientDB::SQL::LiteralExpression.new to_s
66
+ end
67
+
68
+ def like(value)
69
+ "#{to_s} LIKE #{quote(value)}"
70
+ end
71
+
72
+ def eq(value)
73
+ "#{to_s} = #{quote(value)}"
74
+ end
75
+
76
+ def lt(value)
77
+ "#{to_s} < #{quote(value)}"
78
+ end
79
+
80
+ def lte(value)
81
+ "#{to_s} <= #{quote(value)}"
82
+ end
83
+
84
+ def gt(value)
85
+ "#{to_s} > #{quote(value)}"
86
+ end
87
+
88
+ def gte(value)
89
+ "#{to_s} >= #{quote(value)}"
90
+ end
91
+
92
+ def ne(value)
93
+ "#{to_s} <> #{quote(value)}"
94
+ end
95
+
96
+ def is_null
97
+ "#{to_s} IS null"
98
+ end
99
+
100
+ def is_not_null
101
+ "#{to_s} IS NOT null"
102
+ end
103
+
104
+ def in(*values)
105
+ "#{to_s} IN #{quote(values)}"
106
+ end
107
+
108
+ def contains(field, value)
109
+ "#{to_s} contains (#{field} = #{quote(value)})"
110
+ end
111
+
112
+ def contains_all(field, value)
113
+ "#{to_s} containsAll (#{field} = #{quote(value)})"
114
+ end
115
+
116
+ def contains_key(value)
117
+ "#{to_s} containsKey #{quote(value)}"
118
+ end
119
+
120
+ def contains_value(value)
121
+ "#{to_s} containsValue #{quote(value)}"
122
+ end
123
+
124
+ def contains_text(value)
125
+ "#{to_s} containsText #{quote(value)}"
126
+ end
127
+
128
+ def matches(value)
129
+ "#{to_s} matches #{quote(value)}"
130
+ end
131
+ end
132
+
133
+ module FieldOperatorExtension
134
+ include OrientDB::SQL::UtilsMixin
135
+
136
+ # Avoided overriding the native method
137
+ def odb_length
138
+ "#{to_s}.length()"
139
+ end
140
+
141
+ # Avoided overriding the native method
142
+ def odb_trim
143
+ "#{to_s}.trim()"
144
+ end
145
+
146
+ def to_upper_case
147
+ "#{to_s}.toUpperCase()"
148
+ end
149
+
150
+ def to_lower_case
151
+ "#{to_s}.toLowerCase()"
152
+ end
153
+
154
+ # Avoided overriding the native method
155
+ def odb_left(length)
156
+ "#{to_s}.left(#{quote(length)})"
157
+ end
158
+
159
+ # Avoided overriding the native method
160
+ def odb_right(length)
161
+ "#{to_s}.right(#{quote(length)})"
162
+ end
163
+
164
+ def sub_string(start, length = nil)
165
+ "#{to_s}.subString(#{quote(start)}#{length ? ", #{quote(length)}" : ''})"
166
+ end
167
+
168
+ def char_at(pos)
169
+ "#{to_s}.charAt(#{quote(pos)})"
170
+ end
171
+
172
+ def index_of(string, start = nil)
173
+ "#{to_s}.indexOf(#{quote(string)}#{start ? ", #{quote(start)}" : ''})"
174
+ end
175
+
176
+ # Avoided overriding the native method
177
+ def odb_format(frmt)
178
+ "#{to_s}.format(#{quote(frmt)})"
179
+ end
180
+
181
+ # Avoided overriding the native method
182
+ def odb_size
183
+ "#{to_s}.size()"
184
+ end
185
+
186
+ def as_string
187
+ "#{to_s}.asString()"
188
+ end
189
+
190
+ def as_integer
191
+ "#{to_s}.asInteger()"
192
+ end
193
+
194
+ def as_float
195
+ "#{to_s}.asFloat()"
196
+ end
197
+
198
+ def as_date
199
+ "#{to_s}.asDate()"
200
+ end
201
+
202
+ def as_date_time
203
+ "#{to_s}.asDateTime()"
204
+ end
205
+
206
+ def as_boolean
207
+ "#{to_s}.asBoolean()"
208
+ end
209
+ end
210
+
211
+ module BundledFunctionExtension
212
+ include OrientDB::SQL::UtilsMixin
213
+
214
+ # Avoided overriding the native method
215
+ def odb_count
216
+ "count(#{to_s})"
217
+ end
218
+
219
+ # Avoided overriding the native method
220
+ def odb_min
221
+ "min(#{to_s})"
222
+ end
223
+
224
+ # Avoided overriding the native method
225
+ def odb_max
226
+ "max(#{to_s})"
227
+ end
228
+
229
+ # Avoided overriding the native method
230
+ def odb_avg
231
+ "avg(#{to_s})"
232
+ end
233
+
234
+ # Avoided overriding the native method
235
+ def odb_sum
236
+ "sum(#{to_s})"
237
+ end
238
+
239
+ def sysdate
240
+ "sysdate('#{to_s}')"
241
+ end
242
+
243
+ # Avoided overriding the native method
244
+ def odb_format_str(*args)
245
+ "format('#{to_s}', #{args.map{|x| quote(x)}.join(', ')})"
246
+ end
247
+ end
248
+ end
249
+ 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