import { Op } from './attr_builder'; import { Raw } from './raw'; import { BuildResult, FieldsType, FieldType, IndexFor, IndexType, OrderFieldType, SQLTemplateArg, ValueRow, ValueType, WhereType } from './types'; export declare class Builder { private _quote; private _sql; private _params; private _one; private _nestTables; constructor(quote?: (str: string) => string); /** * clone this */ clone(): Builder; raw(str: string): Raw; quote(c: FieldType): Raw; q(c: FieldType): Raw; /** * op('views', '+', 100) => views + ?; [100] */ op(prep: FieldType, op?: string, value?: string | number | Op): Op; /** * param(1) => ?; [1] */ param(value: ValueType): this; /** * append sql */ append(sql: string | Builder, params?: ValueType[]): this; /** * append ES2015 template SQL * SQL`SELECT * FROM {user} WHERE {age} > ${100}` * => SELECT * FROM `user` WHERE `age` > ?; [100] */ SQL(strings: TemplateStringsArray, ...args: SQLTemplateArg[]): this; /** * fields(['id', 'name', { age: 'user_age', id: 'user_id' }]) => id, name, age as user_age, id as user_id * fields([{ user: ['id', 'name'], profile: ['edu', 'work'] }]) => user.id, user.name, profile.edu, profile.work * fields([{ user: { id: 'userId', name: 'user.Name' } }]) => user.id AS userId, user.name as user.Name * fields([{ asName: Builder() }]) => builder AS asName * fields([{ asName: Raw() }]) => raw AS asName */ fields(fields: FieldsType): this; /** * select() => SELECT * * select('id', 'name', { age: 'user_age', id: 'user_id' }, ...) => SELECT id, name, age as user_age, id as user_id * select({ user: ['id', 'name'], profile: ['edu', 'work'] }) => SELECT user.id, user.name, profile.edu, profile.work * select({ user: { id: 'userId', name: 'user.Name' } }) => SELECT user.id AS userId, user.name as user.Name * select({ asName: Builder() }) => builder AS asName * select({ asName: Raw() }) => raw AS asName */ select(...fields: FieldsType): this; /** * update('user', { name: 'yf', age: 18, }) => UPDATE user SET name = ?, age = ?; ['yf', 18] */ update(table: FieldType | FieldType[], columns: Partial): this; /** * insert('user', { name: 'yf', age: 18, }) => INSERT INTO user (name, age) VALUES (?, ?); ['yf', 18] */ insert(table: FieldType, columns: ValueRow | ValueRow[]): this; /** * delete('uset') => DELETE FROM user */ delete(table: FieldType): this; /** * as(raw('MAX(id)'), 'MAX_ID') */ as(from: FieldType, to: FieldType): Raw; /** * from('t') => FROM t * from('t', 'b') => FROM t AS b */ from(name: FieldType, alias?: FieldType): this; /** * index('a') => USE INDEX (a) */ index(name: string, type?: IndexType, for_?: IndexFor): this; /** * where({ username: 'test' }) => WHERE username = ?; ['test'] * where(AB) */ private _where; /** * join('user', null, { 'user.id': b.q('other.id') }) => INNER JOIN user ON (user.id = other.id) * join('user', 'u', { 'u.id': b.q('other.id') }) => INNER JOIN user AS u ON (u.id = other.id) */ join(table: FieldType, alias?: FieldType, on?: WhereType, joinType?: string): this; leftJoin(table: FieldType, alias?: FieldType, on?: WhereType): this; rightJoin(table: FieldType, alias?: FieldType, on?: WhereType): this; /** * where({ username: 'test' }) => WHERE username = ?; ['test'] * where(w => w.eq('username', 'test')) => WHERE username = ?; ['test'] */ where(query?: WhereType, prep?: string, after?: string): this; /** * func('COUNT', '*') => COUNT(*) * func('COUNT', '*', 'c') => COUNT(*) AS c */ func(name: string, exp: FieldType, alias?: FieldType): Raw; /** * count() => SELECT COUNT(*) * count('id') => SELECT COUNT(id) * count('id', 'user_count') => SELECT COUNT(id) AS user_count */ count(column?: FieldType, alias?: FieldType): this; /** * limit(100) => LIMIT 100 * limit(100, 100) => LIMIT 100 OFFSET 100 */ limit(count: number, offset?: number): this; /** * limit 1 */ one(offset?: number): this; setOne(is?: boolean): this; isOne(): boolean; nestTables(is?: boolean): this; getNestTables(): boolean; /** * order('id') => ORDER BY id ASC * order('-id') => ORDER BY id DESC * order('-created_at', '-id') => ORDER BY created_at DESC, id DESC * order(AB.raw('created_at IS NULL DESC'), '-created_at') => ORDER BY created_at IS NULL DESC, created_at DESC */ order(...fields: OrderFieldType[]): this; /** * group('id') => GROUP BY id */ group(...fields: FieldType[]): this; /** * having({ count: { $gt: 10 } }) => HAVING count > 10 */ having(query?: WhereType): this; /** * @returns sql and params */ build(): BuildResult; }