优化
This commit is contained in:
@@ -57,6 +57,13 @@ function sqlStrings(values) {
|
||||
return values.map((value) => `'${value.replaceAll("'", "''")}'`).join(',')
|
||||
}
|
||||
|
||||
export function parseMetadataRows(output) {
|
||||
return output.trim().split(/\r?\n/).filter(Boolean).map((line) => {
|
||||
if (!/^(?:[0-9a-f]{2})+$/i.test(line)) throw new Error('数据库元数据输出不是有效 HEX JSON')
|
||||
return JSON.parse(Buffer.from(line, 'hex').toString('utf8'))
|
||||
})
|
||||
}
|
||||
|
||||
function runMysql(config, sql) {
|
||||
const executable = process.env.MYSQL_BIN || 'mysql'
|
||||
const result = spawnSync(executable, [
|
||||
@@ -70,14 +77,14 @@ function runMysql(config, sql) {
|
||||
})
|
||||
if (result.error) throw new Error(`${config.label} mysql 客户端执行失败:${result.error.message}`)
|
||||
if (result.status !== 0) throw new Error(`${config.label} 元数据查询失败:${(result.stderr || result.stdout).trim()}`)
|
||||
return result.stdout.trim().split(/\r?\n/).filter(Boolean).map((line) => JSON.parse(line))
|
||||
return parseMetadataRows(result.stdout)
|
||||
}
|
||||
|
||||
function snapshot(config, schemas) {
|
||||
const selected = sqlStrings(schemas)
|
||||
const schemaRows = runMysql(config, `SELECT JSON_OBJECT('schema', SCHEMA_NAME) FROM information_schema.SCHEMATA WHERE SCHEMA_NAME IN (${selected}) ORDER BY SCHEMA_NAME`)
|
||||
const tableRows = runMysql(config, `SELECT JSON_OBJECT('schema', TABLE_SCHEMA, 'table', TABLE_NAME, 'type', TABLE_TYPE, 'engine', COALESCE(ENGINE, ''), 'collation', COALESCE(TABLE_COLLATION, '')) FROM information_schema.TABLES WHERE TABLE_SCHEMA IN (${selected}) ORDER BY TABLE_SCHEMA, TABLE_NAME`)
|
||||
const columnRows = runMysql(config, `SELECT JSON_OBJECT('schema', TABLE_SCHEMA, 'table', TABLE_NAME, 'column', COLUMN_NAME, 'ordinal', ORDINAL_POSITION, 'type', COLUMN_TYPE, 'nullable', IS_NULLABLE, 'defaultValue', COLUMN_DEFAULT, 'extra', EXTRA, 'charset', CHARACTER_SET_NAME, 'collation', COLLATION_NAME, 'generation', GENERATION_EXPRESSION) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA IN (${selected}) ORDER BY TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION`)
|
||||
const schemaRows = runMysql(config, `SELECT HEX(JSON_OBJECT('schema', SCHEMA_NAME)) FROM information_schema.SCHEMATA WHERE SCHEMA_NAME IN (${selected}) ORDER BY SCHEMA_NAME`)
|
||||
const tableRows = runMysql(config, `SELECT HEX(JSON_OBJECT('schema', TABLE_SCHEMA, 'table', TABLE_NAME, 'type', TABLE_TYPE, 'engine', COALESCE(ENGINE, ''), 'collation', COALESCE(TABLE_COLLATION, ''))) FROM information_schema.TABLES WHERE TABLE_SCHEMA IN (${selected}) ORDER BY TABLE_SCHEMA, TABLE_NAME`)
|
||||
const columnRows = runMysql(config, `SELECT HEX(JSON_OBJECT('schema', TABLE_SCHEMA, 'table', TABLE_NAME, 'column', COLUMN_NAME, 'ordinal', ORDINAL_POSITION, 'type', COLUMN_TYPE, 'nullable', IS_NULLABLE, 'defaultValue', COLUMN_DEFAULT, 'extra', EXTRA, 'charset', CHARACTER_SET_NAME, 'collation', COLLATION_NAME, 'generation', GENERATION_EXPRESSION)) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA IN (${selected}) ORDER BY TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION`)
|
||||
return { schemas: schemaRows, tables: tableRows, columns: columnRows }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user