View Source Electric.Postgres.Identifiers (electric v0.9.5)
Summary
Functions
Downcase the identifier and truncate if necessary, 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 and truncate if necessary, using PostgreSQL's algorithm for downcasing.
Setting truncate
to true
will truncate the identifier to 63 characters
Setting single_byte_encoding
to true
will downcase the identifier
using single byte encoding
Examples
iex> Electric.Postgres.Identifiers.downcase("FooBar")
"foobar"
iex> Electric.Postgres.Identifiers.downcase(String.duplicate("a", 100), true)
String.duplicate("a", 63)
@spec parse(binary(), boolean(), boolean()) :: {:ok, binary()} | {:error, term()}
@spec parse(binary(), boolean(), boolean()) :: {:ok, binary()} | {:error, term()}
Parse a PostgreSQL identifier, removing quotes if present and escaping internal ones and downcasing the identifier otherwise.
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(~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_unquoted_identifier(ident, truncate \\ false, single_byte_encoding \\ false)
View SourceParse 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|}