Skip to content

QueryGeneratorBase

Wyatt Greenway edited this page Dec 27, 2022 · 12 revisions

class QueryGeneratorBase 📜

The base query generator class.

A "query generator" is an interface that will take parameters (usually a QueryEngine or a Model) and generate database query statements from the input. For SQL type databases this would mean generating SELECT, INSERT, UPDATE, and DELETE statements, as well as generators for creating and altering tables, among other things.

The methods of this class are generally many, with the design pattern for most generators being that nearly all methods are split apart and added to the class, allowing by deliberate design much finer control when using overloaded methods, to modify or replace any parts of the generator.

Any connection can be provided a custom query generator interface via the queryGenerator option that can be used when instantiating a connection. Most connections will supply their own by default.

A connection should always be bound to a query generator instance. If not when first created, then at least when provided to a connection. The connection is a required part of the generator interface, and will do things, such as for example, escaping values and ids using the connection itself. The connection may be used for other operations as well.

Notes:

  • Some database drivers may not have a generator at all. Though database drivers commonly do have a database statement generator, a connection isn't required to have one.

method QueryGeneratorBase::_averageLiteralToString(
    literal: AverageLiteral,
    options?: object,
): string
📜

Convert an AverageLiteral into a string representation for the underlying database.

It is expected that each database driver will implement this method. By default it will simply throw an "unsupported" error.

Refer to the specific documentation for your database driver for more information.

Arguments:

  • literal: AverageLiteral

    The literal to stringify for the underlying database.

  • options?: object

    Options for the stringify process. These are often database driver specific. However, one common option is the as option, which will allow you to give your literal an alias.

Return value: string

The literal, converted into the proper string for the underlying database.


method QueryGeneratorBase::_countLiteralToString(
    literal: CountLiteral,
    options?: object,
): string
📜

Convert an CountLiteral into a string representation for the underlying database.

It is expected that each database driver will implement this method. By default it will simply throw an "unsupported" error.

Refer to the specific documentation for your database driver for more information.

Arguments:

  • literal: CountLiteral

    The literal to stringify for the underlying database.

  • options?: object

    Options for the stringify process. These are often database driver specific. However, one common option is the as option, which will allow you to give your literal an alias.

Return value: string

The literal, converted into the proper string for the underlying database.


method QueryGeneratorBase::_distinctLiteralToString(
    literal: DistinctLiteral,
    options?: object,
): string
📜

Convert an DistinctLiteral into a string representation for the underlying database.

It is expected that each database driver will implement this method. By default it will simply throw an "unsupported" error.

Refer to the specific documentation for your database driver for more information.

Arguments:

  • literal: DistinctLiteral

    The literal to stringify for the underlying database.

  • options?: object

    Options for the stringify process. These are often database driver specific. However, one common option is the as option, which will allow you to give your literal an alias.

Return value: string

The literal, converted into the proper string for the underlying database.


method QueryGeneratorBase::_fieldLiteralToString(
    literal: FieldLiteral,
    options?: object,
): string
📜

Convert an FieldLiteral into a string representation for the underlying database.

It is expected that each database driver will implement this method. By default it will simply throw an "unsupported" error.

Refer to the specific documentation for your database driver for more information.

Arguments:

  • literal: FieldLiteral

    The literal to stringify for the underlying database.

  • options?: object

    Options for the stringify process. These are often database driver specific. However, one common option is the as option, which will allow you to give your literal an alias.

Return value: string

The literal, converted into the proper string for the underlying database.


method QueryGeneratorBase::_maxLiteralToString(
    literal: MaxLiteral,
    options?: object,
): string
📜

Convert an MaxLiteral into a string representation for the underlying database.

It is expected that each database driver will implement this method. By default it will simply throw an "unsupported" error.

Refer to the specific documentation for your database driver for more information.

Arguments:

  • literal: MaxLiteral

    The literal to stringify for the underlying database.

  • options?: object

    Options for the stringify process. These are often database driver specific. However, one common option is the as option, which will allow you to give your literal an alias.

Return value: string

The literal, converted into the proper string for the underlying database.


method QueryGeneratorBase::_minLiteralToString(
    literal: MinLiteral,
    options?: object,
): string
📜

Convert an MinLiteral into a string representation for the underlying database.

It is expected that each database driver will implement this method. By default it will simply throw an "unsupported" error.

Refer to the specific documentation for your database driver for more information.

Arguments:

  • literal: MinLiteral

    The literal to stringify for the underlying database.

  • options?: object

    Options for the stringify process. These are often database driver specific. However, one common option is the as option, which will allow you to give your literal an alias.

Return value: string

The literal, converted into the proper string for the underlying database.


method QueryGeneratorBase::_sumLiteralToString(
    literal: SumLiteral,
    options?: object,
): string
📜

Convert an SumLiteral into a string representation for the underlying database.

It is expected that each database driver will implement this method. By default it will simply throw an "unsupported" error.

Refer to the specific documentation for your database driver for more information.

Arguments:

  • literal: SumLiteral

    The literal to stringify for the underlying database.

  • options?: object

    Options for the stringify process. These are often database driver specific. However, one common option is the as option, which will allow you to give your literal an alias.

Return value: string

The literal, converted into the proper string for the underlying database.


method QueryGeneratorBase::constructor(
    connection?: Connection,
): QueryGeneratorBase
📜

Construct a new query generator.

Arguments:

  • connection?: Connection

    The connection that this interface is for. Sometimes the connection isn't yet available when creating the query generator, so this argument is optional. When provided to a Connection, the connection will call QueryGenerator.setConnection with itself to set the connection for the query generator.

Return value: QueryGeneratorBase


method QueryGeneratorBase::escape(
    args,
)
📜

This call proxies to Connection.escape. Refer to the documentation of that method for more information.

Arguments:

  • args

See also: Connection.escape


method QueryGeneratorBase::escapeID(
    args,
)
📜

This call proxies to Connection.escapeID. Refer to the documentation of that method for more information.

Arguments:

  • args

See also: Connection.escapeID


method QueryGeneratorBase::getConnection(): Connection 📜

Get the Connection bound to this query generator.

Return value: Connection

The connection bound to this query generator. A connection should always be bound before any generating methods of the class are called.


method QueryGeneratorBase::getFieldDefaultValue(
    field: Field,
    fieldName: string,
    options?: object,
): any
📜

Get the "default value" for the given field for the underlying database. This is used primarily for "CREATE TABLE" statements.

By default, the implementation of this method is empty. It is expected that each database driver will implement their own version of this method.

Arguments:

  • field: Field

    The field instance we are getting a "default value" from.

  • fieldName: string

    The name of the field that we are getting the "default value" from. This should always be the same as field.fieldName.

  • options?: object

    Options for the operation. These will likely be connection specific. Please refer to the documentation of your specific connection for more details.

Return value: any

Though in most cases this method will return a string for most database drivers in most situations, it may return other types as well, such as literals, or other raw values. Please refer to the documentation of your specific connection for more details.


method QueryGeneratorBase::getIndexFieldsFromFieldIndex(
    field: Field,
): Array<Array<string>>
📜

Take a Field instance, and generate all index fields for this field.

The index property on a Field is used to generate indexes for the field. If a true value is encountered in the index, it means "this field". If another field name is encountered in the index property, then that means "combine this field with the specified field" (to create a combined index). Take the following example:

...
firstName: {
  ...
  index: [
    true,
    'lastName',
    [ 'email', 'lastName' ],
  ]
}
...

This will create three indexes for this "firstName" column in the database:

  1. true = Index the firstName column by itself (idx_users_firstName)
  2. lastName = Combine firstName and lastName to create a combined index (idx_users_firstName_lastName)
  3. [ 'email', 'lastName' ] = Combine firstName with email and lastName to create a combined index involving three columns (idx_users_firstName_email_lastName) Using the above example, this method would return the following:
[
  [ 'firstName' ],
  [ 'firstName', 'lastName' ],
  [ 'firstName', 'email', 'lastName' ],
]

Which is the list of indexes, and the fields to index for each index.

Arguments:

  • field: Field

    The field used to pull the index property from.

Return value: Array<Array<string>>

Return an array of arrays, where the top-level array lists the indexes, and each sub-array lists the fields to combine into an index.


method QueryGeneratorBase::setConnection(
    connection: Connection,
): QueryGenerator
📜

Set the Connection bound to this query generator.

Arguments:

  • connection: Connection

    The connection to bind to this query generator.

Return value: QueryGenerator

Return this to allow for chaining.


method QueryGeneratorBase::stackAssign(
    obj,
    args,
)
📜

This call proxies to Connection.stackAssign. Refer to the documentation of that method for more information.

Arguments:

  • obj
  • args

See also: Connection.stackAssign


method QueryGeneratorBase::toConnectionString(
    queryEngine: QueryEngine,
    options?: object,
): string
📜

Take a QueryEngine instance and convert it into a query. For SQL type databases this would turn a QueryEngine into a SELECT statement. For other types of databases, this should return a "fetch" query--or string representation of such a query--in the database's native query language.

Arguments:

  • queryEngine: QueryEngine

    The query engine instance to stringify.

  • options?: object

    Connection and operation specific options. These generally aren't needed, but are provided in case the underlying connection needs them.

Return value: string

A "fetch" query in the databases native query language, generated from the provided queryEngine.



Clone this wiki locally