////////////////////////////////////////////////////////////////////////////////
// CppSQLite3 - A C++ wrapper around the SQLite3 embedded database library.
//
// Copyright (c) 2004 Rob Groves. All Rights Reserved. rob.groves@btinternet.com
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement, is hereby granted, provided that the above copyright notice,
// this paragraph and the following two paragraphs appear in all copies,
// modifications, and distributions.
//
// IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
// INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
// PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
// EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
// ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO OBLIGATION
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
//
// V3.0 03/08/2004 -Initial Version for sqlite3
//
// V3.1 16/09/2004 -Implemented getXXXXField using sqlite3 functions
// -Added CppSQLiteDB3::tableExists()
////////////////////////////////////////////////////////////////////////////////
#include "CppSQLite3.h"
#include <cstdlib>
// Named constant for passing to CppSQLite3Exception when passing it a string
// that cannot be deleted.
static const bool DONT_DELETE_MSG=false;
////////////////////////////////////////////////////////////////////////////////
// Prototypes for SQLite functions not included in SQLite DLL, but copied below
// from SQLite encode.c
////////////////////////////////////////////////////////////////////////////////
int sqlite3_encode_binary(const unsigned char *in, int n, unsigned char *out);
int sqlite3_decode_binary(const unsigned char *in, unsigned char *out);
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
CppSQLite3Exception::CppSQLite3Exception(const int nErrCode,
char* szErrMess,
bool bDeleteMsg/*=true*/) :
mnErrCode(nErrCode)
{
mpszErrMess = sqlite3_mprintf("%s[%d]: %s",
errorCodeAsString(nErrCode),
nErrCode,
szErrMess ? szErrMess : "");
if (bDeleteMsg && szErrMess)
{
sqlite3_free(szErrMess);
}
}
CppSQLite3Exception::CppSQLite3Exception(const CppSQLite3Exception& e) :
mnErrCode(e.mnErrCode)
{
mpszErrMess = 0;
if (e.mpszErrMess)
{
mpszErrMess = sqlite3_mprintf("%s", e.mpszErrMess);
}
}
const char* CppSQLite3Exception::errorCodeAsString(int nErrCode)
{
switch (nErrCode)
{
case SQLITE_OK : return "SQLITE_OK";
case SQLITE_ERROR : return "SQLITE_ERROR";
case SQLITE_INTERNAL : return "SQLITE_INTERNAL";
case SQLITE_PERM : return "SQLITE_PERM";
case SQLITE_ABORT : return "SQLITE_ABORT";
case SQLITE_BUSY : return "SQLITE_BUSY";
case SQLITE_LOCKED : return "SQLITE_LOCKED";
case SQLITE_NOMEM : return "SQLITE_NOMEM";
case SQLITE_READONLY : return "SQLITE_READONLY";
case SQLITE_INTERRUPT : return "SQLITE_INTERRUPT";
case SQLITE_IOERR : return "SQLITE_IOERR";
case SQLITE_CORRUPT : return "SQLITE_CORRUPT";
case SQLITE_NOTFOUND : return "SQLITE_NOTFOUND";
case SQLITE_FULL : return "SQLITE_FULL";
case SQLITE_CANTOPEN : return "SQLITE_CANTOPEN";
case SQLITE_PROTOCOL : return "SQLITE_PROTOCOL";
case SQLITE_EMPTY : return "SQLITE_EMPTY";
case SQLITE_SCHEMA : return "SQLITE_SCHEMA";
case SQLITE_TOOBIG : return "SQLITE_TOOBIG";
case SQLITE_CONSTRAINT : return "SQLITE_CONSTRAINT";
case SQLITE_MISMATCH : return "SQLITE_MISMATCH";
case SQLITE_MISUSE : return "SQLITE_MISUSE";
case SQLITE_NOLFS : return "SQLITE_NOLFS";
case SQLITE_AUTH : return "SQLITE_AUTH";
case SQLITE_FORMAT : return "SQLITE_FORMAT";
case SQLITE_RANGE : return "SQLITE_RANGE";
case SQLITE_ROW : return "SQLITE_ROW";
case SQLITE_DONE : return "SQLITE_DONE";
case CPPSQLITE_ERROR : return "CPPSQLITE_ERROR";
default: return "UNKNOWN_ERROR";
}
}
CppSQLite3Exception::~CppSQLite3Exception()
{
if (mpszErrMess)
{
sqlite3_free(mpszErrMess);
mpszErrMess = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
CppSQLite3Buffer::CppSQLite3Buffer()
{
mpBuf = 0;
}
CppSQLite3Buffer::~CppSQLite3Buffer()
{
clear();
}
void CppSQLite3Buffer::clear()
{
if (mpBuf)
{
sqlite3_free(mpBuf);
mpBuf = 0;
}
}
const char* CppSQLite3Buffer::format(const char* szFormat, ...)
{
clear();
va_list va;
va_start(va, szFormat);
mpBuf = sqlite3_vmprintf(szFormat, va);
va_end(va);
return mpBuf;
}
////////////////////////////////////////////////////////////////////////////////
CppSQLite3Binary::CppSQLite3Binary() :
mpBuf(0),
mnBinaryLen(0),
mnBufferLen(0),
mnEncodedLen(0),
mbEncoded(false)
{
}
CppSQLite3Binary::~CppSQLite3Binary()
{
clear();
}
void CppSQLite3Binary::setBinary(const unsigned char* pBuf, int nLen)
{
mpBuf = allocBuffer(nLen);
memcpy(mpBuf, pBuf, nLen);
}
void CppSQLite3Binary::setEncoded(const unsigned char* pBuf)
{
clear();
mnEncodedLen = strlen((const char*)pBuf);
mnBufferLen = mnEncodedLen + 1; // Allow for NULL terminator
mpBuf = (unsigned char*)malloc(mnBufferLen);
if (!mpBuf)
{
throw CppSQLite3Exception(CPPSQLITE_ERROR,
"Cannot allocate memory",
DONT_DELETE_MSG);
}
memcpy(mpBuf, pBuf, mnBufferLen);
mbEncoded = true;
}
const unsigned char* CppSQLite3Binary::getEncoded()
{
if (!mbEncoded)
{
unsigned char* ptmp = (unsigned char*)malloc(mnBinaryLen);
memcpy(ptmp, mpBuf, mnBinaryLen);
mnEncodedLen = sqlite3_encode_binary(ptmp, mnBinaryLen, mpBuf);
free(ptmp);
mbEncoded = true;
}
return mpBuf;
}
const unsigned char* CppSQLite3Binary::getBinary()
{
if (mbEncoded)
{
// in/out buffers can be the same
mnBinaryLen = sqlite3_decode_binary(mpBuf, mpBuf);
if (mnBinaryLen == -1)
{
throw CppSQLite3Exception(CPPSQLITE_ERROR,
"Cannot decode binary",
DONT_DELETE_MSG);
}
mbEncoded = false;
}
return mpBuf;
}
int CppSQLite3Binary::getBinaryLength()
{
getBinary();
return mnBinaryLen;
}
unsigned char* CppSQLite3Binary::allocBuffer(int nLen)
{
clear();
// Allow extra space for encoded binary as per comments in
// SQLite encode.c See bottom of this file for implementation
// of SQLite functions use 3 instead of 2 just to be sure ;-)
mnBinaryLen = nLen;
mnBufferLen = 3 + (257*nLen)/254;
mpBuf = (unsigned char*)malloc(mnBufferLen);
if (!mpBuf)
{
throw CppSQLite3Exception(CPPSQLITE_ERROR,
"Cannot allocate memory",
DONT_DELETE_MSG);
}
mbEncoded = false;
return mpBuf;
}
void CppSQLite3Binary::clear()
{
if (mpBuf)
{
mnBinaryLen = 0;
mnBufferLen = 0;
free(mpBuf);
mpBuf = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
CppSQLite3Query::CppSQLite3Query()
{
mpVM = 0;
mbEof = true;
mnCols = 0;
mbOwnVM = false;
}
CppSQLite3Query::CppSQLite3Query(const CppSQLite3Query& rQuery)
{
mpVM = rQuery.mpVM;
// Only one object can own the VM
const_cast<CppSQLite3Query&>(rQuery).mpVM = 0;
mbEof = rQuery.mbEof;
mnCols = rQuery.mnCols;
mbOwnVM = rQuery.mbOwnVM;
}