-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSqlClientManager.cpp
More file actions
62 lines (57 loc) · 1.75 KB
/
Copy pathSqlClientManager.cpp
File metadata and controls
62 lines (57 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "SqlClientManager.h"
#include "SqlClient.h" // SqlClient 헤더 파일 포함
#include <fstream> // std::ifstream
#include <sstream> // std::stringstream
#include <memory> // std::unique_ptr
#include <iostream> // std::cerr
#include <mariadb/conncpp.hpp>
#include "LogHelper.h"
void SqlClientManager::init()
{
// SqlClient 객체를 생성하여 thread_specific_ptr에 저장합니다.
sqlClientPtr.reset(new SqlClient());
if(!create_tables()) {
// 테이블 생성 실패 시 처리
LOG.error("Failed to create tables.");
}
}
bool SqlClientManager::create_tables()
{
try {
// create_tables.sql 파일을 읽어 SQL 문자열로 만듭니다.
std::ifstream file("SQL/generated/create_tables.sql");
if (file)
{
std::stringstream buffer;
buffer << file.rdbuf();
std::string sql = buffer.str();
// SqlClient의 Connection을 통해 SQL 실행
sql::Connection* conn = sqlClientPtr->getConnection();
if (conn)
{
std::unique_ptr<sql::Statement> stmt(conn->createStatement());
stmt->execute(sql);
}
}
else
{
// 파일이 없을 경우 로깅 등 처리
// 예: std::cerr << "create_tables.sql 파일을 찾을 수 없습니다." << std::endl;
LOG.error("create_tables.sql file not found.");
return false;
}
}
catch (sql::SQLException& e)
{
// SQL 예외 처리
LOG.error("SQLException: " + std::string(e.what()));
return false;
}
catch (std::exception& e)
{
// 일반 예외 처리
LOG.error("Exception: " + std::string(e.what()));
return false;
}
return true;
}