테스트테이블 생성 및 오류 수정

This commit is contained in:
kjs
2025-09-19 02:15:21 +09:00
parent ddcecfd5e2
commit f7d884568b
20 changed files with 1024 additions and 180 deletions

View File

@@ -87,6 +87,48 @@ export class DynamicFormService {
return Boolean(value);
}
// 날짜/시간 타입 처리
if (
lowerDataType.includes("date") ||
lowerDataType.includes("timestamp") ||
lowerDataType.includes("time")
) {
if (typeof value === "string") {
// 빈 문자열이면 null 반환
if (value.trim() === "") {
return null;
}
try {
// YYYY-MM-DD 형식인 경우 시간 추가해서 Date 객체 생성
if (/^\d{4}-\d{2}-\d{2}$/.test(value)) {
console.log(`📅 날짜 타입 변환: ${value} -> Date 객체`);
return new Date(value + "T00:00:00");
}
// 다른 날짜 형식도 Date 객체로 변환
else {
console.log(`📅 날짜 타입 변환: ${value} -> Date 객체`);
return new Date(value);
}
} catch (error) {
console.error(`❌ 날짜 변환 실패: ${value}`, error);
return null;
}
}
// 이미 Date 객체인 경우 그대로 반환
if (value instanceof Date) {
return value;
}
// 숫자인 경우 timestamp로 처리
if (typeof value === "number") {
return new Date(value);
}
return null;
}
// 기본적으로 문자열로 반환
return value;
}
@@ -479,7 +521,7 @@ export class DynamicFormService {
const updateQuery = `
UPDATE ${tableName}
SET ${setClause}
WHERE ${primaryKeyColumn} = $${values.length}
WHERE ${primaryKeyColumn} = $${values.length}::text
RETURNING *
`;
@@ -552,6 +594,31 @@ export class DynamicFormService {
}
});
// 컬럼 타입에 맞는 데이터 변환 (UPDATE용)
const columnInfo = await this.getTableColumnInfo(tableName);
console.log(`📊 테이블 ${tableName}의 컬럼 타입 정보:`, columnInfo);
// 각 컬럼의 타입에 맞게 데이터 변환
Object.keys(dataToUpdate).forEach((columnName) => {
const column = columnInfo.find((col) => col.column_name === columnName);
if (column) {
const originalValue = dataToUpdate[columnName];
const convertedValue = this.convertValueForPostgreSQL(
originalValue,
column.data_type
);
if (originalValue !== convertedValue) {
console.log(
`🔄 UPDATE 타입 변환: ${columnName} (${column.data_type}) = "${originalValue}" -> ${convertedValue}`
);
dataToUpdate[columnName] = convertedValue;
}
}
});
console.log("✅ UPDATE 타입 변환 완료된 데이터:", dataToUpdate);
console.log("🎯 실제 테이블에서 업데이트할 데이터:", {
tableName,
id,
@@ -650,12 +717,15 @@ export class DynamicFormService {
tableName,
});
// 1. 먼저 테이블의 기본키 컬럼명을 동적으로 조회
// 1. 먼저 테이블의 기본키 컬럼명과 데이터 타입을 동적으로 조회
const primaryKeyQuery = `
SELECT kcu.column_name
SELECT kcu.column_name, c.data_type
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.columns c
ON kcu.column_name = c.column_name
AND kcu.table_name = c.table_name
WHERE tc.table_name = $1
AND tc.constraint_type = 'PRIMARY KEY'
LIMIT 1
@@ -677,13 +747,37 @@ export class DynamicFormService {
throw new Error(`테이블 ${tableName}의 기본키를 찾을 수 없습니다.`);
}
const primaryKeyColumn = (primaryKeyResult[0] as any).column_name;
console.log("🔑 발견된 기본키 컬럼:", primaryKeyColumn);
const primaryKeyInfo = primaryKeyResult[0] as any;
const primaryKeyColumn = primaryKeyInfo.column_name;
const primaryKeyDataType = primaryKeyInfo.data_type;
console.log("🔑 발견된 기본키:", {
column: primaryKeyColumn,
dataType: primaryKeyDataType,
});
// 2. 동적으로 발견된 기본키를 사용한 DELETE SQL 생성
// 2. 데이터 타입에 맞는 타입 캐스팅 적용
let typeCastSuffix = "";
if (
primaryKeyDataType.includes("character") ||
primaryKeyDataType.includes("text")
) {
typeCastSuffix = "::text";
} else if (
primaryKeyDataType.includes("integer") ||
primaryKeyDataType.includes("bigint")
) {
typeCastSuffix = "::bigint";
} else if (
primaryKeyDataType.includes("numeric") ||
primaryKeyDataType.includes("decimal")
) {
typeCastSuffix = "::numeric";
}
// 3. 동적으로 발견된 기본키와 타입 캐스팅을 사용한 DELETE SQL 생성
const deleteQuery = `
DELETE FROM ${tableName}
WHERE ${primaryKeyColumn} = $1
WHERE ${primaryKeyColumn} = $1${typeCastSuffix}
RETURNING *
`;