Skip to content

Commit

Permalink
Merge pull request #4 from ValorWaterAnalytics/DEV-6915c
Browse files Browse the repository at this point in the history
Dev 6915c
  • Loading branch information
vmeow authored Jan 26, 2021
2 parents 6b1d44b + b1b3d54 commit dbb8037
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 72 deletions.
2 changes: 1 addition & 1 deletion activerecord6-redshift-adapter.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'activerecord6-redshift-adapter'
s.version = '1.1.4'
s.version = '1.2.0'
s.summary = 'Amazon Redshift adapter for ActiveRecord '
s.description = 'Amazon Redshift _makeshift_ adapter for ActiveRecord 6.'
s.license = 'MIT'
Expand Down
62 changes: 35 additions & 27 deletions lib/active_record/connection_adapters/redshift/column.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
module ActiveRecord
module ConnectionAdapters
class RedshiftColumn < Column #:nodoc:
delegate :oid, :fmod, to: :sql_type_metadata
module Redshift
class Column < ConnectionAdapters::Column #:nodoc:
delegate :oid, :fmod, to: :sql_type_metadata

def initialize(name, default, sql_type_metadata, null = true, table_name = nil, default_function = nil, encoding = nil, auto_increment = nil)
super name, default, sql_type_metadata, null, default_function
@null = null
@default_function = default_function
@encoding = encoding
@auto_increment = auto_increment
end
def initialize(name, default, sql_type_metadata, null = true, table_name = nil, default_function = nil, encoding = nil, auto_increment = nil, **)
super name, default, sql_type_metadata, null, default_function
@null = null
@default_function = default_function
@encoding = encoding
@auto_increment = auto_increment
end

def init_with(coder)
super coder
@encoding = coder["encoding"]
@auto_increment = coder["auto_increment"]
end
def init_with(coder)
super coder
@encoding = coder["encoding"]
@auto_increment = coder["auto_increment"]
end

def encode_with(coder)
super coder
coder["encoding"] = @encoding
coder["auto_increment"] = @auto_increment
end
def encode_with(coder)
super coder
coder["encoding"] = @encoding
coder["auto_increment"] = @auto_increment
end

def encoding
@encoding
end
def encoding
@encoding
end

def null
@null
end
def null
@null
end

def auto_increment
@auto_increment
end

def auto_increment
@auto_increment
def array
sql_type_metadata.sql_type.end_with?("[]")
end
alias :array? :array
end
end
RedshiftColumn = Redshift::Column
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -44,53 +44,33 @@ def pp(result)
end
end

def select_value(arel, name = nil, binds = [])
# In Rails 5.2, arel_from_relation replaced binds_from_relation,
# so we see which method exists to get the variables
#
# In Rails 6.0 to_sql_and_binds began only returning sql, with
# to_sql_and_binds serving as a replacement
if respond_to?(:arel_from_relation, true)
arel = arel_from_relation(arel)
sql, binds = to_sql_and_binds(arel, binds)
# Returns an ActiveRecord::Result instance.
def select_all(arel, name = nil, binds = [], preparable: nil)
arel = arel_from_relation(arel)
sql, binds, preparable = to_sql_and_binds(arel, binds, preparable)

if prepared_statements && preparable
select_prepared(sql, name, binds)
else
arel, binds = binds_from_relation arel, binds
sql = to_sql(arel, binds)
end
execute_and_clear(sql, name, binds) do |result|
result.getvalue(0, 0) if result.ntuples > 0 && result.nfields > 0
select(sql, name, binds)
end
rescue ::RangeError
ActiveRecord::Result.new([], [])
end

def select_values(arel, name = nil)
# In Rails 5.2, arel_from_relation replaced binds_from_relation,
# so we see which method exists to get the variables
#
# In Rails 6.0 to_sql_and_binds began only returning sql, with
# to_sql_and_binds serving as a replacement
if respond_to?(:arel_from_relation, true)
arel = arel_from_relation(arel)
sql, binds = to_sql_and_binds(arel, [])
else
arel, binds = binds_from_relation arel, []
sql = to_sql(arel, binds)
end
# Returns a single value from a record
def select_value(arel, name = nil, binds = [])
single_value_from_rows(select_rows(arel, name, binds))
end

execute_and_clear(sql, name, binds) do |result|
if result.nfields > 0
result.column_values(0)
else
[]
end
end
# Returns an array of the values of the first column in a select:
# select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]
def select_values(arel, name = nil, binds = [])
select_rows(arel, name, binds).map(&:first)
end

# Executes a SELECT query and returns an array of rows. Each row is an
# array of field values.
def select_rows(sql, name = nil, binds = [])
execute_and_clear(sql, name, binds) do |result|
result.values
end
def select_rows(arel, name = nil, binds = [])
select_all(arel, name, binds).rows
end

# The internal PostgreSQL identifier of the money data type.
Expand Down Expand Up @@ -220,6 +200,11 @@ def commit_db_transaction
def exec_rollback_db_transaction
execute "ROLLBACK"
end

def single_value_from_rows(rows)
row = rows.first
row && row.first
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def fetch_type_metadata(column_name, sql_type, oid, fmod)
precision: cast_type.precision,
scale: cast_type.scale,
)
TypeMetadata.new(simple_type, oid: oid, fmod: fmod)
RedshiftTypeMetadata.new(simple_type, oid: oid, fmod: fmod)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module ActiveRecord
module ConnectionAdapters
module Redshift
class TypeMetadata < DelegateClass(SqlTypeMetadata)
undef to_yaml if method_defined?(:to_yaml)

include Deduplicable

attr_reader :oid, :fmod, :array

def initialize(type_metadata, oid: nil, fmod: nil)
Expand Down Expand Up @@ -33,5 +37,6 @@ def attributes_for_hash
end
end
end
RedshiftTypeMetadata = Redshift::TypeMetadata
end
end
6 changes: 3 additions & 3 deletions lib/active_record/connection_adapters/redshift_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ def initialize(connection, logger, connection_parameters, config)
super(connection, logger, config)

@visitor = Arel::Visitors::PostgreSQL.new self
@visitor.extend(ConnectionAdapters::DetermineIfPreparableVisitor)
@prepared_statements = false

@prepared_statements = self.class.type_cast_config_to_boolean(
config.fetch(:prepared_statements, true)
)
@connection_parameters = connection_parameters

# @local_tz is initialized as nil to avoid warnings when connect tries to use it
Expand Down

0 comments on commit dbb8037

Please sign in to comment.