Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if !defined _samp_included
- #include <a_samp>
- #endif
- #if !defined mysql_included
- #include <a_mysql>
- #endif
- #if !defined _INC_SSCANF
- #include <sscanf2>
- #endif
- #if !defined _extend_mysql_included
- #define _extend_mysql_included
- #else
- #error There's no need to include extend_mysql twice
- #endif
- #if !defined isnull
- #define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
- #endif
- #define SQL_ERROR_FATAL 0 //Abort gamemode initalisation/runtime
- #define SQL_ERROR_FAIL 1 //Function failure.
- #define SQL_ERROR_WARN 2 //Warning or failure but proceed script
- stock error_mysql(const msg[], error_level = SQL_ERROR_FATAL, bool:showError = true, MySQL:handle = MYSQL_DEFAULT_HANDLE)
- {
- new error_str[500];
- mysql_error(error_str, sizeof(error_str), handle);
- 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);
- else printf("\n%s %s\n", ((error_level == SQL_ERROR_FATAL)?("[FATAL ERROR]"):(error_level == SQL_ERROR_FAIL)?("[ERROR]"):("<!>")), msg);
- return ((error_level == SQL_ERROR_WARN) ? (1) : (0));
- }
- stock bool:db_col_exists(const table[], const column[], MySQL:handle = MYSQL_DEFAULT_HANDLE)
- {
- new Cache:res, query[70], num_rows, i;
- format(query, sizeof(query), "SHOW COLUMNS FROM %s", table);
- res = mysql_query(handle, query);
- if (mysql_errno() != 0)
- {
- error_mysql("Failed to check columns in table. Table doesn't exist?", SQL_ERROR_WARN);
- return false;
- }
- cache_get_row_count(num_rows);
- while (i < num_rows)
- {
- cache_get_value_name(i, "Field", query);
- if (!strcmp(query, column))
- {
- cache_delete(res);
- return true;
- }
- i++;
- }
- cache_delete(res);
- return false;
- }
- stock db_col_add(const table[], const column[], const column_data[], const add_after[] = "", MySQL:handle = MYSQL_DEFAULT_HANDLE)
- {
- new Cache:res, query[200];
- if (isnull(add_after))
- format(query, sizeof(query), "ALTER TABLE %s ADD COLUMN %s %s", table, column, column_data);
- else format(query, sizeof(query), "ALTER TABLE %s ADD COLUMN %s %s AFTER %s", table, column, column_data, add_after);
- res = mysql_query(handle, query);
- if (cache_warning_count() == 0)
- {
- cache_delete(res);
- return true;
- }
- return false;
- }
- stock db_remove_tables(bool:confirm = false, bool:print_names = false, MySQL:handle = MYSQL_DEFAULT_HANDLE)
- {
- new Cache:res = mysql_query(handle, "SELECT DATABASE() AS DB_NAME");
- if (mysql_errno() != 0) return error_mysql("Failed to get database name", SQL_ERROR_FAIL);
- new db_name[65], query[145];
- cache_get_value_name(0, "DB_NAME", db_name);
- cache_delete(res);
- format(query, sizeof(query), "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%s'", db_name);
- res = mysql_query(handle, query);
- if (mysql_errno() != 0) return error_mysql("Failed to get table data", SQL_ERROR_FAIL);
- new num_rows;
- cache_get_row_count(num_rows);
- if (num_rows > 0)
- {
- new i;
- while (i < num_rows)
- {
- new tbl[64], str[64*2];
- cache_get_value_name(i, "TABLE_NAME", str);
- sscanf(str, "p<.>s[64]s[64]", query, tbl);
- if (!confirm && print_names)
- printf("- Table: %s", tbl);
- else if (confirm)
- {
- format(query, 76, "DROP TABLE %s", tbl);
- mysql_query(handle, query);
- printf("<!> Dropped table '%s'", tbl);
- }
- i++;
- }
- cache_delete(res);
- return i+1;
- }
- else printf("<!> There are no tables in database %s", db_name);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement