forked from wvanbergen/activerecord-databasevalidations
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract MySQL-specific code into Adapters module
- Loading branch information
Showing
7 changed files
with
122 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module ActiveRecord | ||
module DatabaseValidations | ||
module Adapters | ||
class MissingAdapterError < StandardError; end | ||
|
||
@registry = {} | ||
|
||
def self.for(column) | ||
name = column.class.name.dup | ||
name.delete_prefix!("ActiveRecord::ConnectionAdapters::") | ||
name.delete_suffix!("::Column") | ||
|
||
registry.fetch(name).call | ||
rescue LoadError, KeyError | ||
raise MissingAdapterError, "no adapter found for #{name}" | ||
end | ||
|
||
def self.register(name, &loader) | ||
@registry[name] = loader | ||
end | ||
|
||
class << self | ||
attr_reader :registry | ||
end | ||
end | ||
|
||
Adapters.register("MySQL") do | ||
require "active_record/database_validations/adapters/mysql" | ||
ActiveRecord::DatabaseValidations::Adapters::MySQL | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module ActiveRecord | ||
module DatabaseValidations | ||
module Adapters | ||
class MySQL | ||
TYPE_LIMITS = { | ||
char: { type: :characters }, | ||
varchar: { type: :characters }, | ||
varbinary: { type: :bytes }, | ||
|
||
tinytext: { type: :bytes, maximum: 2 ** 8 - 1 }, | ||
text: { type: :bytes, maximum: 2 ** 16 - 1 }, | ||
mediumtext: { type: :bytes, maximum: 2 ** 24 - 1 }, | ||
longtext: { type: :bytes, maximum: 2 ** 32 - 1 }, | ||
|
||
tinyblob: { type: :bytes, maximum: 2 ** 8 - 1 }, | ||
blob: { type: :bytes, maximum: 2 ** 16 - 1 }, | ||
mediumblob: { type: :bytes, maximum: 2 ** 24 - 1 }, | ||
longblob: { type: :bytes, maximum: 2 ** 32 - 1 }, | ||
} | ||
|
||
def self.column_size_limit(column) | ||
column_type = column.sql_type.sub(/\(.*\z/, '').gsub(/\s/, '_').to_sym | ||
type_limit = TYPE_LIMITS.fetch(column_type, {}) | ||
|
||
[ | ||
column.limit || type_limit[:maximum], | ||
type_limit[:type], | ||
determine_encoding(column), | ||
] | ||
end | ||
|
||
def self.column_range(column) | ||
args = {} | ||
unsigned = column.sql_type =~ / unsigned\z/ | ||
case column.type | ||
when :decimal | ||
args[:less_than] = maximum = 10 ** (column.precision - column.scale) | ||
if unsigned | ||
args[:greater_than_or_equal_to] = 0 | ||
else | ||
args[:greater_than] = 0 - maximum | ||
end | ||
|
||
when :integer | ||
args[:only_integer] = true | ||
args[:less_than] = unsigned ? 1 << (column.limit * 8) : 1 << (column.limit * 8 - 1) | ||
args[:greater_than_or_equal_to] = unsigned ? 0 : 0 - args[:less_than] | ||
end | ||
|
||
args | ||
end | ||
|
||
def self.determine_encoding(column) | ||
column = ActiveRecord::Validations::TypedColumn.new(column) | ||
return nil unless column.text? | ||
case column.collation | ||
when /\Autf8/; Encoding::UTF_8 | ||
else raise NotImplementedError, "Don't know how to determine the Ruby encoding for MySQL's #{column.collation} collation." | ||
end | ||
end | ||
|
||
def self.requires_transcoding?(value, column_encoding = nil) | ||
column_encoding.present? && column_encoding != value.encoding | ||
end | ||
|
||
def self.value_for_column(value, column_encoding = nil) | ||
value = value.to_s unless value.is_a?(String) | ||
|
||
if requires_transcoding?(value, column_encoding) | ||
return value.encode(Encoding::UTF_8) | ||
end | ||
|
||
value | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters