Skip to content

Commit

Permalink
complete transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingZhang committed Jun 16, 2020
1 parent bdf43f2 commit 9b872cf
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PostgreSQL client for Deno
* [x] client
* [x] pool
* [ ] ssl
* [ ] transactions
* [x] transactions

## Useage

Expand Down
1 change: 1 addition & 0 deletions connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export default class Connection {
}

async query(sql: string): Promise<Query> {
this.activeQuery = new Query();
const encodedSql = this.encoder.encode(sql);
const sqlLen = encodedSql.byteLength;
const size = 1 + 4 + sqlLen + 1; // Byte1('Q') + Int32 + String + 1(null terminator)
Expand Down
1 change: 0 additions & 1 deletion query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export default class Query {
const parser = parsers.get(dataTypeID);
const val = msg.fields[i];
row[name] = parser ? parser(val) : val;
row[name] = val;
}

this.rows.push(row);
Expand Down
65 changes: 65 additions & 0 deletions test/transaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Pool } from "../mod.ts";
import {
assert,
assertEquals,
assertThrowsAsync,
} from "https://deno.land/std/testing/asserts.ts";

const decoder = new TextDecoder();
const { test } = Deno;

const pool = new Pool({
password: "esri@123",
});

test("transaction should ok", async () => {
// prepare data
const { rowCount, rows } = await pool.query(
`INSERT INTO users(id, name) VALUES(1, 'zfx') RETURNING id`,
);
assert(rowCount === 1);
assert(rows.length === 1);
assert(rows[0].id === 1);

const client = await pool.connect();

try {
// rollback
await client.query("BEGIN");
const del = await client.query("DELETE FROM users WHERE id=1");
assert(del.command === "DELETE");
assert(del.rowCount === 1);
const { rows } = await client.query("SELECT * FROM users");
assert(rows.length === 0);
await client.query("ROLLBACK");
const res = await client.query("SELECT * FROM users");
assert(res.rows.length === 1);
assert(res.rows[0].id === 1);
assert(res.rows[0].name === "zfx");

// commit
try {
await client.query("BEGIN");
const { rows } = await client.query(
`INSERT INTO users(id, name) VALUES(2, 'zfx2') RETURNING id`,
);
assert(rows.length === 1);
assert(rows[0].id === 2);
await client.query("COMMIT");
const res = await client.query("SELECT * FROM users");
assert(res.rows.length === 2);
} catch (e) {
await client.query("ROLLBACK");
throw e;
}
} catch (error) {
console.debug(error);
assert(false);
} finally {
// clean
client.release();
const res = await pool.query("DELETE FROM users WHERE id in(1, 2)");
assert(res.rowCount === 2);
await pool.end();
}
});

0 comments on commit 9b872cf

Please sign in to comment.