Skip to content

Commit

Permalink
add sql-builder deps
Browse files Browse the repository at this point in the history
Fixed. After a connection error occurred, the connection was not returned to the connection pool
  • Loading branch information
manyuanrong committed Jun 14, 2019
1 parent 11ddbc9 commit 953a9ed
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 99 deletions.
3 changes: 3 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
3 changes: 2 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { decode, encode } from "https://deno.land/std/strings/mod.ts";
export { format as byteFormat } from "https://deno.land/x/[email protected]/mod.ts";
export { decode, encode } from "https://deno.land/std/strings/mod.ts";
export { replaceParams } from "https://deno.land/x/[email protected]/util.ts";
3 changes: 1 addition & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { Client, ClientConfig } from "./src/client.ts";
export { replaceParams } from "./src/packets/builders/query.ts";
export { Client, ClientConfig } from "./src/client.ts";
18 changes: 12 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ export class Client {
*/
async query(sql: string, params?: any[]): Promise<any> {
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);
}
}

/**
Expand All @@ -103,9 +106,12 @@ export class Client {
*/
async execute(sql: string, params?: any[]): Promise<ExecuteResult> {
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);
}
}

/**
Expand Down
61 changes: 1 addition & 60 deletions src/packets/builders/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encode } from "../../../deps.ts";
import { encode, replaceParams } from "../../../deps.ts";
import { BufferWriter } from "../../buffer.ts";

/** @ignore */
Expand All @@ -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, '\\"');
}
13 changes: 11 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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"]);
Expand Down
28 changes: 0 additions & 28 deletions tests/query.ts

This file was deleted.

0 comments on commit 953a9ed

Please sign in to comment.