Skip to content

Commit

Permalink
Merge 36ba1a1 into 823cb58
Browse files Browse the repository at this point in the history
  • Loading branch information
yourtion committed Mar 11, 2019
2 parents 823cb58 + 36ba1a1 commit 431e846
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"homepage": "https://github.com/leizongmin/leizm-sql#readme",
"peerDependencies": {
"@types/node": "^10.12.1"
"@types/node": "*"
},
"devDependencies": {
"@types/chai": "^4.1.7",
Expand Down
2 changes: 2 additions & 0 deletions src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export type BaseFieldType = number | string | boolean | Date | null;

export type AdvancedCondition = Record<string | number | symbol, AdvancedConditionField>;

export type RawCondition = { $raw?: string };

export type AdvancedConditionField =
| BaseFieldType
| {
Expand Down
21 changes: 13 additions & 8 deletions src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as assert from "assert";
import * as utils from "./utils";
import { DataRow, QueryOptionsParams, AdvancedCondition, AdvancedUpdate } from "./common";
import { DataRow, QueryOptionsParams, AdvancedCondition, AdvancedUpdate, RawCondition } from "./common";
import { Expression } from "./expr";

export class QueryBuilder<Q = DataRow, R = any> {
Expand Down Expand Up @@ -296,15 +296,15 @@ export class QueryBuilder<Q = DataRow, R = any> {
* 查询条件
* @param condition 键值对数据:{ aaa: 1, bbb: 22 })
*/
public where(condition: Partial<Q> | Partial<Pick<AdvancedCondition, keyof Q>>): this;
public where(condition: Partial<Q> | Partial<Pick<AdvancedCondition, keyof Q>> | RawCondition): this;
/**
* 查询条件
* @param condition 模板字符串,可以为 ('aaa=:a AND bbb=:b', { a: 123, b: 456 }) 或 ('aaa=? AND bbb=?', [ 123, 456 ])
*/
public where(condition: string, values: DataRow | any[]): this;

public where(
condition: Expression | string | Partial<Q> | Partial<Pick<AdvancedCondition, keyof Q>>,
condition: Expression | string | Partial<Q> | Partial<Pick<AdvancedCondition, keyof Q>> | RawCondition,
values?: DataRow | any[],
): this {
if (typeof condition === "string") {
Expand Down Expand Up @@ -332,7 +332,7 @@ export class QueryBuilder<Q = DataRow, R = any> {
* 查询条件
* @param condition 键值对数据:{ aaa: 1, bbb: 22 })
*/
public and(condition: Partial<Q> | Partial<Pick<AdvancedCondition, keyof Q>>): this;
public and(condition: Partial<Q> | Partial<Pick<AdvancedCondition, keyof Q>> | RawCondition): this;
/**
* 查询条件
* @param condition 模板字符串,可以为 ('aaa=:a AND bbb=:b', { a: 123, b: 456 })
Expand All @@ -345,7 +345,7 @@ export class QueryBuilder<Q = DataRow, R = any> {
public and(condition: string, values: any[]): this;

public and(
condition: Expression | string | Partial<Q> | Partial<Pick<AdvancedCondition, keyof Q>>,
condition: Expression | string | Partial<Q> | Partial<Pick<AdvancedCondition, keyof Q>> | RawCondition,
values?: DataRow | any[],
): this {
const t = typeof condition;
Expand All @@ -366,6 +366,10 @@ export class QueryBuilder<Q = DataRow, R = any> {
// 如果是更改操作,检查 condition 不能为空
assert.ok(Object.keys(condition).length > 0, `condition for modify operation cannot be empty`);
}
if ((condition as any)["$raw"]) {
this._data.conditions.push((condition as any)["$raw"] as string);
delete (condition as any)["$raw"];
}
this._data.conditions = this._data.conditions.concat(utils.sqlConditionStrings(condition as any));
}
return this;
Expand Down Expand Up @@ -405,12 +409,13 @@ export class QueryBuilder<Q = DataRow, R = any> {

/**
* 查询数量
* @param name 存储结果的字段名
* @param name 存储结果的字段名(默认`count`)
* @param field 执行 count 的字段(默认`*`)
*/
public count(name: string): this {
public count(name = "count", field = "*"): this {
assert.ok(this._data.type === "", `cannot change query type after it was set to "${this._data.type}"`);
this._data.type = "SELECT";
this._data.fields.push("COUNT(*) AS " + utils.sqlEscapeId(name));
this._data.fields.push(`COUNT(${field}) AS ${utils.sqlEscapeId(name)}`);
return this;
}

Expand Down
24 changes: 24 additions & 0 deletions src/test/query1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,30 @@ test("count", function() {
utils.debug(sql);
expect(sql).to.equal("SELECT COUNT(*) AS `c` FROM `test1` WHERE `a`=456 AND `b`=789 LIMIT 1");
}
{
const sql = Q.table("test1")
.count()
.where({
a: 456,
b: 789,
})
.limit(1)
.build();
utils.debug(sql);
expect(sql).to.equal("SELECT COUNT(*) AS `count` FROM `test1` WHERE `a`=456 AND `b`=789 LIMIT 1");
}
{
const sql = Q.table("test1")
.count("c", "DISTINCT `openid`")
.where({
a: 456,
b: 789,
})
.limit(1)
.build();
utils.debug(sql);
expect(sql).to.equal("SELECT COUNT(DISTINCT `openid`) AS `c` FROM `test1` WHERE `a`=456 AND `b`=789 LIMIT 1");
}
});
test("insert", function() {
{
Expand Down
22 changes: 22 additions & 0 deletions src/test/query3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ test("sub query", function() {
"SELECT * FROM `test1` WHERE `a`=123 AND `b` IN (SELECT `id` FROM `test2` WHERE `id`<10 LIMIT 100)",
);
}
{
const sql = Q.select("*")
.from("test1")
.where({
a: 123,
$raw: "a > b",
})
.build();
utils.debug(sql);
expect(sql).to.equal("SELECT * FROM `test1` WHERE a > b AND `a`=123");
}
{
const sql = Q.select("*")
.from("test1")
.where({
a: 123,
$raw: "a > b",
})
.build();
utils.debug(sql);
expect(sql).to.equal("SELECT * FROM `test1` WHERE a > b AND `a`=123");
}
});

test("clone", function() {
Expand Down

0 comments on commit 431e846

Please sign in to comment.