]> Creatis software - creaImageIO.git/blob - src/CppSQLite3.h
#3185 creaImageIO Feature New Normal - Clean code
[creaImageIO.git] / src / CppSQLite3.h
1 ////////////////////////////////////////////////////////////////////////////////
2
3 // CppSQLite3 - A C++ wrapper around the SQLite3 embedded database library.
4
5 //
6
7 // Copyright (c) 2004 Rob Groves. All Rights Reserved. rob.groves@btinternet.com
8
9 // 
10
11 // Permission to use, copy, modify, and distribute this software and its
12
13 // documentation for any purpose, without fee, and without a written
14
15 // agreement, is hereby granted, provided that the above copyright notice, 
16
17 // this paragraph and the following two paragraphs appear in all copies, 
18
19 // modifications, and distributions.
20
21 //
22
23 // IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
24
25 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
26
27 // PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
28
29 // EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 //
32
33 // THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
34
35 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
36
37 // PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
38
39 // ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO OBLIGATION
40
41 // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
42
43 //
44
45 // V3.0         03/08/2004      -Initial Version for sqlite3
46
47 //
48
49 // V3.1         16/09/2004      -Implemented getXXXXField using sqlite3 functions
50
51 //                                              -Added CppSQLiteDB3::tableExists()
52
53 ////////////////////////////////////////////////////////////////////////////////
54
55 #ifndef _CppSQLite3_H_
56
57 #define _CppSQLite3_H_
58
59
60
61 #include "sqlite3.h"
62
63 #include <cstdio>
64
65 #include <cstring>
66
67
68
69 #define CPPSQLITE_ERROR 1000
70
71
72
73 class CppSQLite3Exception
74
75 {
76
77 public:
78
79
80
81     CppSQLite3Exception(const int nErrCode,
82
83                         char* szErrMess,
84
85                         bool bDeleteMsg=true);
86
87   CppSQLite3Exception(const int nErrCode,
88
89                       const char* szErrMess,
90
91                       bool bDeleteMsg=true);
92
93   
94
95     CppSQLite3Exception(const CppSQLite3Exception&  e);
96
97
98
99     virtual ~CppSQLite3Exception();
100
101
102
103     const int errorCode() { return mnErrCode; }
104
105
106
107     const char* errorMessage() { return mpszErrMess; }
108
109
110
111     static const char* errorCodeAsString(int nErrCode);
112
113
114
115 private:
116
117
118
119     int mnErrCode;
120
121     char* mpszErrMess;
122
123 };
124
125
126
127
128
129 class CppSQLite3Buffer
130
131 {
132
133 public:
134
135
136
137     CppSQLite3Buffer();
138
139
140
141     ~CppSQLite3Buffer();
142
143
144
145     const char* format(const char* szFormat, ...);
146
147
148
149     operator const char*() { return mpBuf; }
150
151
152
153     void clear();
154
155
156
157 private:
158
159
160
161     char* mpBuf;
162
163 };
164
165
166
167
168
169 class CppSQLite3Binary
170
171 {
172
173 public:
174
175
176
177     CppSQLite3Binary();
178
179
180
181     ~CppSQLite3Binary();
182
183
184
185     void setBinary(const unsigned char* pBuf, int nLen);
186
187     void setEncoded(const unsigned char* pBuf);
188
189
190
191     const unsigned char* getEncoded();
192
193     const unsigned char* getBinary();
194
195
196
197     int getBinaryLength();
198
199
200
201     unsigned char* allocBuffer(int nLen);
202
203
204
205     void clear();
206
207
208
209 private:
210
211
212
213     unsigned char* mpBuf;
214
215     int mnBinaryLen;
216
217     int mnBufferLen;
218
219     int mnEncodedLen;
220
221     bool mbEncoded;
222
223 };
224
225
226
227
228
229 class CppSQLite3Query
230
231 {
232
233 public:
234
235
236
237     CppSQLite3Query();
238
239
240
241     CppSQLite3Query(const CppSQLite3Query& rQuery);
242
243
244
245     CppSQLite3Query(sqlite3* pDB,
246
247                                 sqlite3_stmt* pVM,
248
249                 bool bEof,
250
251                 bool bOwnVM=true);
252
253
254
255     CppSQLite3Query& operator=(const CppSQLite3Query& rQuery);
256
257
258
259     virtual ~CppSQLite3Query();
260
261
262
263     int numFields();
264
265
266
267     int fieldIndex(const char* szField);
268
269     const char* fieldName(int nCol);
270
271
272
273     const char* fieldDeclType(int nCol);
274
275     int fieldDataType(int nCol);
276
277
278
279     const char* fieldValue(int nField);
280
281     const char* fieldValue(const char* szField);
282
283
284
285     int getIntField(int nField, int nNullValue=0);
286
287     int getIntField(const char* szField, int nNullValue=0);
288
289
290
291     double getFloatField(int nField, double fNullValue=0.0);
292
293     double getFloatField(const char* szField, double fNullValue=0.0);
294
295
296
297     const char* getStringField(int nField, const char* szNullValue="");
298
299     const char* getStringField(const char* szField, const char* szNullValue="");
300
301
302
303     const unsigned char* getBlobField(int nField, int& nLen);
304
305     const unsigned char* getBlobField(const char* szField, int& nLen);
306
307
308
309     bool fieldIsNull(int nField);
310
311     bool fieldIsNull(const char* szField);
312
313
314
315     bool eof();
316
317
318
319     void nextRow();
320
321
322
323     void finalize();
324
325
326
327 private:
328
329
330
331     void checkVM();
332
333
334
335         sqlite3* mpDB;
336
337     sqlite3_stmt* mpVM;
338
339     bool mbEof;
340
341     int mnCols;
342
343     bool mbOwnVM;
344
345 };
346
347
348
349
350
351 class CppSQLite3Table
352
353 {
354
355 public:
356
357
358
359     CppSQLite3Table();
360
361
362
363     CppSQLite3Table(const CppSQLite3Table& rTable);
364
365
366
367     CppSQLite3Table(char** paszResults, int nRows, int nCols);
368
369
370
371     virtual ~CppSQLite3Table();
372
373
374
375     CppSQLite3Table& operator=(const CppSQLite3Table& rTable);
376
377
378
379     int numFields();
380
381
382
383     int numRows();
384
385
386
387     const char* fieldName(int nCol);
388
389
390
391     const char* fieldValue(int nField);
392
393     const char* fieldValue(const char* szField);
394
395
396
397     int getIntField(int nField, int nNullValue=0);
398
399     int getIntField(const char* szField, int nNullValue=0);
400
401
402
403     double getFloatField(int nField, double fNullValue=0.0);
404
405     double getFloatField(const char* szField, double fNullValue=0.0);
406
407
408
409     const char* getStringField(int nField, const char* szNullValue="");
410
411     const char* getStringField(const char* szField, const char* szNullValue="");
412
413
414
415     bool fieldIsNull(int nField);
416
417     bool fieldIsNull(const char* szField);
418
419
420
421     void setRow(int nRow);
422
423
424
425     void finalize();
426
427
428
429 private:
430
431
432
433     void checkResults();
434
435
436
437     int mnCols;
438
439     int mnRows;
440
441     int mnCurrentRow;
442
443     char** mpaszResults;
444
445 };
446
447
448
449
450
451 class CppSQLite3Statement
452
453 {
454
455 public:
456
457
458
459     CppSQLite3Statement();
460
461
462
463     CppSQLite3Statement(const CppSQLite3Statement& rStatement);
464
465
466
467     CppSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM);
468
469
470
471     virtual ~CppSQLite3Statement();
472
473
474
475     CppSQLite3Statement& operator=(const CppSQLite3Statement& rStatement);
476
477
478
479     int execDML();
480
481
482
483     CppSQLite3Query execQuery();
484
485
486
487     void bind(int nParam, const char* szValue);
488
489     void bind(int nParam, const int nValue);
490
491     void bind(int nParam, const double dwValue);
492
493     void bind(int nParam, const unsigned char* blobValue, int nLen);
494
495     void bindNull(int nParam);
496
497
498
499     void reset();
500
501
502
503     void finalize();
504
505
506
507 private:
508
509
510
511     void checkDB();
512
513     void checkVM();
514
515
516
517     sqlite3* mpDB;
518
519     sqlite3_stmt* mpVM;
520
521 };
522
523
524
525
526
527 class CppSQLite3DB
528
529 {
530
531 public:
532
533
534
535     CppSQLite3DB();
536
537
538
539     virtual ~CppSQLite3DB();
540
541
542
543     void open(const char* szFile);
544
545
546
547     void close();
548
549
550
551         bool tableExists(const char* szTable);
552
553
554
555     int execDML(const char* szSQL);
556
557
558
559     CppSQLite3Query execQuery(const char* szSQL);
560
561
562
563     int execScalar(const char* szSQL);
564
565
566
567     CppSQLite3Table getTable(const char* szSQL);
568
569
570
571     CppSQLite3Statement compileStatement(const char* szSQL);
572
573
574
575     sqlite_int64 lastRowId();
576
577
578
579     void interrupt() { sqlite3_interrupt(mpDB); }
580
581
582
583     void setBusyTimeout(int nMillisecs);
584
585
586
587     static const char* SQLiteVersion() { return SQLITE_VERSION; }
588
589
590
591 private:
592
593
594
595     CppSQLite3DB(const CppSQLite3DB& db);
596
597     CppSQLite3DB& operator=(const CppSQLite3DB& db);
598
599
600
601     sqlite3_stmt* compile(const char* szSQL);
602
603
604
605     void checkDB();
606
607
608
609     sqlite3* mpDB;
610
611     int mnBusyTimeoutMs;
612
613 };
614
615
616
617 #endif
618