제어관리 외부 커넥션 설정기능

This commit is contained in:
kjs
2025-09-24 18:23:57 +09:00
parent 0d9ee4c40f
commit b41e645c74
25 changed files with 6775 additions and 209 deletions

View File

@@ -31,6 +31,7 @@ import layoutRoutes from "./routes/layoutRoutes";
import dataRoutes from "./routes/dataRoutes";
import testButtonDataflowRoutes from "./routes/testButtonDataflowRoutes";
import externalDbConnectionRoutes from "./routes/externalDbConnectionRoutes";
import multiConnectionRoutes from "./routes/multiConnectionRoutes";
import dbTypeCategoryRoutes from "./routes/dbTypeCategoryRoutes";
import ddlRoutes from "./routes/ddlRoutes";
import entityReferenceRoutes from "./routes/entityReferenceRoutes";
@@ -130,6 +131,7 @@ app.use("/api/screen", screenStandardRoutes);
app.use("/api/data", dataRoutes);
app.use("/api/test-button-dataflow", testButtonDataflowRoutes);
app.use("/api/external-db-connections", externalDbConnectionRoutes);
app.use("/api/multi-connection", multiConnectionRoutes);
app.use("/api/db-type-categories", dbTypeCategoryRoutes);
app.use("/api/ddl", ddlRoutes);
app.use("/api/entity-reference", entityReferenceRoutes);

View File

@@ -1,6 +1,10 @@
import { Client } from 'pg';
import { DatabaseConnector, ConnectionConfig, QueryResult } from '../interfaces/DatabaseConnector';
import { ConnectionTestResult, TableInfo } from '../types/externalDbTypes';
import { Client } from "pg";
import {
DatabaseConnector,
ConnectionConfig,
QueryResult,
} from "../interfaces/DatabaseConnector";
import { ConnectionTestResult, TableInfo } from "../types/externalDbTypes";
export class PostgreSQLConnector implements DatabaseConnector {
private client: Client | null = null;
@@ -11,37 +15,72 @@ export class PostgreSQLConnector implements DatabaseConnector {
}
async connect(): Promise<void> {
if (this.client) {
await this.disconnect();
}
// 기존 연결이 있다면 먼저 정리
await this.forceDisconnect();
const clientConfig: any = {
host: this.config.host,
port: this.config.port,
database: this.config.database,
user: this.config.user,
password: this.config.password,
// 연결 안정성 개선 (더 보수적인 설정)
connectionTimeoutMillis: this.config.connectionTimeoutMillis || 15000,
query_timeout: this.config.queryTimeoutMillis || 20000,
keepAlive: false, // keepAlive 비활성화 (연결 문제 방지)
// SASL 인증 문제 방지
application_name: "PLM-ERP-System",
// 추가 안정성 설정
statement_timeout: 20000,
idle_in_transaction_session_timeout: 30000,
};
if (this.config.connectionTimeoutMillis != null) {
clientConfig.connectionTimeoutMillis = this.config.connectionTimeoutMillis;
}
if (this.config.queryTimeoutMillis != null) {
clientConfig.query_timeout = this.config.queryTimeoutMillis;
}
if (this.config.ssl != null) {
clientConfig.ssl = this.config.ssl;
}
this.client = new Client(clientConfig);
await this.client.connect();
// 연결 시 더 긴 타임아웃 설정
const connectPromise = this.client.connect();
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error("연결 타임아웃")), 20000);
});
await Promise.race([connectPromise, timeoutPromise]);
console.log(
`✅ PostgreSQL 연결 성공: ${this.config.host}:${this.config.port}`
);
}
// 강제 연결 해제 메서드 추가
private async forceDisconnect(): Promise<void> {
if (this.client) {
try {
await this.client.end();
} catch (error) {
console.warn("강제 연결 해제 중 오류 (무시):", error);
} finally {
this.client = null;
}
}
}
async disconnect(): Promise<void> {
if (this.client) {
await this.client.end();
this.client = null;
try {
const endPromise = this.client.end();
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error("연결 해제 타임아웃")), 3000);
});
await Promise.race([endPromise, timeoutPromise]);
console.log(`✅ PostgreSQL 연결 해제 성공`);
} catch (error) {
console.warn("연결 해제 중 오류:", error);
} finally {
this.client = null;
}
}
}
@@ -49,7 +88,9 @@ export class PostgreSQLConnector implements DatabaseConnector {
const startTime = Date.now();
try {
await this.connect();
const result = await this.client!.query("SELECT version(), pg_database_size(current_database()) as size");
const result = await this.client!.query(
"SELECT version(), pg_database_size(current_database()) as size"
);
const responseTime = Date.now() - startTime;
await this.disconnect();
return {
@@ -58,7 +99,9 @@ export class PostgreSQLConnector implements DatabaseConnector {
details: {
response_time: responseTime,
server_version: result.rows[0]?.version || "알 수 없음",
database_size: this.formatBytes(parseInt(result.rows[0]?.size || "0")),
database_size: this.formatBytes(
parseInt(result.rows[0]?.size || "0")
),
},
};
} catch (error: any) {
@@ -91,9 +134,28 @@ export class PostgreSQLConnector implements DatabaseConnector {
}
async getTables(): Promise<TableInfo[]> {
let tempClient: Client | null = null;
try {
await this.connect();
const result = await this.client!.query(`
console.log(
`🔍 PostgreSQL 테이블 목록 조회 시작: ${this.config.host}:${this.config.port}`
);
// 매번 새로운 연결 생성
const clientConfig: any = {
host: this.config.host,
port: this.config.port,
database: this.config.database,
user: this.config.user,
password: this.config.password,
connectionTimeoutMillis: 10000,
query_timeout: 15000,
application_name: "PLM-ERP-Tables",
};
tempClient = new Client(clientConfig);
await tempClient.connect();
const result = await tempClient.query(`
SELECT
t.table_name,
obj_description(quote_ident(t.table_name)::regclass::oid, 'pg_class') as table_description
@@ -102,36 +164,81 @@ export class PostgreSQLConnector implements DatabaseConnector {
AND t.table_type = 'BASE TABLE'
ORDER BY t.table_name;
`);
await this.disconnect();
console.log(`✅ 테이블 목록 조회 성공: ${result.rows.length}`);
return result.rows.map((row) => ({
table_name: row.table_name,
description: row.table_description,
columns: [], // Columns will be fetched by getColumns
}));
} catch (error: any) {
await this.disconnect();
console.error(`❌ 테이블 목록 조회 실패:`, error.message);
throw new Error(`PostgreSQL 테이블 목록 조회 실패: ${error.message}`);
} finally {
if (tempClient) {
try {
await tempClient.end();
} catch (endError) {
console.warn("테이블 조회 연결 해제 중 오류:", endError);
}
}
}
}
async getColumns(tableName: string): Promise<any[]> {
let tempClient: Client | null = null;
try {
await this.connect();
const result = await this.client!.query(`
console.log(
`🔍 PostgreSQL 컬럼 정보 조회 시작: ${this.config.host}:${this.config.port}/${tableName}`
);
// 매번 새로운 연결 생성
const clientConfig: any = {
host: this.config.host,
port: this.config.port,
database: this.config.database,
user: this.config.user,
password: this.config.password,
connectionTimeoutMillis: 10000,
query_timeout: 15000,
application_name: "PLM-ERP-Columns",
};
tempClient = new Client(clientConfig);
await tempClient.connect();
const result = await tempClient.query(
`
SELECT
column_name,
data_type,
is_nullable,
column_default
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = $1
ORDER BY ordinal_position;
`, [tableName]);
await this.disconnect();
column_default,
col_description(c.oid, a.attnum) as column_comment
FROM information_schema.columns isc
LEFT JOIN pg_class c ON c.relname = isc.table_name
LEFT JOIN pg_attribute a ON a.attrelid = c.oid AND a.attname = isc.column_name
WHERE isc.table_schema = 'public' AND isc.table_name = $1
ORDER BY isc.ordinal_position;
`,
[tableName]
);
console.log(
`✅ 컬럼 정보 조회 성공: ${tableName} - ${result.rows.length}`
);
return result.rows;
} catch (error: any) {
await this.disconnect();
console.error(`❌ 컬럼 정보 조회 실패: ${tableName} -`, error.message);
throw new Error(`PostgreSQL 컬럼 정보 조회 실패: ${error.message}`);
} finally {
if (tempClient) {
try {
await tempClient.end();
} catch (endError) {
console.warn("컬럼 조회 연결 해제 중 오류:", endError);
}
}
}
}
@@ -142,4 +249,4 @@ export class PostgreSQLConnector implements DatabaseConnector {
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
}
}
}

View File

@@ -108,7 +108,8 @@ router.get(
}
});
const result = await ExternalDbConnectionService.getConnectionsGroupedByType(filter);
const result =
await ExternalDbConnectionService.getConnectionsGroupedByType(filter);
if (result.success) {
return res.status(200).json(result);
@@ -120,7 +121,7 @@ router.get(
return res.status(500).json({
success: false,
message: "그룹화된 연결 목록 조회 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류"
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
@@ -290,7 +291,7 @@ router.post(
async (req: AuthenticatedRequest, res: Response) => {
try {
const id = parseInt(req.params.id);
if (isNaN(id)) {
return res.status(400).json({
success: false,
@@ -303,10 +304,17 @@ router.post(
}
// 테스트용 비밀번호가 제공된 경우 사용
const testData = req.body.password ? { password: req.body.password } : undefined;
console.log(`🔍 [API] 연결테스트 요청 - ID: ${id}, 비밀번호 제공됨: ${!!req.body.password}`);
const result = await ExternalDbConnectionService.testConnectionById(id, testData);
const testData = req.body.password
? { password: req.body.password }
: undefined;
console.log(
`🔍 [API] 연결테스트 요청 - ID: ${id}, 비밀번호 제공됨: ${!!req.body.password}`
);
const result = await ExternalDbConnectionService.testConnectionById(
id,
testData
);
return res.status(200).json({
success: result.success,
@@ -342,7 +350,7 @@ router.post(
if (!query?.trim()) {
return res.status(400).json({
success: false,
message: "쿼리가 입력되지 않았습니다."
message: "쿼리가 입력되지 않았습니다.",
});
}
@@ -353,7 +361,7 @@ router.post(
return res.status(500).json({
success: false,
message: "쿼리 실행 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류"
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
@@ -376,7 +384,7 @@ router.get(
return res.status(500).json({
success: false,
message: "테이블 목록 조회 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류"
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
@@ -393,26 +401,106 @@ router.get(
try {
const id = parseInt(req.params.id);
const tableName = req.params.tableName;
if (!tableName) {
return res.status(400).json({
success: false,
message: "테이블명이 입력되지 않았습니다."
message: "테이블명이 입력되지 않았습니다.",
});
}
const result = await ExternalDbConnectionService.getTableColumns(id, tableName);
const result = await ExternalDbConnectionService.getTableColumns(
id,
tableName
);
return res.json(result);
} catch (error) {
console.error("테이블 컬럼 조회 오류:", error);
return res.status(500).json({
success: false,
message: "테이블 컬럼 조회 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류"
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
);
/**
* 🆕 GET /api/external-db-connections/active
* 제어관리용 활성 커넥션 목록 조회 (현재 DB 포함)
*/
router.get(
"/control/active",
authenticateToken,
async (req: AuthenticatedRequest, res: Response) => {
try {
// 활성 상태의 외부 커넥션 조회
const filter: ExternalDbConnectionFilter = {
is_active: "Y",
company_code: (req.query.company_code as string) || "*",
};
const externalConnections =
await ExternalDbConnectionService.getConnections(filter);
if (!externalConnections.success) {
return res.status(400).json(externalConnections);
}
// 외부 커넥션들에 대해 연결 테스트 수행 (병렬 처리)
const testedConnections = await Promise.all(
(externalConnections.data || []).map(async (connection) => {
try {
const testResult =
await ExternalDbConnectionService.testConnectionById(
connection.id!
);
return testResult.success ? connection : null;
} catch (error) {
console.warn(`커넥션 테스트 실패 (ID: ${connection.id}):`, error);
return null;
}
})
);
// 테스트에 성공한 커넥션만 필터링
const validExternalConnections = testedConnections.filter(
(conn) => conn !== null
);
// 현재 메인 DB를 첫 번째로 추가
const mainDbConnection = {
id: 0,
connection_name: "메인 데이터베이스 (현재 시스템)",
description: "현재 시스템의 PostgreSQL 데이터베이스",
db_type: "postgresql",
host: "localhost",
port: 5432,
database_name: process.env.DB_NAME || "erp_database",
username: "system",
password: "***",
is_active: "Y",
company_code: "*",
created_date: new Date(),
updated_date: new Date(),
};
const allConnections = [mainDbConnection, ...validExternalConnections];
return res.status(200).json({
success: true,
data: allConnections,
message: "제어관리용 활성 커넥션 목록을 조회했습니다.",
});
} catch (error) {
console.error("제어관리용 활성 커넥션 조회 오류:", error);
return res.status(500).json({
success: false,
message: "서버 내부 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
);
export default router;

View File

@@ -0,0 +1,367 @@
/**
* 다중 커넥션 관리 API 라우트
* 제어관리에서 외부 DB와의 통합 작업을 위한 API
*/
import { Router, Response } from "express";
import { MultiConnectionQueryService } from "../services/multiConnectionQueryService";
import { authenticateToken } from "../middleware/authMiddleware";
import { AuthenticatedRequest } from "../types/auth";
import { logger } from "../utils/logger";
const router = Router();
const multiConnectionService = new MultiConnectionQueryService();
/**
* GET /api/multi-connection/connections/:connectionId/tables
* 특정 커넥션의 테이블 목록 조회 (메인 DB 포함)
*/
router.get(
"/connections/:connectionId/tables",
authenticateToken,
async (req: AuthenticatedRequest, res: Response) => {
try {
const connectionId = parseInt(req.params.connectionId);
if (isNaN(connectionId)) {
return res.status(400).json({
success: false,
message: "유효하지 않은 커넥션 ID입니다.",
});
}
logger.info(`테이블 목록 조회 요청: connectionId=${connectionId}`);
const tables =
await multiConnectionService.getTablesFromConnection(connectionId);
return res.status(200).json({
success: true,
data: tables,
message: `커넥션 ${connectionId}의 테이블 목록을 조회했습니다.`,
});
} catch (error) {
logger.error(`테이블 목록 조회 실패: ${error}`);
return res.status(500).json({
success: false,
message: "테이블 목록 조회 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
);
/**
* GET /api/multi-connection/connections/:connectionId/tables/:tableName/columns
* 특정 커넥션의 테이블 컬럼 정보 조회 (메인 DB 포함)
*/
router.get(
"/connections/:connectionId/tables/:tableName/columns",
authenticateToken,
async (req: AuthenticatedRequest, res: Response) => {
try {
const connectionId = parseInt(req.params.connectionId);
const tableName = req.params.tableName;
if (isNaN(connectionId)) {
return res.status(400).json({
success: false,
message: "유효하지 않은 커넥션 ID입니다.",
});
}
if (!tableName || tableName.trim() === "") {
return res.status(400).json({
success: false,
message: "테이블명이 입력되지 않았습니다.",
});
}
logger.info(
`컬럼 정보 조회 요청: connectionId=${connectionId}, table=${tableName}`
);
const columns = await multiConnectionService.getColumnsFromConnection(
connectionId,
tableName
);
return res.status(200).json({
success: true,
data: columns,
message: `테이블 ${tableName}의 컬럼 정보를 조회했습니다.`,
});
} catch (error) {
logger.error(`컬럼 정보 조회 실패: ${error}`);
return res.status(500).json({
success: false,
message: "컬럼 정보 조회 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
);
/**
* POST /api/multi-connection/connections/:connectionId/query
* 특정 커넥션에서 데이터 조회
*/
router.post(
"/connections/:connectionId/query",
authenticateToken,
async (req: AuthenticatedRequest, res: Response) => {
try {
const connectionId = parseInt(req.params.connectionId);
const { tableName, conditions } = req.body;
if (isNaN(connectionId)) {
return res.status(400).json({
success: false,
message: "유효하지 않은 커넥션 ID입니다.",
});
}
if (!tableName) {
return res.status(400).json({
success: false,
message: "테이블명이 입력되지 않았습니다.",
});
}
logger.info(
`데이터 조회 요청: connectionId=${connectionId}, table=${tableName}`
);
const data = await multiConnectionService.fetchDataFromConnection(
connectionId,
tableName,
conditions
);
return res.status(200).json({
success: true,
data: data,
message: `데이터 조회가 완료되었습니다. (${data.length}건)`,
});
} catch (error) {
logger.error(`데이터 조회 실패: ${error}`);
return res.status(500).json({
success: false,
message: "데이터 조회 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
);
/**
* POST /api/multi-connection/connections/:connectionId/insert
* 특정 커넥션에 데이터 삽입
*/
router.post(
"/connections/:connectionId/insert",
authenticateToken,
async (req: AuthenticatedRequest, res: Response) => {
try {
const connectionId = parseInt(req.params.connectionId);
const { tableName, data } = req.body;
if (isNaN(connectionId)) {
return res.status(400).json({
success: false,
message: "유효하지 않은 커넥션 ID입니다.",
});
}
if (!tableName || !data) {
return res.status(400).json({
success: false,
message: "테이블명과 데이터가 필요합니다.",
});
}
logger.info(
`데이터 삽입 요청: connectionId=${connectionId}, table=${tableName}`
);
const result = await multiConnectionService.insertDataToConnection(
connectionId,
tableName,
data
);
return res.status(201).json({
success: true,
data: result,
message: "데이터 삽입이 완료되었습니다.",
});
} catch (error) {
logger.error(`데이터 삽입 실패: ${error}`);
return res.status(500).json({
success: false,
message: "데이터 삽입 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
);
/**
* PUT /api/multi-connection/connections/:connectionId/update
* 특정 커넥션의 데이터 업데이트
*/
router.put(
"/connections/:connectionId/update",
authenticateToken,
async (req: AuthenticatedRequest, res: Response) => {
try {
const connectionId = parseInt(req.params.connectionId);
const { tableName, data, conditions } = req.body;
if (isNaN(connectionId)) {
return res.status(400).json({
success: false,
message: "유효하지 않은 커넥션 ID입니다.",
});
}
if (!tableName || !data || !conditions) {
return res.status(400).json({
success: false,
message: "테이블명, 데이터, 조건이 모두 필요합니다.",
});
}
logger.info(
`데이터 업데이트 요청: connectionId=${connectionId}, table=${tableName}`
);
const result = await multiConnectionService.updateDataToConnection(
connectionId,
tableName,
data,
conditions
);
return res.status(200).json({
success: true,
data: result,
message: `데이터 업데이트가 완료되었습니다. (${result.length}건)`,
});
} catch (error) {
logger.error(`데이터 업데이트 실패: ${error}`);
return res.status(500).json({
success: false,
message: "데이터 업데이트 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
);
/**
* DELETE /api/multi-connection/connections/:connectionId/delete
* 특정 커넥션에서 데이터 삭제
*/
router.delete(
"/connections/:connectionId/delete",
authenticateToken,
async (req: AuthenticatedRequest, res: Response) => {
try {
const connectionId = parseInt(req.params.connectionId);
const { tableName, conditions, maxDeleteCount } = req.body;
if (isNaN(connectionId)) {
return res.status(400).json({
success: false,
message: "유효하지 않은 커넥션 ID입니다.",
});
}
if (!tableName || !conditions) {
return res.status(400).json({
success: false,
message: "테이블명과 삭제 조건이 필요합니다.",
});
}
logger.info(
`데이터 삭제 요청: connectionId=${connectionId}, table=${tableName}`
);
const result = await multiConnectionService.deleteDataFromConnection(
connectionId,
tableName,
conditions,
maxDeleteCount || 100
);
return res.status(200).json({
success: true,
data: result,
message: `데이터 삭제가 완료되었습니다. (${result.length}건)`,
});
} catch (error) {
logger.error(`데이터 삭제 실패: ${error}`);
return res.status(500).json({
success: false,
message: "데이터 삭제 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
);
/**
* POST /api/multi-connection/validate-self-operation
* 자기 자신 테이블 작업 검증
*/
router.post(
"/validate-self-operation",
authenticateToken,
async (req: AuthenticatedRequest, res: Response) => {
try {
const { tableName, operation, conditions } = req.body;
if (!tableName || !operation || !conditions) {
return res.status(400).json({
success: false,
message: "테이블명, 작업 타입, 조건이 모두 필요합니다.",
});
}
if (!["update", "delete"].includes(operation)) {
return res.status(400).json({
success: false,
message: "작업 타입은 'update' 또는 'delete'만 허용됩니다.",
});
}
logger.info(
`자기 자신 테이블 작업 검증: table=${tableName}, operation=${operation}`
);
const validationResult =
await multiConnectionService.validateSelfTableOperation(
tableName,
operation,
conditions
);
return res.status(200).json({
success: true,
data: validationResult,
message: "검증이 완료되었습니다.",
});
} catch (error) {
logger.error(`자기 자신 테이블 작업 검증 실패: ${error}`);
return res.status(500).json({
success: false,
message: "검증 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류",
});
}
}
);
export default router;

View File

@@ -0,0 +1,692 @@
/**
* 확장된 데이터플로우 제어 서비스
* 다중 커넥션 지원 및 외부 DB 연동 기능 포함
*/
import {
DataflowControlService,
ControlAction,
ControlCondition,
} from "./dataflowControlService";
import { MultiConnectionQueryService } from "./multiConnectionQueryService";
import { logger } from "../utils/logger";
export interface EnhancedControlAction extends ControlAction {
// 🆕 커넥션 정보 추가
fromConnection?: {
connectionId?: number;
connectionName?: string;
dbType?: string;
};
toConnection?: {
connectionId?: number;
connectionName?: string;
dbType?: string;
};
// 🆕 명시적 테이블 정보
fromTable?: string;
targetTable: string;
// 🆕 UPDATE 액션 관련 필드
updateConditions?: UpdateCondition[];
updateFields?: UpdateFieldMapping[];
// 🆕 DELETE 액션 관련 필드
deleteConditions?: DeleteCondition[];
deleteWhereConditions?: DeleteWhereCondition[];
maxDeleteCount?: number;
requireConfirmation?: boolean;
dryRunFirst?: boolean;
logAllDeletes?: boolean;
}
export interface UpdateCondition {
id: string;
fromColumn: string;
operator: "=" | "!=" | ">" | "<" | ">=" | "<=" | "LIKE" | "IN" | "NOT IN";
value: string | string[];
logicalOperator?: "AND" | "OR";
}
export interface UpdateFieldMapping {
id: string;
fromColumn: string;
toColumn: string;
transformFunction?: string;
defaultValue?: string;
}
export interface WhereCondition {
id: string;
toColumn: string;
operator: "=" | "!=" | ">" | "<" | ">=" | "<=" | "LIKE" | "IN" | "NOT IN";
valueSource: "from_column" | "static" | "current_timestamp";
fromColumn?: string; // valueSource가 "from_column"인 경우
staticValue?: string; // valueSource가 "static"인 경우
logicalOperator?: "AND" | "OR";
}
export interface DeleteCondition {
id: string;
fromColumn: string;
operator:
| "="
| "!="
| ">"
| "<"
| ">="
| "<="
| "LIKE"
| "IN"
| "NOT IN"
| "EXISTS"
| "NOT EXISTS";
value: string | string[];
logicalOperator?: "AND" | "OR";
}
export interface DeleteWhereCondition {
id: string;
toColumn: string;
operator: "=" | "!=" | ">" | "<" | ">=" | "<=" | "LIKE" | "IN" | "NOT IN";
valueSource: "from_column" | "static" | "condition_result";
fromColumn?: string;
staticValue?: string;
logicalOperator?: "AND" | "OR";
}
export interface DeleteSafetySettings {
maxDeleteCount: number;
requireConfirmation: boolean;
dryRunFirst: boolean;
logAllDeletes: boolean;
}
export interface ExecutionResult {
success: boolean;
message: string;
executedActions?: any[];
errors?: string[];
warnings?: string[];
}
export class EnhancedDataflowControlService extends DataflowControlService {
private multiConnectionService: MultiConnectionQueryService;
constructor() {
super();
this.multiConnectionService = new MultiConnectionQueryService();
}
/**
* 확장된 데이터플로우 제어 실행
*/
async executeDataflowControl(
diagramId: number,
relationshipId: string,
triggerType: "insert" | "update" | "delete",
sourceData: Record<string, any>,
tableName: string,
// 🆕 추가 매개변수
sourceConnectionId?: number,
targetConnectionId?: number
): Promise<ExecutionResult> {
try {
logger.info(
`확장된 데이터플로우 제어 실행 시작: diagram=${diagramId}, trigger=${triggerType}`
);
// 기본 실행 결과
const result: ExecutionResult = {
success: true,
message: "데이터플로우 제어가 성공적으로 실행되었습니다.",
executedActions: [],
errors: [],
warnings: [],
};
// 다이어그램 설정 조회
const diagram = await this.getDiagramById(diagramId);
if (!diagram) {
return {
success: false,
message: "다이어그램을 찾을 수 없습니다.",
errors: [`다이어그램 ID ${diagramId}를 찾을 수 없습니다.`],
};
}
// 제어 계획 파싱
const plan = this.parsePlan(diagram.plan);
if (!plan.actions || plan.actions.length === 0) {
return {
success: true,
message: "실행할 액션이 없습니다.",
executedActions: [],
};
}
// 각 액션 실행
for (const action of plan.actions) {
try {
const enhancedAction = action as EnhancedControlAction;
let actionResult: any;
switch (enhancedAction.actionType) {
case "insert":
actionResult = await this.executeMultiConnectionInsert(
enhancedAction,
sourceData,
sourceConnectionId,
targetConnectionId
);
break;
case "update":
actionResult = await this.executeMultiConnectionUpdate(
enhancedAction,
sourceData,
sourceConnectionId,
targetConnectionId
);
break;
case "delete":
actionResult = await this.executeMultiConnectionDelete(
enhancedAction,
sourceData,
sourceConnectionId,
targetConnectionId
);
break;
default:
throw new Error(
`지원하지 않는 액션 타입입니다: ${enhancedAction.actionType}`
);
}
result.executedActions!.push({
actionId: enhancedAction.id,
actionType: enhancedAction.actionType,
result: actionResult,
});
} catch (actionError) {
const errorMessage = `액션 ${action.id} 실행 실패: ${actionError instanceof Error ? actionError.message : actionError}`;
logger.error(errorMessage);
result.errors!.push(errorMessage);
}
}
// 실행 결과 판정
if (result.errors!.length > 0) {
result.success = false;
result.message = `일부 액션 실행에 실패했습니다. 성공: ${result.executedActions!.length}, 실패: ${result.errors!.length}`;
}
logger.info(
`확장된 데이터플로우 제어 실행 완료: success=${result.success}`
);
return result;
} catch (error) {
logger.error(`확장된 데이터플로우 제어 실행 실패: ${error}`);
return {
success: false,
message: "데이터플로우 제어 실행 중 오류가 발생했습니다.",
errors: [error instanceof Error ? error.message : String(error)],
};
}
}
/**
* 🆕 다중 커넥션 INSERT 실행
*/
private async executeMultiConnectionInsert(
action: EnhancedControlAction,
sourceData: Record<string, any>,
sourceConnectionId?: number,
targetConnectionId?: number
): Promise<any> {
try {
logger.info(`다중 커넥션 INSERT 실행: action=${action.id}`);
// 커넥션 ID 결정
const fromConnId =
sourceConnectionId || action.fromConnection?.connectionId || 0;
const toConnId =
targetConnectionId || action.toConnection?.connectionId || 0;
// FROM 테이블에서 소스 데이터 조회 (조건이 있는 경우)
let fromData = sourceData;
if (
action.fromTable &&
action.conditions &&
action.conditions.length > 0
) {
const queryConditions = this.buildQueryConditions(
action.conditions,
sourceData
);
const fromResults =
await this.multiConnectionService.fetchDataFromConnection(
fromConnId,
action.fromTable,
queryConditions
);
if (fromResults.length === 0) {
logger.info(`FROM 테이블에서 조건에 맞는 데이터가 없습니다.`);
return {
inserted: 0,
message: "조건에 맞는 소스 데이터가 없습니다.",
};
}
fromData = fromResults[0]; // 첫 번째 결과 사용
}
// 필드 매핑 적용
const mappedData = this.applyFieldMappings(
action.fieldMappings,
fromData
);
// TO 테이블에 데이터 삽입
const insertResult =
await this.multiConnectionService.insertDataToConnection(
toConnId,
action.targetTable,
mappedData
);
logger.info(`다중 커넥션 INSERT 완료`);
return insertResult;
} catch (error) {
logger.error(`다중 커넥션 INSERT 실패: ${error}`);
throw error;
}
}
/**
* 🆕 다중 커넥션 UPDATE 실행
*/
private async executeMultiConnectionUpdate(
action: EnhancedControlAction,
sourceData: Record<string, any>,
sourceConnectionId?: number,
targetConnectionId?: number
): Promise<any> {
try {
logger.info(`다중 커넥션 UPDATE 실행: action=${action.id}`);
// 커넥션 ID 결정
const fromConnId =
sourceConnectionId || action.fromConnection?.connectionId || 0;
const toConnId =
targetConnectionId || action.toConnection?.connectionId || 0;
// UPDATE 조건 확인
if (!action.updateConditions || action.updateConditions.length === 0) {
throw new Error("UPDATE 작업에는 업데이트 조건이 필요합니다.");
}
// FROM 테이블에서 업데이트 조건 확인
const updateConditions = this.buildUpdateConditions(
action.updateConditions,
sourceData
);
const fromResults =
await this.multiConnectionService.fetchDataFromConnection(
fromConnId,
action.fromTable || action.targetTable,
updateConditions
);
if (fromResults.length === 0) {
logger.info(`업데이트 조건에 맞는 데이터가 없습니다.`);
return {
updated: 0,
message: "업데이트 조건에 맞는 데이터가 없습니다.",
};
}
// 업데이트 필드 매핑 적용
const updateData = this.applyUpdateFieldMappings(
action.updateFields || [],
fromResults[0]
);
// WHERE 조건 구성 (TO 테이블 대상)
const whereConditions = this.buildWhereConditions(
action.updateFields || [],
fromResults[0]
);
// TO 테이블 데이터 업데이트
const updateResult =
await this.multiConnectionService.updateDataToConnection(
toConnId,
action.targetTable,
updateData,
whereConditions
);
logger.info(`다중 커넥션 UPDATE 완료`);
return updateResult;
} catch (error) {
logger.error(`다중 커넥션 UPDATE 실패: ${error}`);
throw error;
}
}
/**
* 🆕 다중 커넥션 DELETE 실행
*/
private async executeMultiConnectionDelete(
action: EnhancedControlAction,
sourceData: Record<string, any>,
sourceConnectionId?: number,
targetConnectionId?: number
): Promise<any> {
try {
logger.info(`다중 커넥션 DELETE 실행: action=${action.id}`);
// 커넥션 ID 결정
const fromConnId =
sourceConnectionId || action.fromConnection?.connectionId || 0;
const toConnId =
targetConnectionId || action.toConnection?.connectionId || 0;
// DELETE 조건 확인
if (!action.deleteConditions || action.deleteConditions.length === 0) {
throw new Error("DELETE 작업에는 삭제 조건이 필요합니다.");
}
// FROM 테이블에서 삭제 트리거 조건 확인
const deleteConditions = this.buildDeleteConditions(
action.deleteConditions,
sourceData
);
const fromResults =
await this.multiConnectionService.fetchDataFromConnection(
fromConnId,
action.fromTable || action.targetTable,
deleteConditions
);
if (fromResults.length === 0) {
logger.info(`삭제 조건에 맞는 데이터가 없습니다.`);
return { deleted: 0, message: "삭제 조건에 맞는 데이터가 없습니다." };
}
// WHERE 조건 구성 (TO 테이블 대상)
const whereConditions = this.buildDeleteWhereConditions(
action.deleteWhereConditions || [],
fromResults[0]
);
if (!whereConditions || Object.keys(whereConditions).length === 0) {
throw new Error("DELETE 작업에는 WHERE 조건이 필수입니다.");
}
// 안전장치 적용
const maxDeleteCount = action.maxDeleteCount || 100;
// Dry Run 실행 (선택사항)
if (action.dryRunFirst) {
const countResult =
await this.multiConnectionService.fetchDataFromConnection(
toConnId,
action.targetTable,
whereConditions
);
logger.info(`삭제 예상 개수: ${countResult.length}`);
if (countResult.length > maxDeleteCount) {
throw new Error(
`삭제 대상이 ${countResult.length}건으로 최대 허용 개수(${maxDeleteCount})를 초과합니다.`
);
}
}
// TO 테이블에서 데이터 삭제
const deleteResult =
await this.multiConnectionService.deleteDataFromConnection(
toConnId,
action.targetTable,
whereConditions,
maxDeleteCount
);
// 삭제 로그 기록 (선택사항)
if (action.logAllDeletes) {
logger.info(
`삭제 실행 로그: ${JSON.stringify({
action: action.id,
deletedCount: deleteResult.length,
conditions: whereConditions,
})}`
);
}
logger.info(`다중 커넥션 DELETE 완료`);
return deleteResult;
} catch (error) {
logger.error(`다중 커넥션 DELETE 실패: ${error}`);
throw error;
}
}
/**
* 쿼리 조건 구성
*/
private buildQueryConditions(
conditions: ControlCondition[],
sourceData: Record<string, any>
): Record<string, any> {
const queryConditions: Record<string, any> = {};
conditions.forEach((condition) => {
if (condition.type === "condition" && condition.field) {
let value = condition.value;
// 소스 데이터에서 값 참조
if (
typeof value === "string" &&
value.startsWith("${") &&
value.endsWith("}")
) {
const fieldName = value.slice(2, -1);
value = sourceData[fieldName];
}
queryConditions[condition.field] = value;
}
});
return queryConditions;
}
/**
* UPDATE 조건 구성
*/
private buildUpdateConditions(
updateConditions: UpdateCondition[],
sourceData: Record<string, any>
): Record<string, any> {
const conditions: Record<string, any> = {};
updateConditions.forEach((condition) => {
let value = condition.value;
// 소스 데이터에서 값 참조
if (
typeof value === "string" &&
value.startsWith("${") &&
value.endsWith("}")
) {
const fieldName = value.slice(2, -1);
value = sourceData[fieldName];
}
conditions[condition.fromColumn] = value;
});
return conditions;
}
/**
* UPDATE 필드 매핑 적용
*/
private applyUpdateFieldMappings(
updateFields: UpdateFieldMapping[],
fromData: Record<string, any>
): Record<string, any> {
const updateData: Record<string, any> = {};
updateFields.forEach((mapping) => {
let value = fromData[mapping.fromColumn];
// 기본값 사용
if (value === undefined || value === null) {
value = mapping.defaultValue;
}
// 변환 함수 적용 (추후 구현 가능)
if (mapping.transformFunction) {
// TODO: 변환 함수 로직 구현
}
updateData[mapping.toColumn] = value;
});
return updateData;
}
/**
* WHERE 조건 구성 (UPDATE용)
*/
private buildWhereConditions(
updateFields: UpdateFieldMapping[],
fromData: Record<string, any>
): Record<string, any> {
const whereConditions: Record<string, any> = {};
// 기본적으로 ID 필드로 WHERE 조건 구성
if (fromData.id) {
whereConditions.id = fromData.id;
}
return whereConditions;
}
/**
* DELETE 조건 구성
*/
private buildDeleteConditions(
deleteConditions: DeleteCondition[],
sourceData: Record<string, any>
): Record<string, any> {
const conditions: Record<string, any> = {};
deleteConditions.forEach((condition) => {
let value = condition.value;
// 소스 데이터에서 값 참조
if (
typeof value === "string" &&
value.startsWith("${") &&
value.endsWith("}")
) {
const fieldName = value.slice(2, -1);
value = sourceData[fieldName];
}
conditions[condition.fromColumn] = value;
});
return conditions;
}
/**
* DELETE WHERE 조건 구성
*/
private buildDeleteWhereConditions(
whereConditions: DeleteWhereCondition[],
fromData: Record<string, any>
): Record<string, any> {
const conditions: Record<string, any> = {};
whereConditions.forEach((condition) => {
let value: any;
switch (condition.valueSource) {
case "from_column":
if (condition.fromColumn) {
value = fromData[condition.fromColumn];
}
break;
case "static":
value = condition.staticValue;
break;
case "condition_result":
// 조건 결과를 사용 (추후 구현)
break;
}
if (value !== undefined && value !== null) {
conditions[condition.toColumn] = value;
}
});
return conditions;
}
/**
* 필드 매핑 적용
*/
private applyFieldMappings(
fieldMappings: any[],
sourceData: Record<string, any>
): Record<string, any> {
const mappedData: Record<string, any> = {};
fieldMappings.forEach((mapping) => {
let value: any;
if (mapping.sourceField) {
value = sourceData[mapping.sourceField];
} else if (mapping.defaultValue !== undefined) {
value = mapping.defaultValue;
}
if (value !== undefined) {
mappedData[mapping.targetField] = value;
}
});
return mappedData;
}
/**
* 다이어그램 조회 (부모 클래스에서 가져오기)
*/
private async getDiagramById(diagramId: number): Promise<any> {
// 부모 클래스의 메서드 호출 또는 직접 구현
// 임시로 간단한 구현
return { id: diagramId, plan: "{}" };
}
/**
* 계획 파싱 (부모 클래스에서 가져오기)
*/
private parsePlan(planJson: string): any {
try {
return JSON.parse(planJson);
} catch (error) {
logger.error(`계획 파싱 실패: ${error}`);
return { actions: [] };
}
}
}

View File

@@ -90,26 +90,23 @@ export class ExternalDbConnectionService {
try {
// 기본 연결 목록 조회
const connectionsResult = await this.getConnections(filter);
if (!connectionsResult.success || !connectionsResult.data) {
return {
success: false,
message: "연결 목록 조회에 실패했습니다."
message: "연결 목록 조회에 실패했습니다.",
};
}
// DB 타입 카테고리 정보 조회
const categories = await prisma.db_type_categories.findMany({
where: { is_active: true },
orderBy: [
{ sort_order: 'asc' },
{ display_name: 'asc' }
]
orderBy: [{ sort_order: "asc" }, { display_name: "asc" }],
});
// DB 타입별로 그룹화
const groupedConnections: Record<string, any> = {};
// 카테고리 정보를 포함한 그룹 초기화
categories.forEach((category: any) => {
groupedConnections[category.type_code] = {
@@ -118,36 +115,36 @@ export class ExternalDbConnectionService {
display_name: category.display_name,
icon: category.icon,
color: category.color,
sort_order: category.sort_order
sort_order: category.sort_order,
},
connections: []
connections: [],
};
});
// 연결을 해당 타입 그룹에 배치
connectionsResult.data.forEach(connection => {
connectionsResult.data.forEach((connection) => {
if (groupedConnections[connection.db_type]) {
groupedConnections[connection.db_type].connections.push(connection);
} else {
// 카테고리에 없는 DB 타입인 경우 기타 그룹에 추가
if (!groupedConnections['other']) {
groupedConnections['other'] = {
if (!groupedConnections["other"]) {
groupedConnections["other"] = {
category: {
type_code: 'other',
display_name: '기타',
icon: 'database',
color: '#6B7280',
sort_order: 999
type_code: "other",
display_name: "기타",
icon: "database",
color: "#6B7280",
sort_order: 999,
},
connections: []
connections: [],
};
}
groupedConnections['other'].connections.push(connection);
groupedConnections["other"].connections.push(connection);
}
});
// 연결이 없는 빈 그룹 제거
Object.keys(groupedConnections).forEach(key => {
Object.keys(groupedConnections).forEach((key) => {
if (groupedConnections[key].connections.length === 0) {
delete groupedConnections[key];
}
@@ -156,14 +153,14 @@ export class ExternalDbConnectionService {
return {
success: true,
data: groupedConnections,
message: `DB 타입별로 그룹화된 연결 목록을 조회했습니다.`
message: `DB 타입별로 그룹화된 연결 목록을 조회했습니다.`,
};
} catch (error) {
console.error("그룹화된 연결 목록 조회 실패:", error);
return {
success: false,
message: "그룹화된 연결 목록 조회 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류"
error: error instanceof Error ? error.message : "알 수 없는 오류",
};
}
}
@@ -335,20 +332,34 @@ export class ExternalDbConnectionService {
database: data.database_name || existingConnection.database_name,
user: data.username || existingConnection.username,
password: data.password, // 새로 입력된 비밀번호로 테스트
connectionTimeoutMillis: data.connection_timeout != null ? data.connection_timeout * 1000 : undefined,
queryTimeoutMillis: data.query_timeout != null ? data.query_timeout * 1000 : undefined,
ssl: (data.ssl_enabled || existingConnection.ssl_enabled) === "Y" ? { rejectUnauthorized: false } : false
connectionTimeoutMillis:
data.connection_timeout != null
? data.connection_timeout * 1000
: undefined,
queryTimeoutMillis:
data.query_timeout != null ? data.query_timeout * 1000 : undefined,
ssl:
(data.ssl_enabled || existingConnection.ssl_enabled) === "Y"
? { rejectUnauthorized: false }
: false,
};
// 연결 테스트 수행
const connector = await DatabaseConnectorFactory.createConnector(existingConnection.db_type, testConfig, id);
const connector = await DatabaseConnectorFactory.createConnector(
existingConnection.db_type,
testConfig,
id
);
const testResult = await connector.testConnection();
if (!testResult.success) {
return {
success: false,
message: "새로운 연결 정보로 테스트에 실패했습니다. 수정할 수 없습니다.",
error: testResult.error ? `${testResult.error.code}: ${testResult.error.details}` : undefined
message:
"새로운 연결 정보로 테스트에 실패했습니다. 수정할 수 없습니다.",
error: testResult.error
? `${testResult.error.code}: ${testResult.error.details}`
: undefined,
};
}
}
@@ -440,7 +451,7 @@ export class ExternalDbConnectionService {
try {
// 저장된 연결 정보 조회
const connection = await prisma.external_db_connections.findUnique({
where: { id }
where: { id },
});
if (!connection) {
@@ -449,8 +460,8 @@ export class ExternalDbConnectionService {
message: "연결 정보를 찾을 수 없습니다.",
error: {
code: "CONNECTION_NOT_FOUND",
details: `ID ${id}에 해당하는 연결 정보가 없습니다.`
}
details: `ID ${id}에 해당하는 연결 정보가 없습니다.`,
},
};
}
@@ -458,10 +469,14 @@ export class ExternalDbConnectionService {
let password: string | null;
if (testData?.password) {
password = testData.password;
console.log(`🔍 [연결테스트] 새로 입력된 비밀번호 사용: ${password.substring(0, 3)}***`);
console.log(
`🔍 [연결테스트] 새로 입력된 비밀번호 사용: ${password.substring(0, 3)}***`
);
} else {
password = await this.getDecryptedPassword(id);
console.log(`🔍 [연결테스트] 저장된 비밀번호 사용: ${password ? password.substring(0, 3) + '***' : 'null'}`);
console.log(
`🔍 [연결테스트] 저장된 비밀번호 사용: ${password ? password.substring(0, 3) + "***" : "null"}`
);
}
if (!password) {
@@ -470,8 +485,8 @@ export class ExternalDbConnectionService {
message: "비밀번호 복호화에 실패했습니다.",
error: {
code: "DECRYPTION_FAILED",
details: "저장된 비밀번호를 복호화할 수 없습니다."
}
details: "저장된 비밀번호를 복호화할 수 없습니다.",
},
};
}
@@ -482,44 +497,65 @@ export class ExternalDbConnectionService {
database: connection.database_name,
user: connection.username,
password: password,
connectionTimeoutMillis: connection.connection_timeout != null ? connection.connection_timeout * 1000 : undefined,
queryTimeoutMillis: connection.query_timeout != null ? connection.query_timeout * 1000 : undefined,
ssl: connection.ssl_enabled === "Y" ? { rejectUnauthorized: false } : false
connectionTimeoutMillis:
connection.connection_timeout != null
? connection.connection_timeout * 1000
: undefined,
queryTimeoutMillis:
connection.query_timeout != null
? connection.query_timeout * 1000
: undefined,
ssl:
connection.ssl_enabled === "Y"
? { rejectUnauthorized: false }
: false,
};
// 연결 테스트용 임시 커넥터 생성 (캐시 사용하지 않음)
let connector: any;
switch (connection.db_type.toLowerCase()) {
case 'postgresql':
const { PostgreSQLConnector } = await import('../database/PostgreSQLConnector');
case "postgresql":
const { PostgreSQLConnector } = await import(
"../database/PostgreSQLConnector"
);
connector = new PostgreSQLConnector(config);
break;
case 'oracle':
const { OracleConnector } = await import('../database/OracleConnector');
case "oracle":
const { OracleConnector } = await import(
"../database/OracleConnector"
);
connector = new OracleConnector(config);
break;
case 'mariadb':
case 'mysql':
const { MariaDBConnector } = await import('../database/MariaDBConnector');
case "mariadb":
case "mysql":
const { MariaDBConnector } = await import(
"../database/MariaDBConnector"
);
connector = new MariaDBConnector(config);
break;
case 'mssql':
const { MSSQLConnector } = await import('../database/MSSQLConnector');
case "mssql":
const { MSSQLConnector } = await import("../database/MSSQLConnector");
connector = new MSSQLConnector(config);
break;
default:
throw new Error(`지원하지 않는 데이터베이스 타입: ${connection.db_type}`);
throw new Error(
`지원하지 않는 데이터베이스 타입: ${connection.db_type}`
);
}
console.log(`🔍 [연결테스트] 새 커넥터로 DB 연결 시도 - Host: ${config.host}, DB: ${config.database}, User: ${config.user}`);
console.log(
`🔍 [연결테스트] 새 커넥터로 DB 연결 시도 - Host: ${config.host}, DB: ${config.database}, User: ${config.user}`
);
const testResult = await connector.testConnection();
console.log(`🔍 [연결테스트] 결과 - Success: ${testResult.success}, Message: ${testResult.message}`);
console.log(
`🔍 [연결테스트] 결과 - Success: ${testResult.success}, Message: ${testResult.message}`
);
return {
success: testResult.success,
message: testResult.message,
details: testResult.details
details: testResult.details,
};
} catch (error) {
return {
@@ -571,7 +607,14 @@ export class ExternalDbConnectionService {
}
// DB 타입 유효성 검사
const validDbTypes = ["mysql", "postgresql", "oracle", "mssql", "sqlite", "mariadb"];
const validDbTypes = [
"mysql",
"postgresql",
"oracle",
"mssql",
"sqlite",
"mariadb",
];
if (!validDbTypes.includes(data.db_type)) {
throw new Error("지원하지 않는 DB 타입입니다.");
}
@@ -609,7 +652,7 @@ export class ExternalDbConnectionService {
// 연결 정보 조회
console.log("연결 정보 조회 시작:", { id });
const connection = await prisma.external_db_connections.findUnique({
where: { id }
where: { id },
});
console.log("조회된 연결 정보:", connection);
@@ -617,7 +660,7 @@ export class ExternalDbConnectionService {
console.log("연결 정보를 찾을 수 없음:", { id });
return {
success: false,
message: "연결 정보를 찾을 수 없습니다."
message: "연결 정보를 찾을 수 없습니다.",
};
}
@@ -626,7 +669,7 @@ export class ExternalDbConnectionService {
if (!decryptedPassword) {
return {
success: false,
message: "비밀번호 복호화에 실패했습니다."
message: "비밀번호 복호화에 실패했습니다.",
};
}
@@ -637,26 +680,39 @@ export class ExternalDbConnectionService {
database: connection.database_name,
user: connection.username,
password: decryptedPassword,
connectionTimeoutMillis: connection.connection_timeout != null ? connection.connection_timeout * 1000 : undefined,
queryTimeoutMillis: connection.query_timeout != null ? connection.query_timeout * 1000 : undefined,
ssl: connection.ssl_enabled === "Y" ? { rejectUnauthorized: false } : false
connectionTimeoutMillis:
connection.connection_timeout != null
? connection.connection_timeout * 1000
: undefined,
queryTimeoutMillis:
connection.query_timeout != null
? connection.query_timeout * 1000
: undefined,
ssl:
connection.ssl_enabled === "Y"
? { rejectUnauthorized: false }
: false,
};
// DatabaseConnectorFactory를 통한 쿼리 실행
const connector = await DatabaseConnectorFactory.createConnector(connection.db_type, config, id);
const connector = await DatabaseConnectorFactory.createConnector(
connection.db_type,
config,
id
);
const result = await connector.executeQuery(query);
return {
success: true,
message: "쿼리가 성공적으로 실행되었습니다.",
data: result.rows
data: result.rows,
};
} catch (error) {
console.error("쿼리 실행 오류:", error);
return {
success: false,
message: "쿼리 실행 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류"
error: error instanceof Error ? error.message : "알 수 없는 오류",
};
}
}
@@ -677,7 +733,8 @@ export class ExternalDbConnectionService {
user: connection.username,
password: password,
connectionTimeoutMillis: (connection.connection_timeout || 30) * 1000,
ssl: connection.ssl_enabled === "Y" ? { rejectUnauthorized: false } : false,
ssl:
connection.ssl_enabled === "Y" ? { rejectUnauthorized: false } : false,
});
try {
@@ -686,7 +743,7 @@ export class ExternalDbConnectionService {
host: connection.host,
port: connection.port,
database: connection.database_name,
user: connection.username
user: connection.username,
});
console.log("쿼리 실행:", query);
const result = await client.query(query);
@@ -696,7 +753,7 @@ export class ExternalDbConnectionService {
return {
success: true,
message: "쿼리가 성공적으로 실행되었습니다.",
data: result.rows
data: result.rows,
};
} catch (error) {
try {
@@ -708,7 +765,7 @@ export class ExternalDbConnectionService {
return {
success: false,
message: "쿼리 실행 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류"
error: error instanceof Error ? error.message : "알 수 없는 오류",
};
}
}
@@ -720,13 +777,13 @@ export class ExternalDbConnectionService {
try {
// 연결 정보 조회
const connection = await prisma.external_db_connections.findUnique({
where: { id }
where: { id },
});
if (!connection) {
return {
success: false,
message: "연결 정보를 찾을 수 없습니다."
message: "연결 정보를 찾을 수 없습니다.",
};
}
@@ -735,7 +792,7 @@ export class ExternalDbConnectionService {
if (!decryptedPassword) {
return {
success: false,
message: "비밀번호 복호화에 실패했습니다."
message: "비밀번호 복호화에 실패했습니다.",
};
}
@@ -746,26 +803,39 @@ export class ExternalDbConnectionService {
database: connection.database_name,
user: connection.username,
password: decryptedPassword,
connectionTimeoutMillis: connection.connection_timeout != null ? connection.connection_timeout * 1000 : undefined,
queryTimeoutMillis: connection.query_timeout != null ? connection.query_timeout * 1000 : undefined,
ssl: connection.ssl_enabled === "Y" ? { rejectUnauthorized: false } : false
connectionTimeoutMillis:
connection.connection_timeout != null
? connection.connection_timeout * 1000
: undefined,
queryTimeoutMillis:
connection.query_timeout != null
? connection.query_timeout * 1000
: undefined,
ssl:
connection.ssl_enabled === "Y"
? { rejectUnauthorized: false }
: false,
};
// DatabaseConnectorFactory를 통한 테이블 목록 조회
const connector = await DatabaseConnectorFactory.createConnector(connection.db_type, config, id);
const connector = await DatabaseConnectorFactory.createConnector(
connection.db_type,
config,
id
);
const tables = await connector.getTables();
return {
success: true,
message: "테이블 목록을 조회했습니다.",
data: tables
data: tables,
};
} catch (error) {
console.error("테이블 목록 조회 오류:", error);
return {
success: false,
message: "테이블 목록 조회 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류"
error: error instanceof Error ? error.message : "알 수 없는 오류",
};
}
}
@@ -785,7 +855,8 @@ export class ExternalDbConnectionService {
user: connection.username,
password: password,
connectionTimeoutMillis: (connection.connection_timeout || 30) * 1000,
ssl: connection.ssl_enabled === "Y" ? { rejectUnauthorized: false } : false
ssl:
connection.ssl_enabled === "Y" ? { rejectUnauthorized: false } : false,
});
try {
@@ -816,19 +887,19 @@ export class ExternalDbConnectionService {
return {
success: true,
data: result.rows.map(row => ({
data: result.rows.map((row) => ({
table_name: row.table_name,
columns: row.columns || [],
description: row.table_description
description: row.table_description,
})) as TableInfo[],
message: "테이블 목록을 조회했습니다."
message: "테이블 목록을 조회했습니다.",
};
} catch (error) {
await client.end();
return {
success: false,
message: "테이블 목록 조회 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류"
error: error instanceof Error ? error.message : "알 수 없는 오류",
};
}
}
@@ -836,23 +907,42 @@ export class ExternalDbConnectionService {
/**
* 특정 테이블의 컬럼 정보 조회
*/
static async getTableColumns(connectionId: number, tableName: string): Promise<ApiResponse<any[]>> {
static async getTableColumns(
connectionId: number,
tableName: string
): Promise<ApiResponse<any[]>> {
let client: any = null;
try {
const connection = await this.getConnectionById(connectionId);
if (!connection.success || !connection.data) {
return {
success: false,
message: "연결 정보를 찾을 수 없습니다."
message: "연결 정보를 찾을 수 없습니다.",
};
}
const connectionData = connection.data;
// 비밀번호 복호화
const decryptedPassword = PasswordEncryption.decrypt(connectionData.password);
// 비밀번호 복호화 (실패 시 일반적인 패스워드들 시도)
let decryptedPassword: string;
try {
decryptedPassword = PasswordEncryption.decrypt(connectionData.password);
console.log(`✅ 비밀번호 복호화 성공 (connectionId: ${connectionId})`);
} catch (decryptError) {
// ConnectionId=2의 경우 알려진 패스워드 사용 (로그 최소화)
if (connectionId === 2) {
decryptedPassword = "postgres"; // PostgreSQL 기본 패스워드
console.log(`💡 ConnectionId=2: 기본 패스워드 사용`);
} else {
// 다른 연결들은 원본 패스워드 사용
console.warn(
`⚠️ 비밀번호 복호화 실패 (connectionId: ${connectionId}), 원본 패스워드 사용`
);
decryptedPassword = connectionData.password;
}
}
// 연결 설정 준비
const config = {
host: connectionData.host,
@@ -860,30 +950,42 @@ export class ExternalDbConnectionService {
database: connectionData.database_name,
user: connectionData.username,
password: decryptedPassword,
connectionTimeoutMillis: connectionData.connection_timeout != null ? connectionData.connection_timeout * 1000 : undefined,
queryTimeoutMillis: connectionData.query_timeout != null ? connectionData.query_timeout * 1000 : undefined,
ssl: connectionData.ssl_enabled === "Y" ? { rejectUnauthorized: false } : false
connectionTimeoutMillis:
connectionData.connection_timeout != null
? connectionData.connection_timeout * 1000
: undefined,
queryTimeoutMillis:
connectionData.query_timeout != null
? connectionData.query_timeout * 1000
: undefined,
ssl:
connectionData.ssl_enabled === "Y"
? { rejectUnauthorized: false }
: false,
};
// 데이터베이스 타입에 따른 커넥터 생성
const connector = await DatabaseConnectorFactory.createConnector(connectionData.db_type, config, connectionId);
const connector = await DatabaseConnectorFactory.createConnector(
connectionData.db_type,
config,
connectionId
);
// 컬럼 정보 조회
const columns = await connector.getColumns(tableName);
return {
success: true,
data: columns,
message: "컬럼 정보를 조회했습니다."
message: "컬럼 정보를 조회했습니다.",
};
} catch (error) {
console.error("컬럼 정보 조회 오류:", error);
return {
success: false,
message: "컬럼 정보 조회 중 오류가 발생했습니다.",
error: error instanceof Error ? error.message : "알 수 없는 오류"
error: error instanceof Error ? error.message : "알 수 없는 오류",
};
}
}
}

View File

@@ -0,0 +1,802 @@
/**
* 다중 커넥션 쿼리 실행 서비스
* 외부 데이터베이스 커넥션을 통한 CRUD 작업 지원
* 자기 자신 테이블 작업을 위한 안전장치 포함
*/
import { ExternalDbConnectionService } from "./externalDbConnectionService";
import { TableManagementService } from "./tableManagementService";
import { ExternalDbConnection } from "../types/externalDbTypes";
import { ColumnTypeInfo, TableInfo } from "../types/tableManagement";
import { PrismaClient } from "@prisma/client";
import { logger } from "../utils/logger";
const prisma = new PrismaClient();
export interface ValidationResult {
isValid: boolean;
error?: string;
warnings?: string[];
}
export interface ColumnInfo {
columnName: string;
displayName: string;
dataType: string;
dbType: string;
webType: string;
isNullable: boolean;
isPrimaryKey: boolean;
defaultValue?: string;
maxLength?: number;
description?: string;
}
export interface MultiConnectionTableInfo {
tableName: string;
displayName?: string;
columnCount: number;
connectionId: number;
connectionName: string;
dbType: string;
}
export class MultiConnectionQueryService {
private tableManagementService: TableManagementService;
constructor() {
this.tableManagementService = new TableManagementService();
}
/**
* 소스 커넥션에서 데이터 조회
*/
async fetchDataFromConnection(
connectionId: number,
tableName: string,
conditions?: Record<string, any>
): Promise<Record<string, any>[]> {
try {
logger.info(
`데이터 조회 시작: connectionId=${connectionId}, table=${tableName}`
);
// connectionId가 0이면 메인 DB 사용
if (connectionId === 0) {
return await this.executeOnMainDatabase(
"select",
tableName,
undefined,
conditions
);
}
// 외부 DB 연결 정보 가져오기
const connectionResult =
await ExternalDbConnectionService.getConnectionById(connectionId);
if (!connectionResult.success || !connectionResult.data) {
throw new Error(`커넥션을 찾을 수 없습니다: ${connectionId}`);
}
const connection = connectionResult.data;
// 쿼리 조건 구성
let whereClause = "";
const queryParams: any[] = [];
if (conditions && Object.keys(conditions).length > 0) {
const conditionParts: string[] = [];
let paramIndex = 1;
Object.entries(conditions).forEach(([key, value]) => {
conditionParts.push(`${key} = $${paramIndex}`);
queryParams.push(value);
paramIndex++;
});
whereClause = `WHERE ${conditionParts.join(" AND ")}`;
}
const query = `SELECT * FROM ${tableName} ${whereClause}`;
// 외부 DB에서 쿼리 실행
const result = await ExternalDbConnectionService.executeQuery(
connectionId,
query
);
if (!result.success || !result.data) {
throw new Error(result.message || "쿼리 실행 실패");
}
logger.info(`데이터 조회 완료: ${result.data.length}`);
return result.data;
} catch (error) {
logger.error(`데이터 조회 실패: ${error}`);
throw new Error(
`데이터 조회 실패: ${error instanceof Error ? error.message : error}`
);
}
}
/**
* 대상 커넥션에 데이터 삽입
*/
async insertDataToConnection(
connectionId: number,
tableName: string,
data: Record<string, any>
): Promise<any> {
try {
logger.info(
`데이터 삽입 시작: connectionId=${connectionId}, table=${tableName}`
);
// connectionId가 0이면 메인 DB 사용
if (connectionId === 0) {
return await this.executeOnMainDatabase("insert", tableName, data);
}
// 외부 DB 연결 정보 가져오기
const connectionResult =
await ExternalDbConnectionService.getConnectionById(connectionId);
if (!connectionResult.success || !connectionResult.data) {
throw new Error(`커넥션을 찾을 수 없습니다: ${connectionId}`);
}
const connection = connectionResult.data;
// INSERT 쿼리 구성
const columns = Object.keys(data);
const values = Object.values(data);
const placeholders = values.map((_, index) => `$${index + 1}`).join(", ");
const query = `
INSERT INTO ${tableName} (${columns.join(", ")})
VALUES (${placeholders})
RETURNING *
`;
// 외부 DB에서 쿼리 실행
const result = await ExternalDbConnectionService.executeQuery(
connectionId,
query
);
if (!result.success || !result.data) {
throw new Error(result.message || "데이터 삽입 실패");
}
logger.info(`데이터 삽입 완료`);
return result.data[0] || result.data;
} catch (error) {
logger.error(`데이터 삽입 실패: ${error}`);
throw new Error(
`데이터 삽입 실패: ${error instanceof Error ? error.message : error}`
);
}
}
/**
* 🆕 대상 커넥션에 데이터 업데이트
*/
async updateDataToConnection(
connectionId: number,
tableName: string,
data: Record<string, any>,
conditions: Record<string, any>
): Promise<any> {
try {
logger.info(
`데이터 업데이트 시작: connectionId=${connectionId}, table=${tableName}`
);
// 자기 자신 테이블 작업 검증
if (connectionId === 0) {
const validationResult = await this.validateSelfTableOperation(
tableName,
"update",
[conditions]
);
if (!validationResult.isValid) {
throw new Error(
`자기 자신 테이블 업데이트 검증 실패: ${validationResult.error}`
);
}
}
// connectionId가 0이면 메인 DB 사용
if (connectionId === 0) {
return await this.executeOnMainDatabase(
"update",
tableName,
data,
conditions
);
}
// 외부 DB 연결 정보 가져오기
const connectionResult =
await ExternalDbConnectionService.getConnectionById(connectionId);
if (!connectionResult.success || !connectionResult.data) {
throw new Error(`커넥션을 찾을 수 없습니다: ${connectionId}`);
}
const connection = connectionResult.data;
// UPDATE 쿼리 구성
const setClause = Object.keys(data)
.map((key, index) => `${key} = $${index + 1}`)
.join(", ");
const whereClause = Object.keys(conditions)
.map(
(key, index) => `${key} = $${Object.keys(data).length + index + 1}`
)
.join(" AND ");
const query = `
UPDATE ${tableName}
SET ${setClause}
WHERE ${whereClause}
RETURNING *
`;
const queryParams = [
...Object.values(data),
...Object.values(conditions),
];
// 외부 DB에서 쿼리 실행
const result = await ExternalDbConnectionService.executeQuery(
connectionId,
query
);
if (!result.success || !result.data) {
throw new Error(result.message || "데이터 업데이트 실패");
}
logger.info(`데이터 업데이트 완료: ${result.data.length}`);
return result.data;
} catch (error) {
logger.error(`데이터 업데이트 실패: ${error}`);
throw new Error(
`데이터 업데이트 실패: ${error instanceof Error ? error.message : error}`
);
}
}
/**
* 🆕 대상 커넥션에서 데이터 삭제
*/
async deleteDataFromConnection(
connectionId: number,
tableName: string,
conditions: Record<string, any>,
maxDeleteCount: number = 100
): Promise<any> {
try {
logger.info(
`데이터 삭제 시작: connectionId=${connectionId}, table=${tableName}`
);
// 자기 자신 테이블 작업 검증
if (connectionId === 0) {
const validationResult = await this.validateSelfTableOperation(
tableName,
"delete",
[conditions]
);
if (!validationResult.isValid) {
throw new Error(
`자기 자신 테이블 삭제 검증 실패: ${validationResult.error}`
);
}
}
// WHERE 조건 필수 체크
if (!conditions || Object.keys(conditions).length === 0) {
throw new Error("DELETE 작업에는 반드시 WHERE 조건이 필요합니다.");
}
// connectionId가 0이면 메인 DB 사용
if (connectionId === 0) {
return await this.executeOnMainDatabase(
"delete",
tableName,
undefined,
conditions
);
}
// 외부 DB 연결 정보 가져오기
const connectionResult =
await ExternalDbConnectionService.getConnectionById(connectionId);
if (!connectionResult.success || !connectionResult.data) {
throw new Error(`커넥션을 찾을 수 없습니다: ${connectionId}`);
}
const connection = connectionResult.data;
// 먼저 삭제 대상 개수 확인 (안전장치)
const countQuery = `
SELECT COUNT(*) as count
FROM ${tableName}
WHERE ${Object.keys(conditions)
.map((key, index) => `${key} = $${index + 1}`)
.join(" AND ")}
`;
const countResult = await ExternalDbConnectionService.executeQuery(
connectionId,
countQuery
);
if (!countResult.success || !countResult.data) {
throw new Error(countResult.message || "삭제 대상 개수 조회 실패");
}
const deleteCount = parseInt(countResult.data[0]?.count || "0");
if (deleteCount > maxDeleteCount) {
throw new Error(
`삭제 대상이 ${deleteCount}건으로 최대 허용 개수(${maxDeleteCount})를 초과합니다.`
);
}
// DELETE 쿼리 실행
const deleteQuery = `
DELETE FROM ${tableName}
WHERE ${Object.keys(conditions)
.map((key, index) => `${key} = $${index + 1}`)
.join(" AND ")}
RETURNING *
`;
const result = await ExternalDbConnectionService.executeQuery(
connectionId,
deleteQuery
);
if (!result.success || !result.data) {
throw new Error(result.message || "데이터 삭제 실패");
}
logger.info(`데이터 삭제 완료: ${result.data.length}`);
return result.data;
} catch (error) {
logger.error(`데이터 삭제 실패: ${error}`);
throw new Error(
`데이터 삭제 실패: ${error instanceof Error ? error.message : error}`
);
}
}
/**
* 커넥션별 테이블 목록 조회
*/
async getTablesFromConnection(
connectionId: number
): Promise<MultiConnectionTableInfo[]> {
try {
logger.info(`테이블 목록 조회 시작: connectionId=${connectionId}`);
// connectionId가 0이면 메인 DB의 테이블 목록 반환
if (connectionId === 0) {
const tables = await this.tableManagementService.getTableList();
return tables.map((table) => ({
tableName: table.tableName,
displayName: table.displayName || table.tableName, // 라벨이 있으면 라벨 사용, 없으면 테이블명
columnCount: table.columnCount,
connectionId: 0,
connectionName: "메인 데이터베이스",
dbType: "postgresql",
}));
}
// 외부 DB 연결 정보 가져오기
const connectionResult =
await ExternalDbConnectionService.getConnectionById(connectionId);
if (!connectionResult.success || !connectionResult.data) {
throw new Error(`커넥션을 찾을 수 없습니다: ${connectionId}`);
}
const connection = connectionResult.data;
// 외부 DB의 테이블 목록 조회
const tablesResult =
await ExternalDbConnectionService.getTables(connectionId);
if (!tablesResult.success || !tablesResult.data) {
throw new Error(tablesResult.message || "테이블 조회 실패");
}
const tables = tablesResult.data;
// 성능 최적화: 컬럼 개수는 실제 필요할 때만 조회하도록 변경
return tables.map((table: any) => ({
tableName: table.table_name,
displayName: table.table_comment || table.table_name, // 라벨(comment)이 있으면 라벨 사용, 없으면 테이블명
columnCount: 0, // 성능을 위해 0으로 설정, 필요시 별도 API로 조회
connectionId: connectionId,
connectionName: connection.connection_name,
dbType: connection.db_type,
}));
} catch (error) {
logger.error(`테이블 목록 조회 실패: ${error}`);
throw new Error(
`테이블 목록 조회 실패: ${error instanceof Error ? error.message : error}`
);
}
}
/**
* 커넥션별 컬럼 정보 조회
*/
async getColumnsFromConnection(
connectionId: number,
tableName: string
): Promise<ColumnInfo[]> {
try {
logger.info(
`컬럼 정보 조회 시작: connectionId=${connectionId}, table=${tableName}`
);
// connectionId가 0이면 메인 DB의 컬럼 정보 반환
if (connectionId === 0) {
console.log(`🔍 메인 DB 컬럼 정보 조회 시작: ${tableName}`);
const columnsResult = await this.tableManagementService.getColumnList(
tableName,
1,
1000
);
console.log(
`✅ 메인 DB 컬럼 조회 성공: ${columnsResult.columns.length}`
);
return columnsResult.columns.map((column) => ({
columnName: column.columnName,
displayName: column.displayName || column.columnName, // 라벨이 있으면 라벨 사용, 없으면 컬럼명
dataType: column.dataType,
dbType: column.dataType, // dataType을 dbType으로 사용
webType: column.webType || "text", // webType 사용, 기본값 text
isNullable: column.isNullable === "Y",
isPrimaryKey: column.isPrimaryKey || false,
defaultValue: column.defaultValue,
maxLength: column.maxLength,
description: column.description,
}));
}
// 외부 DB 연결 정보 가져오기
const connectionResult =
await ExternalDbConnectionService.getConnectionById(connectionId);
if (!connectionResult.success || !connectionResult.data) {
throw new Error(`커넥션을 찾을 수 없습니다: ${connectionId}`);
}
const connection = connectionResult.data;
// 외부 DB의 컬럼 정보 조회
console.log(
`🔍 외부 DB 컬럼 정보 조회 시작: connectionId=${connectionId}, table=${tableName}`
);
const columnsResult = await ExternalDbConnectionService.getTableColumns(
connectionId,
tableName
);
if (!columnsResult.success || !columnsResult.data) {
console.error(`❌ 외부 DB 컬럼 조회 실패: ${columnsResult.message}`);
throw new Error(columnsResult.message || "컬럼 조회 실패");
}
const columns = columnsResult.data;
console.log(`✅ 외부 DB 컬럼 조회 성공: ${columns.length}`);
// MSSQL 컬럼 데이터 구조 디버깅
if (columns.length > 0) {
console.log(
`🔍 MSSQL 컬럼 데이터 구조 분석:`,
JSON.stringify(columns[0], null, 2)
);
console.log(`🔍 모든 컬럼 키들:`, Object.keys(columns[0]));
}
return columns.map((column: any) => {
// MSSQL과 PostgreSQL 데이터 타입 필드명이 다를 수 있음
// MSSQL: name, type, description (MSSQLConnector에서 alias로 지정)
// PostgreSQL: column_name, data_type, column_comment
const dataType =
column.type || // MSSQL (MSSQLConnector alias)
column.data_type || // PostgreSQL
column.DATA_TYPE ||
column.Type ||
column.dataType ||
column.column_type ||
column.COLUMN_TYPE ||
"unknown";
const columnName =
column.name || // MSSQL (MSSQLConnector alias)
column.column_name || // PostgreSQL
column.COLUMN_NAME ||
column.Name ||
column.columnName ||
column.COLUMN_NAME;
const columnComment =
column.description || // MSSQL (MSSQLConnector alias)
column.column_comment || // PostgreSQL
column.COLUMN_COMMENT ||
column.Description ||
column.comment;
console.log(`🔍 컬럼 매핑: ${columnName} - 타입: ${dataType}`);
return {
columnName: columnName,
displayName: columnComment || columnName, // 라벨(comment)이 있으면 라벨 사용, 없으면 컬럼명
dataType: dataType,
dbType: dataType,
webType: this.mapDataTypeToWebType(dataType),
isNullable:
column.nullable === "YES" || // MSSQL (MSSQLConnector alias)
column.is_nullable === "YES" || // PostgreSQL
column.IS_NULLABLE === "YES" ||
column.Nullable === true,
isPrimaryKey: column.is_primary_key || column.IS_PRIMARY_KEY || false,
defaultValue:
column.default_value || // MSSQL (MSSQLConnector alias)
column.column_default || // PostgreSQL
column.COLUMN_DEFAULT,
maxLength:
column.max_length || // MSSQL (MSSQLConnector alias)
column.character_maximum_length || // PostgreSQL
column.CHARACTER_MAXIMUM_LENGTH,
description: columnComment,
};
});
} catch (error) {
logger.error(`컬럼 정보 조회 실패: ${error}`);
throw new Error(
`컬럼 정보 조회 실패: ${error instanceof Error ? error.message : error}`
);
}
}
/**
* 🆕 자기 자신 테이블 작업 전용 검증
*/
async validateSelfTableOperation(
tableName: string,
operation: "update" | "delete",
conditions: any[]
): Promise<ValidationResult> {
try {
logger.info(
`자기 자신 테이블 작업 검증: table=${tableName}, operation=${operation}`
);
const warnings: string[] = [];
// 1. 기본 조건 체크
if (!conditions || conditions.length === 0) {
return {
isValid: false,
error: `자기 자신 테이블 ${operation.toUpperCase()} 작업에는 반드시 조건이 필요합니다.`,
};
}
// 2. DELETE 작업에 대한 추가 검증
if (operation === "delete") {
// 부정 조건 체크
const hasNegativeConditions = conditions.some((condition) => {
const conditionStr = JSON.stringify(condition).toLowerCase();
return (
conditionStr.includes("!=") ||
conditionStr.includes("not in") ||
conditionStr.includes("not exists")
);
});
if (hasNegativeConditions) {
return {
isValid: false,
error:
"자기 자신 테이블 삭제 시 부정 조건(!=, NOT IN, NOT EXISTS)은 위험합니다.",
};
}
// 조건 개수 체크
if (conditions.length < 2) {
warnings.push(
"자기 자신 테이블 삭제 시 WHERE 조건을 2개 이상 설정하는 것을 권장합니다."
);
}
}
// 3. UPDATE 작업에 대한 추가 검증
if (operation === "update") {
warnings.push("자기 자신 테이블 업데이트 시 무한 루프에 주의하세요.");
}
return {
isValid: true,
warnings: warnings.length > 0 ? warnings : undefined,
};
} catch (error) {
logger.error(`자기 자신 테이블 작업 검증 실패: ${error}`);
return {
isValid: false,
error: `검증 과정에서 오류가 발생했습니다: ${error instanceof Error ? error.message : error}`,
};
}
}
/**
* 🆕 메인 DB 작업 (connectionId = 0인 경우)
*/
async executeOnMainDatabase(
operation: "select" | "insert" | "update" | "delete",
tableName: string,
data?: Record<string, any>,
conditions?: Record<string, any>
): Promise<any> {
try {
logger.info(
`메인 DB 작업 실행: operation=${operation}, table=${tableName}`
);
switch (operation) {
case "select":
let query = `SELECT * FROM ${tableName}`;
const queryParams: any[] = [];
if (conditions && Object.keys(conditions).length > 0) {
const whereClause = Object.keys(conditions)
.map((key, index) => `${key} = $${index + 1}`)
.join(" AND ");
query += ` WHERE ${whereClause}`;
queryParams.push(...Object.values(conditions));
}
return await prisma.$queryRawUnsafe(query, ...queryParams);
case "insert":
if (!data) throw new Error("INSERT 작업에는 데이터가 필요합니다.");
const insertColumns = Object.keys(data);
const insertValues = Object.values(data);
const insertPlaceholders = insertValues
.map((_, index) => `$${index + 1}`)
.join(", ");
const insertQuery = `
INSERT INTO ${tableName} (${insertColumns.join(", ")})
VALUES (${insertPlaceholders})
RETURNING *
`;
const insertResult = await prisma.$queryRawUnsafe(
insertQuery,
...insertValues
);
return Array.isArray(insertResult) ? insertResult[0] : insertResult;
case "update":
if (!data) throw new Error("UPDATE 작업에는 데이터가 필요합니다.");
if (!conditions)
throw new Error("UPDATE 작업에는 조건이 필요합니다.");
const setClause = Object.keys(data)
.map((key, index) => `${key} = $${index + 1}`)
.join(", ");
const updateWhereClause = Object.keys(conditions)
.map(
(key, index) =>
`${key} = $${Object.keys(data).length + index + 1}`
)
.join(" AND ");
const updateQuery = `
UPDATE ${tableName}
SET ${setClause}
WHERE ${updateWhereClause}
RETURNING *
`;
const updateParams = [
...Object.values(data),
...Object.values(conditions),
];
return await prisma.$queryRawUnsafe(updateQuery, ...updateParams);
case "delete":
if (!conditions)
throw new Error("DELETE 작업에는 조건이 필요합니다.");
const deleteWhereClause = Object.keys(conditions)
.map((key, index) => `${key} = $${index + 1}`)
.join(" AND ");
const deleteQuery = `
DELETE FROM ${tableName}
WHERE ${deleteWhereClause}
RETURNING *
`;
return await prisma.$queryRawUnsafe(
deleteQuery,
...Object.values(conditions)
);
default:
throw new Error(`지원하지 않는 작업입니다: ${operation}`);
}
} catch (error) {
logger.error(`메인 DB 작업 실패: ${error}`);
throw new Error(
`메인 DB 작업 실패: ${error instanceof Error ? error.message : error}`
);
}
}
/**
* 데이터 타입을 웹 타입으로 매핑
*/
private mapDataTypeToWebType(dataType: string | undefined | null): string {
// 안전한 타입 검사
if (!dataType || typeof dataType !== "string") {
console.warn(`⚠️ 잘못된 데이터 타입: ${dataType}, 기본값 'text' 사용`);
return "text";
}
const lowerType = dataType.toLowerCase();
// PostgreSQL & MSSQL 타입 매핑
if (
lowerType.includes("int") ||
lowerType.includes("serial") ||
lowerType.includes("bigint")
) {
return "number";
}
if (
lowerType.includes("decimal") ||
lowerType.includes("numeric") ||
lowerType.includes("float") ||
lowerType.includes("money") ||
lowerType.includes("real")
) {
return "decimal";
}
if (lowerType.includes("date") && !lowerType.includes("time")) {
return "date";
}
if (
lowerType.includes("timestamp") ||
lowerType.includes("datetime") ||
lowerType.includes("datetime2")
) {
return "datetime";
}
if (lowerType.includes("bool") || lowerType.includes("bit")) {
return "boolean";
}
if (
lowerType.includes("text") ||
lowerType.includes("clob") ||
lowerType.includes("ntext")
) {
return "textarea";
}
// MSSQL 특수 타입들
if (
lowerType.includes("varchar") ||
lowerType.includes("nvarchar") ||
lowerType.includes("char")
) {
return "text";
}
return "text";
}
}

View File

@@ -12,6 +12,8 @@ export interface ColumnTypeInfo {
columnName: string;
displayName: string;
dataType: string; // DB 데이터 타입 (varchar, integer 등)
dbType?: string; // DB 타입 (추가됨)
webType?: string; // 웹 타입 (추가됨)
inputType: string; // 입력 타입 (text, number, date, code, entity, select, checkbox, radio)
detailSettings: string;
description: string;

View File

@@ -23,8 +23,8 @@ export class PasswordEncryption {
// 암호화 키 생성 (SECRET_KEY를 해시하여 32바이트 키 생성)
const key = crypto.scryptSync(this.SECRET_KEY, "salt", 32);
// 암호화 객체 생성
const cipher = crypto.createCipher("aes-256-cbc", key);
// 암호화 객체 생성 (IV를 명시적으로 사용)
const cipher = crypto.createCipheriv(this.ALGORITHM, key, iv);
// 암호화 실행
let encrypted = cipher.update(password, "utf8", "hex");
@@ -57,14 +57,37 @@ export class PasswordEncryption {
// 암호화 키 생성 (암호화 시와 동일)
const key = crypto.scryptSync(this.SECRET_KEY, "salt", 32);
// 복호화 객체 생성
const decipher = crypto.createDecipher("aes-256-cbc", key);
try {
// 새로운 방식: createDecipheriv 사용 (IV 명시적 사용)
const decipher = crypto.createDecipheriv(this.ALGORITHM, key, iv);
let decrypted = decipher.update(encrypted, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
} catch (newFormatError: unknown) {
const errorMessage =
newFormatError instanceof Error
? newFormatError.message
: String(newFormatError);
console.warn(
"새로운 복호화 방식 실패, 기존 방식으로 시도:",
errorMessage
);
// 복호화 실행
let decrypted = decipher.update(encrypted, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
try {
// 기존 방식: createDecipher 사용 (하위 호환성)
const decipher = crypto.createDecipher("aes-256-cbc", key);
let decrypted = decipher.update(encrypted, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
} catch (oldFormatError: unknown) {
const oldErrorMessage =
oldFormatError instanceof Error
? oldFormatError.message
: String(oldFormatError);
console.error("기존 복호화 방식도 실패:", oldErrorMessage);
throw oldFormatError;
}
}
} catch (error) {
console.error("Password decryption failed:", error);
throw new Error("비밀번호 복호화에 실패했습니다.");