diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e82357f..a0b051f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,7 @@ add_subdirectory( "deps/mysqlConnector" ) add_subdirectory( "src/common" ) add_subdirectory( "src/servers" ) -#add_subdirectory( "src/dbm" ) +add_subdirectory( "src/dbm" ) add_subdirectory( "src/tools/exd_common_gen" ) add_subdirectory( "src/tools/exd_struct_gen" ) diff --git a/src/dbm/DbManager.cpp b/src/dbm/DbManager.cpp index 7035e29f..908895b6 100644 --- a/src/dbm/DbManager.cpp +++ b/src/dbm/DbManager.cpp @@ -14,6 +14,21 @@ DbManager::~DbManager() } +bool DbManager::execute( const std::string& sql ) +{ + try + { + auto stmt = m_pConnection->createStatement(); + bool result = stmt->execute( sql ); + return result; + } + catch( std::runtime_error& e ) + { + m_lastError = e.what(); + return false; + } +} + bool DbManager::connect() { std::shared_ptr< Mysql::MySqlBase > base( new Mysql::MySqlBase() ); @@ -67,3 +82,55 @@ Mode DbManager::getMode() const { return m_mode; } + +bool DbManager::performAction() +{ + bool result = false; + execute( " SET autocommit = 0 " ); + m_pConnection->beginTransaction(); + + switch( m_mode ) + { + case Mode::INIT: + result = modeInit(); + break; + case Mode::LIQUIDATE: + break; + case Mode::UPDATE: + break; + case Mode::CHECK: + break; + case Mode::CLEAN_CHARS: + break; + } + if( !result ) + m_pConnection->rollbackTransaction(); + else + m_pConnection->commitTransaction(); + + return result; +} + +bool DbManager::modeInit() +{ + + bool result = false; + + if( selectSchema() ) + { + m_lastError = "Database already existing, use mode first to remove it."; + return false; + } + + if( !execute( "CREATE DATABASE " + m_database ) ) + return false; + + if( !execute( "CREATE TABLE `dbversion` (\n" + " `major` int(11) NOT NULL,\n" + " `minor` int(11) NOT NULL\n" + ") ENGINE=InnoDB DEFAULT CHARSET=latin1;" ) ) + return false; + + + return false; +} diff --git a/src/dbm/DbManager.h b/src/dbm/DbManager.h index 7c6cd2eb..1d9db329 100644 --- a/src/dbm/DbManager.h +++ b/src/dbm/DbManager.h @@ -29,6 +29,12 @@ class DbManager bool connect(); bool selectSchema(); + bool execute( const std::string& sql ); + + bool performAction(); + + bool modeInit(); + virtual ~DbManager(); const std::string& getLastError(); diff --git a/src/dbm/main.cpp b/src/dbm/main.cpp index d7e337c9..3c1f8d14 100644 --- a/src/dbm/main.cpp +++ b/src/dbm/main.cpp @@ -43,13 +43,8 @@ std::vector< std::string > getAllFilesInDir( const std::string& dirPath, ( std::find( dirSkipList.begin(), dirSkipList.end(), iter->path().filename() ) != dirSkipList.end() ) ) { // Skip the iteration of current directory pointed by iterator -#ifdef USING_BOOST - // Boost Fileystsem API to skip current directory iteration - iter.no_push(); -#else // c++17 Filesystem API to skip current directory iteration iter.disable_recursion_pending(); -#endif } else { @@ -57,7 +52,7 @@ std::vector< std::string > getAllFilesInDir( const std::string& dirPath, listOfFiles.push_back( iter->path().string() ); } - std::error_code ec; + std::error_code ec; // Increment the iterator to point to next entry in recursive iteration iter.increment( ec ); if( ec ) @@ -176,13 +171,19 @@ int main( int32_t argc, char* argv[] ) return 0; } - if( !dbm.selectSchema() ) + if( !dbm.performAction() ) { - g_log.fatal( "Could not set schema!" ); + g_log.fatal( "Could not perform action!" ); g_log.fatal( dbm.getLastError() ); - return 0; } + //if( !dbm.selectSchema() ) + //{ + // g_log.fatal( "Could not set schema!" ); + // g_log.fatal( dbm.getLastError() ); + // return 0; + //} + return 0;