odbc_adapter 5.0.2 → 5.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +32 -0
- data/README.md +2 -2
- data/Rakefile +4 -0
- data/lib/active_record/connection_adapters/odbc_adapter.rb +42 -35
- data/lib/odbc_adapter/adapters/mysql_odbc_adapter.rb +4 -4
- data/lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb +7 -7
- data/lib/odbc_adapter/column.rb +3 -0
- data/lib/odbc_adapter/column_metadata.rb +2 -2
- data/lib/odbc_adapter/database_metadata.rb +1 -1
- data/lib/odbc_adapter/database_statements.rb +8 -12
- data/lib/odbc_adapter/quoting.rb +3 -3
- data/lib/odbc_adapter/schema_statements.rb +7 -5
- data/lib/odbc_adapter/version.rb +1 -1
- data/odbc_adapter.gemspec +1 -0
- metadata +18 -4
- data/lib/odbc_adapter/type_caster.rb +0 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4debf1d258924d263e9f5979f9264048a046dad7
|
4
|
+
data.tar.gz: 4211fdf1c9b1674d521319eca06ceb5f1060c643
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 246b9da217696c875c1df669064d7e7198d8b8505aaf70358125ec77fb90152ba93555b5d8ccbde4c15af85934aebd9277be6c00751d71e78f4a409516da8400
|
7
|
+
data.tar.gz: 015bd47b7d7208c088ed6ecd6f1357e2a24e3c5d3513205bb0d56303396a1d7d868ab2691990782ee3d827033efa41997d553500580cc08a696b843bf997cb09
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
DisplayStyleGuide: true
|
4
|
+
TargetRubyVersion: 2.0
|
5
|
+
Exclude:
|
6
|
+
- 'vendor/**/*'
|
7
|
+
|
8
|
+
Metrics/AbcSize:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Metrics/ClassLength:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Metrics/CyclomaticComplexity:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Metrics/MethodLength:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Metrics/LineLength:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Metrics/PerceivedComplexity:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Style/Documentation:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/PercentLiteralDelimiters:
|
30
|
+
PreferredDelimiters:
|
31
|
+
'%w': '[]'
|
32
|
+
'%i': '[]'
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# ODBCAdapter
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.
|
3
|
+
[![Build Status](https://travis-ci.org/localytics/odbc_adapter.svg?branch=master)](https://travis-ci.org/localytics/odbc_adapter)
|
4
4
|
|
5
|
-
An ActiveRecord ODBC adapter. Master branch is working off of
|
5
|
+
An ActiveRecord ODBC adapter. Master branch is working off of Rails 5.0.1. Previous work has been done to make it compatible with Rails 3.2 and 4.2; for those versions use the 3.2.x or 4.2.x gem releases.
|
6
6
|
|
7
7
|
This adapter will work for basic queries for most DBMSs out of the box, without support for migrations. Full support is built-in for MySQL 5 and PostgreSQL 9 databases. You can register your own adapter to get more support for your DBMS using the `ODBCAdapter.register` function.
|
8
8
|
|
data/Rakefile
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'bundler/gem_tasks'
|
2
2
|
require 'rake/testtask'
|
3
|
+
require 'rubocop/rake_task'
|
3
4
|
|
4
5
|
Rake::TestTask.new(:test) do |t|
|
5
6
|
t.libs << 'test'
|
@@ -7,4 +8,7 @@ Rake::TestTask.new(:test) do |t|
|
|
7
8
|
t.test_files = FileList['test/**/*_test.rb']
|
8
9
|
end
|
9
10
|
|
11
|
+
RuboCop::RakeTask.new(:rubocop)
|
12
|
+
Rake::Task[:test].prerequisites << :rubocop
|
13
|
+
|
10
14
|
task default: :test
|
@@ -12,36 +12,36 @@ require 'odbc_adapter/column'
|
|
12
12
|
require 'odbc_adapter/column_metadata'
|
13
13
|
require 'odbc_adapter/database_metadata'
|
14
14
|
require 'odbc_adapter/registry'
|
15
|
-
require 'odbc_adapter/type_caster'
|
16
15
|
require 'odbc_adapter/version'
|
17
16
|
|
18
17
|
module ActiveRecord
|
19
18
|
class Base
|
20
19
|
class << self
|
20
|
+
# Build a new ODBC connection with the given configuration.
|
21
21
|
def odbc_connection(config)
|
22
22
|
config = config.symbolize_keys
|
23
23
|
|
24
|
-
connection,
|
24
|
+
connection, config =
|
25
25
|
if config.key?(:dsn)
|
26
26
|
odbc_dsn_connection(config)
|
27
27
|
elsif config.key?(:conn_str)
|
28
28
|
odbc_conn_str_connection(config)
|
29
29
|
else
|
30
|
-
raise ArgumentError,
|
30
|
+
raise ArgumentError, 'No data source name (:dsn) or connection string (:conn_str) specified.'
|
31
31
|
end
|
32
32
|
|
33
33
|
database_metadata = ::ODBCAdapter::DatabaseMetadata.new(connection)
|
34
|
-
database_metadata.adapter_class.new(connection, logger, database_metadata)
|
34
|
+
database_metadata.adapter_class.new(connection, logger, config, database_metadata)
|
35
35
|
end
|
36
36
|
|
37
37
|
private
|
38
38
|
|
39
|
+
# Connect using a predefined DSN.
|
39
40
|
def odbc_dsn_connection(config)
|
40
41
|
username = config[:username] ? config[:username].to_s : nil
|
41
42
|
password = config[:password] ? config[:password].to_s : nil
|
42
43
|
connection = ODBC.connect(config[:dsn], username, password)
|
43
|
-
|
44
|
-
[connection, options]
|
44
|
+
[connection, config.merge(username: username, password: password)]
|
45
45
|
end
|
46
46
|
|
47
47
|
# Connect using ODBC connection string
|
@@ -49,20 +49,12 @@ module ActiveRecord
|
|
49
49
|
# e.g. "DSN=virt5;UID=rails;PWD=rails"
|
50
50
|
# "DRIVER={OpenLink Virtuoso};HOST=carlmbp;UID=rails;PWD=rails"
|
51
51
|
def odbc_conn_str_connection(config)
|
52
|
-
connstr_keyval_pairs = config[:conn_str].split(';')
|
53
|
-
|
54
52
|
driver = ODBC::Driver.new
|
55
53
|
driver.name = 'odbc'
|
56
|
-
driver.attrs = {}
|
57
|
-
|
58
|
-
connstr_keyval_pairs.each do |pair|
|
59
|
-
keyval = pair.split('=')
|
60
|
-
driver.attrs[keyval[0]] = keyval[1] if keyval.length == 2
|
61
|
-
end
|
54
|
+
driver.attrs = config[:conn_str].split(';').map { |option| option.split('=', 2) }.to_h
|
62
55
|
|
63
56
|
connection = ODBC::Database.new.drvconnect(driver)
|
64
|
-
|
65
|
-
[connection, options]
|
57
|
+
[connection, config.merge(driver: driver)]
|
66
58
|
end
|
67
59
|
end
|
68
60
|
end
|
@@ -77,35 +69,36 @@ module ActiveRecord
|
|
77
69
|
ADAPTER_NAME = 'ODBC'.freeze
|
78
70
|
BOOLEAN_TYPE = 'BOOLEAN'.freeze
|
79
71
|
|
80
|
-
ERR_DUPLICATE_KEY_VALUE =
|
81
|
-
ERR_QUERY_TIMED_OUT =
|
72
|
+
ERR_DUPLICATE_KEY_VALUE = 23_505
|
73
|
+
ERR_QUERY_TIMED_OUT = 57_014
|
82
74
|
ERR_QUERY_TIMED_OUT_MESSAGE = /Query has timed out/
|
83
75
|
|
76
|
+
# The object that stores the information that is fetched from the DBMS
|
77
|
+
# when a connection is first established.
|
84
78
|
attr_reader :database_metadata
|
85
79
|
|
86
|
-
def initialize(connection, logger, database_metadata)
|
87
|
-
|
88
|
-
|
80
|
+
def initialize(connection, logger, config, database_metadata)
|
81
|
+
configure_time_options(connection)
|
82
|
+
super(connection, logger, config)
|
89
83
|
@database_metadata = database_metadata
|
90
84
|
end
|
91
85
|
|
92
|
-
# Returns the human-readable name of the adapter.
|
93
|
-
# can always use downcase if needed.
|
86
|
+
# Returns the human-readable name of the adapter.
|
94
87
|
def adapter_name
|
95
88
|
ADAPTER_NAME
|
96
89
|
end
|
97
90
|
|
98
|
-
# Does this adapter support migrations? Backend specific, as the
|
99
|
-
#
|
91
|
+
# Does this adapter support migrations? Backend specific, as the abstract
|
92
|
+
# adapter always returns +false+.
|
100
93
|
def supports_migrations?
|
101
94
|
true
|
102
95
|
end
|
103
96
|
|
104
97
|
# CONNECTION MANAGEMENT ====================================
|
105
98
|
|
106
|
-
# Checks whether the connection to the database is still active. This
|
107
|
-
# checking whether the database is actually capable of
|
108
|
-
# the connection isn't stale.
|
99
|
+
# Checks whether the connection to the database is still active. This
|
100
|
+
# includes checking whether the database is actually capable of
|
101
|
+
# responding, i.e. whether the connection isn't stale.
|
109
102
|
def active?
|
110
103
|
@connection.connected?
|
111
104
|
end
|
@@ -115,14 +108,15 @@ module ActiveRecord
|
|
115
108
|
def reconnect!
|
116
109
|
disconnect!
|
117
110
|
@connection =
|
118
|
-
if
|
119
|
-
ODBC.connect(
|
111
|
+
if @config.key?(:dsn)
|
112
|
+
ODBC.connect(@config[:dsn], @config[:username], @config[:password])
|
120
113
|
else
|
121
|
-
ODBC::Database.new.drvconnect(
|
114
|
+
ODBC::Database.new.drvconnect(@config[:driver])
|
122
115
|
end
|
116
|
+
configure_time_options(@connection)
|
123
117
|
super
|
124
118
|
end
|
125
|
-
alias
|
119
|
+
alias reset! reconnect!
|
126
120
|
|
127
121
|
# Disconnects from the database if already connected. Otherwise, this
|
128
122
|
# method does nothing.
|
@@ -130,12 +124,16 @@ module ActiveRecord
|
|
130
124
|
@connection.disconnect if @connection.connected?
|
131
125
|
end
|
132
126
|
|
127
|
+
# Build a new column object from the given options. Effectively the same
|
128
|
+
# as super except that it also passes in the native type.
|
129
|
+
# rubocop:disable Metrics/ParameterLists
|
133
130
|
def new_column(name, default, sql_type_metadata, null, table_name, default_function = nil, collation = nil, native_type = nil)
|
134
131
|
::ODBCAdapter::Column.new(name, default, sql_type_metadata, null, table_name, default_function, collation, native_type)
|
135
132
|
end
|
136
133
|
|
137
134
|
protected
|
138
135
|
|
136
|
+
# Build the type map for ActiveRecord
|
139
137
|
def initialize_type_map(map)
|
140
138
|
map.register_type 'boolean', Type::Boolean.new
|
141
139
|
map.register_type ODBC::SQL_CHAR, Type::String.new
|
@@ -168,13 +166,14 @@ module ActiveRecord
|
|
168
166
|
alias_type map, ODBC::SQL_TYPE_TIMESTAMP, ODBC::SQL_TIMESTAMP
|
169
167
|
end
|
170
168
|
|
169
|
+
# Translate an exception from the native DBMS to something usable by
|
170
|
+
# ActiveRecord.
|
171
171
|
def translate_exception(exception, message)
|
172
172
|
error_number = exception.message[/^\d+/].to_i
|
173
173
|
|
174
|
-
|
175
|
-
when error_number == ERR_DUPLICATE_KEY_VALUE
|
174
|
+
if error_number == ERR_DUPLICATE_KEY_VALUE
|
176
175
|
ActiveRecord::RecordNotUnique.new(message, exception)
|
177
|
-
|
176
|
+
elsif error_number == ERR_QUERY_TIMED_OUT || exception.message =~ ERR_QUERY_TIMED_OUT_MESSAGE
|
178
177
|
::ODBCAdapter::QueryTimeoutError.new(message, exception)
|
179
178
|
else
|
180
179
|
super
|
@@ -183,11 +182,19 @@ module ActiveRecord
|
|
183
182
|
|
184
183
|
private
|
185
184
|
|
185
|
+
# Can't use the built-in ActiveRecord map#alias_type because it doesn't
|
186
|
+
# work with non-string keys, and in our case the keys are (almost) all
|
187
|
+
# numeric
|
186
188
|
def alias_type(map, new_type, old_type)
|
187
189
|
map.register_type(new_type) do |_, *args|
|
188
190
|
map.lookup(old_type, *args)
|
189
191
|
end
|
190
192
|
end
|
193
|
+
|
194
|
+
# Ensure ODBC is mapping time-based fields to native ruby objects
|
195
|
+
def configure_time_options(connection)
|
196
|
+
connection.use_time = true
|
197
|
+
end
|
191
198
|
end
|
192
199
|
end
|
193
200
|
end
|
@@ -16,7 +16,7 @@ module ODBCAdapter
|
|
16
16
|
# Explicitly turning off prepared statements in the MySQL adapter because
|
17
17
|
# of a weird bug with SQLDescribeParam returning a string type for LIMIT
|
18
18
|
# parameters. This is blocking them from running with an error:
|
19
|
-
#
|
19
|
+
#
|
20
20
|
# You have an error in your SQL syntax; ...
|
21
21
|
# ... right syntax to use near ''1'' at line 1: ...
|
22
22
|
def prepared_statements
|
@@ -49,11 +49,11 @@ module ODBCAdapter
|
|
49
49
|
0
|
50
50
|
end
|
51
51
|
|
52
|
-
def disable_referential_integrity(&
|
53
|
-
old = select_value(
|
52
|
+
def disable_referential_integrity(&_block)
|
53
|
+
old = select_value('SELECT @@FOREIGN_KEY_CHECKS')
|
54
54
|
|
55
55
|
begin
|
56
|
-
update(
|
56
|
+
update('SET FOREIGN_KEY_CHECKS = 0')
|
57
57
|
yield
|
58
58
|
ensure
|
59
59
|
update("SET FOREIGN_KEY_CHECKS = #{old}")
|
@@ -6,7 +6,7 @@ module ODBCAdapter
|
|
6
6
|
BOOLEAN_TYPE = 'bool'.freeze
|
7
7
|
PRIMARY_KEY = 'SERIAL PRIMARY KEY'.freeze
|
8
8
|
|
9
|
-
alias
|
9
|
+
alias create insert
|
10
10
|
|
11
11
|
# Override to handle booleans appropriately
|
12
12
|
def native_database_types
|
@@ -19,7 +19,7 @@ module ODBCAdapter
|
|
19
19
|
|
20
20
|
# Filter for ODBCAdapter#tables
|
21
21
|
# Omits table from #tables if table_filter returns true
|
22
|
-
def
|
22
|
+
def table_filtered?(schema_name, table_type)
|
23
23
|
%w[information_schema pg_catalog].include?(schema_name) || table_type !~ /TABLE/i
|
24
24
|
end
|
25
25
|
|
@@ -35,7 +35,7 @@ module ODBCAdapter
|
|
35
35
|
"#{table_name}_#{pk || 'id'}_seq"
|
36
36
|
end
|
37
37
|
|
38
|
-
def sql_for_insert(sql, pk,
|
38
|
+
def sql_for_insert(sql, pk, _id_value, _sequence_name, binds)
|
39
39
|
unless pk
|
40
40
|
table_ref = extract_table_ref_from_insert_sql(sql)
|
41
41
|
pk = primary_key(table_ref) if table_ref
|
@@ -94,7 +94,7 @@ module ODBCAdapter
|
|
94
94
|
when :connection_limit
|
95
95
|
" CONNECTION LIMIT = #{value}"
|
96
96
|
else
|
97
|
-
|
97
|
+
''
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
@@ -131,7 +131,7 @@ module ODBCAdapter
|
|
131
131
|
execute("DROP INDEX #{quote_table_name(index_name)}")
|
132
132
|
end
|
133
133
|
|
134
|
-
def rename_index(
|
134
|
+
def rename_index(_table_name, old_name, new_name)
|
135
135
|
execute("ALTER INDEX #{quote_column_name(old_name)} RENAME TO #{quote_table_name(new_name)}")
|
136
136
|
end
|
137
137
|
|
@@ -149,8 +149,8 @@ module ODBCAdapter
|
|
149
149
|
# Construct a clean list of column names from the ORDER BY clause,
|
150
150
|
# removing any ASC/DESC modifiers
|
151
151
|
order_columns = orders.map { |s| s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '') }
|
152
|
-
order_columns.reject!
|
153
|
-
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
|
152
|
+
order_columns.reject!(&:blank?)
|
153
|
+
order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s, i| "#{s} AS alias_#{i}" }
|
154
154
|
|
155
155
|
"DISTINCT #{columns}, #{order_columns * ', '}"
|
156
156
|
end
|
data/lib/odbc_adapter/column.rb
CHANGED
@@ -2,6 +2,9 @@ module ODBCAdapter
|
|
2
2
|
class Column < ActiveRecord::ConnectionAdapters::Column
|
3
3
|
attr_reader :native_type
|
4
4
|
|
5
|
+
# Add the native_type accessor to allow the native DBMS to report back what
|
6
|
+
# it uses to represent the column internally.
|
7
|
+
# rubocop:disable Metrics/ParameterLists
|
5
8
|
def initialize(name, default, sql_type_metadata = nil, null = true, table_name = nil, native_type = nil, default_function = nil, collation = nil)
|
6
9
|
super(name, default, sql_type_metadata, null, table_name, default_function, collation)
|
7
10
|
@native_type = native_type
|
@@ -13,7 +13,7 @@ module ODBCAdapter
|
|
13
13
|
date: [ODBC::SQL_TYPE_DATE, ODBC::SQL_DATE, ODBC::SQL_TYPE_TIMESTAMP, ODBC::SQL_TIMESTAMP],
|
14
14
|
binary: [ODBC::SQL_LONGVARBINARY, ODBC::SQL_VARBINARY],
|
15
15
|
boolean: [ODBC::SQL_BIT, ODBC::SQL_TINYINT, ODBC::SQL_SMALLINT, ODBC::SQL_INTEGER]
|
16
|
-
}
|
16
|
+
}.freeze
|
17
17
|
|
18
18
|
attr_reader :adapter
|
19
19
|
|
@@ -56,7 +56,7 @@ module ODBCAdapter
|
|
56
56
|
create_params = selected_row[5]
|
57
57
|
# Depending on the column type, the CREATE_PARAMS keywords can
|
58
58
|
# include length, precision or scale.
|
59
|
-
if create_params && create_params.strip.
|
59
|
+
if create_params && !create_params.strip.empty? && abstract != :decimal
|
60
60
|
result[:limit] = selected_row[2] # SQLGetTypeInfo: COL_SIZE
|
61
61
|
end
|
62
62
|
|
@@ -20,7 +20,7 @@ module ODBCAdapter
|
|
20
20
|
# Executes +sql+ statement in the context of this connection using
|
21
21
|
# +binds+ as the bind substitutes. +name+ is logged along with
|
22
22
|
# the executed +sql+ statement.
|
23
|
-
def exec_query(sql, name = 'SQL', binds = [], prepare: false)
|
23
|
+
def exec_query(sql, name = 'SQL', binds = [], prepare: false) # rubocop:disable Lint/UnusedMethodArgument
|
24
24
|
log(sql, name) do
|
25
25
|
stmt =
|
26
26
|
if prepared_statements
|
@@ -33,16 +33,9 @@ module ODBCAdapter
|
|
33
33
|
values = stmt.to_a
|
34
34
|
stmt.drop
|
35
35
|
|
36
|
-
casters = TypeCaster.build_from(columns.values)
|
37
|
-
if casters.any?
|
38
|
-
values.each do |row|
|
39
|
-
casters.each { |caster| row[caster.idx] = caster.cast(row[caster.idx]) }
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
36
|
values = dbms_type_cast(columns.values, values)
|
44
37
|
column_names = columns.keys.map { |key| format_case(key) }
|
45
|
-
|
38
|
+
ActiveRecord::Result.new(column_names, values)
|
46
39
|
end
|
47
40
|
end
|
48
41
|
|
@@ -52,7 +45,7 @@ module ODBCAdapter
|
|
52
45
|
def exec_delete(sql, name, binds)
|
53
46
|
execute(sql, name, binds)
|
54
47
|
end
|
55
|
-
alias
|
48
|
+
alias exec_update exec_delete
|
56
49
|
|
57
50
|
# Begins the transaction (and turns off auto-committing).
|
58
51
|
def begin_db_transaction
|
@@ -81,7 +74,10 @@ module ODBCAdapter
|
|
81
74
|
|
82
75
|
private
|
83
76
|
|
84
|
-
|
77
|
+
# A custom hook to allow end users to overwrite the type casting before it
|
78
|
+
# is returned to ActiveRecord. Useful before a full adapter has made its way
|
79
|
+
# back into this repository.
|
80
|
+
def dbms_type_cast(_columns, values)
|
85
81
|
values
|
86
82
|
end
|
87
83
|
|
@@ -122,7 +118,7 @@ module ODBCAdapter
|
|
122
118
|
|
123
119
|
# Assume column is nullable if nullable == SQL_NULLABLE_UNKNOWN
|
124
120
|
def nullability(col_name, is_nullable, nullable)
|
125
|
-
not_nullable = (!is_nullable || nullable.to_s.match('NO')
|
121
|
+
not_nullable = (!is_nullable || !nullable.to_s.match('NO').nil?)
|
126
122
|
result = !(not_nullable || nullable == SQL_NO_NULLS)
|
127
123
|
|
128
124
|
# HACK!
|
data/lib/odbc_adapter/quoting.rb
CHANGED
@@ -18,7 +18,7 @@ module ODBCAdapter
|
|
18
18
|
|
19
19
|
# If upcase identifiers, only quote mixed case names.
|
20
20
|
if database_metadata.upcase_identifiers?
|
21
|
-
return name unless
|
21
|
+
return name unless name =~ /([A-Z]+[a-z])|([a-z]+[A-Z])/
|
22
22
|
end
|
23
23
|
|
24
24
|
"#{quote_char.chr}#{name}#{quote_char.chr}"
|
@@ -33,9 +33,9 @@ module ODBCAdapter
|
|
33
33
|
if value.respond_to?(zone_conversion_method)
|
34
34
|
value = value.send(zone_conversion_method)
|
35
35
|
end
|
36
|
-
value.strftime(
|
36
|
+
value.strftime('%Y-%m-%d %H:%M:%S') # Time, DateTime
|
37
37
|
else
|
38
|
-
value.strftime(
|
38
|
+
value.strftime('%Y-%m-%d') # Date
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
@@ -27,7 +27,7 @@ module ODBCAdapter
|
|
27
27
|
end
|
28
28
|
|
29
29
|
# Returns an array of indexes for the given table.
|
30
|
-
def indexes(table_name,
|
30
|
+
def indexes(table_name, _name = nil)
|
31
31
|
stmt = @connection.indexes(native_case(table_name.to_s))
|
32
32
|
result = stmt.fetch_all || []
|
33
33
|
stmt.drop unless stmt.nil?
|
@@ -38,7 +38,7 @@ module ODBCAdapter
|
|
38
38
|
|
39
39
|
result.each_with_object([]).with_index do |(row, indices), row_idx|
|
40
40
|
# Skip table statistics
|
41
|
-
next if row[6]
|
41
|
+
next if row[6].zero? # SQLStatistics: TYPE
|
42
42
|
|
43
43
|
if row[7] == 1 # SQLStatistics: ORDINAL_POSITION
|
44
44
|
# Start of column descriptor block for next index
|
@@ -50,7 +50,7 @@ module ODBCAdapter
|
|
50
50
|
index_cols << format_case(row[8]) # SQLStatistics: COLUMN_NAME
|
51
51
|
next_row = result[row_idx + 1]
|
52
52
|
|
53
|
-
if (row_idx == result.length - 1) || (next_row[6]
|
53
|
+
if (row_idx == result.length - 1) || (next_row[6].zero? || next_row[7] == 1)
|
54
54
|
indices << ActiveRecord::ConnectionAdapters::IndexDefinition.new(table_name, format_case(index_name), unique, index_cols)
|
55
55
|
end
|
56
56
|
end
|
@@ -58,7 +58,7 @@ module ODBCAdapter
|
|
58
58
|
|
59
59
|
# Returns an array of Column objects for the table specified by
|
60
60
|
# +table_name+.
|
61
|
-
def columns(table_name,
|
61
|
+
def columns(table_name, _name = nil)
|
62
62
|
stmt = @connection.columns(native_case(table_name.to_s))
|
63
63
|
result = stmt.fetch_all || []
|
64
64
|
stmt.drop
|
@@ -104,7 +104,9 @@ module ODBCAdapter
|
|
104
104
|
fk_from_table = key[2] # PKTABLE_NAME
|
105
105
|
fk_to_table = key[6] # FKTABLE_NAME
|
106
106
|
|
107
|
-
ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(
|
107
|
+
ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(
|
108
|
+
fk_from_table,
|
109
|
+
fk_to_table,
|
108
110
|
name: key[11], # FK_NAME
|
109
111
|
column: key[3], # PKCOLUMN_NAME
|
110
112
|
primary_key: key[7], # FKCOLUMN_NAME
|
data/lib/odbc_adapter/version.rb
CHANGED
data/odbc_adapter.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: odbc_adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.0.
|
4
|
+
version: 5.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Localytics
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-odbc
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0.12'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.47'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.47'
|
83
97
|
description:
|
84
98
|
email:
|
85
99
|
- oss@localytics.com
|
@@ -88,6 +102,7 @@ extensions: []
|
|
88
102
|
extra_rdoc_files: []
|
89
103
|
files:
|
90
104
|
- ".gitignore"
|
105
|
+
- ".rubocop.yml"
|
91
106
|
- ".travis.yml"
|
92
107
|
- Gemfile
|
93
108
|
- LICENSE
|
@@ -110,7 +125,6 @@ files:
|
|
110
125
|
- lib/odbc_adapter/quoting.rb
|
111
126
|
- lib/odbc_adapter/registry.rb
|
112
127
|
- lib/odbc_adapter/schema_statements.rb
|
113
|
-
- lib/odbc_adapter/type_caster.rb
|
114
128
|
- lib/odbc_adapter/version.rb
|
115
129
|
- odbc_adapter.gemspec
|
116
130
|
homepage: https://github.com/localytics/odbc_adapter
|
@@ -133,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
147
|
version: '0'
|
134
148
|
requirements: []
|
135
149
|
rubyforge_project:
|
136
|
-
rubygems_version: 2.
|
150
|
+
rubygems_version: 2.6.8
|
137
151
|
signing_key:
|
138
152
|
specification_version: 4
|
139
153
|
summary: An ActiveRecord ODBC adapter
|
@@ -1,42 +0,0 @@
|
|
1
|
-
module ODBCAdapter
|
2
|
-
class TypeCaster
|
3
|
-
# When fetching a result set, the Ruby ODBC driver converts all ODBC
|
4
|
-
# SQL types to an equivalent Ruby type; with the exception of
|
5
|
-
# SQL_DATE, SQL_TIME and SQL_TIMESTAMP.
|
6
|
-
TYPES = [
|
7
|
-
ODBC::SQL_DATE,
|
8
|
-
ODBC::SQL_TIME,
|
9
|
-
ODBC::SQL_TIMESTAMP
|
10
|
-
]
|
11
|
-
|
12
|
-
attr_reader :idx
|
13
|
-
|
14
|
-
def initialize(idx)
|
15
|
-
@idx = idx
|
16
|
-
end
|
17
|
-
|
18
|
-
def cast(value)
|
19
|
-
case value
|
20
|
-
when ODBC::TimeStamp
|
21
|
-
Time.gm(value.year, value.month, value.day, value.hour, value.minute, value.second)
|
22
|
-
when ODBC::Time
|
23
|
-
now = DateTime.now
|
24
|
-
Time.gm(now.year, now.month, now.day, value.hour, value.minute, value.second)
|
25
|
-
when ODBC::Date
|
26
|
-
Date.new(value.year, value.month, value.day)
|
27
|
-
else
|
28
|
-
value
|
29
|
-
end
|
30
|
-
rescue
|
31
|
-
# Handle pre-epoch dates
|
32
|
-
DateTime.new(value.year, value.month, value.day, value.hour, value.minute, value.second)
|
33
|
-
end
|
34
|
-
|
35
|
-
# Build a list of casters from a list of columns
|
36
|
-
def self.build_from(columns)
|
37
|
-
columns.each_with_index.each_with_object([]) do |(column, idx), casters|
|
38
|
-
casters << new(idx) if TYPES.include?(column.type)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|