Skip to content

Commit

Permalink
chore: bump sqlite version to 3.46.0 & adjust to ffi api breaking cha…
Browse files Browse the repository at this point in the history
…nges (#132)

* chore: bump sqlite version to 3.46.0

* fix: adjust to ffi breaking changes
  • Loading branch information
DjDeveloperr authored Aug 6, 2024
1 parent 0efd04f commit cd61ef0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 137 deletions.
2 changes: 1 addition & 1 deletion sqlite
Submodule sqlite updated 234 files
6 changes: 3 additions & 3 deletions src/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ export class SQLBlob {
readonly: true,
db: "main",
}, options);
const pHandle = new Uint32Array(2);
const pHandle = new BigUint64Array(1);
unwrap(sqlite3_blob_open(
db.unsafeHandle,
toCString(options.db ?? "main"),
toCString(options.table),
toCString(options.column),
options.row,
BigInt(options.row),
options.readonly === false ? 1 : 0,
pHandle,
));
this.#handle = Deno.UnsafePointer.create(pHandle[0] + 2 ** 32 * pHandle[1]);
this.#handle = Deno.UnsafePointer.create(pHandle[0]);
}

/** Byte size of the Blob */
Expand Down
56 changes: 36 additions & 20 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export function isComplete(statement: string): boolean {
return Boolean(sqlite3_complete(toCString(statement)));
}

const BIG_MAX = BigInt(Number.MAX_SAFE_INTEGER);

/**
* Represents a SQLite3 database connection.
*
Expand Down Expand Up @@ -231,9 +233,9 @@ export class Database {
}
}

const pHandle = new Uint32Array(2);
const pHandle = new BigUint64Array(1);
const result = sqlite3_open_v2(toCString(this.#path), pHandle, flags, null);
this.#handle = Deno.UnsafePointer.create(pHandle[0] + 2 ** 32 * pHandle[1]);
this.#handle = Deno.UnsafePointer.create(pHandle[0]);
if (result !== 0) sqlite3_close_v2(this.#handle);
unwrap(result);

Expand Down Expand Up @@ -314,15 +316,15 @@ export class Database {
*/
exec(sql: string, ...params: RestBindParameters): number {
if (params.length === 0) {
const pErr = new Uint32Array(2);
const pErr = new BigUint64Array(1);
sqlite3_exec(
this.#handle,
toCString(sql),
null,
null,
new Uint8Array(pErr.buffer),
);
const errPtr = Deno.UnsafePointer.create(pErr[0] + 2 ** 32 * pErr[1]);
const errPtr = Deno.UnsafePointer.create(pErr[0]);
if (errPtr !== null) {
const err = readCstr(errPtr);
sqlite3_free(errPtr);
Expand Down Expand Up @@ -444,13 +446,19 @@ export class Database {
const args: any[] = [];
for (let i = 0; i < nArgs; i++) {
const arg = Deno.UnsafePointer.create(
Number(argptr.getBigUint64(i * 8)),
argptr.getBigUint64(i * 8),
);
const type = sqlite3_value_type(arg);
switch (type) {
case SQLITE_INTEGER:
args.push(sqlite3_value_int64(arg));
case SQLITE_INTEGER: {
const value = sqlite3_value_int64(arg);
if (value < -BIG_MAX || value > BIG_MAX) {
args.push(value);
} else {
args.push(Number(value));
}
break;
}
case SQLITE_FLOAT:
args.push(sqlite3_value_double(arg));
break;
Expand Down Expand Up @@ -498,15 +506,16 @@ export class Database {
} else if (typeof result === "boolean") {
sqlite3_result_int(ctx, result ? 1 : 0);
} else if (typeof result === "number") {
if (Number.isSafeInteger(result)) sqlite3_result_int64(ctx, result);
else sqlite3_result_double(ctx, result);
if (Number.isSafeInteger(result)) {
sqlite3_result_int64(ctx, BigInt(result));
} else sqlite3_result_double(ctx, result);
} else if (typeof result === "bigint") {
sqlite3_result_int64(ctx, result);
} else if (typeof result === "string") {
const buffer = new TextEncoder().encode(result);
sqlite3_result_text(ctx, buffer, buffer.byteLength, 0);
sqlite3_result_text(ctx, buffer, buffer.byteLength, 0n);
} else if (result instanceof Uint8Array) {
sqlite3_result_blob(ctx, result, result.length, -1);
sqlite3_result_blob(ctx, result, result.length, -1n);
} else {
const buffer = new TextEncoder().encode(
`Invalid return value: ${Deno.inspect(result)}`,
Expand Down Expand Up @@ -585,13 +594,19 @@ export class Database {
const args: any[] = [];
for (let i = 0; i < nArgs; i++) {
const arg = Deno.UnsafePointer.create(
Number(argptr.getBigUint64(i * 8)),
argptr.getBigUint64(i * 8),
);
const type = sqlite3_value_type(arg);
switch (type) {
case SQLITE_INTEGER:
args.push(sqlite3_value_int64(arg));
case SQLITE_INTEGER: {
const value = sqlite3_value_int64(arg);
if (value < -BIG_MAX || value > BIG_MAX) {
args.push(value);
} else {
args.push(Number(value));
}
break;
}
case SQLITE_FLOAT:
args.push(sqlite3_value_double(arg));
break;
Expand Down Expand Up @@ -662,15 +677,16 @@ export class Database {
} else if (typeof result === "boolean") {
sqlite3_result_int(ctx, result ? 1 : 0);
} else if (typeof result === "number") {
if (Number.isSafeInteger(result)) sqlite3_result_int64(ctx, result);
else sqlite3_result_double(ctx, result);
if (Number.isSafeInteger(result)) {
sqlite3_result_int64(ctx, BigInt(result));
} else sqlite3_result_double(ctx, result);
} else if (typeof result === "bigint") {
sqlite3_result_int64(ctx, result);
} else if (typeof result === "string") {
const buffer = new TextEncoder().encode(result);
sqlite3_result_text(ctx, buffer, buffer.byteLength, 0);
sqlite3_result_text(ctx, buffer, buffer.byteLength, 0n);
} else if (result instanceof Uint8Array) {
sqlite3_result_blob(ctx, result, result.length, -1);
sqlite3_result_blob(ctx, result, result.length, -1n);
} else {
const buffer = new TextEncoder().encode(
`Invalid return value: ${Deno.inspect(result)}`,
Expand Down Expand Up @@ -729,7 +745,7 @@ export class Database {
throw new Error("Extension loading is not enabled");
}

const pzErrMsg = new Uint32Array(2);
const pzErrMsg = new BigUint64Array(1);

const result = sqlite3_load_extension(
this.#handle,
Expand All @@ -739,7 +755,7 @@ export class Database {
);

const pzErrPtr = Deno.UnsafePointer.create(
pzErrMsg[0] + 2 ** 32 * pzErrMsg[1],
pzErrMsg[0],
);
if (pzErrPtr !== null) {
const pzErr = readCstr(pzErrPtr);
Expand Down
9 changes: 0 additions & 9 deletions src/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ const symbols = {
result: "i32",
},

sqlite3_step_cb: {
name: "sqlite3_step",
callback: true,
parameters: [
"pointer", // sqlite3_stmt *pStmt
],
result: "i32",
},

sqlite3_column_count: {
parameters: [
"pointer", // sqlite3_stmt *pStmt
Expand Down
Loading

0 comments on commit cd61ef0

Please sign in to comment.