From 953a9edc6127064dc22d6de4a626b22885206461 Mon Sep 17 00:00:00 2001 From: Enok <416828041@qq.com> Date: Sat, 15 Jun 2019 00:10:24 +0800 Subject: [PATCH] add sql-builder deps Fixed. After a connection error occurred, the connection was not returned to the connection pool --- README.MD | 3 ++ deps.ts | 3 +- mod.ts | 3 +- src/client.ts | 18 +++++++---- src/packets/builders/query.ts | 61 +---------------------------------- test.ts | 13 ++++++-- tests/query.ts | 28 ---------------- 7 files changed, 30 insertions(+), 99 deletions(-) delete mode 100644 tests/query.ts diff --git a/README.MD b/README.MD index 312eeda..b5e6ae4 100644 --- a/README.MD +++ b/README.MD @@ -1,6 +1,9 @@ # deno_mysql [![Build Status](https://www.travis-ci.org/manyuanrong/deno_mysql.svg?branch=master)](https://www.travis-ci.org/manyuanrong/deno_mysql) +![GitHub](https://img.shields.io/github/license/manyuanrong/deno_mysql.svg) +![GitHub release](https://img.shields.io/github/release/manyuanrong/deno_mysql.svg) +![(Deno)](https://img.shields.io/badge/deno-0.8.0-green.svg) MySQL database driver for Deno. diff --git a/deps.ts b/deps.ts index 83dcd9a..b140c68 100644 --- a/deps.ts +++ b/deps.ts @@ -1,2 +1,3 @@ +export { decode, encode } from "https://deno.land/std/strings/mod.ts"; export { format as byteFormat } from "https://deno.land/x/bytes_formater@1.0.0/mod.ts"; -export { decode, encode } from "https://deno.land/std/strings/mod.ts"; \ No newline at end of file +export { replaceParams } from "https://deno.land/x/sql_builder@1.3.2/util.ts"; diff --git a/mod.ts b/mod.ts index 8e80479..1e04bc9 100644 --- a/mod.ts +++ b/mod.ts @@ -1,2 +1 @@ -export { Client, ClientConfig } from "./src/client.ts"; -export { replaceParams } from "./src/packets/builders/query.ts"; \ No newline at end of file +export { Client, ClientConfig } from "./src/client.ts"; \ No newline at end of file diff --git a/src/client.ts b/src/client.ts index 0862072..f27361d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -91,9 +91,12 @@ export class Client { */ async query(sql: string, params?: any[]): Promise { const connection = await this._pool.pop(); - const result = await connection.query(sql, params); - this._pool.push(connection); - return result; + try { + const result = await connection.query(sql, params); + return result; + } finally { + this._pool.push(connection); + } } /** @@ -103,9 +106,12 @@ export class Client { */ async execute(sql: string, params?: any[]): Promise { const connection = await this._pool.pop(); - const result = await connection.execute(sql, params); - this._pool.push(connection); - return result; + try { + const result = await connection.execute(sql, params); + return result; + } finally { + this._pool.push(connection); + } } /** diff --git a/src/packets/builders/query.ts b/src/packets/builders/query.ts index 5944d56..3429281 100644 --- a/src/packets/builders/query.ts +++ b/src/packets/builders/query.ts @@ -1,4 +1,4 @@ -import { encode } from "../../../deps.ts"; +import { encode, replaceParams } from "../../../deps.ts"; import { BufferWriter } from "../../buffer.ts"; /** @ignore */ @@ -9,62 +9,3 @@ export function buildQuery(sql: string, params: any[] = []): Uint8Array { writer.writeBuffer(data); return writer.buffer; } - -export function replaceParams(sql: string, params: any[]): string { - let paramIndex = 0; - sql = sql.replace(/(\?\?)|(\?)/g, str => { - // identifier - if (str === "??") { - const val = params[paramIndex++]; - if (val instanceof Array) { - return `(${val.map(item => replaceParams("??", [item])).join(",")})`; - } else { - return ["`", val, "`"].join(""); - } - } - // value - const val = params[paramIndex++]; - switch (typeof val) { - case "object": - if (val instanceof Date) return formatDate(val); - if (val instanceof Array) { - return `(${val.map(item => replaceParams("?", [item])).join(",")})`; - } - case "string": - return `"${escapeString(val)}"`; - case "undefined": - return "NULL"; - case "number": - case "boolean": - default: - return val; - } - }); - return sql; -} - -function formatDate(date: Date) { - const year = date.getFullYear(); - const month = (date.getMonth() + 1).toString().padStart(2, "0"); - const days = date - .getDate() - .toString() - .padStart(2, "0"); - const hours = date - .getHours() - .toString() - .padStart(2, "0"); - const minutes = date - .getMinutes() - .toString() - .padStart(2, "0"); - const seconds = date - .getSeconds() - .toString() - .padStart(2, "0"); - return `${year}-${month}-${days} ${hours}:${minutes}:${seconds}`; -} - -function escapeString(str: string) { - return str.replace(/"/g, '\\"'); -} diff --git a/test.ts b/test.ts index dc65033..e4d6144 100644 --- a/test.ts +++ b/test.ts @@ -1,7 +1,6 @@ -import { assertEquals } from "https://deno.land/x/testing/asserts.ts"; +import { assertEquals, assertThrowsAsync } from "https://deno.land/x/testing/asserts.ts"; import { runTests, test } from "https://deno.land/x/testing/mod.ts"; import { Client } from "./mod.ts"; -import "./tests/query.ts"; let client: Client; @@ -51,6 +50,16 @@ test(async function testQuery() { assertEquals(result, [{ id: 1, name: "MYR" }]); }); +test(async function testQueryErrorOccurred() { + assertEquals(1, client.poolSize); + await assertThrowsAsync( + () => client.query("select unknownfield from `users`"), + Error + ); + await client.query("select 1"); + assertEquals(client.poolSize, 1); +}); + test(async function testQueryList() { const sql = "select ??,?? from ??"; let result = await client.query(sql, ["id", "name", "users"]); diff --git a/tests/query.ts b/tests/query.ts deleted file mode 100644 index abdcc33..0000000 --- a/tests/query.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { test } from "https://deno.land/x/testing/mod.ts"; -import { replaceParams } from "../src/packets/builders/query.ts"; -import { assertEquals } from "https://deno.land/x/testing/asserts.ts"; - -test(function testIdReplace() { - 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'); -}); \ No newline at end of file