Skip to content

Commit

Permalink
update query with new features
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Jun 14, 2019
1 parent 2ee696f commit bd85429
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 23 deletions.
115 changes: 115 additions & 0 deletions docs/query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Query

SQL Query Builder

### table(name: string)

set table name

### order(...orders: Order[])

set orders

### where(where:Where|string)

add where condition by sql string or Where object

### having(where:Where|string)

add having condition by sql string or Where object

### limit(start:number,end:number)

set limit range

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

set group by fields

### join(join: Join | string) {

add join

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

set select fields

```ts
new Query()
.table("articles")
.select("articles.*", "users.name")
.join(Join.left("users").on("users.id", "articles.user_id"))
.where(Where.field("type").eq(1))
.order(Order.by("articles.created_at").desc)
.limit(0, 10)
.build();
// SELECT `articles`.*, `users`.`name` FROM `articles` LEFT OUTER JOIN `users` ON `users`.`id` = `articles`.`user_id` WHERE `type` = 1 ORDER BY `articles`.`created_at` DESC LIMIT 0, 10
```

## insert(data: Object[] | Object)

set one or more insert data object(s)

```ts
const builder = new Query();
const records = [
{
name: "Enok",
password: "foo",
id: 1
},
{
id: 2,
name: "Man",
password: "bar"
}
];

const sql = builder
.table("users")
.insert(records)
.build();

// INSERT INTO `users` (`name`,`password`,`id`) VALUES ("Enok","foo",1) ("Man","bar",2)
```

### update(data: Object)

set update data

```ts
const builder = new Query();
const record = {
name: "Enok",
password: "foo",
id: 1
};
const sql = builder
.table("users")
.where(Where.field("id").eq(1))
.where(Where.field("name").like("%n%"))
.update(record)
.build();

// UPDATE `users` SET `name` = "Enok", `password` = "foo", `id` = 1 WHERE `id` = 1 AND `name` LIKE "%n%"
```

### delete()

set sql type to delete

```ts
const builder = new Query();
const sql = builder
.table("users")
.where(Where.field("id").eq(1))
.where(Where.field("name").like("%n%"))
.delete()
.build();

//DELETE FROM `users` WHERE `id` = 1 AND `name` LIKE "%n%"
```

### build(): string

Generate and return SQL
3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Order } from "./order.ts";
export { Query } from "./query.ts";
export { replaceParams } from "./util.ts";
export { Where } from "./where.ts";
export { Where } from "./where.ts";
28 changes: 13 additions & 15 deletions query.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { assert, replaceParams } from "./deps.ts";
import { Order } from "./order.ts";
import { Where } from "./where.ts";
import { Join } from "./join.ts";

export class Query {
private _type: "select" | "insert" | "update" | "delete";
private _table: string;
private _where: string[] = [];
private _joins: string[] = [];
private _orders: string[] = [];
private _orders: Order[] = [];
private _fields: string[] = [];
private _groupBy: string[] = [];
private _having: string[] = [];
Expand All @@ -16,7 +18,7 @@ export class Query {

private get orderSQL() {
if (this._orders && this._orders.length) {
return `ORDER BY ` + this._orders.join(", ");
return `ORDER BY ` + this._orders.map(order => order.value).join(", ");
}
}

Expand Down Expand Up @@ -106,17 +108,9 @@ export class Query {
return this;
}

order(by: string) {
return {
desc: () => {
this._orders.push(replaceParams(`?? DESC`, [by]));
return this;
},
asc: () => {
this._orders.push(replaceParams(`?? ASC`, [by]));
return this;
}
};
order(...orders: Order[]) {
this._orders = this._orders.concat(orders);
return this;
}

groupBy(...fields: string[]) {
Expand Down Expand Up @@ -147,8 +141,12 @@ export class Query {
return this;
}

join(join: string) {
this._joins.push(join);
join(join: Join | string) {
if (typeof join === "string") {
this._joins.push(join);
} else {
this._joins.push(join.value);
}
return this;
}

Expand Down
2 changes: 2 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { runTests } from "./deps.ts";
import "./test/join.ts";
import "./test/order.ts";
import "./test/query.ts";
import "./test/where.ts";

Expand Down
12 changes: 5 additions & 7 deletions test/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertEquals, test } from "../deps.ts";
import { Query, Where } from "../mod.ts";
import { Join } from "../join.ts";
import { Order, Query, Where } from "../mod.ts";

test(function testQueryInsert() {
const builder = new Query();
Expand Down Expand Up @@ -144,10 +145,7 @@ test(function testQuerySelectOrder() {
.where(Where.field("id").gt(1))
.where(Where.field("name").like("%n%"))
.select("name", "id")
.order("id")
.desc()
.order("name")
.asc()
.order(Order.by("id").desc, Order.by("name").asc)
.build();

assertEquals(
Expand All @@ -164,12 +162,12 @@ test(function testQuerySelectJoin() {
.where(Where.field("id").gt(1))
.where(Where.field("name").like("%n%"))
.select("users.id", "users.name", "`uses`.`avatar` as `uavatar`")
.join("LEFT JOIN posts ON posts.id = users.id")
.join(Join.left("posts").on("posts.id", "users.id"))
.build();

assertEquals(
sql.trim(),
'SELECT `users`.`id`, `users`.`name`, `uses`.`avatar` as `uavatar` FROM `users` LEFT JOIN posts ON posts.id = users.id WHERE `id` > 1 AND `name` LIKE "%n%"'
'SELECT `users`.`id`, `users`.`name`, `uses`.`avatar` as `uavatar` FROM `users` LEFT OUTER JOIN `posts` ON `posts`.`id` = `users`.`id` WHERE `id` > 1 AND `name` LIKE "%n%"'
);
});

Expand Down

0 comments on commit bd85429

Please sign in to comment.