Skip to content

Commit

Permalink
added pool support
Browse files Browse the repository at this point in the history
  • Loading branch information
halvardssm committed Apr 15, 2024
1 parent 4ce8069 commit 7d5841d
Show file tree
Hide file tree
Showing 9 changed files with 544 additions and 577 deletions.
7 changes: 2 additions & 5 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import {
} from "@halvardm/sqlx";
import { MysqlConnection, type MysqlConnectionOptions } from "./connection.ts";
import { buildQuery } from "./packets/builders/query.ts";
import {
getRowObject,
type MysqlParameterType,
} from "./packets/parsers/result.ts";
import type { MysqlParameterType } from "./packets/parsers/result.ts";

export interface MysqlTransactionOptions extends SqlxTransactionOptions {
beginTransactionOptions: {
Expand Down Expand Up @@ -346,7 +343,7 @@ export class MysqlClient extends MysqlTransactionable implements
> {
readonly connectionUrl: string;
readonly connectionOptions: MysqlConnectionOptions;
readonly eventTarget: EventTarget;
eventTarget: EventTarget;
get connected(): boolean {
throw new Error("Method not implemented.");
}
Expand Down
129 changes: 129 additions & 0 deletions lib/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,135 @@ Deno.test("Connection", async (t) => {
}
});

await t.step("can parse time", async () => {
const data = buildQuery(`SELECT CAST("09:04:10" AS time) as time`);
for await (const result1 of connection.sendData(data)) {
assertEquals(result1, {
row: ["09:04:10"],
fields: [
{
catalog: "def",
decimals: 0,
defaultVal: "",
encoding: 63,
fieldFlag: 128,
fieldLen: 10,
fieldType: 11,
name: "time",
originName: "",
originTable: "",
schema: "",
table: "",
},
],
});
}
});

await t.step("can parse date", async () => {
const data = buildQuery(
`SELECT CAST("2024-04-15 09:04:10" AS date) as date`,
);
for await (const result1 of connection.sendData(data)) {
assertEquals(result1, {
row: [new Date("2024-04-15T00:00:00.000Z")],
fields: [
{
catalog: "def",
decimals: 0,
defaultVal: "",
encoding: 63,
fieldFlag: 128,
fieldLen: 10,
fieldType: 10,
name: "date",
originName: "",
originTable: "",
schema: "",
table: "",
},
],
});
}
});

await t.step("can parse bigint", async () => {
const data = buildQuery(`SELECT 9223372036854775807 as result`);
for await (const result1 of connection.sendData(data)) {
assertEquals(result1, {
row: [9223372036854775807n],
fields: [
{
catalog: "def",
decimals: 0,
defaultVal: "",
encoding: 63,
fieldFlag: 129,
fieldLen: 20,
fieldType: 8,
name: "result",
originName: "",
originTable: "",
schema: "",
table: "",
},
],
});
}
});

await t.step("can parse decimal", async () => {
const data = buildQuery(
`SELECT 0.012345678901234567890123456789 as result`,
);
for await (const result1 of connection.sendData(data)) {
assertEquals(result1, {
row: ["0.012345678901234567890123456789"],
fields: [
{
catalog: "def",
decimals: 30,
defaultVal: "",
encoding: 63,
fieldFlag: 129,
fieldLen: 33,
fieldType: 246,
name: "result",
originName: "",
originTable: "",
schema: "",
table: "",
},
],
});
}
});

await t.step("can parse empty string", async () => {
const data = buildQuery(`SELECT '' as result`);
for await (const result1 of connection.sendData(data)) {
assertEquals(result1, {
row: [""],
fields: [
{
catalog: "def",
decimals: 31,
defaultVal: "",
encoding: 33,
fieldFlag: 1,
fieldLen: 0,
fieldType: 253,
name: "result",
originName: "",
originTable: "",
schema: "",
table: "",
},
],
});
}
});

await t.step("can drop and create table", async () => {
const dropTableSql = buildQuery("DROP TABLE IF EXISTS test;");
const dropTableReturned = connection.sendData(dropTableSql);
Expand Down
11 changes: 4 additions & 7 deletions lib/connection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
MysqlConnectionError,
MysqlError,
MysqlProtocolError,
MysqlReadError,
MysqlResponseTimeoutError,
Expand All @@ -14,7 +13,7 @@ import {
parseHandshake,
} from "./packets/parsers/handshake.ts";
import {
ConvertTypeOptions,
type ConvertTypeOptions,
type FieldInfo,
getRowObject,
type MysqlParameterType,
Expand All @@ -38,7 +37,6 @@ import { VERSION } from "./utils/meta.ts";
import { resolve } from "@std/path";
import { toCamelCase } from "@std/text";
import { AuthPluginName } from "./auth_plugins/mod.ts";
import type { MysqlQueryOptions } from "./client.ts";

/**
* Connection state
Expand Down Expand Up @@ -164,7 +162,9 @@ export class MysqlConnection
set conn(conn: Deno.Conn | null) {
this._conn = conn;
}

get connected(): boolean {
return this.state === ConnectionState.CONNECTED;
}
constructor(
connectionUrl: string | URL,
connectionOptions: MysqlConnectionOptions = {},
Expand All @@ -176,9 +176,6 @@ export class MysqlConnection
connectionOptions,
);
}
get connected(): boolean {
return this.state === ConnectionState.CONNECTED;
}

async connect(): Promise<void> {
// TODO: implement connect timeout
Expand Down
71 changes: 0 additions & 71 deletions lib/deferred.ts

This file was deleted.

Loading

0 comments on commit 7d5841d

Please sign in to comment.