From a37f86e89dc01f884a1b4474c27c79d5932093a0 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 16 May 2022 15:46:20 -0700 Subject: `bun:sqlite` (#167) * :scissors: * Add the slow version * draw the rest of the owl * Fix crash when allocating lots of memory * [Bun.Transipiler] Support passing objects * [JS Parser] Support passing objects to macros via Bun.Transpiler * Update JSSQLStatement.cpp * Embed SQLite * Add SQLite to Dockerfile * [sqlite] Add quick one-off queries without creating a whole object * [sqlite] Add `columnsCount`, rename raw() to `values()`, remove `rebind` * Implement `bun:sqlite` * return null * Fix updating query * Update bun.d.ts * more tests * Support variadic arguments, write tests and add types * Update sqlite.d.ts * Update sqlite.d.ts * latest * Implement `Database.loadExtension` and `Database.setCustomSQLite` * Support `require.resolve` * [napi] Improve string performance * [bun.js] Support some of `node:module` * another test * [sqlite] Support serialize & deserialize * [`bun:ffi`] Implement `CFunction` and `linkSymbols` * [bun.js] Fix crash in `Buffer.from` * Update sqlite.test.js * Document linkSymbols * docs * Update README.md --- integration/bunjs-only-snippets/sql-raw.test.js | 71 +++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 integration/bunjs-only-snippets/sql-raw.test.js (limited to 'integration/bunjs-only-snippets/sql-raw.test.js') diff --git a/integration/bunjs-only-snippets/sql-raw.test.js b/integration/bunjs-only-snippets/sql-raw.test.js new file mode 100644 index 000000000..ea7f72bd6 --- /dev/null +++ b/integration/bunjs-only-snippets/sql-raw.test.js @@ -0,0 +1,71 @@ +import { expect, it } from "bun:test"; + +var SQL = globalThis[Symbol.for("Bun.lazy")]("sqlite"); + +it("works", () => { + const handle = SQL.open("/tmp/northwind.sqlite"); + + const stmt = SQL.prepare( + handle, + 'SELECT * FROM "Order" WHERE OrderDate > date($date)' + ); + expect(stmt.toString()).toBe( + `SELECT * FROM "Order" WHERE OrderDate > date(NULL)` + ); + + expect( + Array.isArray( + stmt.all({ + // do the conversion this way so that this test runs in multiple timezones + $date: new Date( + new Date(1996, 8, 1, 0, 0, 0, 0).toUTCString() + ).toISOString(), + }) + ) + ).toBe(true); + expect(stmt.toString()).toBe( + `SELECT * FROM "Order" WHERE OrderDate > date('1996-09-01T07:00:00.000Z')` + ); + + var ran = stmt.run({ + $date: new Date( + new Date(1997, 8, 1, 0, 0, 0, 0).toUTCString() + ).toISOString(), + }); + expect(Array.isArray(ran)).toBe(false); + expect(ran === undefined).toBe(true); + expect(stmt.toString()).toBe( + `SELECT * FROM "Order" WHERE OrderDate > date('1997-09-01T07:00:00.000Z')` + ); + + expect( + Array.isArray( + stmt.get({ + $date: new Date( + new Date(1998, 8, 1, 0, 0, 0, 0).toUTCString() + ).toISOString(), + }) + ) + ).toBe(false); + expect(stmt.toString()).toBe( + `SELECT * FROM "Order" WHERE OrderDate > date('1998-09-01T07:00:00.000Z')` + ); + expect(stmt.paramsCount).toBe(1); + expect(stmt.columnsCount).toBe(14); + expect(stmt.columns.length).toBe(14); + stmt.finalize(); + SQL.close(handle); +}); + +it("SQL.run works", () => { + const handle = SQL.open("/tmp/northwind.sqlite"); + expect(typeof handle).toBe("number"); + + expect( + SQL.run(handle, 'SELECT * FROM "Order" WHERE OrderDate > date($date)', { + $date: new Date(1996, 8, 1).toISOString(), + }) + ).toBe(undefined); + + SQL.close(handle); +}); -- cgit v1.2.3