Add support for subqueries
This commit is contained in:
@@ -4,7 +4,8 @@ import {
|
||||
WhereStatement,
|
||||
TableRef,
|
||||
OrderStatement,
|
||||
OrderDirection, HavingStatement
|
||||
OrderDirection, HavingStatement,
|
||||
escape,
|
||||
} from '../types.ts'
|
||||
import {WhereBuilder} from './WhereBuilder.ts'
|
||||
import {applyMixins} from '../../../../lib/src/support/mixins.ts'
|
||||
@@ -72,10 +73,13 @@ export class Select<T> extends ConnectionExecutable<T> {
|
||||
].filter(x => String(x).trim()).join(`\n${indent}`)
|
||||
}
|
||||
|
||||
field(field: string) {
|
||||
if ( !this._fields.includes(field) ) {
|
||||
field(field: string | Select<any>, as?: string) {
|
||||
if ( field instanceof Select ) {
|
||||
this._fields.push(`${escape(field)}${as ? ' AS '+as : ''}`)
|
||||
} else {
|
||||
this._fields.push(field)
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {WhereOperator} from '../../../lib/src/collection/Where.ts'
|
||||
import RawValue from './RawValue.ts'
|
||||
import {Select} from "./type/Select.ts";
|
||||
|
||||
export type FieldSet = string | string[]
|
||||
export type QuerySource = string | { ref: QuerySource, alias: string }
|
||||
@@ -20,7 +21,7 @@ export type HavingGroup = WhereGroup
|
||||
export type HavingStatement = HavingClause | HavingGroup
|
||||
export type SQLHavingOperator = SQLWhereOperator
|
||||
|
||||
export type EscapedValue = string | number | boolean | Date | RawValue | EscapedValue[]
|
||||
export type EscapedValue = string | number | boolean | Date | RawValue | EscapedValue[] | Select<any>
|
||||
|
||||
export type FieldValue = { field: string, value: EscapedValue }
|
||||
export type FieldValueObject = { [field: string]: EscapedValue }
|
||||
@@ -76,7 +77,9 @@ export function isWhereStatement(something: any): something is WhereStatement {
|
||||
}
|
||||
|
||||
export function escape(value: EscapedValue): string {
|
||||
if ( value instanceof RawValue ) {
|
||||
if ( value instanceof Select ) {
|
||||
return `(${value.sql(5)})`
|
||||
} else if ( value instanceof RawValue ) {
|
||||
return value.value
|
||||
} else if ( Array.isArray(value) ) {
|
||||
return `(${value.map(escape).join(',')})`
|
||||
|
||||
Reference in New Issue
Block a user