Electric.Postgres.Identifiers (electric v1.0.24)
View SourceSummary
Functions
Downcase the identifier using PostgreSQL's algorithm for downcasing.
Parse a PostgreSQL identifier, removing quotes if present and escaping internal ones and downcasing the identifier otherwise.
Parse a PostgreSQL relation identifier
Parse an unquoted PostgreSQL identifier, downcasing characters and failing if any special characters are present
Functions
Downcase the identifier using PostgreSQL's algorithm for downcasing.
Setting single_byte_encoding
to true
will downcase the identifier
using single byte encoding
Examples
iex> Electric.Postgres.Identifiers.downcase("FooBar")
"foobar"
Parse a PostgreSQL identifier, removing quotes if present and escaping internal ones and downcasing the identifier otherwise.
Postgres identifiers are limited to 63 characters - Postgres will truncate them, but we'll fail if the identifier is too long to avoid api injection issues.
Examples
iex> Electric.Postgres.Identifiers.parse("FooBar")
{:ok, "foobar"}
iex> Electric.Postgres.Identifiers.parse(~S|"FooBar"|)
{:ok, "FooBar"}
iex> Electric.Postgres.Identifiers.parse(~S|Foo"Bar"|)
{:error, ~S|Invalid unquoted identifier contains special characters: Foo"Bar"|}
iex> Electric.Postgres.Identifiers.parse(~S| |)
{:error, ~S|Invalid unquoted identifier contains special characters: |}
iex> Electric.Postgres.Identifiers.parse("foob@r")
{:error, ~S|Invalid unquoted identifier contains special characters: foob@r|}
iex> Electric.Postgres.Identifiers.parse(~S|"Foo"Bar"|)
{:error, ~S|Invalid identifier with unescaped quote: Foo"Bar|}
iex> Electric.Postgres.Identifiers.parse(~S|""|)
{:error, "Invalid zero-length delimited identifier"}
iex> Electric.Postgres.Identifiers.parse("")
{:error, "Invalid zero-length delimited identifier"}
iex> Electric.Postgres.Identifiers.parse(for(_ <- 1..64, into: "", do: "a"))
{:error, "Identifier is too long (max length is 63)"}
iex> Electric.Postgres.Identifiers.parse(~S|" "|)
{:ok, " "}
iex> Electric.Postgres.Identifiers.parse(~S|"Foo""Bar"|)
{:ok, ~S|Foo"Bar|}
@spec parse_relation(binary()) :: {:ok, Electric.relation()} | {:error, term()}
Parse a PostgreSQL relation identifier
Examples
iex> Electric.Postgres.Identifiers.parse_relation("foo")
{:ok, {"public", "foo"}}
iex> Electric.Postgres.Identifiers.parse_relation("foo.bar")
{:ok, {"foo", "bar"}}
iex> Electric.Postgres.Identifiers.parse_relation(~S|"foo"."bar"|)
{:ok, {"foo", "bar"}}
iex> Electric.Postgres.Identifiers.parse_relation(~S|"foo.woah"."bar"|)
{:ok, {"foo.woah", "bar"}}
iex> Electric.Postgres.Identifiers.parse_relation(~S|"foo".bar|)
{:ok, {"foo", "bar"}}
iex> Electric.Postgres.Identifiers.parse_relation(~S|"foo"."bar|)
{:error, ~S|Invalid unquoted identifier contains special characters: "bar|}
iex> Electric.Postgres.Identifiers.parse_relation("foo.bar.baz")
{:error, "Invalid relation identifier, too many delimiters: foo.bar.baz"}
Parse an unquoted PostgreSQL identifier, downcasing characters and failing if any special characters are present
Examples
iex> Electric.Postgres.Identifiers.parse_unquoted_identifier("FooBar")
{:ok, "foobar"}
iex> Electric.Postgres.Identifiers.parse_unquoted_identifier("foob@r")
{:error, ~S|Invalid unquoted identifier contains special characters: foob@r|}