00001 #include "sqlitewrapper.h"
00002 #include <sqlite3.h>
00003 #include <cstring>
00004 #include <stdexcept>
00005
00006 #include <iostream>
00007
00008
00009 Dedupe::Dataholding::SqliteWrapper::
00010 SqliteWrapper( std::string Path ) : dbs_path( Path ),
00011 db_handle( NULL ),
00012 ReturningTable( )
00013 {
00014
00015 Check( sqlite3_open( dbs_path.c_str(), &db_handle ) );
00016 }
00017
00018 Dedupe::Dataholding::SqliteWrapper::~SqliteWrapper()
00019 {
00020
00021 FreeResult();
00022
00023 sqlite3_close( db_handle );
00024 }
00025
00026 Dedupe::Dataholding::SqliteWrapper::WrapperCode
00027 Dedupe::Dataholding::SqliteWrapper::Check( short SqlState )
00028 {
00029 switch( SqlState )
00030 {
00031 case SQLITE_OK : return WrapperOk; break;
00032 default : sqlite3_errmsg( db_handle );
00033 throw std::runtime_error( sqlite3_errmsg( db_handle ) );
00034 }
00035
00036 return WrapperError;
00037 }
00038
00039 Dedupe::Dataholding::SqliteWrapper::WrapperCode
00040 Dedupe::Dataholding::SqliteWrapper::SqlExec( const std::string &SqlCommand )
00041 {
00042
00043 FreeResult();
00044
00045 sqlite3_stmt* stmt;
00046 const char* PointerToCommand = SqlCommand.c_str();
00047 int size = SqlCommand.size();
00048 const char* NextSqlCommand = NULL;
00049 Dedupe::Dataholding::SqliteWrapper::Row CurrentRow;
00050
00051
00052 while( PointerToCommand != NULL )
00053 {
00054 Dedupe::Dataholding::SqliteWrapper::Check( sqlite3_prepare_v2(
00055 db_handle, PointerToCommand, size, &stmt, &NextSqlCommand ) );
00056
00057
00058 if( strlen( NextSqlCommand ) != 0 ) PointerToCommand = NextSqlCommand;
00059 else PointerToCommand = NULL;
00060
00061
00062 int cols = sqlite3_column_count( stmt );
00063
00064
00065 std::string name, value;
00066 while ( sqlite3_step( stmt ) == SQLITE_ROW )
00067 {
00068 for( int i = 0; i < cols; i++ )
00069 {
00070 if( !sqlite3_column_name( stmt, i ) ) continue;
00071
00072 name = sqlite3_column_name( stmt, i );
00073 if( name.empty() ) continue;
00074
00075 if( sqlite3_column_text( stmt, i ) )
00076 value = reinterpret_cast<const char*>( sqlite3_column_text( stmt, i ));
00077 CurrentRow[ name ] = value;
00078 }
00079 ReturningTable.push_back( CurrentRow );
00080 }
00081
00082
00083
00084 sqlite3_finalize(stmt);
00085 }
00086 return WrapperOk;
00087 }
00088
00089 Dedupe::Dataholding::SqliteWrapper::WrapperCode
00090 Dedupe::Dataholding::SqliteWrapper::ExecStatement( sqlite3_stmt *stmt )
00091 {
00092
00093 FreeResult();
00094
00095
00096 Dedupe::Dataholding::SqliteWrapper::Row CurrentRow;
00097
00098
00099 int cols = sqlite3_column_count( stmt );
00100
00101
00102 std::string name, value;
00103 while ( sqlite3_step( stmt ) == SQLITE_ROW )
00104 {
00105 for( int i = 0; i < cols; i++ )
00106 {
00107 if( !sqlite3_column_name( stmt, i ) ) continue;
00108
00109 name = sqlite3_column_name( stmt, i );
00110 if( name.empty() ) continue;
00111
00112 if( sqlite3_column_text( stmt, i ) )
00113 value = reinterpret_cast<const char*>( sqlite3_column_text( stmt, i ));
00114 CurrentRow[ name ] = value;
00115 }
00116 ReturningTable.push_back( CurrentRow );
00117 }
00118
00119 sqlite3_finalize(stmt);
00120
00121 return WrapperOk;
00122 }
00123
00124 Dedupe::Dataholding::SqliteWrapper::Result
00125 Dedupe::Dataholding::SqliteWrapper::GetTabledata()
00126 {
00127 return Dedupe::Dataholding::SqliteWrapper::ReturningTable;
00128 }
00129
00130 void Dedupe::Dataholding::SqliteWrapper::FreeResult()
00131 {
00132 ReturningTable.clear();
00133 }