-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite.deno.ts
140 lines (126 loc) · 2.79 KB
/
sqlite.deno.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {
SQLITE3_OPEN_CREATE,
SQLITE3_OPEN_MEMORY,
SQLITE3_OPEN_PRIVATECACHE,
SQLITE3_OPEN_READWRITE,
SQLITE3_ROW,
toCString,
unwrap,
} from "./sqlite_shared.mjs";
import { bench } from "./bench.mjs";
const { symbols } = Deno.dlopen("../sqlite3/build/libsqlite3_aarch64.dylib", {
sqlite3_initialize: {
parameters: [],
result: "i32",
},
sqlite3_open_v2: {
parameters: [
"buffer",
"buffer",
"i32",
"pointer",
],
result: "i32",
},
sqlite3_exec: {
parameters: [
"pointer", // sqlite3 *db
"buffer", // const char *sql
"pointer", // sqlite3_callback callback
"pointer", // void *arg
"buffer", // char **errmsg
],
result: "i32",
},
sqlite3_prepare_v2: {
parameters: [
"pointer", // sqlite3 *db
"buffer", // const char *zSql
"i32", // int nByte
"buffer", // sqlite3_stmt **ppStmt
"pointer", // const char **pzTail
],
result: "i32",
},
sqlite3_reset: {
parameters: [
"pointer", // sqlite3_stmt *pStmt
],
result: "i32",
},
sqlite3_step: {
parameters: [
"pointer", // sqlite3_stmt *pStmt
],
result: "i32",
},
sqlite3_column_int: {
parameters: [
"pointer", // sqlite3_stmt *pStmt
"i32", // int iCol
],
result: "i32",
},
});
const {
sqlite3_initialize,
sqlite3_open_v2,
sqlite3_exec,
sqlite3_prepare_v2,
sqlite3_reset,
sqlite3_step,
sqlite3_column_int,
} = symbols;
sqlite3_initialize();
const pHandle = new Uint32Array(2);
unwrap(
sqlite3_open_v2(
toCString(":memory:"),
pHandle,
SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_PRIVATECACHE |
SQLITE3_OPEN_CREATE | SQLITE3_OPEN_MEMORY,
null,
),
);
const db = Deno.UnsafePointer.create(pHandle[0] + 2 ** 32 * pHandle[1]);
function exec(sql) {
const _pErr = new Uint32Array(2);
unwrap(sqlite3_exec(db, toCString(sql), null, null, _pErr));
}
exec("PRAGMA auto_vacuum = none");
exec("PRAGMA temp_store = memory");
exec("PRAGMA locking_mode = exclusive");
exec("PRAGMA user_version = 100");
const sql = `WITH RECURSIVE c(x) AS (
VALUES(1)
UNION ALL
SELECT x+1 FROM c WHERE x<50
)
SELECT x, x as a FROM c;`;
function prepareStatement() {
const pHandle = new Uint32Array(2);
unwrap(
sqlite3_prepare_v2(
db,
toCString(sql),
sql.length,
pHandle,
null,
),
);
return Deno.UnsafePointer.create(pHandle[0] + 2 ** 32 * pHandle[1]);
}
const prepared = prepareStatement();
function run(): number[] {
const result: number[] = new Array(50);
let status = SQLITE3_ROW;
while(status === SQLITE3_ROW) {
status = sqlite3_step(prepared);
const int = sqlite3_column_int(prepared, 0);
result[int] = int;
}
sqlite3_reset(prepared);
return result;
}
console.log(`result: ${run()}`);
bench(run);