imparcial 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. data/lib/imparcial/driver/base/expression/base.rb +104 -0
  2. data/lib/imparcial/driver/base/expression/delete.rb +72 -0
  3. data/lib/imparcial/driver/base/expression/index.rb +199 -0
  4. data/lib/imparcial/driver/base/expression/insert.rb +33 -0
  5. data/lib/imparcial/driver/base/expression/lock.rb +11 -0
  6. data/lib/imparcial/driver/base/expression/select.rb +36 -0
  7. data/lib/imparcial/driver/base/expression/sequence.rb +189 -0
  8. data/lib/imparcial/driver/base/expression/statement.rb +128 -0
  9. data/lib/imparcial/driver/base/expression/table_diff.rb +154 -0
  10. data/lib/imparcial/driver/base/expression/table_evolution.rb +94 -0
  11. data/lib/imparcial/driver/base/expression/table_metadata.rb +122 -0
  12. data/lib/imparcial/driver/base/expression/table_operation.rb +137 -0
  13. data/lib/imparcial/driver/base/expression/transaction.rb +59 -0
  14. data/lib/imparcial/driver/base/expression/update.rb +33 -0
  15. data/lib/imparcial/driver/base/expression/util.rb +45 -0
  16. data/lib/imparcial/driver/base/expression.rb +37 -0
  17. data/lib/imparcial/driver/base/result.rb +94 -0
  18. data/lib/imparcial/driver/base/sql/delete.rb +22 -0
  19. data/lib/imparcial/driver/base/sql/index.rb +53 -0
  20. data/lib/imparcial/driver/base/sql/insert.rb +63 -0
  21. data/lib/imparcial/driver/base/sql/select.rb +101 -0
  22. data/lib/imparcial/driver/base/sql/sequence.rb +55 -0
  23. data/lib/imparcial/driver/base/sql/table_metadata.rb +29 -0
  24. data/lib/imparcial/driver/base/sql/table_operation.rb +49 -0
  25. data/lib/imparcial/driver/base/sql/transaction.rb +43 -0
  26. data/lib/imparcial/driver/base/sql/update.rb +29 -0
  27. data/lib/imparcial/driver/base/sql.rb +25 -0
  28. data/lib/imparcial/driver/base/typemap.rb +214 -0
  29. data/lib/imparcial/driver/base/util.rb +41 -0
  30. data/lib/imparcial/driver/base.rb +156 -0
  31. data/lib/imparcial/driver/mysql/expression/index.rb +44 -0
  32. data/lib/imparcial/driver/mysql/expression/table.rb +26 -0
  33. data/lib/imparcial/driver/mysql/expression.rb +11 -0
  34. data/lib/imparcial/driver/mysql/result.rb +33 -0
  35. data/lib/imparcial/driver/mysql/sql/index.rb +51 -0
  36. data/lib/imparcial/driver/mysql/sql/sequence.rb +39 -0
  37. data/lib/imparcial/driver/mysql/sql/table_metadata.rb +43 -0
  38. data/lib/imparcial/driver/mysql/sql/table_operation.rb +20 -0
  39. data/lib/imparcial/driver/mysql/sql.rb +15 -0
  40. data/lib/imparcial/driver/mysql/typemap.rb +13 -0
  41. data/lib/imparcial/driver/mysql/util.rb +13 -0
  42. data/lib/imparcial/driver/mysql.rb +48 -0
  43. data/lib/imparcial/driver/postgre/expression/index.rb +10 -0
  44. data/lib/imparcial/driver/postgre/expression/sequence.rb +9 -0
  45. data/lib/imparcial/driver/postgre/expression/table.rb +20 -0
  46. data/lib/imparcial/driver/postgre/expression.rb +13 -0
  47. data/lib/imparcial/driver/postgre/result.rb +35 -0
  48. data/lib/imparcial/driver/postgre/sql/index.rb +53 -0
  49. data/lib/imparcial/driver/postgre/sql/sequence.rb +28 -0
  50. data/lib/imparcial/driver/postgre/sql/table_metadata.rb +46 -0
  51. data/lib/imparcial/driver/postgre/sql/table_operation.rb +9 -0
  52. data/lib/imparcial/driver/postgre/sql.rb +15 -0
  53. data/lib/imparcial/driver/postgre/typemap.rb +29 -0
  54. data/lib/imparcial/driver/postgre/util.rb +19 -0
  55. data/lib/imparcial/driver/postgre.rb +43 -0
  56. data/lib/imparcial/driver.rb +1 -0
  57. data/lib/imparcial/exception.rb +61 -0
  58. data/lib/imparcial.rb +82 -0
  59. metadata +114 -0
@@ -0,0 +1,104 @@
1
+ module Imparcial
2
+ module Driver
3
+ module ExpressionBase
4
+ module Base
5
+
6
+ private
7
+
8
+ # Basically it validates every user's entered option.
9
+
10
+ def check_options ( expected_options , user_options )
11
+
12
+ # Let's iterate over all user options.
13
+
14
+ user_options.each do |user_option, content|
15
+
16
+ confirmed = false
17
+
18
+ # Let's iterate over expected options.
19
+
20
+ expected_options.each_key do |name|
21
+
22
+ if name.to_s == user_option.to_s
23
+
24
+ # Let's verify if content is a collection.
25
+
26
+ if content.class == Array || content.class == Hash
27
+
28
+ # If so, we need to check out for content.
29
+
30
+ if content.length == 0
31
+
32
+ raise OptionError.new('Option ' + name.to_s + ' needs content')
33
+
34
+ end
35
+
36
+ end
37
+
38
+ # Yeah, user option matches with expected option.
39
+
40
+ confirmed = true
41
+ break
42
+
43
+ end
44
+
45
+ end
46
+
47
+ # If there's a match, we move forward.
48
+
49
+ if confirmed
50
+
51
+ confirmed = false
52
+ next
53
+
54
+ end
55
+
56
+ # Opss. user has entered with an unknown option.
57
+
58
+ raise OptionError.new('Cannot find option ' + user_option.to_s)
59
+
60
+ end
61
+
62
+ # Let's iterate over all expected and required options.
63
+
64
+ for name, condition in expected_options
65
+
66
+ # If this option is not required, we can move forward.
67
+
68
+ next unless condition == :required
69
+ confirmed = false
70
+
71
+ # Over user's.
72
+
73
+ user_options.each_key do |user_option|
74
+
75
+ if name.to_s == user_option.to_s
76
+
77
+ confirmed = true
78
+ break
79
+
80
+ end
81
+
82
+ end
83
+
84
+ # Alright, user has entered with a necessary option.
85
+
86
+ if confirmed
87
+
88
+ confirmed = false
89
+ next
90
+
91
+ end
92
+
93
+ # Opss... user need to provide a required option.
94
+
95
+ raise OptionError.new('Option ' + name.to_s + ' is necessary')
96
+
97
+ end
98
+
99
+ end
100
+
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,72 @@
1
+ module Imparcial
2
+ module Driver
3
+ module ExpressionBase
4
+ module Delete
5
+
6
+ private
7
+
8
+ def expected_options_for_deleting
9
+
10
+ {:table_name => :required, :conditions => :required}
11
+
12
+ end
13
+
14
+ def expected_options_for_deleting_all
15
+
16
+ {:table_name => :required}
17
+
18
+ end
19
+
20
+ public
21
+
22
+ # Delete some records. Except, this function only supports
23
+ # deleting with conditions. Otherwise an exception shall be
24
+ # raised.
25
+
26
+ def delete ( options = {} )
27
+
28
+ check_options expected_options_for_deleting, options
29
+
30
+ query sql_for_deleting( options )
31
+
32
+ rescue adapter_specific_exception => ex
33
+
34
+ raise DeleteError.new(ex.message)
35
+
36
+ end
37
+
38
+ # Delete all records. This function doesn't support conditions.
39
+ # If you provide one, an exception shall be raised.
40
+
41
+ def delete_all ( options = {} )
42
+
43
+ check_options expected_options_for_deleting_all, options
44
+
45
+ query sql_for_deleting( options )
46
+
47
+ rescue adapter_specific_exception => ex
48
+
49
+ raise DeleteError.new(ex.message)
50
+
51
+ end
52
+
53
+ # Delete records without raising any exception.
54
+
55
+ def delete_if_necessary ( options = {} )
56
+
57
+ delete options
58
+
59
+ rescue DeleteError; end
60
+
61
+ # Delete all records without raising any exception.
62
+
63
+ def delete_all_if_necessary ( options = {} )
64
+
65
+ delete_all options
66
+
67
+ rescue DeleteError; end
68
+
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,199 @@
1
+ module Imparcial
2
+ module Driver
3
+ module ExpressionBase
4
+ module Index
5
+
6
+ private
7
+
8
+ def expected_options_for_creating_index
9
+
10
+ {
11
+ :index_name => :required, :index_type => :optional, :table_name => :required,
12
+ :column_name => :required
13
+ }
14
+
15
+ end
16
+
17
+ def expected_options_for_dropping_index
18
+
19
+ {:index_name => :required, :table_name => :required}
20
+
21
+ end
22
+
23
+ def expected_options_for_index_table
24
+
25
+ {:table_name => :required}
26
+
27
+ end
28
+
29
+ def expected_options_for_verifying_index
30
+
31
+ {:index_name => :required}
32
+
33
+ end
34
+
35
+ ###########################################
36
+ # #
37
+ # Index Creation #
38
+ # #
39
+ ###########################################
40
+
41
+ public
42
+
43
+ # Create an index.
44
+
45
+ def create_index ( options = {} )
46
+
47
+ check_options expected_options_for_creating_index, options
48
+
49
+ query sql_for_creating_index( options )
50
+
51
+ rescue adapter_specific_exception => ex
52
+
53
+ raise IndexCreateError.new(ex.message)
54
+
55
+ end
56
+
57
+ ###########################################
58
+ # #
59
+ # Index Dropping #
60
+ # #
61
+ ###########################################
62
+
63
+ public
64
+
65
+ # Drop an index by name.
66
+
67
+ def drop_index ( options = {} )
68
+
69
+ check_options expected_options_for_dropping_index, options
70
+
71
+ query sql_for_dropping_index( options )
72
+
73
+ rescue adapter_specific_exception => ex
74
+
75
+ raise IndexDropError.new(ex.message)
76
+
77
+ end
78
+
79
+ # Drop all indexes for a specific table.
80
+
81
+ def drop_all_indexes_for_table ( options = {} )
82
+
83
+ check_options expected_options_for_index_table, options
84
+
85
+ syntax = ''
86
+
87
+ for index in retrieve_indexes_for_table options
88
+
89
+ syntax += sql_for_dropping_index(:index_name => index[:name]) + ';'
90
+
91
+ end
92
+
93
+ return if syntax.length == 0
94
+
95
+ query syntax
96
+
97
+ rescue adapter_specific_exception => ex
98
+
99
+ raise IndexDropError.new(ex.message)
100
+
101
+ end
102
+
103
+ # Drop all indexes for all tables.
104
+
105
+ def drop_all_indexes
106
+
107
+ syntax = ''
108
+
109
+ for index in retrieve_indexes
110
+
111
+ meta = {:index_name => index[:name]}
112
+ syntax += sql_for_dropping_index(meta) + ';'
113
+
114
+ end
115
+
116
+ return if syntax.length == 0
117
+
118
+ query syntax
119
+
120
+ rescue adapter_specific_exception => ex
121
+
122
+ raise IndexDropError.new(ex.message)
123
+
124
+ end
125
+
126
+ ###########################################
127
+ # #
128
+ # Index Features #
129
+ # #
130
+ ###########################################
131
+
132
+ public
133
+
134
+ # Retrieve all indexes avaliable.
135
+
136
+ def retrieve_indexes
137
+
138
+ query sql_for_retrieving_indexes
139
+
140
+ indexes = []
141
+
142
+ result.fetch do |table, column, index|
143
+
144
+ indexes << {:table => table.value,:column => column.value,:name => index.value}
145
+
146
+ end
147
+
148
+ indexes
149
+
150
+ rescue adapter_specific_exception => ex
151
+
152
+ raise IndexError.new(ex.message)
153
+
154
+ end
155
+
156
+ # Retrieve all indexes for a specific table.
157
+
158
+ def retrieve_indexes_for_table ( options = {} )
159
+
160
+ check_options expected_options_for_index_table, options
161
+
162
+ query sql_for_retrieving_indexes_for_table( options )
163
+
164
+ indexes = []
165
+
166
+ result.fetch do |table, column, index|
167
+
168
+ indexes << {:table => table.value,:column => column.value,:name => index.value}
169
+
170
+ end
171
+
172
+ indexes
173
+
174
+ rescue adapter_specific_exception => ex
175
+
176
+ raise IndexDropError.new(ex.message)
177
+
178
+ end
179
+
180
+ # Verify if a given index exists.
181
+
182
+ def index_exists? ( options = {} )
183
+
184
+ check_options expected_options_for_verifying_index, options
185
+
186
+ query sql_for_index_exists?( options )
187
+
188
+ result.rows > 0
189
+
190
+ rescue adapter_specific_exception => ex
191
+
192
+ raise IndexDropError.new(ex.message)
193
+
194
+ end
195
+
196
+ end
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,33 @@
1
+ module Imparcial
2
+ module Driver
3
+ module ExpressionBase
4
+ module Insert
5
+
6
+ private
7
+
8
+ def expected_options_for_inserting
9
+
10
+ {:table_name => :required, :values => :required}
11
+
12
+ end
13
+
14
+ public
15
+
16
+ # Insert records in a table. Values are must.
17
+
18
+ def insert ( options = {} )
19
+
20
+ check_options expected_options_for_inserting, options
21
+
22
+ query sql_for_inserting( options )
23
+
24
+ rescue adapter_specific_exception => ex
25
+
26
+ raise InsertError.new(ex.message)
27
+
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ module Imparcial
2
+ module Driver
3
+ module ExpressionBase
4
+ module Lock
5
+
6
+ # Need implementation.
7
+
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ module Imparcial
2
+ module Driver
3
+ module ExpressionBase
4
+ module Select
5
+
6
+ private
7
+
8
+ def expected_options_for_selecting
9
+
10
+ {
11
+ :table_name => :optional, :joins => :optional, :fields => :optional,
12
+ :conditions => :optional, :limit => :optional, :order_asc => :optional,
13
+ :order_desc => :optional
14
+ }
15
+
16
+ end
17
+
18
+
19
+ public
20
+
21
+ def select ( options = {} )
22
+
23
+ options[:amount] ||= :all
24
+
25
+ query sql_for_selecting( options )
26
+
27
+ rescue adapter_specific_exception => ex
28
+
29
+ raise SelectError.new(ex.message)
30
+
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,189 @@
1
+ module Imparcial
2
+ module Driver
3
+ module ExpressionBase
4
+
5
+ # It's important to be aware that some databases don't support sequences.
6
+ # Databases like Mysql, need to override those methods.
7
+
8
+ module Sequence
9
+
10
+ private
11
+
12
+ def parse_sequence ( options )
13
+
14
+ options[:start_with] ||= 1
15
+ options[:min_value] ||= 1
16
+ options[:increment_by] ||= 1
17
+ options[:cycle] ||= false
18
+
19
+ end
20
+
21
+ def expected_options_for_creating_sequence
22
+
23
+ {
24
+ :sequence_name => :required, :start_with => :optional, :min_value => :optional,
25
+ :max_value => :optional, :increment_by => :optional, :cycle => :optional,
26
+ :cache => :optional
27
+ }
28
+
29
+ end
30
+
31
+ def expected_options_for_dropping_sequence
32
+
33
+ {:sequence_name => :required}
34
+
35
+ end
36
+
37
+ def expected_options_for_verifying_sequence_existance
38
+
39
+ {:sequence_name => :required}
40
+
41
+ end
42
+
43
+ ###############################################
44
+ # #
45
+ # Creating Sequences #
46
+ # #
47
+ ###############################################
48
+
49
+ public
50
+
51
+ # Create a sequence with options.
52
+
53
+ def create_sequence ( options = {} )
54
+
55
+ check_options expected_options_for_creating_sequence, options
56
+
57
+ parse_sequence options
58
+
59
+ begin
60
+
61
+ query sql_for_creating_sequence( options )
62
+
63
+ rescue adapter_specific_exception => ex
64
+
65
+ raise SequenceCreateError.new(ex.message)
66
+
67
+ end
68
+
69
+ end
70
+
71
+ def create_sequence_if_necessary ( options = {} )
72
+
73
+ create_sequence options
74
+
75
+ rescue SequenceCreateError => ex; end
76
+
77
+ ###############################################
78
+ # #
79
+ # Dropping Sequences #
80
+ # #
81
+ ###############################################
82
+
83
+ public
84
+
85
+ # Drop a sequence.
86
+
87
+ def drop_sequence ( options = {} )
88
+
89
+ check_options expected_options_for_dropping_sequence, options
90
+
91
+ query sql_for_dropping_sequence( options )
92
+
93
+ rescue adapter_specific_exception => ex
94
+
95
+ raise SequenceDropError.new(ex.message)
96
+
97
+ end
98
+
99
+ # Drop a sequence raising no error.
100
+
101
+ def drop_sequence_if_necessary ( options = {} )
102
+
103
+ drop_sequence options
104
+
105
+ rescue SequenceDropError; end
106
+
107
+
108
+ # Drop all sequences.
109
+
110
+ def drop_all_sequences
111
+
112
+ sequences = retrieve_sequences
113
+
114
+ syntax = ''
115
+
116
+ for sequence in sequences
117
+
118
+ syntax += sql_for_dropping_sequence( :sequence_name => sequence ) + ';'
119
+
120
+ end
121
+
122
+ return if syntax.length == 0
123
+
124
+ query syntax
125
+
126
+ rescue adapter_specific_exception => ex
127
+
128
+ raise SequenceError.new(ex.message)
129
+
130
+ end
131
+
132
+ ###############################################
133
+ # #
134
+ # Advanced Features #
135
+ # #
136
+ ###############################################
137
+
138
+ public
139
+
140
+ # Ask for a sequence returning true or false
141
+
142
+ def sequence_exists? ( options = {} )
143
+
144
+ check_options expected_options_for_verifying_sequence_existance, options
145
+
146
+ begin
147
+
148
+ query sql_for_sequence_exists?( options )
149
+
150
+ rescue adapter_specific_exception => ex
151
+
152
+ raise SequenceError.new(ex.message)
153
+
154
+ end
155
+
156
+ result.rows > 0
157
+
158
+ end
159
+
160
+ # Retrieve all sequences. Similar to retrive_tables.
161
+
162
+ def retrieve_sequences
163
+
164
+ begin
165
+
166
+ query sql_for_retrieving_sequences
167
+
168
+ rescue adapter_specific_exception => ex
169
+
170
+ raise SequenceError.new(ex.message)
171
+
172
+ end
173
+
174
+ sequences = []
175
+
176
+ result.fetch do |name|
177
+
178
+ sequences << name.value
179
+
180
+ end
181
+
182
+ sequences
183
+
184
+ end
185
+
186
+ end
187
+ end
188
+ end
189
+ end