Skip to content

PostgreSQLGrammar

PostgreSQL-specific query grammar implementation. Compiles query components into SQL statements for PostgreSQL. Supports features like CTEs, ON CONFLICT (UPSERT), RETURNING, table inheritance, schema-qualified identifiers, and more.

compileSelect

Compiles a SELECT query into SQL.

Parameters

NameDescription
queryQuery components including ctes, columns, from, joins,
           wheres, groups, havings, orders, limit, offset. |

returns — The compiled SQL SELECT statement.

compileInsert

Compiles an INSERT query into SQL, supporting ON CONFLICT and RETURNING.

Parameters

NameDescription
queryContains table, values, optional onConflict, and returning columns.

returns — The compiled SQL INSERT statement.

compileUpdate

Compiles an UPDATE query into SQL with optional RETURNING.

Parameters

NameDescription
queryContains table, values, wheres, and optional returning columns.

returns — The compiled SQL UPDATE statement.

compileDelete

Compiles a DELETE query into SQL with optional RETURNING.

Parameters

NameDescription
queryContains table, wheres, and optional returning columns.

returns — The compiled SQL DELETE statement.

compileWheres

Compiles WHERE clauses into SQL. Supports basic, IN, NOT IN, BETWEEN, NULL checks, EXISTS, and raw.

Parameters

NameDescription
wheresArray of where clause objects.

returns — The compiled WHERE clause string.

compileJoins

Compiles JOIN clauses into SQL.

Parameters

NameDescription
joinsArray of join clause objects.

returns — The compiled JOIN clause string.

compileCtes

Compiles CTEs into SQL.

Parameters

NameDescription
ctesArray of CTE definition objects.

returns — The compiled CTE list string.

wrapTable

Wraps a table name with double quotes for schema-qualified identifiers.

Parameters

NameDescription
tableTable name, optionally schema-qualified.

returns — The wrapped table name.

wrapColumn

Wraps a column name with double quotes.

Parameters

NameDescription
columnColumn name, optionally table-qualified.

returns — The wrapped column name.

parameter

Returns the parameter placeholder for PostgreSQL ($1, $2,...).

Parameters

NameDescription
indexThe 1-based index of the parameter.

returns — The placeholder string (e.g., $index).

getColumnType

Maps a ColumnDefinition to its PostgreSQL column type.

Parameters

NameDescription
columnColumn definition object.

returns — The SQL column type.

formatDefault

Formats default values for SQL.

Parameters

NameDescription
defDefault value.

returns — The formatted default clause.

compileCreateTable

Compiles a CREATE TABLE statement for PostgreSQL. Supports column definitions, PRIMARY KEY, UNIQUE, DEFAULT, and INHERITS.

Parameters

NameDescription
definitionTableDefinition with columns and optional inherits.

returns — SQL CREATE TABLE string.

compileAlterTable

Compiles an ALTER TABLE statement for PostgreSQL. Supports adding columns only.

Parameters

NameDescription
definitionTableDefinition with new columns.

returns — SQL ALTER TABLE string.

compileTableExists

Checks if a table exists in the current schema.

Parameters

NameDescription
tableTable name to check.

returns — SQL SELECT against information_schema.tables.

compileColumnExists

Checks if a column exists in a given table.

Parameters

NameDescription
tableTable name.
columnColumn name.

returns — SQL SELECT against information_schema.columns.

rename

Renames a table in PostgreSQL.

Parameters

NameDescription
fromCurrent table name.
toNew table name.

compileColumnDefinition

Compiles a single column definition for CREATE TABLE.

Parameters

NameDescription
columnColumnDefinition object.

returns — The compiled column definition string.

Released under the MIT License.