Advertisement
Kwarde

extend_mysql.inc

Nov 10th, 2019
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.54 KB | None | 0 0
  1. #if !defined _samp_included
  2.     #include <a_samp>
  3. #endif
  4. #if !defined mysql_included
  5.     #include <a_mysql>
  6. #endif
  7. #if !defined _INC_SSCANF
  8.     #include <sscanf2>
  9. #endif
  10. #if !defined _extend_mysql_included
  11.     #define _extend_mysql_included
  12. #else
  13.     #error There's no need to include extend_mysql twice
  14. #endif
  15. #if !defined isnull
  16.     #define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
  17. #endif
  18.  
  19. #define SQL_ERROR_FATAL 0 //Abort gamemode initalisation/runtime
  20. #define SQL_ERROR_FAIL  1 //Function failure.
  21. #define SQL_ERROR_WARN  2 //Warning or failure but proceed script
  22.  
  23. stock error_mysql(const msg[], error_level = SQL_ERROR_FATAL, bool:showError = true, MySQL:handle = MYSQL_DEFAULT_HANDLE)
  24. {
  25.     new error_str[500];
  26.     mysql_error(error_str, sizeof(error_str), handle);
  27.     if (showError) printf("\n%s %s. Details:\n<%d> %s\n", ((error_level == SQL_ERROR_FATAL)?("[FATAL ERROR]"):(error_level == SQL_ERROR_FAIL)?("[ERROR]"):("<!>")), msg, mysql_errno(handle), error_str);
  28.     else printf("\n%s %s\n", ((error_level == SQL_ERROR_FATAL)?("[FATAL ERROR]"):(error_level == SQL_ERROR_FAIL)?("[ERROR]"):("<!>")), msg);
  29.     return ((error_level == SQL_ERROR_WARN) ? (1) : (0));
  30. }
  31.  
  32. stock bool:db_col_exists(const table[], const column[], MySQL:handle = MYSQL_DEFAULT_HANDLE)
  33. {
  34.     new Cache:res, query[70], num_rows, i;
  35.     format(query, sizeof(query), "SHOW COLUMNS FROM %s", table);
  36.     res = mysql_query(handle, query);
  37.     if (mysql_errno() != 0)
  38.     {
  39.         error_mysql("Failed to check columns in table. Table doesn't exist?", SQL_ERROR_WARN);
  40.         return false;
  41.     }
  42.  
  43.     cache_get_row_count(num_rows);
  44.     while (i < num_rows)
  45.     {
  46.         cache_get_value_name(i, "Field", query);
  47.         if (!strcmp(query, column))
  48.         {
  49.             cache_delete(res);
  50.             return true;
  51.         }
  52.         i++;
  53.     }
  54.     cache_delete(res);
  55.     return false;
  56. }
  57.  
  58. stock db_col_add(const table[], const column[], const column_data[], const add_after[] = "", MySQL:handle = MYSQL_DEFAULT_HANDLE)
  59. {
  60.     new Cache:res, query[200];
  61.     if (isnull(add_after))
  62.         format(query, sizeof(query), "ALTER TABLE %s ADD COLUMN %s %s", table, column, column_data);
  63.     else format(query, sizeof(query), "ALTER TABLE %s ADD COLUMN %s %s AFTER %s", table, column, column_data, add_after);
  64.     res = mysql_query(handle, query);
  65.     if (cache_warning_count() == 0)
  66.     {
  67.         cache_delete(res);
  68.         return true;
  69.     }
  70.     return false;
  71. }
  72.  
  73. stock db_remove_tables(bool:confirm = false, bool:print_names = false, MySQL:handle = MYSQL_DEFAULT_HANDLE)
  74. {
  75.     new Cache:res = mysql_query(handle, "SELECT DATABASE() AS DB_NAME");
  76.     if (mysql_errno() != 0) return error_mysql("Failed to get database name", SQL_ERROR_FAIL);
  77.     new db_name[65], query[145];
  78.     cache_get_value_name(0, "DB_NAME", db_name);
  79.     cache_delete(res);
  80.     format(query, sizeof(query), "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%s'", db_name);
  81.     res = mysql_query(handle, query);
  82.     if (mysql_errno() != 0) return error_mysql("Failed to get table data", SQL_ERROR_FAIL);
  83.     new num_rows;
  84.     cache_get_row_count(num_rows);
  85.     if (num_rows > 0)
  86.     {
  87.         new i;
  88.         while (i < num_rows)
  89.         {
  90.             new tbl[64], str[64*2];
  91.             cache_get_value_name(i, "TABLE_NAME", str);
  92.             sscanf(str, "p<.>s[64]s[64]", query, tbl);
  93.             if (!confirm && print_names)
  94.                 printf("- Table: %s", tbl);
  95.             else if (confirm)
  96.             {
  97.                 format(query, 76, "DROP TABLE %s", tbl);
  98.                 mysql_query(handle, query);
  99.                 printf("<!> Dropped table '%s'", tbl);
  100.             }
  101.             i++;
  102.         }
  103.         cache_delete(res);
  104.         return i+1;
  105.     }
  106.     else printf("<!> There are no tables in database %s", db_name);
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement