Skip to content

Commit

Permalink
fix: update for new ffi changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DjDeveloperr committed Feb 24, 2023
1 parent f752989 commit 4cb7e8f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class SQLBlob {
options.readonly === false ? 1 : 0,
pHandle,
));
this.#handle = pHandle[0] + 2 ** 32 * pHandle[1];
this.#handle = Deno.UnsafePointer.create(pHandle[0] + 2 ** 32 * pHandle[1]);
}

/** Byte size of the Blob */
Expand Down
72 changes: 43 additions & 29 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ const {
} = ffi;

/** SQLite version string */
export const SQLITE_VERSION = readCstr(sqlite3_libversion());
export const SQLITE_VERSION = readCstr(sqlite3_libversion()!);
/** SQLite source ID string */
export const SQLITE_SOURCEID = readCstr(sqlite3_sourceid());
export const SQLITE_SOURCEID = readCstr(sqlite3_sourceid()!);

/**
* Whether the given SQL statement is complete.
Expand Down Expand Up @@ -215,8 +215,8 @@ export class Database {
}

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

Expand Down Expand Up @@ -298,9 +298,15 @@ export class Database {
exec(sql: string, ...params: RestBindParameters): number {
if (params.length === 0) {
const pErr = new Uint32Array(2);
sqlite3_exec(this.#handle, toCString(sql), 0, 0, pErr);
const errPtr = pErr[0] + 2 ** 32 * pErr[1];
if (errPtr !== 0) {
sqlite3_exec(
this.#handle,
toCString(sql),
null,
null,
new Uint8Array(pErr.buffer),
);
const errPtr = Deno.UnsafePointer.create(pErr[0] + 2 ** 32 * pErr[1]);
if (errPtr !== null) {
const err = readCstr(errPtr);
sqlite3_free(errPtr);
throw new Error(err);
Expand Down Expand Up @@ -401,10 +407,12 @@ export class Database {
result: "void",
} as const,
(ctx, nArgs, pArgs) => {
const argptr = new Deno.UnsafePointerView(pArgs);
const argptr = new Deno.UnsafePointerView(pArgs!);
const args: any[] = [];
for (let i = 0; i < nArgs; i++) {
const arg = Number(argptr.getBigUint64(i * 8));
const arg = Deno.UnsafePointer.create(
Number(argptr.getBigUint64(i * 8)),
);
const type = sqlite3_value_type(arg);
switch (type) {
case SQLITE_INTEGER:
Expand All @@ -418,7 +426,7 @@ export class Database {
new TextDecoder().decode(
new Uint8Array(
Deno.UnsafePointerView.getArrayBuffer(
sqlite3_value_text(arg),
sqlite3_value_text(arg)!,
sqlite3_value_bytes(arg),
),
),
Expand All @@ -429,7 +437,7 @@ export class Database {
args.push(
new Uint8Array(
Deno.UnsafePointerView.getArrayBuffer(
sqlite3_value_blob(arg),
sqlite3_value_blob(arg)!,
sqlite3_value_bytes(arg),
),
),
Expand Down Expand Up @@ -498,10 +506,10 @@ export class Database {
toCString(name),
options?.varargs ? -1 : fn.length,
flags,
0,
null,
cb.pointer,
0,
0,
null,
null,
);

unwrap(err, this.#handle);
Expand All @@ -513,7 +521,7 @@ export class Database {
* Creates a new user-defined aggregate function.
*/
aggregate(name: string, options: AggregateFunctionOptions): void {
const contexts = new Map<Deno.PointerValue, any>();
const contexts = new Map<number | bigint, any>();

const cb = new Deno.UnsafeCallback(
{
Expand All @@ -522,19 +530,22 @@ export class Database {
} as const,
(ctx, nArgs, pArgs) => {
const aggrCtx = sqlite3_aggregate_context(ctx, 8);
const aggrPtr = Deno.UnsafePointer.value(aggrCtx);
let aggregate;
if (contexts.has(aggrCtx)) {
aggregate = contexts.get(aggrCtx);
if (contexts.has(aggrPtr)) {
aggregate = contexts.get(aggrPtr);
} else {
aggregate = typeof options.start === "function"
? options.start()
: options.start;
contexts.set(aggrCtx, aggregate);
contexts.set(aggrPtr, aggregate);
}
const argptr = new Deno.UnsafePointerView(pArgs);
const argptr = new Deno.UnsafePointerView(pArgs!);
const args: any[] = [];
for (let i = 0; i < nArgs; i++) {
const arg = Number(argptr.getBigUint64(i * 8));
const arg = Deno.UnsafePointer.create(
Number(argptr.getBigUint64(i * 8)),
);
const type = sqlite3_value_type(arg);
switch (type) {
case SQLITE_INTEGER:
Expand All @@ -548,7 +559,7 @@ export class Database {
new TextDecoder().decode(
new Uint8Array(
Deno.UnsafePointerView.getArrayBuffer(
sqlite3_value_text(arg),
sqlite3_value_text(arg)!,
sqlite3_value_bytes(arg),
),
),
Expand All @@ -559,7 +570,7 @@ export class Database {
args.push(
new Uint8Array(
Deno.UnsafePointerView.getArrayBuffer(
sqlite3_value_blob(arg),
sqlite3_value_blob(arg)!,
sqlite3_value_bytes(arg),
),
),
Expand All @@ -582,7 +593,7 @@ export class Database {
return;
}

contexts.set(aggrCtx, result);
contexts.set(aggrPtr, result);
},
);

Expand All @@ -593,8 +604,9 @@ export class Database {
} as const,
(ctx) => {
const aggrCtx = sqlite3_aggregate_context(ctx, 0);
const aggregate = contexts.get(aggrCtx);
contexts.delete(aggrCtx);
const aggrPtr = Deno.UnsafePointer.value(aggrCtx);
const aggregate = contexts.get(aggrPtr);
contexts.delete(aggrPtr);
let result: any;
try {
result = options.final ? options.final(aggregate) : aggregate;
Expand Down Expand Up @@ -650,8 +662,8 @@ export class Database {
toCString(name),
options?.varargs ? -1 : options.step.length - 1,
flags,
0,
0,
null,
null,
cb.pointer,
cbFinal.pointer,
);
Expand Down Expand Up @@ -679,8 +691,10 @@ export class Database {
pzErrMsg,
);

const pzErrPtr = pzErrMsg[0] + 2 ** 32 * pzErrMsg[1];
if (pzErrPtr !== 0) {
const pzErrPtr = Deno.UnsafePointer.create(
pzErrMsg[0] + 2 ** 32 * pzErrMsg[1],
);
if (pzErrPtr !== null) {
const pzErr = readCstr(pzErrPtr);
sqlite3_free(pzErrPtr);
throw new Error(pzErr);
Expand Down
14 changes: 7 additions & 7 deletions src/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const symbols = {
sqlite3_column_type: {
parameters: [
"pointer", // sqlite3_stmt *pStmt
"pointer", // int iCol
"i32", // int iCol
],
result: "i32",
},
Expand All @@ -108,7 +108,7 @@ const symbols = {
"pointer", // sqlite3_stmt *pStmt
"i32", // int iCol
],
result: "u64",
result: "pointer",
},

sqlite3_finalize: {
Expand Down Expand Up @@ -157,7 +157,7 @@ const symbols = {
"pointer", // sqlite3_stmt *pStmt
"i32", // int iCol
],
result: "u64",
result: "pointer",
},

sqlite3_column_bytes: {
Expand All @@ -173,7 +173,7 @@ const symbols = {
"pointer", // sqlite3_stmt *pStmt
"i32", // int iCol
],
result: "u64",
result: "pointer",
},

sqlite3_column_decltype: {
Expand Down Expand Up @@ -253,7 +253,7 @@ const symbols = {
parameters: [
"pointer", // sqlite3_stmt *pStmt
],
result: "u64",
result: "pointer",
},

sqlite3_bind_parameter_count: {
Expand Down Expand Up @@ -425,7 +425,7 @@ const symbols = {
"pointer", // sqlite3_context *p
"buffer", // const void *z
"i32", // int n
"pointer", // void (*xDel)(void*)
"isize", // void (*xDel)(void*)
],
result: "void",
},
Expand Down Expand Up @@ -475,7 +475,7 @@ const symbols = {
"pointer", // sqlite3_context *p
"buffer", // const char *z
"i32", // int n
"pointer", // void (*xDel)(void*)
"isize", // void (*xDel)(void*)
],
result: "void",
},
Expand Down
Loading

0 comments on commit 4cb7e8f

Please sign in to comment.