Skip to content

Commit

Permalink
fix: don't use C string in text bind parameter (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
DjDeveloperr authored Dec 29, 2021
1 parent 8e0d002 commit 5324992
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
sqlite3_stmt,
sqlite3_total_changes,
} from "./ffi.ts";
import { cstr } from "./util.ts";
import { cstr, encode } from "./util.ts";
import { fromFileUrl } from "../deps.ts";

/** SQLite version string */
Expand Down Expand Up @@ -429,7 +429,9 @@ export class PreparedStatement {
break;

case "string": {
const buffer = this.#cstr(value);
// Bind parameters do not need C string,
// because we specify it's length.
const buffer = encode(value);
this.#bufferRefs.add(buffer);
sqlite3_bind_text(
this.db.unsafeRawHandle,
Expand Down
12 changes: 12 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ Deno.test("sqlite", async (t) => {
}
});

await t.step("query with string param", () => {
const row = db.queryArray<[number, string, number, Uint8Array, null]>(
"select * from test where text = ?",
"hello world",
)[0];
assertEquals(row[0], 0);
assertEquals(row[1], "hello world");
assertEquals(row[2], 3.14);
assertEquals(row[3], new Uint8Array([1, 2, 3]));
assertEquals(row[4], null);
});

await t.step("drop table", () => {
db.execute("drop table test");
});
Expand Down

0 comments on commit 5324992

Please sign in to comment.