firebird_adapter 1.0.7 → 1.0.8
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.
- checksums.yaml +4 -4
- data/lib/active_record/connection_adapters/firebird/connection.rb +2 -1
- data/lib/active_record/connection_adapters/firebird/database_statements.rb +15 -13
- data/lib/active_record/connection_adapters/firebird/quoting.rb +5 -0
- data/lib/active_record/connection_adapters/firebird/schema_statements.rb +99 -0
- data/lib/active_record/connection_adapters/firebird_adapter.rb +7 -1
- data/lib/active_record/internal_metadata_extensions.rb +31 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84e152eafb3ac4ec3815a9db822e0af3f78ef8b2bebc6a9ab146c0d8d6f2db2a
|
4
|
+
data.tar.gz: 7be80a59b8b9783d5406f6c020fdcb6456d5d33bf60d3836bb92587d07baaf52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdb14f1266f7de57c93afe4e7efcbd37d4e2b0e46421e97061cdeaf8858fee054b2c0dc58ed03176c4374bddb06cf7d25b1bee04efe77d1fc37fef321c1f2839
|
7
|
+
data.tar.gz: 77f23df54f7a892117b9fe5976c95857db49dd3471c52a62bc9904aaf24bfc61c1e5ecb726c3b093a066a1b73d63875aa42f787676d7073e36f3b78b523c9bf7
|
@@ -1,7 +1,8 @@
|
|
1
1
|
module ActiveRecord::ConnectionHandling
|
2
2
|
def firebird_connection(config)
|
3
3
|
require 'active_record/extensions'
|
4
|
-
|
4
|
+
require 'active_record/internal_metadata_extensions'
|
5
|
+
|
5
6
|
config = config.symbolize_keys.dup.reverse_merge(downcase_names: true, port: 3050, encoding: ActiveRecord::ConnectionAdapters::FirebirdAdapter::DEFAULT_ENCODING)
|
6
7
|
|
7
8
|
if config[:host]
|
@@ -35,22 +35,24 @@ module ActiveRecord::ConnectionAdapters::Firebird::DatabaseStatements
|
|
35
35
|
|
36
36
|
log(sql, name, binds, type_casted_binds) do
|
37
37
|
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
38
|
+
begin
|
39
|
+
result = @connection.execute(sql, *type_casted_binds)
|
40
|
+
if result.is_a?(Fb::Cursor)
|
41
|
+
fields = result.fields.map(&:name)
|
42
|
+
rows = result.fetchall.map do |row|
|
43
|
+
row.map do |col|
|
44
|
+
col.encode('UTF-8', @connection.encoding) rescue col
|
45
|
+
end
|
44
46
|
end
|
45
|
-
end
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
result.close
|
49
|
+
ActiveRecord::Result.new(fields, rows)
|
50
|
+
else
|
51
|
+
result
|
52
|
+
end
|
53
|
+
rescue Exception => e
|
54
|
+
raise e.message.encode('UTF-8', @connection.encoding)
|
51
55
|
end
|
52
|
-
rescue Exception => e
|
53
|
-
raise e.message.encode('UTF-8', @connection.encoding)
|
54
56
|
end
|
55
57
|
end
|
56
58
|
end
|
@@ -8,6 +8,105 @@ module ActiveRecord::ConnectionAdapters::Firebird::SchemaStatements
|
|
8
8
|
@connection.view_names
|
9
9
|
end
|
10
10
|
|
11
|
+
def indexes(table_name)
|
12
|
+
result = query(<<~SQL, "SCHEMA")
|
13
|
+
SELECT
|
14
|
+
rdb$index_name,
|
15
|
+
rdb$unique_flag
|
16
|
+
FROM
|
17
|
+
rdb$indices
|
18
|
+
WHERE
|
19
|
+
rdb$relation_name = '#{table_name.upcase}';
|
20
|
+
SQL
|
21
|
+
|
22
|
+
result.map do |row|
|
23
|
+
index_name = row[0].strip
|
24
|
+
unique = row[1] == 1
|
25
|
+
columns = query_values(<<~SQL, "SCHEMA")
|
26
|
+
SELECT
|
27
|
+
rdb$field_name
|
28
|
+
FROM
|
29
|
+
rdb$index_segments
|
30
|
+
WHERE
|
31
|
+
rdb$index_name = '#{index_name}'
|
32
|
+
ORDER BY
|
33
|
+
rdb$field_position
|
34
|
+
SQL
|
35
|
+
|
36
|
+
ActiveRecord::ConnectionAdapters::IndexDefinition.new(
|
37
|
+
table_name.downcase,
|
38
|
+
index_name.downcase,
|
39
|
+
unique,
|
40
|
+
columns.map(&:strip).map(&:downcase),
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def remove_index(table_name, options = {})
|
46
|
+
index_name = index_name_for_remove(table_name, options)
|
47
|
+
execute "DROP INDEX #{quote_column_name(index_name)}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def foreign_keys(table_name)
|
51
|
+
result = query(<<-SQL.strip_heredoc, "SCHEMA")
|
52
|
+
WITH FK_FIELDS AS (
|
53
|
+
SELECT
|
54
|
+
AA2.RDB$RELATION_NAME,
|
55
|
+
AA2.RDB$CONSTRAINT_NAME,
|
56
|
+
BB2.RDB$CONST_NAME_UQ AS LINK_UK_OR_PK,
|
57
|
+
EE2.RDB$RELATION_NAME AS REFERENCE_TABLE,
|
58
|
+
CC2.RDB$FIELD_NAME,
|
59
|
+
AA2.RDB$CONSTRAINT_TYPE
|
60
|
+
FROM
|
61
|
+
RDB$RELATION_CONSTRAINTS AA2
|
62
|
+
LEFT JOIN RDB$REF_CONSTRAINTS BB2 ON BB2.RDB$CONSTRAINT_NAME = AA2.RDB$CONSTRAINT_NAME
|
63
|
+
LEFT JOIN RDB$INDEX_SEGMENTS CC2 ON CC2.RDB$INDEX_NAME = AA2.RDB$INDEX_NAME
|
64
|
+
LEFT JOIN RDB$RELATION_FIELDS DD2 ON DD2.RDB$FIELD_NAME = CC2.RDB$FIELD_NAME AND DD2.RDB$RELATION_NAME = AA2.RDB$RELATION_NAME
|
65
|
+
LEFT JOIN RDB$RELATION_CONSTRAINTS EE2 ON EE2.RDB$CONSTRAINT_NAME = BB2.RDB$CONST_NAME_UQ
|
66
|
+
)
|
67
|
+
SELECT
|
68
|
+
AA.RDB$CONSTRAINT_NAME,
|
69
|
+
BB.REFERENCE_TABLE,
|
70
|
+
BB.FIELDS,
|
71
|
+
BB.REFERENCE_FIELDS
|
72
|
+
FROM
|
73
|
+
RDB$RELATION_CONSTRAINTS AA
|
74
|
+
LEFT JOIN (
|
75
|
+
SELECT
|
76
|
+
AA1.RDB$RELATION_NAME,
|
77
|
+
AA1.RDB$CONSTRAINT_NAME,
|
78
|
+
AA1.LINK_UK_OR_PK,
|
79
|
+
AA1.REFERENCE_TABLE,
|
80
|
+
(
|
81
|
+
SELECT LIST(TRIM(AA3.RDB$FIELD_NAME), ', ') FROM FK_FIELDS AA3 WHERE AA3.RDB$CONSTRAINT_NAME = AA1.RDB$CONSTRAINT_NAME ROWS 1
|
82
|
+
) AS FIELDS,
|
83
|
+
(
|
84
|
+
SELECT LIST(TRIM(AA4.RDB$FIELD_NAME), ', ') FROM FK_FIELDS AA4 WHERE AA4.RDB$CONSTRAINT_NAME = AA1.LINK_UK_OR_PK ROWS 1
|
85
|
+
) AS REFERENCE_FIELDS
|
86
|
+
FROM
|
87
|
+
FK_FIELDS AA1
|
88
|
+
GROUP BY
|
89
|
+
AA1.RDB$RELATION_NAME,
|
90
|
+
AA1.RDB$CONSTRAINT_NAME,
|
91
|
+
AA1.REFERENCE_TABLE,
|
92
|
+
AA1.LINK_UK_OR_PK
|
93
|
+
) BB ON BB.RDB$RELATION_NAME = AA.RDB$RELATION_NAME AND BB.RDB$CONSTRAINT_NAME = AA.RDB$CONSTRAINT_NAME
|
94
|
+
WHERE
|
95
|
+
AA.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
|
96
|
+
AND AA.RDB$RELATION_NAME = '#{table_name.upcase}';
|
97
|
+
SQL
|
98
|
+
|
99
|
+
result.map do |row|
|
100
|
+
options = {
|
101
|
+
column: row[2].downcase,
|
102
|
+
name: row[0].strip.downcase,
|
103
|
+
primary_key: row[3].downcase
|
104
|
+
}
|
105
|
+
|
106
|
+
ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(table_name, row[1].strip.downcase, options)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
11
110
|
private
|
12
111
|
|
13
112
|
def column_definitions(table_name)
|
@@ -4,6 +4,7 @@ require 'active_record/connection_adapters/firebird/connection'
|
|
4
4
|
require 'active_record/connection_adapters/firebird/database_limits'
|
5
5
|
require 'active_record/connection_adapters/firebird/database_statements'
|
6
6
|
require 'active_record/connection_adapters/firebird/schema_statements'
|
7
|
+
require 'active_record/connection_adapters/firebird/quoting'
|
7
8
|
|
8
9
|
require 'arel/visitors/firebird'
|
9
10
|
|
@@ -15,6 +16,7 @@ class ActiveRecord::ConnectionAdapters::FirebirdAdapter < ActiveRecord::Connecti
|
|
15
16
|
include ActiveRecord::ConnectionAdapters::Firebird::DatabaseLimits
|
16
17
|
include ActiveRecord::ConnectionAdapters::Firebird::DatabaseStatements
|
17
18
|
include ActiveRecord::ConnectionAdapters::Firebird::SchemaStatements
|
19
|
+
include ActiveRecord::ConnectionAdapters::Firebird::Quoting
|
18
20
|
|
19
21
|
def arel_visitor
|
20
22
|
@arel_visitor ||= Arel::Visitors::Firebird.new(self)
|
@@ -66,7 +68,7 @@ class ActiveRecord::ConnectionAdapters::FirebirdAdapter < ActiveRecord::Connecti
|
|
66
68
|
end
|
67
69
|
|
68
70
|
def encoding
|
69
|
-
|
71
|
+
@connection.encoding
|
70
72
|
end
|
71
73
|
|
72
74
|
def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil) # :doc:
|
@@ -74,6 +76,10 @@ class ActiveRecord::ConnectionAdapters::FirebirdAdapter < ActiveRecord::Connecti
|
|
74
76
|
super
|
75
77
|
end
|
76
78
|
|
79
|
+
def supports_foreign_keys?
|
80
|
+
true
|
81
|
+
end
|
82
|
+
|
77
83
|
protected
|
78
84
|
|
79
85
|
def translate_exception(e, message)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class ActiveRecord::InternalMetadata
|
2
|
+
class << self
|
3
|
+
def adapter_name
|
4
|
+
connection.adapter_name.downcase.to_sym
|
5
|
+
end
|
6
|
+
|
7
|
+
def value_name
|
8
|
+
adapter_name == :firebird ? :value_ : :value
|
9
|
+
end
|
10
|
+
|
11
|
+
def []=(key, value)
|
12
|
+
find_or_initialize_by(key: key).update!(value_name => value)
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](key)
|
16
|
+
where(key: key).pluck(value_name).first
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_table
|
20
|
+
unless table_exists?
|
21
|
+
key_options = connection.internal_string_options_for_primary_key
|
22
|
+
|
23
|
+
connection.create_table(table_name, id: false) do |t|
|
24
|
+
t.string :key, key_options
|
25
|
+
t.string value_name
|
26
|
+
t.timestamps
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: firebird_adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fábio Rodrigues
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -118,9 +118,11 @@ files:
|
|
118
118
|
- lib/active_record/connection_adapters/firebird/connection.rb
|
119
119
|
- lib/active_record/connection_adapters/firebird/database_limits.rb
|
120
120
|
- lib/active_record/connection_adapters/firebird/database_statements.rb
|
121
|
+
- lib/active_record/connection_adapters/firebird/quoting.rb
|
121
122
|
- lib/active_record/connection_adapters/firebird/schema_statements.rb
|
122
123
|
- lib/active_record/connection_adapters/firebird_adapter.rb
|
123
124
|
- lib/active_record/extensions.rb
|
125
|
+
- lib/active_record/internal_metadata_extensions.rb
|
124
126
|
- lib/arel/visitors/firebird.rb
|
125
127
|
homepage: https://github.com/FabioMR/firebird_adapter
|
126
128
|
licenses:
|