-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
164 lines (144 loc) · 3.46 KB
/
mod.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import lib from "./sys.ts";
const {
mysql_init,
mysql_real_connect,
mysql_real_query,
mysql_stmt_init,
mysql_stmt_prepare,
mysql_stmt_execute,
mysql_stmt_close,
mysql_close,
mysql_stmt_error,
mysql_error,
mysql_free_result,
mysql_num_rows,
mysql_store_result,
mysql_num_fields,
mysql_fetch_row,
mysql_library_init,
} = lib;
const { getArrayBuffer, getCString } = Deno.UnsafePointerView;
const encode = Deno.core?.encode || ((s) => new TextEncoder().encode(s));
function cstr(str?: string) {
return str ? encode(str + "\0") : null;
}
class Statement {
#stmt;
constructor(handle) {
this.#stmt = mysql_stmt_init(handle);
}
prepare(query: string) {
const q = encode(query);
mysql_stmt_prepare(this.#stmt, q, q.byteLength);
}
execute() {
mysql_stmt_execute(this.#stmt);
}
close() {
mysql_stmt_close(this.#stmt);
}
}
function unwrapErr(e) {
const str = getCString(e);
if (str) throw new Error(str);
}
export class Connection {
#handle;
constructor() {
this.#handle = mysql_init(null);
}
connect({ host, user, password, database, port, unixSocket, flags }) {
mysql_real_connect(
this.#handle,
cstr(host),
cstr(user),
cstr(password),
cstr(database),
port || 0,
cstr(unixSocket),
flags || 0,
);
unwrapErr(mysql_error(this.#handle));
}
#lastResult = null;
#clearResult() {
if (this.#lastResult) {
mysql_free_result(this.#lastResult);
this.#lastResult = null;
}
}
execute(query: string) {
this.#clearResult();
mysql_real_query(this.#handle, cstr(query), query.length);
unwrapErr(mysql_error(this.#handle));
}
prepare(query: string) {
const stmt = new Statement(this.#handle);
stmt.prepare(query);
return stmt;
}
query(query: string) {
this.#clearResult();
mysql_real_query(this.#handle, cstr(query), query.length);
unwrapErr(mysql_error(this.#handle));
this.#lastResult = mysql_store_result(this.#handle);
return new Result(this.#lastResult);
}
close() {
mysql_close(this.#handle);
}
}
class Result {
#handle;
constructor(result) {
this.#handle = result;
}
#rowCount = null;
get rowCount() {
if (this.#rowCount === null) {
this.#rowCount = mysql_num_rows(this.#handle);
}
return this.#rowCount;
}
#fieldCount = null;
get fieldCount() {
if (this.#fieldCount === null) {
this.#fieldCount = mysql_num_fields(this.#handle);
}
return this.#fieldCount;
}
all() {
let data = new Array(this.rowCount);
let rowData = new Array(this.fieldCount);
while (true) {
const row = mysql_fetch_row(this.#handle);
if (row === 0) break;
const b = getArrayBuffer(row, rowData.length * 8);
const vui = new Uint32Array(b);
let j = 0;
for (let i = 0; i < rowData.length; i++) {
const n1 = vui[j] + 2 ** 32 * vui[j + 1];
rowData[i] = getCString(n1);
j += 2;
}
data.push(rowData);
}
return data;
}
*[Symbol.iterator]() {
let rowData = new Array(this.fieldCount);
while (true) {
const row = mysql_fetch_row(this.#handle);
if (row === 0) break;
const b = getArrayBuffer(row, rowData.length * 8);
const vui = new Uint32Array(b);
let j = 0;
for (let i = 0; i < rowData.length; i++) {
const n1 = vui[j] + 2 ** 32 * vui[j + 1];
rowData[i] = getCString(n1);
j += 2;
}
yield rowData;
}
}
}