2020-07-06 14:53:03 +00:00
|
|
|
import {escape, EscapedValue, FieldSet, QuerySource} from './types.ts'
|
|
|
|
import { Select } from './type/Select.ts'
|
|
|
|
import RawValue from './RawValue.ts'
|
|
|
|
import {Statement} from './Statement.ts'
|
2020-07-19 19:13:48 +00:00
|
|
|
import {Update} from './type/Update.ts'
|
|
|
|
import {Insert} from './type/Insert.ts'
|
|
|
|
import {Delete} from './type/Delete.ts'
|
|
|
|
import {Truncate} from "./type/Truncate.ts";
|
2020-07-06 14:53:03 +00:00
|
|
|
|
|
|
|
export function raw(value: string) {
|
|
|
|
return new RawValue(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
export class IncorrectInterpolationError extends Error {
|
|
|
|
constructor(expected: number, received: number) {
|
|
|
|
super(`Unable to interpolate arguments into query. Expected ${expected} argument${expected === 1 ? '' : 's'}, but received ${received}.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 19:13:48 +00:00
|
|
|
export class Builder<T> {
|
|
|
|
// create table, alter table, drop table, select
|
2020-07-06 14:53:03 +00:00
|
|
|
|
2020-07-19 19:13:48 +00:00
|
|
|
public select(...fields: FieldSet[]): Select<T> {
|
2020-07-06 14:53:03 +00:00
|
|
|
fields = fields.flat()
|
2020-07-19 19:13:48 +00:00
|
|
|
const select = new Select<T>()
|
2020-07-06 14:53:03 +00:00
|
|
|
return select.fields(...fields)
|
|
|
|
}
|
|
|
|
|
2020-07-19 19:13:48 +00:00
|
|
|
public update(target?: QuerySource, alias?: string): Update<T> {
|
|
|
|
const update = new Update<T>()
|
2020-07-06 14:53:03 +00:00
|
|
|
if ( target ) update.to(target, alias)
|
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
2020-07-19 19:13:48 +00:00
|
|
|
public delete(target?: QuerySource, alias?: string): Delete<T> {
|
|
|
|
const del = new Delete<T>()
|
|
|
|
if ( target ) del.from(target, alias)
|
|
|
|
return del
|
|
|
|
}
|
|
|
|
|
|
|
|
public insert(target?: QuerySource, alias?: string): Insert<T> {
|
|
|
|
const insert = new Insert<T>()
|
2020-07-06 14:53:03 +00:00
|
|
|
if ( target ) insert.into(target, alias)
|
|
|
|
return insert
|
|
|
|
}
|
|
|
|
|
2020-07-19 19:13:48 +00:00
|
|
|
public statement(statement: string, ...interpolations: EscapedValue[]): Statement<T> {
|
|
|
|
return new Statement<T>(statement, interpolations)
|
|
|
|
}
|
|
|
|
|
|
|
|
public truncate(target?: QuerySource, alias?: string): Truncate<T> {
|
|
|
|
return new Truncate<T>(target, alias)
|
2020-07-06 14:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static raw(value: string) {
|
|
|
|
return new RawValue(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
public static default() {
|
|
|
|
return this.raw('DEFAULT')
|
|
|
|
}
|
|
|
|
}
|