Files
jurong_circle_black/scripts/fix_table_aliases.js
2025-08-26 10:06:23 +08:00

133 lines
3.7 KiB
JavaScript

const fs = require('fs');
const path = require('path');
/**
* 表别名修复脚本:修复 SQL 查询中的表别名问题
*/
class TableAliasFixer {
constructor() {
this.filesToFix = [
'services/matchingService.js',
'routes/matchingAdmin.js',
'routes/transfers.js',
'routes/matching.js'
];
}
/**
* 修复单个文件中的表别名问题
* @param {string} filePath - 文件路径
*/
async fixFile(filePath) {
const fullPath = path.join(process.cwd(), filePath);
if (!fs.existsSync(fullPath)) {
console.log(`文件不存在: ${filePath}`);
return;
}
console.log(`正在修复文件: ${filePath}`);
let content = fs.readFileSync(fullPath, 'utf8');
let originalContent = content;
// 1. 修复 "FROM transfers WHERE source_type = 'allocation' oa" 的问题
content = content.replace(
/FROM transfers WHERE source_type = 'allocation' (\w+)/g,
"FROM transfers $1 WHERE $1.source_type = 'allocation'"
);
// 2. 修复重复的 source_type 条件
content = content.replace(
/FROM transfers WHERE source_type = 'allocation' (\w+) AND \1\.source_type = 'allocation'/g,
"FROM transfers $1 WHERE $1.source_type = 'allocation'"
);
// 3. 修复 "FROM transfers WHERE source_type = 'allocation'" 后面直接跟其他子句的情况
content = content.replace(
/FROM transfers WHERE source_type = 'allocation'\s+(JOIN|ORDER|GROUP|LIMIT)/g,
"FROM transfers WHERE source_type = 'allocation' $1"
);
// 4. 修复子查询中的问题
content = content.replace(
/\(SELECT[^)]*FROM transfers WHERE source_type = 'allocation' (\w+)/g,
(match, alias) => {
return match.replace(
`FROM transfers WHERE source_type = 'allocation' ${alias}`,
`FROM transfers ${alias} WHERE ${alias}.source_type = 'allocation'`
);
}
);
// 5. 修复特定的查询模式
content = this.fixSpecificPatterns(content, filePath);
if (content !== originalContent) {
fs.writeFileSync(fullPath, content);
console.log(`✓ 已修复: ${filePath}`);
} else {
console.log(`- 无需修复: ${filePath}`);
}
}
/**
* 修复特定的查询模式
* @param {string} content - 文件内容
* @param {string} filePath - 文件路径
* @returns {string} 修复后的内容
*/
fixSpecificPatterns(content, filePath) {
// 修复 SELECT 语句中的表别名问题
content = content.replace(
/SELECT ([^F]*?) FROM transfers WHERE source_type = 'allocation' (\w+)/g,
"SELECT $1 FROM transfers $2 WHERE $2.source_type = 'allocation'"
);
// 修复 UPDATE 语句
content = content.replace(
/UPDATE transfers WHERE source_type = 'allocation' SET/g,
"UPDATE transfers SET"
);
// 修复 WHERE 子句中的条件
content = content.replace(
/WHERE source_type = 'allocation' AND (\w+)\./g,
"WHERE $1.source_type = 'allocation' AND $1."
);
return content;
}
/**
* 执行所有文件的修复
*/
async fixAllFiles() {
console.log('开始修复表别名问题...');
console.log('=' .repeat(60));
for (const filePath of this.filesToFix) {
try {
await this.fixFile(filePath);
} catch (error) {
console.error(`修复文件 ${filePath} 失败:`, error.message);
}
}
console.log('\n' + '=' .repeat(60));
console.log('✓ 表别名修复完成!');
}
}
async function main() {
const fixer = new TableAliasFixer();
await fixer.fixAllFiles();
}
// 如果直接运行此脚本
if (require.main === module) {
main().catch(console.error);
}
module.exports = TableAliasFixer;