imparcial 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/History.txt +2 -5
  2. data/Manifest.txt +79 -5
  3. data/README.txt +71 -7
  4. data/Rakefile +1 -1
  5. data/lib/imparcial/driver/abstract/expression/column.rb +313 -0
  6. data/lib/imparcial/driver/abstract/expression/constraint.rb +149 -0
  7. data/lib/imparcial/driver/abstract/expression/delete.rb +88 -0
  8. data/lib/imparcial/driver/abstract/expression/index.rb +206 -0
  9. data/lib/imparcial/driver/abstract/expression/insert.rb +49 -0
  10. data/lib/imparcial/driver/abstract/expression/lock.rb +11 -0
  11. data/lib/imparcial/driver/abstract/expression/record.rb +41 -0
  12. data/lib/imparcial/driver/abstract/expression/select.rb +38 -0
  13. data/lib/imparcial/driver/abstract/expression/sequence.rb +260 -0
  14. data/lib/imparcial/driver/abstract/expression/statement.rb +128 -0
  15. data/lib/imparcial/driver/abstract/expression/table.rb +416 -0
  16. data/lib/imparcial/driver/abstract/expression/transaction.rb +143 -0
  17. data/lib/imparcial/driver/abstract/expression/update.rb +50 -0
  18. data/lib/imparcial/driver/abstract/expression.rb +23 -0
  19. data/lib/imparcial/driver/abstract/result.rb +95 -0
  20. data/lib/imparcial/driver/abstract/sql/column.rb +103 -0
  21. data/lib/imparcial/driver/abstract/sql/constraint.rb +42 -0
  22. data/lib/imparcial/driver/abstract/sql/delete.rb +22 -0
  23. data/lib/imparcial/driver/abstract/sql/index.rb +45 -0
  24. data/lib/imparcial/driver/abstract/sql/insert.rb +63 -0
  25. data/lib/imparcial/driver/abstract/sql/record.rb +19 -0
  26. data/lib/imparcial/driver/abstract/sql/select.rb +101 -0
  27. data/lib/imparcial/driver/abstract/sql/sequence.rb +55 -0
  28. data/lib/imparcial/driver/abstract/sql/table.rb +42 -0
  29. data/lib/imparcial/driver/abstract/sql/transaction.rb +43 -0
  30. data/lib/imparcial/driver/abstract/sql/update.rb +29 -0
  31. data/lib/imparcial/driver/abstract/sql.rb +21 -0
  32. data/lib/imparcial/driver/abstract/typemap.rb +168 -0
  33. data/lib/imparcial/driver/abstract/util.rb +53 -0
  34. data/lib/imparcial/driver/abstract.rb +255 -0
  35. data/lib/imparcial/driver/mysql/expression/table.rb +17 -0
  36. data/lib/imparcial/driver/mysql/expression.rb +11 -0
  37. data/lib/imparcial/driver/mysql/result.rb +33 -0
  38. data/lib/imparcial/driver/mysql/sql/column.rb +59 -0
  39. data/lib/imparcial/driver/mysql/sql/constraint.rb +39 -0
  40. data/lib/imparcial/driver/mysql/sql/index.rb +42 -0
  41. data/lib/imparcial/driver/mysql/sql/sequence.rb +39 -0
  42. data/lib/imparcial/driver/mysql/sql/table.rb +67 -0
  43. data/lib/imparcial/driver/mysql/sql.rb +15 -0
  44. data/lib/imparcial/driver/mysql/typemap.rb +13 -0
  45. data/lib/imparcial/driver/mysql/util.rb +13 -0
  46. data/lib/imparcial/driver/mysql.rb +49 -0
  47. data/lib/imparcial/driver/postgre/expression.rb +32 -0
  48. data/lib/imparcial/driver/postgre/result.rb +35 -0
  49. data/lib/imparcial/driver/postgre/sql/column.rb +53 -0
  50. data/lib/imparcial/driver/postgre/sql/constraint.rb +37 -0
  51. data/lib/imparcial/driver/postgre/sql/index.rb +53 -0
  52. data/lib/imparcial/driver/postgre/sql/sequence.rb +30 -0
  53. data/lib/imparcial/driver/postgre/sql/table.rb +46 -0
  54. data/lib/imparcial/driver/postgre/sql.rb +15 -0
  55. data/lib/imparcial/driver/postgre/typemap.rb +36 -0
  56. data/lib/imparcial/driver/postgre/util.rb +19 -0
  57. data/lib/imparcial/driver/postgre.rb +43 -0
  58. data/lib/imparcial/driver.rb +1 -0
  59. data/lib/imparcial/exception.rb +71 -0
  60. data/lib/imparcial/extension.rb +90 -0
  61. data/lib/imparcial/initializer.rb +30 -0
  62. data/lib/imparcial.rb +1 -1
  63. data/test/unit/base/common/tc_quote.rb +30 -0
  64. data/test/unit/base/expression/tc_column.rb +84 -0
  65. data/test/unit/base/expression/tc_constraint.rb +39 -0
  66. data/test/unit/base/expression/tc_delete.rb +51 -0
  67. data/test/unit/base/expression/tc_index.rb +43 -0
  68. data/test/unit/base/expression/tc_insert.rb +44 -0
  69. data/test/unit/base/expression/tc_select.rb +142 -0
  70. data/test/unit/base/expression/tc_sequence.rb +48 -0
  71. data/test/unit/base/expression/tc_table.rb +68 -0
  72. data/test/unit/base/expression/tc_table_diff.rb +41 -0
  73. data/test/unit/base/expression/tc_transaction.rb +46 -0
  74. data/test/unit/base/expression/tc_update.rb +29 -0
  75. data/test/unit/base/statement/tc_conditions.rb +84 -0
  76. data/test/unit/base/statement/tc_limit.rb +25 -0
  77. data/test/unit/base/statement/tc_order.rb +25 -0
  78. data/test/unit/helper.rb +64 -0
  79. data/test/unit/mysql/tc_sequence.rb +41 -0
  80. metadata +102 -10
@@ -0,0 +1,128 @@
1
+ module Imparcial
2
+ module Driver
3
+ module AbstractExpression
4
+ module Statement
5
+
6
+ private
7
+
8
+ def parse_conditions ( conditions )
9
+
10
+ # No condition? no evaluation!
11
+
12
+ return '' unless conditions
13
+
14
+ ref = ' WHERE '
15
+
16
+ # ['id = ?', 1]
17
+ # ^ ^
18
+ # | |
19
+ # syntax values
20
+
21
+ syntax = conditions[0]
22
+ values = conditions[1..-1]
23
+
24
+ # Extract all args.
25
+ #
26
+ # for example:
27
+ #
28
+ # 'id = ? AND name = ?'
29
+ #
30
+ # It has two args:
31
+ # id = ?
32
+ # name = ?
33
+
34
+ ops = '=|!=|>|<|>=|<=|<>|IN|NOT IN'
35
+ aps = "\"\'"
36
+ log = "AND|OR"
37
+
38
+ args = syntax.scan(/([\w|\.]+) ?(#{ops}) ?(\?|[#{aps}]?[\w|\d]+[#{aps}]?) ?(#{log})?/)
39
+ index = -1
40
+
41
+ args.each do |arg|
42
+
43
+ name, operator, value, logical = arg
44
+
45
+ if value != '?'
46
+
47
+ column = quote(name) + ' ' + operator + ' ' + value
48
+ column += ' ' + logical + ' ' if logical
49
+ ref += column
50
+ next
51
+
52
+ end
53
+
54
+ index += 1
55
+
56
+ if values[index].class == Array
57
+
58
+ raise ConditionError if values[index].length == 0
59
+
60
+ op = 'IN' if operator == '='
61
+ op = 'NOT IN' if operator == '!='
62
+ column = quote(name) + " #{op} ("
63
+
64
+ for i in 0..values[index].length-1
65
+ values[index][i] = quote_value(values[index][i])
66
+ end
67
+
68
+ column += values[index].join(',')
69
+
70
+ column += ')'
71
+ column += ' ' + logical + ' ' if logical
72
+
73
+ ref += column
74
+
75
+ else
76
+
77
+ value = values[index] || 'NULL'
78
+ value = quote_value(value)
79
+
80
+ column = quote(name) + ' ' + operator + ' ' + value
81
+ column += ' ' + logical + ' ' if logical
82
+
83
+ ref += column
84
+
85
+ end
86
+
87
+ end #end of args.each
88
+
89
+ ref
90
+
91
+ end # end of parse_condition
92
+
93
+ def parse_limit ( limit )
94
+
95
+ # No limit? no evaluation!
96
+
97
+ return '' unless limit
98
+
99
+ if limit.class != Range
100
+
101
+ ' LIMIT ' + limit.to_s
102
+
103
+ else
104
+
105
+ ' LIMIT ' + limit.first.to_i.to_s + ',' + limit.last.to_i.to_s
106
+
107
+ end
108
+
109
+ end
110
+
111
+ def parse_order ( options )
112
+
113
+ # No order? no evaluation!
114
+
115
+ return '' if !options[:order_desc] && !options[:order_asc]
116
+
117
+ syntax = ' ORDER BY '
118
+ syntax += quote( options[:order_desc] || options[:order_asc])
119
+ syntax += options[:order_desc] ? ' DESC' : ' ASC'
120
+
121
+ syntax
122
+
123
+ end
124
+
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,416 @@
1
+ module Imparcial
2
+ module Driver
3
+ module AbstractExpression
4
+ module Table
5
+
6
+ ###########################################
7
+ # #
8
+ # Dropping Table #
9
+ # #
10
+ ###########################################
11
+
12
+ private
13
+
14
+ def expected_options_for_dropping_table
15
+
16
+ {:table_name => :required}
17
+
18
+ end
19
+
20
+ public
21
+
22
+ # === Description
23
+ # Drop a table raising an exception if necessary.
24
+ #
25
+ # === Usage
26
+ # abstract_adapter.drop_table :table_name => 'person'
27
+ #
28
+ # === Options
29
+ # :table_name
30
+ #
31
+ # === Returning
32
+ # nothing
33
+
34
+ def drop_table ( options = {} )
35
+
36
+ check_options expected_options_for_dropping_table, options
37
+
38
+ sql = sql_for_dropping_table( options )
39
+
40
+ logger.warn sql if @table_logging
41
+
42
+ query sql
43
+
44
+ rescue adapter_specific_exception => ex
45
+
46
+ raise TableDropError.new(ex.message)
47
+
48
+ end
49
+
50
+ # === Description
51
+ # Drop a table by not raising any exception.
52
+ #
53
+ # === Usage
54
+ # abstract_adapter.drop_table_if_necessary :table_name => 'person'
55
+ #
56
+ # === Option
57
+ # :table_name
58
+ #
59
+ # === Returning
60
+ # nothing
61
+
62
+ def drop_table_if_necessary ( options = {} )
63
+
64
+ drop_table options
65
+
66
+ rescue TableDropError; end
67
+
68
+ # === Description
69
+ # Drop all tables raising an exception if necessary.
70
+ #
71
+ # === Usage
72
+ # abstract_adapter.drop_all_tables
73
+ #
74
+ # === Option
75
+ # No option
76
+ #
77
+ # === Returning
78
+ # nothing
79
+
80
+ def drop_all_tables
81
+
82
+ for table_name in get_tables
83
+
84
+ sql = sql_for_dropping_table( {:table_name => table_name} )
85
+
86
+ logger.warn sql if @table_logging
87
+
88
+ query sql
89
+
90
+ end
91
+
92
+ rescue adapter_specific_exception => ex
93
+
94
+ raise TableDropError.new(ex.message)
95
+
96
+ end
97
+
98
+ ###########################################
99
+ # #
100
+ # Creating Table #
101
+ # #
102
+ ###########################################
103
+
104
+ private
105
+
106
+ def expected_options_for_creating_table
107
+
108
+ {:table_name => :required, :fields => :required}
109
+
110
+ end
111
+
112
+ public
113
+
114
+ # === Description
115
+ # Create a table.
116
+ #
117
+ # === Usage
118
+ # abstract_adapter.create_table :table_name => 'person', :fields =>
119
+ # [{:name => 'id', :type => :integer},{:name => 'name', :type => :string}]
120
+ #
121
+ # === Option
122
+ # * :table_name
123
+ # * :fields = Having an array of hashes with information about columns.
124
+ #
125
+ # === Returning
126
+ # nothing
127
+ #
128
+ # === Note
129
+ # Some databases do override this method by adding up more options.
130
+ # Please, check out the adapter specific's one.
131
+
132
+ def create_table ( options = {} )
133
+
134
+ check_options expected_options_for_creating_table, options
135
+
136
+ sql = sql_for_creating_table( options )
137
+
138
+ logger.warn sql if @table_logging
139
+
140
+ query sql
141
+
142
+ rescue adapter_specific_exception => ex
143
+
144
+ raise TableCreateError.new(ex.message)
145
+
146
+ end
147
+
148
+ ###########################################
149
+ # #
150
+ # Table Listing #
151
+ # #
152
+ ###########################################
153
+
154
+ private
155
+
156
+ def expected_options_for_verifying_table_existence
157
+
158
+ {:table_name => :required}
159
+
160
+ end
161
+
162
+ public
163
+
164
+ # === Description
165
+ # Check if a given table exists.
166
+ #
167
+ # === Usage
168
+ # abstract_adapter.table_exists?(:table_name => 'person')
169
+ #
170
+ # === Option
171
+ # No options
172
+ #
173
+ # === Returning
174
+ # true or false
175
+
176
+ def table_exists? ( options = {} )
177
+
178
+ check_options expected_options_for_verifying_table_existence, options
179
+
180
+ tables = get_tables
181
+
182
+ tables.each do |name|
183
+
184
+ return true if name == options[:table_name]
185
+
186
+ end
187
+
188
+ return false
189
+
190
+ end
191
+
192
+ def table_not_exists? ( options = {} )
193
+
194
+ not table_exists? options
195
+
196
+ end
197
+
198
+ # === Description
199
+ # Return all tables avaliable in the current schema by name.
200
+ #
201
+ # === Usage
202
+ # abstract_adapter.get_tables
203
+ #
204
+ # === Option
205
+ # No options
206
+ #
207
+ # === Returning
208
+ # an array ['person','invoice',...]
209
+
210
+ def get_tables
211
+
212
+ sql = sql_for_getting_tables
213
+
214
+ logger.warn sql if @table_logging
215
+
216
+ query sql
217
+
218
+ tables = []
219
+
220
+ result.fetch do |table_name_column|
221
+
222
+ tables << table_name_column.value
223
+
224
+ end
225
+
226
+ tables
227
+
228
+ rescue adapter_specific_exception => ex
229
+
230
+ raise TableRetrieveError.new(ex.message)
231
+
232
+ end
233
+
234
+ ###########################################
235
+ # #
236
+ # Table Diffing #
237
+ # #
238
+ ###########################################
239
+
240
+ private
241
+
242
+ def expected_options_for_reporting_tables
243
+
244
+ {:table_name => :required, :fields => :required}
245
+
246
+ end
247
+
248
+ public
249
+
250
+ def diff_columns ( options = {} )
251
+
252
+ modified = report_modified_columns options
253
+ new = report_new_columns options
254
+ old = report_old_columns options
255
+
256
+ [modified, new, old]
257
+
258
+ end
259
+
260
+ # === Description
261
+ # Execute a diff by comparing incoming fields with existent ones.
262
+ #
263
+ # === Usage
264
+ # abstract_adapter.report_modified_columns :table_name => 'person',
265
+ # :fields => [{:name => :id, :type => :bigint}]
266
+ #
267
+ # === Option
268
+ # * :table_name
269
+ # * :fields => array of hashes
270
+ #
271
+ # === Returning
272
+ # fields affected
273
+
274
+ def report_modified_columns ( options = {} )
275
+
276
+ check_options expected_options_for_reporting_tables, options
277
+
278
+ modified_fields = options[:fields]
279
+
280
+ modified_columns = []
281
+ current_fields = get_columns_information :table_name => options[:table_name]
282
+
283
+ parse_fields modified_fields do |modified_field|
284
+
285
+ current_fields.each do |current_field|
286
+
287
+ if modified_field[:name] == current_field[:name]
288
+
289
+ # We gotta check every attribute to see if a modification can occur.
290
+
291
+ if modified_field[:type] != current_field[:type]||modified_field[:size]!=current_field[:size]
292
+
293
+ modified_columns << current_field
294
+
295
+ end
296
+
297
+ end
298
+
299
+ end
300
+
301
+ end #end of parse_fields
302
+
303
+ modified_columns
304
+
305
+ end
306
+
307
+ # === Description
308
+ # Execute a diff by comparing incoming fields with existent ones.
309
+ #
310
+ # === Usage
311
+ # abstract_adapter.report_new_columns :table_name => 'person',
312
+ # :fields => [{:name => :new_id, :type => :int}]
313
+ #
314
+ # === Option
315
+ # * :table_name
316
+ # * :fields => array of hashes
317
+ #
318
+ # === Returning
319
+ # fields affected
320
+
321
+ def report_new_columns ( options = {} )
322
+
323
+ check_options expected_options_for_reporting_tables, options
324
+
325
+ new_fields = options[:fields]
326
+
327
+ new_columns = []
328
+ no_new_field = false
329
+
330
+ current_fields = get_columns_information :table_name => options[:table_name]
331
+
332
+ parse_fields new_fields do |new_field|
333
+
334
+ current_fields.each do |current_field|
335
+
336
+ if new_field[:name] == current_field[:name]
337
+
338
+ no_new_field = true
339
+ break
340
+
341
+ end
342
+
343
+ end
344
+
345
+ if no_new_field
346
+
347
+ no_new_field = false
348
+ next
349
+
350
+ end
351
+
352
+ new_columns << new_field
353
+
354
+ end
355
+
356
+ new_columns
357
+
358
+ end
359
+
360
+ # === Description
361
+ # Execute a diff by comparing incoming fields with existent ones.
362
+ #
363
+ # === Usage
364
+ # abstract_adapter.report_old_columns :table_name => 'person',
365
+ # :fields => [{:name => :new_id, :type => :int}]
366
+ #
367
+ # === Option
368
+ # * :table_name
369
+ # * :fields => array of hashes
370
+ #
371
+ # === Returning
372
+ # fields affected
373
+
374
+ def report_old_columns ( options = {} )
375
+
376
+ check_options expected_options_for_reporting_tables, options
377
+
378
+ new_fields = options[:fields]
379
+
380
+ legacy_fields = []
381
+ no_old_field = false
382
+
383
+ current_fields = get_columns_information :table_name => options[:table_name]
384
+
385
+ current_fields.each do |current_field|
386
+
387
+ parse_fields new_fields do |new_field|
388
+
389
+ if new_field[:name] == current_field[:name]
390
+
391
+ no_old_field = true
392
+ break
393
+
394
+ end
395
+
396
+ if no_old_field == true
397
+
398
+ no_old_field = false
399
+ next
400
+
401
+ end
402
+
403
+ legacy_fields << current_field
404
+
405
+ end #end of parse_fields
406
+
407
+ end
408
+
409
+ legacy_fields
410
+
411
+ end
412
+
413
+ end
414
+ end
415
+ end
416
+ end
@@ -0,0 +1,143 @@
1
+ module Imparcial
2
+ module Driver
3
+ module AbstractExpression
4
+ module Transaction
5
+
6
+ private
7
+
8
+ def expected_options_for_saving_point
9
+
10
+ {:savepoint_name => :required}
11
+
12
+ end
13
+
14
+ public
15
+
16
+ # === Description
17
+ # Stablish a savepoint in a given moment during transactions.
18
+ #
19
+ # === Usage
20
+ # abstract_adapter.create_savepoint :savepoint_name => 'here'
21
+ #
22
+ # === Options
23
+ # * :savepoint_name
24
+ #
25
+ # === Returning
26
+ # nothing
27
+
28
+ def create_savepoint ( options = {} )
29
+
30
+ check_options expected_options_for_saving_point, options
31
+
32
+ query sql_for_saving_point( options )
33
+
34
+ rescue adapter_specific_exception => ex
35
+
36
+ raise TransactionError.new(ex.message)
37
+
38
+ end
39
+
40
+ private
41
+
42
+ def expected_options_for_restoring_point
43
+
44
+ {:savepoint_name => :required}
45
+
46
+ end
47
+
48
+ public
49
+
50
+ # === Description
51
+ # Rollback to a given savepoint.
52
+ #
53
+ # === Usage
54
+ # abstract_adapter.restore_savepoint :savepoint_name => 'here'
55
+ #
56
+ # === Options
57
+ # * :savepoint_name
58
+ #
59
+ # === Returning
60
+ # nothing
61
+
62
+ def restore_savepoint ( options = {} )
63
+
64
+ check_options expected_options_for_restoring_point, options
65
+
66
+ query sql_for_rolling_back( options )
67
+
68
+ rescue adapter_specific_exception => ex
69
+
70
+ raise TransactionError.new(ex.message)
71
+
72
+ end
73
+
74
+ # === Description
75
+ # Initiate a transaction.
76
+ #
77
+ # === Usage
78
+ # abstract_adapter.initialize_transaction
79
+ #
80
+ # === Options
81
+ # no options
82
+ #
83
+ # === Returning
84
+ # nothing
85
+
86
+ def initialize_transaction
87
+
88
+ query sql_for_initializing_a_transaction
89
+
90
+ rescue adapter_specific_exception => ex
91
+
92
+ raise TransactionError.new(ex.message)
93
+
94
+ end
95
+
96
+ # === Description
97
+ # Terminate a transaction by commiting.
98
+ #
99
+ # === Usage
100
+ # abstract_adapter.terminate_transaction
101
+ #
102
+ # === Options
103
+ # no options
104
+ #
105
+ # === Returning
106
+ # nothing
107
+
108
+ def terminate_transaction
109
+
110
+ query sql_for_terminating_a_transaction
111
+
112
+ rescue adapter_specific_exception => ex
113
+
114
+ raise TransactionError.new(ex.message)
115
+
116
+ end
117
+
118
+ # === Description
119
+ # Rollback a transaction.
120
+ #
121
+ # === Usage
122
+ # abstract_adapter.rollback_transaction
123
+ #
124
+ # === Options
125
+ # no options
126
+ #
127
+ # === Returning
128
+ # nothing
129
+
130
+ def rollback_transaction
131
+
132
+ query sql_for_rolling_back
133
+
134
+ rescue adapter_specific_exception => ex
135
+
136
+ raise TransactionError.new(ex.message)
137
+
138
+ end
139
+
140
+ end
141
+ end
142
+ end
143
+ end