Skip to main content
Module

x/sql/mod.ts>QueryBuilder

Powerful and Easy use SQL Query Builder on Deno From @leizm/sql
Latest
class QueryBuilder
Re-export
import { QueryBuilder } from "https://deno.land/x/sql@1.0/mod.ts";

Constructors

new
QueryBuilder()

创建 QueryBuilder

Type Parameters

optional
Q = DataRow
optional
R = any

Properties

protected
readonly
_data: { tableName?: string; tableNameEscaped?: string; fields: string[]; conditions: string[]; type: string; update: string[]; insert: string; insertRows: number; delete: string; sql: string; sqlTpl: string; sqlValues: any[]; orderFields: string; orderBy: string; groupBy: string; offsetRows: number; limitRows: number; limit: string; mapTableToAlias: Record<string, string>; mapAliasToTable: Record<string, string>; currentJoinTableName: string; joinTables: Array<{ table: string; fields: string[]; type: "LEFT JOIN" | "JOIN" | "RIGHT JOIN"; on: string; alias: string; }>; }

Methods

protected
addJoinTable(
tableName: string,
type: "JOIN" | "LEFT JOIN" | "RIGHT JOIN",
fields: string[],
alias?: string,
): this

添加 JOIN 查询

protected
setTableAlias(tableName: string, aliasName: string)

设置表名对应的别名

and(condition: Expression): this

查询条件

and(condition: string): this

查询条件

and(condition: Partial<Q> | Partial<Pick<AdvancedCondition, keyof Q>> | RawCondition): this

查询条件

and(condition: string, values: DataRow): this

查询条件

and(condition: string, values: any[]): this

查询条件

as(name: string): this

设置表别名,准备连表查询

build(): string

生成 SQL 语句

克隆当前QueryBuilder

count(name?, field?): this

查询数量

delete(): this

删除

fields(...fields: string[]): this

设置查询字段

format(tpl: string): string

格式化模板字符串

format(tpl: string, values: DataRow): string

格式化模板字符串

format(tpl: string, values: any[]): string

格式化模板字符串

from(tableName: string): this

设置表名

groupBy(...fields: string[]): this

分组方法

having(tpl: string): this

分组条件

having(tpl: string, values: DataRow): this

分组条件

having(tpl: string, values: any[]): this

分组条件

insert(data: Partial<Q>): this

插入

insert(data: Array<Partial<Q>>): this

插入

into(tableName: string): this

设置表名

join(tableName: string, fields?: string[]): this

JOIN 连表

leftJoin(tableName: string, fields?: string[]): this

LEFT JOIN 连表

limit(rows: number): this

返回指定行数

offset(rows: number): this

跳过指定行数

on(condition: string, values?: DataRow | any[]): this

连表条件

插入记录时如果键冲突,则改为更新 ON DUPLICATE KEY UPDATE 用法:table("xx").insert(row).onDuplicateKeyUpdate().set(update)

options(options: QueryOptionsParams): this

批量设置 options

orderBy(tpl: string): this

排序方法

orderBy(tpl: string, values: DataRow): this

排序方法

orderBy(tpl: string, values: any[]): this

排序方法

rightJoin(tableName: string, fields?: string[]): this

RIGHT JOIN 连表

select(...fields: string[]): this

查询的字段

selectDistinct(...fields: string[]): this

去除重复的字段查询

set(update: string): this

更新

set(update: string, values: DataRow): this

更新

set(update: string, values: any[]): this

更新

set(update: Partial<Q> | Partial<Pick<AdvancedUpdate, keyof Q>>): this

更新

skip(rows: number): this

跳过指定行数

sql(sql: string): this

自定义SQL语句

sql(sql: string, values: DataRow): this

自定义SQL语句

sql(sql: string, values: any[]): this

自定义SQL语句

table(tableName: string): this

设置表名

update(): this

更新

update(update: Partial<Q> | Pick<AdvancedUpdate, keyof Q>): this

更新

update(update: string): this

更新

update(update: string, values: DataRow): this

更新

update(update: string, values: any[]): this

更新

where(condition: Expression): this

查询条件

where(condition: string): this

查询条件

where(condition: Partial<Q> | Partial<Pick<AdvancedCondition, keyof Q>> | RawCondition): this

查询条件

where(condition: string, values: DataRow | any[]): this

查询条件

Static Methods

delete<Q = DataRow, R = any>(): QueryBuilder<Q, R>

删除

表达式构造器

insert<Q = DataRow, R = any>(data: Array<Partial<Q>> | Partial<Q>): QueryBuilder<Q, R>

插入

select<Q = DataRow, R = any>(...fields: string[]): QueryBuilder<Q, R>

查询

selectDistinct<Q = DataRow, R = any>(...fields: string[]): QueryBuilder<Q, R>

查询

table<Q = DataRow, R = any>(name: string): QueryBuilder<Q, R>

创建新Query,设置表名

update<Q = DataRow, R = any>(): QueryBuilder<Q, R>

更新