Skip to content

DbLangPg

do- edited this page May 6, 2023 · 42 revisions

DbLangPg is a class containing a collection of methods for generating/transforming SQL statements to be executed by a PostgreSQL server.

Methods of this class have no access to any database on their own, they deal with command texts only.

An instance of DbLangPg or its descendant is available as .lang property of each DbPoolPg and DbClientPg object.

Constants

DbLangPg.MAP_TYPE_ID_2_NAME and DbLangPg.MAP_TYPE_NAME_2_ID are PostgreSQL type Maps loaded from pg-types.

Methods

toParamsSql (query)

An overloaded version of DbQuery.toParamsSql that honors query.options.limit and query.options.offset.

addLimitOffset ([...params, sql], limit, offset = 0)

For given [...params, sql] array, limit and offset values:

  • pops sql,
  • pushes limit and offset into params
  • and repushes there appends sql with LIMIT ? OFFSET ? appended.

normalizeSQL (sql)

Returns the sql string with all comments stripped off and ?s replaced with $1, $2 and so on.

? occurring right after ::jsonb are not replaced (to let use JSONB operators).

genDropViews (model)

For a given model, this generator yields one ['DROP VIEW IF EXISTS ... CASCADE'] array with all the views quoted names, or nothing.

genCopyFromSql

For a given DbRelation name, its columns list and a bag of options, returns the COPY t (c1, c2... cn) FROM STDIN WITH (...) statement source.

genCopyFromSqlOptions

For a given bag of options, returns the WITH (...) clause to be used by genCopyFromSql. For an empty object, returns a zero length string.

genCopyFromSqlOption

For a given option name (in upper case) and the corresponding value, returns the string representation of value good to be used by genCopyFromSqlOptions.

genUpsertParamsSql

For a given DbRelation name, the data object representing a single record and the {key: [...]} option, returns an array or parameters followed by an INSERT INTO ... (f1, f2, ... fn) VALUES (?, ?,... ?) ON CONFLICT (${key}) DO UPDATE SET f1=EXCLUDED.f1,f2=EXCLUDED.f2,...fn=EXCLUDED.fn SQL string.

This is the base genInsertParamsSql method with the addition of ON CONFLICT clause to implement the upsert operation.

genCreateMockViews (model)

For a given model, this generator yields one [genCreateMockView ()] result per view.

genCreateViews (model)

For a given model, this generator yields one ['CREATE VIEW ...'] array per view.

genDDL (model)

For a given model, this generator yields a sequence of [sql, params] arrays to render the underlying database structure up to date with the model by executing with db.do. As of this writing, only views are recreated.

getCanonicalTypeName

For a given string acceptable data type name, returns its synonym from pg-types.

genSelectColumnsSql ()

Returns the SQL used by getStreamOfExistingTables. As of now, it is a getter for a constant string.

Clone this wiki locally