const { getConnection } = require('./connection'); // 데이터 조회 async function getAllData(tableName = 'API_DATA') { let connection; try { connection = await getConnection(); const result = await connection.execute( `SELECT * FROM ${tableName} ORDER BY CREATED_AT DESC` ); return result.rows; } catch (err) { console.error('데이터 조회 실패:', err); throw err; } finally { if (connection) { await connection.close(); } } } // 데이터 조회 (ID로) async function getDataById(id, tableName = 'API_DATA') { let connection; try { connection = await getConnection(); const result = await connection.execute( `SELECT * FROM ${tableName} WHERE ID = :id`, [id] ); return result.rows[0]; } catch (err) { console.error('데이터 조회 실패:', err); throw err; } finally { if (connection) { await connection.close(); } } } // 데이터 삽입 async function insertData(data, tableName = 'API_DATA') { let connection; try { connection = await getConnection(); const result = await connection.execute( `INSERT INTO ${tableName} (NAME, DESCRIPTION, DATA_VALUE) VALUES (:name, :description, :dataValue)`, { name: data.name, description: data.description, dataValue: data.dataValue }, { autoCommit: true } ); return result.rowsAffected; } catch (err) { console.error('데이터 삽입 실패:', err); throw err; } finally { if (connection) { await connection.close(); } } } // 데이터 업데이트 async function updateData(id, data, tableName = 'API_DATA') { let connection; try { connection = await getConnection(); const result = await connection.execute( `UPDATE ${tableName} SET NAME = :name, DESCRIPTION = :description, DATA_VALUE = :dataValue, UPDATED_AT = CURRENT_TIMESTAMP WHERE ID = :id`, { id: id, name: data.name, description: data.description, dataValue: data.dataValue }, { autoCommit: true } ); return result.rowsAffected; } catch (err) { console.error('데이터 업데이트 실패:', err); throw err; } finally { if (connection) { await connection.close(); } } } // 데이터 삭제 async function deleteData(id, tableName = 'API_DATA') { let connection; try { connection = await getConnection(); const result = await connection.execute( `DELETE FROM ${tableName} WHERE ID = :id`, [id], { autoCommit: true } ); return result.rowsAffected; } catch (err) { console.error('데이터 삭제 실패:', err); throw err; } finally { if (connection) { await connection.close(); } } } // 테이블 생성 async function createTable(tableName = 'API_DATA') { let connection; try { connection = await getConnection(); // 테이블이 존재하는지 확인 const checkTable = await connection.execute( `SELECT COUNT(*) as count FROM user_tables WHERE table_name = :tableName`, [tableName.toUpperCase()] ); if (checkTable.rows[0][0] === 0) { // 테이블 생성 await connection.execute( `CREATE TABLE ${tableName} ( ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, NAME VARCHAR2(100) NOT NULL, DESCRIPTION VARCHAR2(500), DATA_VALUE CLOB, CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UPDATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP )`, [], { autoCommit: true } ); console.log(`테이블 ${tableName}이 생성되었습니다.`); } else { console.log(`테이블 ${tableName}이 이미 존재합니다.`); } } catch (err) { console.error('테이블 생성 실패:', err); throw err; } finally { if (connection) { await connection.close(); } } } module.exports = { getAllData, getDataById, insertData, updateData, deleteData, createTable };