[Commands] Cleanup #mysql Command. (#1837)

* [Commands] Cleanup #mysql Command.
- Cleanup messages and logic.

* Update mysql.cpp
This commit is contained in:
Kinglykrab 2021-11-27 20:32:21 -05:00 committed by GitHub
parent a6e5534b64
commit fd862d16bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 66 deletions

View File

@ -248,7 +248,7 @@ int command_init(void)
command_add("movechar", "[Character ID|Character Name] [Zone ID|Zone Short Name] - Move an offline character to the specified zone", AccountStatus::Guide, command_movechar) || command_add("movechar", "[Character ID|Character Name] [Zone ID|Zone Short Name] - Move an offline character to the specified zone", AccountStatus::Guide, command_movechar) ||
command_add("movement", "Various movement commands", AccountStatus::GMMgmt, command_movement) || command_add("movement", "Various movement commands", AccountStatus::GMMgmt, command_movement) ||
command_add("myskills", "- Show details about your current skill levels", AccountStatus::Player, command_myskills) || command_add("myskills", "- Show details about your current skill levels", AccountStatus::Player, command_myskills) ||
command_add("mysql", "Mysql CLI, see 'help' for options.", AccountStatus::GMImpossible, command_mysql) || command_add("mysql", "[Help|Query] [SQL Query] - Mysql CLI, see 'Help' for options.", AccountStatus::GMImpossible, command_mysql) ||
command_add("mystats", "- Show details about you or your pet", AccountStatus::Guide, command_mystats) || command_add("mystats", "- Show details about you or your pet", AccountStatus::Guide, command_mystats) ||
command_add("name", "[newname] - Rename your player target", AccountStatus::GMLeadAdmin, command_name) || command_add("name", "[newname] - Rename your player target", AccountStatus::GMLeadAdmin, command_name) ||
command_add("netstats", "- Gets the network stats for a stream.", AccountStatus::GMMgmt, command_netstats) || command_add("netstats", "- Gets the network stats for a stream.", AccountStatus::GMMgmt, command_netstats) ||

View File

@ -2,85 +2,83 @@
void command_mysql(Client *c, const Seperator *sep) void command_mysql(Client *c, const Seperator *sep)
{ {
if (!sep->arg[1][0] || !sep->arg[2][0]) { int arguments = sep->argnum;
c->Message(Chat::White, "Usage: #mysql query \"Query here\""); if (!arguments) {
c->Message(Chat::White, "Usage: #mysql [Help|Query] [SQL Query]");
return; return;
} }
if (strcasecmp(sep->arg[1], "help") == 0) { bool is_help = !strcasecmp(sep->arg[1], "help");
c->Message(Chat::White, "MYSQL In-Game CLI Interface:"); bool is_query = !strcasecmp(sep->arg[1], "query");
c->Message(Chat::White, "Example: #mysql query \"Query goes here quoted\" -s -h"); if (
c->Message(Chat::White, "To use 'like \"%%something%%\" replace the %% with #"); !is_help &&
c->Message(Chat::White, "Example: #mysql query \"select * from table where name like \"#something#\""); !is_query
c->Message(Chat::White, "-s - Spaces select entries apart"); ) {
c->Message(Chat::White, "-h - Colors every other select result"); c->Message(Chat::White, "Usage: #mysql [Help|Query] [SQL Query]");
return; return;
} }
if (strcasecmp(sep->arg[1], "query") == 0) { if (is_help) {
///Parse switches here c->Message(Chat::White, "Usage: #mysql query \"Query goes here quoted\"");
int argnum = 3; c->Message(Chat::White, "Note: To use 'LIKE \"%%something%%\" replace the %% with a #");
bool optionS = false; c->Message(Chat::White, "Example: #mysql query \"SELECT * FROM items WHERE `name` LIKE \"#Apple#\"");
bool optionH = false; return;
while (sep->arg[argnum] && strlen(sep->arg[argnum]) > 1) { } else if (is_query) {
switch (sep->arg[argnum][1]) { if (arguments < 2) {
case 's': c->Message(Chat::White, "Usage: #mysql query \"Query goes here quoted\"");
optionS = true; c->Message(Chat::White, "Note: To use 'LIKE \"%%something%%\" replace the %% with a #");
break; c->Message(Chat::White, "Example: #mysql query \"SELECT * FROM items WHERE `name` LIKE \"#Apple#\"");
case 'h': return;
optionH = true;
break;
default:
c->Message(Chat::Yellow, "%s, there is no option '%c'", c->GetName(), sep->arg[argnum][1]);
return;
}
++argnum;
} }
int highlightTextIndex = 0; std::string query = sep->arg[2];
std::string query(sep->arg[2]); find_replace(query, "#", "%");
//swap # for % so like queries can work
std::replace(query.begin(), query.end(), '#', '%');
auto results = database.QueryDatabase(query); auto results = database.QueryDatabase(query);
if (!results.Success()) { if (!results.Success()) {
return; return;
} }
//Using sep->arg[2] again, replace # with %% so it doesn't screw up when sent through vsnprintf in Message query = sep->arg[2];
query = sep->arg[2]; find_replace(query, "#", "%%");
int pos = query.find('#');
while (pos != std::string::npos) { c->Message(
query.erase(pos, 1); Chat::White,
query.insert(pos, "%%"); fmt::format(
pos = query.find('#'); "Running Query: '{}'",
query
).c_str()
);
std::vector<std::string> lines;
for (auto row : results) {
for (
int row_index = 0;
row_index < results.ColumnCount();
row_index++
) {
lines.push_back(
fmt::format(
"{} | {} ",
results.FieldName(row_index),
(
row[row_index] ?
(
strlen(row[row_index]) ?
row[row_index] :
"Empty String"
) :
"NULL"
)
)
);
}
} }
c->Message(Chat::Yellow, "---Running query: '%s'", query.c_str());
for (auto row = results.begin(); row != results.end(); ++row) { for (auto line : lines) {
std::stringstream lineText; c->Message(
std::vector<std::string> lineVec; Chat::White,
for (int i = 0; i < results.RowCount(); i++) { line.c_str()
//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") << "] ";
}
lineVec.push_back(lineText.str());
if (optionS) { //This provides spacing for the space switch
c->Message(Chat::White, " ");
}
if (optionH) { //This option will highlight every other row
highlightTextIndex = 1 - highlightTextIndex;
}
for (int lineNum = 0; lineNum < lineVec.size(); ++lineNum)
c->Message(highlightTextIndex, lineVec[lineNum].c_str());
} }
} }
} }