-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBConnect_impl.hpp
236 lines (204 loc) · 4.6 KB
/
DBConnect_impl.hpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#pragma once
CDBConnect::CDBConnect() {}
CDBConnect::~CDBConnect() {}
CDBConnect::CDBConnect(WSTR dbPath)
{
Open(dbPath);
}
I32 CDBConnect::Open(WSTR dbPath)
{
I32 nRet = CODE_ERROR;
CHECK_BEGIN
STR path = wstring_to_utf8(dbPath);
CHECK_BREAK(path.empty());
sqlite3* pDb = NULL;
CHECK_BREAK(SQLITE_OK != sqlite3_open(path.c_str(), &pDb));
m_DBHandle = std::make_shared<PTR_STORAGE<sqlite3, DeleteDbPtr>>(pDb);
nRet = CODE_OK;
CHECK_END
return nRet;
}
BOOL CDBConnect::IsOpen()
{
return m_DBHandle != NULL;
}
void CDBConnect::Close()
{
if (m_DBHandle)
{
m_DBHandle.reset();
}
}
I32 CDBConnect::ExecSql(const WSTR sql)
{
I32 nRet = CODE_ERROR;
CHECK_BEGIN
CHECK_BREAK(sql.empty() || !IsOpen());
STR str = wstring_to_utf8(sql);
CHECK_BREAK(str.empty());
int nError = sqlite3_exec(GetOriginalPtr(m_DBHandle), str.c_str(), NULL, NULL, NULL);
CHECK_BREAK(SQLITE_OK != nError);
nRet = CODE_OK;
CHECK_END
return nRet;
}
Sql_Stmt_Ptr CDBConnect::PrepareSql(const WSTR sql)
{
Sql_Stmt_Ptr Ret;
CHECK_BEGIN
CHECK_BREAK(sql.empty() || !IsOpen());
STR str = wstring_to_utf8(sql);
CHECK_BREAK(str.empty());
sqlite3_stmt* pStmt = NULL;
CHECK_BREAK(SQLITE_OK != sqlite3_prepare_v2(GetOriginalPtr(m_DBHandle), str.c_str(), (int)str.size(), &pStmt, NULL));
CHECK_BREAK(!pStmt);
Ret = std::make_shared<PTR_STORAGE<sqlite3_stmt, DeleteStmtPtr>>(pStmt);
CHECK_END
return Ret;
}
template<class ...T>
I32 CDBConnect::Query(const WSTR sql, CRecordList<T...>& outRecords)
{
I32 nRet = CODE_ERROR;
CHECK_BEGIN
Sql_Stmt_Ptr stmt = PrepareSql(sql);
nRet = QueryWithCount(stmt, outRecords, -1);
CHECK_END
return nRet;
}
template<class ...T>
I32 CDBConnect::QueryStmt(Sql_Stmt_Ptr& stmt, CRecordList<T...>& outRecords)
{
I32 nRet = CODE_ERROR;
CHECK_BEGIN
nRet = QueryWithCount(stmt, outRecords, -1);
CHECK_END
return nRet;
}
template<class ...T>
I32 CDBConnect::QueryWithCount(HQUERY& hQ, CRecordList<T...>& outRecords, int nCount)
{
typedef CRecordList<T...> TList;
I32 iRet = CODE_ERROR;
CHECK_BEGIN
sqlite3_stmt* pStmt = GetOriginalPtr(hQ);
CHECK_BREAK(!pStmt);
int nDone = SQLITE_ROW;
while (SQLITE_ROW == nDone && nCount--)
{
nDone = sqlite3_step(pStmt);
CHECK_BREAK(SQLITE_ROW != nDone);
typename TList::RECORD_ELEMENT t;
if (t.Parse(hQ))
{
outRecords.push_back(t);
}
}
if (nCount > 0)
{
hQ.reset();
}
iRet = CODE_OK;
CHECK_END
return iRet;
}
I32 CDBConnect::TransactionBegin()
{
I32 nRet = CODE_ERROR;
CHECK_BEGIN
CHECK_BREAK(CODE_OK != ExecSql(L"BEGIN TRANSACTION;"));
nRet = CODE_OK;
CHECK_END
return nRet;
}
I32 CDBConnect::TransactionCommit()
{
I32 nRet = CODE_ERROR;
CHECK_BEGIN
CHECK_BREAK(CODE_OK != ExecSql(L"COMMIT;"));
nRet = CODE_OK;
CHECK_END
return nRet;
}
I32 CDBConnect::TansactionRollback()
{
I32 nRet = CODE_ERROR;
CHECK_BEGIN
CHECK_BREAK(CODE_OK != ExecSql(L"ROLLBACK;"));
nRet = CODE_OK;
CHECK_END
return nRet;
}
I32 CDBConnect::ExecStmt(Sql_Stmt_Ptr& stmt)
{
I32 iRet = CODE_ERROR;
CHECK_BEGIN
sqlite3_stmt* pStmt = GetOriginalPtr(stmt);
CHECK_BREAK(!pStmt);
int nError = sqlite3_step(pStmt);
CHECK_BREAK(SQLITE_DONE != nError);
iRet = CODE_OK;
CHECK_END
return iRet;
}
BOOL CDBConnect::IsEffectStmt(Sql_Stmt_Ptr& stmt)
{
BOOL bRet = FALSE;
CHECK_BEGIN
sqlite3_stmt* pStmt = GetOriginalPtr(stmt);
CHECK_BREAK(!pStmt);
bRet = TRUE;
CHECK_END
return bRet;
}
template<class ...T>
HQUERY CDBConnect::XQuery(const WSTR sql, CRecordList<T...>& outRecords, int nCount)
{
Sql_Stmt_Ptr stmtRet;
CHECK_BEGIN
stmtRet = PrepareSql(sql);
nRet = QueryWithCount(stmtRet, outRecords, nCount);
CHECK_END
return stmtRet;
}
template<class ...T>
I32 CDBConnect::XQuery(HQUERY& hQ, CRecordList<T...>& outRecords, int nCount)
{
I32 iRet = CODE_ERROR;
CHECK_BEGIN
iRet = QueryWithCount(hQ, outRecords, nCount);
CHECK_END
return iRet;
}
template<class T>
int CDBConnect::BindStmt(Sql_Stmt_Ptr& stmt, int nSet, T val)
{
I32 iRet = CODE_ERROR;
CHECK_BEGIN
iRet = ToBind(stmt, nSet, val);
CHECK_END
return iRet;
}
int CDBConnect::StmtReset(Sql_Stmt_Ptr& stmt)
{
I32 iRet = CODE_ERROR;
CHECK_BEGIN
sqlite3_stmt* pStmt = GetOriginalPtr(stmt);
CHECK_BREAK(!pStmt);
CHECK_BREAK(SQLITE_OK != sqlite3_reset(pStmt));
iRet = CODE_OK;
CHECK_END
return iRet;
}
LIST<WSTR> CDBConnect::GetTablesNames()
{
LIST<WSTR> strRet;
CRecordList<WSTR> records;
WSTR strSql = L"select tbl_name from sqlite_master where type = 'table';";
Query(strSql, records);
for (CRecordList<WSTR>::iterator it = records.begin(); it != records.end();++it)
{
strRet.push_back(it->m_1);
}
return strRet;
}