Packages
postgrex
0.15.13
1.0.0-rc.1
retired
1.0.0-rc.0
retired
0.22.3
0.22.2
0.22.1
0.22.0
0.21.1
0.21.0
0.20.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.5
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.13
0.15.12
0.15.11
0.15.10
0.15.9
0.15.8
0.15.7
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.3
0.14.2
0.14.1
0.14.0
0.14.0-rc.1
0.14.0-rc.0
0.13.5
0.13.4
0.13.3
0.13.2
0.13.1
0.13.0
0.13.0-rc.0
0.12.2
0.12.1
0.12.0
0.11.2
0.11.1
0.11.0
0.10.0
0.9.1
0.9.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.0
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.2
PostgreSQL driver for Elixir
Current section
Files
Jump to
Current section
Files
lib/postgrex/utils.ex
defmodule Postgrex.Utils do
@moduledoc false
@extensions [
Postgrex.Extensions.Array,
Postgrex.Extensions.BitString,
Postgrex.Extensions.Bool,
Postgrex.Extensions.Box,
Postgrex.Extensions.Circle,
Postgrex.Extensions.Date,
Postgrex.Extensions.Float4,
Postgrex.Extensions.Float8,
Postgrex.Extensions.HStore,
Postgrex.Extensions.INET,
Postgrex.Extensions.Int2,
Postgrex.Extensions.Int4,
Postgrex.Extensions.Int8,
Postgrex.Extensions.Interval,
Postgrex.Extensions.JSON,
Postgrex.Extensions.JSONB,
Postgrex.Extensions.Line,
Postgrex.Extensions.LineSegment,
Postgrex.Extensions.MACADDR,
Postgrex.Extensions.Name,
Postgrex.Extensions.Numeric,
Postgrex.Extensions.OID,
Postgrex.Extensions.Path,
Postgrex.Extensions.Point,
Postgrex.Extensions.Polygon,
Postgrex.Extensions.Range,
Postgrex.Extensions.Raw,
Postgrex.Extensions.Record,
Postgrex.Extensions.TID,
Postgrex.Extensions.Time,
Postgrex.Extensions.Timestamp,
Postgrex.Extensions.TimestampTZ,
Postgrex.Extensions.TimeTZ,
Postgrex.Extensions.TSVector,
Postgrex.Extensions.UUID,
Postgrex.Extensions.VoidBinary,
Postgrex.Extensions.VoidText,
Postgrex.Extensions.Xid8
]
@doc """
Checks if a given extension is a default extension.
"""
for ext <- @extensions do
def default_extension?(unquote(ext)), do: true
end
def default_extension?(_), do: false
@doc """
List all default extensions.
"""
@spec default_extensions(Keyword.t()) :: [{module(), Keyword.t()}]
def default_extensions(opts \\ []) do
Enum.map(@extensions, &{&1, opts})
end
@doc """
Converts pg major.minor.patch (http://www.postgresql.org/support/versioning) version to an integer
"""
def parse_version(version) do
segments =
version
|> String.split(" ", parts: 2)
|> hd()
|> String.split(".", parts: 4)
|> Enum.map(&parse_version_bit/1)
case segments do
[major, minor, patch, _] -> {major, minor, patch}
[major, minor, patch] -> {major, minor, patch}
[major, minor] -> {major, minor, 0}
[major] -> {major, 0, 0}
end
end
@doc """
Fills in the given `opts` with default options.
"""
@spec default_opts(Keyword.t()) :: Keyword.t()
def default_opts(opts) do
opts
|> Keyword.put_new(:username, System.get_env("PGUSER") || System.get_env("USER"))
|> Keyword.put_new(:password, System.get_env("PGPASSWORD"))
|> Keyword.put_new(:database, System.get_env("PGDATABASE"))
|> Keyword.put_new(:hostname, System.get_env("PGHOST") || "localhost")
|> Keyword.update(:port, normalize_port(System.get_env("PGPORT")), &normalize_port/1)
|> Keyword.put_new(:types, Postgrex.DefaultTypes)
|> Enum.reject(fn {_k, v} -> is_nil(v) end)
end
defp normalize_port(port) when is_binary(port), do: String.to_integer(port)
defp normalize_port(port), do: port
@doc """
Return encode error message.
"""
def encode_msg(%Postgrex.TypeInfo{type: type}, observed, expected) do
"Postgrex expected #{to_desc(expected)} that can be encoded/cast to " <>
"type #{inspect(type)}, got #{inspect(observed)}. Please make sure the " <>
"value you are passing matches the definition in your table or in your " <>
"query or convert the value accordingly."
end
@doc """
Return encode error message.
"""
def encode_msg(%Date{calendar: calendar} = observed, _expected) when calendar != Calendar.ISO do
"Postgrex expected a %Date{} in the `Calendar.ISO` calendar, got #{inspect(observed)}. " <>
"Postgrex (and PostgreSQL) support dates in the `Calendar.ISO` calendar only."
end
def encode_msg(%NaiveDateTime{calendar: calendar} = observed, _expected)
when calendar != Calendar.ISO do
"Postgrex expected a %NaiveDateTime{} in the `Calendar.ISO` calendar, got #{inspect(observed)}. " <>
"Postgrex (and PostgreSQL) support naive datetimes in the `Calendar.ISO` calendar only."
end
def encode_msg(%DateTime{calendar: calendar} = observed, _expected)
when calendar != Calendar.ISO do
"Postgrex expected a %DateTime{} in the `Calendar.ISO` calendar, got #{inspect(observed)}. " <>
"Postgrex (and PostgreSQL) support datetimes in the `Calendar.ISO` calendar only."
end
def encode_msg(%Time{calendar: calendar} = observed, _expected) when calendar != Calendar.ISO do
"Postgrex expected a %Time{} in the `Calendar.ISO` calendar, got #{inspect(observed)}. " <>
"Postgrex (and PostgreSQL) support times in the `Calendar.ISO` calendar only."
end
def encode_msg(observed, expected) do
"Postgrex expected #{to_desc(expected)}, got #{inspect(observed)}. " <>
"Please make sure the value you are passing matches the definition in " <>
"your table or in your query or convert the value accordingly."
end
@doc """
Return type error message.
"""
def type_msg(%Postgrex.TypeInfo{type: json}, module)
when json in ["json", "jsonb"] do
"type `#{json}` can not be handled by the types module #{inspect(module)}, " <>
"it must define a `:json` library in its options to support JSON types"
end
def type_msg(%Postgrex.TypeInfo{type: type}, module) do
"type `#{type}` can not be handled by the types module #{inspect(module)}"
end
## Helpers
defp parse_version_bit(bit) do
{int, _} = Integer.parse(bit)
int
end
defp to_desc(struct) when is_atom(struct), do: "%#{inspect(struct)}{}"
defp to_desc(%Range{} = range), do: "an integer in #{inspect(range)}"
defp to_desc({a, b}), do: to_desc(a) <> " or " <> to_desc(b)
defp to_desc(desc) when is_binary(desc), do: desc
end