command_mysql converted to QueryDatabase

This commit is contained in:
Arthur Ice 2014-10-04 12:08:50 -07:00
parent bf6b9aaef8
commit f4203d0621

View File

@ -10838,7 +10838,9 @@ void command_mysql(Client *c, const Seperator *sep)
{ {
if(!sep->arg[1][0] || !sep->arg[2][0]) { if(!sep->arg[1][0] || !sep->arg[2][0]) {
c->Message(0, "Usage: #mysql query \"Query here\""); c->Message(0, "Usage: #mysql query \"Query here\"");
return;
} }
if ( strcasecmp( sep->arg[1], "help" ) == 0 ) { if ( strcasecmp( sep->arg[1], "help" ) == 0 ) {
c->Message(0, "MYSQL In-Game CLI Interface:"); c->Message(0, "MYSQL In-Game CLI Interface:");
c->Message(0, "Example: #mysql query \"Query goes here quoted\" -s -h"); c->Message(0, "Example: #mysql query \"Query goes here quoted\" -s -h");
@ -10846,79 +10848,68 @@ void command_mysql(Client *c, const Seperator *sep)
c->Message(0, "Example: #mysql query \"select * from table where name like \"#something#\""); c->Message(0, "Example: #mysql query \"select * from table where name like \"#something#\"");
c->Message(0, "-s - Spaces select entries apart"); c->Message(0, "-s - Spaces select entries apart");
c->Message(0, "-h - Colors every other select result"); c->Message(0, "-h - Colors every other select result");
return;
} }
if ( strcasecmp( sep->arg[1], "query" ) == 0 ) { if ( strcasecmp( sep->arg[1], "query" ) == 0 ) {
///Parse switches here ///Parse switches here
int argnum = 3; bool Options = false, Optionh = false; bool Fail = false; int argnum = 3;
bool optionS = false;
bool optionH = false;
while(sep->arg[argnum] && strlen(sep->arg[argnum]) > 1){ while(sep->arg[argnum] && strlen(sep->arg[argnum]) > 1){
switch(sep->arg[argnum][1]){ switch(sep->arg[argnum][1]){
case 's': Options = true; break; case 's': optionS = true; break;
case 'h': Optionh = true; break; case 'h': optionH = true; break;
default: c->Message(15, "%s, there is no option '%c'", c->GetName(), sep->arg[argnum][1]); Fail = true; default:
c->Message(15, "%s, there is no option '%c'", c->GetName(), sep->arg[argnum][1]);
return;
} }
++argnum; ++argnum;
} }
if(!Fail) { int highlightTextIndex = 0;
char errbuf[MYSQL_ERRMSG_SIZE]; std::string query(sep->arg[2]);
int HText = 0;
MYSQL_RES *result;
std::stringstream MsgText;
std::string QueryText(sep->arg[2]);
//swap # for % so like queries can work //swap # for % so like queries can work
std::replace(QueryText.begin(), QueryText.end(), '#', '%'); std::replace(query.begin(), query.end(), '#', '%');
auto results = database.QueryDatabase(query);
if (database.RunQuery(QueryText.c_str(), QueryText.length(), errbuf, &result)) { if (!results.Success()) {
//Using sep->arg[2] again, replace # with %% so it doesn't screw up when sent through vsnprintf in Message c->Message(0, "Invalid query: '%s', '%s'", sep->arg[2], results.ErrorMessage().c_str());
QueryText = sep->arg[2]; return;
int pos = QueryText.find('#');
while(pos != std::string::npos)
{
QueryText.erase(pos,1);
QueryText.insert(pos, "%%");
pos = QueryText.find('#');
} }
MsgText << "---Running query: '" << QueryText << "'"; //Using sep->arg[2] again, replace # with %% so it doesn't screw up when sent through vsnprintf in Message
c->Message (15, MsgText.str().c_str()); query = sep->arg[2];
MsgText.str(""); int pos = query.find('#');
while(pos != std::string::npos) {
query.erase(pos,1);
query.insert(pos, "%%");
pos = query.find('#');
}
c->Message(15, "---Running query: '%s'", query.c_str());
MYSQL_ROW row; for (auto row = results.begin(); row != results.end(); ++row) {
while ((row = mysql_fetch_row(result))) { std::stringstream lineText;
std::vector<std::string> lineVec;
MYSQL_FIELD *fields = mysql_fetch_fields(result); for(int i = 0; i < results.RowCount(); i++) {
unsigned int num_fields = mysql_num_fields(result);
std::stringstream LineText;
std::vector<std::string> LineVec;
for(int i = 0; i < num_fields; i++) {
//split lines that could overflow the buffer in Client::Message and get cut off //split lines that could overflow the buffer in Client::Message and get cut off
//This will crash MQ2 @ 4000 since their internal buffer is only 2048. //This will crash MQ2 @ 4000 since their internal buffer is only 2048.
//Reducing it to 2000 fixes that but splits more results from tables with a lot of columns. //Reducing it to 2000 fixes that but splits more results from tables with a lot of columns.
if(LineText.str().length() > 4000) { if(lineText.str().length() > 4000) {
LineVec.push_back(LineText.str()); lineVec.push_back(lineText.str());
LineText.str(""); lineText.str("");
} }
LineText << fields[i].name << ":" << "[" << (row[i] ? row[i] : "nullptr") << "] "; lineText << results.FieldName(i) << ":" << "[" << (row[i] ? row[i] : "nullptr") << "] ";
} }
LineVec.push_back(LineText.str());
if(Options) { //This provides spacing for the space switch lineVec.push_back(lineText.str());
if(optionS) //This provides spacing for the space switch
c->Message(0, " "); c->Message(0, " ");
} if(optionH) //This option will highlight every other row
if(Optionh) { //This option will highlight every other row highlightTextIndex = 1 - highlightTextIndex;
HText = 1 - HText;
} for(int lineNum = 0; lineNum < lineVec.size(); ++lineNum)
for(int lineNum = 0; lineNum < LineVec.size(); ++lineNum) c->Message(highlightTextIndex, lineVec[lineNum].c_str());
{
c->Message(HText, LineVec[lineNum].c_str());
}
}
}
else {
MsgText << "Invalid query: ' " << sep->arg[2] << " ', ' " << errbuf << " '";
c->Message(0, MsgText.str().c_str());
MsgText.str("");
}
} }
} }
} }