|
|
@ -1,5 +1,6 @@
|
|
|
|
// g++ mysqlclient_main.cpp src/Logger.cpp src/MysqlClient.cpp src/ArgParse.cpp -I./include -lmysqlclient
|
|
|
|
// gpp mysqlclient_main.cpp ../src/Logger.cpp ../src/MysqlClient.cpp ../src/ArgParse.cpp -I../include -lmysqlclient -o mysqlclient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ./mysqlclient -u USER -db MYSQL_DB -p PASS
|
|
|
|
|
|
|
|
|
|
|
|
#include "Logger.hpp"
|
|
|
|
#include "Logger.hpp"
|
|
|
|
#include "Common.hpp"
|
|
|
|
#include "Common.hpp"
|
|
|
@ -15,21 +16,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
void setUpArgs(ArgParse &argParse)
|
|
|
|
void setUpArgs(ArgParse &argParse)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
TRACE_STATIC;
|
|
|
|
|
|
|
|
|
|
|
|
argParse.addArgument("--host",
|
|
|
|
argParse.addArgument("--host",
|
|
|
|
"Hostname/IP",
|
|
|
|
"Hostname/IP",
|
|
|
|
ArgParse::STRING );
|
|
|
|
ArgParse::STRING );
|
|
|
|
argParse.addArgument("-u, --user",
|
|
|
|
argParse.addArgument("-u, --user",
|
|
|
|
"Username",
|
|
|
|
"Username",
|
|
|
|
ArgParse::STRING,
|
|
|
|
ArgParse::STRING,
|
|
|
|
ArgParse::REQUIRED );
|
|
|
|
ArgParse::REQUIRED, ArgParse::REQUIRED );
|
|
|
|
argParse.addArgument("-db, --database",
|
|
|
|
argParse.addArgument("-db, --database",
|
|
|
|
"Database",
|
|
|
|
"Database",
|
|
|
|
ArgParse::STRING,
|
|
|
|
ArgParse::STRING,
|
|
|
|
ArgParse::REQUIRED );
|
|
|
|
ArgParse::REQUIRED, ArgParse::REQUIRED );
|
|
|
|
argParse.addArgument("-p, --password",
|
|
|
|
argParse.addArgument("-p, --password",
|
|
|
|
"Password",
|
|
|
|
"Password",
|
|
|
|
ArgParse::STRING,
|
|
|
|
ArgParse::STRING,
|
|
|
|
ArgParse::REQUIRED );
|
|
|
|
ArgParse::REQUIRED, ArgParse::REQUIRED );
|
|
|
|
argParse.addArgument("-port",
|
|
|
|
argParse.addArgument("-port",
|
|
|
|
"Port",
|
|
|
|
"Port",
|
|
|
|
ArgParse::INT );
|
|
|
|
ArgParse::INT );
|
|
|
@ -39,6 +42,11 @@ void setUpArgs(ArgParse &argParse)
|
|
|
|
argParse.addArgument("-f, --client-flags",
|
|
|
|
argParse.addArgument("-f, --client-flags",
|
|
|
|
"Client flags",
|
|
|
|
"Client flags",
|
|
|
|
ArgParse::INT );
|
|
|
|
ArgParse::INT );
|
|
|
|
|
|
|
|
argParse.addArgument("-r, --max-reconnect",
|
|
|
|
|
|
|
|
"Maximum number of retries if connection is lost. "
|
|
|
|
|
|
|
|
"Default is 5.",
|
|
|
|
|
|
|
|
ArgParse::INT,
|
|
|
|
|
|
|
|
ArgParse::REQUIRED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -50,8 +58,11 @@ void getArgs( int argc, char* argv[],
|
|
|
|
std::string &pass,
|
|
|
|
std::string &pass,
|
|
|
|
std::string &unixsocket,
|
|
|
|
std::string &unixsocket,
|
|
|
|
int &port,
|
|
|
|
int &port,
|
|
|
|
int &clientflags )
|
|
|
|
int &clientflags,
|
|
|
|
|
|
|
|
int &retry )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
TRACE_STATIC;
|
|
|
|
|
|
|
|
|
|
|
|
argParse.parseArgs(argc, argv);
|
|
|
|
argParse.parseArgs(argc, argv);
|
|
|
|
|
|
|
|
|
|
|
|
argParse.argAsString("--host", host);
|
|
|
|
argParse.argAsString("--host", host);
|
|
|
@ -61,11 +72,14 @@ void getArgs( int argc, char* argv[],
|
|
|
|
argParse.argAsInt("-port", port);
|
|
|
|
argParse.argAsInt("-port", port);
|
|
|
|
argParse.argAsString("-s, --unix-socket", unixsocket);
|
|
|
|
argParse.argAsString("-s, --unix-socket", unixsocket);
|
|
|
|
argParse.argAsInt("-f, --client-flags", clientflags);
|
|
|
|
argParse.argAsInt("-f, --client-flags", clientflags);
|
|
|
|
|
|
|
|
argParse.argAsInt("-r, --max-reconnect", retry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void printResults(std::list<std::string> &results)
|
|
|
|
void printResults(std::list<std::string> &results)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
TRACE_STATIC;
|
|
|
|
|
|
|
|
|
|
|
|
LOG ( Logger::DEBUG, std::string("Got query result number of rows: ").
|
|
|
|
LOG ( Logger::DEBUG, std::string("Got query result number of rows: ").
|
|
|
|
append(TToStr(results.size())).c_str() );
|
|
|
|
append(TToStr(results.size())).c_str() );
|
|
|
|
|
|
|
|
|
|
|
@ -89,13 +103,13 @@ int main(int argc, char* argv[] )
|
|
|
|
setUpArgs(argParse);
|
|
|
|
setUpArgs(argParse);
|
|
|
|
|
|
|
|
|
|
|
|
std::string host, user, db, pass, unixsocket;
|
|
|
|
std::string host, user, db, pass, unixsocket;
|
|
|
|
int port, clientflags;
|
|
|
|
int port, clientflags, retry;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
getArgs( argc, argv,
|
|
|
|
getArgs( argc, argv,
|
|
|
|
argParse,
|
|
|
|
argParse,
|
|
|
|
host, user, db, pass, unixsocket,
|
|
|
|
host, user, db, pass, unixsocket,
|
|
|
|
port, clientflags );
|
|
|
|
port, clientflags, retry );
|
|
|
|
} catch (std::runtime_error e) {
|
|
|
|
} catch (std::runtime_error e) {
|
|
|
|
if ( argParse.foundArg("-h, --help") ) {
|
|
|
|
if ( argParse.foundArg("-h, --help") ) {
|
|
|
|
std::cout << argParse.usage() << std::endl;
|
|
|
|
std::cout << argParse.usage() << std::endl;
|
|
|
@ -121,17 +135,42 @@ int main(int argc, char* argv[] )
|
|
|
|
argParse.foundArg("-db, --database") ? db .c_str() : NULL,
|
|
|
|
argParse.foundArg("-db, --database") ? db .c_str() : NULL,
|
|
|
|
argParse.foundArg("-port") ? port : 0,
|
|
|
|
argParse.foundArg("-port") ? port : 0,
|
|
|
|
argParse.foundArg("-s, --unix-socket") ? unixsocket.c_str() : NULL,
|
|
|
|
argParse.foundArg("-s, --unix-socket") ? unixsocket.c_str() : NULL,
|
|
|
|
argParse.foundArg("-f, --client-flags") ? clientflags : 0 );
|
|
|
|
argParse.foundArg("-f, --client-flags") ? clientflags : 0,
|
|
|
|
|
|
|
|
argParse.foundArg("-r, --max-reconnect") ? retry : 5 );
|
|
|
|
|
|
|
|
|
|
|
|
std::list<std::string> results;
|
|
|
|
if ( !mysqlClient.connect() && !mysqlClient.reconnect() ) {
|
|
|
|
if ( !mysqlClient.querty("SELECT * FROM seats", results) ) {
|
|
|
|
finish_client_errs();
|
|
|
|
LOG ( Logger::ERR, "Could not execute query." );
|
|
|
|
Logger::destroy();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
printResults(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string queryMsg("SELECT * FROM seats");
|
|
|
|
|
|
|
|
MYSQL_RES *queryResult;
|
|
|
|
|
|
|
|
if ( !mysqlClient.querty(queryMsg.c_str(), queryMsg.length(), &queryResult) ) {
|
|
|
|
|
|
|
|
LOG( Logger::ERR, "Could not execute query." );
|
|
|
|
|
|
|
|
if ( !mysqlClient.reconnect() ) {
|
|
|
|
|
|
|
|
LOG( Logger::ERR, "Reconnect failed, exiting." );
|
|
|
|
|
|
|
|
finish_client_errs();
|
|
|
|
|
|
|
|
Logger::destroy();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOG( Logger::ERR, "Trying query again." );
|
|
|
|
|
|
|
|
if ( !mysqlClient.querty(queryMsg.c_str(), queryMsg.length(), &queryResult) ) {
|
|
|
|
|
|
|
|
LOG( Logger::ERR, "Query failed again, exiting." );
|
|
|
|
finish_client_errs();
|
|
|
|
finish_client_errs();
|
|
|
|
|
|
|
|
Logger::destroy();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::list<std::string> results;
|
|
|
|
|
|
|
|
MysqlClient::queryResultToStringList(queryResult, results);
|
|
|
|
|
|
|
|
printResults(results);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mysql_free_result(queryResult);
|
|
|
|
|
|
|
|
finish_client_errs();
|
|
|
|
Logger::destroy();
|
|
|
|
Logger::destroy();
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|