From e69d112d5f02f1c34b4b11be5844f2d1df7ce98c Mon Sep 17 00:00:00 2001 From: mordred Date: Wed, 12 Dec 2018 12:23:56 +0100 Subject: [PATCH] Allow force mode in dbm --- sql_import.sh | 2 +- src/dbm/DbManager.cpp | 24 +++++++++++++++--------- src/dbm/DbManager.h | 3 +++ src/dbm/main.cpp | 11 +++++++---- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/sql_import.sh b/sql_import.sh index e95463df..cce9bdef 100644 --- a/sql_import.sh +++ b/sql_import.sh @@ -1,3 +1,3 @@ #!/bin/bash bin/sapphire_dbm --mode initialize --user root --database sapphire --sfile bin/sql/schema/schema.sql --ifile bin/sql/schema/inserts.sql -bin/sapphire_dbm --mode liquidate --user root --database sapphire +bin/sapphire_dbm --mode liquidate --user root --database sapphire --force diff --git a/src/dbm/DbManager.cpp b/src/dbm/DbManager.cpp index e6b98064..b4545ae7 100644 --- a/src/dbm/DbManager.cpp +++ b/src/dbm/DbManager.cpp @@ -12,7 +12,8 @@ DbManager::DbManager( const std::string& host, const std::string& database, cons m_password( pw ), m_port( port ), m_sFile( "sql/schema/schema.sql" ), - m_iFile( "sql/schema/inserts.sql" ) + m_iFile( "sql/schema/inserts.sql" ), + m_force( false ) { } @@ -36,6 +37,11 @@ bool DbManager::execute( const std::string& sql ) } } +void DbManager::setForceMode( bool mode ) +{ + m_force = mode; +} + bool DbManager::connect() { std::shared_ptr< Mysql::MySqlBase > base( new Mysql::MySqlBase() ); @@ -250,14 +256,14 @@ bool DbManager::modeLiquidate() return false; char type = '\0'; - - while( promptForChar( "This action will drop all tables in the database. Are you sure? [y/n]", type ) ) - { - if( type == 'y' ) - break; - if( type == 'n' ) - return true; - } + if( !m_force ) + while( promptForChar( "This action will drop all tables in the database. Are you sure? [y/n]", type ) ) + { + if( type == 'y' ) + break; + if( type == 'n' ) + return true; + } std::string query = "SELECT TABLE_NAME " "FROM information_schema.tables " diff --git a/src/dbm/DbManager.h b/src/dbm/DbManager.h index a750a980..d53e27f4 100644 --- a/src/dbm/DbManager.h +++ b/src/dbm/DbManager.h @@ -47,6 +47,8 @@ class DbManager void setSchemaFile( const std::string& schemaFile ); void setInsertFile( const std::string& insertFile ); + void setForceMode( bool ); + private: std::string m_host; std::string m_database; @@ -58,6 +60,7 @@ class DbManager Mode m_mode; std::string m_iFile; std::string m_sFile; + bool m_force; }; diff --git a/src/dbm/main.cpp b/src/dbm/main.cpp index b2654593..7087fbf2 100644 --- a/src/dbm/main.cpp +++ b/src/dbm/main.cpp @@ -90,8 +90,7 @@ void printUsage() g_log.info( "\t --port ( default 3306 )" ); g_log.info( "\t --database " ); g_log.info( "\t --sfile ( default sql/schema/schema.sql )" ); - g_log.info( "\t --ifile ( default sql/schema/inserts.sql )" ); - + g_log.info( "\t --force ( skips user input / auto Yes )" ); } int main( int32_t argc, char* argv[] ) @@ -110,6 +109,8 @@ int main( int32_t argc, char* argv[] ) std::string sFile; std::string iFile; + bool force = false; + std::vector< std::string > args( argv + 1, argv + argc ); for( uint32_t i = 0; i + 1 < args.size(); i += 2 ) { @@ -132,7 +133,8 @@ int main( int32_t argc, char* argv[] ) sFile = val; else if( arg == "ifile" ) iFile = val; - + else if( arg == "force" ) + force = true; } if( host.empty() ) @@ -150,8 +152,9 @@ int main( int32_t argc, char* argv[] ) { dbm.setInsertFile( iFile ); dbm.setSchemaFile( sFile ); - } + if( force ) + dbm.setForceMode( true ); //initialize|check|update|clearchars|liquidate if( mode.find( "initialize" ) != std::string::npos ) {