sql fatures

This commit is contained in:
Walter Julius Hennecke 2011-12-16 21:41:27 +01:00
parent bc33c63ed6
commit 4bc8f3f4d1
3 changed files with 88 additions and 16 deletions

View file

@ -119,7 +119,7 @@ G_Sql_UserDB_Del
*/
qboolean G_Sql_UserDB_Del(const char *uName) {
sqlite3_stmt *stmt;
int res, cols, i, id = -1;
int res, i, id = -1;
res = sqlite3_prepare_v2(user_db, SQL_USER_GETUID(uName), -1, &stmt, 0);
if(res) {
@ -127,19 +127,16 @@ qboolean G_Sql_UserDB_Del(const char *uName) {
return qfalse;
}
cols = sqlite3_column_count(stmt);
for(i = 0; i < cols; i++) {
res = sqlite3_step(stmt);
if(res == SQLITE_ROW) {
id = sqlite3_column_int(stmt, 0);
} else if(res == SQLITE_DONE) {
break;
} else {
G_Printf(S_COLOR_RED "SQL ERROR: An error occured getting results from a row\n");
return qfalse;
}
}
res = sqlite3_step(stmt);
if(res == SQLITE_ROW) {
id = sqlite3_column_int(stmt, 0);
} else if(res == SQLITE_DONE) {
G_Printf("SQL: User \'%s\' not found\n", uName);
return qfalse;
} else {
G_Printf(S_COLOR_RED "SQL ERROR: An error occured getting results from a row\n");
return qfalse;
}
sqlite3_free(stmt);
if(id == -1) {
@ -179,6 +176,77 @@ G_Sql_UserAdd
===============
*/
qboolean Do_Sql_UserAdd(const char *uName, const char *password) {
sqlite3_stmt *stmt;
int res, id;
char *hashedpw;
res = sqlite3_prepare_v2(user_db, SQL_USER_GETUID(uName), -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
res = sqlite3_step(stmt);
if(res == SQLITE_ROW) {
G_Printf("SQL: There already exists a user with username \'%s\'\n", uName);
G_Printf("SQL: If you lost your password please contact an admin with access to the database.\n");
sqlite3_free(stmt);
return qfalse;
}
sqlite3_free(stmt);
res = sqlite3_prepare_v2(user_db, SQL_BEGIN_TRANSACTION, -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
sqlite3_free(stmt);
hashedpw = G_Sql_Md5((const char*)password);
res = sqlite3_prepare_v2(user_db, SQL_USER_ADD(uName, hashedpw), -1, &stmt, 0);
free(hashedpw);
if(res) {
G_Printf("SQL ERROR: User add query failed\n");
return qfalse;
}
res = sqlite3_prepare_v2(user_db, SQL_USER_GETUID(uName), -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
res = sqlite3_step(stmt);
if(res == SQLITE_ROW) {
id = sqlite3_column_int(stmt, 0);
} else {
G_Printf("SQL: There already exists a user with username \'%s\'\n", uName);
G_Printf("SQL: If you lost your password please contact an admin with access to the database.\n");
sqlite3_free(stmt);
return qfalse;
}
sqlite3_free(stmt);
res = sqlite3_prepare_v2(user_db, SQL_USER_ADD_RIGHTS(id), -1, &stmt, 0);
if(res) {
G_Printf("SQL ERROR: Add user rights query failed\n");
res = sqlite3_prepare_v2(user_db, SQL_ROLLBACK_TRANSACTION, -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
return qfalse;
}
sqlite3_free(stmt);
res = sqlite3_prepare_v2(user_db, SQL_COMMIT_TRANSACTION, -1, &stmt, 0);
if(res) {
G_Printf(S_COLOR_RED "SQL ERROR: Query failed\n");
return qfalse;
}
sqlite3_free(stmt);
return qfalse;
}

View file

@ -4,12 +4,14 @@
#define MAX_SQL_RESULT 4096
#define SQL_ENABLE_FOREIGN_KEY_CONSTRAINTS "PRAGMA foreign_keys = ON;"
#define SQL_BEGIN_TRANSACTION "BEGIN TRANSACTION"
#define SQL_ROLLBACK_TRANSACTION "ROLLBACK TRANSACTION"
#define SQL_COMMIT_TRANSACTION "COMMIT TRANSACTION"
#define SQL_USER_GETUID(UNAME) va("SELECT id FROM rpgx_users WHERE username = %s", UNAME)
#define SQL_USER_CREATEUSERTABLE "CREATE TABLE IF NOT EXISTS rpgx_users ( \
id INTEGER PRIMARY KEY AUTOINCREMENT, \
username TEXT NOT NULL , \
password TEXT NOT NULL, \
mail TEXT \
password TEXT NOT NULL \
)"
#define SQL_USER_CREATERIGHTSTABLE "CREATE TABLE IF NOT EXISTS rpgx_userRights ( \
id INT NOT NULL PRIMARY KEY, \
@ -19,6 +21,8 @@
)"
#define SQL_USER_DELETE(UNAME) va("DELETE FROM rpgx_users WHERE username = %s", UNAME)
#define SQL_USER_DELTE_RIGHTS(ID) va("DELETE FROM rpgx_userRights WHERE id = %d", ID)
#define SQL_USER_ADD(USERNAME,PASSWORD) va("INSERT INTO rpgx_users VALUES(NULL,%s,%s)", USERNAME, PASSWORD)
#define SQL_USER_ADD_RIGHTS(ID) va("INSERT INTO rpgx_userRights VALUES(NULL, 0, 0, %d)", ID)
typedef enum {
SQLF_GIVE = 1,

Binary file not shown.