Skip to content

Commit

Permalink
SQL-Admin-Manager: style clean-up (#974)
Browse files Browse the repository at this point in the history
* Switched to sizeof instead of hard-coding for loop size

* Switched back to pre-increment.

* Dynamically pad variables
  • Loading branch information
B3none authored and KyleSanderson committed Apr 6, 2019
1 parent 318b36c commit 8f05274
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions plugins/sql-admin-manager.sp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@
* Version: $Id$ * Version: $Id$
*/ */


/* We like semicolons */
#pragma semicolon 1

#include <sourcemod> #include <sourcemod>


/* We like semicolons */
#pragma semicolon 1
#pragma newdecls required #pragma newdecls required


#define CURRENT_SCHEMA_VERSION 1409 #define CURRENT_SCHEMA_VERSION 1409
#define SCHEMA_UPGRADE_1 1409 #define SCHEMA_UPGRADE_1 1409


int current_version[4] = {1, 0, 0, CURRENT_SCHEMA_VERSION}; int current_version[] = {1, 0, 0, CURRENT_SCHEMA_VERSION};


public Plugin myinfo = public Plugin myinfo =
{ {
Expand Down Expand Up @@ -88,7 +87,7 @@ Database Connect()


void CreateMySQL(int client, Database db) void CreateMySQL(int client, Database db)
{ {
char queries[7][] = char queries[][] =
{ {
"CREATE TABLE sm_admins (id int(10) unsigned NOT NULL auto_increment, authtype enum('steam','name','ip') NOT NULL, identity varchar(65) NOT NULL, password varchar(65), flags varchar(30) NOT NULL, name varchar(65) NOT NULL, immunity int(10) unsigned NOT NULL, PRIMARY KEY (id))", "CREATE TABLE sm_admins (id int(10) unsigned NOT NULL auto_increment, authtype enum('steam','name','ip') NOT NULL, identity varchar(65) NOT NULL, password varchar(65), flags varchar(30) NOT NULL, name varchar(65) NOT NULL, immunity int(10) unsigned NOT NULL, PRIMARY KEY (id))",
"CREATE TABLE sm_groups (id int(10) unsigned NOT NULL auto_increment, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level int(1) unsigned NOT NULL, PRIMARY KEY (id))", "CREATE TABLE sm_groups (id int(10) unsigned NOT NULL auto_increment, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level int(1) unsigned NOT NULL, PRIMARY KEY (id))",
Expand All @@ -99,7 +98,7 @@ void CreateMySQL(int client, Database db)
"CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))" "CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))"
}; };


for (int i = 0; i < 7; i++) for (int i = 0; i < sizeof(queries); i++)
{ {
if (!DoQuery(client, db, queries[i])) if (!DoQuery(client, db, queries[i]))
{ {
Expand All @@ -124,7 +123,7 @@ void CreateMySQL(int client, Database db)


void CreateSQLite(int client, Database db) void CreateSQLite(int client, Database db)
{ {
char queries[7][] = char queries[][] =
{ {
"CREATE TABLE sm_admins (id INTEGER PRIMARY KEY AUTOINCREMENT, authtype varchar(16) NOT NULL CHECK(authtype IN ('steam', 'ip', 'name')), identity varchar(65) NOT NULL, password varchar(65), flags varchar(30) NOT NULL, name varchar(65) NOT NULL, immunity INTEGER NOT NULL)", "CREATE TABLE sm_admins (id INTEGER PRIMARY KEY AUTOINCREMENT, authtype varchar(16) NOT NULL CHECK(authtype IN ('steam', 'ip', 'name')), identity varchar(65) NOT NULL, password varchar(65), flags varchar(30) NOT NULL, name varchar(65) NOT NULL, immunity INTEGER NOT NULL)",
"CREATE TABLE sm_groups (id INTEGER PRIMARY KEY AUTOINCREMENT, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level INTEGER NOT NULL)", "CREATE TABLE sm_groups (id INTEGER PRIMARY KEY AUTOINCREMENT, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level INTEGER NOT NULL)",
Expand All @@ -135,7 +134,7 @@ void CreateSQLite(int client, Database db)
"CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))" "CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))"
}; };


for (int i = 0; i < 7; i++) for (int i = 0; i < sizeof(queries); i++)
{ {
if (!DoQuery(client, db, queries[i])) if (!DoQuery(client, db, queries[i]))
{ {
Expand Down Expand Up @@ -203,7 +202,7 @@ bool GetUpdateVersion(int client, Database db, int versions[4])
char version_numbers[4][12]; char version_numbers[4][12];
if (ExplodeString(version_string, ".", version_numbers, 4, 12) == 4) if (ExplodeString(version_string, ".", version_numbers, 4, 12) == 4)
{ {
for (int i = 0; i < 4; i++) for (int i = 0; i < sizeof(version_numbers); i++)
{ {
versions[i] = StringToInt(version_numbers[i]); versions[i] = StringToInt(version_numbers[i]);
} }
Expand Down Expand Up @@ -257,7 +256,7 @@ void UpdateSQLite(int client, Database db)
*/ */
if (versions[3] < SCHEMA_UPGRADE_1) if (versions[3] < SCHEMA_UPGRADE_1)
{ {
char queries[8][] = char queries[][] =
{ {
"ALTER TABLE sm_admins ADD immunity INTEGER DEFAULT 0 NOT NULL", "ALTER TABLE sm_admins ADD immunity INTEGER DEFAULT 0 NOT NULL",
"CREATE TABLE _sm_groups_temp (id INTEGER PRIMARY KEY AUTOINCREMENT, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level INTEGER DEFAULT 0 NOT NULL)", "CREATE TABLE _sm_groups_temp (id INTEGER PRIMARY KEY AUTOINCREMENT, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level INTEGER DEFAULT 0 NOT NULL)",
Expand All @@ -269,7 +268,7 @@ void UpdateSQLite(int client, Database db)
"CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))" "CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))"
}; };


for (int i = 0; i < 8; i++) for (int i = 0; i < sizeof(queries); i++)
{ {
if (!DoQuery(client, db, queries[i])) if (!DoQuery(client, db, queries[i]))
{ {
Expand Down Expand Up @@ -329,7 +328,7 @@ void UpdateMySQL(int client, Database db)
*/ */
if (versions[3] < SCHEMA_UPGRADE_1) if (versions[3] < SCHEMA_UPGRADE_1)
{ {
char queries[6][] = char queries[][] =
{ {
"CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))", "CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))",
"ALTER TABLE sm_admins ADD immunity INT UNSIGNED NOT NULL", "ALTER TABLE sm_admins ADD immunity INT UNSIGNED NOT NULL",
Expand All @@ -339,7 +338,7 @@ void UpdateMySQL(int client, Database db)
"ALTER TABLE sm_groups DROP immunity" "ALTER TABLE sm_groups DROP immunity"
}; };


for (int i = 0; i < 6; i++) for (int i = 0; i < sizeof(queries); i++)
{ {
if (!DoQuery(client, db, queries[i])) if (!DoQuery(client, db, queries[i]))
{ {
Expand Down Expand Up @@ -482,7 +481,7 @@ public Action Command_SetAdminGroups(int client, int args)


char name[80]; char name[80];
int inherit_order = 0; int inherit_order = 0;
for (int i=3; i<=args; i++) for (int i = 3; i <= args; i++)
{ {
GetCmdArg(i, name, sizeof(name)); GetCmdArg(i, name, sizeof(name));


Expand Down Expand Up @@ -872,20 +871,20 @@ stock bool DoQuery(int client, Database db, const char[] query)


stock Action DoError(int client, Database db, const char[] query, const char[] msg) stock Action DoError(int client, Database db, const char[] query, const char[] msg)
{ {
char error[255]; char error[255];
SQL_GetError(db, error, sizeof(error)); SQL_GetError(db, error, sizeof(error));
LogError("%s: %s", msg, error); LogError("%s: %s", msg, error);
LogError("Query dump: %s", query); LogError("Query dump: %s", query);
delete db; delete db;
ReplyToCommand(client, "[SM] %t", "Failed to query database"); ReplyToCommand(client, "[SM] %t", "Failed to query database");
return Plugin_Handled; return Plugin_Handled;
} }


stock Action DoStmtError(int client, Database db, const char[] query, const char[] error, const char[] msg) stock Action DoStmtError(int client, Database db, const char[] query, const char[] error, const char[] msg)
{ {
LogError("%s: %s", msg, error); LogError("%s: %s", msg, error);
LogError("Query dump: %s", query); LogError("Query dump: %s", query);
delete db; delete db;
ReplyToCommand(client, "[SM] %t", "Failed to query database"); ReplyToCommand(client, "[SM] %t", "Failed to query database");
return Plugin_Handled; return Plugin_Handled;
} }

0 comments on commit 8f05274

Please sign in to comment.