1 ////////////////////////////////////////////////////////////////////////////////
3 // CppSQLite3 - A C++ wrapper around the SQLite3 embedded database library.
7 // Copyright (c) 2004 Rob Groves. All Rights Reserved. rob.groves@btinternet.com
11 // Permission to use, copy, modify, and distribute this software and its
13 // documentation for any purpose, without fee, and without a written
15 // agreement, is hereby granted, provided that the above copyright notice,
17 // this paragraph and the following two paragraphs appear in all copies,
19 // modifications, and distributions.
23 // IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
25 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
27 // PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
29 // EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
35 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37 // PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
39 // ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO OBLIGATION
41 // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
45 // V3.0 03/08/2004 -Initial Version for sqlite3
49 // V3.1 16/09/2004 -Implemented getXXXXField using sqlite3 functions
51 // -Added CppSQLiteDB3::tableExists()
53 ////////////////////////////////////////////////////////////////////////////////
55 #include "CppSQLite3.h"
63 // Named constant for passing to CppSQLite3Exception when passing it a string
65 // that cannot be deleted.
67 static const bool DONT_DELETE_MSG=false;
71 ////////////////////////////////////////////////////////////////////////////////
73 // Prototypes for SQLite functions not included in SQLite DLL, but copied below
75 // from SQLite encode.c
77 ////////////////////////////////////////////////////////////////////////////////
79 int sqlite3_encode_binary(const unsigned char *in, int n, unsigned char *out);
81 int sqlite3_decode_binary(const unsigned char *in, unsigned char *out);
85 ////////////////////////////////////////////////////////////////////////////////
89 ////////////////////////////////////////////////////////////////////////////////
93 CppSQLite3Exception::CppSQLite3Exception(const int nErrCode,
95 const char* szErrMess,
97 bool bDeleteMsg/*=true*/) :
103 mpszErrMess = sqlite3_mprintf("%s[%d]: %s",
105 errorCodeAsString(nErrCode),
109 szErrMess ? szErrMess : "");
113 if (bDeleteMsg && szErrMess)
117 sqlite3_free(szErrMess);
127 CppSQLite3Exception::CppSQLite3Exception(const int nErrCode,
131 bool bDeleteMsg/*=true*/) :
137 mpszErrMess = sqlite3_mprintf("%s[%d]: %s",
139 errorCodeAsString(nErrCode),
143 szErrMess ? szErrMess : "");
147 if (bDeleteMsg && szErrMess)
151 sqlite3_free(szErrMess);
159 CppSQLite3Exception::CppSQLite3Exception(const CppSQLite3Exception& e) :
161 mnErrCode(e.mnErrCode)
171 mpszErrMess = sqlite3_mprintf("%s", e.mpszErrMess);
181 const char* CppSQLite3Exception::errorCodeAsString(int nErrCode)
189 case SQLITE_OK : return "SQLITE_OK";
191 case SQLITE_ERROR : return "SQLITE_ERROR";
193 case SQLITE_INTERNAL : return "SQLITE_INTERNAL";
195 case SQLITE_PERM : return "SQLITE_PERM";
197 case SQLITE_ABORT : return "SQLITE_ABORT";
199 case SQLITE_BUSY : return "SQLITE_BUSY";
201 case SQLITE_LOCKED : return "SQLITE_LOCKED";
203 case SQLITE_NOMEM : return "SQLITE_NOMEM";
205 case SQLITE_READONLY : return "SQLITE_READONLY";
207 case SQLITE_INTERRUPT : return "SQLITE_INTERRUPT";
209 case SQLITE_IOERR : return "SQLITE_IOERR";
211 case SQLITE_CORRUPT : return "SQLITE_CORRUPT";
213 case SQLITE_NOTFOUND : return "SQLITE_NOTFOUND";
215 case SQLITE_FULL : return "SQLITE_FULL";
217 case SQLITE_CANTOPEN : return "SQLITE_CANTOPEN";
219 case SQLITE_PROTOCOL : return "SQLITE_PROTOCOL";
221 case SQLITE_EMPTY : return "SQLITE_EMPTY";
223 case SQLITE_SCHEMA : return "SQLITE_SCHEMA";
225 case SQLITE_TOOBIG : return "SQLITE_TOOBIG";
227 case SQLITE_CONSTRAINT : return "SQLITE_CONSTRAINT";
229 case SQLITE_MISMATCH : return "SQLITE_MISMATCH";
231 case SQLITE_MISUSE : return "SQLITE_MISUSE";
233 case SQLITE_NOLFS : return "SQLITE_NOLFS";
235 case SQLITE_AUTH : return "SQLITE_AUTH";
237 case SQLITE_FORMAT : return "SQLITE_FORMAT";
239 case SQLITE_RANGE : return "SQLITE_RANGE";
241 case SQLITE_ROW : return "SQLITE_ROW";
243 case SQLITE_DONE : return "SQLITE_DONE";
245 case CPPSQLITE_ERROR : return "CPPSQLITE_ERROR";
247 default: return "UNKNOWN_ERROR";
257 CppSQLite3Exception::~CppSQLite3Exception()
265 sqlite3_free(mpszErrMess);
277 ////////////////////////////////////////////////////////////////////////////////
281 CppSQLite3Buffer::CppSQLite3Buffer()
293 CppSQLite3Buffer::~CppSQLite3Buffer()
305 void CppSQLite3Buffer::clear()
327 const char* CppSQLite3Buffer::format(const char* szFormat, ...)
335 va_start(va, szFormat);
337 mpBuf = sqlite3_vmprintf(szFormat, va);
349 ////////////////////////////////////////////////////////////////////////////////
353 CppSQLite3Binary::CppSQLite3Binary() :
373 CppSQLite3Binary::~CppSQLite3Binary()
385 void CppSQLite3Binary::setBinary(const unsigned char* pBuf, int nLen)
389 mpBuf = allocBuffer(nLen);
391 memcpy(mpBuf, pBuf, nLen);
399 void CppSQLite3Binary::setEncoded(const unsigned char* pBuf)
407 mnEncodedLen = strlen((const char*)pBuf);
409 mnBufferLen = mnEncodedLen + 1; // Allow for NULL terminator
413 mpBuf = (unsigned char*)malloc(mnBufferLen);
421 throw CppSQLite3Exception(CPPSQLITE_ERROR,
423 "Cannot allocate memory",
431 memcpy(mpBuf, pBuf, mnBufferLen);
441 const unsigned char* CppSQLite3Binary::getEncoded()
449 unsigned char* ptmp = (unsigned char*)malloc(mnBinaryLen);
451 memcpy(ptmp, mpBuf, mnBinaryLen);
453 mnEncodedLen = sqlite3_encode_binary(ptmp, mnBinaryLen, mpBuf);
471 const unsigned char* CppSQLite3Binary::getBinary()
479 // in/out buffers can be the same
481 mnBinaryLen = sqlite3_decode_binary(mpBuf, mpBuf);
485 if (mnBinaryLen == -1)
489 throw CppSQLite3Exception(CPPSQLITE_ERROR,
491 "Cannot decode binary",
513 int CppSQLite3Binary::getBinaryLength()
527 unsigned char* CppSQLite3Binary::allocBuffer(int nLen)
535 // Allow extra space for encoded binary as per comments in
537 // SQLite encode.c See bottom of this file for implementation
539 // of SQLite functions use 3 instead of 2 just to be sure ;-)
543 mnBufferLen = 3 + (257*nLen)/254;
547 mpBuf = (unsigned char*)malloc(mnBufferLen);
555 throw CppSQLite3Exception(CPPSQLITE_ERROR,
557 "Cannot allocate memory",
577 void CppSQLite3Binary::clear()
601 ////////////////////////////////////////////////////////////////////////////////
605 CppSQLite3Query::CppSQLite3Query()
623 CppSQLite3Query::CppSQLite3Query(const CppSQLite3Query& rQuery)
629 // Only one object can own the VM
631 const_cast<CppSQLite3Query&>(rQuery).mpVM = 0;
633 mbEof = rQuery.mbEof;
635 mnCols = rQuery.mnCols;
637 mbOwnVM = rQuery.mbOwnVM;
645 CppSQLite3Query::CppSQLite3Query(sqlite3* pDB,
651 bool bOwnVM/*=true*/)
661 mnCols = sqlite3_column_count(mpVM);
671 CppSQLite3Query::~CppSQLite3Query()
695 CppSQLite3Query& CppSQLite3Query::operator=(const CppSQLite3Query& rQuery)
715 // Only one object can own the VM
717 const_cast<CppSQLite3Query&>(rQuery).mpVM = 0;
719 mbEof = rQuery.mbEof;
721 mnCols = rQuery.mnCols;
723 mbOwnVM = rQuery.mbOwnVM;
733 int CppSQLite3Query::numFields()
747 const char* CppSQLite3Query::fieldValue(int nField)
755 if (nField < 0 || nField > mnCols-1)
759 throw CppSQLite3Exception(CPPSQLITE_ERROR,
761 "Invalid field index requested",
769 return (const char*)sqlite3_column_text(mpVM, nField);
777 const char* CppSQLite3Query::fieldValue(const char* szField)
781 int nField = fieldIndex(szField);
783 return (const char*)sqlite3_column_text(mpVM, nField);
791 int CppSQLite3Query::getIntField(int nField, int nNullValue/*=0*/)
795 if (fieldDataType(nField) == SQLITE_NULL)
807 return sqlite3_column_int(mpVM, nField);
817 int CppSQLite3Query::getIntField(const char* szField, int nNullValue/*=0*/)
821 int nField = fieldIndex(szField);
823 return getIntField(nField, nNullValue);
831 double CppSQLite3Query::getFloatField(int nField, double fNullValue/*=0.0*/)
835 if (fieldDataType(nField) == SQLITE_NULL)
847 return sqlite3_column_double(mpVM, nField);
857 double CppSQLite3Query::getFloatField(const char* szField, double fNullValue/*=0.0*/)
861 int nField = fieldIndex(szField);
863 return getFloatField(nField, fNullValue);
871 const char* CppSQLite3Query::getStringField(int nField, const char* szNullValue/*=""*/)
875 if (fieldDataType(nField) == SQLITE_NULL)
887 return (const char*)sqlite3_column_text(mpVM, nField);
897 const char* CppSQLite3Query::getStringField(const char* szField, const char* szNullValue/*=""*/)
901 int nField = fieldIndex(szField);
903 return getStringField(nField, szNullValue);
911 const unsigned char* CppSQLite3Query::getBlobField(int nField, int& nLen)
919 if (nField < 0 || nField > mnCols-1)
923 throw CppSQLite3Exception(CPPSQLITE_ERROR,
925 "Invalid field index requested",
933 nLen = sqlite3_column_bytes(mpVM, nField);
935 return (const unsigned char*)sqlite3_column_blob(mpVM, nField);
943 const unsigned char* CppSQLite3Query::getBlobField(const char* szField, int& nLen)
947 int nField = fieldIndex(szField);
949 return getBlobField(nField, nLen);
957 bool CppSQLite3Query::fieldIsNull(int nField)
961 return (fieldDataType(nField) == SQLITE_NULL);
969 bool CppSQLite3Query::fieldIsNull(const char* szField)
973 int nField = fieldIndex(szField);
975 return (fieldDataType(nField) == SQLITE_NULL);
983 int CppSQLite3Query::fieldIndex(const char* szField)
995 for (int nField = 0; nField < mnCols; nField++)
999 const char* szTemp = sqlite3_column_name(mpVM, nField);
1003 if (strcmp(szField, szTemp) == 0)
1017 throw CppSQLite3Exception(CPPSQLITE_ERROR,
1019 "Invalid field name requested",
1029 const char* CppSQLite3Query::fieldName(int nCol)
1037 if (nCol < 0 || nCol > mnCols-1)
1041 throw CppSQLite3Exception(CPPSQLITE_ERROR,
1043 "Invalid field index requested",
1051 return sqlite3_column_name(mpVM, nCol);
1059 const char* CppSQLite3Query::fieldDeclType(int nCol)
1067 if (nCol < 0 || nCol > mnCols-1)
1071 throw CppSQLite3Exception(CPPSQLITE_ERROR,
1073 "Invalid field index requested",
1081 return sqlite3_column_decltype(mpVM, nCol);
1089 int CppSQLite3Query::fieldDataType(int nCol)
1097 if (nCol < 0 || nCol > mnCols-1)
1101 throw CppSQLite3Exception(CPPSQLITE_ERROR,
1103 "Invalid field index requested",
1111 return sqlite3_column_type(mpVM, nCol);
1119 bool CppSQLite3Query::eof()
1133 void CppSQLite3Query::nextRow()
1141 int nRet = sqlite3_step(mpVM);
1145 if (nRet == SQLITE_DONE)
1155 else if (nRet == SQLITE_ROW)
1159 // more rows, nothing to do
1167 nRet = sqlite3_finalize(mpVM);
1171 const char* szError = sqlite3_errmsg(mpDB);
1173 throw CppSQLite3Exception(nRet,
1187 void CppSQLite3Query::finalize()
1191 if (mpVM && mbOwnVM)
1195 int nRet = sqlite3_finalize(mpVM);
1199 if (nRet != SQLITE_OK)
1203 const char* szError = sqlite3_errmsg(mpDB);
1205 throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
1217 void CppSQLite3Query::checkVM()
1225 throw CppSQLite3Exception(CPPSQLITE_ERROR,
1227 "Null Virtual Machine pointer",
1239 ////////////////////////////////////////////////////////////////////////////////
1243 CppSQLite3Table::CppSQLite3Table()
1261 CppSQLite3Table::CppSQLite3Table(const CppSQLite3Table& rTable)
1265 mpaszResults = rTable.mpaszResults;
1267 // Only one object can own the results
1269 const_cast<CppSQLite3Table&>(rTable).mpaszResults = 0;
1271 mnRows = rTable.mnRows;
1273 mnCols = rTable.mnCols;
1275 mnCurrentRow = rTable.mnCurrentRow;
1283 CppSQLite3Table::CppSQLite3Table(char** paszResults, int nRows, int nCols)
1287 mpaszResults = paszResults;
1301 CppSQLite3Table::~CppSQLite3Table()
1325 CppSQLite3Table& CppSQLite3Table::operator=(const CppSQLite3Table& rTable)
1343 mpaszResults = rTable.mpaszResults;
1345 // Only one object can own the results
1347 const_cast<CppSQLite3Table&>(rTable).mpaszResults = 0;
1349 mnRows = rTable.mnRows;
1351 mnCols = rTable.mnCols;
1353 mnCurrentRow = rTable.mnCurrentRow;
1363 void CppSQLite3Table::finalize()
1371 sqlite3_free_table(mpaszResults);
1383 int CppSQLite3Table::numFields()
1397 int CppSQLite3Table::numRows()
1411 const char* CppSQLite3Table::fieldValue(int nField)
1419 if (nField < 0 || nField > mnCols-1)
1423 throw CppSQLite3Exception(CPPSQLITE_ERROR,
1425 "Invalid field index requested",
1433 int nIndex = (mnCurrentRow*mnCols) + mnCols + nField;
1435 return mpaszResults[nIndex];
1443 const char* CppSQLite3Table::fieldValue(const char* szField)
1455 for (int nField = 0; nField < mnCols; nField++)
1459 if (strcmp(szField, mpaszResults[nField]) == 0)
1463 int nIndex = (mnCurrentRow*mnCols) + mnCols + nField;
1465 return mpaszResults[nIndex];
1475 throw CppSQLite3Exception(CPPSQLITE_ERROR,
1477 "Invalid field name requested",
1487 int CppSQLite3Table::getIntField(int nField, int nNullValue/*=0*/)
1491 if (fieldIsNull(nField))
1503 return atoi(fieldValue(nField));
1513 int CppSQLite3Table::getIntField(const char* szField, int nNullValue/*=0*/)
1517 if (fieldIsNull(szField))
1529 return atoi(fieldValue(szField));
1539 double CppSQLite3Table::getFloatField(int nField, double fNullValue/*=0.0*/)
1543 if (fieldIsNull(nField))
1555 return atof(fieldValue(nField));
1565 double CppSQLite3Table::getFloatField(const char* szField, double fNullValue/*=0.0*/)
1569 if (fieldIsNull(szField))
1581 return atof(fieldValue(szField));
1591 const char* CppSQLite3Table::getStringField(int nField, const char* szNullValue/*=""*/)
1595 if (fieldIsNull(nField))
1607 return fieldValue(nField);
1617 const char* CppSQLite3Table::getStringField(const char* szField, const char* szNullValue/*=""*/)
1621 if (fieldIsNull(szField))
1633 return fieldValue(szField);
1643 bool CppSQLite3Table::fieldIsNull(int nField)
1649 return (fieldValue(nField) == 0);
1657 bool CppSQLite3Table::fieldIsNull(const char* szField)
1663 return (fieldValue(szField) == 0);
1671 const char* CppSQLite3Table::fieldName(int nCol)
1679 if (nCol < 0 || nCol > mnCols-1)
1683 throw CppSQLite3Exception(CPPSQLITE_ERROR,
1685 "Invalid field index requested",
1693 return mpaszResults[nCol];
1701 void CppSQLite3Table::setRow(int nRow)
1709 if (nRow < 0 || nRow > mnRows-1)
1713 throw CppSQLite3Exception(CPPSQLITE_ERROR,
1715 "Invalid row index requested",
1723 mnCurrentRow = nRow;
1731 void CppSQLite3Table::checkResults()
1735 if (mpaszResults == 0)
1739 throw CppSQLite3Exception(CPPSQLITE_ERROR,
1741 "Null Results pointer",
1753 ////////////////////////////////////////////////////////////////////////////////
1757 CppSQLite3Statement::CppSQLite3Statement()
1771 CppSQLite3Statement::CppSQLite3Statement(const CppSQLite3Statement& rStatement)
1775 mpDB = rStatement.mpDB;
1777 mpVM = rStatement.mpVM;
1779 // Only one object can own VM
1781 const_cast<CppSQLite3Statement&>(rStatement).mpVM = 0;
1789 CppSQLite3Statement::CppSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM)
1803 CppSQLite3Statement::~CppSQLite3Statement()
1827 CppSQLite3Statement& CppSQLite3Statement::operator=(const CppSQLite3Statement& rStatement)
1831 mpDB = rStatement.mpDB;
1833 mpVM = rStatement.mpVM;
1835 // Only one object can own VM
1837 const_cast<CppSQLite3Statement&>(rStatement).mpVM = 0;
1847 int CppSQLite3Statement::execDML()
1857 const char* szError=0;
1861 int nRet = sqlite3_step(mpVM);
1865 if (nRet == SQLITE_DONE)
1869 int nRowsChanged = sqlite3_changes(mpDB);
1873 nRet = sqlite3_reset(mpVM);
1877 if (nRet != SQLITE_OK)
1881 szError = sqlite3_errmsg(mpDB);
1883 throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
1889 return nRowsChanged;
1897 nRet = sqlite3_reset(mpVM);
1899 szError = sqlite3_errmsg(mpDB);
1901 throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
1911 CppSQLite3Query CppSQLite3Statement::execQuery()
1921 int nRet = sqlite3_step(mpVM);
1925 if (nRet == SQLITE_DONE)
1931 return CppSQLite3Query(mpDB, mpVM, true/*eof*/, false);
1935 else if (nRet == SQLITE_ROW)
1941 return CppSQLite3Query(mpDB, mpVM, false/*eof*/, false);
1949 nRet = sqlite3_reset(mpVM);
1951 const char* szError = sqlite3_errmsg(mpDB);
1953 throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
1963 void CppSQLite3Statement::bind(int nParam, const char* szValue)
1969 int nRes = sqlite3_bind_text(mpVM, nParam, szValue, -1, SQLITE_TRANSIENT);
1973 if (nRes != SQLITE_OK)
1977 throw CppSQLite3Exception(nRes,
1979 "Error binding string param",
1991 void CppSQLite3Statement::bind(int nParam, const int nValue)
1997 int nRes = sqlite3_bind_int(mpVM, nParam, nValue);
2001 if (nRes != SQLITE_OK)
2005 throw CppSQLite3Exception(nRes,
2007 "Error binding int param",
2019 void CppSQLite3Statement::bind(int nParam, const double dValue)
2025 int nRes = sqlite3_bind_double(mpVM, nParam, dValue);
2029 if (nRes != SQLITE_OK)
2033 throw CppSQLite3Exception(nRes,
2035 "Error binding double param",
2047 void CppSQLite3Statement::bind(int nParam, const unsigned char* blobValue, int nLen)
2053 int nRes = sqlite3_bind_blob(mpVM, nParam,
2055 (const void*)blobValue, nLen, SQLITE_TRANSIENT);
2059 if (nRes != SQLITE_OK)
2063 throw CppSQLite3Exception(nRes,
2065 "Error binding blob param",
2077 void CppSQLite3Statement::bindNull(int nParam)
2083 int nRes = sqlite3_bind_null(mpVM, nParam);
2087 if (nRes != SQLITE_OK)
2091 throw CppSQLite3Exception(nRes,
2093 "Error binding NULL param",
2105 void CppSQLite3Statement::reset()
2113 int nRet = sqlite3_reset(mpVM);
2117 if (nRet != SQLITE_OK)
2121 const char* szError = sqlite3_errmsg(mpDB);
2123 throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
2135 void CppSQLite3Statement::finalize()
2143 int nRet = sqlite3_finalize(mpVM);
2149 if (nRet != SQLITE_OK)
2153 const char* szError = sqlite3_errmsg(mpDB);
2155 throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
2167 void CppSQLite3Statement::checkDB()
2175 throw CppSQLite3Exception(CPPSQLITE_ERROR,
2177 "Database not open",
2189 void CppSQLite3Statement::checkVM()
2197 throw CppSQLite3Exception(CPPSQLITE_ERROR,
2199 "Null Virtual Machine pointer",
2211 ////////////////////////////////////////////////////////////////////////////////
2215 CppSQLite3DB::CppSQLite3DB()
2221 mnBusyTimeoutMs = 60000; // 60 seconds
2229 CppSQLite3DB::CppSQLite3DB(const CppSQLite3DB& db)
2235 mnBusyTimeoutMs = 60000; // 60 seconds
2243 CppSQLite3DB::~CppSQLite3DB()
2255 CppSQLite3DB& CppSQLite3DB::operator=(const CppSQLite3DB& db)
2261 mnBusyTimeoutMs = 60000; // 60 seconds
2271 void CppSQLite3DB::open(const char* szFile)
2275 int nRet = sqlite3_open(szFile, &mpDB);
2279 if (nRet != SQLITE_OK)
2283 const char* szError = sqlite3_errmsg(mpDB);
2285 throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
2291 setBusyTimeout(mnBusyTimeoutMs);
2299 void CppSQLite3DB::close()
2307 sqlite3_close(mpDB);
2319 CppSQLite3Statement CppSQLite3DB::compileStatement(const char* szSQL)
2327 sqlite3_stmt* pVM = compile(szSQL);
2329 return CppSQLite3Statement(mpDB, pVM);
2337 bool CppSQLite3DB::tableExists(const char* szTable)
2345 "select count(*) from sqlite_master where type='table' and name='%s'",
2349 int nRet = execScalar(szSQL);
2359 int CppSQLite3DB::execDML(const char* szSQL)
2371 int nRet = sqlite3_exec(mpDB, szSQL, 0, 0, &szError);
2375 if (nRet == SQLITE_OK)
2379 return sqlite3_changes(mpDB);
2387 throw CppSQLite3Exception(nRet, szError);
2397 CppSQLite3Query CppSQLite3DB::execQuery(const char* szSQL)
2405 sqlite3_stmt* pVM = compile(szSQL);
2409 int nRet = sqlite3_step(pVM);
2413 if (nRet == SQLITE_DONE)
2419 return CppSQLite3Query(mpDB, pVM, true/*eof*/);
2423 else if (nRet == SQLITE_ROW)
2429 return CppSQLite3Query(mpDB, pVM, false/*eof*/);
2437 nRet = sqlite3_finalize(pVM);
2439 const char* szError= sqlite3_errmsg(mpDB);
2441 throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
2451 int CppSQLite3DB::execScalar(const char* szSQL)
2455 CppSQLite3Query q = execQuery(szSQL);
2459 if (q.eof() || q.numFields() < 1)
2463 throw CppSQLite3Exception(CPPSQLITE_ERROR,
2465 "Invalid scalar query",
2473 return atoi(q.fieldValue(0));
2481 CppSQLite3Table CppSQLite3DB::getTable(const char* szSQL)
2491 char** paszResults=0;
2501 nRet = sqlite3_get_table(mpDB, szSQL, &paszResults, &nRows, &nCols, &szError);
2505 if (nRet == SQLITE_OK)
2509 return CppSQLite3Table(paszResults, nRows, nCols);
2517 throw CppSQLite3Exception(nRet, szError);
2527 sqlite_int64 CppSQLite3DB::lastRowId()
2531 return sqlite3_last_insert_rowid(mpDB);
2539 void CppSQLite3DB::setBusyTimeout(int nMillisecs)
2543 mnBusyTimeoutMs = nMillisecs;
2545 sqlite3_busy_timeout(mpDB, mnBusyTimeoutMs);
2553 void CppSQLite3DB::checkDB()
2561 throw CppSQLite3Exception(CPPSQLITE_ERROR,
2563 "Database not open",
2575 sqlite3_stmt* CppSQLite3DB::compile(const char* szSQL)
2585 const char* szTail=0;
2591 int nRet = sqlite3_prepare(mpDB, szSQL, -1, &pVM, &szTail);
2595 if (nRet != SQLITE_OK)
2599 throw CppSQLite3Exception(nRet, szError);
2613 ////////////////////////////////////////////////////////////////////////////////
2615 // SQLite encode.c reproduced here, containing implementation notes and source
2617 // for sqlite3_encode_binary() and sqlite3_decode_binary()
2619 ////////////////////////////////////////////////////////////////////////////////
2629 ** The author disclaims copyright to this source code. In place of
2631 ** a legal notice, here is a blessing:
2635 ** May you do good and not evil.
2637 ** May you find forgiveness for yourself and forgive others.
2639 ** May you share freely, never taking more than you give.
2643 *************************************************************************
2645 ** This file contains helper routines used to translate binary data into
2647 ** a null-terminated string (suitable for use in SQLite) and back again.
2649 ** These are convenience routines for use by people who want to store binary
2651 ** data in an SQLite database. The code in this file is not used by any other
2653 ** part of the SQLite library.
2657 ** $Id: CppSQLite3.cpp,v 1.2 2009/08/27 11:47:02 cervenansky Exp $
2665 ** How This Encoder Works
2669 ** The output is allowed to contain any character except 0x27 (') and
2671 ** 0x00. This is accomplished by using an escape character to encode
2673 ** 0x27 and 0x00 as a two-byte sequence. The escape character is always
2675 ** 0x01. An 0x00 is encoded as the two byte sequence 0x01 0x01. The
2677 ** 0x27 character is encoded as the two byte sequence 0x01 0x03. Finally,
2679 ** the escape character itself is encoded as the two-character sequence
2685 ** To summarize, the encoder works by using an escape sequences as follows:
2689 ** 0x00 -> 0x01 0x01
2691 ** 0x01 -> 0x01 0x02
2693 ** 0x27 -> 0x01 0x03
2697 ** If that were all the encoder did, it would work, but in certain cases
2699 ** it could double the size of the encoded string. For example, to
2701 ** encode a string of 100 0x27 characters would require 100 instances of
2703 ** the 0x01 0x03 escape sequence resulting in a 200-character output.
2705 ** We would prefer to keep the size of the encoded string smaller than
2711 ** To minimize the encoding size, we first add a fixed offset value to each
2713 ** byte in the sequence. The addition is modulo 256. (That is to say, if
2715 ** the sum of the original character value and the offset exceeds 256, then
2717 ** the higher order bits are truncated.) The offset is chosen to minimize
2719 ** the number of characters in the string that need to be escaped. For
2721 ** example, in the case above where the string was composed of 100 0x27
2723 ** characters, the offset might be 0x01. Each of the 0x27 characters would
2725 ** then be converted into an 0x28 character which would not need to be
2727 ** escaped at all and so the 100 character input string would be converted
2729 ** into just 100 characters of output. Actually 101 characters of output -
2731 ** we have to record the offset used as the first byte in the sequence so
2733 ** that the string can be decoded. Since the offset value is stored as
2735 ** part of the output string and the output string is not allowed to contain
2737 ** characters 0x00 or 0x27, the offset cannot be 0x00 or 0x27.
2741 ** Here, then, are the encoding steps:
2745 ** (1) Choose an offset value and make it the first character of
2751 ** (2) Copy each input character into the output buffer, one by
2753 ** one, adding the offset value as you copy.
2757 ** (3) If the value of an input character plus offset is 0x00, replace
2759 ** that one character by the two-character sequence 0x01 0x01.
2761 ** If the sum is 0x01, replace it with 0x01 0x02. If the sum
2763 ** is 0x27, replace it with 0x01 0x03.
2767 ** (4) Put a 0x00 terminator at the end of the output.
2771 ** Decoding is obvious:
2775 ** (5) Copy encoded characters except the first into the decode
2777 ** buffer. Set the first encoded character aside for use as
2779 ** the offset in step 7 below.
2783 ** (6) Convert each 0x01 0x01 sequence into a single character 0x00.
2785 ** Convert 0x01 0x02 into 0x01. Convert 0x01 0x03 into 0x27.
2789 ** (7) Subtract the offset value that was the first character of
2791 ** the encoded buffer from all characters in the output buffer.
2795 ** The only tricky part is step (1) - how to compute an offset value to
2797 ** minimize the size of the output buffer. This is accomplished by testing
2799 ** all offset values and picking the one that results in the fewest number
2801 ** of escapes. To do that, we first scan the entire input and count the
2803 ** number of occurances of each character value in the input. Suppose
2805 ** the number of 0x00 characters is N(0), the number of occurances of 0x01
2807 ** is N(1), and so forth up to the number of occurances of 0xff is N(255).
2809 ** An offset of 0 is not allowed so we don't have to test it. The number
2811 ** of escapes required for an offset of 1 is N(1)+N(2)+N(40). The number
2813 ** of escapes required for an offset of 2 is N(2)+N(3)+N(41). And so forth.
2815 ** In this way we find the offset that gives the minimum number of escapes,
2817 ** and thus minimizes the length of the output string.
2825 ** Encode a binary buffer "in" of size n bytes so that it contains
2827 ** no instances of characters '\'' or '\000'. The output is
2829 ** null-terminated and can be used as a string value in an INSERT
2831 ** or UPDATE statement. Use sqlite3_decode_binary() to convert the
2833 ** string back into its original binary.
2837 ** The result is written into a preallocated output buffer "out".
2839 ** "out" must be able to hold at least 2 +(257*n)/254 bytes.
2841 ** In other words, the output will be expanded by as much as 3
2843 ** bytes for every 254 bytes of input plus 2 bytes of fixed overhead.
2845 ** (This is approximately 2 + 1.0118*n or about a 1.2% size increase.)
2849 ** The return value is the number of characters in the encoded
2851 ** string, excluding the "\000" terminator.
2855 int sqlite3_encode_binary(const unsigned char *in, int n, unsigned char *out){
2871 memset(cnt, 0, sizeof(cnt));
2873 for(i=n-1; i>=0; i--){ cnt[in[i]]++; }
2877 for(i=1; i<256; i++){
2881 if( i=='\'' ) continue;
2883 sum = cnt[i] + cnt[(i+1)&0xff] + cnt[(i+'\'')&0xff];
2903 int c = (in[i] - e)&0xff;
2917 }else if( c=='\'' ){
2941 ** Decode the string "in" into binary data and write it into "out".
2943 ** This routine reverses the encoding created by sqlite3_encode_binary().
2945 ** The output will always be a few bytes less than the input. The number
2947 ** of bytes of output is returned. If the input is not a well-formed
2949 ** encoding, -1 is returned.
2953 ** The "in" and "out" parameters may point to the same buffer in order
2955 ** to decode a string in place.
2959 int sqlite3_decode_binary(const unsigned char *in, unsigned char *out){
2967 while( (c = *(in++))!=0 ){
2993 out[i++] = (c + e)&0xff;