-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
Ecto.Adapters.MyXQL.Constraint
and delegate from the connection
- Loading branch information
1 parent
e5ea9f6
commit ca6f1bc
Showing
3 changed files
with
52 additions
and
40 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,45 @@ | ||
defmodule Ecto.Adapters.MyXQL.Constraint do | ||
@moduledoc false | ||
|
||
@behaviour Ecto.Adapters.SQL.Constraint | ||
|
||
@quotes ~w(" ' `) | ||
|
||
@impl true | ||
def to_constraints(%MyXQL.Error{mysql: %{name: :ER_DUP_ENTRY}, message: message}, opts) do | ||
with [_, quoted] <- :binary.split(message, " for key "), | ||
[_, index | _] <- :binary.split(quoted, @quotes, [:global]) do | ||
[unique: strip_source(index, opts[:source])] | ||
else | ||
_ -> [] | ||
end | ||
end | ||
|
||
def to_constraints(%MyXQL.Error{mysql: %{name: name}, message: message}, _opts) | ||
when name in [:ER_ROW_IS_REFERENCED_2, :ER_NO_REFERENCED_ROW_2] do | ||
with [_, quoted] <- :binary.split(message, [" CONSTRAINT ", " FOREIGN KEY "]), | ||
[_, index | _] <- :binary.split(quoted, @quotes, [:global]) do | ||
[foreign_key: index] | ||
else | ||
_ -> [] | ||
end | ||
end | ||
|
||
def to_constraints( | ||
%MyXQL.Error{mysql: %{name: :ER_CHECK_CONSTRAINT_VIOLATED}, message: message}, | ||
_opts | ||
) do | ||
with [_, quoted] <- :binary.split(message, ["Check constraint "]), | ||
[_, constraint | _] <- :binary.split(quoted, @quotes, [:global]) do | ||
[check: constraint] | ||
else | ||
_ -> [] | ||
end | ||
end | ||
|
||
def to_constraints(_, _), | ||
do: [] | ||
|
||
defp strip_source(name, nil), do: name | ||
defp strip_source(name, source), do: String.trim_leading(name, "#{source}.") | ||
end |