fix(db): complete PostgreSQL integration in validation and cleaner services
OrderNumberResolver, validation, and cleaner services had incomplete PostgreSQL support - they only handled SQL Server and MySQL, causing PostgreSQL to fall through to MySQL code paths with invalid syntax (backticks, ? placeholders) and missing schema.table name splitting. Changes: - Add PostgreSQL SQL generation ($N params, double-quoted identifiers) in OrderNumberResolver, validation-application-service, production-input-service, and validation-database - Add PostgreSQL to database factory functions in validation-database and cleaner-application-service - Add UPPER, LOWER, and 40+ common SQL functions to SQL_KEYWORDS to prevent prepareSql() from quoting them as identifiers Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -29,7 +29,7 @@ export async function getSourceNumbersFromInputs(
|
||||
const productionIds: string[] = []
|
||||
const orderNumbers: string[] = []
|
||||
const configManager = ConfigManager.getInstance()
|
||||
const isSqlServer = configManager.getDatabaseType() === 'sqlserver'
|
||||
const dbType = configManager.getDatabaseType()
|
||||
|
||||
for (const item of inputs) {
|
||||
const type = identifyInputType(item)
|
||||
@@ -44,7 +44,7 @@ export async function getSourceNumbersFromInputs(
|
||||
const contractTableName = getValidationTableName('productionContractData_26年压力表合同数据')
|
||||
const batchSize = 2000
|
||||
|
||||
if (isSqlServer) {
|
||||
if (dbType === 'sqlserver') {
|
||||
const sql = await import('mssql')
|
||||
const allOrderNumbers: string[] = []
|
||||
|
||||
@@ -71,6 +71,22 @@ export async function getSourceNumbersFromInputs(
|
||||
)
|
||||
}
|
||||
|
||||
orderNumbers.push(...allOrderNumbers)
|
||||
} else if (dbType === 'postgresql') {
|
||||
const allOrderNumbers: string[] = []
|
||||
|
||||
for (let i = 0; i < productionIds.length; i += batchSize) {
|
||||
const batch = productionIds.slice(i, i + batchSize)
|
||||
const placeholders = batch.map((_, idx) => `$${idx + 1}`).join(',')
|
||||
const contractSql = `
|
||||
SELECT DISTINCT "生产订单号"
|
||||
FROM ${contractTableName}
|
||||
WHERE "总排号" IN (${placeholders})
|
||||
`
|
||||
const contractResult = await dbService.query(contractSql, batch)
|
||||
allOrderNumbers.push(...contractResult.rows.map((row) => row.生产订单号 as string))
|
||||
}
|
||||
|
||||
orderNumbers.push(...allOrderNumbers)
|
||||
} else {
|
||||
const allOrderNumbers: string[] = []
|
||||
|
||||
@@ -463,6 +463,18 @@ export class ValidationApplicationService {
|
||||
)
|
||||
}
|
||||
|
||||
if (dbService.type === 'postgresql') {
|
||||
return dbService.query(
|
||||
`
|
||||
SELECT "MaterialName", "Specification", "Model"
|
||||
FROM ${detailTableName}
|
||||
WHERE "MaterialCode" = $1
|
||||
LIMIT 1
|
||||
`,
|
||||
[materialCode]
|
||||
)
|
||||
}
|
||||
|
||||
return dbService.query(
|
||||
`
|
||||
SELECT MaterialName, Specification, Model
|
||||
@@ -521,6 +533,24 @@ export class ValidationApplicationService {
|
||||
return materialCodes
|
||||
}
|
||||
|
||||
if (dbService.type === 'postgresql') {
|
||||
const result = await dbService.query(
|
||||
`
|
||||
SELECT "MaterialCode"
|
||||
FROM ${markedTableName}
|
||||
WHERE "ManagerName" = $1 AND "MaterialCode" IS NOT NULL
|
||||
`,
|
||||
[username]
|
||||
)
|
||||
const materialCodes = result.rows.map((row) => row.MaterialCode as string).filter(Boolean)
|
||||
log.info(`Regular user: got ${materialCodes.length} materials`, {
|
||||
userId: username,
|
||||
isAdmin: false,
|
||||
materialCount: materialCodes.length
|
||||
})
|
||||
return materialCodes
|
||||
}
|
||||
|
||||
const result = await dbService.query(
|
||||
`
|
||||
SELECT MaterialCode
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { ConfigManager } from '../config/config-manager'
|
||||
import { MySqlService } from '../database/mysql'
|
||||
import { SqlServerService } from '../database/sql-server'
|
||||
import { PostgreSqlService } from '../database/postgresql'
|
||||
|
||||
export type ValidationDatabaseService = MySqlService | SqlServerService
|
||||
export type ValidationDatabaseService = MySqlService | SqlServerService | PostgreSqlService
|
||||
|
||||
export async function createValidationDatabaseService(): Promise<ValidationDatabaseService> {
|
||||
const configManager = ConfigManager.getInstance()
|
||||
@@ -26,6 +27,19 @@ export async function createValidationDatabaseService(): Promise<ValidationDatab
|
||||
return sqlServerService
|
||||
}
|
||||
|
||||
if (dbType === 'postgresql') {
|
||||
const dbConfig = config.database.postgresql
|
||||
const pgService = new PostgreSqlService({
|
||||
host: dbConfig.host,
|
||||
port: dbConfig.port,
|
||||
user: dbConfig.username,
|
||||
password: dbConfig.password,
|
||||
database: dbConfig.database
|
||||
})
|
||||
await pgService.connect()
|
||||
return pgService
|
||||
}
|
||||
|
||||
const dbConfig = config.database.mysql
|
||||
const mysqlService = new MySqlService({
|
||||
host: dbConfig.host,
|
||||
@@ -42,14 +56,20 @@ export function getValidationTableName(mysqlTableName: string): string {
|
||||
const configManager = ConfigManager.getInstance()
|
||||
const dbType = configManager.getDatabaseType()
|
||||
|
||||
if (dbType === 'sqlserver') {
|
||||
if (dbType === 'sqlserver' || dbType === 'postgresql') {
|
||||
const firstUnderscoreIndex = mysqlTableName.indexOf('_')
|
||||
if (firstUnderscoreIndex > 0) {
|
||||
const schema = mysqlTableName.substring(0, firstUnderscoreIndex)
|
||||
const tableName = mysqlTableName.substring(firstUnderscoreIndex + 1)
|
||||
return `[${schema}].[${tableName}]`
|
||||
if (dbType === 'sqlserver') {
|
||||
return `[${schema}].[${tableName}]`
|
||||
}
|
||||
return `"${schema}"."${tableName}"`
|
||||
}
|
||||
return `[dbo].[${mysqlTableName}]`
|
||||
if (dbType === 'sqlserver') {
|
||||
return `[dbo].[${mysqlTableName}]`
|
||||
}
|
||||
return `"public"."${mysqlTableName}"`
|
||||
}
|
||||
|
||||
return mysqlTableName
|
||||
|
||||
Reference in New Issue
Block a user