-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3173e8f
commit 3a660ff
Showing
7 changed files
with
123 additions
and
30 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 |
---|---|---|
@@ -1,28 +1,8 @@ | ||
require "active_record-mysql-uuid_column/version" | ||
require 'active_record' | ||
require 'active_record/connection_adapters/mysql2_adapter' | ||
require 'active_record-mysql-uuid_column/version' | ||
require 'active_record-mysql-uuid_column/migration_column.rb' | ||
require 'active_record-mysql-uuid_column/type_class.rb' | ||
require 'active_record-mysql-uuid_column/mysql2_adapter_monkey_patch.rb' | ||
|
||
module ActiveModel | ||
module Type | ||
class Uuid | ||
def method_missing(name) | ||
debugger | ||
puts "whatever" | ||
end | ||
end | ||
end | ||
end | ||
module ActiveRecord | ||
module ConnectionAdapters | ||
module MySQL | ||
module ColumnMethods | ||
def uuid(*args, **options) | ||
# http://dba.stackexchange.com/questions/904/mysql-data-type-for-128-bit-integers | ||
# http://dev.mysql.com/doc/refman/5.7/en/binary-varbinary.html | ||
args.each { |name| column(name, :binary, options.merge(limit: 16)) } | ||
end | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
||
ActiveRecord::Type.register(:uuid, ActiveRecord::Type::Uuid, adapter: :mysql2) |
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,13 @@ | ||
module ActiveRecord | ||
module ConnectionAdapters | ||
module MySQL | ||
module ColumnMethods | ||
def uuid(*args, **options) | ||
# http://dba.stackexchange.com/questions/904/mysql-data-type-for-128-bit-integers | ||
# http://dev.mysql.com/doc/refman/5.7/en/binary-varbinary.html | ||
args.each { |name| column(name, :binary, options.merge(limit: 16)) } | ||
end | ||
end | ||
end | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
lib/active_record-mysql-uuid_column/mysql2_adapter_monkey_patch.rb
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,15 @@ | ||
module ActiveRecord | ||
module ConnectionAdapters | ||
class Mysql2Adapter | ||
private | ||
|
||
def _quote(value) | ||
if value.is_a?(Type::Uuid::Data) or value.is_a?(Type::Binary::Data) | ||
"x'#{value.hex}'" | ||
else | ||
super | ||
end | ||
end | ||
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 Type | ||
class Uuid < ActiveRecord::Type::Binary | ||
def type | ||
:uuid | ||
end | ||
|
||
# from database binary(16) to string | ||
def deserialize(value) | ||
return nil if value.nil? | ||
Data.from_database(value) | ||
end | ||
|
||
# from user input (string) to database | ||
def cast(value) | ||
if value.is_a?(Data) | ||
value | ||
elsif value.is_a?(ActiveSupport::ToJsonWithActiveSupportEncoder) or value.is_a?(String) | ||
Data.from_uuid_string(super) | ||
else | ||
raise ArgumentError, | ||
"Unsupported input data of class type #{value.class}" | ||
end | ||
end | ||
|
||
def serialize(value) | ||
value | ||
end | ||
|
||
def assert_valid_value(value) | ||
case value.class | ||
when String, ActiveSupport::ToJsonWithActiveSupportEncoder | ||
if value.downcase.gsub(/[^a-f0-9]/, '').size == 32 | ||
value | ||
else | ||
raise SerializationTypeMismatch, | ||
"Invalid String uuid #{value}." | ||
end | ||
else | ||
raise SerializationTypeMismatch, | ||
"Unsupported value object of type #{value.class}." | ||
end | ||
end | ||
|
||
class Data | ||
def initialize(display_format, storage_format) | ||
@display_format, @storage_format = display_format, storage_format | ||
end | ||
|
||
def self.from_uuid_string(uuid) | ||
new(uuid, uuid.downcase.gsub(/[^a-f0-9]/, '')) | ||
end | ||
|
||
def self.from_database(value) | ||
storage_format = value.unpack('H*').first.rjust(32, '0') | ||
new( | ||
storage_format.gsub(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '\1-\2-\3-\4-\5'), | ||
storage_format | ||
) | ||
end | ||
|
||
def to_s | ||
@display_format | ||
end | ||
alias_method :to_str, :to_s | ||
|
||
def hex | ||
@storage_format | ||
end | ||
|
||
def ==(other) | ||
other == to_s || super | ||
end | ||
end | ||
|
||
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