View Source Electric.Postgres.Identifiers (electric v0.7.3)

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.

Functions

Link to this function

downcase(ident, truncate \\ false, single_byte_encoding \\ false)

View Source

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

See: https://github.com/postgres/postgres/blob/259a0a99fe3d45dcf624788c1724d9989f3382dc/src/backend/parser/scansup.c#L46-L80

Examples

iex> Electric.Postgres.Identifiers.downcase("FooBar")
"foobar"
iex> Electric.Postgres.Identifiers.downcase(String.duplicate("a", 100), true)
String.duplicate("a", 63)
Link to this function

parse(ident, truncate \\ false, single_byte_encoding \\ false)

View Source
@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|}