Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- ****** iDataBase Master Include by FlatMaN**************************************
- *******Make the Database editing Easier*****************************************
- ********************************************************************************/
- #if defined DB_MASTER_INCLUDED
- #endinput
- #endif
- #define DB_MASTER_INCLUDED
- enum iDB_Types {
- iDB_TYPE_STRING,
- iDB_TYPE_INTEGER,
- iDB_TYPE_FLOAT };
- new DB:iDB_DataBase,
- DBResult:iDB_Result,
- iDB_String[128],
- bool:iDB_opened = false;
- #define iDB_Check iDB_opened
- stock iDB_Open(szName[]) {
- if(iDB_Check) return print("DB_MASTER ERROR: One database already opened. Close it first!");
- iDB_DataBase = db_open(szName);
- iDB_opened = true;
- db_query(iDB_DataBase, "PRAGMA synchronous = 0");
- return 1;
- }
- stock iDB_Close() {
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
- iDB_opened = false;
- db_close(iDB_DataBase);
- return 1;
- }
- stock iDB_Create(szTable[]) {
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
- format(iDB_String, sizeof(iDB_String), "CREATE TABLE %s (id INTEGER PRIMARY KEY, name TEXT)", szTable);
- db_query(iDB_DataBase, iDB_String);
- return 1;
- }
- stock iDB_Verify(szTable[], szColumn[], iDB_Type) {
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
- switch (iDB_Type) {
- case iDB_TYPE_INTEGER: format(iDB_String, sizeof(iDB_String), "ALTER TABLE `%s` ADD COLUMN `%s` INTEGER DEFAULT(%d)", szTable, szColumn, 0);
- case iDB_TYPE_FLOAT: format(iDB_String, sizeof(iDB_String), "ALTER TABLE `%s` ADD COLUMN `%s` REAL DEFAULT(%f)", szTable, szColumn, 0.0);
- case iDB_TYPE_STRING: format(iDB_String, sizeof(iDB_String), "ALTER TABLE `%s` ADD COLUMN `%s` TEXT DEFAULT('%s')", szTable, szColumn, " ");
- default: return printf("DB_MASTER ERROR: Column %s in Table %s have bad TYPE", szColumn, szTable);
- }
- db_free_result(db_query(iDB_DataBase, iDB_String));
- return 1;
- }
- stock bool:iDB_RowExist(szTable[], iID) {
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!"), false;
- format(iDB_String, sizeof(iDB_String), "SELECT `name` FROM `%s` WHERE `id`='%d' COLLATE NOCASE", szTable, iID);
- iDB_Result = db_query(iDB_DataBase, iDB_String);
- if(iDB_Result && db_num_rows(iDB_Result) != 0) return true;
- return false;
- }
- stock iDB_DelRow(szTable[], szName[]) {
- new szID = iDB_getnameid(szTable, szName);
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
- if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable);
- format(iDB_String, sizeof(iDB_String), "DELETE FROM `%s` WHERE `id`=%d COLLATE NOCASE", szTable, szID);
- db_query(iDB_DataBase, iDB_String);
- return 1;
- }
- stock iDB_RegRow(szTable[], szInput[]) {
- new szID = iDB_getnameid(szTable, szInput);
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
- if(iDB_RowExist(szTable, szID)) return printf("DB_MASTER NOTICE: row '%s' in '%s' already exist!", szInput, szTable);
- format(iDB_String, sizeof(iDB_String), "INSERT INTO %s (name) VALUES('%s')", szTable, szInput);
- iDB_Result = db_query(iDB_DataBase, iDB_String);
- if(iDB_Result) db_free_result(iDB_Result);
- return 1;
- }
- stock iDB_SetInt(szTable[], szName[], szColumn[], iInput) {
- new szID = iDB_getnameid(szTable, szName);
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
- if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable);
- format(iDB_String, sizeof(iDB_String), "UPDATE `%s` SET `%s`=%d WHERE `id`=%d", szTable, szColumn, iInput, szID);
- db_query(iDB_DataBase, iDB_String);
- return 1;
- }
- stock iDB_SetFloat(szTable[], szName[], szColumn[], Float:fInput) {
- new szID = iDB_getnameid(szTable, szName);
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
- if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable);
- format(iDB_String, sizeof(iDB_String), "UPDATE `%s` SET `%s`=%f WHERE `id`=%d", szTable, szColumn, fInput, szID);
- db_query(iDB_DataBase, iDB_String);
- return 1;
- }
- stock iDB_SetString(szTable[], szName[], szColumn[], szInput[]) {
- new szID = iDB_getnameid(szTable, szName);
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
- if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable);
- format(iDB_String, sizeof(iDB_String), "UPDATE `%s` SET `%s`='%s' WHERE `id`=%d", szTable, szColumn, szInput, szID);
- db_query(iDB_DataBase, iDB_String);
- return 1;
- }
- stock Float:iDB_GetFloat(szTable[], szName[], szColumn[]) {
- new Float:iFloatEntry,
- szID = iDB_getnameid(szTable, szName);
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!"), iFloatEntry;
- if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable), iFloatEntry;
- format(iDB_String, sizeof(iDB_String), "SELECT `%s` FROM `%s` WHERE `id`=%d", szColumn, szTable, szID);
- iDB_Result = db_query(iDB_DataBase, iDB_String);
- if(iDB_Result) {
- if(db_num_rows(iDB_Result) == 1) db_get_field(iDB_Result, 0, iDB_String, sizeof(iDB_String) - 1);
- else iDB_String[0] = EOS;
- db_free_result(iDB_Result);
- if(iDB_String[0]) return floatstr(iDB_String);
- }
- return iFloatEntry;
- }
- stock iDB_GetInt(szTable[], szName[], szColumn[]) {
- new iIntEntry,
- szID = iDB_getnameid(szTable, szName);
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!"), iIntEntry;
- if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable), iIntEntry;
- format(iDB_String, sizeof(iDB_String), "SELECT `%s` FROM `%s` WHERE `id`=%d", szColumn, szTable, szID);
- iDB_Result = db_query(iDB_DataBase, iDB_String);
- if(iDB_Result) {
- if(db_num_rows(iDB_Result) == 1) db_get_field(iDB_Result, 0, iDB_String, sizeof(iDB_String) - 1);
- else iDB_String[0] = EOS;
- db_free_result(iDB_Result);
- if(iDB_String[0]) return strval(iDB_String);
- }
- return iIntEntry;
- }
- stock iDB_GetString(szTable[], szName[], szColumn[], szOutput[], iSize = sizeof(szOutput)) {
- new szID = iDB_getnameid(szTable, szName);
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
- if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable);
- format(iDB_String, sizeof(iDB_String), "SELECT `%s` FROM `%s` WHERE `id`=%d", szColumn, szTable, szID);
- iDB_Result = db_query(iDB_DataBase, iDB_String);
- if(iDB_Result) {
- if(db_num_rows(iDB_Result) == 1) db_get_field(iDB_Result, 0, szOutput, iSize-1);
- else szOutput[0] = EOS;
- db_free_result(iDB_Result);
- }
- return 1;
- }
- stock iDB_getnameid(szTable[], szName[]) {
- if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
- format(iDB_String, sizeof(iDB_String), "SELECT `id` FROM `%s` WHERE `name`='%s' COLLATE NOCASE", szTable, szName);
- iDB_Result = db_query(iDB_DataBase, iDB_String);
- if(iDB_Result) {
- new id = -1;
- if(db_num_rows(iDB_Result) != 0) {
- db_get_field(iDB_Result, 0, iDB_String, sizeof(iDB_String));
- id = strval(iDB_String);
- }
- db_free_result(iDB_Result);
- return id;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement