atrade/src/main/kotlin/database/DatabaseFactory.kt

69 lines
2.4 KiB
Kotlin
Raw Normal View History

2026-01-10 18:16:50 +09:00
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.javatime.datetime
import org.jetbrains.exposed.sql.transactions.transaction
import java.io.File
import java.time.LocalDateTime
// 1. 앱 설정 테이블
object ConfigTable : Table("app_config") {
val id = integer("id").autoIncrement()
val appKey = varchar("app_key", 255)
val secretKey = varchar("secret_key", 255)
val accountNo = varchar("account_no", 20)
val isSimulation = bool("is_simulation")
val modelPath = varchar("model_path", 512).default("") // 이 라인이 있어야 합니다.
override val primaryKey = PrimaryKey(id)
}
// 2. 거래 내역 테이블 (대량 데이터용)
object TradeLogTable : Table("trade_logs") {
val id = long("id").autoIncrement()
val stockCode = varchar("stock_code", 20) // 종목코드
val stockName = varchar("stock_name", 50) // 종목명
val tradeType = varchar("trade_type", 10) // 매수/매도
val price = double("price") // 체결가
val quantity = integer("quantity") // 수량
val timestamp = datetime("timestamp") // 거래 시간
val logMessage = text("log_message") // Ollama의 판단 근거 등 상세 정보
override val primaryKey = PrimaryKey(id)
}
object DatabaseFactory {
fun init() {
val dbPath =File("db/autotrade_db").absolutePath
// 드라이버를 org.h2.Driver로 설정
Database.connect(
"jdbc:h2:$dbPath;DB_CLOSE_DELAY=-1;",
driver = "org.h2.Driver"
)
transaction {
SchemaUtils.create(ConfigTable, TradeLogTable)
}
}
fun saveTradeLog(code: String, name: String, type: String, price: Double, qty: Int, msg: String) {
transaction {
TradeLogTable.insert {
it[stockCode] = code
it[stockName] = name
it[tradeType] = type
it[TradeLogTable.price] = price
it[quantity] = qty
it[timestamp] = LocalDateTime.now()
it[logMessage] = msg
}
}
}
// fun fetchRecentLogs(limit: Int = 50): List<TradeLog> {
// return transaction {
// TradeLogTable.selectAll()
// .orderBy(TradeLogTable.timestamp to SortOrder.DESC)
// .limit(limit)
// .map {
// // ResultRow를 객체로 변환
// }
// }
// }
}