~ubuntu-branches/ubuntu/wily/ginkgocadx/wily-proposed

« back to all changes in this revision

Viewing changes to src/cadxcore/wx/wxsqlite3/wxsqlite3.h

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Tille
  • Date: 2011-05-02 08:09:26 UTC
  • Revision ID: james.westby@ubuntu.com-20110502080926-bql5wep49c7hg91t
Tags: upstream-2.4.1.1
ImportĀ upstreamĀ versionĀ 2.4.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
///////////////////////////////////////////////////////////////////////////////
 
2
// Name:        wxsqlite3.h
 
3
// Purpose:     wxWidgets wrapper around the SQLite3 embedded database library.
 
4
// Author:      Ulrich Telle
 
5
// Modified by:
 
6
// Created:     2005-07-14
 
7
// Copyright:   (c) Ulrich Telle
 
8
// Licence:     wxWindows licence
 
9
///////////////////////////////////////////////////////////////////////////////
 
10
 
 
11
/// \file wxsqlite3.h Interface of the wxSQLite3 class
 
12
 
 
13
#ifndef _WX_SQLITE3_H_
 
14
#define _WX_SQLITE3_H_
 
15
 
 
16
#if defined(__GNUG__) && !defined(__APPLE__)
 
17
    #pragma interface "wxsqlite3.h"
 
18
#endif
 
19
 
 
20
#include <api/api.h>
 
21
#include <wx/arrstr.h>
 
22
#include <wx/datetime.h>
 
23
#include <wx/buffer.h>
 
24
#include <wx/hashmap.h>
 
25
#include <wx/regex.h>
 
26
#include <wx/string.h>
 
27
 
 
28
#include "wxsqlite3def.h"
 
29
 
 
30
#define WXSQLITE_ERROR 1000
 
31
 
 
32
#define WXSQLITE_INTEGER  1
 
33
#define WXSQLITE_FLOAT    2
 
34
#define WXSQLITE_TEXT     3
 
35
#define WXSQLITE_BLOB     4
 
36
#define WXSQLITE_NULL     5
 
37
 
 
38
#if defined(_MSC_VER) || defined(__BORLANDC__)
 
39
  typedef __int64 wxsqlite_int64;
 
40
#else
 
41
  typedef long long int wxsqlite_int64;
 
42
#endif
 
43
 
 
44
/// Enumeration of transaction types
 
45
enum wxSQLite3TransactionType
 
46
{
 
47
  WXSQLITE_TRANSACTION_DEFAULT,
 
48
  WXSQLITE_TRANSACTION_DEFERRED,
 
49
  WXSQLITE_TRANSACTION_IMMEDIATE,
 
50
  WXSQLITE_TRANSACTION_EXCLUSIVE
 
51
};
 
52
 
 
53
/// Enumeration of SQLite limitation types
 
54
enum wxSQLite3LimitType
 
55
{
 
56
  WXSQLITE_LIMIT_LENGTH              = 0,
 
57
  WXSQLITE_LIMIT_SQL_LENGTH          = 1,
 
58
  WXSQLITE_LIMIT_COLUMN              = 2,
 
59
  WXSQLITE_LIMIT_EXPR_DEPTH          = 3,
 
60
  WXSQLITE_LIMIT_COMPOUND_SELECT     = 4,
 
61
  WXSQLITE_LIMIT_VDBE_OP             = 5,
 
62
  WXSQLITE_LIMIT_FUNCTION_ARG        = 6,
 
63
  WXSQLITE_LIMIT_ATTACHED            = 7,
 
64
  WXSQLITE_LIMIT_LIKE_PATTERN_LENGTH = 8,
 
65
  WXSQLITE_LIMIT_VARIABLE_NUMBER     = 9,
 
66
  WXSQLITE_LIMIT_TRIGGER_DEPTH       = 10
 
67
};
 
68
 
 
69
/// Enumeration of journal modes
 
70
enum wxSQLite3JournalMode
 
71
{
 
72
  WXSQLITE_JOURNALMODE_DELETE     = 0,   // Commit by deleting journal file
 
73
  WXSQLITE_JOURNALMODE_PERSIST    = 1,   // Commit by zeroing journal header
 
74
  WXSQLITE_JOURNALMODE_OFF        = 2,   // Journal omitted.
 
75
  WXSQLITE_JOURNALMODE_TRUNCATE   = 3,   // Commit by truncating journal
 
76
  WXSQLITE_JOURNALMODE_MEMORY     = 4,   // In-memory journal file
 
77
  WXSQLITE_JOURNALMODE_WAL        = 5    // Use write-ahead logging
 
78
};
 
79
 
 
80
#define WXSQLITE_OPEN_READONLY         0x00000001
 
81
#define WXSQLITE_OPEN_READWRITE        0x00000002
 
82
#define WXSQLITE_OPEN_CREATE           0x00000004
 
83
#define WXSQLITE_OPEN_NOMUTEX          0x00008000
 
84
#define WXSQLITE_OPEN_FULLMUTEX        0x00010000
 
85
#define WXSQLITE_OPEN_SHAREDCACHE      0x00020000
 
86
#define WXSQLITE_OPEN_PRIVATECACHE     0x00040000
 
87
 
 
88
#define WXSQLITE_CHECKPOINT_PASSIVE 0
 
89
#define WXSQLITE_CHECKPOINT_FULL    1
 
90
#define WXSQLITE_CHECKPOINT_RESTART 2
 
91
 
 
92
inline void operator++(wxSQLite3LimitType& value)
 
93
{
 
94
  value = wxSQLite3LimitType(value+1);
 
95
}
 
96
 
 
97
/// SQL exception
 
98
class EXTAPI wxSQLite3Exception
 
99
{
 
100
public:
 
101
  /// Constructor
 
102
  wxSQLite3Exception(int errorCode, const wxString& errMsg);
 
103
 
 
104
  /// Copy constructor
 
105
  wxSQLite3Exception(const wxSQLite3Exception&  e);
 
106
 
 
107
  /// Destructor
 
108
  virtual ~wxSQLite3Exception();
 
109
 
 
110
  /// Get error code associated with the exception
 
111
  int GetErrorCode() const { return (m_errorCode & 0xff); }
 
112
 
 
113
  /// Get extended error code associated with the exception
 
114
  int GetExtendedErrorCode() const { return m_errorCode; }
 
115
 
 
116
  /// Get error message associated with the exception
 
117
  const wxString GetMessage() const { return m_errorMessage; }
 
118
 
 
119
  /// Convert error code to error message
 
120
  static const wxString ErrorCodeAsString(int errorCode);
 
121
 
 
122
private:
 
123
  int      m_errorCode;     ///< SQLite3 error code associated with this exception
 
124
  wxString m_errorMessage;  ///< SQLite3 error message associated with this exception
 
125
};
 
126
 
 
127
/// SQL statment buffer for use with SQLite3's printf method
 
128
class EXTAPI wxSQLite3StatementBuffer
 
129
{
 
130
public:
 
131
  /// Constructor
 
132
  wxSQLite3StatementBuffer();
 
133
 
 
134
  /// Destructor
 
135
  ~wxSQLite3StatementBuffer();
 
136
 
 
137
  /// Format a SQL statement using SQLite3's printf method
 
138
  /**
 
139
  * This method is a variant of the "sprintf()" from the standard C library.
 
140
  * All of the usual printf formatting options apply. In addition,
 
141
  * there is a "%q" option. %q works like %s in that it substitutes
 
142
  * a null-terminated string from the argument list. But %q also
 
143
  * doubles every '\'' character. %q is designed for use inside a
 
144
  * string literal. By doubling each '\'' character it escapes that
 
145
  * character and allows it to be inserted into the string.
 
146
  *
 
147
  * For example, so some string variable contains text as follows:
 
148
  *
 
149
  * char *zText = "It's a happy day!";
 
150
  *
 
151
  * One can use this text in an SQL statement as follows:
 
152
  *
 
153
  * wxSQLite3StatementBuffer stmtBuffer;
 
154
  * stmtBuffer.Format("INSERT INTO table VALUES('%q')", zText);
 
155
  *
 
156
  * Because the %q format string is used, the '\'' character in
 
157
  * zText is escaped and the SQL generated is as follows:
 
158
  *
 
159
  * INSERT INTO table1 VALUES('It''s a happy day!')
 
160
  *
 
161
  * \param format SQL statement string with formatting options
 
162
  * \param ... list of statement parameters
 
163
  * \return const char pointer to the resulting statement buffer
 
164
  */
 
165
  const char* Format(const char* format, ...);
 
166
 
 
167
  /// Format a SQL statement using SQLite3's printf method
 
168
  /**
 
169
  * This method is like method Format but takes a va_list argument
 
170
  * to pass the statement parameters.
 
171
  *
 
172
  * \param format SQL statement string with formatting options
 
173
  * \param va va_list of statement parameters
 
174
  * \return const char pointer to the resulting statement buffer
 
175
  */
 
176
  const char* FormatV(const char* format, va_list va);
 
177
 
 
178
  /// Dereference the internal buffer
 
179
  /**
 
180
  * \return const char pointer to the resulting statement buffer
 
181
  */
 
182
  operator const char*() const { return m_buffer; }
 
183
 
 
184
  /// Clear the internal buffer
 
185
  void Clear();
 
186
 
 
187
private:
 
188
  char* m_buffer;  ///< Internal buffer
 
189
};
 
190
 
 
191
/// Context for user defined scalar or aggregate functions
 
192
/**
 
193
* A function context gives user defined scalar or aggregate functions
 
194
* access to function arguments and function results. The "Execute" method
 
195
* resp. the "Aggregate" and "Finalize" methods receive the current
 
196
* function context as an argument.
 
197
*/
 
198
class EXTAPI wxSQLite3FunctionContext
 
199
{
 
200
public:
 
201
  /// Get the number of function arguments
 
202
  /**
 
203
  * \return the number of arguments the function was called with
 
204
  */
 
205
  int GetArgCount();
 
206
 
 
207
  /// Get the type of a function argument
 
208
  /**
 
209
  * \param argIndex index of the function argument. Indices start with 0.
 
210
  * \return argument type as one of the values WXSQLITE_INTEGER, WXSQLITE_FLOAT, WXSQLITE_TEXT, WXSQLITE_BLOB, or WXSQLITE_NULL
 
211
  */
 
212
  int GetArgType(int argIndex);
 
213
 
 
214
  /// Check whether a function argument is a NULL value
 
215
  /**
 
216
  * \param argIndex index of the function argument. Indices start with 0.
 
217
  * \return TRUE if the argument is NULL or the argIndex is out of bounds, FALSE otherwise
 
218
  */
 
219
  bool IsNull(int argIndex);
 
220
 
 
221
  /// Get a function argument as an integer value
 
222
  /**
 
223
  * \param argIndex index of the function argument. Indices start with 0.
 
224
  * \param nullValue value to be returned in case the argument is NULL
 
225
  * \return argument value
 
226
  */
 
227
  int GetInt(int argIndex, int nullValue = 0);
 
228
 
 
229
  /// Get a function argument as an 64-bit integer value
 
230
  /**
 
231
  * \param argIndex index of the function argument. Indices start with 0.
 
232
  * \param nullValue value to be returned in case the argument is NULL
 
233
  * \return argument value
 
234
  */
 
235
  wxLongLong GetInt64(int argIndex, wxLongLong nullValue = 0);
 
236
 
 
237
  /// Get a function argument as a double value
 
238
  /**
 
239
  * \param argIndex index of the function argument. Indices start with 0.
 
240
  * \param nullValue value to be returned in case the argument is NULL
 
241
  * \return argument value
 
242
  */
 
243
  double GetDouble(int argIndex, double nullValue = 0);
 
244
 
 
245
  /// Get a function argument as a string value
 
246
  /**
 
247
  * \param argIndex index of the function argument. Indices start with 0.
 
248
  * \param nullValue value to be returned in case the argument is NULL
 
249
  * \return argument value
 
250
  */
 
251
  wxString GetString(int argIndex, const wxString& nullValue = wxEmptyString);
 
252
 
 
253
  /// Get a function argument as a BLOB value
 
254
  /**
 
255
  * \param argIndex index of the function argument. Indices start with 0.
 
256
  * \param[out] len length of the blob argument in bytes
 
257
  * \return argument value
 
258
  */
 
259
  const unsigned char* GetBlob(int argIndex, int& len);
 
260
 
 
261
  /// Get a function argument as a BLOB value
 
262
  /**
 
263
  * \param argIndex index of the function argument. Indices start with 0.
 
264
  * \param[out] buffer to which the blob argument value is appended
 
265
  * \return reference to argument value
 
266
  */
 
267
  wxMemoryBuffer& GetBlob(int argIndex, wxMemoryBuffer& buffer);
 
268
 
 
269
  /// Set the function result as an integer value
 
270
  /**
 
271
  * \param value function result value
 
272
  */
 
273
  void SetResult(int value);
 
274
 
 
275
  /// Set the function result as an 64-bit integer value
 
276
  /**
 
277
  * \param value function result value
 
278
  */
 
279
  void SetResult(wxLongLong value);
 
280
 
 
281
  /// Set the function result as a double value
 
282
  /**
 
283
  * \param value function result value
 
284
  */
 
285
  void SetResult(double value);
 
286
 
 
287
  /// Set the function result as a string value
 
288
  /**
 
289
  * \param value function result value
 
290
  */
 
291
  void SetResult(const wxString& value);
 
292
 
 
293
  /// Set the function result as a BLOB value
 
294
  /**
 
295
  * \param value function result value
 
296
  * \param len length of the result blob in bytes
 
297
  */
 
298
  void SetResult(unsigned char* value, int len);
 
299
 
 
300
  /// Set the function result as a BLOB value
 
301
  /**
 
302
  * \param buffer containing the function result value
 
303
  */
 
304
  void SetResult(const wxMemoryBuffer& buffer);
 
305
 
 
306
  /// Set the function result as a NULL value
 
307
  void SetResultNull();
 
308
 
 
309
  /// Set the function result as a zero BLOB value
 
310
  /**
 
311
  * \param blobSize size of the zero filled BLOB value
 
312
  */
 
313
  void SetResultZeroBlob(int blobSize);
 
314
 
 
315
  /// Set the function result as a exact copy of a function argument
 
316
  /**
 
317
  * \param argIndex index of the argument which should be copied as the result value
 
318
  */
 
319
  void SetResultArg(int argIndex);
 
320
 
 
321
  /// Set an error message as the function result
 
322
  /**
 
323
  * \param errmsg string containing an error message
 
324
  */
 
325
  void SetResultError(const wxString& errmsg);
 
326
 
 
327
  /// Get the number of aggregate steps
 
328
  /**
 
329
  * \return the number of aggregation steps. The current aggregation step counts so at least 1 is returned.
 
330
  */
 
331
  int GetAggregateCount();
 
332
 
 
333
  /// Get a pointer to an aggregate structure of specified length
 
334
  /**
 
335
  * Usually an aggregation functions needs temporary memory to collect
 
336
  * the information gathered from each invocation of the "Aggregate" method.
 
337
  * On the first invocation of this method the returned memory contains
 
338
  * binary zeros.
 
339
  * If this memory is used to store pointers to allocated objects,
 
340
  * it is important to free all allocated objects in the "Finalize" method.
 
341
  *
 
342
  * \param len amount of memory needed in bytes
 
343
  * \return pointer to the allocated memory
 
344
  */
 
345
  void* GetAggregateStruct(int len);
 
346
 
 
347
  /// Execute a user defined scalar function (internal use only)
 
348
  static void ExecScalarFunction(void* ctx, int argc, void** argv);
 
349
 
 
350
  /// Execute an aggregate step of a user defined aggregate function (internal use only)
 
351
  static void ExecAggregateStep(void* ctx, int argc, void** argv);
 
352
 
 
353
  /// Execute the final step of a user defined aggregate function (internal use only)
 
354
  static void ExecAggregateFinalize(void* ctx);
 
355
 
 
356
  /// Execute the user defined authorizer function (internal use only)
 
357
  static int  ExecAuthorizer(void*, int type,
 
358
                             const char* arg1, const char* arg2,
 
359
                             const char* arg3, const char* arg4);
 
360
 
 
361
  /// Execute the user defined commit hook (internal use only)
 
362
  static int ExecCommitHook(void* hook);
 
363
 
 
364
  /// Execute the user defined rollback hook (internal use only)
 
365
  static void ExecRollbackHook(void* hook);
 
366
 
 
367
  /// Execute the user defined update hook (internal use only)
 
368
  static void ExecUpdateHook(void* hook, int type,
 
369
                             const char* database, const char* table,
 
370
                             wxsqlite_int64 rowid);
 
371
 
 
372
  /// Execute the user defined Write Ahead Log hook (internal use only)
 
373
  static int ExecWriteAheadLogHook(void* hook, void* dbHandle,
 
374
                                   const char* database, int numPages);
 
375
 
 
376
private:
 
377
  /// Constructor
 
378
  wxSQLite3FunctionContext(void* ctx, bool isAggregate, int argc = 0, void** argv = NULL);
 
379
 
 
380
  /// Copy constructor
 
381
  wxSQLite3FunctionContext(wxSQLite3FunctionContext& ctx);
 
382
 
 
383
  void*  m_ctx;          ///< SQLite3 context
 
384
  bool   m_isAggregate;  ///< Flag whether this is the context of an aggregate function
 
385
  int    m_count;        ///< Aggregate count
 
386
  int    m_argc;         ///< Number of arguments
 
387
  void** m_argv;         ///< Array of SQLite3 arguments
 
388
};
 
389
 
 
390
 
 
391
/// Interface for user defined scalar functions
 
392
/**
 
393
*/
 
394
class EXTAPI wxSQLite3ScalarFunction
 
395
{
 
396
public:
 
397
  /// Constructor
 
398
  wxSQLite3ScalarFunction() {}
 
399
 
 
400
  /// Virtual destructor
 
401
  virtual ~wxSQLite3ScalarFunction() {}
 
402
  /// Execute the scalar function
 
403
  /**
 
404
  * This method is invoked for each appearance of the scalar function in the SQL query.
 
405
  * \param ctx function context which can be used to access arguments and result value
 
406
  */
 
407
  virtual void Execute(wxSQLite3FunctionContext& ctx) = 0;
 
408
};
 
409
 
 
410
 
 
411
/// Interface for user defined aggregate functions
 
412
/**
 
413
*/
 
414
class EXTAPI wxSQLite3AggregateFunction
 
415
{
 
416
public:
 
417
  /// Constructor
 
418
  wxSQLite3AggregateFunction() { m_count = 0; }
 
419
 
 
420
  /// Virtual destructor
 
421
  virtual ~wxSQLite3AggregateFunction() {}
 
422
  /// Execute the aggregate of the function
 
423
  /**
 
424
  * This method is invoked for each row of the result set of the query using the aggregate function.
 
425
  * \param ctx function context which can be used to access arguments and result value
 
426
  */
 
427
  virtual void Aggregate(wxSQLite3FunctionContext& ctx) = 0;
 
428
 
 
429
  /// Prepare the result of the aggregate function
 
430
  /**
 
431
  * This method is invoked after all rows of the result set of the query
 
432
  * using the aggregate function have been processed. Usually the final result
 
433
  * is calculated and returned in this method.
 
434
  * \param ctx function context which can be used to access arguments and result value
 
435
  */
 
436
  virtual void Finalize(wxSQLite3FunctionContext& ctx) = 0;
 
437
 
 
438
private:
 
439
  int    m_count;        ///< Aggregate count
 
440
  friend class wxSQLite3FunctionContext;
 
441
};
 
442
 
 
443
 
 
444
/// Interface for a user defined authorizer function
 
445
/**
 
446
*/
 
447
class EXTAPI wxSQLite3Authorizer
 
448
{
 
449
public:
 
450
  /// Codes identifying the command for which authorization is requested
 
451
  enum wxAuthorizationCode
 
452
  {                                    // arg1 =          arg2 =
 
453
    SQLITE_COPY                =  0,   // Table Name      File Name
 
454
    SQLITE_CREATE_INDEX        =  1,   // Index Name      Table Name
 
455
    SQLITE_CREATE_TABLE        =  2,   // Table Name      NULL
 
456
    SQLITE_CREATE_TEMP_INDEX   =  3,   // Index Name      Table Name
 
457
    SQLITE_CREATE_TEMP_TABLE   =  4,   // Table Name      NULL
 
458
    SQLITE_CREATE_TEMP_TRIGGER =  5,   // Trigger Name    Table Name
 
459
    SQLITE_CREATE_TEMP_VIEW    =  6,   // View Name       NULL
 
460
    SQLITE_CREATE_TRIGGER      =  7,   // Trigger Name    Table Name
 
461
    SQLITE_CREATE_VIEW         =  8,   // View Name       NULL
 
462
    SQLITE_DELETE              =  9,   // Table Name      NULL
 
463
    SQLITE_DROP_INDEX          = 10,   // Index Name      Table Name
 
464
    SQLITE_DROP_TABLE          = 11,   // Table Name      NULL
 
465
    SQLITE_DROP_TEMP_INDEX     = 12,   // Index Name      Table Name
 
466
    SQLITE_DROP_TEMP_TABLE     = 13,   // Table Name      NULL
 
467
    SQLITE_DROP_TEMP_TRIGGER   = 14,   // Trigger Name    Table Name
 
468
    SQLITE_DROP_TEMP_VIEW      = 15,   // View Name       NULL
 
469
    SQLITE_DROP_TRIGGER        = 16,   // Trigger Name    Table Name
 
470
    SQLITE_DROP_VIEW           = 17,   // View Name       NULL
 
471
    SQLITE_INSERT              = 18,   // Table Name      NULL
 
472
    SQLITE_PRAGMA              = 19,   // Pragma Name     1st arg or NULL
 
473
    SQLITE_READ                = 20,   // Table Name      Column Name
 
474
    SQLITE_SELECT              = 21,   // NULL            NULL
 
475
    SQLITE_TRANSACTION         = 22,   // Operation       NULL
 
476
    SQLITE_UPDATE              = 23,   // Table Name      Column Name
 
477
    SQLITE_ATTACH              = 24,   // Filename        NULL
 
478
    SQLITE_DETACH              = 25,   // Database Name   NULL
 
479
    SQLITE_ALTER_TABLE         = 26,   // Database Name   Table Name
 
480
    SQLITE_REINDEX             = 27,   // Index Name      NULL
 
481
    SQLITE_ANALYZE             = 28,   // Table Name      NULL
 
482
    SQLITE_CREATE_VTABLE       = 29,   // Table Name      Module Name
 
483
    SQLITE_DROP_VTABLE         = 30,   // Table Name      Module Name
 
484
    SQLITE_FUNCTION            = 31,   // NULL            Function Name
 
485
    SQLITE_SAVEPOINT           = 32,   // Operation       Savepoint Name
 
486
    SQLITE_MAX_CODE            = SQLITE_SAVEPOINT
 
487
  };
 
488
 
 
489
   /// Return codes of the authorizer
 
490
  enum wxAuthorizationResult
 
491
  {
 
492
    SQLITE_OK     = 0,   // Allow access
 
493
    SQLITE_DENY   = 1,   // Abort the SQL statement with an error
 
494
    SQLITE_IGNORE = 2    // Don't allow access, but don't generate an error
 
495
  };
 
496
  /// Virtual destructor
 
497
  virtual ~wxSQLite3Authorizer() {}
 
498
  /// Execute the authorizer function
 
499
  /**
 
500
  * Please refer to the SQLite documentation for further information about the
 
501
  * meaning of the parameters.
 
502
  *
 
503
  * \param type wxAuthorizationCode. The value signifies what kind of operation is to be authorized.
 
504
  * \param arg1 first argument (value depends on "type")
 
505
  * \param arg2 second argument (value depends on "type")
 
506
  * \param arg3 third argument (name of database if applicable)
 
507
  * \param arg4 fourth argument (name of trigger or view if applicable)
 
508
  * \return a wxAuthorizationResult, i.e. SQLITE_OK, SQLITE_DENY or SQLITE_IGNORE
 
509
  */
 
510
  virtual wxAuthorizationResult Authorize(wxAuthorizationCode type,
 
511
                                          const wxString& arg1, const wxString& arg2,
 
512
                                          const wxString& arg3, const wxString& arg4) = 0;
 
513
  /// Convert authorization code to string
 
514
  /**
 
515
  * \param type wxAuthorizationCode. The value signifies what kind of operation is to be authorized.
 
516
  */
 
517
  static wxString AuthorizationCodeToString(wxSQLite3Authorizer::wxAuthorizationCode type);
 
518
};
 
519
 
 
520
class WXDLLIMPEXP_FWD_SQLITE3 wxSQLite3Database;
 
521
 
 
522
/// Interface for a user defined hook function
 
523
/**
 
524
*/
 
525
class EXTAPI wxSQLite3Hook
 
526
{
 
527
public:
 
528
  /// Codes identifying the command for which the hook is called
 
529
  enum wxUpdateType
 
530
  {
 
531
    SQLITE_DELETE              =  9,
 
532
    SQLITE_INSERT              = 18,
 
533
    SQLITE_UPDATE              = 23
 
534
  };
 
535
  /// Default constructor
 
536
  wxSQLite3Hook() : m_db(NULL) {}
 
537
 
 
538
  /// Virtual destructor
 
539
  virtual ~wxSQLite3Hook() {}
 
540
 
 
541
  /// Execute the commit hook callback function
 
542
  /**
 
543
  * Please refer to the SQLite documentation for further information.
 
544
  * \return true to request rollback of the transaction, false to continue with commit
 
545
  */
 
546
  virtual bool CommitCallback() { return false; }
 
547
 
 
548
  /// Execute the rollback hook callback function
 
549
  /**
 
550
  * Please refer to the SQLite documentation for further information.
 
551
  */
 
552
  virtual void RollbackCallback() {}
 
553
 
 
554
  /// Execute the hook callback function
 
555
  /**
 
556
  * Please refer to the SQLite documentation for further information about the
 
557
  * meaning of the parameters.
 
558
  *
 
559
  * \param type wxHookType. The value signifies what kind of operation is to be authorized.
 
560
  * \param database Name of the database
 
561
  * \param table Name of the table
 
562
  * \param rowid The rowid of the affected row
 
563
  */
 
564
  virtual void UpdateCallback(wxUpdateType WXUNUSED(type),
 
565
                              const wxString& WXUNUSED(database), const wxString& WXUNUSED(table),
 
566
                              wxLongLong WXUNUSED(rowid)) {}
 
567
 
 
568
  /// Execute the write-ahead log hook callback function
 
569
  /**
 
570
  * Please refer to the SQLite documentation for further information about the
 
571
  * meaning of the parameters.
 
572
  *
 
573
  * \param database Name of the database
 
574
  * \param numPages the number of pages
 
575
  */
 
576
  virtual int WriteAheadLogCallback(const wxString& WXUNUSED(database),
 
577
                                    int WXUNUSED(numPages)) { return 0; }
 
578
 
 
579
  /// Set the associated database
 
580
  /**
 
581
  * For the write-ahead log hook the associated database is set internally.
 
582
  * \param db pointer to the associated database instance
 
583
  */
 
584
  void SetDatabase(wxSQLite3Database* db) { m_db = db; }
 
585
 
 
586
  /// Get the associated database
 
587
  /**
 
588
  * For the write-ahead log hook the associated database can be accessed.
 
589
  *
 
590
  * \return pointer to the associated database instance
 
591
  * \note Access to the associated database is only provided for write-ahead log hooks.
 
592
  */
 
593
  wxSQLite3Database* GetDatabase() const { return m_db; }
 
594
 
 
595
private:
 
596
  wxSQLite3Database* m_db;
 
597
};
 
598
 
 
599
/// Interface for a user defined collation sequence
 
600
/**
 
601
*/
 
602
class EXTAPI wxSQLite3Collation
 
603
{
 
604
public:
 
605
  /// Virtual destructor
 
606
  virtual ~wxSQLite3Collation() {}
 
607
 
 
608
  /// Execute a comparison using a user-defined collation sequence
 
609
  /**
 
610
  * Please refer to the SQLite documentation for further information.
 
611
  * \param text1 first text string
 
612
  * \param text2 second text string
 
613
  * \return an integer < 0, = 0, or > 0 depending on whether text1 is less than, equal to, or greater than text2.
 
614
  */
 
615
  virtual int Compare(const wxString& text1, const wxString& text2) { return text1.Cmp(text2); }
 
616
};
 
617
 
 
618
/// Result set of a SQL query
 
619
class EXTAPI wxSQLite3ResultSet
 
620
{
 
621
public:
 
622
  /// Constructor
 
623
  wxSQLite3ResultSet();
 
624
 
 
625
  /// Copy constructor
 
626
  wxSQLite3ResultSet(const wxSQLite3ResultSet& resultSet);
 
627
 
 
628
  /// Constructor for internal use
 
629
  wxSQLite3ResultSet(void* db, void* stmt,
 
630
                     bool eof, bool first = true, bool ownStmt = true);
 
631
 
 
632
  /// Assignment constructor
 
633
  wxSQLite3ResultSet& operator=(const wxSQLite3ResultSet& resultSet);
 
634
 
 
635
  /// Destructor
 
636
  /**
 
637
  */
 
638
  virtual ~wxSQLite3ResultSet();
 
639
 
 
640
  /// Get the number of columns in the result set
 
641
  /**
 
642
  * \return number of columns in result set
 
643
  */
 
644
  int GetColumnCount();
 
645
 
 
646
  /// Find the index of a column by name
 
647
  /**
 
648
  * \param columnName name of the column
 
649
  * \return index of the column. Indices start with 0.
 
650
  */
 
651
  int FindColumnIndex(const wxString& columnName);
 
652
 
 
653
  /// Get the name of a column
 
654
  /**
 
655
  * \param columnIndex index of the column. Indices start with 0.
 
656
  * \return column name as string
 
657
  */
 
658
  wxString GetColumnName(int columnIndex);
 
659
 
 
660
  /// Get the declared type of a column
 
661
  /**
 
662
  * \param columnIndex index of the column. Indices start with 0.
 
663
  * \return type string as specified in the table definition
 
664
  */
 
665
  wxString GetDeclaredColumnType(int columnIndex);
 
666
 
 
667
  /// Get the actual type of a column
 
668
  /**
 
669
  * \param columnIndex index of the column. Indices start with 0.
 
670
  * \return column type as one of the values WXSQLITE_INTEGER, WXSQLITE_FLOAT, WXSQLITE_TEXT, WXSQLITE_BLOB, or WXSQLITE_NULL
 
671
  */
 
672
  int GetColumnType(int columnIndex);
 
673
 
 
674
  /// Get the database name of a column
 
675
  /**
 
676
  * \param columnIndex index of the column. Indices start with 0.
 
677
  * \return database name the column belongs to or empty string
 
678
  *
 
679
  * This method is only available if WXSQLITE3_HAVE_METADATA is defined and SQLite has been compiled with SQLITE_ENABLE_COLUMN_METADATA defined.
 
680
  */
 
681
  wxString GetDatabaseName(int columnIndex);
 
682
 
 
683
  /// Get the table name of a column
 
684
  /**
 
685
  * \param columnIndex index of the column. Indices start with 0.
 
686
  * \return table name the column belongs to or empty string
 
687
  *
 
688
  * This method is only available if WXSQLITE3_HAVE_METADATA is defined and SQLite has been compiled with SQLITE_ENABLE_COLUMN_METADATA defined.
 
689
  */
 
690
  wxString GetTableName(int columnIndex);
 
691
 
 
692
  /// Get the origin name of a column
 
693
  /**
 
694
  * \param columnIndex index of the column. Indices start with 0.
 
695
  * \return origin name the column belongs to or empty string
 
696
  *
 
697
  * This method is only available if WXSQLITE3_HAVE_METADATA is defined and SQLite has been compiled with SQLITE_ENABLE_COLUMN_METADATA defined.
 
698
  */
 
699
  wxString GetOriginName(int columnIndex);
 
700
 
 
701
  /// Get a column as a string using the column index
 
702
  /**
 
703
  * \param columnIndex index of the column. Indices start with 0.
 
704
  * \return value of the column as string
 
705
  */
 
706
  wxString GetAsString(int columnIndex);
 
707
 
 
708
  /// Get a column as a string using the column name
 
709
  /**
 
710
  * \param columnName name of the column
 
711
  * \return value of the column
 
712
  */
 
713
  wxString GetAsString(const wxString& columnName);
 
714
 
 
715
  /// Get a column as an integer using the column index
 
716
  /**
 
717
  * \param columnIndex index of the column. Indices start with 0.
 
718
  * \param nullValue value to be returned in case the column is NULL
 
719
  * \return value of the column
 
720
  */
 
721
  int GetInt(int columnIndex, int nullValue = 0);
 
722
 
 
723
  /// Get a column as an integer using the column name
 
724
  /**
 
725
  * \param columnName name of the column
 
726
  * \param nullValue value to be returned in case the column is NULL
 
727
  * \return value of the column
 
728
  */
 
729
  int GetInt(const wxString& columnName, int nullValue = 0);
 
730
 
 
731
  /// Get a column as a 64-bit integer using the column index
 
732
  /**
 
733
  * \param columnIndex index of the column. Indices start with 0.
 
734
  * \param nullValue value to be returned in case the column is NULL
 
735
  * \return value of the column
 
736
  */
 
737
  wxLongLong GetInt64(int columnIndex, wxLongLong nullValue = 0);
 
738
 
 
739
  /// Get a column as a 64-bit integer using the column name
 
740
  /**
 
741
  * \param columnName name of the column
 
742
  * \param nullValue value to be returned in case the column is NULL
 
743
  * \return value of the column
 
744
  */
 
745
  wxLongLong GetInt64(const wxString& columnName, wxLongLong nullValue = 0);
 
746
 
 
747
  /// Get a column as a double using the column index
 
748
  /**
 
749
  * \param columnIndex index of the column. Indices start with 0.
 
750
  * \param nullValue value to be returned in case the column is NULL
 
751
  * \return value of the column
 
752
  */
 
753
  double GetDouble(int columnIndex, double nullValue = 0.0);
 
754
 
 
755
  /// Get a column as a double using the column name
 
756
  /**
 
757
  * \param columnName name of the column
 
758
  * \param nullValue value to be returned in case the column is NULL
 
759
  * \return value of the column
 
760
  */
 
761
  double GetDouble(const wxString& columnName, double nullValue = 0.0);
 
762
 
 
763
  /// Get a column as a string using the column index
 
764
  /**
 
765
  * \param columnIndex index of the column. Indices start with 0.
 
766
  * \param nullValue value to be returned in case the column is NULL
 
767
  * \return value of the column
 
768
  */
 
769
  wxString GetString(int columnIndex, const wxString& nullValue = wxEmptyString);
 
770
 
 
771
  /// Get a column as a string using the column name
 
772
  /**
 
773
  * \param columnName name of the column
 
774
  * \param nullValue value to be returned in case the column is NULL
 
775
  * \return value of the column
 
776
  */
 
777
  wxString GetString(const wxString& columnName, const wxString& nullValue = wxEmptyString);
 
778
 
 
779
  /// Get a column as a BLOB using the column index
 
780
  /**
 
781
  * \param columnIndex index of the column. Indices start with 0.
 
782
  * \param[out] len length of the blob in bytes
 
783
  * \return value of the column
 
784
  */
 
785
  const unsigned char* GetBlob(int columnIndex, int& len);
 
786
 
 
787
  /// Get a column as a BLOB using the column name
 
788
  /**
 
789
  * \param columnName name of the column
 
790
  * \param[out] len length of the blob in bytes
 
791
  * \return value of the column
 
792
  */
 
793
  const unsigned char* GetBlob(const wxString& columnName, int& len);
 
794
 
 
795
  /// Get a column as a BLOB using the column index and append to memory buffer
 
796
  /**
 
797
  * \param columnIndex index of the column. Indices start with 0.
 
798
  * \param[out] buffer the memory buffer to which the BLOB value is appended
 
799
  * \return reference to the memory buffer
 
800
  */
 
801
  wxMemoryBuffer& GetBlob(int columnIndex, wxMemoryBuffer& buffer);
 
802
 
 
803
  /// Get a column as a BLOB using the column index and append to memory buffer
 
804
  /**
 
805
  * \param columnName name of the column
 
806
  * \param[out] buffer the memory buffer to which the BLOB value is appended
 
807
  * \return reference to the memory buffer
 
808
  */
 
809
  wxMemoryBuffer& GetBlob(const wxString& columnName, wxMemoryBuffer& buffer);
 
810
 
 
811
  /// Get a column as a date value using the column index
 
812
  /**
 
813
  * \param columnIndex index of the column. Indices start with 0.
 
814
  * \return value of the column
 
815
  */
 
816
  wxDateTime GetDate(int columnIndex);
 
817
 
 
818
  /// Get a column as a date value using the column name
 
819
  /**
 
820
  * \param columnName name of the column
 
821
  * \return value of the column
 
822
  */
 
823
  wxDateTime GetDate(const wxString& columnName);
 
824
 
 
825
  /// Get a column as a time value using the column index
 
826
  /**
 
827
  * \param columnIndex index of the column. Indices start with 0.
 
828
  * \return value of the column
 
829
  */
 
830
  wxDateTime GetTime(int columnIndex);
 
831
 
 
832
  /// Get a column as a time value using the column name
 
833
  /**
 
834
  * \param columnName name of the column
 
835
  * \return value of the column
 
836
  */
 
837
  wxDateTime GetTime(const wxString& columnName);
 
838
 
 
839
  /// Get a column as a date and time value using the column index
 
840
  /**
 
841
  * \param columnIndex index of the column. Indices start with 0.
 
842
  * \return value of the column
 
843
  */
 
844
  wxDateTime GetDateTime(int columnIndex);
 
845
 
 
846
  /// Get a column as a date and time value using the column name
 
847
  /**
 
848
  * \param columnName name of the column
 
849
  * \return value of the column
 
850
  */
 
851
  wxDateTime GetDateTime(const wxString& columnName);
 
852
 
 
853
  /// Get a column as a timestamp value using the column index
 
854
  /**
 
855
  * \param columnIndex index of the column. Indices start with 0.
 
856
  * \return value of the column
 
857
  */
 
858
  wxDateTime GetTimestamp(int columnIndex);
 
859
 
 
860
  /// Get a column as a timestamp value using the column name
 
861
  /**
 
862
  * \param columnName name of the column
 
863
  * \return value of the column
 
864
  */
 
865
  wxDateTime GetTimestamp(const wxString& columnName);
 
866
 
 
867
  /// Get a column as a date and time value using the column index
 
868
  /**
 
869
  * The date/time value is expected to be stored in the database as a numeric value (i.e. int64).
 
870
  *
 
871
  * \param columnIndex index of the column. Indices start with 0.
 
872
  * \return value of the column
 
873
  */
 
874
  wxDateTime GetNumericDateTime(int columnIndex);
 
875
 
 
876
  /// Get a column as a date and time value using the column name
 
877
  /**
 
878
  * The date/time value is expected to be stored in the database as a numeric value (i.e. int64).
 
879
  *
 
880
  * \param columnName name of the column
 
881
  * \return value of the column
 
882
  */
 
883
  wxDateTime GetNumericDateTime(const wxString& columnName);
 
884
 
 
885
  /// Get a column as a date and time value using the column index
 
886
  /**
 
887
  * The date/time value is expected to be stored in the database as a Julian Day Number (i.e. double).
 
888
  *
 
889
  * \param columnIndex index of the column. Indices start with 0.
 
890
  * \return value of the column
 
891
  */
 
892
  wxDateTime GetJulianDayNumber(int columnIndex);
 
893
 
 
894
  /// Get a column as a date and time value using the column name
 
895
  /**
 
896
  * The date/time value is expected to be stored in the database as a Julian Day Number (i.e. double).
 
897
  *
 
898
  * \param columnName name of the column
 
899
  * \return value of the column
 
900
  */
 
901
  wxDateTime GetJulianDayNumber(const wxString& columnName);
 
902
 
 
903
  /// Get a column as a boolean value using the column index
 
904
  /**
 
905
  * \param columnIndex index of the column. Indices start with 0.
 
906
  * \return value of the column
 
907
  */
 
908
  bool GetBool(int columnIndex);
 
909
 
 
910
  /// Get a column as a boolean value using the column name
 
911
  /**
 
912
  * \param columnName name of the column
 
913
  * \return value of the column
 
914
  */
 
915
  bool GetBool(const wxString& columnName);
 
916
 
 
917
  /// Check whether a column has a NULL value using the column index
 
918
  /**
 
919
  * \param columnIndex index of the column. Indices start with 0.
 
920
  * \return TRUE if the value is NULL, FALSE otherwise
 
921
  */
 
922
  bool IsNull(int columnIndex);
 
923
 
 
924
  /// Check whether a column has a NULL value using the column name
 
925
  /**
 
926
  * \param columnName name of the column
 
927
  * \return TRUE if the value is NULL, FALSE otherwise
 
928
  */
 
929
  bool IsNull(const wxString& columnName);
 
930
 
 
931
  /// Check whether all rows of the result set have been processed
 
932
  /**
 
933
  * \return TRUE if all rows of the result have been processed, FALSE otherwise
 
934
  */
 
935
  bool Eof();
 
936
 
 
937
  /// Retrieve next row of the result set
 
938
  /**
 
939
  * Advances the cursor to the next row.
 
940
  * On creation of the result set the cursor is positioned BEFORE the first row, i.e.
 
941
  * the first call to this method makes the first row available for processing.
 
942
  * \return TRUE while there are still rows to process, FALSE otherwise
 
943
  */
 
944
  bool NextRow();
 
945
 
 
946
  /// Finalize the result set
 
947
  /**
 
948
  */
 
949
  void Finalize();
 
950
 
 
951
  /// Get the original SQL string for preparing the query statement
 
952
  /**
 
953
  * \return the original SQL string used to prepare the query statement
 
954
  */
 
955
  wxString GetSQL();
 
956
 
 
957
  /// Validate associated SQLite database and statement
 
958
  /**
 
959
  * \return TRUE if both, a SQLite database and a SQLite statement, are associated, FALSE otherwise
 
960
  */
 
961
  bool IsOk();
 
962
 
 
963
private:
 
964
  /// Check the validity of the associated statement
 
965
  void CheckStmt();
 
966
 
 
967
  void* m_db;       ///< associated database
 
968
  void* m_stmt;     ///< associated statement
 
969
  bool  m_eof;      ///< Flag for end of result set
 
970
  bool  m_first;    ///< Flag for first row of the result set
 
971
  int   m_cols;     ///< Numver of columns in row set
 
972
  bool  m_ownStmt;  ///< Flag for ownership of the associated statement
 
973
};
 
974
 
 
975
 
 
976
/// Holds the complete result set of a SQL query
 
977
class EXTAPI wxSQLite3Table
 
978
{
 
979
public:
 
980
  /// Constructor
 
981
  wxSQLite3Table();
 
982
 
 
983
  wxSQLite3Table(const wxSQLite3Table& table);
 
984
 
 
985
  wxSQLite3Table(char** results, int rows, int cols);
 
986
 
 
987
  virtual ~wxSQLite3Table();
 
988
 
 
989
  wxSQLite3Table& operator=(const wxSQLite3Table& table);
 
990
 
 
991
  /// Get the number of columns in the result set
 
992
  /**
 
993
  * \return the number of columns
 
994
  */
 
995
  int GetColumnCount();
 
996
 
 
997
  /// Get the number of rows in the result set
 
998
  /**
 
999
  * \return the number of rows
 
1000
  */
 
1001
  int GetRowCount();
 
1002
 
 
1003
  /// Find the index of a column by name
 
1004
  /**
 
1005
  * \param columnName name of the column
 
1006
  * \return the index of the column
 
1007
  */
 
1008
  int FindColumnIndex(const wxString& columnName);
 
1009
 
 
1010
  /// Get the name of a column
 
1011
  /**
 
1012
  * \param columnIndex index of the column. Indices start with 0.
 
1013
  * \return name of the column
 
1014
  */
 
1015
  wxString GetColumnName(int columnIndex);
 
1016
 
 
1017
  /// Get a column as a string using the column index
 
1018
  /**
 
1019
  * \param columnIndex index of the column. Indices start with 0.
 
1020
  * \return value of the column as a string
 
1021
  *
 
1022
  * \note This method returns values of type <code>double</code>
 
1023
  * always using the point character as the decimal separator.
 
1024
  * This is SQLite default behaviour. Use method wxSQLite3Table::GetDouble
 
1025
  * to apply correct conversion from <code>string</code> to <code>double</code>.
 
1026
  */
 
1027
  wxString GetAsString(int columnIndex);
 
1028
 
 
1029
  /// Get a column as a string using the column name
 
1030
  /**
 
1031
  * \param columnName name of the column
 
1032
  * \return value of the column as a string
 
1033
  *
 
1034
  * \note This method returns values of type <code>double</code>
 
1035
  * always using the point character as the decimal separator.
 
1036
  * This is SQLite default behaviour. Use method wxSQLite3Table::GetDouble
 
1037
  * to apply correct conversion from <code>string</code> to <code>double</code>.
 
1038
  */
 
1039
  wxString GetAsString(const wxString& columnName);
 
1040
 
 
1041
  /// Get a column as an integer using the column index
 
1042
  /**
 
1043
  * \param columnIndex index of the column. Indices start with 0.
 
1044
  * \param nullValue value to be returned in case the column is NULL
 
1045
  * \return value of the column
 
1046
  */
 
1047
  int GetInt(int columnIndex, int nullValue = 0);
 
1048
 
 
1049
  /// Get a column as an integer using the column name
 
1050
  /**
 
1051
  * \param columnName name of the column
 
1052
  * \param nullValue value to be returned in case the column is NULL
 
1053
  * \return value of the column
 
1054
  */
 
1055
  int GetInt(const wxString& columnName, int nullValue = 0);
 
1056
 
 
1057
  /// Get a column as a 64-bit integer using the column index
 
1058
  /**
 
1059
  * \param columnIndex index of the column. Indices start with 0.
 
1060
  * \param nullValue value to be returned in case the column is NULL
 
1061
  * \return value of the column
 
1062
  */
 
1063
  wxLongLong GetInt64(int columnIndex, wxLongLong nullValue = 0);
 
1064
 
 
1065
  /// Get a column as an integer using the column name
 
1066
  /**
 
1067
  * \param columnName name of the column
 
1068
  * \param nullValue value to be returned in case the column is NULL
 
1069
  * \return value of the column
 
1070
  */
 
1071
  wxLongLong GetInt64(const wxString& columnName, wxLongLong nullValue = 0);
 
1072
 
 
1073
  /// Get a column as a double using the column index
 
1074
  /**
 
1075
  * \param columnIndex index of the column. Indices start with 0.
 
1076
  * \param nullValue value to be returned in case the column is NULL
 
1077
  * \return value of the column
 
1078
  */
 
1079
  double GetDouble(int columnIndex, double nullValue = 0.0);
 
1080
 
 
1081
  /// Get a column as a double using the column name
 
1082
  /**
 
1083
  * \param columnName name of the column
 
1084
  * \param nullValue value to be returned in case the column is NULL
 
1085
  * \return value of the column
 
1086
  */
 
1087
  double GetDouble(const wxString& columnName, double nullValue = 0.0);
 
1088
 
 
1089
  /// Get a column as a string using the column index
 
1090
  /**
 
1091
  * \param columnIndex index of the column. Indices start with 0.
 
1092
  * \param nullValue value to be returned in case the column is NULL
 
1093
  * \return value of the column
 
1094
  */
 
1095
  wxString GetString(int columnIndex, const wxString& nullValue = wxEmptyString);
 
1096
 
 
1097
  /// Get a column as a string using the column name
 
1098
  /**
 
1099
  * \param columnName name of the column
 
1100
  * \param nullValue value to be returned in case the column is NULL
 
1101
  * \return value of the column
 
1102
  */
 
1103
  wxString GetString(const wxString& columnName, const wxString& nullValue = wxEmptyString);
 
1104
 
 
1105
  /// Get a column as a date value using the column index
 
1106
  /**
 
1107
  * \param columnIndex index of the column. Indices start with 0.
 
1108
  * \return value of the column
 
1109
  */
 
1110
  wxDateTime GetDate(int columnIndex);
 
1111
 
 
1112
  /// Get a column as a date value using the column name
 
1113
  /**
 
1114
  * \param columnName name of the column
 
1115
  * \return value of the column
 
1116
  */
 
1117
  wxDateTime GetDate(const wxString& columnName);
 
1118
 
 
1119
  /// Get a column as a time value using the column index
 
1120
  /**
 
1121
  * \param columnIndex index of the column. Indices start with 0.
 
1122
  * \return value of the column
 
1123
  */
 
1124
  wxDateTime GetTime(int columnIndex);
 
1125
 
 
1126
  /// Get a column as a time value using the column name
 
1127
  /**
 
1128
  * \param columnName name of the column
 
1129
  * \return value of the column
 
1130
  */
 
1131
  wxDateTime GetTime(const wxString& columnName);
 
1132
 
 
1133
  /// Get a column as a date/time value using the column index
 
1134
  /**
 
1135
  * \param columnIndex index of the column. Indices start with 0.
 
1136
  * \return value of the column
 
1137
  */
 
1138
  wxDateTime GetDateTime(int columnIndex);
 
1139
 
 
1140
  /// Get a column as a date/time value using the column name
 
1141
  /**
 
1142
  * \param columnName name of the column
 
1143
  * \return value of the column
 
1144
  */
 
1145
  wxDateTime GetDateTime(const wxString& columnName);
 
1146
 
 
1147
  /// Get a column as a boolean using the column index
 
1148
  /**
 
1149
  * \param columnIndex index of the column. Indices start with 0.
 
1150
  * \return value of the column
 
1151
  */
 
1152
  bool GetBool(int columnIndex);
 
1153
 
 
1154
  /// Get a column as a boolean using the column name
 
1155
  /**
 
1156
  * \param columnName name of the column
 
1157
  * \return value of the column
 
1158
  */
 
1159
  bool GetBool(const wxString& columnName);
 
1160
 
 
1161
  /// Check whether the column selected by index is a NULL value
 
1162
  /**
 
1163
  * \param columnIndex index of the column. Indices start with 0.
 
1164
  * \return TRUE if the value is NULL, FALSE otherwise
 
1165
  */
 
1166
  bool IsNull(int columnIndex);
 
1167
 
 
1168
  /// Check whether the column selected by name is a NULL value
 
1169
  /**
 
1170
  * \param columnName name of the column
 
1171
  * \return TRUE if the value is NULL, FALSE otherwise
 
1172
  */
 
1173
  bool IsNull(const wxString& columnName);
 
1174
 
 
1175
  /// Set the current row
 
1176
  /**
 
1177
  * \param row index of the requested row. Indices start with 0.
 
1178
  */
 
1179
  void SetRow(int row);
 
1180
 
 
1181
  /// Finalize the result set
 
1182
  /**
 
1183
  */
 
1184
  void Finalize();
 
1185
 
 
1186
  /// Validate associated SQLite resultset
 
1187
  /**
 
1188
  * \return TRUE if SQLite resultset is associated, FALSE otherwise
 
1189
  */
 
1190
  bool IsOk();
 
1191
 
 
1192
private:
 
1193
    /// Check for valid results
 
1194
    void CheckResults();
 
1195
 
 
1196
    int m_cols;        ///< Number of columns
 
1197
    int m_rows;        ///< Number of rows
 
1198
    int m_currentRow;  ///< Index of the current row
 
1199
    char** m_results;  ///< SQLite3 result buffer
 
1200
};
 
1201
 
 
1202
 
 
1203
/// Represents a prepared SQL statement
 
1204
class EXTAPI wxSQLite3Statement
 
1205
{
 
1206
public:
 
1207
  /// Constructor
 
1208
  /**
 
1209
  */
 
1210
  wxSQLite3Statement();
 
1211
 
 
1212
  /// Copy constructor
 
1213
  /**
 
1214
  */
 
1215
  wxSQLite3Statement(const wxSQLite3Statement& statement);
 
1216
 
 
1217
  /// Assignement constructor
 
1218
  /**
 
1219
  */
 
1220
  wxSQLite3Statement& operator=(const wxSQLite3Statement& statement);
 
1221
 
 
1222
  /// Constructor (internal use only)
 
1223
  /**
 
1224
  */
 
1225
  wxSQLite3Statement(void* db, void* stmt);
 
1226
 
 
1227
  /// Destructor
 
1228
  /**
 
1229
  */
 
1230
  virtual ~wxSQLite3Statement();
 
1231
 
 
1232
  /// Execute the database update represented by this statement
 
1233
  /**
 
1234
  * \return the number of database rows that were changed (or inserted or deleted)
 
1235
  */
 
1236
  int ExecuteUpdate();
 
1237
 
 
1238
  /// Execute the query represented by this statement
 
1239
  /**
 
1240
  * \param transferStatementOwnership if TRUE the ownership of the underlying SQLite 
 
1241
  * statement object is transferred to the created result set (default: FALSE)
 
1242
  * \return result set instance
 
1243
  * \note the transfer of ownership of the underlying SQLite statement object can be
 
1244
  * performed only once. If the transfer of ownership has been requested this
 
1245
  * wxSQL3Statement instance isn't usable anymore as soon as the result set is destroyed.
 
1246
  * If the transfer of ownership isn't requested the created result set can be used to
 
1247
  * retrieve the selected data rows only as long as this wxSQLite3Statement instance exists.
 
1248
  */
 
1249
  wxSQLite3ResultSet ExecuteQuery(bool transferStatementOwnership = false);
 
1250
 
 
1251
  /// Execute a scalar SQL query statement given as a wxString
 
1252
  /**
 
1253
  * Allows to easily retrieve the result of queries returning a single integer result
 
1254
  * like SELECT COUNT(*) FROM table WHERE condition.
 
1255
  * \return first column of first row as an int
 
1256
  */
 
1257
  int ExecuteScalar();
 
1258
 
 
1259
  /// Get the number of statement parameters
 
1260
  /**
 
1261
  * \return the number of parameters in the prepared statement
 
1262
  */
 
1263
  int GetParamCount();
 
1264
 
 
1265
  /// Get the index of a parameter with a given name
 
1266
  /**
 
1267
  * \param paramName
 
1268
  * \return the index of the parameter with the given name. The name must match exactly.
 
1269
  * If there is no parameter with the given name, return 0.
 
1270
  */
 
1271
  int GetParamIndex(const wxString& paramName);
 
1272
 
 
1273
  /// Get the name of a paramater at the given position
 
1274
  /**
 
1275
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1276
  * \return the name of the paramIndex-th parameter in the precompiled statement.
 
1277
  * Parameters of the form ":AAA" or "$VVV" have a name which is the string ":AAA" or "$VVV".
 
1278
  * Parameters of the form "?" have no name.
 
1279
  */
 
1280
  wxString GetParamName(int paramIndex);
 
1281
 
 
1282
  /// Bind parameter to a string value
 
1283
  /**
 
1284
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1285
  * \param stringValue value of the parameter
 
1286
  */
 
1287
  void Bind(int paramIndex, const wxString& stringValue);
 
1288
 
 
1289
  /// Bind parameter to a integer value
 
1290
  /**
 
1291
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1292
  * \param intValue value of the parameter
 
1293
  */
 
1294
  void Bind(int paramIndex, int intValue);
 
1295
 
 
1296
  /// Bind parameter to a 64-bit integer value
 
1297
  /**
 
1298
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1299
  * \param int64Value value of the parameter
 
1300
  */
 
1301
  void Bind(int paramIndex, wxLongLong int64Value);
 
1302
 
 
1303
  /// Bind parameter to a double value
 
1304
  /**
 
1305
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1306
  * \param doubleValue value of the parameter
 
1307
  */
 
1308
  void Bind(int paramIndex, double doubleValue);
 
1309
 
 
1310
  /// Bind parameter to a utf-8 character string value
 
1311
  /**
 
1312
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1313
  * \param charValue value of the parameter
 
1314
  */
 
1315
  void Bind(int paramIndex, const char* charValue);
 
1316
 
 
1317
  /// Bind parameter to a BLOB value
 
1318
  /**
 
1319
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1320
  * \param blobValue value of the parameter
 
1321
  * \param blobLen length of the blob in bytes
 
1322
  */
 
1323
  void Bind(int paramIndex, const unsigned char* blobValue, int blobLen);
 
1324
 
 
1325
  /// Bind parameter to a BLOB value
 
1326
  /**
 
1327
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1328
  * \param blobValue value of the parameter
 
1329
  */
 
1330
  void Bind(int paramIndex, const wxMemoryBuffer& blobValue);
 
1331
 
 
1332
  /// Bind parameter to a date value
 
1333
  /**
 
1334
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1335
  * \param date value of the parameter
 
1336
  */
 
1337
  void BindDate(int paramIndex, const wxDateTime& date);
 
1338
 
 
1339
  /// Bind parameter to a time value
 
1340
  /**
 
1341
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1342
  * \param time value of the parameter
 
1343
  */
 
1344
  void BindTime(int paramIndex, const wxDateTime& time);
 
1345
 
 
1346
  /// Bind parameter to a date and time value
 
1347
  /**
 
1348
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1349
  * \param datetime value of the parameter
 
1350
  */
 
1351
  void BindDateTime(int paramIndex, const wxDateTime& datetime);
 
1352
 
 
1353
  /// Bind parameter to a timestamp value
 
1354
  /**
 
1355
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1356
  * \param timestamp value of the parameter
 
1357
  */
 
1358
  void BindTimestamp(int paramIndex, const wxDateTime& timestamp);
 
1359
 
 
1360
  /// Bind parameter to a date and time value
 
1361
  /**
 
1362
  * The date/time value is transferred to the database as a numeric value (i.e. int64).
 
1363
  *
 
1364
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1365
  * \param datetime value of the parameter
 
1366
  */
 
1367
  void BindNumericDateTime(int paramIndex, const wxDateTime& datetime);
 
1368
 
 
1369
  /// Bind parameter to a date and time value
 
1370
  /**
 
1371
  * The date/time value is transferred to the database as a Julian Day Number value (i.e. double).
 
1372
  *
 
1373
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1374
  * \param datetime value of the parameter
 
1375
  */
 
1376
  void BindJulianDayNumber(int paramIndex, const wxDateTime& datetime);
 
1377
 
 
1378
  /// Bind parameter to a boolean value
 
1379
  /**
 
1380
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1381
  * \param value value of the parameter
 
1382
  */
 
1383
  void BindBool(int paramIndex, bool value);
 
1384
 
 
1385
  /// Bind parameter to a NULL value
 
1386
  /**
 
1387
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1388
  */
 
1389
  void BindNull(int paramIndex);
 
1390
 
 
1391
  /// Bind parameter to a Zero BLOB value
 
1392
  /**
 
1393
  * Space for a BLOB is reserved and filled with binary zeros for later reference
 
1394
  * through a BLOB handle.
 
1395
  *
 
1396
  * \param paramIndex index of the parameter. The first parameter has an index of 1.
 
1397
  * \param blobSize size of the BLOB
 
1398
  */
 
1399
  void BindZeroBlob(int paramIndex, int blobSize);
 
1400
 
 
1401
  /// Clear all parameter bindings
 
1402
  /**
 
1403
  * Sets all the parameters in the prepared SQL statement back to NULL.
 
1404
  */
 
1405
  void ClearBindings();
 
1406
 
 
1407
  /// Get the original SQL string for the prepared statement
 
1408
  /**
 
1409
  * \return the original SQL string used to prepare the statement
 
1410
  */
 
1411
  wxString GetSQL();
 
1412
 
 
1413
  /// Reset the prepared statement
 
1414
  /**
 
1415
  * Resets the statement back to it's initial state, ready to be re-executed.
 
1416
  * Any SQL statement variables that had values bound to them retain their values.
 
1417
  */
 
1418
  void Reset();
 
1419
 
 
1420
  /// Determine whether the statement is read-only
 
1421
  /**
 
1422
  * \return TRUE if the statement is read-only, FALSE otherwise
 
1423
  * \since SQLite3 version 3.7.4
 
1424
  * \note For SQLite3 version before version 3.7.4 this method returns always FALSE.
 
1425
  */
 
1426
  bool IsReadOnly();
 
1427
 
 
1428
  /// Finalize the prepared statement
 
1429
  /**
 
1430
  */
 
1431
  void Finalize();
 
1432
 
 
1433
  /// Validate associated SQLite database and statement
 
1434
  /**
 
1435
  * \return TRUE if both, a SQLite database and a SQLite statement, are associated, FALSE otherwise
 
1436
  */
 
1437
  bool IsOk();
 
1438
 
 
1439
private:
 
1440
  /// Check for valid database connection
 
1441
  void CheckDatabase();
 
1442
 
 
1443
  /// Check for valid statement
 
1444
  void CheckStmt();
 
1445
 
 
1446
  void* m_db;            ///< associated SQLite3 database
 
1447
  void* m_stmt;          ///< associated SQLite3 statement
 
1448
  bool  m_hasOwnership;  ///< flag whether the associated SQLite3 statement is owned
 
1449
};
 
1450
 
 
1451
 
 
1452
/// Represents a SQLite BLOB handle
 
1453
class EXTAPI wxSQLite3Blob
 
1454
{
 
1455
public:
 
1456
  /// Constructor
 
1457
  /**
 
1458
  */
 
1459
  wxSQLite3Blob();
 
1460
 
 
1461
  /// Copy constructor
 
1462
  /**
 
1463
  */
 
1464
  wxSQLite3Blob(const wxSQLite3Blob& blob);
 
1465
 
 
1466
  /// Assignement constructor
 
1467
  /**
 
1468
  */
 
1469
  wxSQLite3Blob& operator=(const wxSQLite3Blob& blob);
 
1470
 
 
1471
  /// Constructor (internal use only)
 
1472
  /**
 
1473
  */
 
1474
  wxSQLite3Blob(void* m_db, void* blobHandle, bool writable);
 
1475
 
 
1476
  /// Destructor
 
1477
  /**
 
1478
  */
 
1479
  virtual ~wxSQLite3Blob();
 
1480
 
 
1481
  /// Read partial BLOB value
 
1482
  /**
 
1483
   * \param blobValue memory buffer receiving the partial content of the BLOB
 
1484
   * \param length length of BLOB content to be read
 
1485
   * \param offset offset within BLOB where the read starts
 
1486
   * \return the address of the memory buffer
 
1487
  */
 
1488
  wxMemoryBuffer& Read(wxMemoryBuffer& blobValue, int length, int offset);
 
1489
 
 
1490
  /// Write partial BLOB value
 
1491
  /**
 
1492
   * \param blobValue memory buffer receiving the partial content of the BLOB
 
1493
   * \param offset offset within BLOB where the read starts
 
1494
  */
 
1495
  void Write(const wxMemoryBuffer& blobValue, int offset);
 
1496
 
 
1497
  /// Check whether the BLOB handle is correctly initialized
 
1498
  /**
 
1499
   * \return TRUE if the BLOB handle is correctly initialized, FALSE otherweis
 
1500
  */
 
1501
  bool IsOk();
 
1502
 
 
1503
  /// Check whether the BLOB handle is read only
 
1504
  /**
 
1505
   * \return TRUE if the BLOB handle is readonly, FALSE otherweis
 
1506
  */
 
1507
  bool IsReadOnly();
 
1508
 
 
1509
  /// Get the size of the associated BLOB
 
1510
  /**
 
1511
   * \return the BLOB size
 
1512
  */
 
1513
  int GetSize();
 
1514
 
 
1515
  /// Rebind the associated BLOB to a new row
 
1516
  /**
 
1517
   * Please refer to the SQLite documentation for further information
 
1518
   * (see function sqlite3_blob_reopen)
 
1519
   * \param rowid id of the row to which the BLOB should be rebound
 
1520
   * \since SQLite3 version 3.7.4
 
1521
  */
 
1522
  void Rebind(wxLongLong rowid);
 
1523
 
 
1524
  /// Finalize the BLOB
 
1525
  /**
 
1526
  */
 
1527
  void Finalize();
 
1528
 
 
1529
private:
 
1530
  /// Check for valid BLOB
 
1531
  void CheckBlob();
 
1532
 
 
1533
  void* m_db;       ///< associated SQLite3 database handle
 
1534
  void* m_blob;     ///< associated SQLite3 BLOB handle
 
1535
  bool  m_ok;       ///< flag whether the BLOB handle is correctly initialized
 
1536
  bool  m_writable; ///< flag whether the BLOB is writable or read only
 
1537
};
 
1538
 
 
1539
/// Represents a named collection
 
1540
/**
 
1541
* A named collection is designed to facilitate using an array of
 
1542
* integers or strings as the right-hand side of an IN operator.
 
1543
* So instead of doing a prepared statement like this:
 
1544
*
 
1545
*     SELECT * FROM table WHERE x IN (?,?,?,...,?);
 
1546
*
 
1547
* And then binding indivdual integers to each of ? slots, an application
 
1548
* can create a named collection object (named "ex1" in the following
 
1549
* example), prepare a statement like this:
 
1550
*
 
1551
*     SELECT * FROM table WHERE x IN ex1;
 
1552
*
 
1553
* Then bind an array of integer or string values to the ex1 object
 
1554
* to run the statement.
 
1555
*
 
1556
* USAGE:
 
1557
*
 
1558
* One or more named collection objects can be created as follows:
 
1559
*
 
1560
*      wxSQLite3IntegerCollection p1, p2, p3;
 
1561
*      p1 = db.CreateIntegerCollection("ex1");
 
1562
*      p2 = db.CreateIntegerCollection("ex2");
 
1563
*      p3 = db.CreateIntegerCollection("ex3");
 
1564
*
 
1565
* Each call to CreateIntegerCollection generates a new virtual table
 
1566
* module and a singleton of that virtual table module in the TEMP
 
1567
* database.  Both the module and the virtual table instance use the
 
1568
* name given by the second parameter.  The virtual tables can then be
 
1569
* used in prepared statements:
 
1570
*
 
1571
*      SELECT * FROM t1, t2, t3
 
1572
*       WHERE t1.x IN ex1
 
1573
*         AND t2.y IN ex2
 
1574
*         AND t3.z IN ex3;
 
1575
*
 
1576
* Each integer array is initially empty.  New arrays can be bound to
 
1577
* an integer array as follows:
 
1578
*
 
1579
*     int a1[] = { 1, 2, 3, 4 };
 
1580
*     int a2[] = { 5, 6, 7, 8, 9, 10, 11 };
 
1581
*     wxArrayInt a3;
 
1582
*     // Fill a3
 
1583
*     p1.Bind(4, a1);
 
1584
*     p2.Bind(7, a2);
 
1585
*     p3.Bind(a3);
 
1586
*
 
1587
* A single named collection object can be rebound multiple times.  But do not
 
1588
* attempt to change the bindings of a named collection while it is in the middle
 
1589
* of a query.
 
1590
*
 
1591
* The array that holds the integer or string values is automatically allocated
 
1592
* by the Bind method.
 
1593
*
 
1594
* The named collection object is automatically destroyed when its corresponding
 
1595
* virtual table is dropped.  Since the virtual tables are created in the
 
1596
* TEMP database, they are automatically dropped when the database connection
 
1597
* closes so the application does not normally need to take any special
 
1598
* action to free the named collection objects.
 
1599
*/
 
1600
class EXTAPI wxSQLite3NamedCollection
 
1601
{
 
1602
public:
 
1603
  /// Constructor
 
1604
  wxSQLite3NamedCollection();
 
1605
 
 
1606
  /// Copy constructor
 
1607
  wxSQLite3NamedCollection(const wxSQLite3NamedCollection& collection);
 
1608
 
 
1609
  /// Assignement constructor
 
1610
  wxSQLite3NamedCollection& operator=(const wxSQLite3NamedCollection& collection);
 
1611
 
 
1612
  /// Constructor (internal use only)
 
1613
  wxSQLite3NamedCollection(const wxString& collectionName, void* collectionData);
 
1614
 
 
1615
  /// Destructor
 
1616
  virtual ~wxSQLite3NamedCollection();
 
1617
 
 
1618
  /// Get the name of the collection
 
1619
  /**
 
1620
  * \return the name of the collection
 
1621
  */
 
1622
  const wxString& GetName() { return m_name; }
 
1623
 
 
1624
protected:
 
1625
  wxString m_name; ///< Name of the collection
 
1626
  void*    m_data; ///< Reference to the actual array of values representing the collection
 
1627
 
 
1628
  friend class wxSQLite3Database;
 
1629
};
 
1630
 
 
1631
/// Represents a named integer value collection
 
1632
class EXTAPI wxSQLite3IntegerCollection : public wxSQLite3NamedCollection
 
1633
{
 
1634
public:
 
1635
  /// Constructor
 
1636
  wxSQLite3IntegerCollection();
 
1637
 
 
1638
  /// Copy constructor
 
1639
  wxSQLite3IntegerCollection(const wxSQLite3IntegerCollection& collection);
 
1640
 
 
1641
  /// Assignement constructor
 
1642
  wxSQLite3IntegerCollection& operator=(const wxSQLite3IntegerCollection& collection);
 
1643
 
 
1644
  /// Constructor (internal use only)
 
1645
  wxSQLite3IntegerCollection(const wxString& collectionName, void* collectionData);
 
1646
 
 
1647
  /// Destructor
 
1648
  virtual ~wxSQLite3IntegerCollection();
 
1649
 
 
1650
  /// Bind a new array of integer values
 
1651
  /**
 
1652
  * Bind a new array of integer values to this named collection object.
 
1653
  * \param integerCollection array of integer values to be bound
 
1654
  * \note Binding values to a named collection after closing the corresponding
 
1655
  * database results in undefined behaviour, i.e. the application is likely to crash.
 
1656
  */
 
1657
  void Bind(const wxArrayInt& integerCollection);
 
1658
 
 
1659
  /// Bind a new array of integer values
 
1660
  /**
 
1661
  * Bind a new array of integer values to this named collection object.
 
1662
  * \param n number of elements in the array
 
1663
  * \param integerCollection array of integer values to be bound
 
1664
  * \note Binding values to a named collection after closing the corresponding
 
1665
  * database results in undefined behaviour, i.e. the application is likely to crash.
 
1666
  */
 
1667
  void Bind(int n, int* integerCollection);
 
1668
 
 
1669
private:
 
1670
  friend class wxSQLite3Database;
 
1671
};
 
1672
 
 
1673
/// Represents a named string value collection
 
1674
class EXTAPI wxSQLite3StringCollection : public wxSQLite3NamedCollection
 
1675
{
 
1676
public:
 
1677
  /// Constructor
 
1678
  wxSQLite3StringCollection();
 
1679
 
 
1680
  /// Copy constructor
 
1681
  wxSQLite3StringCollection(const wxSQLite3StringCollection& collection);
 
1682
 
 
1683
  /// Assignement constructor
 
1684
  wxSQLite3StringCollection& operator=(const wxSQLite3StringCollection& collection);
 
1685
 
 
1686
  /// Constructor (internal use only)
 
1687
  wxSQLite3StringCollection(const wxString& collectionName, void* collectionData);
 
1688
 
 
1689
  /// Destructor
 
1690
  virtual ~wxSQLite3StringCollection();
 
1691
 
 
1692
  /// Bind a new array of integer values
 
1693
  /**
 
1694
  * Bind a new array of integer values to this named collection object.
 
1695
  * \param stringCollection array of integer values to be bound
 
1696
  * \note Binding values to a named collection after closing the corresponding
 
1697
  * database results in undefined behaviour, i.e. the application is likely to crash.
 
1698
  */
 
1699
  void Bind(const wxArrayString& stringCollection);
 
1700
 
 
1701
private:
 
1702
  friend class wxSQLite3Database;
 
1703
};
 
1704
 
 
1705
/// Represents a SQLite3 database object
 
1706
class EXTAPI wxSQLite3Database
 
1707
{
 
1708
public:
 
1709
  /// Default constructor
 
1710
  /**
 
1711
  * Initializes a SQLite database object.
 
1712
  * The SQLite database object can only be used in the same thread in which it was created.
 
1713
  */
 
1714
  wxSQLite3Database();
 
1715
 
 
1716
  /// Destructor
 
1717
  /**
 
1718
  * Destructs a SQLite database object.
 
1719
  * The database will be closed implicitly if it is still open.
 
1720
  */
 
1721
  virtual ~wxSQLite3Database();
 
1722
 
 
1723
  /// Open a SQLite3 database
 
1724
  /**
 
1725
  * Opens the sqlite database file "filename". The "filename" is UTF-8 encoded.
 
1726
  * If the database could not be opened (or created) successfully, then an exception is thrown.
 
1727
  * If the database file does not exist, then a new database will be created as needed.
 
1728
  * \param[in] fileName Name of the database file.
 
1729
  * \param[in] key Database encryption key.
 
1730
  * \param[in] flags Control over the database connection (see http://www.sqlite.org/c3ref/open.html for further information).
 
1731
  * Flag values are prefixed by WX to distinguish them from the original SQLite flag values.
 
1732
  */
 
1733
  void Open(const wxString& fileName, const wxString& key = wxEmptyString,
 
1734
            int flags = WXSQLITE_OPEN_READWRITE | WXSQLITE_OPEN_CREATE);
 
1735
 
 
1736
  /// Open a SQLite3 database using a binary key
 
1737
  /**
 
1738
  * Opens the sqlite database file "filename". The "filename" is UTF-8 encoded.
 
1739
  * If the database could not be opened (or created) successfully, then an exception is thrown.
 
1740
  * If the database file does not exist, then a new database will be created as needed.
 
1741
  * \param[in] fileName Name of the database file.
 
1742
  * \param[in] key Database encryption key.
 
1743
  * \param[in] flags Control over the database connection (see http://www.sqlite.org/c3ref/open.html for further information).
 
1744
  * Flag values are prefixed by WX to distinguish them from the original SQLite flag values.
 
1745
  */
 
1746
  void Open(const wxString& fileName, const wxMemoryBuffer& key,
 
1747
            int flags = WXSQLITE_OPEN_READWRITE | WXSQLITE_OPEN_CREATE);
 
1748
 
 
1749
  /// Check whether the database has been opened
 
1750
  /**
 
1751
  * \return TRUE if database has been opened, FALSE otherwise
 
1752
  */
 
1753
  bool IsOpen() const;
 
1754
 
 
1755
  /// Close a SQLite3 database
 
1756
  /**
 
1757
  * Take care that all prepared statements have been finalized!
 
1758
  * Starting with version 3.6.0 SQLite has support to finialize all unfinalized
 
1759
  * prepared statements. The Close method has been changed to take advantage of
 
1760
  * this feature. Nevertheless it is recommended to explicitly finalize all
 
1761
  * wxSQLite3Statement instances before closing a database.
 
1762
  *
 
1763
  * NOTE: Finalizing all wxSQLite3Blob instances before closing a database is still required!
 
1764
  *
 
1765
  */
 
1766
  void Close();
 
1767
 
 
1768
  /// Backup a SQLite3 database
 
1769
  /**
 
1770
  * This method is used to overwrite the contents of a database with the contents
 
1771
  * of this database. This is useful either for creating backups of the database or
 
1772
  * for copying an in-memory database to persistent files.
 
1773
  *
 
1774
  * NOTE: Exclusive access is required to the target database for the
 
1775
  * duration of the operation. However the source database is only
 
1776
  * read-locked while it is actually being read, it is not locked
 
1777
  * continuously for the entire operation. Thus, the backup may be
 
1778
  * performed on a live database without preventing other users from
 
1779
  * writing to the database for an extended period of time.
 
1780
  *
 
1781
  * NOTE: If the target database file already exists it must be a valid
 
1782
  * SQLite database, in case of an encrypted database the key used for
 
1783
  * backup must be the same as the key used for creation.
 
1784
  * If this does not hold true, the file should be deleted prior to
 
1785
  * performing the backup.
 
1786
  *
 
1787
  * \param[in] targetFileName Name of the target database file.
 
1788
  * \param[in] key Optional database encryption key for the target database.
 
1789
  * \param[in] sourceDatabaseName Optional name of the source database (default: 'main').
 
1790
  */
 
1791
  void Backup(const wxString& targetFileName, const wxString& key = wxEmptyString, const wxString& sourceDatabaseName = wxT("main"));
 
1792
 
 
1793
  /// Backup a SQLite3 database
 
1794
  /**
 
1795
  * This method is used to overwrite the contents of a database with the contents
 
1796
  * of this database. This is useful either for creating backups of the database or
 
1797
  * for copying an in-memory database to persistent files.
 
1798
  *
 
1799
  * NOTE: Exclusive access is required to the target database for the
 
1800
  * duration of the operation. However the source database is only
 
1801
  * read-locked while it is actually being read, it is not locked
 
1802
  * continuously for the entire operation. Thus, the backup may be
 
1803
  * performed on a live database without preventing other users from
 
1804
  * writing to the database for an extended period of time.
 
1805
  *
 
1806
  * NOTE: If the target database file already exists it must be a valid
 
1807
  * SQLite database, in case of an encrypted database the key used for
 
1808
  * backup must be the same as the key used for creation.
 
1809
  * If this does not hold true, the file should be deleted prior to
 
1810
  * performing the backup.
 
1811
  *
 
1812
  * \param[in] targetFileName Name of the target database file.
 
1813
  * \param[in] key Binary database encryption key for the target database.
 
1814
  * \param[in] sourceDatabaseName Optional name of the source database (default: 'main').
 
1815
  */
 
1816
  void Backup(const wxString& targetFileName, const wxMemoryBuffer& key, const wxString& sourceDatabaseName = wxT("main"));
 
1817
 
 
1818
  /// Restore a SQLite3 database
 
1819
  /**
 
1820
  * This method is used to restore the contents of this database with the contents
 
1821
  * of another database. This is useful either for restoring a backup of the database or
 
1822
  * for copying a persistent file to an in-memory database.
 
1823
  *
 
1824
  * NOTE: Exclusive access is required to the target database for the
 
1825
  * duration of the operation. However the source database is only
 
1826
  * read-locked while it is actually being read, it is not locked
 
1827
  * continuously for the entire operation. Thus, the backup may be
 
1828
  * performed on a live database without preventing other users from
 
1829
  * writing to the database for an extended period of time.
 
1830
  *
 
1831
  * \param[in] sourceFileName Name of the source database file.
 
1832
  * \param[in] key Optional database encryption key for the source database.
 
1833
  * \param[in] targetDatabaseName Optional name of the target database (default: 'main').
 
1834
  */
 
1835
  void Restore(const wxString& sourceFileName, const wxString& key = wxEmptyString, const wxString& targetDatabaseName = wxT("main"));
 
1836
 
 
1837
  /// Restore a SQLite3 database
 
1838
  /**
 
1839
  * This method is used to restore the contents of this database with the contents
 
1840
  * of another database. This is useful either for restoring a backup of the database or
 
1841
  * for copying a persistent file to an in-memory database.
 
1842
  *
 
1843
  * NOTE: Exclusive access is required to the target database for the
 
1844
  * duration of the operation. However the source database is only
 
1845
  * read-locked while it is actually being read, it is not locked
 
1846
  * continuously for the entire operation. Thus, the backup may be
 
1847
  * performed on a live database without preventing other users from
 
1848
  * writing to the database for an extended period of time.
 
1849
  *
 
1850
  * \param[in] sourceFileName Name of the source database file.
 
1851
  * \param[in] key Optional binary database encryption key for the source database.
 
1852
  * \param[in] targetDatabaseName Optional name of the target database (default: 'main').
 
1853
  */
 
1854
  void Restore(const wxString& sourceFileName, const wxMemoryBuffer& key, const wxString& targetDatabaseName = wxT("main"));
 
1855
 
 
1856
  /// Begin transaction
 
1857
  /**
 
1858
  * In SQLite transactions can be deferred, immediate, or exclusive.
 
1859
  * Deferred means that no locks are acquired on the database until the database is first accessed.
 
1860
  * Thus with a deferred transaction, the BEGIN statement itself does nothing. Locks are not
 
1861
  * acquired until the first read or write operation. The first read operation against a database
 
1862
  * creates a SHARED lock and the first write operation creates a RESERVED lock. Because the
 
1863
  * acquisition of locks is deferred until they are needed, it is possible that another thread or
 
1864
  * process could create a separate transaction and write to the database after the BEGIN on the
 
1865
  * current thread has executed. If the transaction is immediate, then RESERVED locks are acquired
 
1866
  * on all databases as soon as the BEGIN command is executed, without waiting for the database to
 
1867
  * be used. After a BEGIN IMMEDIATE, it is guaranteed that no other thread or process will be able
 
1868
  * to write to the database or do a BEGIN IMMEDIATE or BEGIN EXCLUSIVE. Other processes can continue
 
1869
  * to read from the database, however. An exclusive transaction causes EXCLUSIVE locks to be acquired
 
1870
  * on all databases. After a BEGIN EXCLUSIVE, it is guaranteed that no other thread or process will
 
1871
  * be able to read or write the database until the transaction is complete.
 
1872
  *
 
1873
  * \param[in] transactionType type of transaction (default: DEFERRED).
 
1874
  */
 
1875
  void Begin(wxSQLite3TransactionType transactionType = WXSQLITE_TRANSACTION_DEFAULT);
 
1876
 
 
1877
  /// Commit transaction
 
1878
  /**
 
1879
  */
 
1880
  void Commit();
 
1881
 
 
1882
  /// Rollback transaction
 
1883
  /**
 
1884
  * Rolls back a transaction or optionally to a previously set savepoint
 
1885
  *
 
1886
  * \param savepointName optional name of a previously set savepoint
 
1887
  */
 
1888
  void Rollback(const wxString& savepointName = wxEmptyString);
 
1889
 
 
1890
  /// Get the auto commit state
 
1891
  /**
 
1892
  * Test to see whether or not the database connection is in autocommit mode.
 
1893
  * \return TRUE if it is and FALSE if not.
 
1894
  * Autocommit mode is on by default. Autocommit is disabled by a BEGIN statement
 
1895
  * and reenabled by the next COMMIT or ROLLBACK.
 
1896
  */
 
1897
  bool GetAutoCommit();
 
1898
 
 
1899
  /// Set savepoint
 
1900
  /*
 
1901
  * Sets a savepoint with a given name
 
1902
  *
 
1903
  * \param savepointName the name of the savepoint
 
1904
  */
 
1905
  void Savepoint(const wxString& savepointName);
 
1906
 
 
1907
  /// Release savepoint
 
1908
  /*
 
1909
  * Releases a savepoint with a given name
 
1910
  *
 
1911
  * \param savepointName the name of the savepoint
 
1912
  */
 
1913
  void ReleaseSavepoint(const wxString& savepointName);
 
1914
 
 
1915
  /// Check whether a table with the given name exists
 
1916
  /**
 
1917
  * Checks the main database or a specific attached database for existence of a table
 
1918
  * with a given name.
 
1919
  *
 
1920
  * \param tableName name of the table
 
1921
  * \param databaseName optional name of an attached database
 
1922
  * \return TRUE if the table exists, FALSE otherwise
 
1923
  */
 
1924
  bool TableExists(const wxString& tableName, const wxString& databaseName = wxEmptyString);
 
1925
 
 
1926
  /// Check whether a table with the given name exists in the main database or any attached database
 
1927
  /**
 
1928
  * \param tableName name of the table
 
1929
  * \param databaseNames list of the names of those databases in which the table exists
 
1930
  * \return TRUE if the table exists at least in one database, FALSE otherwise
 
1931
  */
 
1932
  bool TableExists(const wxString& tableName, wxArrayString& databaseNames);
 
1933
 
 
1934
  /// Get a list containing the names of all attached databases including the main database
 
1935
  /**
 
1936
  * \param databaseNames contains on return the list of the database names
 
1937
  */
 
1938
  void GetDatabaseList(wxArrayString& databaseNames);
 
1939
 
 
1940
  /// Get a list containing the names of all attached databases including the main database
 
1941
  /**
 
1942
  * \param databaseNames contains on return the list of the database names
 
1943
  * \param databaseFiles contains on return the list of the database file names
 
1944
  */
 
1945
  void GetDatabaseList(wxArrayString& databaseNames, wxArrayString& databaseFiles);
 
1946
 
 
1947
  /// Enable or disable foreign key support
 
1948
  /**
 
1949
  * Starting with SQLite version 3.6.19 foreign key constraints can be enforced.
 
1950
  * Foreign key constraints are disabled by default (for backwards compatibility),
 
1951
  * so they must be enabled separately for each database connection.
 
1952
  * \note Future releases of SQLite might change so that foreign key constraints
 
1953
  * are enabled by default. No assumptions should be made about whether or not
 
1954
  * foreign keys are enabled by default
 
1955
  * \return TRUE if the requested action succeeded, FALSE otherwise
 
1956
  */
 
1957
  bool EnableForeignKeySupport(bool enable);
 
1958
 
 
1959
  /// Check whether foreign key support is enabled for this database
 
1960
  /**
 
1961
  * \return TRUE if foreign key support is enabled, FALSE otherwise
 
1962
  */
 
1963
  bool IsForeignKeySupportEnabled();
 
1964
 
 
1965
  /// Set SQLite journal mode
 
1966
  /**
 
1967
  * \param mode the journal mode to be set
 
1968
  * \param database the attached database for which the journal mode should be set. If not given then
 
1969
  *                 the journal mode of all attached databases is set.
 
1970
  * \return the active journal mode
 
1971
  * \note The journal mode for an in-memory database  is either MEMORY or OFF and can not be changed
 
1972
  * to a different value. An attempt to change the journal mode of an in-memory database to any setting
 
1973
  * other than MEMORY or OFF is ignored. Note also that the journal mode cannot be changed while a
 
1974
  * transaction is active.
 
1975
  * The WAL journaling mode uses a write-ahead log instead of a rollback journal to implement transactions.
 
1976
  * The WAL journaling mode is persistent; after being set it stays in effect across multiple database
 
1977
  * connections and after closing and reopening the database. A database in WAL journaling mode can only be
 
1978
  * accessed by SQLite version 3.7.0 or later.
 
1979
  */
 
1980
  wxSQLite3JournalMode SetJournalMode(wxSQLite3JournalMode mode, const wxString& database = wxEmptyString);
 
1981
 
 
1982
  /// Get the active SQLite journal mode
 
1983
  /**
 
1984
  * \param database the attached database for which the journal mode should be queried (default: main)
 
1985
  * \return active journal mode
 
1986
  */
 
1987
  wxSQLite3JournalMode GetJournalMode(const wxString& database = wxEmptyString);
 
1988
 
 
1989
  /// Check the syntax of an SQL statement given as a wxString
 
1990
  /**
 
1991
  * \param sql query string
 
1992
  * \return TRUE if the syntax is correct, FALSE otherwise
 
1993
  */
 
1994
  bool CheckSyntax(const wxString& sql);
 
1995
 
 
1996
  /// Check the syntax of an SQL statement given as a statement buffer
 
1997
  /**
 
1998
  * \param sql query string
 
1999
  * \return TRUE if the syntax is correct, FALSE otherwise
 
2000
  */
 
2001
  bool CheckSyntax(const wxSQLite3StatementBuffer& sql);
 
2002
 
 
2003
  /// Check the syntax of an SQL statement given as a utf-8 character string
 
2004
  /**
 
2005
  * \param sql query string
 
2006
  * \return TRUE if the syntax is correct, FALSE otherwise
 
2007
  */
 
2008
  bool CheckSyntax(const char* sql);
 
2009
 
 
2010
  /// Execute a insert, update or delete SQL statement given as a wxString
 
2011
  /**
 
2012
  * \param sql query string
 
2013
  * \return the number of database rows that were changed (or inserted or deleted)
 
2014
  */
 
2015
  int ExecuteUpdate(const wxString& sql);
 
2016
 
 
2017
  /// Execute a insert, update or delete SQL statement given as a statement buffer
 
2018
  /**
 
2019
  * \param sql query string
 
2020
  * \return the number of database rows that were changed (or inserted or deleted)
 
2021
  */
 
2022
  int ExecuteUpdate(const wxSQLite3StatementBuffer& sql);
 
2023
 
 
2024
  /// Execute a insert, update or delete SQL statement given as a utf-8 character string
 
2025
  /**
 
2026
  * \param sql query string
 
2027
  * \return the number of database rows that were changed (or inserted or deleted)
 
2028
  */
 
2029
  int ExecuteUpdate(const char* sql);
 
2030
 
 
2031
  /// Execute a SQL query statement given as a wxString
 
2032
  /**
 
2033
  * \param sql query string
 
2034
  * \return result set instance
 
2035
  */
 
2036
  wxSQLite3ResultSet ExecuteQuery(const wxString& sql);
 
2037
 
 
2038
  /// Execute a SQL query statement given as a statement buffer
 
2039
  /**
 
2040
  * \param sql query string
 
2041
  * \return result set instance
 
2042
  */
 
2043
  wxSQLite3ResultSet ExecuteQuery(const wxSQLite3StatementBuffer& sql);
 
2044
 
 
2045
  /// Execute a SQL query statement given as a utf-8 character string
 
2046
  /**
 
2047
  * \param sql query string
 
2048
  * \return result set instance
 
2049
  */
 
2050
  wxSQLite3ResultSet ExecuteQuery(const char* sql);
 
2051
 
 
2052
  /// Execute a scalar SQL query statement given as a wxString
 
2053
  /**
 
2054
  * Allows to easily retrieve the result of queries returning a single integer result
 
2055
  * like SELECT COUNT(*) FROM table WHERE condition.
 
2056
  * \param sql query string
 
2057
  * \return first column of first row as an int
 
2058
  */
 
2059
  int ExecuteScalar(const wxString& sql);
 
2060
 
 
2061
  /// Execute a scalar SQL query statement given as a statement buffer
 
2062
  /**
 
2063
  * Allows to easily retrieve the result of queries returning a single integer result
 
2064
  * like SELECT COUNT(*) FROM table WHERE condition.
 
2065
  * \param sql query string
 
2066
  * \return first column of first row as an int
 
2067
  */
 
2068
  int ExecuteScalar(const wxSQLite3StatementBuffer& sql);
 
2069
 
 
2070
  /// Execute a scalar SQL query statement given as a utf-8 character string
 
2071
  /**
 
2072
  * Allows to easily retrieve the result of queries returning a single integer result
 
2073
  * like SELECT COUNT(*) FROM table WHERE condition.
 
2074
  * \param sql query string
 
2075
  * \return first column of first row as an int
 
2076
  */
 
2077
  int ExecuteScalar(const char* sql);
 
2078
 
 
2079
  /// Get the result table for a SQL query statement given as a wxString
 
2080
  /**
 
2081
  * Returns all resulting rows of the query for later processing.
 
2082
  * \param sql query string
 
2083
  * \return table instance
 
2084
  */
 
2085
  wxSQLite3Table GetTable(const wxString& sql);
 
2086
 
 
2087
  /// Get the result table for a SQL query statement given as a statement buffer
 
2088
  /**
 
2089
  * Returns all resulting rows of the query for later processing.
 
2090
  * \param sql query string
 
2091
  * \return table instance
 
2092
  */
 
2093
  wxSQLite3Table GetTable(const wxSQLite3StatementBuffer& sql);
 
2094
 
 
2095
  /// Get the result table for a SQL query statement given as a utf-8 character string
 
2096
  /**
 
2097
  * Returns all resulting rows of the query for later processing.
 
2098
  * \param sql query string
 
2099
  * \return table instance
 
2100
  */
 
2101
  wxSQLite3Table GetTable(const char* sql);
 
2102
 
 
2103
  /// Prepare a SQL query statement given as a wxString for parameter binding
 
2104
  /**
 
2105
  * \param sql query string
 
2106
  * \return statement instance
 
2107
  */
 
2108
  wxSQLite3Statement PrepareStatement(const wxString& sql);
 
2109
 
 
2110
  /// Prepare a SQL query statement given as a statement buffer for parameter binding
 
2111
  /**
 
2112
  * \param sql query string
 
2113
  * \return statement instance
 
2114
  */
 
2115
  wxSQLite3Statement PrepareStatement(const wxSQLite3StatementBuffer& sql);
 
2116
 
 
2117
  /// Prepare a SQL query statement given as a utf-8 character string for parameter binding
 
2118
  /**
 
2119
  * \param sql query string
 
2120
  * \return statement instance
 
2121
  */
 
2122
  wxSQLite3Statement PrepareStatement(const char* sql);
 
2123
 
 
2124
  /// Get the row id of last inserted row
 
2125
  /**
 
2126
  * Each entry in an SQLite table has a unique integer key.
 
2127
  * (The key is the value of the INTEGER PRIMARY KEY column if there is such a column,
 
2128
  * otherwise the key is generated at random. The unique key is always available as the
 
2129
  * ROWID, OID, or _ROWID_ column.)
 
2130
  * \return the integer key of the most recent insert in the database.
 
2131
  */
 
2132
  wxLongLong GetLastRowId();
 
2133
 
 
2134
  /// Get handle to a read only BLOB
 
2135
  /**
 
2136
  * \param rowId
 
2137
  * \param columnName
 
2138
  * \param tableName
 
2139
  * \param dbName
 
2140
  */
 
2141
  wxSQLite3Blob GetReadOnlyBlob(wxLongLong rowId,
 
2142
                                const wxString& columnName,
 
2143
                                const wxString& tableName,
 
2144
                                const wxString& dbName = wxEmptyString);
 
2145
 
 
2146
  /// Get handle to a writable BLOB
 
2147
  /**
 
2148
  * \param rowId
 
2149
  * \param columnName
 
2150
  * \param tableName
 
2151
  * \param dbName
 
2152
  */
 
2153
  wxSQLite3Blob GetWritableBlob(wxLongLong rowId,
 
2154
                                const wxString& columnName,
 
2155
                                const wxString& tableName,
 
2156
                                const wxString& dbName = wxEmptyString);
 
2157
 
 
2158
  /// Get handle to a BLOB
 
2159
  /**
 
2160
  * \param rowId
 
2161
  * \param columnName
 
2162
  * \param tableName
 
2163
  * \param dbName
 
2164
  * \param writable
 
2165
  */
 
2166
  wxSQLite3Blob GetBlob(wxLongLong rowId,
 
2167
                        const wxString& columnName,
 
2168
                        const wxString& tableName,
 
2169
                        const wxString& dbName = wxEmptyString,
 
2170
                        bool writable = true);
 
2171
 
 
2172
  /// Create a named integer value collection
 
2173
  /**
 
2174
  * Invoke this method to create a specific instance of an integer collection object.
 
2175
  * Initially the created collection is empty. Use it's Bind method to actually bind
 
2176
  * an array of values to the collection.
 
2177
  * \param collectionName name of the collection
 
2178
  * \return the new integer collection object.
 
2179
  *
 
2180
  * Each integer value collection object corresponds to a virtual table in the TEMP table
 
2181
  * with a name of collectionName.
 
2182
  *
 
2183
  * The virtual table will be dropped implicitly when the database connection is closed.
 
2184
  */
 
2185
  wxSQLite3IntegerCollection CreateIntegerCollection(const wxString& collectionName);
 
2186
 
 
2187
  /// Create a named string value collection
 
2188
  /**
 
2189
  * Invoke this method to create a specific instance of a string collection object.
 
2190
  * Initially the created collection is empty. Use it's Bind method to actually bind
 
2191
  * an array of values to the collection.
 
2192
  * \param collectionName name of the collection
 
2193
  * \return the new string collection object.
 
2194
  *
 
2195
  * Each integer value collection object corresponds to a virtual table in the TEMP table
 
2196
  * with a name of collectionName.
 
2197
  *
 
2198
  * The virtual table will be dropped implicitly when the database connection is closed.
 
2199
  */
 
2200
  wxSQLite3StringCollection CreateStringCollection(const wxString& collectionName);
 
2201
 
 
2202
  /// Interrupt a long running query
 
2203
  /**
 
2204
  * Causes any pending database operation to abort and return at its earliest opportunity.
 
2205
  * This method is typically called in response to a user action such as pressing "Cancel"
 
2206
  * or Ctrl-C where the user wants a long query operation to halt immediately.
 
2207
  */
 
2208
  void Interrupt();
 
2209
 
 
2210
  /// Set the busy timeout
 
2211
  /**
 
2212
  * This method sets a busy handler that sleeps for a while when a table is locked.
 
2213
  * The handler will sleep multiple times until at least "ms" milliseconds of sleeping
 
2214
  * have been done.
 
2215
  * Calling this routine with an argument less than or equal to zero turns off all busy handlers.
 
2216
  * \param milliSeconds timeout in milliseconds
 
2217
  */
 
2218
  void SetBusyTimeout(int milliSeconds);
 
2219
 
 
2220
  /// Create a user-defined scalar function
 
2221
  /**
 
2222
  * Registers a SQL scalar function with the database.
 
2223
  * \param name
 
2224
  * \param argCount number of arguments the scalar function takes.
 
2225
  *                 If this argument is -1 then the scalar function may take any number of arguments.
 
2226
  * \param function instance of an scalar function
 
2227
  * \return TRUE on successful registration, FALSE otherwise
 
2228
  */
 
2229
  bool CreateFunction(const wxString& name, int argCount, wxSQLite3ScalarFunction& function);
 
2230
 
 
2231
  /// Create a user-defined aggregate function
 
2232
  /**
 
2233
  * Registers a SQL aggregate function with the database.
 
2234
  * \param name
 
2235
  * \param argCount number of arguments the aggregate function takes.
 
2236
  *                 If this argument is -1 then the aggregate function may take any number of arguments.
 
2237
  * \param function instance of an aggregate function
 
2238
  * \return TRUE on successful registration, FALSE otherwise
 
2239
  */
 
2240
  bool CreateFunction(const wxString& name, int argCount, wxSQLite3AggregateFunction& function);
 
2241
 
 
2242
  /// Create a user-defined authorizer function
 
2243
  /**
 
2244
  * Registers an authorizer object with the SQLite library. The authorizer is invoked
 
2245
  * (at compile-time, not at run-time) for each attempt to access a column of a table in the database.
 
2246
  * The authorizer should return SQLITE_OK if access is allowed, SQLITE_DENY if the entire SQL statement
 
2247
  * should be aborted with an error and SQLITE_IGNORE if the column should be treated as a NULL value.
 
2248
  * \param authorizer instance of an authorizer function
 
2249
  * \return TRUE on successful registration, FALSE otherwise
 
2250
  */
 
2251
  bool SetAuthorizer(wxSQLite3Authorizer& authorizer);
 
2252
 
 
2253
  /// Create a user-defined commit callback function
 
2254
  /**
 
2255
  * Registers a callback function object to be invoked whenever a new transaction is committed.
 
2256
  * If the callback function returns non-zero, then the commit is converted into a rollback.
 
2257
  * Registering a NULL function object disables the callback. Only a single commit hook callback
 
2258
  * can be registered at a time.
 
2259
  * \param commitHook address of an instance of a commit callback function
 
2260
  */
 
2261
  void SetCommitHook(wxSQLite3Hook* commitHook);
 
2262
 
 
2263
  /// Create a user-defined rollback callback function
 
2264
  /**
 
2265
  * Registers a callback function object to be invoked whenever a transaction is rolled back.
 
2266
  * Registering a NULL function object disables the callback. Only a single rollback hook callback
 
2267
  * can be registered at a time.
 
2268
  *
 
2269
  * For the purposes of this API, a transaction is said to have been rolled back if an explicit
 
2270
  * "ROLLBACK" statement is executed, or an error or constraint causes an implicit rollback to occur.
 
2271
  * The callback is not invoked if a transaction is automatically rolled back because the database
 
2272
  * connection is closed.
 
2273
  * \param rollbackHook address of an instance of a rollback callback function
 
2274
  */
 
2275
  void SetRollbackHook(wxSQLite3Hook* rollbackHook);
 
2276
 
 
2277
  /// Create a user-defined update callback function
 
2278
  /**
 
2279
  * Registers a callback function object to be invoked whenever a row is updated, inserted or deleted.
 
2280
  * Registering a NULL function object disables the callback. Only a single commit hook callback
 
2281
  * can be registered at a time.
 
2282
  * The update hook is not invoked when internal system tables are modified (i.e. sqlite_master and sqlite_sequence).
 
2283
  * \param updateHook address of an instance of an update callback function
 
2284
  */
 
2285
  void SetUpdateHook(wxSQLite3Hook* updateHook);
 
2286
 
 
2287
  /// Create a user-defined Write Ahead Log callback function
 
2288
  /**
 
2289
  * Registers a callback function object to be invoked whenever a commit has taken place in WAL journal mode.
 
2290
  * Registering a NULL function object disables the callback. Only a single Write Ahead Log hook callback
 
2291
  * can be registered at a time.
 
2292
  * \param walHook address of an instance of a Write Ahead Log callback function
 
2293
  */
 
2294
  void SetWriteAheadLogHook(wxSQLite3Hook* walHook);
 
2295
 
 
2296
  /// Checkpoint database in write-ahead log mode
 
2297
  /**
 
2298
  * Causes an optionally named database to be checkpointed.
 
2299
  * If no database name is given, then a checkpoint is run on all databases associated with this
 
2300
  * database instance. If the database instance is not in write-ahead log mode then this method
 
2301
  * is a harmless no-op.
 
2302
  * \param database name of a database to be checkpointed
 
2303
  * \param mode checkpoint mode, allowed values: WXSQLITE_CHECKPOINT_PASSIVE (default),
 
2304
  *             WXSQLITE_CHECKPOINT_FULL, WXSQLITE_CHECKPOINT_RESTART
 
2305
  *             (see http://www.sqlite.org/c3ref/wal_checkpoint_v2.html)
 
2306
  * \param logFrameCount size of write-ahead log in frames
 
2307
  * \param ckptFrameCount number of frames actually checkpointed
 
2308
  * \note The frame counts are set to zero if the SQLite version is below 3.7.6.
 
2309
  */
 
2310
  void WriteAheadLogCheckpoint(const wxString& database, int mode = WXSQLITE_CHECKPOINT_PASSIVE,
 
2311
                               int* logFrameCount = NULL, int* ckptFrameCount = NULL);
 
2312
 
 
2313
  /// Automatically checkpoint database in write-ahead log mode
 
2314
  /**
 
2315
  * Causes any database associated with this database instance to automatically checkpoint after
 
2316
  * committing a transaction if there are N or more frames in the write-ahead log file.
 
2317
  * Passing zero or a negative value as the nFrame parameter disables automatic checkpoints entirely.
 
2318
  * \param frameCount frame threshold
 
2319
  */
 
2320
  void AutoWriteAheadLogCheckpoint(int frameCount);
 
2321
 
 
2322
  /// Create a user-defined collation sequence
 
2323
  /**
 
2324
  * Registers a callback function object to be invoked whenever this collation is needed
 
2325
  * in comparing strings.
 
2326
  * Registering a NULL function object disables the specified collation sequence.
 
2327
  * \param name name of a user-defined collation sequence
 
2328
  * \param collation address of an instance of a user-defined collation sequence
 
2329
  */
 
2330
  void SetCollation(const wxString& name, wxSQLite3Collation* collation);
 
2331
 
 
2332
  /// Return meta information about a specific column of a specific database table
 
2333
  /**
 
2334
  * \param dbName is either the name of the database (i.e. "main", "temp" or an attached database) or an empty string. If it is an empty string all attached databases are searched for the table.
 
2335
  * \param tableName name of the database table
 
2336
  * \param columnName name of the database column
 
2337
  * \param dataType declared data type of the column. Pass NULL if information not needed.
 
2338
  * \param collation name of the collation sequence. Pass NULL if information is not needed.
 
2339
  * \param notNull output flag whether the column has a not null constraint. Pass NULL if information not needed.
 
2340
  * \param primaryKey output flag whether the column is part of the primary key. Pass NULL if information not needed.
 
2341
  * \param autoIncrement output flag whether the column is an auto increment column. Pass NULL if information not needed.
 
2342
  *
 
2343
  * This method is only available if WXSQLITE3_HAVE_METADATA is defined and SQLite has been compiled with SQLITE_ENABLE_COLUMN_METADATA defined.
 
2344
  */
 
2345
  void GetMetaData(const wxString& dbName, const wxString& tableName, const wxString& columnName,
 
2346
                   wxString* dataType = NULL, wxString* collation = NULL,
 
2347
                   bool* notNull = NULL, bool* primaryKey = NULL, bool* autoIncrement = NULL);
 
2348
 
 
2349
  /// Load a database extension
 
2350
  /**
 
2351
  * \param fileName Name of the shared library containing extension.
 
2352
  * \param entryPoint Name of the entry point.
 
2353
  */
 
2354
  void LoadExtension(const wxString& fileName, const wxString& entryPoint = wxT("sqlite3_extension_init"));
 
2355
 
 
2356
  /// Enable or disable loading of database extensions
 
2357
  /**
 
2358
  * \param enable Flag whether to enable (TRUE) or disable (FALSE) loadable extensions
 
2359
  */
 
2360
  void EnableLoadExtension(bool enable);
 
2361
 
 
2362
  /// Change the encryption key of the database
 
2363
  /**
 
2364
  * If the database is currently not encrypted, this method will encrypt it.
 
2365
  * If an empty key (with key length == 0) is given, the database is decrypted.
 
2366
  *
 
2367
  * \param newKey The new encryption key (will be converted to UTF-8)
 
2368
  */
 
2369
  void ReKey(const wxString& newKey);
 
2370
 
 
2371
  /// Change the encryption key of the database
 
2372
  /**
 
2373
  * If the database is currently not encrypted, this method will encrypt it.
 
2374
  * If an empty key (with key length == 0) is given, the database is decrypted.
 
2375
  *
 
2376
  * \param newKey The new encryption key
 
2377
  */
 
2378
  void ReKey(const wxMemoryBuffer& newKey);
 
2379
 
 
2380
  /// Check whether the database is encrypted
 
2381
  /**
 
2382
  * Check whether the database has been opened using an encryption key.
 
2383
  *
 
2384
  * \return TRUE if database is encrypted, FALSE otherwise
 
2385
  */
 
2386
  bool IsEncrypted() const { return m_isEncrypted; }
 
2387
 
 
2388
  /// Query the value of a database limit
 
2389
  /**
 
2390
  * This method allows to query several database limits. Consult the SQLite
 
2391
  * documentation for further explanation.
 
2392
  *
 
2393
  * \param id The identifier of the limit to be queried
 
2394
  * \return the current value of the queried limit
 
2395
  */
 
2396
  int GetLimit(wxSQLite3LimitType id);
 
2397
 
 
2398
  /// Change a database limit to a new value
 
2399
  /**
 
2400
  * This method allows to change several database limits. Consult the SQLite
 
2401
  * documentation for further explanation.
 
2402
  *
 
2403
  * \param id The identifier of the limit to be queried
 
2404
  * \param newValue The new value of the limit to be set
 
2405
  * \return the previous value of the specified limit
 
2406
  */
 
2407
  int SetLimit(wxSQLite3LimitType id, int newValue);
 
2408
 
 
2409
  /// Convert database limit type to string
 
2410
  /**
 
2411
  * \param type The database limit type to be converted to string representation.
 
2412
  */
 
2413
  static wxString LimitTypeToString(wxSQLite3LimitType type);
 
2414
 
 
2415
  /// Initialize the SQLite library
 
2416
  /**
 
2417
  * Starting with SQLite version 3.6.0 there is a new method to initialize
 
2418
  * the SQLite library. Currently an explicit call to this method is not
 
2419
  * required, but this behaviour might change in the future of SQLite.
 
2420
  * Therefore it is recommended to call this method once before accessing
 
2421
  * any SQLite databases.
 
2422
  */
 
2423
  static void InitializeSQLite();
 
2424
 
 
2425
  /// Shutdown the SQLite library
 
2426
  /**
 
2427
  * Starting with SQLite version 3.6.0 there is a new method to shutdown
 
2428
  * the SQLite library. Currently an explicit call to this method is not
 
2429
  * required, but this behaviour might change in the future of SQLite.
 
2430
  * Therefore it is recommended to call this method once when no further
 
2431
  * access to any SQLite databases is required.
 
2432
  */
 
2433
  static void ShutdownSQLite();
 
2434
 
 
2435
  /// Get random bytes
 
2436
  /**
 
2437
  * SQLite contains a high-quality pseudo-random number generator.
 
2438
  * This method allows to access it for application specofoc purposes.
 
2439
  *
 
2440
  * \param n The amount of random bytes to be created
 
2441
  * \param random A memory buffer containing the random bytes on return
 
2442
  */
 
2443
  static bool Randomness(int n, wxMemoryBuffer& random);
 
2444
 
 
2445
  /// Enable or disable SQLite shared cache
 
2446
  /**
 
2447
  * The cache sharing mode set effects all subsequent database connections.
 
2448
  * Existing database connections continue use the sharing mode that was in effect
 
2449
  * at the time they were opened.
 
2450
  *
 
2451
  * Virtual tables cannot be used with a shared cache.
 
2452
  */
 
2453
  static void SetSharedCache(bool enable);
 
2454
 
 
2455
  /// Check whether SQLite shared cache is enabled
 
2456
  /**
 
2457
  * \return TRUE if the SQLite shared cache is enabled, FALSE otherwise
 
2458
  */
 
2459
  static bool IsSharedCacheEnabled() { return ms_sharedCacheEnabled; }
 
2460
 
 
2461
  /// Get the version of the underlying SQLite3 library
 
2462
  /**
 
2463
  * \return a string which contains the version number of the library
 
2464
  */
 
2465
  static wxString GetVersion();
 
2466
 
 
2467
  /// Get the source id of the underlying SQLite3 library
 
2468
  /**
 
2469
  * \return a string which contains the source id of the library
 
2470
  */
 
2471
  static wxString GetSourceId();
 
2472
 
 
2473
  /// Check SQLite compile option
 
2474
  /**
 
2475
  * Check whether the compile option with a given name has been used on building SQLite.
 
2476
  * The SQLITE_ prefix may be omitted from the option name passed to this method.
 
2477
  *
 
2478
  * \param optionName name of the compile option to be queried
 
2479
  * \return TRUE if the compile option was in use, FALSE otherwise
 
2480
  *
 
2481
  * \note If the option name is unknown or if the SQLite version is lower than 3.6.23
 
2482
  * this method returns FALSE.
 
2483
  */
 
2484
  static bool CompileOptionUsed(const wxString& optionName);
 
2485
 
 
2486
  /// Get SQLite compile option name
 
2487
  /**
 
2488
  * Get the name of a SQLite compile option at a given index.
 
2489
  * This method allows interating over the list of options that were defined
 
2490
  * at compile time. If the option index is out of range, an empty string is returned.
 
2491
  * The SQLITE_ prefix is omitted from any strings returned by this method.
 
2492
  *
 
2493
  * \param optionIndex Index of the compile option
 
2494
  * \return a string containing the name of the n-th
 
2495
  */
 
2496
  static wxString GetCompileOptionName(int optionIndex);
 
2497
 
 
2498
  /// Convert journal mode to/from string
 
2499
  /**
 
2500
  * \param mode the wxSQLite3JournalMode enum value signifying the desired journal mode.
 
2501
  * \return the string representation of the journal mode
 
2502
  */
 
2503
  static wxString ConvertJournalMode(wxSQLite3JournalMode mode);
 
2504
 
 
2505
  /// Convert journal mode to/from string
 
2506
  /**
 
2507
  * \param mode the string representation of the desired journal mode.
 
2508
  * \return the enum representation of the journal mode
 
2509
  */
 
2510
  static wxSQLite3JournalMode ConvertJournalMode(const wxString& mode);
 
2511
 
 
2512
  /// Check whether wxSQLite3 has been compiled with encryption support
 
2513
  /**
 
2514
  * \return TRUE if encryption support is enabled, FALSE otherwise
 
2515
  */
 
2516
  static bool HasEncryptionSupport();
 
2517
 
 
2518
  /// Check whether wxSQLite3 has been compiled with meta data support
 
2519
  /**
 
2520
  * \return TRUE if meta data support is enabled, FALSE otherwise
 
2521
  */
 
2522
  static bool HasMetaDataSupport();
 
2523
 
 
2524
  /// Check whether wxSQLite3 has been compiled with loadable extension support
 
2525
  /**
 
2526
  * \return TRUE if loadable extension support is enabled, FALSE otherwise
 
2527
  */
 
2528
  static bool HasLoadExtSupport();
 
2529
 
 
2530
  /// Check whether wxSQLite3 has been compiled with support for named collections
 
2531
  /**
 
2532
  * \return TRUE if named collection support is enabled, FALSE otherwise
 
2533
  */
 
2534
  static bool HasNamedCollectionSupport();
 
2535
 
 
2536
  /// Check whether wxSQLite3 has support for incremental BLOBs
 
2537
  /**
 
2538
  * \return TRUE if incremental BLOB support is available, FALSE otherwise
 
2539
  */
 
2540
  static bool HasIncrementalBlobSupport();
 
2541
 
 
2542
  /// Check whether wxSQLite3 has support for SQLite savepoints
 
2543
  /**
 
2544
  * \return TRUE if SQLite savepoints are supported, FALSE otherwise
 
2545
  */
 
2546
  static bool HasSavepointSupport();
 
2547
 
 
2548
  /// Check whether wxSQLite3 has support for SQLite backup/restore
 
2549
  /**
 
2550
  * \return TRUE if SQLite backup/restore is supported, FALSE otherwise
 
2551
  */
 
2552
  static bool HasBackupSupport();
 
2553
 
 
2554
  /// Check whether wxSQLite3 has support for SQLite write-ahead log
 
2555
  /**
 
2556
  * \return TRUE if SQLite write-ahead log is supported, FALSE otherwise
 
2557
  */
 
2558
  static bool HasWriteAheadLogSupport();
 
2559
 
 
2560
protected:
 
2561
  /// Access SQLite's internal database handle
 
2562
  void* GetDatabaseHandle() { return m_db; }
 
2563
 
 
2564
  /// Activate the callback for needed collations for this database
 
2565
  /**
 
2566
  * To avoid having to register all collation sequences before a database can be used,
 
2567
  * a single callback function may be registered with the database handle to be called
 
2568
  * whenever an undefined collation sequence is required.
 
2569
  */
 
2570
  void SetCollationNeededCallback();
 
2571
 
 
2572
  /// Request the instantiation of a user defined collation sequence
 
2573
  /**
 
2574
  * This method is called for every undefined collation sequence.
 
2575
  * In a derived database class this method should call SetCollation registering an
 
2576
  * appropriate collation class instance.
 
2577
  * \param collationName name of the collation which is needed for string comparison
 
2578
  */
 
2579
  virtual void SetNeededCollation(const wxString& WXUNUSED(collationName)) {}
 
2580
 
 
2581
  /// Execute a comparison using a user-defined collation
 
2582
  static int ExecComparisonWithCollation(void* collation, int len1, const void* txt1, int len2, const void* txt2);
 
2583
 
 
2584
  /// Execute callback for needed collation sequences
 
2585
  static void ExecCollationNeeded(void* db, void* internalDb, int eTextRep, const char* name);
 
2586
 
 
2587
private:
 
2588
  /// Private copy constructor
 
2589
  wxSQLite3Database(const wxSQLite3Database& db);
 
2590
 
 
2591
  /// Private assignment constructor
 
2592
  wxSQLite3Database& operator=(const wxSQLite3Database& db);
 
2593
 
 
2594
  /// Prepare a SQL statement (internal use only)
 
2595
  void* Prepare(const char* sql);
 
2596
 
 
2597
  /// Check for valid database connection
 
2598
  void CheckDatabase();
 
2599
 
 
2600
  void* m_db;             ///< associated SQLite3 database
 
2601
  int   m_busyTimeoutMs;  ///< Timeout in milli seconds
 
2602
  bool  m_isEncrypted;    ///< Flag whether the database is encrypted or not
 
2603
 
 
2604
  static bool  ms_sharedCacheEnabled;        ///< Flag whether SQLite shared cache is enabled
 
2605
  static bool  ms_hasEncryptionSupport;      ///< Flag whether wxSQLite3 has been compiled with encryption support
 
2606
  static bool  ms_hasMetaDataSupport;        ///< Flag whether wxSQLite3 has been compiled with meta data support
 
2607
  static bool  ms_hasLoadExtSupport;         ///< Flag whether wxSQLite3 has been compiled with loadable extension support
 
2608
  static bool  ms_hasNamedCollectionSupport; ///< Flag whether wxSQLite3 has been compiled with support for named collections
 
2609
  static bool  ms_hasIncrementalBlobSupport; ///< Flag whether wxSQLite3 has support for incremental BLOBs
 
2610
  static bool  ms_hasSavepointSupport;       ///< Flag whether wxSQLite3 has support for SQLite savepoints
 
2611
  static bool  ms_hasBackupSupport;          ///< Flag whether wxSQLite3 has support for SQLite backup/restore
 
2612
  static bool  ms_hasWriteAheadLogSupport;   ///< Flag whether wxSQLite3 has support for SQLite write-ahead log
 
2613
};
 
2614
 
 
2615
/// RAII class for managing transactions
 
2616
/***
 
2617
* This object allows easy managment of transaction. It con only be
 
2618
* created on the stack. This guarantees that the destructor is called
 
2619
* at the moment it goes out of scope. Usage:
 
2620
* \code
 
2621
* void doDB(wxSQLite3Database *db)
 
2622
* {
 
2623
*   wxSQLite3Transaction t(db);
 
2624
*   doDatabaseOperations();
 
2625
*   t.Commit();
 
2626
* }
 
2627
* \endcode
 
2628
* In case doDatabseOperations() fails by throwing an exception,
 
2629
* the transaction is automatically rolled back. If it succedes,
 
2630
* Commit() commits the changes to the db and the destructor
 
2631
* of Transaction does nothing.
 
2632
*/
 
2633
class EXTAPI wxSQLite3Transaction
 
2634
{
 
2635
public:
 
2636
  /// Constructor. Start the Transaction.
 
2637
  /**
 
2638
    * The constructor starts the transaction.
 
2639
    * \param db Pointer to the open Database. The pointer to the database
 
2640
    * is NOT freed on destruction!
 
2641
    * \param transactionType Type of the transaction to be opened.
 
2642
    */
 
2643
  explicit wxSQLite3Transaction(wxSQLite3Database* db, wxSQLite3TransactionType transactionType = WXSQLITE_TRANSACTION_DEFAULT);
 
2644
 
 
2645
  /// Destructor.
 
2646
  /**
 
2647
    * The destructor does nothing if the changes were already commited (see commit()).
 
2648
    * In case the changes were not commited, a call to the destructor rolls back the
 
2649
    * transaction.
 
2650
    */
 
2651
  ~wxSQLite3Transaction();
 
2652
 
 
2653
  /// Commits the transaction
 
2654
  /**
 
2655
    * Commits the transaction if active. If not, it does nothing.
 
2656
    * After the commit, the transaction is not active.
 
2657
    */
 
2658
  void Commit();
 
2659
 
 
2660
  /// Rolls back the transaction
 
2661
  /**
 
2662
    * Rolls back the transaction if active. If not, it does nothing.
 
2663
    * After the rollback, the transaction is not active.
 
2664
    */
 
2665
  void Rollback();
 
2666
 
 
2667
  /// Determins wether the transaction is open or not
 
2668
  /**
 
2669
    * \return TRUE if the constructor successfully opend the transaction, false otherwise.
 
2670
    * After committing the transaction, active returns false.
 
2671
    */
 
2672
  inline bool IsActive()
 
2673
  {
 
2674
    return m_database != NULL;
 
2675
  }
 
2676
 
 
2677
private:
 
2678
  /// New operator (May only be created on the stack)
 
2679
  static void *operator new(size_t size);
 
2680
 
 
2681
  /// Delete operator (May not be deleted (for symmetry))
 
2682
  static void operator delete(void *ptr);
 
2683
 
 
2684
  /// Copy constructor (Must not be copied)
 
2685
  wxSQLite3Transaction(const wxSQLite3Transaction&);
 
2686
 
 
2687
  /// Assignment operator (Must not be assigned)
 
2688
  wxSQLite3Transaction& operator=(const wxSQLite3Transaction&);
 
2689
 
 
2690
  wxSQLite3Database* m_database; ///< Pointer to the associated database (no ownership)
 
2691
};
 
2692
 
 
2693
#if wxUSE_REGEX
 
2694
 
 
2695
/// User defined function for REGEXP operator
 
2696
/**
 
2697
*/
 
2698
class EXTAPI wxSQLite3RegExpOperator : public wxSQLite3ScalarFunction
 
2699
{
 
2700
public:
 
2701
  /// Constructor
 
2702
  wxSQLite3RegExpOperator(int flags = wxRE_DEFAULT);
 
2703
 
 
2704
  /// Virtual destructor
 
2705
  virtual ~wxSQLite3RegExpOperator();
 
2706
 
 
2707
  /// Execute the scalar function
 
2708
  /**
 
2709
  * This method is invoked for each appearance of the scalar function in the SQL query.
 
2710
  * \param ctx function context which can be used to access arguments and result value
 
2711
  */
 
2712
  virtual void Execute(wxSQLite3FunctionContext& ctx);
 
2713
 
 
2714
private:
 
2715
  wxString m_exprStr; ///< Last regular expression string
 
2716
  wxRegEx  m_regEx;   ///< Regular expression cache (currently only 1 instance)
 
2717
  int      m_flags;   ///< Flags for regular expression
 
2718
};
 
2719
 
 
2720
#endif // wxUSE_REGEX
 
2721
 
 
2722
#endif
 
2723