📄 vmsqllite.cpp
字号:
/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: Implementation of sql lite database wrapper classes
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.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/
#include "../../../stdafx.h"
#include <stdlib.h>
#include <io.h>
#include <stdio.h>
#include <time.h>
#include "../../SqlLite/sqlite.h"
#include "VMSqlLite.h"
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteDatabase::VMSqlLiteDatabase
DESCRIPTION: opens the connection to the indicated database file
INPUT: pchFileName - pointer to the full path to the database file
OUTPUT: none
RETURNS: -
*/
VMSqlLiteDatabase::VMSqlLiteDatabase( const char* pchFileName ) : m_poDB( NULL )
{
InitializeCriticalSection( &m_xCriticalSection );
if ( NULL != pchFileName )
{
Open( VMString( pchFileName ) );
}
}
/* End of function "VMSqlLiteDatabase::VMSqlLiteDatabase"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteDatabase::VMSqlLiteDatabase
DESCRIPTION: default ctor for database connection object
INPUT: void
OUTPUT: none
RETURNS: none
*/
VMSqlLiteDatabase::VMSqlLiteDatabase( void ) : m_poDB( NULL )
{
InitializeCriticalSection( &m_xCriticalSection );
}
/* End of function "VMSqlLiteDatabase::VMSqlLiteDatabase"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteDatabase::~VMSqlLiteDatabase
DESCRIPTION: dtor
INPUT: void
OUTPUT: none
RETURNS: none
*/
VMSqlLiteDatabase::~VMSqlLiteDatabase( void )
{
Close();
DeleteCriticalSection( &m_xCriticalSection );
}
/* End of function "VMSqlLiteDatabase::~VMSqlLiteDatabase"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteDatabase::GetLock
DESCRIPTION: single threads the connection objct
INPUT: void
OUTPUT: none
RETURNS: void
*/
void VMSqlLiteDatabase::GetLock( void )
{
EnterCriticalSection( &m_xCriticalSection );
}
/* End of function "VMSqlLiteDatabase::GetLock"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteDatabase::FreeLock
DESCRIPTION: thread release of this
INPUT: void
OUTPUT: none
RETURNS: void
*/
void VMSqlLiteDatabase::FreeLock( void )
{
LeaveCriticalSection( &m_xCriticalSection );
}
/* End of function "VMSqlLiteDatabase::FreeLock"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteDatabase::Open
DESCRIPTION: opens and attaches to the given database file
INPUT: oFileName - the file name to open
OUTPUT: none
RETURNS: true if worked, false if not
*/
bool VMSqlLiteDatabase::Open( VMString oFileName )
{
char* pchErrorMessage = NULL;
GetLock();
m_poDB = sqlite_open( oFileName.Buffer(), 0, &pchErrorMessage );
if ( !m_poDB )
{
if ( pchErrorMessage )
{
m_oErrorMessage = VMString( pchErrorMessage );
sqlite_freemem( pchErrorMessage );
}
FreeLock();
return( false );
}
FreeLock();
return( true );
}
/* End of function "VMSqlLiteDatabase::Open"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteDatabase::Close
DESCRIPTION: detaches this from the database file
INPUT: void
OUTPUT: none
RETURNS: true if worked, false if not
*/
bool VMSqlLiteDatabase::Close( void )
{
GetLock();
if ( m_poDB )
{
sqlite_close( m_poDB );
m_poDB = NULL;
}
FreeLock();
return( true );
}
/* End of function "VMSqlLiteDatabase::Close"
/*****************************************************************************/
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQueryBase::VMSqlLiteQueryBase
DESCRIPTION: ctor
INPUT: poDB - pointer to database connection object
OUTPUT: none
RETURNS: none
*/
VMSqlLiteQueryBase::VMSqlLiteQueryBase( VMSqlLiteDatabase* poDB ) : m_poDB( poDB )
{
}
/* End of function "VMSqlLiteQueryBase::VMSqlLiteQueryBase"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQueryBase::SetColumnNameAtIndex
DESCRIPTION: for results result set, set the name of the column at the
the each column index
INPUT: ppchColumnNames - pointer to column names array
iColumnCount - the number of columns in the names array
OUTPUT:
RETURNS: void -
*/
void VMSqlLiteQueryBase::SetColumnNameAtIndex( const char** ppchColumnNames, int iColumnCount )
{
m_oColumnNames.clear();
for ( int iLoop = 0; iLoop < iColumnCount; iLoop++ )
{
VMString oColumnName( ppchColumnNames[ iLoop ] );
oColumnName.Lower();
m_oColumnNames[ std::string( oColumnName.Buffer() ) ] = iLoop;
}
}
/* End of function "VMSqlLiteQueryBase::SetColumnNameAtIndex"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQueryBase::GetColumnIndexForName
DESCRIPTION: returns the column index for the given column
INPUT: oFieldName - the name of the column to look up
OUTPUT: none
RETURNS: -1 if failed, or column index
*/
int VMSqlLiteQueryBase::GetColumnIndexForName( VMString oFieldName )
{
oFieldName.Lower();
MAP_FIELDS_BY_NAME_ITER oIter = m_oColumnNames.find( std::string( oFieldName.Buffer() ) );
if ( oIter == m_oColumnNames.end() )
{
return( -1 );
}
return( (*oIter).second );
}
/* End of function "VMSqlLiteQueryBase::GetColumnIndexForName"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQueryBase::CDBField::AsChar
DESCRIPTION: convert a result value to a character pointer
INPUT: void
OUTPUT: none
RETURNS: value as character pointer
*/
const char* VMSqlLiteQueryBase::CDBField::AsChar( void ) const
{
return( m_pchData );
}
/* End of function "VMSqlLiteQueryBase::CDBField::AsChar"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQueryBase::CDBField::AsString
DESCRIPTION: convert a result value to a string object
INPUT: void
OUTPUT: none
RETURNS: value as string
*/
VMString VMSqlLiteQueryBase::CDBField::AsString( void ) const
{
return( VMString( m_pchData ) );
}
/* End of function "VMSqlLiteQueryBase::CDBField::AsString"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQueryBase::CDBField::AsLong
DESCRIPTION: convert a result value to a long
INPUT: void
OUTPUT: none
RETURNS: value as a long
*/
long VMSqlLiteQueryBase::CDBField::AsLong( void ) const
{
return( atol( m_pchData ) );
}
/* End of function "VMSqlLiteQueryBase::CDBField::AsLong"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQueryBase::CDBField::AsDouble
DESCRIPTION: convert a result value to a double
INPUT: void
OUTPUT: none
RETURNS: value as double
*/
double VMSqlLiteQueryBase::CDBField::AsDouble( void ) const
{
return( atof( m_pchData ) );
}
/* End of function "VMSqlLiteQueryBase::CDBField::AsDouble"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQueryBase::CDBField::AsBool
DESCRIPTION: convert a result value to a boolean
INPUT: void
OUTPUT: none
RETURNS: value as boolean
*/
bool VMSqlLiteQueryBase::CDBField::AsBool( void ) const
{
return( AsLong() != 0 );
}
/* End of function "VMSqlLiteQueryBase::CDBField::AsBool"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQueryBase::CDBField::AsDate
DESCRIPTION: convert a result value to a date object
INPUT: void
OUTPUT: none
RETURNS: value as date
*/
VMDateTime VMSqlLiteQueryBase::CDBField::AsDate( void ) const
{
return( VMDateTime( m_pchData ) );
}
/* End of function "VMSqlLiteQueryBase::CDBField::AsDate"
/*****************************************************************************/
///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQuery::VMSqlLiteQuery
DESCRIPTION: ctor
INPUT: poDB - pointer to database connection object
OUTPUT: none
RETURNS: none
*/
VMSqlLiteQuery::VMSqlLiteQuery( VMSqlLiteDatabase* poDB )
: VMSqlLiteQueryBase( poDB ),
m_poDBVM( NULL ),
m_iColumnIndex( -1 ),
m_ppchColumnValue( NULL ),
m_ppchColumnName(NULL)
{
}
/* End of function "VMSqlLiteQuery::VMSqlLiteQuery"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQuery::~VMSqlLiteQuery
DESCRIPTION: dtor
INPUT: void
OUTPUT: none
RETURNS: none
*/
VMSqlLiteQuery::~VMSqlLiteQuery( void )
{
Close();
}
/* End of function "VMSqlLiteQuery::~VMSqlLiteQuery"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMSqlLiteQuery::Close
DESCRIPTION: close the query set
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -