activerecord-rdb-adapter 0.9.4 → 0.9.6.beta2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,28 +1,28 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- module Rdb
4
- module ColumnMethods # :nodoc:
5
-
6
- attr_accessor :needs_sequence
7
-
8
- def primary_key(name, type = :primary_key, **options)
9
- self.needs_sequence = true
10
- super
11
- end
12
-
13
- end
14
-
15
- class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition # :nodoc:
16
- include ColumnMethods
17
-
18
- def new_column_definition(name, type, **options)
19
- super
20
- end
21
- end
22
-
23
- class Table < ActiveRecord::ConnectionAdapters::Table # :nodoc:
24
- include ColumnMethods
25
- end
26
- end
27
- end
28
- end
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module Rdb
4
+ module ColumnMethods # :nodoc:
5
+
6
+ attr_accessor :needs_sequence
7
+
8
+ def primary_key(name, type = :primary_key, **options)
9
+ self.needs_sequence = true
10
+ super
11
+ end
12
+
13
+ end
14
+
15
+ class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition # :nodoc:
16
+ include ColumnMethods
17
+
18
+ def new_column_definition(name, type, **options)
19
+ super
20
+ end
21
+ end
22
+
23
+ class Table < ActiveRecord::ConnectionAdapters::Table # :nodoc:
24
+ include ColumnMethods
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,163 +1,160 @@
1
- require 'fb'
2
- require 'base64'
3
- require 'arel'
4
- require 'arel/visitors/rdb_visitor'
5
-
6
- require 'active_record'
7
- require 'active_record/base'
8
- require 'active_record/connection_adapters/abstract_adapter'
9
- require 'active_record/connection_adapters/rdb/database_statements'
10
- require 'active_record/connection_adapters/rdb/database_limits'
11
- require 'active_record/connection_adapters/rdb/schema_creation'
12
- require 'active_record/connection_adapters/rdb/schema_dumper'
13
- require 'active_record/connection_adapters/rdb/schema_statements'
14
- require 'active_record/connection_adapters/rdb/quoting'
15
- require 'active_record/connection_adapters/rdb/table_definition'
16
- require 'active_record/connection_adapters/rdb_column'
17
- require 'active_record/rdb_base'
18
-
19
- module ActiveRecord
20
- module ConnectionAdapters
21
- class RdbAdapter < AbstractAdapter # :nodoc:
22
- include Rdb::DatabaseLimits
23
- include Rdb::DatabaseStatements
24
- include Rdb::Quoting
25
- include Rdb::SchemaStatements
26
-
27
- @@default_transaction_isolation = :read_committed
28
- cattr_accessor :default_transaction_isolation
29
-
30
- ADAPTER_NAME = 'rdb'.freeze
31
-
32
- def initialize(connection, logger = nil, config = {})
33
- super(connection, logger, config)
34
- # Our Responsibility
35
- @config = config
36
- @visitor = Arel::Visitors::Rdb.new self
37
- @prepared_statements = true
38
- @visitor.extend(DetermineIfPreparableVisitor)
39
- end
40
-
41
- def arel_visitor
42
- Arel::Visitors::Rdb.new self
43
- end
44
-
45
- def valid_type?(type)
46
- !native_database_types[type].nil? || !native_database_types[type.type].nil?
47
- end
48
-
49
- def adapter_name
50
- ADAPTER_NAME
51
- end
52
-
53
- def schema_creation
54
- Rdb::SchemaCreation.new self
55
- end
56
-
57
- def supports_migrations?
58
- true
59
- end
60
-
61
- def supports_primary_key?
62
- true
63
- end
64
-
65
- def supports_count_distinct?
66
- true
67
- end
68
-
69
- def supports_ddl_transactions?
70
- true
71
- end
72
-
73
- def supports_transaction_isolation?
74
- true
75
- end
76
-
77
- def supports_savepoints?
78
- true
79
- end
80
-
81
- def prefetch_primary_key?(_table_name = nil)
82
- true
83
- end
84
-
85
- def ids_in_list_limit
86
- 1499
87
- end
88
-
89
- def supports_multi_insert?
90
- false
91
- end
92
-
93
- def active?
94
- return false unless @connection.open?
95
- # return true if @connection.transaction_started
96
- @connection.query('SELECT 1 FROM RDB$DATABASE')
97
- true
98
- rescue StandardError
99
- false
100
- end
101
-
102
- def reconnect!
103
- disconnect!
104
- @connection = ::Fb::Database.connect(@config)
105
- end
106
-
107
- def disconnect!
108
- super
109
- begin
110
- @connection.close
111
- rescue StandardError
112
- nil
113
- end
114
- end
115
-
116
- def reset!
117
- reconnect!
118
- end
119
-
120
- def requires_reloading?
121
- false
122
- end
123
-
124
- def create_savepoint(name = current_savepoint_name)
125
- execute("SAVEPOINT #{name}")
126
- end
127
-
128
- def rollback_to_savepoint(name = current_savepoint_name)
129
- execute("ROLLBACK TO SAVEPOINT #{name}")
130
- end
131
-
132
- def release_savepoint(name = current_savepoint_name)
133
- execute("RELEASE SAVEPOINT #{name}")
134
- end
135
-
136
- protected
137
-
138
- def initialize_type_map(map)
139
- super
140
- map.register_type(/timestamp/i, Type::DateTime.new)
141
- map.alias_type(/blob sub_type text/i, 'text')
142
- end
143
-
144
- def translate_exception(e, message)
145
- case e.message
146
- when /violation of FOREIGN KEY constraint/
147
- ActiveRecord::InvalidForeignKey.new(message)
148
- when /violation of PRIMARY or UNIQUE KEY constraint/, /attempt to store duplicate value/
149
- ActiveRecord::RecordNotUnique.new(message)
150
- when /This operation is not defined for system tables/
151
- ActiveRecord::ActiveRecordError.new(message)
152
- when /Column does not belong to referenced table/,
153
- /Unsuccessful execution caused by system error that does not preclude successful execution of subsequent statements/,
154
- /The cursor identified in the UPDATE or DELETE statement is not positioned on a row/,
155
- /Overflow occurred during data type conversion/
156
- ActiveRecord::StatementInvalid.new(message)
157
- else
158
- super
159
- end
160
- end
161
- end
162
- end
163
- end
1
+ require 'fb'
2
+ require 'base64'
3
+ require 'arel'
4
+ require 'arel/visitors/rdb_visitor'
5
+
6
+ require 'active_record'
7
+ require 'active_record/base'
8
+ require 'active_record/connection_adapters/abstract_adapter'
9
+ require 'active_record/connection_adapters/rdb/database_statements'
10
+ require 'active_record/connection_adapters/rdb/database_limits'
11
+ require 'active_record/connection_adapters/rdb/schema_creation'
12
+ require 'active_record/connection_adapters/rdb/schema_dumper'
13
+ require 'active_record/connection_adapters/rdb/schema_statements'
14
+ require 'active_record/connection_adapters/rdb/quoting'
15
+ require 'active_record/connection_adapters/rdb/table_definition'
16
+ require 'active_record/connection_adapters/rdb_column'
17
+ require 'active_record/rdb_base'
18
+
19
+ module ActiveRecord
20
+ module ConnectionAdapters
21
+ class RdbAdapter < AbstractAdapter # :nodoc:
22
+ include Rdb::DatabaseLimits
23
+ include Rdb::DatabaseStatements
24
+ include Rdb::Quoting
25
+ include Rdb::SchemaStatements
26
+
27
+ @@default_transaction_isolation = :read_committed
28
+ cattr_accessor :default_transaction_isolation
29
+
30
+ ADAPTER_NAME = 'rdb'.freeze
31
+
32
+ def initialize(connection, logger = nil, config = {})
33
+ super(connection, logger, config)
34
+ # Our Responsibility
35
+ @config = config
36
+ end
37
+
38
+ def arel_visitor
39
+ Arel::Visitors::Rdb.new self
40
+ end
41
+
42
+ def valid_type?(type)
43
+ !native_database_types[type].nil? || !native_database_types[type.type].nil?
44
+ end
45
+
46
+ def adapter_name
47
+ ADAPTER_NAME
48
+ end
49
+
50
+ def schema_creation
51
+ Rdb::SchemaCreation.new self
52
+ end
53
+
54
+ def supports_migrations?
55
+ true
56
+ end
57
+
58
+ def supports_primary_key?
59
+ true
60
+ end
61
+
62
+ def supports_count_distinct?
63
+ true
64
+ end
65
+
66
+ def supports_ddl_transactions?
67
+ true
68
+ end
69
+
70
+ def supports_transaction_isolation?
71
+ true
72
+ end
73
+
74
+ def supports_savepoints?
75
+ true
76
+ end
77
+
78
+ def prefetch_primary_key?(_table_name = nil)
79
+ true
80
+ end
81
+
82
+ def ids_in_list_limit
83
+ 1499
84
+ end
85
+
86
+ def supports_multi_insert?
87
+ false
88
+ end
89
+
90
+ def active?
91
+ return false unless @connection.open?
92
+
93
+ @connection.query('SELECT 1 FROM RDB$DATABASE')
94
+ true
95
+ rescue StandardError
96
+ false
97
+ end
98
+
99
+ def reconnect!
100
+ disconnect!
101
+ @connection = ::Fb::Database.connect(@config)
102
+ end
103
+
104
+ def disconnect!
105
+ super
106
+ begin
107
+ @connection.close
108
+ rescue StandardError
109
+ nil
110
+ end
111
+ end
112
+
113
+ def reset!
114
+ reconnect!
115
+ end
116
+
117
+ def requires_reloading?
118
+ false
119
+ end
120
+
121
+ def create_savepoint(name = current_savepoint_name)
122
+ execute("SAVEPOINT #{name}")
123
+ end
124
+
125
+ def rollback_to_savepoint(name = current_savepoint_name)
126
+ execute("ROLLBACK TO SAVEPOINT #{name}")
127
+ end
128
+
129
+ def release_savepoint(name = current_savepoint_name)
130
+ execute("RELEASE SAVEPOINT #{name}")
131
+ end
132
+
133
+ protected
134
+
135
+ def initialize_type_map(map)
136
+ super
137
+ map.register_type(/timestamp/i, Type::DateTime.new)
138
+ map.alias_type(/blob sub_type text/i, 'text')
139
+ end
140
+
141
+ def translate_exception(e, message)
142
+ case e.message
143
+ when /violation of FOREIGN KEY constraint/
144
+ ActiveRecord::InvalidForeignKey.new(message)
145
+ when /violation of PRIMARY or UNIQUE KEY constraint/, /attempt to store duplicate value/
146
+ ActiveRecord::RecordNotUnique.new(message)
147
+ when /This operation is not defined for system tables/
148
+ ActiveRecord::ActiveRecordError.new(message)
149
+ when /Column does not belong to referenced table/,
150
+ /Unsuccessful execution caused by system error that does not preclude successful execution of subsequent statements/,
151
+ /The cursor identified in the UPDATE or DELETE statement is not positioned on a row/,
152
+ /Overflow occurred during data type conversion/
153
+ ActiveRecord::StatementInvalid.new(message)
154
+ else
155
+ super
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
@@ -1,69 +1,69 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- class RdbColumn < Column # :nodoc:
4
- class << self
5
- def sql_type_for(field)
6
- sql_type = field[:sql_type]
7
- sub_type = field[:sql_subtype]
8
-
9
- sql_type << case sql_type
10
- when /(numeric|decimal)/i
11
- "(#{field[:precision]},#{field[:scale].abs})"
12
- when /(int|float|double|char|varchar|bigint)/i
13
- "(#{field[:length]})"
14
- else
15
- ''
16
- end
17
-
18
- sql_type << ' sub_type text' if /blob/i.match?(sql_type) && sub_type == 1
19
- sql_type
20
- end
21
- end
22
-
23
- attr_reader :sub_type, :domain
24
-
25
- def initialize(name, default, sql_type_metadata = nil, null = true, table_name = nil, rdb_options = {})
26
- @domain, @sub_type = rdb_options.values_at(:domain, :sub_type)
27
- name = name.dup
28
- name.downcase!
29
- super(name, parse_default(default), sql_type_metadata, null, table_name)
30
- end
31
-
32
- def sql_type
33
- @sql_type_metadata[:sql_type]
34
- end
35
-
36
- def type
37
- @sql_type_metadata[:type]
38
- end
39
-
40
- def precision
41
- @sql_type_metadata[:precision]
42
- end
43
-
44
- def scale
45
- @sql_type_metadata[:scale]
46
- end
47
-
48
- def limit
49
- @sql_type_metadata[:limit]
50
- end
51
-
52
- private
53
-
54
- def parse_default(default)
55
- return if default.nil? || /null/i.match?(default)
56
- d = default.dup
57
- d.gsub!(/^\s*DEFAULT\s+/i, '')
58
- d.gsub!(/(^'|'$)/, '')
59
- d
60
- end
61
-
62
- def simplified_type(field_type)
63
- return :datetime if /timestamp/i.match?(field_type)
64
- return :text if /blob sub_type text/i.match?(field_type)
65
- super
66
- end
67
- end
68
- end
69
- end
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class RdbColumn < Column # :nodoc:
4
+ class << self
5
+ def sql_type_for(field)
6
+ sql_type = field[:sql_type]
7
+ sub_type = field[:sql_subtype]
8
+
9
+ sql_type << case sql_type
10
+ when /(numeric|decimal)/i
11
+ "(#{field[:precision]},#{field[:scale].abs})"
12
+ when /(int|float|double|char|varchar|bigint)/i
13
+ "(#{field[:length]})"
14
+ else
15
+ ''
16
+ end
17
+
18
+ sql_type << ' sub_type text' if /blob/i.match?(sql_type) && sub_type == 1
19
+ sql_type
20
+ end
21
+ end
22
+
23
+ attr_reader :sub_type, :domain
24
+
25
+ def initialize(name, default, sql_type_metadata = nil, null = true, table_name = nil, rdb_options = {})
26
+ @domain, @sub_type = rdb_options.values_at(:domain, :sub_type)
27
+ name = name.dup
28
+ name.downcase!
29
+ super(name, parse_default(default), sql_type_metadata, null, table_name)
30
+ end
31
+
32
+ def sql_type
33
+ @sql_type_metadata[:sql_type]
34
+ end
35
+
36
+ def type
37
+ @sql_type_metadata[:type]
38
+ end
39
+
40
+ def precision
41
+ @sql_type_metadata[:precision]
42
+ end
43
+
44
+ def scale
45
+ @sql_type_metadata[:scale]
46
+ end
47
+
48
+ def limit
49
+ @sql_type_metadata[:limit]
50
+ end
51
+
52
+ private
53
+
54
+ def parse_default(default)
55
+ return if default.nil? || /null/i.match?(default)
56
+ d = default.dup
57
+ d.gsub!(/^\s*DEFAULT\s+/i, '')
58
+ d.gsub!(/(^'|'$)/, '')
59
+ d
60
+ end
61
+
62
+ def simplified_type(field_type)
63
+ return :datetime if /timestamp/i.match?(field_type)
64
+ return :text if /blob sub_type text/i.match?(field_type)
65
+ super
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,34 +1,34 @@
1
- module ActiveRecord
2
- module ConnectionHandling # :nodoc:
3
- def rdb_connection(config)
4
- require 'fb'
5
- config = rdb_connection_config(config)
6
- db = ::Fb::Database.new(config)
7
- @database = db
8
- begin
9
- connection = db.connect
10
- rescue StandardError
11
- unless config[:create]
12
- require 'pp'
13
- pp config
14
- raise ConnectionNotEstablished, 'No Firebird connections established.'
15
- end
16
- connection = db.create.connect
17
- end
18
- ConnectionAdapters::RdbAdapter.new(connection, logger, config)
19
- end
20
-
21
- def rdb_connection_config(config)
22
- config = config.symbolize_keys.dup.reverse_merge(downcase_names: true)
23
- port = config[:port] || 3050
24
- raise ArgumentError, 'No database specified. Missing argument: database.' unless config[:database]
25
- config[:database] = File.expand_path(config[:database], defined?(Rails) && Rails.root) if config[:host].nil? || /localhost/i.match?(config[:host])
26
- config[:database] = "#{config[:host]}/#{port}:#{config[:database]}" if config[:host]
27
- # config[:charset] = config[:charset].gsub(/-/, '') if config[:charset]
28
- # config[:encoding] = config[:encoding].gsub(/-/, '') if config[:encoding]
29
- config[:page_size] = 8192 unless config[:page_size]
30
- config[:readonly_selects] = true unless config[:readonly_selects].present?
31
- config
32
- end
33
- end
34
- end
1
+ module ActiveRecord
2
+ module ConnectionHandling # :nodoc:
3
+ def rdb_connection(config)
4
+ require 'fb'
5
+ config = rdb_connection_config(config)
6
+ db = ::Fb::Database.new(config)
7
+ @database = db
8
+ begin
9
+ connection = db.connect
10
+ rescue StandardError
11
+ unless config[:create]
12
+ require 'pp'
13
+ pp config
14
+ raise ConnectionNotEstablished, 'No Firebird connections established.'
15
+ end
16
+ connection = db.create.connect
17
+ end
18
+ ConnectionAdapters::RdbAdapter.new(connection, logger, config)
19
+ end
20
+
21
+ def rdb_connection_config(config)
22
+ config = config.symbolize_keys.dup.reverse_merge(downcase_names: true)
23
+ port = config[:port] || 3050
24
+ raise ArgumentError, 'No database specified. Missing argument: database.' unless config[:database]
25
+ config[:database] = File.expand_path(config[:database], defined?(Rails) && Rails.root) if config[:host].nil? || /localhost/i.match?(config[:host])
26
+ config[:database] = "#{config[:host]}/#{port}:#{config[:database]}" if config[:host]
27
+ # config[:charset] = config[:charset].gsub(/-/, '') if config[:charset]
28
+ # config[:encoding] = config[:encoding].gsub(/-/, '') if config[:encoding]
29
+ config[:page_size] = 8192 unless config[:page_size]
30
+ config[:readonly_selects] = true unless config[:readonly_selects].present?
31
+ config
32
+ end
33
+ end
34
+ end