@sqb/mariadb 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +191 -0
- package/README.md +65 -0
- package/index.d.ts +4 -0
- package/index.js +7 -0
- package/mariadb-adapter.d.ts +8 -0
- package/mariadb-adapter.js +32 -0
- package/mariadb-connection.d.ts +18 -0
- package/mariadb-connection.js +175 -0
- package/mariadb-cursor.d.ts +14 -0
- package/mariadb-cursor.js +37 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
https://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2013-2018 Docker, Inc.
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
<p style="text-align:center">
|
|
2
|
+
<img src="https://user-images.githubusercontent.com/3836517/32965280-1a2b63ce-cbe7-11e7-8ee1-ba47313503c5.png" width="500px" alt="SQB Logo"/>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<br>
|
|
6
|
+
|
|
7
|
+
[![NPM Version][npm-image]][npm-url]
|
|
8
|
+
[![NPM Downloads][downloads-image]][downloads-url]
|
|
9
|
+
[![CI Tests][ci-test-image]][ci-test-url]
|
|
10
|
+
[![Test Coverage][coveralls-image]][coveralls-url]
|
|
11
|
+
|
|
12
|
+
## About SQB
|
|
13
|
+
|
|
14
|
+
SQB is an extensible, multi-dialect SQL query builder and Database connection wrapper for NodeJS.
|
|
15
|
+
|
|
16
|
+
## About @sqb/mariadb
|
|
17
|
+
|
|
18
|
+
This package is a SQB `Adapter` for MariaDB, backed by the official
|
|
19
|
+
[`mariadb`](https://github.com/mariadb-corporation/mariadb-connector-nodejs) driver. It uses the
|
|
20
|
+
driver's named placeholder support (`:name`), which matches the parameter syntax `@sqb/builder`
|
|
21
|
+
generates by default.
|
|
22
|
+
|
|
23
|
+
Unlike [`@sqb/mysql`](../mysql), `INSERT ... RETURNING` and `DELETE ... RETURNING` don't need a
|
|
24
|
+
follow-up `SELECT`: MariaDB (10.5+) supports `RETURNING` natively for those, so affected rows are
|
|
25
|
+
read directly from the statement response. MariaDB has no `UPDATE ... RETURNING` (verified against
|
|
26
|
+
11.8), so that one case still falls back to a follow-up `SELECT`, the same way `@sqb/mysql` handles
|
|
27
|
+
every statement type.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
$ npm install @sqb/mariadb --save
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import '@sqb/mariadb';
|
|
39
|
+
import { SqbClient } from '@sqb/connect';
|
|
40
|
+
|
|
41
|
+
const client = new SqbClient({
|
|
42
|
+
driver: 'mariadb',
|
|
43
|
+
host: 'localhost',
|
|
44
|
+
port: 3306,
|
|
45
|
+
user: 'root',
|
|
46
|
+
database: 'mydb',
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Node Compatibility
|
|
51
|
+
|
|
52
|
+
- node >= 20.x
|
|
53
|
+
|
|
54
|
+
### License
|
|
55
|
+
|
|
56
|
+
SQB is available under [MIT](LICENSE) license.
|
|
57
|
+
|
|
58
|
+
[npm-image]: https://img.shields.io/npm/v/@sqb/mariadb.svg
|
|
59
|
+
[npm-url]: https://npmjs.org/package/@sqb/mariadb
|
|
60
|
+
[downloads-image]: https://img.shields.io/npm/dm/@sqb/mariadb.svg
|
|
61
|
+
[downloads-url]: https://npmjs.org/package/@sqb/mariadb
|
|
62
|
+
[ci-test-image]: https://github.com/panates/sqb/actions/workflows/test.yml/badge.svg
|
|
63
|
+
[ci-test-url]: https://github.com/panates/sqb/actions/workflows/test.yml
|
|
64
|
+
[coveralls-image]: https://coveralls.io/repos/github/sqbjs/sqb/badge.svg?branch=master
|
|
65
|
+
[coveralls-url]: https://coveralls.io/github/sqbjs/sqb?branch=master
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import '@sqb/mariadb-dialect';
|
|
2
|
+
import { AdapterRegistry } from '@sqb/connect';
|
|
3
|
+
import { MariadbAdapter } from './mariadb-adapter.js';
|
|
4
|
+
export * from './mariadb-adapter.js';
|
|
5
|
+
export * from './mariadb-connection.js';
|
|
6
|
+
export * from './mariadb-cursor.js';
|
|
7
|
+
AdapterRegistry.register(new MariadbAdapter());
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import '@sqb/mariadb-dialect';
|
|
2
|
+
import type { Adapter, ClientConfiguration } from '@sqb/connect';
|
|
3
|
+
export declare class MariadbAdapter implements Adapter {
|
|
4
|
+
driver: string;
|
|
5
|
+
dialect: string;
|
|
6
|
+
features: Adapter.Features;
|
|
7
|
+
connect(config: ClientConfiguration): Promise<Adapter.Connection>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import '@sqb/mariadb-dialect';
|
|
2
|
+
import { createConnection } from 'mariadb';
|
|
3
|
+
import { MariadbConnection } from './mariadb-connection.js';
|
|
4
|
+
export class MariadbAdapter {
|
|
5
|
+
driver = 'mariadb';
|
|
6
|
+
dialect = 'mariadb';
|
|
7
|
+
features = {
|
|
8
|
+
cursor: true,
|
|
9
|
+
};
|
|
10
|
+
async connect(config) {
|
|
11
|
+
const cfg = { ...config.driverOptions };
|
|
12
|
+
if (config.host)
|
|
13
|
+
cfg.host = config.host;
|
|
14
|
+
if (config.port)
|
|
15
|
+
cfg.port = config.port;
|
|
16
|
+
if (config.user)
|
|
17
|
+
cfg.user = config.user;
|
|
18
|
+
if (config.password)
|
|
19
|
+
cfg.password = config.password;
|
|
20
|
+
if (config.database)
|
|
21
|
+
cfg.database = config.database;
|
|
22
|
+
cfg.namedPlaceholders = true;
|
|
23
|
+
// Keep numeric/id columns as JS numbers instead of strings/BigInt, to
|
|
24
|
+
// match the convention @sqb/mysql already established for parity across
|
|
25
|
+
// adapters.
|
|
26
|
+
cfg.decimalAsNumber = true;
|
|
27
|
+
cfg.bigIntAsNumber = true;
|
|
28
|
+
cfg.insertIdAsNumber = true;
|
|
29
|
+
const connection = await createConnection(cfg);
|
|
30
|
+
return new MariadbConnection(connection);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Adapter, QueryRequest } from '@sqb/connect';
|
|
2
|
+
import type { Connection as MariadbDriverConnection } from 'mariadb';
|
|
3
|
+
export declare class MariadbConnection implements Adapter.Connection {
|
|
4
|
+
private intlcon?;
|
|
5
|
+
private _inTransaction;
|
|
6
|
+
constructor(conn: MariadbDriverConnection);
|
|
7
|
+
get sessionId(): any;
|
|
8
|
+
close(): Promise<void>;
|
|
9
|
+
reset(): Promise<void>;
|
|
10
|
+
startTransaction(): Promise<void>;
|
|
11
|
+
commit(): Promise<void>;
|
|
12
|
+
rollback(): Promise<void>;
|
|
13
|
+
getInTransaction(): boolean;
|
|
14
|
+
test(): Promise<void>;
|
|
15
|
+
execute(query: QueryRequest): Promise<Adapter.Response>;
|
|
16
|
+
private _fillSelectResult;
|
|
17
|
+
private _convertFields;
|
|
18
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { MariadbCursor } from './mariadb-cursor.js';
|
|
2
|
+
const typeMap = {
|
|
3
|
+
DECIMAL: { dataType: 'DECIMAL', jsType: 'number' },
|
|
4
|
+
NEWDECIMAL: { dataType: 'DECIMAL', jsType: 'number' },
|
|
5
|
+
TINY: { dataType: 'TINYINT', jsType: 'number' },
|
|
6
|
+
SHORT: { dataType: 'SMALLINT', jsType: 'number' },
|
|
7
|
+
LONG: { dataType: 'INT', jsType: 'number' },
|
|
8
|
+
INT24: { dataType: 'MEDIUMINT', jsType: 'number' },
|
|
9
|
+
FLOAT: { dataType: 'FLOAT', jsType: 'number' },
|
|
10
|
+
DOUBLE: { dataType: 'DOUBLE', jsType: 'number' },
|
|
11
|
+
BIGINT: { dataType: 'BIGINT', jsType: 'number' },
|
|
12
|
+
TIMESTAMP: { dataType: 'TIMESTAMP', jsType: 'Date' },
|
|
13
|
+
TIMESTAMP2: { dataType: 'TIMESTAMP', jsType: 'Date' },
|
|
14
|
+
DATE: { dataType: 'DATE', jsType: 'Date' },
|
|
15
|
+
NEWDATE: { dataType: 'DATE', jsType: 'Date' },
|
|
16
|
+
TIME: { dataType: 'TIME', jsType: 'string' },
|
|
17
|
+
TIME2: { dataType: 'TIME', jsType: 'string' },
|
|
18
|
+
DATETIME: { dataType: 'DATETIME', jsType: 'Date' },
|
|
19
|
+
DATETIME2: { dataType: 'DATETIME', jsType: 'Date' },
|
|
20
|
+
YEAR: { dataType: 'YEAR', jsType: 'number' },
|
|
21
|
+
BIT: { dataType: 'BIT', jsType: 'Buffer' },
|
|
22
|
+
JSON: { dataType: 'JSON', jsType: 'string' },
|
|
23
|
+
ENUM: { dataType: 'ENUM', jsType: 'string' },
|
|
24
|
+
SET: { dataType: 'SET', jsType: 'string' },
|
|
25
|
+
TINY_BLOB: { dataType: 'TINYBLOB', jsType: 'Buffer' },
|
|
26
|
+
MEDIUM_BLOB: { dataType: 'MEDIUMBLOB', jsType: 'Buffer' },
|
|
27
|
+
LONG_BLOB: { dataType: 'LONGBLOB', jsType: 'Buffer' },
|
|
28
|
+
BLOB: { dataType: 'BLOB', jsType: 'Buffer' },
|
|
29
|
+
VAR_STRING: { dataType: 'VARCHAR', jsType: 'string' },
|
|
30
|
+
STRING: { dataType: 'CHAR', jsType: 'string' },
|
|
31
|
+
VARCHAR: { dataType: 'VARCHAR', jsType: 'string' },
|
|
32
|
+
GEOMETRY: { dataType: 'GEOMETRY', jsType: 'Buffer' },
|
|
33
|
+
NULL: { dataType: 'UNKNOWN', jsType: 'any' },
|
|
34
|
+
};
|
|
35
|
+
export class MariadbConnection {
|
|
36
|
+
intlcon;
|
|
37
|
+
_inTransaction = false;
|
|
38
|
+
constructor(conn) {
|
|
39
|
+
this.intlcon = conn;
|
|
40
|
+
}
|
|
41
|
+
get sessionId() {
|
|
42
|
+
return this.intlcon?.threadId;
|
|
43
|
+
}
|
|
44
|
+
async close() {
|
|
45
|
+
if (!this.intlcon)
|
|
46
|
+
return;
|
|
47
|
+
const conn = this.intlcon;
|
|
48
|
+
this.intlcon = undefined;
|
|
49
|
+
// destroy() (not end()) because a cursor's stream may not have been
|
|
50
|
+
// fully consumed; end() waits for all in-flight commands to finish
|
|
51
|
+
// and would hang in that case.
|
|
52
|
+
conn.destroy();
|
|
53
|
+
}
|
|
54
|
+
async reset() {
|
|
55
|
+
return this.rollback();
|
|
56
|
+
}
|
|
57
|
+
async startTransaction() {
|
|
58
|
+
assertDefined(this.intlcon);
|
|
59
|
+
// MariaDB implicitly commits any already-active transaction when BEGIN
|
|
60
|
+
// is issued again, so no "already in transaction" guard is needed here.
|
|
61
|
+
await this.intlcon.beginTransaction();
|
|
62
|
+
this._inTransaction = true;
|
|
63
|
+
}
|
|
64
|
+
async commit() {
|
|
65
|
+
assertDefined(this.intlcon);
|
|
66
|
+
// COMMIT outside of an active transaction is a no-op in MariaDB.
|
|
67
|
+
await this.intlcon.commit();
|
|
68
|
+
this._inTransaction = false;
|
|
69
|
+
}
|
|
70
|
+
async rollback() {
|
|
71
|
+
assertDefined(this.intlcon);
|
|
72
|
+
await this.intlcon.rollback();
|
|
73
|
+
this._inTransaction = false;
|
|
74
|
+
}
|
|
75
|
+
getInTransaction() {
|
|
76
|
+
return this._inTransaction;
|
|
77
|
+
}
|
|
78
|
+
async test() {
|
|
79
|
+
assertDefined(this.intlcon);
|
|
80
|
+
await this.intlcon.query('select 1');
|
|
81
|
+
}
|
|
82
|
+
async execute(query) {
|
|
83
|
+
assertDefined(this.intlcon);
|
|
84
|
+
const intlcon = this.intlcon;
|
|
85
|
+
// MariaDB runs in autocommit mode by default; an explicit BEGIN is only
|
|
86
|
+
// needed once per logical transaction. Re-issuing BEGIN while already
|
|
87
|
+
// in a transaction would silently commit it early (MariaDB semantics),
|
|
88
|
+
// so it must be guarded with _inTransaction rather than called
|
|
89
|
+
// unconditionally.
|
|
90
|
+
if (!query.autoCommit && !this._inTransaction)
|
|
91
|
+
await this.startTransaction();
|
|
92
|
+
const params = query.params;
|
|
93
|
+
const rowsAsArray = !query.objectRows;
|
|
94
|
+
const rowType = query.objectRows ? 'object' : 'array';
|
|
95
|
+
const out = {};
|
|
96
|
+
if (query.cursor) {
|
|
97
|
+
const rawStream = intlcon.queryStream({ sql: query.sql, rowsAsArray }, params);
|
|
98
|
+
const fieldsPromise = new Promise((resolve, reject) => {
|
|
99
|
+
rawStream.once('fields', (f) => resolve(f));
|
|
100
|
+
rawStream.once('error', reject);
|
|
101
|
+
});
|
|
102
|
+
const cursor = new MariadbCursor(rawStream, { rowType });
|
|
103
|
+
out.fields = this._convertFields(await fieldsPromise);
|
|
104
|
+
out.rowType = rowType;
|
|
105
|
+
out.cursor = cursor;
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
// MariaDB has no UPDATE ... RETURNING (only INSERT/DELETE), so the
|
|
109
|
+
// dialect strips RETURNING there and it must be emulated with a
|
|
110
|
+
// follow-up SELECT reusing the original WHERE clause - the same
|
|
111
|
+
// approach @sqb/mysql uses for every statement type.
|
|
112
|
+
if (query.returningFields && /^\s*update\b/i.test(query.sql)) {
|
|
113
|
+
const info = await intlcon.execute({ sql: query.sql, rowsAsArray }, params);
|
|
114
|
+
if (query.autoCommit)
|
|
115
|
+
await this.commit();
|
|
116
|
+
out.rowsAffected = info.affectedRows;
|
|
117
|
+
if (out.rowsAffected === 1) {
|
|
118
|
+
const m = query.sql.match(/^\s*update\s+("?\w+"?)/i);
|
|
119
|
+
const wm = query.sql.match(/\bwhere\b([\s\S]+)$/i);
|
|
120
|
+
if (m && wm) {
|
|
121
|
+
const table = m[1].replace(/"/g, '');
|
|
122
|
+
const selectFields = query.returningFields.map(x => x.field + (x.alias ? ' as ' + x.alias : ''));
|
|
123
|
+
const sql = `select ${selectFields.join(',')} from ${table} where ${wm[1]}`;
|
|
124
|
+
await this._fillSelectResult(out, sql, params, query);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
const result = await intlcon.execute({ sql: query.sql, rowsAsArray }, params);
|
|
130
|
+
if (query.autoCommit)
|
|
131
|
+
await this.commit();
|
|
132
|
+
if (Array.isArray(result)) {
|
|
133
|
+
// A RETURNING clause on INSERT/DELETE (or a plain SELECT) makes the
|
|
134
|
+
// server respond with a result set instead of an OK packet - no
|
|
135
|
+
// follow-up SELECT is needed to read back inserted/deleted rows.
|
|
136
|
+
// `result` narrows to `any[]` here (a plain array type with no
|
|
137
|
+
// `.meta`), even though at runtime the driver attaches a non-
|
|
138
|
+
// enumerable `meta` property to it - cast back to `any` to read it.
|
|
139
|
+
out.fields = this._convertFields(result.meta);
|
|
140
|
+
out.rowType = rowType;
|
|
141
|
+
out.rows = query.fetchRows ? result.slice(0, query.fetchRows) : result;
|
|
142
|
+
if (query.returningFields)
|
|
143
|
+
out.rowsAffected = result.length;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
out.rowsAffected = result.affectedRows;
|
|
147
|
+
}
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
async _fillSelectResult(out, sql, params, query) {
|
|
151
|
+
assertDefined(this.intlcon);
|
|
152
|
+
const rowsAsArray = !query.objectRows;
|
|
153
|
+
const result = await this.intlcon.execute({ sql, rowsAsArray }, params);
|
|
154
|
+
out.fields = this._convertFields(result.meta);
|
|
155
|
+
out.rowType = query.objectRows ? 'object' : 'array';
|
|
156
|
+
out.rows = result;
|
|
157
|
+
}
|
|
158
|
+
_convertFields(fields) {
|
|
159
|
+
const result = [];
|
|
160
|
+
for (const f of fields) {
|
|
161
|
+
const t = typeMap[f.type] || { dataType: 'UNKNOWN', jsType: 'any' };
|
|
162
|
+
result.push({
|
|
163
|
+
fieldName: f.name(),
|
|
164
|
+
dataType: t.dataType,
|
|
165
|
+
jsType: t.jsType,
|
|
166
|
+
_inf: f,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function assertDefined(d) {
|
|
173
|
+
if (d == null)
|
|
174
|
+
throw new Error('DB session is closed');
|
|
175
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Readable } from 'node:stream';
|
|
2
|
+
import type { Adapter, RowType } from '@sqb/connect';
|
|
3
|
+
export declare class MariadbCursor implements Adapter.Cursor {
|
|
4
|
+
private _iterator?;
|
|
5
|
+
private readonly _rowType;
|
|
6
|
+
private readonly _stream;
|
|
7
|
+
constructor(stream: Readable, opts: {
|
|
8
|
+
rowType: RowType;
|
|
9
|
+
});
|
|
10
|
+
get isClosed(): boolean;
|
|
11
|
+
get rowType(): RowType;
|
|
12
|
+
close(): Promise<void>;
|
|
13
|
+
fetch(nRows: number): Promise<any[] | undefined>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export class MariadbCursor {
|
|
2
|
+
_iterator;
|
|
3
|
+
_rowType;
|
|
4
|
+
_stream;
|
|
5
|
+
constructor(stream, opts) {
|
|
6
|
+
this._rowType = opts.rowType;
|
|
7
|
+
this._stream = stream;
|
|
8
|
+
this._iterator = stream[Symbol.asyncIterator]();
|
|
9
|
+
}
|
|
10
|
+
get isClosed() {
|
|
11
|
+
return !this._iterator;
|
|
12
|
+
}
|
|
13
|
+
get rowType() {
|
|
14
|
+
return this._rowType;
|
|
15
|
+
}
|
|
16
|
+
async close() {
|
|
17
|
+
this._iterator = undefined;
|
|
18
|
+
// mariadb's stream exposes a custom close() that stops pulling further
|
|
19
|
+
// rows and resumes the underlying socket, so an early close doesn't
|
|
20
|
+
// leave it paused mid-backpressure.
|
|
21
|
+
this._stream.close?.();
|
|
22
|
+
}
|
|
23
|
+
async fetch(nRows) {
|
|
24
|
+
if (!this._iterator)
|
|
25
|
+
return undefined;
|
|
26
|
+
const rows = [];
|
|
27
|
+
while (nRows-- > 0) {
|
|
28
|
+
const r = await this._iterator.next();
|
|
29
|
+
if (r.done) {
|
|
30
|
+
this._iterator = undefined;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
rows.push(r.value);
|
|
34
|
+
}
|
|
35
|
+
return rows.length ? rows : undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sqb/mariadb",
|
|
3
|
+
"description": "SQB adapter for the mariadb driver",
|
|
4
|
+
"version": "6.0.0",
|
|
5
|
+
"author": "Panates",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"tslib": "^2.8.1"
|
|
9
|
+
},
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"@sqb/builder": "^6.0.0",
|
|
12
|
+
"@sqb/connect": "^6.0.0",
|
|
13
|
+
"@sqb/mariadb-dialect": "^6.0.0",
|
|
14
|
+
"mariadb": "^3.5.3"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"module": "./index.js",
|
|
18
|
+
"types": "./index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"default": "./index.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20.0"
|
|
28
|
+
},
|
|
29
|
+
"contributors": [
|
|
30
|
+
"Eray Hanoglu <e.hanoglu@panates.com>",
|
|
31
|
+
"Ilker Gurelli <i.gurelli@panates.com>"
|
|
32
|
+
],
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/panates/sqb.git",
|
|
36
|
+
"directory": "packages/mariadb"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"sqb",
|
|
40
|
+
"mariadb",
|
|
41
|
+
"sql",
|
|
42
|
+
"query",
|
|
43
|
+
"builder",
|
|
44
|
+
"dialect",
|
|
45
|
+
"serializer",
|
|
46
|
+
"database"
|
|
47
|
+
],
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|