DbQueryTable - do-/node-doix-db GitHub Wiki

DbQueryTable is a class representing a part of the DbQuery's FROM clause.

Although it's called "...Table", as in SQL-92 "table reference" and, in simple case, its instance is related to some regular data table, in general, this data source may be any table like record set: a SQL VIEW, a function call etc. Anyway, in the context of the query it's meant to be a "table" of some kind.

Properties

Name Type Description
query DbQuery The query this table belongs to
lang DbLang Copy of query.lang
sql String The expression to be selected (left to the AS keyword in the FROM clause)
alias String The name of this table in this query (right to the AS keyword in the FROM clause)
qName String Copy of alias, safely quoted for interpolation in SQL
relation DbRelation The relation from the query.model.map which name is equal to sql. May be undefined.
columns [DbQueryColumn] DbQuery's columns selected from this table. May be undefined.
isFirst Boolean true for query.tables [0], false for all other
join String undefined when isFirst, otherwise one of 'LEFT', 'INNER' or 'CROSS'
on String logical expression to put after ON, verbatim
filters Array list of {sql, params} objects, such as DbQueryTableColumnComparison or DbQueryOr instances
unknownColumnComparisons Array list of [name, op, value] arrays, part of filters option with name not in relation.columns (if defined)

Constructor

Parameters

Name Type Description
query DbQuery The query this table belongs to
sql String The name of a DbRelation from the query.model or an arbitrary expression to be selected
options Object A bag of options

Options

Name Type Description
as String The name of this table in this query (defaults to sql if it's a relation name)
columns [String] The list of column names to select from the relation. If undefined, means all columns (as in SELECT *). Zero length array means no column must be selected from this table (it's joined only to filter data)
join String for tables other than query.tables [0], 'LEFT' by default
on String the JOIN ... ON condition or an argument to derive it with adjustJoinCondition
filters [Array] list of lists of arguments for addColumnComparison

Methods

adjustJoinCondition

This internal method is called from the constructor to calculate the final value of this.on property in case where join is LEFT or INNER, but on is not a string containing the '=' sign (and so, is not believed to be good for interpolation into the ON clause).

If this.on is a string ${referringTableAlias}.${referringColumnName}, it is appended to be the equality to the first column of this relation's primary key.

If this.on is undefined, there must be exactly one DbReference to the underlying relation among preceding query.tables, and it will be used to derive the real joinCondition.

addColumnComparison

Calls createColumnComparison with same parameters and then addFilter with its result.

addFilter

For a null or undefined argument, does nothing. Otherwise, pushes the argument into filters.

createColumnComparison

Given name, op and value, this method returns a new instance of DbQueryTableColumnComparison bound to this table and carrying incoming parameters.

If the value is null or undefined and isUnaryOperator (op) is false, returns null (that is, fields left empty are ignored, which is a standard practice for search forms).

For a non-empty value, if the relation is defined, but the name corresponds to none of its columns, the return value, too, is null, but in this case [name, op, value] is pushed into unknownColumnComparisons as a side effect (to implement additional search parameters that cannot be directly bound to individual columns).