imparcial 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/lib/imparcial/driver/{base → abstract}/expression/base.rb +11 -4
  2. data/lib/imparcial/driver/abstract/expression/column.rb +313 -0
  3. data/lib/imparcial/driver/abstract/expression/constraint.rb +149 -0
  4. data/lib/imparcial/driver/abstract/expression/delete.rb +88 -0
  5. data/lib/imparcial/driver/{base → abstract}/expression/index.rb +103 -96
  6. data/lib/imparcial/driver/abstract/expression/insert.rb +49 -0
  7. data/lib/imparcial/driver/{base → abstract}/expression/lock.rb +1 -1
  8. data/lib/imparcial/driver/{base/expression/util.rb → abstract/expression/record.rb} +8 -12
  9. data/lib/imparcial/driver/{base → abstract}/expression/select.rb +6 -4
  10. data/lib/imparcial/driver/{base → abstract}/expression/sequence.rb +133 -62
  11. data/lib/imparcial/driver/{base → abstract}/expression/statement.rb +1 -1
  12. data/lib/imparcial/driver/abstract/expression/table.rb +416 -0
  13. data/lib/imparcial/driver/abstract/expression/transaction.rb +143 -0
  14. data/lib/imparcial/driver/{base → abstract}/expression/update.rb +20 -3
  15. data/lib/imparcial/driver/abstract/expression.rb +24 -0
  16. data/lib/imparcial/driver/{base → abstract}/result.rb +12 -11
  17. data/lib/imparcial/driver/abstract/sql/column.rb +103 -0
  18. data/lib/imparcial/driver/abstract/sql/constraint.rb +42 -0
  19. data/lib/imparcial/driver/{base → abstract}/sql/delete.rb +1 -1
  20. data/lib/imparcial/driver/{base → abstract}/sql/index.rb +3 -11
  21. data/lib/imparcial/driver/{base → abstract}/sql/insert.rb +1 -1
  22. data/lib/imparcial/driver/abstract/sql/record.rb +19 -0
  23. data/lib/imparcial/driver/{base → abstract}/sql/select.rb +2 -2
  24. data/lib/imparcial/driver/{base → abstract}/sql/sequence.rb +2 -2
  25. data/lib/imparcial/driver/{base/sql/table_operation.rb → abstract/sql/table.rb} +8 -15
  26. data/lib/imparcial/driver/{base → abstract}/sql/transaction.rb +5 -5
  27. data/lib/imparcial/driver/{base → abstract}/sql/update.rb +1 -1
  28. data/lib/imparcial/driver/abstract/sql.rb +21 -0
  29. data/lib/imparcial/driver/{base → abstract}/typemap.rb +43 -89
  30. data/lib/imparcial/driver/{base → abstract}/util.rb +17 -5
  31. data/lib/imparcial/driver/abstract.rb +255 -0
  32. data/lib/imparcial/driver/mysql/expression/table.rb +2 -11
  33. data/lib/imparcial/driver/mysql/expression.rb +4 -4
  34. data/lib/imparcial/driver/mysql/result.rb +1 -1
  35. data/lib/imparcial/driver/mysql/sql/column.rb +59 -0
  36. data/lib/imparcial/driver/mysql/sql/constraint.rb +39 -0
  37. data/lib/imparcial/driver/mysql/sql/index.rb +14 -23
  38. data/lib/imparcial/driver/mysql/sql/sequence.rb +1 -1
  39. data/lib/imparcial/driver/mysql/sql/{table_metadata.rb → table.rb} +29 -5
  40. data/lib/imparcial/driver/mysql/sql.rb +7 -7
  41. data/lib/imparcial/driver/mysql/typemap.rb +1 -1
  42. data/lib/imparcial/driver/mysql/util.rb +1 -1
  43. data/lib/imparcial/driver/mysql.rb +19 -18
  44. data/lib/imparcial/driver/postgre/expression.rb +26 -7
  45. data/lib/imparcial/driver/postgre/result.rb +1 -1
  46. data/lib/imparcial/driver/postgre/sql/column.rb +53 -0
  47. data/lib/imparcial/driver/postgre/sql/constraint.rb +37 -0
  48. data/lib/imparcial/driver/postgre/sql/index.rb +21 -21
  49. data/lib/imparcial/driver/postgre/sql/sequence.rb +6 -4
  50. data/lib/imparcial/driver/postgre/sql/{table_metadata.rb → table.rb} +8 -8
  51. data/lib/imparcial/driver/postgre/sql.rb +7 -7
  52. data/lib/imparcial/driver/postgre/typemap.rb +2 -2
  53. data/lib/imparcial/driver/postgre/util.rb +1 -1
  54. data/lib/imparcial/driver/postgre.rb +11 -11
  55. data/lib/imparcial/driver.rb +1 -1
  56. data/lib/imparcial/exception.rb +28 -18
  57. data/lib/imparcial/initializer.rb +62 -0
  58. data/lib/imparcial.rb +1 -74
  59. metadata +50 -50
  60. data/lib/imparcial/driver/base/expression/delete.rb +0 -72
  61. data/lib/imparcial/driver/base/expression/insert.rb +0 -33
  62. data/lib/imparcial/driver/base/expression/table_diff.rb +0 -154
  63. data/lib/imparcial/driver/base/expression/table_evolution.rb +0 -94
  64. data/lib/imparcial/driver/base/expression/table_metadata.rb +0 -122
  65. data/lib/imparcial/driver/base/expression/table_operation.rb +0 -137
  66. data/lib/imparcial/driver/base/expression/transaction.rb +0 -59
  67. data/lib/imparcial/driver/base/expression.rb +0 -37
  68. data/lib/imparcial/driver/base/sql/table_metadata.rb +0 -29
  69. data/lib/imparcial/driver/base/sql.rb +0 -25
  70. data/lib/imparcial/driver/base.rb +0 -156
  71. data/lib/imparcial/driver/mysql/expression/index.rb +0 -44
  72. data/lib/imparcial/driver/mysql/sql/table_operation.rb +0 -20
  73. data/lib/imparcial/driver/postgre/expression/index.rb +0 -10
  74. data/lib/imparcial/driver/postgre/expression/sequence.rb +0 -9
  75. data/lib/imparcial/driver/postgre/expression/table.rb +0 -20
  76. data/lib/imparcial/driver/postgre/sql/table_operation.rb +0 -9
@@ -1,6 +1,6 @@
1
1
  module Imparcial
2
2
  module Driver
3
- module ExpressionBase
3
+ module AbstractExpression
4
4
  module Statement
5
5
 
6
6
  private
@@ -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
@@ -1,6 +1,6 @@
1
1
  module Imparcial
2
2
  module Driver
3
- module ExpressionBase
3
+ module AbstractExpression
4
4
  module Update
5
5
 
6
6
  private
@@ -13,13 +13,30 @@ module Imparcial
13
13
 
14
14
  public
15
15
 
16
- # Update a table.
16
+ # === Description
17
+ # Update some records.
18
+ #
19
+ # === Usage
20
+ # abstract_adapter.update :table_name => 'person', :values => {:salary => 1500},
21
+ # :conditions => ['id = ?',10]
22
+ #
23
+ # === Options
24
+ # * :table_name
25
+ # * :values
26
+ # * :conditions
27
+ #
28
+ # === Returning
29
+ # nothing
17
30
 
18
31
  def update ( options )
19
32
 
20
33
  check_options expected_options_for_updating, options
21
34
 
22
- query sql_for_updating( options )
35
+ sql = sql_for_updating( options )
36
+
37
+ logger.warn sql if @update_logging
38
+
39
+ query sql
23
40
 
24
41
  rescue adapter_specific_exception => ex
25
42
 
@@ -0,0 +1,24 @@
1
+ FileList[File.dirname(__FILE__) + '/expression/*.rb'].each do |f|
2
+ require f
3
+ end
4
+
5
+ module Imparcial
6
+ module Driver
7
+ module AbstractExpression
8
+ include Base
9
+ include Statement
10
+ include Sequence
11
+ include Column
12
+ include Table
13
+ include Constraint
14
+ include Insert
15
+ include Select
16
+ include Delete
17
+ include Update
18
+ include Record
19
+ include Lock
20
+ include Transaction
21
+ include Index
22
+ end
23
+ end
24
+ end
@@ -3,7 +3,7 @@ module Imparcial
3
3
 
4
4
  # This class will serve as base for adapter specific result class.
5
5
 
6
- class ResultBase
6
+ class AbstractResult
7
7
  attr_reader :specific
8
8
 
9
9
  def initialize ( result_specific )
@@ -12,7 +12,8 @@ module Imparcial
12
12
 
13
13
  end
14
14
 
15
- # Subclasses need to override this method.
15
+ # === Description
16
+ # get number of rows avaliable.
16
17
 
17
18
  def rows
18
19
 
@@ -20,7 +21,8 @@ module Imparcial
20
21
 
21
22
  end
22
23
 
23
- # Subclasses need to override this method.
24
+ # === Description
25
+ # fetch the result.
24
26
 
25
27
  def fetch
26
28
 
@@ -28,23 +30,22 @@ module Imparcial
28
30
 
29
31
  end
30
32
 
31
- # Fetch result in array form.
32
-
33
33
  def fetch_in_array
34
34
 
35
35
  array = []
36
36
 
37
- fetch do |*all|
37
+ fetch do |*rows|
38
38
 
39
- array << all
39
+ array << rows
40
40
 
41
41
  end
42
-
42
+
43
43
  array
44
44
 
45
45
  end
46
46
 
47
- # Fetch only the first row
47
+ # === Description
48
+ # Fetch the first row. Ignoring others if avaliable.
48
49
 
49
50
  def fetch_first_row
50
51
 
@@ -57,7 +58,7 @@ module Imparcial
57
58
 
58
59
  end
59
60
 
60
- # This class wraps a row.
61
+ # This class wraps a regular row.
61
62
  # Usually, a row will only come with value.
62
63
  # Instead, we can also keep the column name. So that, this class
63
64
  # can keep track of column name and value.
@@ -69,7 +70,7 @@ module Imparcial
69
70
  # -------------------
70
71
 
71
72
  class Row
72
- include Imparcial::Driver::UtilBase
73
+ include Imparcial::Driver::AbstractUtil
73
74
 
74
75
  attr_accessor :name, :value
75
76
  alias_method :column_name, :name