You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

import {JoinOperator, TableRef, WhereStatement} from '../../types.ts'
import {TableRefBuilder} from '../TableRefBuilder.ts'
import {applyMixins} from '../../../../../lib/src/support/mixins.ts'
import {WhereBuilder} from '../WhereBuilder.ts'
import {Scope} from '../../Scope.ts'
/**
* Query builder class which builds JOIN clauses.
*/
export class Join {
/**
* The join operator to use in the SQL.
* @type JoinOperator
*/
public readonly operator: JoinOperator = 'JOIN'
/**
* The where statements applied to this join. (i.e. JOIN table ON ...)
* @type Array<WhereStatement>
*/
protected _wheres: WhereStatement[] = []
/**
* The scopes applied to this join.
* @type Array<Scope>
*/
protected _scopes: Scope[] = []
constructor(
/**
* The table ref being joined.
* @type TableRef
*/
public readonly table_ref: TableRef
) {}
/**
* Serialize the join to raw SQL.
* @param level
*/
sql(level = 0): string {
const indent = Array(level * 2).fill(' ').join('')
return [
`${this.operator} ${this.serialize_table_ref(this.table_ref)}`,
...[this._wheres.length > 0 ? ['ON'] : []],
this.wheres_to_sql(this._wheres, level),
].filter(Boolean).join(`\n${indent}`)
}
}
export interface Join extends TableRefBuilder, WhereBuilder {}
applyMixins(Join, [TableRefBuilder, WhereBuilder])