Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if !defined MAX_DB_HANDLE
- #define MAX_DB_HANDLE 10
- #endif
- #define MAX_QUERY_STRING 3072
- #define MAX_WHERE_STRING 128
- #define MAX_TABLE_NAME 50
- #define MAX_COLUMN_NAME 50
- #define MAX_COLUMN_VALUE_STRING 256
- #define MAX_COLUMN_VALUE_INT 20
- #define MAX_COLUMN_VALUE_FLOAT 128
- #define INVALID_DB_HANDLE -1
- enum E_DB_HANDLE_DATA
- {
- _Table[MAX_TABLE_NAME],
- _Where[MAX_WHERE_STRING],
- _Value1[MAX_QUERY_STRING],
- _Value2[MAX_QUERY_STRING],
- _Query[MAX_QUERY_STRING],
- _Elapsed
- }
- static __QueryData[MAX_DB_HANDLE][E_DB_HANDLE_DATA];
- static DB:__dbHandle[MAX_DB_HANDLE] = {DB:INVALID_DB_HANDLE, ...};
- forward OnDbExecuteQuery(DB:handle, sql[], bool:error, elapsed);
- stock DBResult:hook_db_query(DB:db, const query[], bool:use_result)
- {
- new tick = GetTickCount();
- new DBResult:result = db_query(db, query);
- if(!use_result)
- db_free_result(result);
- CallLocalFunction("OnDbExecuteQuery", "isbi", _:db, query, (result < DBResult:1), (GetTickCount() - tick));
- return result;
- }
- #if defined _ALS_db_query
- #undef db_query
- #else
- #define _ALS_db_query
- #endif
- #define db_query hook_db_query
- // --- COMMAND HANDLER ---
- stock db_query_handle(DB:handle, bool:force_handle = true)
- {
- new idx = GetFreeDbHandleSlot();
- if(force_handle)
- {
- new bool:wait_handle = false;
- new wait_handle_tick = GetTickCount();
- while(idx == -1)
- {
- wait_handle = true;
- idx = GetFreeDbHandleSlot();
- }
- if(wait_handle)
- printf("\n\n[E_DB WARN] Waiting for create new db handle (%ims)\n\n", GetTickCount() - wait_handle_tick);
- }
- if(idx == -1)
- {
- print("\n\n[E_DB ERROR] Error to create new db handle (%ims)\n\n");
- return INVALID_DB_HANDLE;
- }
- __dbHandle[idx] = handle;
- __QueryData[idx][_Elapsed] = GetTickCount();
- return idx;
- }
- // --- TABLE CREATE ---
- stock db_create_table(handle, const name[])
- {
- if(!IsValidDbHandle(handle) || !strlen(name))
- {
- ClearCashHandle(handle);
- return false;
- }
- strcat(__QueryData[handle][_Value1], "CREATE TABLE IF NOT EXISTS ");
- strcat(__QueryData[handle][_Value1], name);
- return true;
- }
- stock db_add_column(handle, const name[], const datatype[])
- {
- if(!IsValidDbHandle(handle) || !strlen(name) || !strlen(datatype))
- {
- ClearCashHandle(handle);
- return false;
- }
- strcat(__QueryData[handle][_Value2], name);
- strcat(__QueryData[handle][_Value2], " ");
- strcat(__QueryData[handle][_Value2], datatype);
- strcat(__QueryData[handle][_Value2], ",");
- return true;
- }
- stock DBResult:db_execute_create_table(handle, bool:use_result = false, bool:debug_query = false)
- {
- if(!IsValidDbHandle(handle) || !strlen(__QueryData[handle][_Value1]) || !strlen(__QueryData[handle][_Value2]))
- {
- ClearCashHandle(handle);
- return DBResult:0;
- }
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Value1]);
- strcat(__QueryData[handle][_Query], " (");
- strdel(__QueryData[handle][_Value2], strlen(__QueryData[handle][_Value2]) - 1, strlen(__QueryData[handle][_Value2]));
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Value2]);
- strcat(__QueryData[handle][_Query], ");");
- new DBResult:result = db_query(__dbHandle[handle], __QueryData[handle][_Query], use_result);
- if(!use_result)
- db_free_result(result);
- if(debug_query)
- printf(__QueryData[handle][_Query]);
- ClearCashHandle(handle);
- return result;
- }
- // --- TABLE DEFINE ---
- stock db_set_table(handle, const table[], bool:use_backticks = true)
- {
- if(!IsValidDbHandle(handle) || !strlen(table))
- {
- ClearCashHandle(handle);
- return false;
- }
- __QueryData[handle][_Table][0] = '\0';
- if(use_backticks)
- strcat(__QueryData[handle][_Table], "`");
- strcat(__QueryData[handle][_Table], table);
- if(use_backticks)
- strcat(__QueryData[handle][_Table], "`");
- return true;
- }
- // --- WHERE CLAUSE ---
- #pragma unused __temp@WhereClause
- new __temp@WhereClause[MAX_WHERE_STRING];
- #define db_set_where_clause_ex(%0,%1,%2)\
- format(__temp@WhereClause, sizeof __temp@WhereClause, %1, %2),\
- db_set_where_clause(%0, __temp@WhereClause)
- stock db_set_where_clause(handle, const where[])
- {
- if(__dbHandle[handle] == DB:INVALID_DB_HANDLE || !strlen(where))
- {
- ClearCashHandle(handle);
- return false;
- }
- strcat(__QueryData[handle][_Where], where);
- return true;
- }
- // --- DELETE COMMAND ---
- stock DBResult:db_execute_delete(handle, bool:use_result = false, bool:debug_query = false)
- {
- if(!IsValidDbHandle(handle) || !strlen(__QueryData[handle][_Table]))
- {
- ClearCashHandle(handle);
- return DBResult:0;
- }
- strcat(__QueryData[handle][_Query], "DELETE FROM ");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Table]);
- if(__QueryData[handle][_Where][0] != '\0')
- {
- strcat(__QueryData[handle][_Query], " WHERE ");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Where]);
- }
- strcat(__QueryData[handle][_Query], ";");
- new DBResult:result = db_query(__dbHandle[handle], __QueryData[handle][_Query], use_result);
- if(debug_query)
- printf(__QueryData[handle][_Query]);
- if(!use_result)
- db_free_result(result);
- ClearCashHandle(handle);
- return result;
- }
- // --- SELECT COMMAND ---
- stock db_add_select(handle, const column[], const as[] = "", bool:use_backticks = true)
- {
- if(!IsValidDbHandle(handle) || !strlen(column))
- {
- ClearCashHandle(handle);
- return false;
- }
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], column);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- if(as[0] != '\0')
- {
- strcat(__QueryData[handle][_Value1], " AS ");
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], as);
- strcat(__QueryData[handle][_Value1], "`");
- }
- strcat(__QueryData[handle][_Value1], ",");
- return true;
- }
- stock DBResult:db_execute_select(handle, bool:use_result = false, bool:debug_query = false)
- {
- if(!IsValidDbHandle(handle) || !strlen(__QueryData[handle][_Value1]) || !strlen(__QueryData[handle][_Table]))
- {
- ClearCashHandle(handle);
- return DBResult:0;
- }
- strdel(__QueryData[handle][_Value1], strlen(__QueryData[handle][_Value1]) - 1, strlen(__QueryData[handle][_Value1]));
- strcat(__QueryData[handle][_Query], "SELECT ");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Value1]);
- strcat(__QueryData[handle][_Query], " FROM ");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Table]);
- if(__QueryData[handle][_Where][0] != '\0')
- {
- strcat(__QueryData[handle][_Query], " WHERE ");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Where]);
- }
- strcat(__QueryData[handle][_Query], ";");
- new DBResult:result = db_query(__dbHandle[handle], __QueryData[handle][_Query], use_result);
- if(!use_result)
- db_free_result(result);
- if(debug_query)
- printf(__QueryData[handle][_Query]);
- ClearCashHandle(handle);
- return result;
- }
- // --- UPDATE COMMAND ---
- stock db_update_string(handle, const column[], const value[], bool:use_backticks = true)
- {
- if(!IsValidDbHandle(handle) || !strlen(column))
- {
- ClearCashHandle(handle);
- return false;
- }
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], column);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], "=");
- strcat(__QueryData[handle][_Value1], "'");
- strcat(__QueryData[handle][_Value1], value);
- strcat(__QueryData[handle][_Value1], "'");
- strcat(__QueryData[handle][_Value1], ",");
- return true;
- }
- stock db_update_int(handle, const column[], value, bool:use_backticks = true)
- {
- if(!IsValidDbHandle(handle) || !strlen(column))
- {
- ClearCashHandle(handle);
- return false;
- }
- new valueStr[MAX_COLUMN_VALUE_INT];
- valstr(valueStr, value);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], column);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], "=");
- strcat(__QueryData[handle][_Value1], valueStr);
- strcat(__QueryData[handle][_Value1], ",");
- return true;
- }
- stock db_update_float(handle, const column[], Float:value, bool:use_backticks = true)
- {
- if(!IsValidDbHandle(handle) || !strlen(column))
- {
- ClearCashHandle(handle);
- return false;
- }
- new valueStr[MAX_COLUMN_VALUE_FLOAT];
- format(valueStr, sizeof valueStr, "%f", value);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], column);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], "=");
- strcat(__QueryData[handle][_Value1], valueStr);
- strcat(__QueryData[handle][_Value1], ",");
- return true;
- }
- stock DBResult:db_execute_update(handle, bool:use_result = false, bool:debug_query = false)
- {
- if(!IsValidDbHandle(handle) || !strlen(__QueryData[handle][_Value1]) || !strlen(__QueryData[handle][_Table]))
- {
- ClearCashHandle(handle);
- return DBResult:0;
- }
- strdel(__QueryData[handle][_Value1], strlen(__QueryData[handle][_Value1]) - 1, strlen(__QueryData[handle][_Value1]));
- strcat(__QueryData[handle][_Query], "UPDATE ");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Table]);
- strcat(__QueryData[handle][_Query], " SET ");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Value1]);
- if(__QueryData[handle][_Where][0] != '\0')
- {
- strcat(__QueryData[handle][_Query], " WHERE ");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Where]);
- }
- strcat(__QueryData[handle][_Query], ";");
- new DBResult:result = db_query(__dbHandle[handle], __QueryData[handle][_Query], use_result);
- if(!use_result)
- db_free_result(result);
- if(debug_query)
- printf(__QueryData[handle][_Query]);
- ClearCashHandle(handle);
- return result;
- }
- // --- INSERT COMMAND ---
- stock db_insert_string(handle, const column[], const value[], bool:use_backticks = true)
- {
- if(!IsValidDbHandle(handle) || !strlen(column))
- {
- ClearCashHandle(handle);
- return false;
- }
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], column);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], ",");
- strcat(__QueryData[handle][_Value2], "'");
- strcat(__QueryData[handle][_Value2], value);
- strcat(__QueryData[handle][_Value2], "'");
- strcat(__QueryData[handle][_Value2], ",");
- return true;
- }
- stock db_insert_int(handle, const column[], value, bool:use_backticks = true)
- {
- if(!IsValidDbHandle(handle) || !strlen(column))
- {
- ClearCashHandle(handle);
- return false;
- }
- new valueStr[MAX_COLUMN_VALUE_INT];
- valstr(valueStr, value);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], column);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], ",");
- strcat(__QueryData[handle][_Value2], valueStr);
- strcat(__QueryData[handle][_Value2], ",");
- return true;
- }
- stock db_insert_float(handle, const column[], Float:value, bool:use_backticks = true)
- {
- if(!IsValidDbHandle(handle) || !strlen(column))
- {
- ClearCashHandle(handle);
- return false;
- }
- new valueStr[MAX_COLUMN_VALUE_FLOAT];
- format(valueStr, sizeof valueStr, "%f", value);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], column);
- if(use_backticks)
- strcat(__QueryData[handle][_Value1], "`");
- strcat(__QueryData[handle][_Value1], ",");
- strcat(__QueryData[handle][_Value2], valueStr);
- strcat(__QueryData[handle][_Value2], ",");
- return true;
- }
- stock DBResult:db_execute_insert(handle, bool:use_result = false, bool:debug_query = false)
- {
- if(!IsValidDbHandle(handle) || !strlen(__QueryData[handle][_Value1]) || !strlen(__QueryData[handle][_Value2]) || !strlen(__QueryData[handle][_Table]))
- {
- ClearCashHandle(handle);
- return DBResult:0;
- }
- strdel(__QueryData[handle][_Value1], strlen(__QueryData[handle][_Value1]) - 1, strlen(__QueryData[handle][_Value1]));
- strdel(__QueryData[handle][_Value2], strlen(__QueryData[handle][_Value2]) - 1, strlen(__QueryData[handle][_Value2]));
- strcat(__QueryData[handle][_Query], "INSERT INTO ");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Table]);
- strcat(__QueryData[handle][_Query], " (");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Value1]);
- strcat(__QueryData[handle][_Query], ") ");
- strcat(__QueryData[handle][_Query], "VALUES");
- strcat(__QueryData[handle][_Query], " (");
- strcat(__QueryData[handle][_Query], __QueryData[handle][_Value2]);
- strcat(__QueryData[handle][_Query], ");");
- new DBResult:result = db_query(__dbHandle[handle], __QueryData[handle][_Query], use_result);
- if(!use_result)
- db_free_result(result);
- if(debug_query)
- printf(__QueryData[handle][_Query]);
- ClearCashHandle(handle);
- return result;
- }
- stock IsValidDbHandle(handle)
- {
- if(handle < 0 || handle >= MAX_DB_HANDLE)
- return false;
- if(__dbHandle[handle] == DB:INVALID_DB_HANDLE)
- return false;
- return true;
- }
- // --- INTERNAL ---
- static stock ClearCashHandle(handle)
- {
- if(!IsValidDbHandle(handle))
- return false;
- __QueryData[handle][_Table][0] = '\0';
- __QueryData[handle][_Value1][0] = '\0';
- __QueryData[handle][_Value2][0] = '\0';
- __QueryData[handle][_Query][0] = '\0';
- __QueryData[handle][_Where][0] = '\0';
- __QueryData[handle][_Elapsed] = 0;
- __dbHandle[handle] = DB:INVALID_DB_HANDLE;
- return true;
- }
- static stock GetFreeDbHandleSlot()
- {
- for(new i = 0; i < MAX_DB_HANDLE; i ++)
- {
- if(__dbHandle[i] == DB:-1)
- return i;
- }
- return INVALID_DB_HANDLE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement