Skip to content

Commit

Permalink
upgtade to std@v0.52.0
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed May 16, 2020
1 parent 7f337e2 commit 7bdfc78
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 120 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
4 changes: 2 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {
assert,
assertEquals
} from "https://deno.land/std@v0.35.0/testing/asserts.ts";
assertEquals,
} from "https://deno.land/std@v0.51.0/testing/asserts.ts";
export { replaceParams } from "./util.ts";
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export { Join } from "./join.ts";
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";
2 changes: 1 addition & 1 deletion order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Order {
get asc() {
order.value = replaceParams("?? ASC", [field]);
return order;
}
},
};
}
}
24 changes: 12 additions & 12 deletions query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Query {

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

Expand All @@ -44,7 +44,7 @@ export class Query {
if (this._groupBy && this._groupBy.length) {
return (
"GROUP BY " +
this._groupBy.map(f => replaceParams("??", [f])).join(", ")
this._groupBy.map((f) => replaceParams("??", [f])).join(", ")
);
}
}
Expand All @@ -65,41 +65,41 @@ export class Query {
this.groupSQL,
this.havingSQL,
this.orderSQL,
this.limitSQL
this.limitSQL,
]
.filter(str => str)
.filter((str) => str)
.join(" ");
}

private get insertSQL() {
const len = this._insertValues.length;
const fields = Object.keys(this._insertValues[0]);
const values = this._insertValues.map(row => {
return fields.map(key => row[key]!);
const values = this._insertValues.map((row) => {
return fields.map((key) => row[key]!);
});
return replaceParams(`INSERT INTO ?? ?? VALUES ${"? ".repeat(len)}`, [
this._table,
fields,
...values
...values,
]);
}

private get updateSQL() {
assert(!!this._updateValue);
const set = Object.keys(this._updateValue)
.map(key => {
.map((key) => {
return replaceParams(`?? = ?`, [key, this._updateValue[key]]);
})
.join(", ");
return [
replaceParams(`UPDATE ?? SET ${set}`, [this._table]),
this.whereSQL
this.whereSQL,
].join(" ");
}

private get deleteSQL() {
return [replaceParams(`DELETE FROM ??`, [this._table]), this.whereSQL].join(
" "
" ",
);
}

Expand Down Expand Up @@ -154,15 +154,15 @@ export class Query {
this._type = "select";
assert(fields.length > 0);
this._fields = this._fields.concat(
fields.map(field => {
fields.map((field) => {
if (field.toLocaleLowerCase().indexOf(" as ") > -1) {
return field;
} else if (field.split(".").length > 1) {
return replaceParams("??.??", field.split("."));
} else {
return replaceParams("??", [field]);
}
})
}),
);
return this;
}
Expand Down
2 changes: 0 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ import "./test/order.ts";
import "./test/query.ts";
import "./test/util.ts";
import "./test/where.ts";

Deno.runTests();
24 changes: 12 additions & 12 deletions test/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@ import { Join } from "../join.ts";

const { test } = Deno;

test(function testInnerJoin() {
test("testInnerJoin", function () {
assertEquals(
Join.inner("users").on("other.id", "users.id").value,
"INNER JOIN `users` ON `other`.`id` = `users`.`id`"
"INNER JOIN `users` ON `other`.`id` = `users`.`id`",
);
assertEquals(
Join.inner("users", "u").on("other.id", "u.id").value,
"INNER JOIN `users` `u` ON `other`.`id` = `u`.`id`"
"INNER JOIN `users` `u` ON `other`.`id` = `u`.`id`",
);
});

test(function testLeftJoin() {
test("testLeftJoin", function () {
assertEquals(
Join.left("users").on("other.id", "users.id").value,
"LEFT OUTER JOIN `users` ON `other`.`id` = `users`.`id`"
"LEFT OUTER JOIN `users` ON `other`.`id` = `users`.`id`",
);
assertEquals(
Join.left("users", "u").on("other.id", "u.id").value,
"LEFT OUTER JOIN `users` `u` ON `other`.`id` = `u`.`id`"
"LEFT OUTER JOIN `users` `u` ON `other`.`id` = `u`.`id`",
);
});

test(function testRightJoin() {
test("testRightJoin", function () {
assertEquals(
Join.right("users").on("other.id", "users.id").value,
"RIGHT OUTER JOIN `users` ON `other`.`id` = `users`.`id`"
"RIGHT OUTER JOIN `users` ON `other`.`id` = `users`.`id`",
);
assertEquals(
Join.right("users", "u").on("other.id", "u.id").value,
"RIGHT OUTER JOIN `users` `u` ON `other`.`id` = `u`.`id`"
"RIGHT OUTER JOIN `users` `u` ON `other`.`id` = `u`.`id`",
);
});

test(function testFullJoin() {
test("testFullJoin", function () {
assertEquals(
Join.full("users").on("other.id", "users.id").value,
"FULL OUTER JOIN `users` ON `other`.`id` = `users`.`id`"
"FULL OUTER JOIN `users` ON `other`.`id` = `users`.`id`",
);
assertEquals(
Join.full("users", "u").on("other.id", "u.id").value,
"FULL OUTER JOIN `users` `u` ON `other`.`id` = `u`.`id`"
"FULL OUTER JOIN `users` `u` ON `other`.`id` = `u`.`id`",
);
});
2 changes: 1 addition & 1 deletion test/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Order } from "../order.ts";

const { test } = Deno;

test(function testOrderBuilder() {
test("testOrderBuilder", function () {
assertEquals(Order.by("name").desc.value, "`name` DESC");
assertEquals(Order.by("name").asc.value, "`name` ASC");
});
70 changes: 29 additions & 41 deletions test/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,51 @@ import { Order, Query, Where } from "../mod.ts";

const { test } = Deno;

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

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

assertEquals(
sql.trim(),
'INSERT INTO `users` (`name`,`password`,`id`) VALUES ("Enok","foo",1) ("Man","bar",2)'
'INSERT INTO `users` (`name`,`password`,`id`) VALUES ("Enok","foo",1) ("Man","bar",2)',
);
});

test(function testQueryUpdate() {
test("testQueryUpdate", function () {
const builder = new Query();
const record = {
name: "Enok",
password: "foo",
id: 1
id: 1,
};

const sql = builder
.table("users")
.update(record)
.build();
const sql = builder.table("users").update(record).build();

assertEquals(
sql.trim(),
'UPDATE `users` SET `name` = "Enok", `password` = "foo", `id` = 1'
'UPDATE `users` SET `name` = "Enok", `password` = "foo", `id` = 1',
);
});

test(function testQueryUpdateWithWhere() {
test("testQueryUpdateWithWhere", function () {
const builder = new Query();
const record = {
name: "Enok",
password: "foo",
id: 1
id: 1,
};

const sql = builder
Expand All @@ -66,22 +60,19 @@ test(function testQueryUpdateWithWhere() {

assertEquals(
sql.trim(),
'UPDATE `users` SET `name` = "Enok", `password` = "foo", `id` = 1 WHERE `id` = 1 AND `name` LIKE "%n%"'
'UPDATE `users` SET `name` = "Enok", `password` = "foo", `id` = 1 WHERE `id` = 1 AND `name` LIKE "%n%"',
);
});

test(function testQueryDelete() {
test("testQueryDelete", function () {
const builder = new Query();

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

assertEquals(sql.trim(), "DELETE FROM `users`");
});

test(function testQueryDeleteWithWhere() {
test("testQueryDeleteWithWhere", function () {
const builder = new Query();

const sql = builder
Expand All @@ -93,22 +84,19 @@ test(function testQueryDeleteWithWhere() {

assertEquals(
sql.trim(),
'DELETE FROM `users` WHERE `id` = 1 AND `name` LIKE "%n%"'
'DELETE FROM `users` WHERE `id` = 1 AND `name` LIKE "%n%"',
);
});

test(function testQuerySelectSimple() {
test("testQuerySelectSimple", function () {
const builder = new Query();

const sql = builder
.table("users")
.select("name", "id")
.build();
const sql = builder.table("users").select("name", "id").build();

assertEquals(sql.trim(), "SELECT `name`, `id` FROM `users`");
});

test(function testQuerySelectGroupBy() {
test("testQuerySelectGroupBy", function () {
const builder = new Query();

const sql = builder
Expand All @@ -120,11 +108,11 @@ test(function testQuerySelectGroupBy() {

assertEquals(
sql.trim(),
"SELECT `name`, `id`, `type` FROM `users` GROUP BY `type` HAVING `type` NOT IN (1,2)"
"SELECT `name`, `id`, `type` FROM `users` GROUP BY `type` HAVING `type` NOT IN (1,2)",
);
});

test(function testQuerySelectWhere() {
test("testQuerySelectWhere", function () {
const builder = new Query();

const sql = builder
Expand All @@ -135,11 +123,11 @@ test(function testQuerySelectWhere() {

assertEquals(
sql.trim(),
'SELECT `name`, `id` FROM `users` WHERE (`id` > 1 AND `name` LIKE "%n%")'
'SELECT `name`, `id` FROM `users` WHERE (`id` > 1 AND `name` LIKE "%n%")',
);
});

test(function testQuerySelectOrder() {
test("testQuerySelectOrder", function () {
const builder = new Query();

const sql = builder
Expand All @@ -152,11 +140,11 @@ test(function testQuerySelectOrder() {

assertEquals(
sql.trim(),
'SELECT `name`, `id` FROM `users` WHERE `id` > 1 AND `name` LIKE "%n%" ORDER BY `id` DESC, `name` ASC'
'SELECT `name`, `id` FROM `users` WHERE `id` > 1 AND `name` LIKE "%n%" ORDER BY `id` DESC, `name` ASC',
);
});

test(function testQuerySelectJoin() {
test("testQuerySelectJoin", function () {
const builder = new Query();

const sql = builder
Expand All @@ -169,11 +157,11 @@ test(function testQuerySelectJoin() {

assertEquals(
sql.trim(),
'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%"'
'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%"',
);
});

test(function testQuerySelectLimit() {
test("testQuerySelectLimit", function () {
const builder = new Query();

const sql = builder
Expand All @@ -186,6 +174,6 @@ test(function testQuerySelectLimit() {

assertEquals(
sql.trim(),
'SELECT * FROM `users` WHERE `id` > 1 AND `name` LIKE "%n%" LIMIT 0, 10'
'SELECT * FROM `users` WHERE `id` > 1 AND `name` LIKE "%n%" LIMIT 0, 10',
);
});
Loading

0 comments on commit 7bdfc78

Please sign in to comment.