Skip to content

Commit ede0d53

Browse files
authored
Advance to "Ensure adapters work primarily on primitive types" commit. (#20)
1 parent 87e9ed3 commit ede0d53

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

lib/sqlite_ecto.ex

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Sqlite.Ecto do
1+
defmodule Sqlite.Ecto do
22
@moduledoc ~S"""
33
Ecto Adapter module for SQLite.
44
@@ -27,8 +27,6 @@ defmodule Sqlite.Ecto do
2727
2828
"""
2929

30-
import Sqlite.Ecto.Connection, only: [json_library: 0]
31-
3230
# Inherit all behaviour from Ecto.Adapters.SQL
3331
use Ecto.Adapters.SQL, :sqlitex
3432

@@ -37,13 +35,21 @@ defmodule Sqlite.Ecto do
3735

3836
## Custom SQLite Types
3937

40-
def load({:embed, _} = type, binary) when is_binary(binary) do
41-
super(type, json_library.decode!(binary))
42-
end
43-
def load(:map, binary) when is_binary(binary) do
44-
super(:map, json_library.decode!(binary))
45-
end
46-
def load(type, value), do: super(type, value)
38+
def loaders(:map, type), do: [&json_decode/1, type]
39+
def loaders(:boolean, type), do: [&bool_decode/1, type]
40+
def loaders(:binary_id, type), do: [Ecto.UUID, type]
41+
def loaders({:embed, _} = type, _),
42+
do: [&json_decode/1, &Ecto.Adapters.SQL.load_embed(type, &1)]
43+
def loaders(_primitive, type), do: [type]
44+
45+
defp bool_decode(0), do: {:ok, false}
46+
defp bool_decode(1), do: {:ok, true}
47+
defp bool_decode(x), do: {:ok, x}
48+
49+
defp json_decode(x) when is_binary(x),
50+
do: {:ok, Application.get_env(:ecto, :json_library).decode!(x)}
51+
defp json_decode(x),
52+
do: {:ok, x}
4753

4854
## Storage API
4955

lib/sqlite_ecto/connection.ex

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ if Code.ensure_loaded?(Sqlitex.Server) do
355355
defp distinct(nil, _sources), do: ""
356356
defp distinct(%QueryExpr{expr: true}, _exprs), do: "DISTINCT "
357357
defp distinct(%QueryExpr{expr: false}, _exprs), do: ""
358-
defp distinct(_query, exprs) do
358+
defp distinct(_query, _exprs) do
359359
raise ArgumentError, "DISTINCT with multiple columns is not supported by SQLite"
360360
end
361361

@@ -698,8 +698,6 @@ if Code.ensure_loaded?(Sqlitex.Server) do
698698
end
699699
defp table_identifier(_stmt, table, _pos), do: quote_id(table)
700700

701-
defp fragment_identifier(pos), do: "f" <> Integer.to_string(pos)
702-
703701
# DDL
704702

705703
alias Ecto.Migration.Table
@@ -888,7 +886,7 @@ if Code.ensure_loaded?(Sqlitex.Server) do
888886
## Helpers
889887

890888
# Use Ecto's JSON library (currently Poison) for embedded JSON datatypes.
891-
def json_library, do: Application.get_env(:ecto, :json_library)
889+
defp json_library, do: Application.get_env(:ecto, :json_library)
892890

893891
# Initiate a transaction with a savepoint. If any error occurs when we call
894892
# the func parameter, rollback our changes. Returns the result of the call
@@ -988,14 +986,6 @@ if Code.ensure_loaded?(Sqlitex.Server) do
988986
list |> Enum.map(&func.(&1)) |> Enum.intersperse(item)
989987
end
990988

991-
defp if_do(condition, value) do
992-
if condition, do: value, else: []
993-
end
994-
995-
defp escape_string(value) when is_binary(value) do
996-
:binary.replace(value, "'", "''", [:global])
997-
end
998-
999989
defp error!(nil, message) do
1000990
raise ArgumentError, message: message
1001991
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule Sqlite.Ecto.Mixfile do
3333
defp deps do
3434
[{:coverex, "~> 1.4.11", only: :coverage},
3535
{:ex_doc, "~> 0.14.5", only: :dev},
36-
{:ecto, git: "https://github.com/scouten/ecto.git", ref: "bfad2326dedb0f7cfb5ff0272cb6810e3c29d341"},
36+
{:ecto, git: "https://github.com/scouten/ecto.git", ref: "5490aa66ac2115225a6219d3bd371bc58c00dc98"},
3737
{:poison, "~> 1.0"},
3838
{:sqlitex, git: "https://github.com/scouten/sqlitex.git", ref: "c997c613a69ece59d8dd6b7e7ee557d4c4a1c709"}]
3939
end

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"db_connection": {:git, "https://github.com/fishcakez/db_connection.git", "3e169b0b595276cc44fc935b200f428bd1abc08d", []},
55
"decimal": {:hex, :decimal, "1.1.1", "a8ff5b673105e6cdaca96f799aeefc6f07142881b616c65db16e14e556b16e76", [:mix], []},
66
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []},
7-
"ecto": {:git, "https://github.com/scouten/ecto.git", "bfad2326dedb0f7cfb5ff0272cb6810e3c29d341", [ref: "bfad2326dedb0f7cfb5ff0272cb6810e3c29d341"]},
7+
"ecto": {:git, "https://github.com/scouten/ecto.git", "5490aa66ac2115225a6219d3bd371bc58c00dc98", [ref: "5490aa66ac2115225a6219d3bd371bc58c00dc98"]},
88
"esqlite": {:hex, :esqlite, "0.2.1", "0e2896f177180b33e5a8fe65342b4e81de46e0db59e4cbf6e8b9101e98d423fd", [:make, :rebar], []},
99
"ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
1010
"hackney": {:hex, :hackney, "1.6.5", "8c025ee397ac94a184b0743c73b33b96465e85f90a02e210e86df6cbafaa5065", [:rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},

0 commit comments

Comments
 (0)