📄 vmsqllite3.h
字号:
#ifndef SQL_LITE_DBVERSION_3_WRAPPERS_H_INCLUDED
#define SQL_LITE_DBVERSION_3_WRAPPERS_H_INCLUDED
/*****************************************************************************/
/* HEADER FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: Declaration 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.
*/
/*****************************************************************************/
#include "../../SqlLite3x/sqlite3.h"
#include <cstdio>
#include <cstring>
#define CPPSQLITE_ERROR 1000
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
class VMSqlite3xException
{
public:
VMSqlite3xException( const int iErrCode,
char* pchErrorMessage,
bool bDeleteMsg = true );
VMSqlite3xException( const VMSqlite3xException& roOther );
virtual ~VMSqlite3xException( void );
const int GetErrorCode( void ) { return m_iErrorCode; };
const char* GetErrorText( void ) { return m_pchErrorMessage; };
static const char* GetErrorCodeAsString( int iErrorCode );
private:
int m_iErrorCode;
char* m_pchErrorMessage;
};
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
class VMSqlite3xBuffer
{
public:
VMSqlite3xBuffer( void );
~VMSqlite3xBuffer( void );
const char* FormatStatement( const char* pchFormat, ... );
void ClearBuffer( void );
operator const char*() { return m_pchBuffer; }
private:
char* m_pchBuffer;
};
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
class VMSqlite3xBlob
{
public:
VMSqlite3xBlob( void );
~VMSqlite3xBlob( void );
void SetBinaryValue ( const unsigned char* pchBuffer, int iLength );
void SetEncodedValue( const unsigned char* pchBuffer );
const unsigned char* GetEncodedValue( void );
const unsigned char* GetBinaryValue( void );
int GetBinaryValueLength( void );
unsigned char* AllocateBuffer( int iLength );
void ClearBuffer( void );
private:
unsigned char* m_pchBuffer;
int m_iBinaryValueLength;
int m_iBufferLength;
int m_iEncodedValueLength;
bool m_bValueIsEncoded;
};
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
class VMSqlite3xQuery
{
public:
VMSqlite3xQuery( void );
VMSqlite3xQuery( const VMSqlite3xQuery& roOther );
VMSqlite3xQuery( sqlite3* pDB,
sqlite3_stmt* pVM,
bool bEof,
bool bOwnVM = true );
VMSqlite3xQuery& operator=( const VMSqlite3xQuery& roOther );
virtual ~VMSqlite3xQuery();
int GetColumnCount( void );
int GetColumnIndexByName( const char* pchColumnName );
const char* GetColumnNameAtIndex( int iColumnIndex );
const char* GetColumnDataTypeNameAtIndex( int iColumnIndex );
int GetColumnDataTypeByIndex ( int iColumnIndex );
const char* GetColumnBufferAtIndex( int iColumnIndex );
const char* GetColumnBufferByName ( const char* pchColumnName );
int GetColumnValueAtIndexAsInt( int iColumnIndex, int iValueIfNull = 0 );
int GetColumnValueByNameAsInt ( const char* pchColumnName, int iValueIfNull = 0 );
double GetColumnValueAtIndexAsDouble( int iColumnIndex, double dblValueIfNull = 0.0 );
double GetColumnValueByNameAsDouble ( const char* pchColumnName, double dblValueIfNull = 0.0 );
const char* GetColumnValueAtIndexAsString( int iColumnIndex, const char* pchValueIfNull = "" );
const char* GetColumnValueByNameAsString ( const char* pchColumnName, const char* pchValueIfNull = "" );
const unsigned char* GetColumnValueAtIndexAsBlob( int iColumnIndex, int& riBufferLength );
const unsigned char* GetColumnValueByNameAsBlob ( const char* pchColumnName, int& riBufferLength );
bool IsColumnValueNullAtIndex( int iColumnIndex );
bool IsColumnValueNullAtColumnName( const char* pchColumnName );
bool IsEOF( void );
void MoveToNextRow( void );
void Close( void );
private:
void CheckDatabaseVirtualMachine( void );
sqlite3* m_poDb3Instance;
sqlite3_stmt* m_poDb3VirtualMachineInstance;
bool m_bIsAtEof;
int m_iColumnCount;
bool m_bThisOwnsVirtualMachine;
};
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
class VMSqlite3xTable
{
public:
VMSqlite3xTable( void );
VMSqlite3xTable( const VMSqlite3xTable& roOther );
VMSqlite3xTable( char** ppchResults, int iRows, int iCols );
virtual ~VMSqlite3xTable( void );
VMSqlite3xTable& operator=( const VMSqlite3xTable& roOther );
int GetColumnCount( void );
int GetRowCount( void );
const char* GetColumnNameAtIndex( int iColumnIndex );
const char* GetColumnBufferAtIndex( int iColumnIndex );
const char* GetColumnBufferByName (const char* pchColumnName );
int GetColumnValueAtIndexAsInt( int iColumnIndex, int iValueIfNull = 0 );
int GetColumnValueByNameAsInt ( const char* pchColumnName, int iValueIfNull = 0 );
double GetColumnValueAtIndexAsDouble( int iColumnIndex, double dblValueIfNull = 0.0 );
double GetColumnValueByNameAsDouble ( const char* pchColumnName, double dblValueIfNull = 0.0 );
const char* GetColumnValueAtIndexAsString( int iColumnIndex, const char* pchValueIfNull = "" );
const char* GetColumnValueByNameAsString ( const char* pchColumnName, const char* pchValueIfNull = "" );
bool IsColumnValueNullAtIndex( int iColumnIndex );
bool IsColumnValueNullAtColumnName( const char* pchColumnName );
void MoveToRow( int iRowIndex );
int GetCurrentRowIndex( void ) { return( m_iCurrentRowIndex ); };
void Close( void );
private:
void CheckResults( void );
int m_iColumnCount;
int m_iRowCount;
int m_iCurrentRowIndex;
char** m_ppchResults;
};
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
class VMSqlite3xStatement
{
public:
VMSqlite3xStatement( void );
VMSqlite3xStatement( const VMSqlite3xStatement& roOther );
VMSqlite3xStatement( sqlite3* poDB, sqlite3_stmt* poVM );
virtual ~VMSqlite3xStatement( void );
VMSqlite3xStatement& operator=( const VMSqlite3xStatement& roOther );
int ExecuteDML( void );
VMSqlite3xQuery ExecuteQuery( void );
void BindParameter( int iParameterIndex, const char* pchValue );
void BindParameter( int iParameterIndex, const int iValue );
void BindParameter( int iParameterIndex, const double dblValue );
void BindParameter( int iParameterIndex, const unsigned char* pchBlobValue, int iLength );
void BindNullParameter( int iParameterIndex );
void Reset( void );
void Close( void );
private:
void CheckDatabase( void );
void CheckDatabaseVirtualMachine( void );
sqlite3* m_poDb3Instance;
sqlite3_stmt* m_poDb3VirtualMachineInstance;
};
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
class VMSqlite3xDatabase
{
public:
VMSqlite3xDatabase( void );
virtual ~VMSqlite3xDatabase( void );
void Open( const char* szFile );
void Close( void );
bool DoesTableExist( const char* pchTableName );
int ExecuteDML ( const char* pchSqlText );
int ExecuteScalar( const char* pchSqlText );
VMSqlite3xQuery ExecuteQuery ( const char* pchSqlText );
VMSqlite3xTable GetTableByName( const char* pchTableName );
VMSqlite3xStatement PrepareStatement( const char* pchFormattedSqlText );
sqlite_int64 GetRowIdForLastInsert( void );
void SendInterruptSignal( void ) { sqlite3_interrupt( m_poDb3Instance ); };
void SetTimeout( int iMilliSeconds );
static const char* GetSqlLiteVersion( void ) { return SQLITE_VERSION; };
private:
VMSqlite3xDatabase( const VMSqlite3xDatabase& roOther );
VMSqlite3xDatabase& operator=( const VMSqlite3xDatabase& roOther );
sqlite3_stmt* CompileStatement( const char* pchFormattedSqlText );
void CheckDatabase( void );
sqlite3* m_poDb3Instance;
int m_iTimeoutValue;
};
#endif
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -