Repository navigation
Replies: 1 comment
|
数据源 charset 是 修复方案:① 数据源 charset 改为
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
我在nodejs 中使用以下代码中文正常
function fbConnect(dsConfig) {
const Firebird = loadDriver('firebird');
const options = {
host: dsConfig.host,
port: Number(dsConfig.port) || 3050,
database: dsConfig.database,
user: dsConfig.username,
password: dsConfig.password,
lowercase_keys: true,
encoding: 'UTF8',
pageSize: 4096,
};
return new Promise((resolve, reject) => {
Firebird.attach(options, (err, db) => {
if (err) return reject(err);
// 记录数据字符集:Firebird charset NONE 列存原始字节,按 dsConfig.charset(如 gbk)在 JS 层解码
db._tfDataCharset = String(dsConfig.charset || '').toLowerCase();
resolve(db);
});
});
}
// Firebird 多字节字符集(charset NONE 列存 GBK/GB2312/GB18030/BIG5 原始字节)解码映射
const FB_MULTIBYTE_DECODERS = { gbk: 1, gb2312: 1, gb18030: 1, '936': 1, big5: 1, '950': 1, euckr: 1, '949': 1, sjis: 1, shift_jis: 1, '932': 1 };
function fbQuery(db, sql, params) {
return new Promise((resolve, reject) => {
db.query(sql, params || [], (err, result) => {
if (err) return reject(err);
const dc = db._tfDataCharset;
if (dc && FB_MULTIBYTE_DECODERS[dc] && Array.isArray(result)) {
let td = null;
try { td = new TextDecoder(dc); } catch (e) { td = null; }
if (td) {
for (const row of result) {
if (!row || typeof row !== 'object') continue;
for (const k in row) {
const v = row[k];
if (typeof v !== 'string' || v.length === 0) continue;
let hasHigh = false;
for (let i = 0; i < v.length; i++) { if (v.charCodeAt(i) > 127) { hasHigh = true; break; } }
if (!hasHigh) continue;
try { row[k] = td.decode(Buffer.from(v, 'latin1')); } catch (e) { /* 解码失败保留原值 */ }
}
}
}
}
resolve(result);
});
});
}
All reactions