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,81 +10848,70 @@ 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; //swap # for % so like queries can work
MYSQL_RES *result; std::replace(query.begin(), query.end(), '#', '%');
std::stringstream MsgText; auto results = database.QueryDatabase(query);
std::string QueryText(sep->arg[2]); if (!results.Success()) {
//swap # for % so like queries can work c->Message(0, "Invalid query: '%s', '%s'", sep->arg[2], results.ErrorMessage().c_str());
std::replace(QueryText.begin(), QueryText.end(), '#', '%'); return;
}
if (database.RunQuery(QueryText.c_str(), QueryText.length(), errbuf, &result)) { //Using sep->arg[2] again, replace # with %% so it doesn't screw up when sent through vsnprintf in Message
//Using sep->arg[2] again, replace # with %% so it doesn't screw up when sent through vsnprintf in Message query = sep->arg[2];
QueryText = sep->arg[2]; int pos = query.find('#');
int pos = QueryText.find('#'); while(pos != std::string::npos) {
while(pos != std::string::npos) query.erase(pos,1);
{ query.insert(pos, "%%");
QueryText.erase(pos,1); pos = query.find('#');
QueryText.insert(pos, "%%"); }
pos = QueryText.find('#'); c->Message(15, "---Running query: '%s'", query.c_str());
}
MsgText << "---Running query: '" << QueryText << "'"; for (auto row = results.begin(); row != results.end(); ++row) {
c->Message (15, MsgText.str().c_str()); std::stringstream lineText;
MsgText.str(""); std::vector<std::string> lineVec;
for(int i = 0; i < results.RowCount(); i++) {
//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.
//Reducing it to 2000 fixes that but splits more results from tables with a lot of columns.
if(lineText.str().length() > 4000) {
lineVec.push_back(lineText.str());
lineText.str("");
}
lineText << results.FieldName(i) << ":" << "[" << (row[i] ? row[i] : "nullptr") << "] ";
}
MYSQL_ROW row; lineVec.push_back(lineText.str());
while ((row = mysql_fetch_row(result))) {
MYSQL_FIELD *fields = mysql_fetch_fields(result); if(optionS) //This provides spacing for the space switch
unsigned int num_fields = mysql_num_fields(result); c->Message(0, " ");
std::stringstream LineText; if(optionH) //This option will highlight every other row
std::vector<std::string> LineVec; highlightTextIndex = 1 - highlightTextIndex;
for(int i = 0; i < num_fields; i++) {
//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.
//Reducing it to 2000 fixes that but splits more results from tables with a lot of columns.
if(LineText.str().length() > 4000) {
LineVec.push_back(LineText.str());
LineText.str("");
}
LineText << fields[i].name << ":" << "[" << (row[i] ? row[i] : "nullptr") << "] ";
}
LineVec.push_back(LineText.str());
if(Options) { //This provides spacing for the space switch for(int lineNum = 0; lineNum < lineVec.size(); ++lineNum)
c->Message(0, " "); c->Message(highlightTextIndex, lineVec[lineNum].c_str());
} }
if(Optionh) { //This option will highlight every other row }
HText = 1 - HText;
}
for(int lineNum = 0; lineNum < LineVec.size(); ++lineNum)
{
c->Message(HText, LineVec[lineNum].c_str());
}
}
}
else {
MsgText << "Invalid query: ' " << sep->arg[2] << " ', ' " << errbuf << " '";
c->Message(0, MsgText.str().c_str());
MsgText.str("");
}
}
}
} }
void command_xtargets(Client *c, const Seperator *sep) void command_xtargets(Client *c, const Seperator *sep)