dsh-db-tool 1.5.91 → 1.7.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/client/client.js +980 -31
- package/dist/adapters/dmdb/index.js +25 -4
- package/dist/adapters/gaussdb/index.js +2 -0
- package/dist/adapters/mongodb/index.js +47 -8
- package/dist/adapters/mysql/index.js +31 -5
- package/dist/adapters/oracle/index.js +39 -9
- package/dist/adapters/postgresql/index.js +4 -0
- package/dist/adapters/sql-shared/pg-like.js +54 -16
- package/dist/adapters/sqlite/index.js +50 -19
- package/dist/adapters/transfer/kinds/mongo.js +378 -0
- package/dist/adapters/transfer/kinds/mysql.js +331 -0
- package/dist/adapters/transfer/kinds/oralike.js +437 -0
- package/dist/adapters/transfer/kinds/pg.js +311 -0
- package/dist/adapters/transfer/kinds/sqlite.js +250 -0
- package/dist/adapters/transfer/model.js +77 -0
- package/dist/http/index.js +74 -0
- package/dist/manager.js +239 -2
- package/dist/store/connections.js +85 -2
- package/dist/store/export-crypto.js +78 -0
- package/dist/store/index.js +7 -1
- package/dist/store/transfer-history.js +114 -0
- package/dist/transfer.js +287 -0
- package/package.json +1 -1
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
* - db.createPool({ connectString: 'dm://user:pass@host:5236', poolMin: 1, poolMax: 10,
|
|
13
13
|
* poolTimeout: 60, queueTimeout: 60000 });不启用压缩(默认 compress=0,规避
|
|
14
14
|
* @napi-rs/snappy 跨平台坑)。
|
|
15
|
-
* - execute 必须显式 maxRows(默认 0
|
|
15
|
+
* - execute 必须显式 maxRows(默认 0=无限制);autoCommit 显式传 true 使语义
|
|
16
|
+
* 不依赖驱动默认(驱动连接属性默认已是自动提交:dmdb connection.js
|
|
17
|
+
* conn_prop_autoCommit = !0;d.ts 三级优先链 执行选项 > dmdb.autoCommit > 连接属性)。
|
|
16
18
|
* - 返回 { metaData, rows, rowsAffected } 与 oracledb 兼容。
|
|
17
19
|
*/
|
|
18
20
|
import db from 'dmdb';
|
|
@@ -103,7 +105,8 @@ export async function createDmAdapter(conn, opts) {
|
|
|
103
105
|
throw humanizeDmError(e);
|
|
104
106
|
}
|
|
105
107
|
}
|
|
106
|
-
/** 默认 execute 选项:显式 maxRows
|
|
108
|
+
/** 默认 execute 选项:显式 maxRows(dmdb 默认 0=无限制,必须覆盖)+ 显式 autoCommit:true
|
|
109
|
+
* (驱动连接属性默认已是 true,显式传递仅让语义不依赖驱动默认;SELECT 立即结束隐式事务,避免 ro 事务悬挂) */
|
|
107
110
|
const execOpts = { outFormat: db.OUT_FORMAT_OBJECT, maxRows: ROWS_MAX + 1, autoCommit: true };
|
|
108
111
|
/** 事务内 execute:显式关闭自动提交,由 tx 的 commit/rollback 收口 */
|
|
109
112
|
const execOptsTx = { ...execOpts, autoCommit: false };
|
|
@@ -312,8 +315,10 @@ export async function createDmAdapter(conn, opts) {
|
|
|
312
315
|
const n = Math.max(1, Math.min(Math.floor(limit) || 20, ROWS_MAX));
|
|
313
316
|
const off = Math.max(0, Math.floor(offset ?? 0) || 0);
|
|
314
317
|
try {
|
|
315
|
-
// 统一 ANSI 分页(不依赖 compatibleMode=oracle
|
|
316
|
-
|
|
318
|
+
// 统一 ANSI 分页(不依赖 compatibleMode=oracle)。ROW_LIMIT 子句语法图的
|
|
319
|
+
// <offset>/<大小> 均为字面 <整数>(DM8 SQL 手册),绑定占位符能否用于该
|
|
320
|
+
// 位置无官方佐证 → 内联经 clamp 的整数(off/n 源自内部 offset/limit,无注入面)
|
|
321
|
+
const r = await withConn((c) => c.execute(`SELECT * FROM "${escIdent(owner)}"."${escIdent(tab)}" OFFSET ${off} ROWS FETCH FIRST ${n} ROWS ONLY`, [], execOpts));
|
|
317
322
|
return await rowsToQueryResult(r);
|
|
318
323
|
}
|
|
319
324
|
catch (e) {
|
|
@@ -330,3 +335,19 @@ export async function createDmAdapter(conn, opts) {
|
|
|
330
335
|
return adapter;
|
|
331
336
|
}
|
|
332
337
|
export const factory = async (conn) => createDmAdapter(conn);
|
|
338
|
+
/** 数据传输读写端用的独立连接池 */
|
|
339
|
+
export async function openDmPool(conn) {
|
|
340
|
+
const { connectString } = resolveDmConn(conn);
|
|
341
|
+
try {
|
|
342
|
+
return (await db.createPool({
|
|
343
|
+
connectString,
|
|
344
|
+
poolMin: 1,
|
|
345
|
+
poolMax: 10,
|
|
346
|
+
poolTimeout: 60,
|
|
347
|
+
queueTimeout: 60000,
|
|
348
|
+
}));
|
|
349
|
+
}
|
|
350
|
+
catch (e) {
|
|
351
|
+
throw humanizeDmError(e);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
@@ -170,6 +170,11 @@ export async function createMongoAdapter(conn, opts) {
|
|
|
170
170
|
? urlDbName(conn.url)
|
|
171
171
|
: defaultDbName(conn.fields);
|
|
172
172
|
const db = client.db(dbName);
|
|
173
|
+
/** 库定位:database 指定且非连接默认库时切库(client.db() 是轻量句柄,不建新连接);
|
|
174
|
+
* 表清单/预览按所选库真实返回,避免「清单来自固定库、定位发所选库」的错位 */
|
|
175
|
+
function dbOf(database) {
|
|
176
|
+
return database && database !== dbName ? client.db(database) : db;
|
|
177
|
+
}
|
|
173
178
|
function wrap(e) {
|
|
174
179
|
const M = vendorOrNull();
|
|
175
180
|
if (M && e instanceof M.MongoServerError) {
|
|
@@ -194,8 +199,17 @@ export async function createMongoAdapter(conn, opts) {
|
|
|
194
199
|
connId: conn.meta.id,
|
|
195
200
|
async testConnect() {
|
|
196
201
|
try {
|
|
197
|
-
|
|
198
|
-
|
|
202
|
+
// hello 响应没有 version 字段(官方手册字段表仅 isWritablePrimary/topologyVersion/
|
|
203
|
+
// maxBsonObjectSize/maxWireVersion 等),软件版本属 buildInfo 命令 → 取
|
|
204
|
+
// buildInfo.version;权限不足等失败回退 hello 仅判连通(版本信息尽力而为)
|
|
205
|
+
try {
|
|
206
|
+
const info = (await client.db('admin').command({ buildInfo: 1 }));
|
|
207
|
+
return { ok: true, serverInfo: `MongoDB ${String(info.version ?? '')}` };
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
await client.db('admin').command({ hello: 1 });
|
|
211
|
+
return { ok: true, serverInfo: 'MongoDB(版本信息不可读)' };
|
|
212
|
+
}
|
|
199
213
|
}
|
|
200
214
|
catch (e) {
|
|
201
215
|
return { ok: false, error: wrap(e).message };
|
|
@@ -338,8 +352,11 @@ export async function createMongoAdapter(conn, opts) {
|
|
|
338
352
|
: isPlainObject(cmd.indexes) ? [cmd.indexes] : Array.isArray(cmd.indexes) ? cmd.indexes : null;
|
|
339
353
|
if (!specs)
|
|
340
354
|
throw new Error('MongoDB createIndex 需要 index 规格对象或数组:{"createIndex":"coll","index":{"key":{"a":1},"name":"a_1"}}');
|
|
341
|
-
|
|
342
|
-
|
|
355
|
+
// 官方返回索引名数组(mongodb.d.ts: createIndexes(...): Promise<string[]>),
|
|
356
|
+
// 无 createdNewIndexes 字段(曾按记忆臆造,恒 undefined);驱动不区分
|
|
357
|
+
// 新建/已存在,affectedRows = 规格数
|
|
358
|
+
const created = await db.collection(name).createIndexes(specs);
|
|
359
|
+
return { affectedRows: created.length, message: `已在集合 ${name} 创建索引(${created.length} 个)` };
|
|
343
360
|
}
|
|
344
361
|
if (op === 'dropIndex' || op === 'dropIndexes') {
|
|
345
362
|
const name = collOf(cmd, op);
|
|
@@ -417,9 +434,9 @@ export async function createMongoAdapter(conn, opts) {
|
|
|
417
434
|
throw wrap(e);
|
|
418
435
|
}
|
|
419
436
|
},
|
|
420
|
-
async listTables() {
|
|
437
|
+
async listTables(database) {
|
|
421
438
|
try {
|
|
422
|
-
const infos = await
|
|
439
|
+
const infos = await dbOf(database).listCollections().toArray();
|
|
423
440
|
return infos.map((c) => ({ name: c.name, type: c.type === 'view' ? 'view' : 'collection' }));
|
|
424
441
|
}
|
|
425
442
|
catch (e) {
|
|
@@ -468,11 +485,11 @@ export async function createMongoAdapter(conn, opts) {
|
|
|
468
485
|
throw wrap(e);
|
|
469
486
|
}
|
|
470
487
|
},
|
|
471
|
-
async previewRows(name, limit,
|
|
488
|
+
async previewRows(name, limit, database, offset) {
|
|
472
489
|
try {
|
|
473
490
|
const n = Math.max(1, Math.min(Math.floor(limit) || 20, FIND_MAX_LIMIT));
|
|
474
491
|
const skip = Math.max(0, Math.floor(offset ?? 0) || 0);
|
|
475
|
-
const docs = await
|
|
492
|
+
const docs = await dbOf(database).collection(name).find({}).sort({ _id: 1 }).skip(skip).limit(n).toArray();
|
|
476
493
|
return docsToResult(docs, false);
|
|
477
494
|
}
|
|
478
495
|
catch (e) {
|
|
@@ -587,3 +604,25 @@ function sanitizeUri(uri) {
|
|
|
587
604
|
return uri.replace(/:\/\/[^@/]*@/, '://***:***@');
|
|
588
605
|
}
|
|
589
606
|
export const factory = async (conn) => createMongoAdapter(conn);
|
|
607
|
+
export async function openMongoRaw(conn) {
|
|
608
|
+
const M = vendor();
|
|
609
|
+
const uri = conn.url ?? buildUri(conn.fields, conn.ssl);
|
|
610
|
+
const client = new M.MongoClient(uri, {
|
|
611
|
+
maxPoolSize: 10,
|
|
612
|
+
readPreference: 'primary',
|
|
613
|
+
w: 'majority',
|
|
614
|
+
family: 4,
|
|
615
|
+
});
|
|
616
|
+
try {
|
|
617
|
+
await client.connect();
|
|
618
|
+
}
|
|
619
|
+
catch (e) {
|
|
620
|
+
throw new Error(`MongoDB 连接失败(${sanitizeUri(uri)}):${e instanceof Error ? e.message : String(e)}`);
|
|
621
|
+
}
|
|
622
|
+
const dbName = typeof conn.meta.database === 'string' && conn.meta.database !== ''
|
|
623
|
+
? conn.meta.database
|
|
624
|
+
: conn.url
|
|
625
|
+
? urlDbName(conn.url)
|
|
626
|
+
: defaultDbName(conn.fields);
|
|
627
|
+
return { client, db: client.db(dbName), M };
|
|
628
|
+
}
|
|
@@ -9,6 +9,23 @@ import mysql from 'mysql2/promise';
|
|
|
9
9
|
import { assertIdent, clampLimit, clampOffset, humanize, normalizeCell, quoteIdent } from '../sql-shared/common.js';
|
|
10
10
|
/** ro 模式下 query 允许的读语句首词白名单 */
|
|
11
11
|
const RO_READ_PREFIX = /^(select|show|desc|describe|explain|use|help|table)\b/i;
|
|
12
|
+
/**
|
|
13
|
+
* 吞掉 'connection' 回调里会话级 SET 的失败(ro / time_zone 仅是增强,失败不应拖垮连接与进程)。
|
|
14
|
+
* mysql2 promise 池的 'connection' 事件由核心池 emit、经 lib/promise/inherit_events.js
|
|
15
|
+
* 原样转发参数,回调收到的是 callback 版 PoolConnection:其 query() 返回 Query 命令
|
|
16
|
+
* 对象(EventEmitter,无 onResult 时错误走 emit('error'),lib/commands/command.js),
|
|
17
|
+
* 无监听会抛 uncaughtException 崩溃进程——.catch 防御对它恒不生效,必须挂 error 监听。
|
|
18
|
+
* 若驱动版本改为返回 Promise,则走 .catch 吞 rejection(双分支防御)。
|
|
19
|
+
*/
|
|
20
|
+
function swallowSetError(r) {
|
|
21
|
+
if (r && typeof r.catch === 'function') {
|
|
22
|
+
void r.catch(() => { });
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (r && typeof r.on === 'function') {
|
|
26
|
+
r.on('error', () => { });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
12
29
|
function connOptions(conn) {
|
|
13
30
|
const base = {
|
|
14
31
|
waitForConnections: true,
|
|
@@ -65,12 +82,10 @@ export async function createMysqlAdapter(conn, opts) {
|
|
|
65
82
|
const readOnly = opts?.mode === 'ro';
|
|
66
83
|
if (readOnly) {
|
|
67
84
|
// 服务器级 ro 强制:每个新底层连接自动设为只读会话(应用层另有 query 白名单双保险)。
|
|
68
|
-
//
|
|
85
|
+
// 回调收到的是 callback 版 PoolConnection,query() 返回 Query 命令对象(EventEmitter):
|
|
86
|
+
// 吞掉 SET 失败防 uncaughtException(见 swallowSetError)。
|
|
69
87
|
pool.on('connection', (c) => {
|
|
70
|
-
|
|
71
|
-
if (r && typeof r.catch === 'function') {
|
|
72
|
-
void r.catch(() => { });
|
|
73
|
-
}
|
|
88
|
+
swallowSetError(c.query('SET SESSION TRANSACTION READ ONLY'));
|
|
74
89
|
});
|
|
75
90
|
}
|
|
76
91
|
/** 事务激活期间路由到同一连接,保证 BEGIN/COMMIT 生效 */
|
|
@@ -220,3 +235,14 @@ export async function createMysqlAdapter(conn, opts) {
|
|
|
220
235
|
};
|
|
221
236
|
}
|
|
222
237
|
export const factory = (conn) => createMysqlAdapter(conn);
|
|
238
|
+
export async function openMysqlRaw(conn) {
|
|
239
|
+
const opts = connOptions(conn);
|
|
240
|
+
const pool = mysql.createPool({ ...opts, dateStrings: true });
|
|
241
|
+
// 回调式 query(带 err 回调吞错)。必须用回调式:非回调式返回的 Query 命令对象与
|
|
242
|
+
// promise 池连接状态机冲突 → 该连接所有后续 query 永挂(真机实证 mysql2 3.24.4)
|
|
243
|
+
pool.on('connection', (c) => {
|
|
244
|
+
// 运行时 c 是 callback 版连接(promise 池类型标注不反映),按回调式调用
|
|
245
|
+
c.query("SET time_zone = '+00:00'", () => { });
|
|
246
|
+
});
|
|
247
|
+
return { pool, database: typeof opts.database === 'string' ? opts.database : '' };
|
|
248
|
+
}
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
* 设计要点(依据官方文档调研):
|
|
5
5
|
* - oracledb.createPool({ user, password, connectString, poolMin: 1, poolMax: 10, poolTimeout: 60 })。
|
|
6
6
|
* 禁止 SYSDBA 等特权连接(不透传 privilege)。
|
|
7
|
-
* - CLOB 经全局 fetchAsString 转字符串;BLOB
|
|
7
|
+
* - CLOB 经全局 fetchAsString 转字符串;BLOB 经全局 fetchAsBuffer 转 Buffer。
|
|
8
|
+
* (fetchInfo 的键是【列名】而非类型名,按类型统一转换只能用全局 fetchXXX 属性——
|
|
9
|
+
* 官方 connection.rst「fetchInfo」节:「Each column is specified by name」)
|
|
8
10
|
* NUMBER 保持 number(超精度损失由用户承担,见文档标注)。
|
|
9
11
|
* - query() 仅允许 SELECT/WITH;execute() DML/DDL,autoCommit: true 单语句语义。
|
|
10
12
|
* - Oracle 无 BEGIN,事务用 SET TRANSACTION READ WRITE + commit/rollback(tx 成员)。
|
|
@@ -14,14 +16,17 @@
|
|
|
14
16
|
*/
|
|
15
17
|
import oracledb from 'oracledb';
|
|
16
18
|
import { sqlHead, SQL_READ_HEADS } from '../../guard/index.js';
|
|
17
|
-
// CLOB
|
|
19
|
+
// CLOB 统一按字符串返回;BLOB 统一按 Buffer 返回。均为官方全局按类型转换
|
|
20
|
+
// (fetchInfo 的键是列名而非类型名,`{ BLOB: … }` 只会匹配恰好名为 "BLOB" 的列,
|
|
21
|
+
// 按类型统一转换须用全局 fetchAsString/fetchAsBuffer——官方 connection.rst
|
|
22
|
+
// 「fetchInfo」节原文「Each column is specified by name, using Oracle's standard
|
|
23
|
+
// naming convention.」;fetchInfo 自 6.0 标记 deprecated,官方建议全局属性/fetchTypeHandler)
|
|
18
24
|
if (!oracledb.fetchAsString.includes(oracledb.CLOB)) {
|
|
19
25
|
oracledb.fetchAsString = [...oracledb.fetchAsString, oracledb.CLOB];
|
|
20
26
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
27
|
+
if (!oracledb.fetchAsBuffer.includes(oracledb.BLOB)) {
|
|
28
|
+
oracledb.fetchAsBuffer = [...oracledb.fetchAsBuffer, oracledb.BLOB];
|
|
29
|
+
}
|
|
25
30
|
const ROWS_MAX = 500;
|
|
26
31
|
const CELL_TRUNC = 1000;
|
|
27
32
|
const DDL_KEYWORDS = new Set([
|
|
@@ -75,8 +80,16 @@ async function normalizeCell(v) {
|
|
|
75
80
|
return trunc(v);
|
|
76
81
|
if (v instanceof Date)
|
|
77
82
|
return v.toISOString();
|
|
78
|
-
if (
|
|
79
|
-
|
|
83
|
+
if (Buffer.isBuffer(v))
|
|
84
|
+
return trunc(`0x${v.toString('hex')}`); // fetchAsBuffer 产出的 BLOB
|
|
85
|
+
if (v instanceof oracledb.Lob) {
|
|
86
|
+
// 官方 Lob 类成员仅 getData()/read()/close()/destroy() 等,无 toString()(官方 lob.rst;
|
|
87
|
+
// 实测 Lob.prototype.toString 即 Object.prototype.toString)——曾误用 toString()
|
|
88
|
+
// 致 LOB 列恒显示 '[object Object]'。getData() 按 CLOB/BFILE 返回 string、按
|
|
89
|
+
// 未转换 BLOB 返回 Buffer。判别用官方导出类 instanceof(constructor.name 非
|
|
90
|
+
// 官方承诺,esbuild 压缩改名场景会失效)。
|
|
91
|
+
const data = await v.getData();
|
|
92
|
+
const s = typeof data === 'string' ? data : `0x${Buffer.from(data).toString('hex')}`;
|
|
80
93
|
return trunc(s);
|
|
81
94
|
}
|
|
82
95
|
return trunc(String(v));
|
|
@@ -166,7 +179,7 @@ export async function createOracleAdapter(conn, opts) {
|
|
|
166
179
|
return { columns, rows, rowCount: rows.length, truncated: truncated || undefined };
|
|
167
180
|
}
|
|
168
181
|
// autoCommit 默认 true:query/元数据 SELECT 结束隐式事务避免 ro 事务悬挂;tx 专用连接显式 autoCommit:false 覆盖
|
|
169
|
-
const execOpts = { outFormat: oracledb.OBJECT, maxRows: ROWS_MAX + 1, autoCommit: true
|
|
182
|
+
const execOpts = { outFormat: oracledb.OBJECT, maxRows: ROWS_MAX + 1, autoCommit: true };
|
|
170
183
|
/**
|
|
171
184
|
* 从池借一条连接执行后归还。⚠️ oracledb 的 Pool 没有 execute,
|
|
172
185
|
* 一切语句必须在 Connection 上执行;close() 即归还连接。
|
|
@@ -374,3 +387,20 @@ export async function createOracleAdapter(conn, opts) {
|
|
|
374
387
|
return adapter;
|
|
375
388
|
}
|
|
376
389
|
export const factory = async (conn) => createOracleAdapter(conn);
|
|
390
|
+
/** 数据传输读写端用的独立连接池(复用 resolveOracleConn 与错误包装) */
|
|
391
|
+
export async function openOraclePool(conn) {
|
|
392
|
+
const { user, password, connectString } = resolveOracleConn(conn);
|
|
393
|
+
try {
|
|
394
|
+
return await oracledb.createPool({
|
|
395
|
+
user,
|
|
396
|
+
password,
|
|
397
|
+
connectString,
|
|
398
|
+
poolMin: 1,
|
|
399
|
+
poolMax: 10,
|
|
400
|
+
poolTimeout: 60,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
catch (e) {
|
|
404
|
+
throw humanizeOraError(e);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
import { createRequire } from 'node:module';
|
|
9
9
|
import { createPgLikeAdapter } from '../sql-shared/pg-like.js';
|
|
10
10
|
const require = createRequire(import.meta.url);
|
|
11
|
+
/** 数据传输模块用的驱动加载入口(与适配器同一份 require 缓存) */
|
|
12
|
+
export function getPgDriver() {
|
|
13
|
+
return require('pg');
|
|
14
|
+
}
|
|
11
15
|
export async function createPostgresqlAdapter(conn, opts) {
|
|
12
16
|
const driver = require('pg');
|
|
13
17
|
return createPgLikeAdapter('postgresql', driver, conn, opts);
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { assertIdent, clampLimit, clampOffset, humanize, normalizeCell, quoteIdent } from './common.js';
|
|
2
|
-
|
|
2
|
+
/** 多主机 authority 逐台重组段(host[:port]) */
|
|
3
|
+
function joinHostSeg(h) {
|
|
4
|
+
return `${h.host}${h.port != null ? ':' + h.port : ''}`;
|
|
5
|
+
}
|
|
6
|
+
function poolConfig(conn, overrideUrl, max = 10) {
|
|
3
7
|
const base = {
|
|
4
|
-
max
|
|
8
|
+
max,
|
|
5
9
|
idleTimeoutMillis: 30000,
|
|
6
10
|
connectionTimeoutMillis: 5000,
|
|
7
11
|
};
|
|
@@ -104,18 +108,22 @@ export async function pickHost(driver, base, hosts, masterOnly) {
|
|
|
104
108
|
}
|
|
105
109
|
return undefined;
|
|
106
110
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
111
|
+
/**
|
|
112
|
+
* 建池前置解析(浏览适配器与传输读写端共用,保证两条路径连接语义一致):
|
|
113
|
+
* URL 规范化(剥 jdbc:、多主机 authority 重组)后,多主机一律逐台探测选可用节点——
|
|
114
|
+
* 纯 JS 驱动(pg 8.x 与 gaussdb-node)均无 libpq 式多主机 failover:带端口多台
|
|
115
|
+
* connectionString 在 pg-connection-string / gaussdb-connection-string parse 时直接抛
|
|
116
|
+
* Invalid URL(实测),不带端口则整串 host 'h1,h2' 直传 net.connect → DNS ENOTFOUND,
|
|
117
|
+
* 故不能透传,须拆台探测(targetservertype=master 两 kind 同义:只接受主节点)。
|
|
118
|
+
* 返回最终驱动池配置与最终连接 URL。
|
|
119
|
+
*/
|
|
120
|
+
export async function pgLikePoolCfg(kind, driver, conn, max) {
|
|
112
121
|
const norm = conn.url ? normalizeUrl(conn.url) : null;
|
|
113
|
-
const
|
|
114
|
-
const cleanUrl = norm ? norm.prefix + (norm.hosts ?? []).map(joinSeg).join(',') + norm.suffix : undefined;
|
|
122
|
+
const cleanUrl = norm ? norm.prefix + (norm.hosts ?? []).map(joinHostSeg).join(',') + norm.suffix : undefined;
|
|
115
123
|
let finalUrl = cleanUrl;
|
|
116
|
-
if (
|
|
124
|
+
if (norm?.hosts && norm.hosts.length > 1) {
|
|
117
125
|
const { prefix, suffix } = norm;
|
|
118
|
-
const joinHost = (h) => prefix +
|
|
126
|
+
const joinHost = (h) => prefix + joinHostSeg(h) + suffix;
|
|
119
127
|
const firstUrl = joinHost(norm.hosts[0]);
|
|
120
128
|
// targetServerType=master(键值大小写不敏感)→ 只接受主节点
|
|
121
129
|
const qi = suffix.indexOf('?');
|
|
@@ -124,7 +132,7 @@ export async function createPgLikeAdapter(kind, Driver, conn, opts) {
|
|
|
124
132
|
if (k.toLowerCase() === 'targetservertype' && v.toLowerCase() === 'master')
|
|
125
133
|
masterOnly = true;
|
|
126
134
|
}
|
|
127
|
-
const base = poolConfig(conn, firstUrl);
|
|
135
|
+
const base = poolConfig(conn, firstUrl, max);
|
|
128
136
|
// 探测凭据自 prefix userinfo 解出(按最后一个 @ / 最后一个 : 分界,decode 与
|
|
129
137
|
// percent-encode 注入互逆);仅密码无用户名时 user 不设
|
|
130
138
|
if (prefix.endsWith('@')) {
|
|
@@ -140,11 +148,39 @@ export async function createPgLikeAdapter(kind, Driver, conn, opts) {
|
|
|
140
148
|
base.urlUser = decodeURIComponent(body);
|
|
141
149
|
}
|
|
142
150
|
}
|
|
143
|
-
const picked = await pickHost(
|
|
151
|
+
const picked = await pickHost(driver, base, norm.hosts, masterOnly);
|
|
144
152
|
// 全败回退首台重组值:让 testConnect 报真实连接错误
|
|
145
153
|
finalUrl = picked ? joinHost(picked) : firstUrl;
|
|
146
154
|
}
|
|
147
|
-
|
|
155
|
+
return { cfg: poolConfig(conn, finalUrl, max), url: finalUrl };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* 生成「同一份池配置仅覆盖库名」的 cfg:pg / gaussdb ConnectionParameters 按
|
|
159
|
+
* Object.assign({}, config, parse(config.connectionString)) 合并——connectionString
|
|
160
|
+
* 解析值覆盖全部显式字段(实测:{connectionString:'postgres://u:p@h1:5432/appdb',
|
|
161
|
+
* database:'otherdb',host:'x',user:'y'} → database 'appdb'/host 'h1'/user 'u';
|
|
162
|
+
* node-postgres packages/pg/lib/connection-parameters.js:53-57,gaussdb-node
|
|
163
|
+
* lib/connection-parameters.js:55-57 同款)。故 URL 形态叠加 database 字段会被 URL
|
|
164
|
+
* 里的原库名覆盖(跨库池静默连回原库),必须改写 URL path 段;字段形态直接叠加。
|
|
165
|
+
* 写回库名 encodeURIComponent 与驱动 parse 的 decodeURI 对空格/非 ASCII 往返无损
|
|
166
|
+
* (两驱动实测 parse('.../a%20b') → database 'a b';两驱动源码 pg-connection-string
|
|
167
|
+
* index.js:67 / gaussdb-connection-string index.js:62 均为 decodeURI(pathname))——
|
|
168
|
+
* 含 # / 等保留字符的库名 decodeURI 不还原(驱动能力边界,libpq 才有完整
|
|
169
|
+
* percent-decoding),此类库名无法经 URL 形态无损表达。
|
|
170
|
+
*/
|
|
171
|
+
export function pgCfgWithDatabase(cfg, db) {
|
|
172
|
+
const cs = cfg.connectionString;
|
|
173
|
+
// 无 authority 的非 URL 形态不产生垃圾改写,退回字段叠加
|
|
174
|
+
if (typeof cs !== 'string' || !cs.includes('://'))
|
|
175
|
+
return { ...cfg, database: db };
|
|
176
|
+
const norm = normalizeUrl(cs);
|
|
177
|
+
const qi = /[?#]/.exec(norm.suffix)?.index ?? -1;
|
|
178
|
+
const tail = qi >= 0 ? norm.suffix.slice(qi) : '';
|
|
179
|
+
const hosts = (norm.hosts ?? []).map(joinHostSeg).join(',');
|
|
180
|
+
return { ...cfg, connectionString: `${norm.prefix}${hosts}/${encodeURIComponent(db)}${tail}` };
|
|
181
|
+
}
|
|
182
|
+
export async function createPgLikeAdapter(kind, Driver, conn, opts) {
|
|
183
|
+
const { cfg, url: finalUrl } = await pgLikePoolCfg(kind, Driver, conn, 10);
|
|
148
184
|
const pool = new Driver.Pool(cfg);
|
|
149
185
|
const readOnly = opts?.mode === 'ro';
|
|
150
186
|
// 所有池(主池 + 跨库池)统一走此初始化。
|
|
@@ -189,8 +225,10 @@ export async function createPgLikeAdapter(kind, Driver, conn, opts) {
|
|
|
189
225
|
const cached = dbPools.get(db);
|
|
190
226
|
if (cached)
|
|
191
227
|
return cached;
|
|
192
|
-
// 与主池同一份最终 cfg(多主机探测选定台后的 connectionString
|
|
193
|
-
|
|
228
|
+
// 与主池同一份最终 cfg(多主机探测选定台后的 connectionString),仅覆盖库名——
|
|
229
|
+
// URL 形态经 pgCfgWithDatabase 改写 path(叠加 database 字段会被驱动里 URL
|
|
230
|
+
// 库名覆盖,静默连回原库)
|
|
231
|
+
const created = new Driver.Pool(pgCfgWithDatabase(cfg, db));
|
|
194
232
|
setupPool(created); // 与主池一致:error 兜底 + ro 会话只读
|
|
195
233
|
dbPools.set(db, created);
|
|
196
234
|
return created;
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLite 适配器(同步 API 包装 Promise)。
|
|
3
|
+
* fields.database / fields.path 为文件路径;ro 模式 readonly 打开。
|
|
4
|
+
*
|
|
5
|
+
* 驱动双路径(离线环境友好):
|
|
6
|
+
* 1. node:sqlite(Node ≥ 22.5 内置,零原生依赖,离线/无编译工具链环境直接可用)
|
|
7
|
+
* 2. better-sqlite3(optionalDependency;预构建缺失或本地编译失败时 pnpm 会
|
|
8
|
+
* 自动排除该包,不影响插件整体安装,动态加载失败时回退到明确报错)
|
|
9
|
+
*/
|
|
10
|
+
import { basename } from 'node:path';
|
|
1
11
|
import { assertIdent, clampLimit, clampOffset, humanize, normalizeCell, quoteIdent } from '../sql-shared/common.js';
|
|
2
12
|
let cached = null;
|
|
3
13
|
let cachedFor = '';
|
|
@@ -57,30 +67,53 @@ function resolveFile(conn) {
|
|
|
57
67
|
}
|
|
58
68
|
throw new Error('sqlite 连接缺少文件路径(fields.database / fields.path 或 url)');
|
|
59
69
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
export async function createSqliteAdapter(conn, opts) {
|
|
70
|
+
/**
|
|
71
|
+
* 打开原始数据库句柄(不包适配器规范化)。数据传输读写端用:
|
|
72
|
+
* 必须绕过 normalizeCell(BLOB 截断 / bigint 降位)保真搬运原始值。
|
|
73
|
+
*/
|
|
74
|
+
export async function openRaw(conn, readOnly) {
|
|
68
75
|
const file = resolveFile(conn);
|
|
69
|
-
const readOnly = opts?.mode === 'ro';
|
|
70
76
|
const { ctor, driver } = await loadDriver();
|
|
71
|
-
let db;
|
|
72
77
|
try {
|
|
73
|
-
// 选项名按驱动区分:node:sqlite 用 readOnly
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
|
|
78
|
+
// 选项名按驱动区分:node:sqlite 用 readOnly(打开不存在文件即报错;官方签名
|
|
79
|
+
// new DatabaseSync(location[, options]),options 为可选参数,空对象与不传/undefined
|
|
80
|
+
// 等价,open 默认 true —— https://nodejs.org/docs/latest-v24.x/api/sqlite.html);
|
|
81
|
+
// better-sqlite3 用 readonly + fileMustExist。rw 模式两驱动均传空对象。
|
|
82
|
+
const db = driver === 'node:sqlite'
|
|
77
83
|
? new ctor(file, readOnly ? { readOnly: true } : {})
|
|
78
84
|
: new ctor(file, readOnly ? { readonly: true, fileMustExist: true } : {});
|
|
85
|
+
return { db, driver };
|
|
79
86
|
}
|
|
80
87
|
catch (e) {
|
|
81
88
|
const msg = e instanceof Error ? e.message : String(e);
|
|
82
|
-
|
|
89
|
+
// 只带文件名不带完整路径:错误会经驱动错误通道外发(传输失败快照等),避免内网路径泄露
|
|
90
|
+
throw new Error(`sqlite 打开失败 (${basename(file)}, 驱动 ${driver}): ${msg}`);
|
|
83
91
|
}
|
|
92
|
+
}
|
|
93
|
+
function toQueryResult(raw, columns) {
|
|
94
|
+
return {
|
|
95
|
+
columns,
|
|
96
|
+
rows: raw.map((r) => columns.map((c) => normalizeCell(r[c]))),
|
|
97
|
+
rowCount: raw.length,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* 取 SELECT 列名。stmt.columns() 在 node:sqlite 需 Node ≥ 22.16.0(官方文档
|
|
102
|
+
* "Added in: v22.16.0",https://nodejs.org/docs/latest-v22.x/api/sqlite.html),
|
|
103
|
+
* 而本适配器放行的最低版本是 22.5——22.13.0–22.15.x 上 columns 为 undefined,
|
|
104
|
+
* 无条件调用即 TypeError(浏览主路径 query/previewRows 每次必崩)。旧版本回退
|
|
105
|
+
* 到结果首行的 keys(对象字符串键保序 = SELECT 列序;纯数字列名会被当作整数
|
|
106
|
+
* 索引键排前,属降级路径已知盲区)。空结果集无行可推,返回空列头。
|
|
107
|
+
*/
|
|
108
|
+
export function stmtColumns(stmt, raw) {
|
|
109
|
+
if (typeof stmt.columns === 'function')
|
|
110
|
+
return stmt.columns().map((c) => c.name);
|
|
111
|
+
const first = raw[0];
|
|
112
|
+
return first ? Object.keys(first) : [];
|
|
113
|
+
}
|
|
114
|
+
export async function createSqliteAdapter(conn, opts) {
|
|
115
|
+
const readOnly = opts?.mode === 'ro';
|
|
116
|
+
const { db, driver } = await openRaw(conn, readOnly);
|
|
84
117
|
return {
|
|
85
118
|
kind: 'sqlite',
|
|
86
119
|
connId: conn.meta.id,
|
|
@@ -90,9 +123,8 @@ export async function createSqliteAdapter(conn, opts) {
|
|
|
90
123
|
}),
|
|
91
124
|
query: (sql, params) => humanize('sqlite 查询失败', async () => {
|
|
92
125
|
const stmt = db.prepare(sql);
|
|
93
|
-
const columns = stmt.columns().map((c) => c.name);
|
|
94
126
|
const raw = stmt.all(...(params ?? []));
|
|
95
|
-
return toQueryResult(raw,
|
|
127
|
+
return toQueryResult(raw, stmtColumns(stmt, raw));
|
|
96
128
|
}),
|
|
97
129
|
execute: (statement, params) => humanize('sqlite 执行失败', async () => {
|
|
98
130
|
const stmt = db.prepare(statement);
|
|
@@ -139,9 +171,8 @@ export async function createSqliteAdapter(conn, opts) {
|
|
|
139
171
|
const lim = clampLimit(limit);
|
|
140
172
|
const off = clampOffset(offset);
|
|
141
173
|
const stmt = db.prepare(`SELECT * FROM ${tbl} LIMIT ? OFFSET ?`);
|
|
142
|
-
const columns = stmt.columns().map((c) => c.name);
|
|
143
174
|
const raw = stmt.all(lim, off);
|
|
144
|
-
return toQueryResult(raw,
|
|
175
|
+
return toQueryResult(raw, stmtColumns(stmt, raw));
|
|
145
176
|
}),
|
|
146
177
|
close: () => humanize('sqlite 关闭连接', async () => {
|
|
147
178
|
db.close();
|