import { Callback, KVObject } from "./define"; import { Schema } from "./schema"; export interface QueryBuilderOptions { /** * 表名 */ table: string; /** * 可选的 Schema */ schema?: Schema; /** * 执行查询 */ exec: QueryBuilderExecFunction; } export interface QueryBuilderExecFunction { /** * 执行查询 * @param sql SQL 语句 * @param callback 回调函数 */ (sql: string, callback?: Callback): void; } export interface QueryOptionsParams { /** * 跳过的行数 */ skip?: number; /** * 返回的行数 */ limit?: number; /** * 排序方向 */ orderBy?: string; /** * 分组 */ groupBy?: string; /** * 返回字段列表 */ fields?: string[]; } export declare class QueryBuilder { private _tableName; private _tableNameEscaped; private _execCallback; private _data; private _schema; /** * 创建 QueryBuilder */ constructor(options: QueryBuilderOptions); /** * 格式化模板字符串 * @param tpl 模板字符串 */ format(tpl: string): string; /** * 格式化模板字符串 * @param tpl 模板字符串 * @param values 键值对数据 */ format(tpl: string, values: KVObject): string; /** * 格式化模板字符串 * @param tpl 模板字符串 * @param values 参数数组 */ format(tpl: string, values: any[]): string; /** * 查询条件 * @param condition 键值对数据:{ aaa: 1, bbb: 22 }) */ where(condition: KVObject): this; /** * 查询条件 * @param condition SQL 语句 */ where(condition: string): this; /** * 查询条件 * @param condition 模板字符串,可以为 ('aaa=:a AND bbb=:b', { a: 123, b: 456 }) 或 ('aaa=? AND bbb=?', [ 123, 456 ]) */ where(condition: string, values: KVObject | any[]): this; /** * 查询条件 * @param condition 键值对数据:{ aaa: 1, bbb: 22 }) */ and(condition: KVObject): this; /** * 查询条件 * @param condition SQL 语句 */ and(condition: string): this; /** * 查询条件 * @param condition 模板字符串,可以为 ('aaa=:a AND bbb=:b', { a: 123, b: 456 }) */ and(condition: string, values: KVObject): this; /** * 查询条件 * @param condition 模板字符串,可以为 ('aaa=? AND bbb=?', [ 123, 456 ]) */ and(condition: string, values: any[]): this; /** * 查询的字段 * @param fields 要查询的字段 */ select(...fields: string[]): this; /** * 设置查询字段 * @param fields 要查询的字段 */ fields(...fields: string[]): this; /** * 查询数量 * @param name 存储结果的字段名 */ count(name: string): this; /** * 更新 */ update(): this; /** * 更新 * @param update 键值对数据,如 { a: 123, b: 456 } */ update(update: KVObject): this; /** * 更新 * @param update SQL 语句,如 a=a+1 */ update(update: string): this; /** * 更新 * @param update SQL 语句模板,如 a=:a * @param values 模板参数,如 { a: 123 } */ update(update: string, values: KVObject): this; /** * 更新 * @param update SQL 语句模板,如 a=? * @param values 模板参数,如 [ 123 ] */ update(update: string, values: any[]): this; /** * 更新 * @param update 键值对数据,如 { a: 123, b: 456 } */ set(update: KVObject): this; /** * 更新 * @param update SQL 语句,如 a=a+1 */ set(update: string): this; /** * 更新 * @param update SQL 语句模板,如 a=:a * @param values 模板参数,如 { a: 123 } */ set(update: string, values: KVObject): this; /** * 更新 * @param update SQL 语句模板,如 a=? * @param values 模板参数,如 [ 123 ] */ set(update: string, values: any[]): this; /** * 插入 * @param data 键值对数据 */ insert(data: KVObject): this; /** * 插入 * @param data 键值对数据数组 */ insert(data: KVObject[]): this; /** * 删除 */ delete(): this; /** * 自定义SQL语句 * @param sql SQL 查询语句 */ sql(sql: string): this; /** * 自定义SQL语句 * @param sql SQL 查询语句 * @param values 模板参数,如 { a: 123 } */ sql(sql: string, values: KVObject): this; /** * 自定义SQL语句 * @param sql SQL 查询语句 * @param values 模板参数,如 [ 123 ] */ sql(sql: string, values: any[]): this; /** * 排序方法 * @param tpl SQL 查询语句 */ orderBy(tpl: string): this; /** * 排序方法 * @param tpl SQL 查询语句 * @param values 模板参数,如 { a: 123 } */ orderBy(tpl: string, values: KVObject): this; /** * 排序方法 * @param tpl SQL 查询语句 * @param values 模板参数,如 [ 123 ] */ orderBy(tpl: string, values: any[]): this; /** * 分组方法 * @param tpl SQL 查询语句 */ groupBy(tpl: string): this; /** * 分组方法 * @param tpl SQL 查询语句 * @param values 模板参数,如 { a: 123 } */ groupBy(tpl: string, values: KVObject): this; /** * 分组方法 * @param tpl SQL 查询语句 * @param values 模板参数,如 [ 123 ] */ groupBy(tpl: string, values: any[]): this; /** * 跳过指定行数 * @param rows 行数 */ skip(rows: number): this; /** * 返回指定行数 * @param rows 行数 */ limit(rows: number): this; /** * 批量设置 options * @param options 选项,包含 { skip, limit, orderBy, groupBy, fields } */ options(options: QueryOptionsParams): this; /** * 生成 SQL 语句 */ build(): string; /** * 执行 */ exec(callback?: Callback): Promise | void; }