Advertisement
flatman

Untitled

Jul 10th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.33 KB | None | 0 0
  1. /*******************************************************************************
  2. ****** iDataBase Master Include by FlatMaN**************************************
  3. *******Make the Database editing Easier*****************************************
  4. ********************************************************************************/
  5.  
  6. #if defined DB_MASTER_INCLUDED
  7.     #endinput
  8. #endif
  9.  
  10. #define DB_MASTER_INCLUDED
  11.  
  12. enum iDB_Types {
  13.     iDB_TYPE_STRING,
  14.     iDB_TYPE_INTEGER,
  15.     iDB_TYPE_FLOAT };
  16.  
  17. new DB:iDB_DataBase,
  18.     DBResult:iDB_Result,
  19.     iDB_String[128],
  20.     bool:iDB_opened = false;
  21.    
  22. #define iDB_Check   iDB_opened
  23.  
  24. stock iDB_Open(szName[]) {
  25.     if(iDB_Check) return print("DB_MASTER ERROR: One database already opened. Close it first!");
  26.     iDB_DataBase = db_open(szName);
  27.     iDB_opened = true;
  28.     db_query(iDB_DataBase, "PRAGMA synchronous = 0");
  29.     return 1;
  30. }
  31. stock iDB_Close() {
  32.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
  33.     iDB_opened = false;
  34.     db_close(iDB_DataBase);
  35.     return 1;
  36. }
  37. stock iDB_Create(szTable[]) {
  38.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
  39.     format(iDB_String, sizeof(iDB_String), "CREATE TABLE %s (id INTEGER PRIMARY KEY, name TEXT)", szTable);
  40.     db_query(iDB_DataBase, iDB_String);
  41.     return 1;
  42. }
  43. stock iDB_Verify(szTable[], szColumn[], iDB_Type) {
  44.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
  45.     switch (iDB_Type) {
  46.         case iDB_TYPE_INTEGER: format(iDB_String, sizeof(iDB_String), "ALTER TABLE `%s` ADD COLUMN `%s` INTEGER DEFAULT(%d)", szTable, szColumn, 0);
  47.         case iDB_TYPE_FLOAT: format(iDB_String, sizeof(iDB_String), "ALTER TABLE `%s` ADD COLUMN `%s` REAL DEFAULT(%f)", szTable, szColumn, 0.0);
  48.         case iDB_TYPE_STRING: format(iDB_String, sizeof(iDB_String), "ALTER TABLE `%s` ADD COLUMN `%s` TEXT DEFAULT('%s')", szTable, szColumn, " ");
  49.         default: return printf("DB_MASTER ERROR: Column %s in Table %s have bad TYPE", szColumn, szTable);
  50.     }
  51.     db_free_result(db_query(iDB_DataBase, iDB_String));
  52.     return 1;
  53. }
  54. stock bool:iDB_RowExist(szTable[], iID) {
  55.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!"), false;
  56.     format(iDB_String, sizeof(iDB_String), "SELECT `name` FROM `%s` WHERE `id`='%d' COLLATE NOCASE", szTable, iID);
  57.     iDB_Result = db_query(iDB_DataBase, iDB_String);
  58.     if(iDB_Result && db_num_rows(iDB_Result) != 0) return true;
  59.     return false;
  60. }
  61. stock iDB_DelRow(szTable[], szName[]) {
  62.     new szID = iDB_getnameid(szTable, szName);
  63.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
  64.     if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable);
  65.     format(iDB_String, sizeof(iDB_String), "DELETE FROM `%s` WHERE `id`=%d COLLATE NOCASE", szTable, szID);
  66.     db_query(iDB_DataBase, iDB_String);
  67.     return 1;
  68. }
  69. stock iDB_RegRow(szTable[], szInput[]) {
  70.     new szID = iDB_getnameid(szTable, szInput);
  71.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
  72.     if(iDB_RowExist(szTable, szID)) return printf("DB_MASTER NOTICE: row '%s' in '%s' already exist!", szInput, szTable);
  73.     format(iDB_String, sizeof(iDB_String), "INSERT INTO %s (name) VALUES('%s')", szTable, szInput);
  74.     iDB_Result = db_query(iDB_DataBase, iDB_String);
  75.     if(iDB_Result) db_free_result(iDB_Result);
  76.     return 1;
  77. }
  78. stock iDB_SetInt(szTable[], szName[], szColumn[], iInput) {
  79.     new szID = iDB_getnameid(szTable, szName);
  80.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
  81.     if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable);
  82.     format(iDB_String, sizeof(iDB_String), "UPDATE `%s` SET `%s`=%d WHERE `id`=%d", szTable, szColumn, iInput, szID);
  83.     db_query(iDB_DataBase, iDB_String);
  84.     return 1;
  85. }
  86. stock iDB_SetFloat(szTable[], szName[], szColumn[], Float:fInput) {
  87.     new szID = iDB_getnameid(szTable, szName);
  88.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
  89.     if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable);
  90.     format(iDB_String, sizeof(iDB_String), "UPDATE `%s` SET `%s`=%f WHERE `id`=%d", szTable, szColumn, fInput, szID);
  91.     db_query(iDB_DataBase, iDB_String);
  92.     return 1;
  93. }
  94. stock iDB_SetString(szTable[], szName[], szColumn[], szInput[]) {
  95.     new szID = iDB_getnameid(szTable, szName);
  96.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
  97.     if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable);
  98.     format(iDB_String, sizeof(iDB_String), "UPDATE `%s` SET `%s`='%s' WHERE `id`=%d", szTable, szColumn, szInput, szID);
  99.     db_query(iDB_DataBase, iDB_String);
  100.     return 1;
  101. }
  102. stock Float:iDB_GetFloat(szTable[], szName[], szColumn[]) {
  103.     new Float:iFloatEntry,
  104.         szID = iDB_getnameid(szTable, szName);
  105.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!"), iFloatEntry;
  106.     if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable), iFloatEntry;
  107.     format(iDB_String, sizeof(iDB_String), "SELECT `%s` FROM `%s` WHERE `id`=%d", szColumn, szTable, szID);
  108.     iDB_Result = db_query(iDB_DataBase, iDB_String);
  109.     if(iDB_Result) {
  110.         if(db_num_rows(iDB_Result) == 1) db_get_field(iDB_Result, 0, iDB_String, sizeof(iDB_String) - 1);
  111.         else iDB_String[0] = EOS;
  112.         db_free_result(iDB_Result);
  113.         if(iDB_String[0]) return floatstr(iDB_String);
  114.     }
  115.     return iFloatEntry;
  116. }
  117. stock iDB_GetInt(szTable[], szName[], szColumn[]) {
  118.     new iIntEntry,
  119.         szID = iDB_getnameid(szTable, szName);
  120.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!"), iIntEntry;
  121.     if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable), iIntEntry;
  122.     format(iDB_String, sizeof(iDB_String), "SELECT `%s` FROM `%s` WHERE `id`=%d", szColumn, szTable, szID);
  123.     iDB_Result = db_query(iDB_DataBase, iDB_String);
  124.     if(iDB_Result) {
  125.         if(db_num_rows(iDB_Result) == 1) db_get_field(iDB_Result, 0, iDB_String, sizeof(iDB_String) - 1);
  126.         else iDB_String[0] = EOS;
  127.         db_free_result(iDB_Result);
  128.         if(iDB_String[0]) return strval(iDB_String);
  129.     }
  130.     return iIntEntry;
  131. }
  132. stock iDB_GetString(szTable[], szName[], szColumn[], szOutput[], iSize = sizeof(szOutput)) {
  133.     new szID = iDB_getnameid(szTable, szName);
  134.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
  135.     if(!iDB_RowExist(szTable, szID)) return printf("DB_MASTER WARNING: row '%s' in '%s' does not exist!", szName, szTable);
  136.     format(iDB_String, sizeof(iDB_String), "SELECT `%s` FROM `%s` WHERE `id`=%d", szColumn, szTable, szID);
  137.     iDB_Result = db_query(iDB_DataBase, iDB_String);
  138.     if(iDB_Result) {
  139.         if(db_num_rows(iDB_Result) == 1) db_get_field(iDB_Result, 0, szOutput, iSize-1);
  140.         else szOutput[0] = EOS;
  141.         db_free_result(iDB_Result);
  142.     }
  143.     return 1;
  144. }
  145. stock iDB_getnameid(szTable[], szName[]) {
  146.     if(!iDB_Check) return print("DB_MASTER ERROR: No database opened!");
  147.     format(iDB_String, sizeof(iDB_String), "SELECT `id` FROM `%s` WHERE `name`='%s' COLLATE NOCASE", szTable, szName);
  148.     iDB_Result = db_query(iDB_DataBase, iDB_String);
  149.     if(iDB_Result) {
  150.         new id = -1;
  151.         if(db_num_rows(iDB_Result) != 0) {
  152.             db_get_field(iDB_Result, 0, iDB_String, sizeof(iDB_String));
  153.             id = strval(iDB_String);
  154.         }
  155.         db_free_result(iDB_Result);
  156.         return id;
  157.     }
  158.     return -1;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement