/** * @leizm/mysql * * @author Zongmin Lei */ import { DataRow, QueryOptionsParams, AdvancedCondition, AdvancedUpdate, RawCondition } from "./common"; import { Expression } from "./expr"; export declare class QueryBuilder { /** * 表达式构造器 */ static expr(): Expression; /** * 创建新Query,设置表名 */ static table(name: string): QueryBuilder; /** * 更新 */ static update(): QueryBuilder; /** * 查询 * @param fields */ static select(...fields: string[]): QueryBuilder; /** * 查询 * @param fields */ static selectDistinct(...fields: string[]): QueryBuilder; /** * 插入 * @param data 数据 */ static insert(data: Array> | Partial): QueryBuilder; /** * 删除 */ static delete(): QueryBuilder; protected readonly _data: { tableName?: string; tableNameEscaped?: string; fields: string[]; conditions: string[]; type: string; update: string[]; insert: string; insertRows: number; insertIgnore: boolean; delete: string; sql: string; sqlTpl: string; sqlValues: any[]; orderFields: string; orderBy: string; groupBy: string; offsetRows: number; limitRows: number; limit: string; mapTableToAlias: Record; mapAliasToTable: Record; currentJoinTableName: string; joinTables: Array<{ table: string; fields: string[]; type: "LEFT JOIN" | "JOIN" | "RIGHT JOIN"; on: string; alias: string; }>; }; /** * 创建 QueryBuilder */ constructor(); /** * 克隆当前QueryBuilder */ clone(): QueryBuilder; /** * 格式化模板字符串 * @param tpl 模板字符串 */ format(tpl: string): string; /** * 格式化模板字符串 * @param tpl 模板字符串 * @param values 键值对数据 */ format(tpl: string, values: DataRow): string; /** * 格式化模板字符串 * @param tpl 模板字符串 * @param values 参数数组 */ format(tpl: string, values: any[]): string; /** * 设置表名 * @param tableName 表名 */ into(tableName: string): this; /** * 设置表名 * @param tableName 表名 */ from(tableName: string): this; /** * 设置表名 * @param tableName 表名 */ table(tableName: string): this; /** * 设置表名对应的别名 * @param tableName 表名 * @param aliasName 别名 */ protected setTableAlias(tableName: string, aliasName: string): void; /** * 添加 JOIN 查询 * @param tableName * @param type * @param fields * @param alias */ protected addJoinTable(tableName: string, type: "JOIN" | "LEFT JOIN" | "RIGHT JOIN", fields: string[], alias?: string): this; /** * 设置表别名,准备连表查询 * @param name 别名 */ as(name: string): this; /** * JOIN 连表 * @param tableName 表名 */ join(tableName: string, fields?: string[]): this; /** * LEFT JOIN 连表 * @param tableName 表名 */ leftJoin(tableName: string, fields?: string[]): this; /** * RIGHT JOIN 连表 * @param tableName 表名 */ rightJoin(tableName: string, fields?: string[]): this; /** * 连表条件 * @param condition 条件字符串 * @param values 模板参数 */ on(condition: string, values?: DataRow | any[]): this; /** * 查询条件 * @param condition 表达式 */ where(condition: Expression): this; /** * 查询条件 * @param condition SQL 语句 */ where(condition: string): this; /** * 查询条件 * @param condition 键值对数据:{ aaa: 1, bbb: 22 }) */ where(condition: Partial | Partial> | RawCondition): this; /** * 查询条件 * @param condition 模板字符串,可以为 ('aaa=:a AND bbb=:b', { a: 123, b: 456 }) 或 ('aaa=? AND bbb=?', [ 123, 456 ]) */ where(condition: string, values: DataRow | any[]): this; /** * 查询条件 * @param condition 表达式 */ and(condition: Expression): this; /** * 查询条件 * @param condition SQL 语句 */ and(condition: string): this; /** * 查询条件 * @param condition 键值对数据:{ aaa: 1, bbb: 22 }) */ and(condition: Partial | Partial> | RawCondition): this; /** * 查询条件 * @param condition 模板字符串,可以为 ('aaa=:a AND bbb=:b', { a: 123, b: 456 }) */ and(condition: string, values: DataRow): 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 fields */ selectDistinct(...fields: string[]): this; /** * 查询数量 * @param name 存储结果的字段名(默认`count`) * @param field 执行 count 的字段(默认`*`) */ count(name?: string, field?: string): this; /** * 更新 */ update(): this; /** * 更新 * @param update 键值对数据,如 { a: 123, b: 456 } */ update(update: Partial | Pick): this; /** * 更新 * @param update SQL 语句,如 a=a+1 */ update(update: string): this; /** * 更新 * @param update SQL 语句模板,如 a=:a * @param values 模板参数,如 { a: 123 } */ update(update: string, values: DataRow): this; /** * 更新 * @param update SQL 语句模板,如 a=? * @param values 模板参数,如 [ 123 ] */ update(update: string, values: any[]): this; /** * 更新 * @param update SQL 语句,如 a=a+1 */ set(update: string): this; /** * 更新 * @param update SQL 语句模板,如 a=:a * @param values 模板参数,如 { a: 123 } */ set(update: string, values: DataRow): this; /** * 更新 * @param update SQL 语句模板,如 a=? * @param values 模板参数,如 [ 123 ] */ set(update: string, values: any[]): this; /** * 更新 * @param update 键值对数据,如 { a: 123, b: 456 } */ set(update: Partial | Partial>): this; /** * 插入 * @param data 键值对数据 */ insert(data: Partial): this; /** * 插入 * @param data 键值对数据数组 */ insert(data: Array>): this; private _insert; /** * 插入忽略已存在 */ ignore(): this; /** * 替换,删除原来的记录,添加新的记录 * @param data 键值对数据 */ replace(data: Partial): this; /** * 替换,删除原来的记录,添加新的记录 * @param data 键值对数据数组 */ replace(data: Array>): this; /** * 删除 */ delete(): this; /** * 插入记录时如果键冲突,则改为更新 * ON DUPLICATE KEY UPDATE * 用法:table("xx").insert(row).onDuplicateKeyUpdate().set(update) */ onDuplicateKeyUpdate(): this; /** * 自定义SQL语句 * @param sql SQL 查询语句 */ sql(sql: string): this; /** * 自定义SQL语句 * @param sql SQL 查询语句 * @param values 模板参数,如 { a: 123 } */ sql(sql: string, values: DataRow): 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: DataRow): this; /** * 排序方法 * @param tpl SQL 查询语句 * @param values 模板参数,如 [ 123 ] */ orderBy(tpl: string, values: any[]): this; /** * 分组方法 * @param fields 字段 */ groupBy(...fields: string[]): this; /** * 分组条件 * @param tpl SQL 查询语句 */ having(tpl: string): this; /** * 分组条件 * @param tpl SQL 查询语句 * @param values 模板参数,如 { a: 123 } */ having(tpl: string, values: DataRow): this; /** * 分组条件 * @param tpl SQL 查询语句 * @param values 模板参数,如 [ 123 ] */ having(tpl: string, values: any[]): this; /** * 跳过指定行数 * @param rows 行数 */ offset(rows: number): this; /** * 跳过指定行数 * @param rows 行数 */ skip(rows: number): this; /** * 返回指定行数 * @param rows 行数 */ limit(rows: number): this; /** * 批量设置 options * @param options 选项,包含 { offset, limit, orderBy, groupBy, fields } */ options(options: QueryOptionsParams): this; /** * 生成 SQL 语句 */ build(): string; }