Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to rails 6 #39

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AllCops:
DisplayCopNames: true
DisplayStyleGuide: true
TargetRubyVersion: 2.1
TargetRubyVersion: 2.2
Exclude:
- 'vendor/**/*'

Expand Down
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
source 'https://rubygems.org'

gemspec

gem 'activerecord', '5.0.1'
gem 'pry', '~> 0.11.1'
3 changes: 1 addition & 2 deletions lib/active_record/connection_adapters/odbc_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'active_record'
require 'arel/visitors/bind_visitor'
require 'odbc'
require 'odbc_utf8'

Expand Down Expand Up @@ -137,7 +136,7 @@ def disconnect!
# Build a new column object from the given options. Effectively the same
# as super except that it also passes in the native type.
# rubocop:disable Metrics/ParameterLists
def new_column(name, default, sql_type_metadata, null, table_name, default_function = nil, collation = nil, native_type = nil)
def new_column(name, default, sql_type_metadata, null, default_function = nil)
::ODBCAdapter::Column.new(name, default, sql_type_metadata, null, table_name, default_function, collation, native_type)
end

Expand Down
6 changes: 1 addition & 5 deletions lib/odbc_adapter/adapters/mysql_odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ module Adapters
class MySQLODBCAdapter < ActiveRecord::ConnectionAdapters::ODBCAdapter
PRIMARY_KEY = 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY'.freeze

class BindSubstitution < Arel::Visitors::MySQL
include Arel::Visitors::BindVisitor
end

def arel_visitor
BindSubstitution.new(self)
Arel::Visitors::MySQL.new(self)
end

# Explicitly turning off prepared statements in the MySQL adapter because
Expand Down
5 changes: 1 addition & 4 deletions lib/odbc_adapter/adapters/null_odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ module Adapters
# registry. This allows for minimal support for DBMSs for which we don't
# have an explicit adapter.
class NullODBCAdapter < ActiveRecord::ConnectionAdapters::ODBCAdapter
class BindSubstitution < Arel::Visitors::ToSql
include Arel::Visitors::BindVisitor
end

# Using a BindVisitor so that the SQL string gets substituted before it is
# sent to the DBMS (to attempt to get as much coverage as possible for
# DBMSs we don't support).
def arel_visitor
BindSubstitution.new(self)
Arel::Visitors::PostgreSQL.new(self)
end

# Explicitly turning off prepared_statements in the null adapter because
Expand Down
2 changes: 1 addition & 1 deletion lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def default_sequence_name(table_name, pk = nil)
"#{table_name}_#{pk || 'id'}_seq"
end

def sql_for_insert(sql, pk, _id_value, _sequence_name, binds)
def sql_for_insert(sql, pk, binds)
unless pk
table_ref = extract_table_ref_from_insert_sql(sql)
pk = primary_key(table_ref) if table_ref
Expand Down
4 changes: 2 additions & 2 deletions lib/odbc_adapter/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Column < ActiveRecord::ConnectionAdapters::Column
# Add the native_type accessor to allow the native DBMS to report back what
# it uses to represent the column internally.
# rubocop:disable Metrics/ParameterLists
def initialize(name, default, sql_type_metadata = nil, null = true, table_name = nil, native_type = nil, default_function = nil, collation = nil)
super(name, default, sql_type_metadata, null, table_name, default_function, collation)
def initialize(name, default, sql_type_metadata = nil, null = true, native_type = nil, default_function = nil)
super(name, default, sql_type_metadata, null, default_function)
@native_type = native_type
end
end
Expand Down
30 changes: 18 additions & 12 deletions lib/odbc_adapter/database_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ module DatabaseStatements
# Returns the number of rows affected.
def execute(sql, name = nil, binds = [])
log(sql, name) do
if prepared_statements
@connection.do(sql, *prepared_binds(binds))
else
@connection.do(sql)
end
sql = bind_params(binds, sql) if prepared_statements
@connection.do(sql)
end
end

Expand All @@ -22,12 +19,8 @@ def execute(sql, name = nil, binds = [])
# the executed +sql+ statement.
def exec_query(sql, name = 'SQL', binds = [], prepare: false) # rubocop:disable Lint/UnusedMethodArgument
log(sql, name) do
stmt =
if prepared_statements
@connection.run(sql, *prepared_binds(binds))
else
@connection.run(sql)
end
sql = bind_params(binds, sql) if prepared_statements
stmt = @connection.run(sql)

columns = stmt.columns
values = stmt.to_a
Expand Down Expand Up @@ -81,6 +74,14 @@ def dbms_type_cast(_columns, values)
values
end

def bind_params(binds, sql)
Shehbaz marked this conversation as resolved.
Show resolved Hide resolved
prepared_binds = *prepared_binds(binds)
prepared_binds.each.with_index(1) do |val, ind|
sql = sql.gsub("$#{ind}", "'#{val}'")
end
sql
end

# Assume received identifier is in DBMS's data dictionary case.
def format_case(identifier)
if database_metadata.upcase_identifiers?
Expand Down Expand Up @@ -127,8 +128,13 @@ def nullability(col_name, is_nullable, nullable)
col_name == 'id' ? false : result
end

# Adapt to Rails 5.2
def prepare_statement_sub(sql)
sql.gsub(/\$\d+/, '?')
end

def prepared_binds(binds)
prepare_binds_for_database(binds).map { |bind| _type_cast(bind) }
binds.map(&:value_for_database).map { |bind| _type_cast(bind) }
end
end
end
2 changes: 1 addition & 1 deletion lib/odbc_adapter/schema_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def columns(table_name, _name = nil)
end
sql_type_metadata = ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(**args)

cols << new_column(format_case(col_name), col_default, sql_type_metadata, col_nullable, table_name, col_native_type)
cols << new_column(format_case(col_name), col_default, sql_type_metadata, col_nullable, col_native_type)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/odbc_adapter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ODBCAdapter
VERSION = '5.0.5'.freeze
VERSION = '5.0.6'.freeze
end
4 changes: 3 additions & 1 deletion odbc_adapter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'activerecord', '>= 5.0.1'
spec.add_dependency 'ruby-odbc', '~> 0.9'

spec.add_development_dependency 'bundler', '~> 1.14'
spec.add_development_dependency 'bundler', '>= 1.14'
spec.add_development_dependency 'minitest', '~> 5.10'
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'rubocop', '0.48.1'
spec.add_development_dependency 'simplecov', '~> 0.14'
spec.add_development_dependency 'pry', '~> 0.11.1'
end