SQLiteCommand.h:
#pragma once
#include
#include
SQLiteCommand.cpp:
#include "SQLiteCommand.h"
#include
#include
#define SQLITE_SAFE_CLOSE( Ptr ){ if( nullptr != Ptr ){ sqlite3_close( Ptr ); Ptr = nullptr; } }
#define SQLITE_SAFE_FREE( Ptr ){ if( nullptr != Ptr ){ sqlite3_free( Ptr ); Ptr = nullptr; } }
#define SQLITE_SAFE_FREE_TABLE( Ptr ){ if( nullptr != Ptr ){ sqlite3_free_table( Ptr ); Ptr = nullptr; } }
#define SQL_SCRIPT_SIZE 1024
SQLiteCommand::SQLiteCommand()
:mpCommand( nullptr )
,mIndex( -1 )
{}
SQLiteCommand::~SQLiteCommand()
{
Disconnect();
}
bool SQLiteCommand::Connect( LPCSTR pDataBaseName )
{
if( nullptr == pDataBaseName )
{
assert( false );
return false;
}
if( SQLITE_OK != sqlite3_open( pDataBaseName, &mpCommand ) )
{
assert( false );
return false;
}
return true;
}
bool SQLiteCommand::Disconnect()
{
if( nullptr == mpCommand )
{
return true;
}
mIndex = -1;
mDataTable.clear();
SQLITE_SAFE_CLOSE( mpCommand );
return true;
}
bool SQLiteCommand::IsExistsTable( LPCSTR pTableName )
{
if( nullptr == pTableName )
{
assert( false );
return false;
}
CHAR SQLScript[ SQL_SCRIPT_SIZE ];
StringCbPrintfA( SQLScript, sizeof( SQLScript ), "select count(*) from sqlite_master where type='table' and name='%s'", pTableName );
LPSTR pException = nullptr;
LPCH *pData = nullptr;
int RowCount = 0;
int ColumnCount = 0;
if( SQLITE_OK != sqlite3_get_table( mpCommand, SQLScript, &pData, &RowCount, &ColumnCount, &pException ) )
{
assert( !pException );
return false;
}
const bool IsExists = atoi( pData[ ColumnCount ] ) > 0;
SQLITE_SAFE_FREE_TABLE( pData );
return IsExists;
}
bool SQLiteCommand::CreateTable( LPCSTR pTableName, LPCSTR pFieldScript )
{
if( nullptr == pTableName || nullptr == pFieldScript )
{
assert( false );
return false;
}
CHAR SQLScript[ SQL_SCRIPT_SIZE ];
StringCbPrintfA( SQLScript, sizeof( SQLScript ), "create table %s(%s)", pTableName, pFieldScript );
LPSTR pException = nullptr;
if( SQLITE_OK != sqlite3_exec( mpCommand, SQLScript, nullptr, nullptr, &pException ) )
{
assert( !pException );
return false;
}
return true;
}
bool SQLiteCommand::SelectTable( LPCSTR pTableName, LPCSTR pSelectField /*= "*"*/, LPCSTR pWhere /*= "" */, LPCSTR pOrder /*= "" */ )
{
if( nullptr == pTableName || nullptr == pSelectField || nullptr == pWhere || nullptr == pOrder )
{
assert( false );
return false;
}
CHAR SQLScript[ SQL_SCRIPT_SIZE ];
if( 0 == *pWhere )
{
StringCbPrintfA( SQLScript, sizeof( SQLScript ), "select %s from %s", pSelectField, pTableName );
}
else
{
StringCbPrintfA( SQLScript, sizeof( SQLScript ), "select %s from %s where %s", pSelectField, pTableName, pWhere );
}
if( 0 != *pOrder )
{
StringCbCatA( SQLScript, sizeof( SQLScript ), " order by " );
StringCbCatA( SQLScript, sizeof( SQLScript ), pOrder );
}
LPSTR pException = nullptr;
LPCH *pData = nullptr;
int RowCount = 0;
int ColumnCount = 0;
if( SQLITE_OK != sqlite3_get_table( mpCommand, SQLScript, &pData, &RowCount, &ColumnCount, &pException ) )
{
assert( !pException );
return false;
}
mDataTable.clear();
std::map< std::string, std::string > Row;
for( int i = 0; i < RowCount; ++i )
{
for( int j = 0; j < ColumnCount; ++j )
{
const auto Insert = Row.insert( std::make_pair( pData[ j ], pData[ ColumnCount + i * RowCount + j ] ) );
if( false == Insert.second )
{
assert( false );
return false;
}
}
mDataTable.push_back( Row );
Row.clear();
}
mIndex = 0;
SQLITE_SAFE_FREE_TABLE( pData );
return true;
}
bool SQLiteCommand::InsertTable( LPCSTR pTableName, LPCSTR pInsertField, LPCSTR pInsertValue )
{
if( nullptr == pTableName || nullptr == pInsertField || nullptr == pInsertValue )
{
assert( false );
return false;
}
CHAR SQLScript[ SQL_SCRIPT_SIZE ];
StringCbPrintfA( SQLScript, sizeof( SQLScript ), "insert into %s( %s )values( %s )", pTableName, pInsertField, pInsertValue );
LPCH pException = nullptr;
if( SQLITE_OK != sqlite3_exec( mpCommand, SQLScript, nullptr, nullptr, &pException ) )
{
assert( !pException );
return false;
}
return true;
}
bool SQLiteCommand::UpdateTable(LPCSTR pTableName, LPCSTR pSetField, LPCSTR pWhere )
{
if( nullptr == pTableName || nullptr == pSetField || nullptr == pWhere )
{
assert( false );
return false;
}
CHAR SQLScript[ SQL_SCRIPT_SIZE ];
if( 0 == *pWhere )
{
StringCbPrintfA( SQLScript, sizeof( SQLScript ), "update set %s from %s", pSetField, pTableName );
}
else
{
StringCbPrintfA( SQLScript, sizeof( SQLScript ), "update set %s from %s where %s", pSetField, pTableName, pWhere );
}
LPCH pException = nullptr;
if( SQLITE_OK != sqlite3_exec( mpCommand, SQLScript, nullptr, nullptr, &pException ) )
{
assert( !pException );
return false;
}
return true;
}
bool SQLiteCommand::DeleteTable( LPCSTR pTableName, LPCSTR pWhere )
{
if( nullptr == pTableName || nullptr == pWhere )
{
assert( false );
return false;
}
CHAR SQLScript[ SQL_SCRIPT_SIZE ];
if( 0 == *pWhere )
{
StringCbPrintfA( SQLScript, sizeof( SQLScript ), "delete from %s", pTableName );
}
else
{
StringCbPrintfA( SQLScript, sizeof( SQLScript ), "delete from %s where %s", pTableName, pWhere );
}
LPCH pException = nullptr;
if( SQLITE_OK != sqlite3_exec( mpCommand, SQLScript, nullptr, nullptr, &pException ) )
{
assert( !pException );
return false;
}
return true;
}
bool SQLiteCommand::DropTable( LPCSTR pTableName )
{
if( nullptr == pTableName )
{
assert( false );
return false;
}
CHAR SQLScript[ SQL_SCRIPT_SIZE ];
StringCbPrintfA( SQLScript, sizeof( SQLScript ), "drop table %s", pTableName );
LPCH pException = nullptr;
if( SQLITE_OK != sqlite3_exec( mpCommand, SQLScript, nullptr, nullptr, &pException ) )
{
assert( !pException );
return false;
}
return true;
}
bool SQLiteCommand::IsBOF()
{
if( mDataTable.empty() )
{
return true;
}
return -1 == mIndex;
}
bool SQLiteCommand::IsEOF()
{
if( mDataTable.empty() )
{
return true;
}
return mDataTable.size() == mIndex;
}
bool SQLiteCommand::MovePrevious()
{
if( IsBOF() )
{
assert( false );
return false;
}
--mIndex;
return true;
}
bool SQLiteCommand::MoveNext()
{
if( IsEOF() )
{
assert( false );
return false;
}
++mIndex;
return true;
}
bool SQLiteCommand::MoveFirst()
{
if( mDataTable.empty() )
{
return true;
}
mIndex = 0;
return true;
}
bool SQLiteCommand::MoveLast()
{
if( mDataTable.empty() )
{
return true;
}
mIndex = mDataTable.size() - 1;
return true;
}
LPCSTR SQLiteCommand::GetField( LPCSTR pFieldName )
{
if( nullptr == pFieldName )
{
assert( false );
return nullptr;
}
const auto &Row = mDataTable[ mIndex ];
const auto &Cell = Row.find( pFieldName );
return Row.end() == Cell ? nullptr : Cell->second.c_str();
}