Skip to content

Commit

Permalink
replace params support as
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Jun 14, 2019
1 parent e22e9f5 commit e3d36ed
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Chnage log

## 1.3.2

### Features

- replace param support `AS`.
> `user.id as user_id` => \`user\`.\`id\` AS \`user_id\`
## 1.3.1

### Fixes

- export `Join`

## 1.3.0

### Features
Expand Down
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ More Useage see:

```ts
import { assertEquals, runTests, test } from "./deps.ts";
import { Query } from "https://deno.land/x/sql-builder@1.3.0/mod.ts";
import { Query } from "https://deno.land/x/sql-builder@1.3.2/mod.ts";

test(function testQueryInsert() {
const builder = new Query();
Expand Down
4 changes: 1 addition & 3 deletions join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { replaceParams } from "./util.ts";

export class Join {
value: string = null;
constructor(type: string, table: string, alias?: string) {
constructor(type: string, readonly table: string, readonly alias?: string) {
const name = alias ? "?? ??" : "??";
this.value = replaceParams(`${type} ${name}`, [table, alias]);
}
Expand All @@ -28,5 +28,3 @@ export class Join {
return this;
}
}

Join.inner("b").on("a.id", "b.id");
1 change: 1 addition & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { runTests } from "./deps.ts";
import "./test/join.ts";
import "./test/order.ts";
import "./test/query.ts";
import "./test/util.ts";
import "./test/where.ts";

runTests();
42 changes: 42 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { assertEquals, replaceParams, test } from "../deps.ts";

test(function testIdReplace() {
assertEquals(replaceParams("??", ["user.id"]), "`user`.`id`");
assertEquals(replaceParams("??", ["user.*"]), "`user`.*");
assertEquals(
replaceParams("??", ["user.id as user_id"]),
"`user`.`id` AS `user_id`"
);

assertEquals(replaceParams("?? ?", ["id", "val"]), '`id` "val"');
assertEquals(replaceParams("??", ["id"]), "`id`");
assertEquals(replaceParams("??", [1]), "`1`");
assertEquals(replaceParams("??", [true]), "`true`");
assertEquals(replaceParams("??", []), "``");
assertEquals(replaceParams("?", ["string"]), `"string"`);
assertEquals(replaceParams("?", [123]), `123`);
assertEquals(
replaceParams("?", [new Date(1551244259181)]),
`2019-02-27 13:10:59`
);
assertEquals(replaceParams("?", [`"test"`]), '"\\"test\\""');
assertEquals(replaceParams("?", [["a", "b", "c", "d"]]), '("a","b","c","d")');
assertEquals(replaceParams("?", [[1, 2, 3, 4]]), "(1,2,3,4)");
assertEquals(replaceParams("??", [["a", "b", "c"]]), "(`a`,`b`,`c`)");

let keys: string[] = ["a", "b", "c"];
assertEquals(replaceParams("??", [keys]), "(`a`,`b`,`c`)");
assertEquals(
replaceParams("??", [Object.keys({ a: 1, b: 1, c: 1 })]),
"(`a`,`b`,`c`)"
);

const query = replaceParams(
`select ??, ?? from ?? where ?? = ? and ?? = ? and is_admin = ?`,
["name", "email", "users", "id", 1, "name", "manyuanrong", true]
);
assertEquals(
query,
'select `name`, `email` from `users` where `id` = 1 and `name` = "manyuanrong" and is_admin = true'
);
});
14 changes: 12 additions & 2 deletions util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@ export function replaceParams(sql: string, params: any[]): string {
return `(${val.map(item => replaceParams("??", [item])).join(",")})`;
} else if (val === "*") {
return val;
} else if ((val as string).indexOf(".") > -1) {
const _arr = (val as string).split(".");
} else if (typeof val === "string" && val.indexOf(".") > -1) {
// a.b => `a`.`b`
const _arr = val.split(".");
return replaceParams(_arr.map(() => "??").join("."), _arr);
} else if (
typeof val === "string" &&
(val.toLowerCase().indexOf(" as ") > -1 ||
val.toLowerCase().indexOf(" AS ") > -1)
) {
// a as b => `a` AS `b`
const newVal = val.replace(" as ", " AS ");
const _arr = newVal.split(" AS ");
return replaceParams(_arr.map(() => "??").join(" AS "), _arr);
} else {
return ["`", val, "`"].join("");
}
Expand Down

0 comments on commit e3d36ed

Please sign in to comment.