⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmsqllite.h

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 H
字号:
#ifndef VM_SQL_LITE_WRAPPER_H_INCLUDED
#define VM_SQL_LITE_WRAPPER_H_INCLUDED
/*****************************************************************************/
/*                              HEADER FILE                                  */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description:   Declaration of a wrapper class for the sql lite library

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      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.
*/
/*****************************************************************************/


#pragma warning( disable : 4101 4786)

#include <map>
#include <string>

#include "VMDateTime.h"
#include "VMString.h"

typedef struct sqlite    sqlite;
typedef struct sqlite_vm sqlite_vm;


///////////////////////////////////////////////////////////////////////////////
//
// Define an exception class that will be thrown on any database error
//
///////////////////////////////////////////////////////////////////////////////


class VMSqlLiteException
{
public:
	VMString m_oErrorText;

	VMSqlLiteException( VMString oErrorText = _T("") ) 
  : m_oErrorText( oErrorText ) {;};

	inline operator VMString ()  const { return (m_oErrorText ); };
};



///////////////////////////////////////////////////////////////////////////////
//
// Next, define a database connection object
//
///////////////////////////////////////////////////////////////////////////////


class VMSqlLiteDatabase
{
private:
  sqlite*              m_poDB;
  CRITICAL_SECTION     m_xCriticalSection;

  VMSqlLiteDatabase( void );
  VMSqlLiteDatabase& operator = ( VMSqlLiteDatabase& roOther ){;};
  VMSqlLiteDatabase( VMSqlLiteDatabase& roOther ){;};

public:
  VMSqlLiteDatabase( const char* pchFileName );

  VMString m_oErrorMessage;

  ~VMSqlLiteDatabase( void );

  void GetLock( void );
  void FreeLock( void );

  bool Open( VMString oFileName );
  bool Close( void );

  bool IsOkay( void ) const 
  { return( m_poDB != NULL ); };

  sqlite* GetDB( void ){ return( m_poDB ); };
};


///////////////////////////////////////////////////////////////////////////////
//
// Next, define a collection object for storage of result sets
//
///////////////////////////////////////////////////////////////////////////////


typedef std::map< std::string, int > MAP_FIELDS_BY_NAME;
typedef MAP_FIELDS_BY_NAME::iterator MAP_FIELDS_BY_NAME_ITER;


///////////////////////////////////////////////////////////////////////////////
//
// Next, define a base query class
//
///////////////////////////////////////////////////////////////////////////////


class VMSqlLiteQueryBase
{
public:
  int GetColumnIndexForName( VMString oFieldName );

  class CDBField
  {
  private:
    const char* m_pchData;

  public:
    CDBField( const char* pchData ) : m_pchData( pchData ){;};

    const char* AsChar  ( void ) const;
    VMString    AsString( void ) const;
    long        AsLong  ( void ) const;
    bool        AsBool  ( void ) const;
    VMDateTime  AsDate  ( void ) const;
    double      AsDouble( void ) const;
  };

protected:
  VMSqlLiteDatabase*     m_poDB;
  MAP_FIELDS_BY_NAME     m_oColumnNames;

  VMSqlLiteQueryBase( VMSqlLiteDatabase* poDB );

  void SetColumnNameAtIndex( const char** ppchColumnNames, int iColumnCount );
};


///////////////////////////////////////////////////////////////////////////////
//
// Next, define a query class
//
///////////////////////////////////////////////////////////////////////////////


class VMSqlLiteQuery : public VMSqlLiteQueryBase
{
private:
  sqlite_vm*   m_poDBVM;
  int          m_iColumnIndex;
  const char** m_ppchColumnValue;
  const char** m_ppchColumnName;

public:
  VMString m_oLastError;

  VMSqlLiteQuery( VMSqlLiteDatabase* poDB );
  ~VMSqlLiteQuery( void );

  bool Close( void );
  bool Execute( const char* pchFormatString, ... );

  bool IsEOF( void ) const;
  bool FetchData( void );
  void CancelStatement( void );

  int         GetColumnIndex( void ) const;
  const char* GetColumnNameAtIndex( int iFieldIndex );

  CDBField GetColumnValueByIndex( int iFieldIdx );
  CDBField GetColumnValueByName( VMString oFieldName );
};


///////////////////////////////////////////////////////////////////////////////
//
// Next, define a query class that can iterate result sets
//
///////////////////////////////////////////////////////////////////////////////


class VMSqlLiteQueryResult : public VMSqlLiteQueryBase
{
private:   
  char**  m_ppchResult;
  int     m_iRowCount;
  int     m_iColumnCount;
  int     m_iCursor;

public:
  VMString m_oLastError;

  VMSqlLiteQueryResult( VMSqlLiteDatabase* poDB );
  ~VMSqlLiteQueryResult( void );

  bool Execute( const char* pchFormatText, ... );
  int  NumChanges( void );

  int  GetRowCount( void );
  int  GetColumnCount( void );

  bool IsBOF() { return( m_iCursor == 0 );           };
  bool IsEOF() { return( m_iCursor == m_iRowCount ); };

  bool MoveNextRow( void )
  {
    if ( !IsEOF() )
    {
      ++m_iCursor;
      return( true );
    }
    else
    {
      return( false );
    }
  };

  bool MovePrevRow( void )
  {
    if ( !IsBOF() )
    {
      --m_iCursor;
      return( true );
    }
    else
    {
      return( false );
    }
  }


  char* GetFieldNameByIndex( int iFieldIndex );

  CDBField GetValueAt( int fieldIdx );
  CDBField FieldByName( VMString fieldName );

  CDBField GetValueAt( int rowIdx, int fieldIdx );
  CDBField FieldByName( int rowIdx, VMString fieldName );
};



#endif


/*****************************************************************************/
/* Check-in history */
/*
 *$Log:  $
*/
/*****************************************************************************/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -