command_findnpctype converted to QueryDatabase

This commit is contained in:
Arthur Ice 2014-10-04 10:07:25 -07:00
parent ca2c2ccfac
commit 61cd48ff44

View File

@ -3096,67 +3096,47 @@ void command_peekinv(Client *c, const Seperator *sep)
void command_findnpctype(Client *c, const Seperator *sep) void command_findnpctype(Client *c, const Seperator *sep)
{ {
if(sep->arg[1][0] == 0) if(sep->arg[1][0] == 0) {
c->Message(0, "Usage: #findnpctype [search criteria]"); c->Message(0, "Usage: #findnpctype [search criteria]");
else return;
{ }
int id;
int count;
const int maxrows = 20;
char errbuf[MYSQL_ERRMSG_SIZE];
char *query;
MYSQL_RES *result;
MYSQL_ROW row;
query = new char[256]; std::string query;
// If id evaluates to 0, then search as if user entered a string. int id = atoi((const char *)sep->arg[1]);
if ((id = atoi((const char *)sep->arg[1])) == 0) if (id == 0) // If id evaluates to 0, then search as if user entered a string.
MakeAnyLenString(&query, query = StringFormat("SELECT id, name FROM npc_types WHERE name LIKE '%%%s%%'", sep->arg[1]);
"SELECT id,name" else // Otherwise, look for just that npc id.
" FROM npc_types WHERE name LIKE '%%%s%%'", query = StringFormat("SELECT id, name FROM npc_types WHERE id = %i", id);
sep->arg[1]);
// Otherwise, look for just that npc id. auto results = database.QueryDatabase(query);
else if (!results.Success()) {
MakeAnyLenString(&query, c->Message (0, "Error querying database.");
"SELECT id,name FROM npc_types WHERE id=%i", id); c->Message (0, query.c_str());
}
if (results.RowCount() == 0) // No matches found.
c->Message (0, "No matches found for %s.", sep->arg[1]);
// If query runs successfully. // If query runs successfully.
if (database.RunQuery(query, strlen(query), errbuf, &result)) int count = 0;
{ const int maxrows = 20;
count = 0;
// Process each row returned. // Process each row returned.
while((row = mysql_fetch_row(result))) for (auto row = results.begin(); row != results.end(); ++row) {
{
// Limit to returning maxrows rows. // Limit to returning maxrows rows.
if (++count > maxrows) if (++count > maxrows) {
{ c->Message (0, "%i npc types shown. Too many results.", maxrows);
c->Message (0,
"%i npc types shown. Too many results.", maxrows);
break; break;
} }
c->Message (0, " %s: %s", row[0], row[1]); c->Message (0, " %s: %s", row[0], row[1]);
} }
// If we did not hit the maxrows limit. // If we did not hit the maxrows limit.
if (count <= maxrows) if (count <= maxrows)
c->Message (0, "Query complete. %i rows shown.", count); c->Message (0, "Query complete. %i rows shown.", count);
// No matches found.
else if (count == 0)
c->Message (0, "No matches found for %s.", sep->arg[1]);
mysql_free_result(result);
}
// If query failed.
else
{
c->Message (0, "Error querying database.");
c->Message (0, query);
}
safe_delete_array(query);
}
} }
void command_findzone(Client *c, const Seperator *sep) void command_findzone(Client *c, const Seperator *sep)