📄 vmsqllite3.cpp
字号:
/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: Implementation of SQL-Lite version 3 database wrapper objects
This file is based on a web-sample found at:
http://www.codeproject.com/database/CppSQLite.asp
by Rob Groves. This file has been heavily revised, renamed
and reformatted, to be compliant with this project's coding
standards so may not resemble the original file very much
at all. The original authors comments follow.
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()
TOOL And XML FORMS License
==========================
Except where otherwise noted, and where not in conflict with other
rights already owned, all of the documentation and software included
in the TOOL package is copyrighted by Michael Swartzendruber.
Portions of this file are Copyright (C) 2005
Michael John Swartzendruber. All rights reserved.
Access to this code, whether intentional or accidental,
does NOT IMPLY any transfer of rights.
This software is provided "as-is," without any express
or implied warranty. In no event shall the author be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for
any purpose, including commercial applications, and to
alter and redistribute it, provided that the following
conditions are met:
1. All redistributions of source code files must retain
all copyright notices that are currently in place,
and this list of conditions without modification.
2. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
3. If you use this software in another product, an acknowledgment
in the product documentation would be appreciated but is
not required.
4. Modified versions in source or binary form must be plainly
marked as such, and must not be misrepresented as being
the original software.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/
#include "../../SqlLite3x/PragmaWarningDisable.h"
#include "VMSqlLite3.h"
#include <cstdlib>
///////////////////////////////////////////////////////////////////////////////
//
// Named constant for passing to VMSqlite3xException 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* pchInput, int iLength, unsigned char* pchOutput );
int sqlite3_decode_binary( const unsigned char* pchInput, unsigned char* pchOutput );
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
VMSqlite3xException::VMSqlite3xException( const int iErrorCode,
char* pchErrorMessage,
bool bDeleteMessage )
: m_iErrorCode( iErrorCode )
{
m_pchErrorMessage = sqlite3_mprintf( "%s[%d]: %s",
GetErrorCodeAsString( iErrorCode ),
iErrorCode,
pchErrorMessage ? pchErrorMessage : "" );
if ( bDeleteMessage && pchErrorMessage )
{
sqlite3_free( pchErrorMessage );
}
}
VMSqlite3xException::VMSqlite3xException( const VMSqlite3xException& roOther )
: m_iErrorCode( roOther.m_iErrorCode )
{
m_pchErrorMessage = 0;
if ( roOther.m_pchErrorMessage )
{
m_pchErrorMessage = sqlite3_mprintf( "%s", roOther.m_pchErrorMessage );
}
}
const char* VMSqlite3xException::GetErrorCodeAsString( int iErrorCode )
{
switch ( iErrorCode )
{
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";
}
}
VMSqlite3xException::~VMSqlite3xException( void )
{
if ( m_pchErrorMessage )
{
sqlite3_free( m_pchErrorMessage );
m_pchErrorMessage = 0;
}
}
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
VMSqlite3xBuffer::VMSqlite3xBuffer( void )
{
m_pchBuffer = 0;
}
VMSqlite3xBuffer::~VMSqlite3xBuffer( void )
{
ClearBuffer();
}
void VMSqlite3xBuffer::ClearBuffer( void )
{
if ( m_pchBuffer )
{
sqlite3_free( m_pchBuffer );
m_pchBuffer = 0;
}
}
const char* VMSqlite3xBuffer::FormatStatement( const char* pchFormat, ... )
{
ClearBuffer();
va_list vaArgs;
va_start( vaArgs, pchFormat );
m_pchBuffer = sqlite3_vmprintf( pchFormat, vaArgs );
va_end( vaArgs );
return( m_pchBuffer );
}
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
VMSqlite3xBlob::VMSqlite3xBlob( void )
: m_pchBuffer( 0 ),
m_iBinaryValueLength( 0 ),
m_iBufferLength( 0 ),
m_iEncodedValueLength( 0 ),
m_bValueIsEncoded( false )
{
}
VMSqlite3xBlob::~VMSqlite3xBlob( void )
{
ClearBuffer();
}
void VMSqlite3xBlob::SetBinaryValue( const unsigned char* pchBuffer, int iLength )
{
m_pchBuffer = AllocateBuffer( iLength );
memcpy( m_pchBuffer, pchBuffer, iLength );
}
void VMSqlite3xBlob::SetEncodedValue( const unsigned char* pchBuffer )
{
ClearBuffer();
m_iEncodedValueLength = strlen( (const char*)pchBuffer );
m_iBufferLength = m_iEncodedValueLength + 1; // Allow for NULL terminator
m_pchBuffer = (unsigned char*)malloc( m_iBufferLength );
if ( !m_pchBuffer )
{
throw VMSqlite3xException( CPPSQLITE_ERROR,
"Cannot allocate memory",
DONT_DELETE_MSG );
}
memcpy( m_pchBuffer, pchBuffer, m_iBufferLength );
m_bValueIsEncoded = true;
}
const unsigned char* VMSqlite3xBlob::GetEncodedValue( void )
{
if ( !m_bValueIsEncoded )
{
unsigned char* pchTemp = (unsigned char*)malloc( m_iBinaryValueLength );
memcpy( pchTemp, m_pchBuffer, m_iBinaryValueLength );
m_iEncodedValueLength = sqlite3_encode_binary( pchTemp, m_iBinaryValueLength, m_pchBuffer );
free( pchTemp );
m_bValueIsEncoded = true;
}
return( m_pchBuffer );
}
const unsigned char* VMSqlite3xBlob::GetBinaryValue( void )
{
if ( m_bValueIsEncoded )
{
// in/out buffers can be the same
//
m_iBinaryValueLength = sqlite3_decode_binary( m_pchBuffer, m_pchBuffer );
if ( m_iBinaryValueLength == -1 )
{
throw VMSqlite3xException( CPPSQLITE_ERROR,
"Cannot decode binary",
DONT_DELETE_MSG );
}
m_bValueIsEncoded = false;
}
return( m_pchBuffer );
}
int VMSqlite3xBlob::GetBinaryValueLength( void )
{
GetBinaryValue();
return( m_iBinaryValueLength );
}
unsigned char* VMSqlite3xBlob::AllocateBuffer( int iBufferLength )
{
ClearBuffer();
// 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 ;-)
//
m_iBinaryValueLength = iBufferLength;
m_iBufferLength = 3 + ( 257 * iBufferLength ) / 254;
m_pchBuffer = (unsigned char*)malloc( m_iBufferLength );
if ( !m_pchBuffer )
{
throw VMSqlite3xException( CPPSQLITE_ERROR,
"Cannot allocate memory",
DONT_DELETE_MSG );
}
m_bValueIsEncoded = false;
return( m_pchBuffer );
}
void VMSqlite3xBlob::ClearBuffer( void )
{
if ( m_pchBuffer )
{
m_iBinaryValueLength = 0;
m_iBufferLength = 0;
free( m_pchBuffer );
m_pchBuffer = 0;
}
}
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
VMSqlite3xQuery::VMSqlite3xQuery( void )
{
m_poDb3VirtualMachineInstance = 0;
m_bIsAtEof = true;
m_iColumnCount = 0;
m_bThisOwnsVirtualMachine = false;
}
VMSqlite3xQuery::VMSqlite3xQuery( const VMSqlite3xQuery& roOther )
{
m_poDb3VirtualMachineInstance = roOther.m_poDb3VirtualMachineInstance;
// Only one object can own the VM
//
const_cast< VMSqlite3xQuery& >(roOther ).m_poDb3VirtualMachineInstance = 0;
m_bIsAtEof = roOther.m_bIsAtEof;
m_iColumnCount = roOther.m_iColumnCount;
m_bThisOwnsVirtualMachine = roOther.m_bThisOwnsVirtualMachine;
}
VMSqlite3xQuery::VMSqlite3xQuery( sqlite3* poDB,
sqlite3_stmt* poVM,
bool bIsAtEof,
bool bThisOwnsVM )
{
m_poDb3Instance = poDB;
m_poDb3VirtualMachineInstance = poVM;
m_bIsAtEof = bIsAtEof;
m_iColumnCount = sqlite3_column_count( m_poDb3VirtualMachineInstance );
m_bThisOwnsVirtualMachine = bThisOwnsVM;
}
VMSqlite3xQuery::~VMSqlite3xQuery( void )
{
try
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -