-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbSQLite.cpp
479 lines (404 loc) · 10.1 KB
/
DbSQLite.cpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*----------------------------------------------------------------------------*
* DbSQLite.h
*
* 6 APR 2005
*
* This source code is derived from the SQLiteWrapper source produced by
* [email protected]. I altered the original source while
* packaging it for use with Microsoft MFC.
*
* The primary objective in this exercise was to make the wrapper
* suitable for both MCBS and Unicode because Unicdoe is native to the
* Windows CE OS. There are three key differences between this wrapper
* and the author's original source.
*
* First, all std::string variables were converted to Microsoft's generic
* string pointers using LPCTSTR. This should be familiar to developer's
* accustomed to working with MFC. This also means that you should rely
* upon the the Microsoft TEXT or _T macros for hard-coded character strings.
*
* Second, I have change the class SQLiteStatement to CSqlStatement and
* SQLiteWrapper to CDbSQLite. This was primarily a matter of preference
* since most MFC developer's recoginize Microsoft's convention for the
* CFunction nomenclature.
*
* Finally, I have added the header SQLite3i.h with typedefs to the various
* sqlite3.h functions. These type definitions are "internal" accessors
* to the sqlite3 functions utilizing either the UTF-8 or UTF-16 variation
* as appropriate.
*
* The remaining comments at the beginning of this file are the author's
* original copyright message.
*----------------------------------------------------------------------------*
* SQLiteWrapper.h
*
* Copyright (C) 2004 René Nyffenegger
*
* This source code is provided 'as-is', without any express or implied
* warranty. In no event will the author be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this source code must not be misrepresented; you must not
* claim that you wrote the original source code. If you use this source code
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original source code.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* René Nyffenegger [email protected]
*----------------------------------------------------------------------------*/
#include "stdafx.h"
#include "DbSQLite.h"
#include "telar.h"
//
// Default ctor.
//
CSqlStatement::CSqlStatement()
{
m_stmt = NULL;
}
CSqlStatement::CSqlStatement(LPCTSTR statement, sqlite3* db)
{
m_stmt = NULL;
int rc = _sqlite3_prepare(db, statement, -1, &m_stmt, 0 );
if (rc != SQLITE_OK)
{
m_stmt = NULL;
}
}
CSqlStatement::~CSqlStatement()
{
if (m_stmt) _sqlite3_finalize(m_stmt);
}
bool
CSqlStatement::Bind(int pos_zero_indexed, LPCTSTR value)
{
bool fResult = TRUE;
CString v = CString(value);
int rc = _sqlite3_bind_text(m_stmt, pos_zero_indexed+1,
v, v.GetLength(), SQLITE_TRANSIENT);
if (rc != SQLITE_OK )
{
fResult = false;
}
return fResult;
}
bool
CSqlStatement::Bind(int pos_zero_indexed, double value)
{
bool fResult = TRUE;
int rc = _sqlite3_bind_double( m_stmt, pos_zero_indexed+1,value );
if (rc != SQLITE_OK )
{
fResult = false;
}
return fResult;
}
bool
CSqlStatement::Bind(int pos_zero_indexed, int value)
{
bool fResult = TRUE;
int rc = _sqlite3_bind_int( m_stmt, pos_zero_indexed+1, value );
if (rc != SQLITE_OK )
{
fResult = false;
}
return fResult;
}
bool
CSqlStatement::BindNull(int pos_zero_indexed)
{
bool fResult = TRUE;
int rc = _sqlite3_bind_null( m_stmt, pos_zero_indexed+1);
if (rc != SQLITE_OK )
{
fResult = false;
}
return fResult;
}
bool
CSqlStatement::Execute()
{
CString szMessage;
CString szCaption = _T("CSqlStatement::Execute");
int rc;
do
{
rc = _sqlite3_step(m_stmt);
}
while (rc == SQLITE_BUSY && ::MessageBox(AfxGetMainWnd()->m_hWnd, "Unable to access the index file. Another user seems to perform a bigger operation on it. Try again in a few moments.", "Index file locked", MB_RETRYCANCEL) == IDRETRY);
if ( rc == SQLITE_BUSY )
{
szMessage = _T("SQLITE_BUSY");
DF2(">>>>>> %s -- %s <<<<<<", szCaption, szMessage);
#if defined(_DEBUG)
::MessageBox(0, szMessage, szCaption, MB_OK);
#endif
return false;
}
if ( rc == SQLITE_ERROR )
{
szMessage = _T("SQLITE_ERROR");
DF2(">>>>>> %s -- %s <<<<<<", szCaption, szMessage);
#if defined(_DEBUG)
::MessageBox(0, szMessage, szCaption, MB_OK);
#endif
return false;
}
if ( rc == SQLITE_MISUSE )
{
szMessage = _T("SQLITE_MISUSE");
DF2(">>>>>> %s -- %s <<<<<<", szCaption, szMessage);
#if defined(_DEBUG)
::MessageBox(0, szMessage, szCaption, MB_OK);
#endif
return false;
}
if ( rc != SQLITE_DONE )
{
//_sqlite3_reset(m_stmt);
return false;
}
// tm 24.11.05 _sqlite3_reset(m_stmt);
return true;
}
int
CSqlStatement::Fields()
{
return _sqlite3_column_count(m_stmt);
}
LPCTSTR
CSqlStatement::FieldName(int pos_zero_indexed)
{
m_szText = LPCTSTR(_sqlite3_column_name(m_stmt, pos_zero_indexed));
return m_szText;
}
CSqlStatement::EDataType
CSqlStatement::DataType(int pos_zero_indexed)
{
return EDataType(_sqlite3_column_type(m_stmt, pos_zero_indexed));
}
LPCTSTR
CSqlStatement::FieldType(int pos_zero_indexed)
{
int dt = _sqlite3_column_type(m_stmt, pos_zero_indexed);
switch (dt)
{
case SQLITE_INTEGER :
m_szText = _T("INTEGER");
break;
case SQLITE_FLOAT:
m_szText = _T("FLOAT");
break;
case SQLITE_TEXT :
m_szText = _T("TEXT");
break;
case SQLITE_BLOB :
m_szText = _T("BLOBL");
break;
case SQLITE_NULL :
m_szText = _T("NULL");
break;
}
return m_szText;
}
int
CSqlStatement::ValueInt(int pos_zero_indexed)
{
return _sqlite3_column_int(m_stmt, pos_zero_indexed);
}
LPCTSTR
CSqlStatement::ValueString(int pos_zero_indexed)
{
m_szText.Format(_T("%s"), _sqlite3_column_text(m_stmt, pos_zero_indexed));
return m_szText;
}
bool
CSqlStatement::RestartSelect()
{
_sqlite3_reset(m_stmt);
return true;
}
bool
CSqlStatement::Reset()
{
int rc = _sqlite3_step(m_stmt);
_sqlite3_reset(m_stmt);
if ( rc == SQLITE_ROW ) return true;
return false;
}
bool
CSqlStatement::NextRow()
{
CString szError;
int rc;
do
{
rc = _sqlite3_step(m_stmt);
}
while (rc == SQLITE_BUSY && ::MessageBox(AfxGetMainWnd()->m_hWnd, "Unable to access the index file. Another user seems to perform a bigger operation on it. Try again in a few moments.", "Index file locked", MB_RETRYCANCEL) == IDRETRY);
if ( rc == SQLITE_ROW )
{
return true;
}
if ( rc == SQLITE_DONE )
{
// tm 24.11.05 _sqlite3_reset(m_stmt);
return false;
}
else if ( rc == SQLITE_MISUSE )
{
szError = _T("CSqlStatement::NextRow SQLITE_MISUSE");
}
else if ( rc == SQLITE_BUSY )
{
szError = _T("CSqlStatement::NextRow SQLITE_BUSY");
}
else if ( rc == SQLITE_ERROR )
{
szError = _T("CSqlStatement::NextRow SQLITE_ERROR");
}
DF1(">>>>>> CSqlStatement::NextRow() -- %s <<<<<<", szError);
#if defined(_DEBUG)
::MessageBox(0, szError, 0, 0);
#endif
return false;
}
//
// ctor
//
CDbSQLite::CDbSQLite()
{
m_db = NULL;
}
CDbSQLite::~CDbSQLite()
{
if (m_db)
{
sqlite3_close(m_db);
m_db = NULL;
}
}
bool
CDbSQLite::Open(LPCTSTR db_file)
{
bool fResult = TRUE;
m_szName = db_file;
int rc = _sqlite3_open(db_file, &m_db);
if ( rc != SQLITE_OK )
{
fResult = false;
}
return fResult;
}
void
CDbSQLite::Close()
{
if (m_db)
{
sqlite3_close(m_db);
m_db = NULL;
}
}
bool
CDbSQLite::DirectStatement(LPCTSTR stmt)
{
CSqlStatement* pStatement = this->Statement(stmt);
bool bResult = pStatement->Execute();
delete pStatement;
return bResult;
}
bool
CDbSQLite::SelectStatement(LPCTSTR stmt, ResultTable& res)
{
LPSTR lpsz = NULL;
int len = 0;
int rc = 0;
char* errmsg = NULL;
bool fResult = TRUE;
res.reset();
len = (int)_tcslen(stmt);
lpsz = (LPSTR) LocalAlloc(LMEM_ZEROINIT, len+2);
// A UTF-16 version of sqlite_exec does not exist at this time,
// so we will convert the string if necessary.
#if defined(_UNICODE) || defined(UNICODE)
::WideCharToMultiByte(CP_ACP, 0, stmt, -1, lpsz, len, 0, 0);
#else
memcpy(lpsz, stmt, len);
#endif
rc = sqlite3_exec(m_db,lpsz,SelectCallback,static_cast<void*>(&res),&errmsg);
if ( rc != SQLITE_OK )
{
fResult = false;
}
LocalFree(lpsz);
return fResult;
}
// TODO parameter p_col_names
int
CDbSQLite::SelectCallback(void *p_data, int num_fields, char **p_fields, char** /*p_col_names*/)
{
ResultTable* res = reinterpret_cast<ResultTable*>(p_data);
ResultRecord record;
for ( int i=0; i < num_fields; i++ )
{
CString szField = CString(p_fields[i]);
record.m_fields.push_back(szField);
}
res->m_records.push_back(record);
return 0;
}
CSqlStatement*
CDbSQLite::Statement(LPCTSTR statement)
{
CSqlStatement* stmt = NULL;
stmt = new CSqlStatement(statement, m_db);
return stmt;
}
LPCTSTR
CDbSQLite::LastError()
{
m_szText = (LPCTSTR)_sqlite3_errmsg(m_db);
return m_szText;
}
bool
CDbSQLite::Begin()
{
m_szText = _T("begin");
return DirectStatement(m_szText);
}
bool
CDbSQLite::Commit()
{
m_szText = _T("commit");
return DirectStatement(m_szText);
}
bool
CDbSQLite::Rollback()
{
m_szText = _T("rollback");
return DirectStatement(m_szText);
}
bool CDbSQLite::GetAutoCommit()
{
return (sqlite3_get_autocommit(m_db) != 0);
}
int CDbSQLite::GetChanges()
{
return sqlite3_changes(m_db);
}
CString CDbSQLite::GetName()
{
return m_szName;
}
void CDbSQLite::SetBusyTimeout(int ms)
{
sqlite3_busy_timeout(m_db, ms);
}