refactor(db): use SqlDialect in MaterialsTypeToBeDeletedDAO
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { create, type IDatabaseService } from './index'
|
import { create, type IDatabaseService } from './index'
|
||||||
|
import { createDialect, type SqlDialect } from './dialects'
|
||||||
import { createLogger, run, getRequestId, trackDuration } from '../logger'
|
import { createLogger, run, getRequestId, trackDuration } from '../logger'
|
||||||
|
|
||||||
const log = createLogger('MaterialsTypeToBeDeletedDAO')
|
const log = createLogger('MaterialsTypeToBeDeletedDAO')
|
||||||
@@ -32,8 +33,6 @@ export interface MaterialTypeBatchRequest {
|
|||||||
* Configuration for MaterialsTypeToBeDeleted table
|
* Configuration for MaterialsTypeToBeDeleted table
|
||||||
*/
|
*/
|
||||||
export const MATERIALS_TYPE_TO_BE_DELETED_CONFIG = {
|
export const MATERIALS_TYPE_TO_BE_DELETED_CONFIG = {
|
||||||
TABLE_NAME_SQLSERVER: '[dbo].[MaterialsTypeToBeDeleted]',
|
|
||||||
TABLE_NAME_MYSQL: 'dbo_MaterialsTypeToBeDeleted',
|
|
||||||
COLUMNS: {
|
COLUMNS: {
|
||||||
ID: 'ID',
|
ID: 'ID',
|
||||||
MATERIAL_NAME: 'MaterialName',
|
MATERIAL_NAME: 'MaterialName',
|
||||||
@@ -46,15 +45,20 @@ export const MATERIALS_TYPE_TO_BE_DELETED_CONFIG = {
|
|||||||
*/
|
*/
|
||||||
export class MaterialsTypeToBeDeletedDAO {
|
export class MaterialsTypeToBeDeletedDAO {
|
||||||
private dbService: IDatabaseService | null = null
|
private dbService: IDatabaseService | null = null
|
||||||
|
private dialect: SqlDialect | null = null
|
||||||
|
|
||||||
|
private getDialect(): SqlDialect {
|
||||||
|
if (!this.dialect) {
|
||||||
|
this.dialect = createDialect(this.dbService!.type)
|
||||||
|
}
|
||||||
|
return this.dialect
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the appropriate table name based on database type
|
* Get the appropriate table name based on database type
|
||||||
*/
|
*/
|
||||||
private getTableName(): string {
|
private getTableName(): string {
|
||||||
const isSqlServer = this.dbService?.type === 'sqlserver'
|
return this.getDialect().quoteTableName('dbo', 'MaterialsTypeToBeDeleted')
|
||||||
return isSqlServer
|
|
||||||
? MATERIALS_TYPE_TO_BE_DELETED_CONFIG.TABLE_NAME_SQLSERVER
|
|
||||||
: MATERIALS_TYPE_TO_BE_DELETED_CONFIG.TABLE_NAME_MYSQL
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,9 +120,9 @@ export class MaterialsTypeToBeDeletedDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
const placeholder = dialect.param(0)
|
||||||
const sqlString = `
|
const sqlString = `
|
||||||
SELECT ID, MaterialName, ManagerName
|
SELECT ID, MaterialName, ManagerName
|
||||||
FROM ${tableName}
|
FROM ${tableName}
|
||||||
@@ -204,33 +208,19 @@ export class MaterialsTypeToBeDeletedDAO {
|
|||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const name = materialName.trim()
|
const name = materialName.trim()
|
||||||
const manager = managerName?.trim() || null
|
const manager = managerName?.trim() || null
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (isSqlServer) {
|
const { sql: sqlString } = dialect.upsert({
|
||||||
const sqlString = `
|
table: tableName,
|
||||||
MERGE ${tableName} AS target
|
keyColumns: ['MaterialName'],
|
||||||
USING (VALUES (@p0, @p1)) AS source (MaterialName, ManagerName)
|
allColumns: ['MaterialName', 'ManagerName'],
|
||||||
ON target.MaterialName = source.MaterialName
|
startParamIndex: 0
|
||||||
WHEN MATCHED THEN UPDATE SET ManagerName = source.ManagerName
|
})
|
||||||
WHEN NOT MATCHED THEN INSERT (MaterialName, ManagerName) VALUES (source.MaterialName, source.ManagerName);
|
|
||||||
`
|
|
||||||
|
|
||||||
await trackDuration(async () => await dbService.query(sqlString, [name, manager]), {
|
await trackDuration(async () => await dbService.query(sqlString, [name, manager]), {
|
||||||
operationName: 'MaterialsTypeToBeDeletedDAO.upsertMaterial',
|
operationName: 'MaterialsTypeToBeDeletedDAO.upsertMaterial',
|
||||||
context: { tableName, operationType: 'MERGE' }
|
context: { tableName, operationType: 'UPSERT' }
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
INSERT INTO ${tableName} (MaterialName, ManagerName)
|
|
||||||
VALUES (?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE ManagerName = VALUES(ManagerName)
|
|
||||||
`
|
|
||||||
|
|
||||||
await trackDuration(async () => await dbService.query(sqlString, [name, manager]), {
|
|
||||||
operationName: 'MaterialsTypeToBeDeletedDAO.upsertMaterial',
|
|
||||||
context: { tableName, operationType: 'INSERT' }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -257,25 +247,16 @@ export class MaterialsTypeToBeDeletedDAO {
|
|||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const name = materialName.trim()
|
const name = materialName.trim()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
let sqlString: string
|
let sqlString: string
|
||||||
let params: (string | null)[]
|
let params: (string | null)[]
|
||||||
|
|
||||||
if (managerName) {
|
if (managerName) {
|
||||||
const placeholder1 = isSqlServer ? '@p0' : '?'
|
sqlString = `DELETE FROM ${tableName} WHERE MaterialName = ${dialect.param(0)} AND ManagerName = ${dialect.param(1)}`
|
||||||
const placeholder2 = isSqlServer ? '@p1' : '?'
|
|
||||||
sqlString = `
|
|
||||||
DELETE FROM ${tableName}
|
|
||||||
WHERE MaterialName = ${placeholder1} AND ManagerName = ${placeholder2}
|
|
||||||
`
|
|
||||||
params = [name, managerName.trim()]
|
params = [name, managerName.trim()]
|
||||||
} else {
|
} else {
|
||||||
const placeholder = isSqlServer ? '@p0' : '?'
|
sqlString = `DELETE FROM ${tableName} WHERE MaterialName = ${dialect.param(0)}`
|
||||||
sqlString = `
|
|
||||||
DELETE FROM ${tableName}
|
|
||||||
WHERE MaterialName = ${placeholder}
|
|
||||||
`
|
|
||||||
params = [name]
|
params = [name]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,49 +295,27 @@ export class MaterialsTypeToBeDeletedDAO {
|
|||||||
try {
|
try {
|
||||||
const dbService = await this.getDatabaseService()
|
const dbService = await this.getDatabaseService()
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
const isSqlServer = dbService.type === 'sqlserver'
|
const dialect = this.getDialect()
|
||||||
|
|
||||||
if (isSqlServer) {
|
const sqlString = `
|
||||||
const sqlString = `
|
UPDATE ${tableName}
|
||||||
UPDATE ${tableName}
|
SET MaterialName = ${dialect.param(0)}, ManagerName = ${dialect.param(1)}
|
||||||
SET MaterialName = @p0, ManagerName = @p1
|
WHERE MaterialName = ${dialect.param(2)} AND ManagerName = ${dialect.param(3)}
|
||||||
WHERE MaterialName = @p2 AND ManagerName = @p3
|
`
|
||||||
`
|
const result = await trackDuration(
|
||||||
const result = await trackDuration(
|
async () =>
|
||||||
async () =>
|
await dbService.query(sqlString, [
|
||||||
await dbService.query(sqlString, [
|
newName.trim(),
|
||||||
newName.trim(),
|
newManager.trim(),
|
||||||
newManager.trim(),
|
oldName.trim(),
|
||||||
oldName.trim(),
|
oldManager.trim()
|
||||||
oldManager.trim()
|
]),
|
||||||
]),
|
{
|
||||||
{
|
operationName: 'MaterialsTypeToBeDeletedDAO.updateMaterial',
|
||||||
operationName: 'MaterialsTypeToBeDeletedDAO.updateMaterial',
|
context: { tableName, operationType: 'UPDATE' }
|
||||||
context: { tableName, operationType: 'UPDATE' }
|
}
|
||||||
}
|
)
|
||||||
)
|
return result.result.rowCount > 0
|
||||||
return result.result.rowCount > 0
|
|
||||||
} else {
|
|
||||||
const sqlString = `
|
|
||||||
UPDATE ${tableName}
|
|
||||||
SET MaterialName = ?, ManagerName = ?
|
|
||||||
WHERE MaterialName = ? AND ManagerName = ?
|
|
||||||
`
|
|
||||||
const result = await trackDuration(
|
|
||||||
async () =>
|
|
||||||
await dbService.query(sqlString, [
|
|
||||||
newName.trim(),
|
|
||||||
newManager.trim(),
|
|
||||||
oldName.trim(),
|
|
||||||
oldManager.trim()
|
|
||||||
]),
|
|
||||||
{
|
|
||||||
operationName: 'MaterialsTypeToBeDeletedDAO.updateMaterial',
|
|
||||||
context: { tableName, operationType: 'UPDATE' }
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return result.result.rowCount > 0
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error('Update material error', {
|
log.error('Update material error', {
|
||||||
tableName: this.getTableName(),
|
tableName: this.getTableName(),
|
||||||
@@ -456,6 +415,7 @@ export class MaterialsTypeToBeDeletedDAO {
|
|||||||
if (this.dbService) {
|
if (this.dbService) {
|
||||||
await this.dbService.disconnect()
|
await this.dbService.disconnect()
|
||||||
this.dbService = null
|
this.dbService = null
|
||||||
|
this.dialect = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user