Re: SQL Connection Manager
Hello friend look over the code vo te he ta mexi ORIGINAL not in it at all so I changed the line that you told me about SQL_TRAC.
PHP Code:
#pragma dynamic 16000
#include <amxmisc> #include <sqlx> #include <hashx> #include <bits> #include <text> #include <vdf> #include <sql_manager>
#define PLUGIN "SQL Connection Manager" #define VERSION "1.0" #define AUTHOR "Zefir"
#define FLAG_CONNECTED (1 << 0) #define FLAG_RECONNECTING (1 << 1)
#define DEBUG #define SQL_TRACE
new bool:PLUGIN_END = false
enum e_cvars { amx_sql_host, amx_sql_type, amx_sql_db, amx_sql_user, amx_sql_pass } new pcvar[e_cvars]
//Tuples enum e_SQLTuple { tuple_flags, tuple_name[SQL_NAME], Handle:tuple_handle, tuple_driver[SQL_NAME], tuple_user[SQL_NAME], tuple_pass[SQL_NAME], tuple_host[SQL_HOST], tuple_db[SQL_NAME], tuple_timeout, tuple_autoconnect, //Connection Handle:tuple_connect_handle, // Statistics tuple_Reconnected, tuple_Threaded_Query, tuple_Direct_Query, // Errors stat and messages tuple_Error_Query, // error count tuple_First_Error, tuple_Last_Error, tuple_First_Error_Msg[SQL_ERROR_LENGTH], tuple_Last_Error_Msg[SQL_ERROR_LENGTH] } new cur_SQLTuple[e_SQLTuple], tmp_SQLTuple[e_SQLTuple] new HashX:gSQLTuple
enum e_thread { th_tuple_name[SQL_NAME], th_plugin_id, th_func_id, th_data } new tmp_thread[e_thread] new Array:gThreads
// Handles new Hash:gSQLHandle
// Forwards new fw_connection_established // pointer to forward callback
log_sql(fmt[], any:...) { static buffer[SQL_ERROR_LENGTH] vformat(buffer, charsmax(buffer), fmt, 2)
#if defined DEBUG // server_print("[SQLM] %s", buffer) // server_print("[SQLM Flags]: <%s> FLAG_CONNECTED: %d, FLAG_RECONNECTING: %d", cur_SQLTuple[tuple_name], get_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED), get_bits(cur_SQLTuple[tuple_flags], FLAG_RECONNECTING)) log_amx("[SQLM:%s] %s", cur_SQLTuple[tuple_name], buffer) // return log_error(AMX_ERR_NATIVE, "[SQLM] %s", buffer) #endif }
public sqlm_stats(id) { new name[SQL_NAME] read_argv(1, name, charsmax(name))
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
if (!set_tuple(name)) return
console_print(id, "^n%s, version %s, by %s", PLUGIN, VERSION, AUTHOR) console_print(id, " Statistic for Tuple:............ '%s'", cur_SQLTuple[tuple_name]) console_print(id, " Direct Queryes:............... %d", cur_SQLTuple[tuple_Direct_Query]) console_print(id, " Threaded Queryes:............. %d", cur_SQLTuple[tuple_Threaded_Query]) console_print(id, " Errors:") console_print(id, " Reconnect count:.............. %d", cur_SQLTuple[tuple_Reconnected]) console_print(id, " Error count:.................. %d", cur_SQLTuple[tuple_Error_Query]) console_print(id, " First Error:.................. (%d) '%s'", cur_SQLTuple[tuple_First_Error], cur_SQLTuple[tuple_First_Error_Msg]) console_print(id, " Last Error:................... (%d) '%s'", cur_SQLTuple[tuple_Last_Error], cur_SQLTuple[tuple_Last_Error_Msg])
set_tuple(old_tuple) }
/*=============================================================================
FORWARDS
=============================================================================*/
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
pcvar[amx_sql_host] = register_cvar("amx_sql_host", "") pcvar[amx_sql_type] = register_cvar("amx_sql_type", "") pcvar[amx_sql_db] = register_cvar("amx_sql_db", "") pcvar[amx_sql_user] = register_cvar("amx_sql_user", "") pcvar[amx_sql_pass] = register_cvar("amx_sql_pass", "")
gSQLTuple = HashXCreate() gSQLHandle = HashCreate() gThreads = ArrayCreate(e_thread)
fw_connection_established = CreateMultiForward("sqlm_connection_established", ET_IGNORE, FP_STRING, FP_STRING)
register_srvcmd("sqlm_stats", "sqlm_stats")
new config_dir[PATH_LENGTH] get_configsdir(config_dir, charsmax(config_dir))
server_cmd("exec %s/sql.cfg", config_dir)
// set_task(5.0, "sqlm_init") }
public plugin_modules() require_module("vdf");
public plugin_cfg() { server_exec(); sqlm_init(); }
public plugin_end() { PLUGIN_END = true; HashXForEach(0, gSQLTuple, "sqlm_close_tuple"); ArrayDestroy(gThreads); HashXDestroy(gSQLTuple); HashDestroy(gSQLHandle); }
/*=============================================================================
FUNCTIONS
=============================================================================*/ sqlm_init() { static name[SQL_NAME], host[SQL_HOST], user[SQL_NAME], pass[SQL_NAME], db[SQL_NAME] static type[SQL_NAME], timeout, autoconnect, tmp_str[16]
get_pcvar_string(pcvar[amx_sql_host], host, charsmax(host)) get_pcvar_string(pcvar[amx_sql_user], user, charsmax(user)) get_pcvar_string(pcvar[amx_sql_pass], pass, charsmax(pass)) get_pcvar_string(pcvar[amx_sql_type], type, charsmax(type)) get_pcvar_string(pcvar[amx_sql_db], db, charsmax(db)) timeout = 0 autoconnect = 0
// sqlm_register_tuple(DEFAULT_TUPLE_NAME, type, host, user, pass, db, timeout)
log_sql("INIT Complete")
new config_dir[PATH_LENGTH], filename[PATH_LENGTH] get_configsdir(config_dir, charsmax(config_dir)) formatex(filename, charsmax(filename), "%s/sql_manager.vdf", config_dir)
// vdf load new VdfTree:tree, VdfNode:node, VdfNode:tuple_node, key[SQL_NAME] if ((tree = vdf_open(filename))) { node = vdf_get_root_node(tree) if ((node = vdf_find_in_branch(node, "Tuples")) && (tuple_node = vdf_get_child_node(node))) { do { vdf_get_node_key(tuple_node, name, charsmax(name)) node = vdf_get_child_node(tuple_node)
do { vdf_get_node_key(node, key, charsmax(key)) trim(key) if (equali(key, "type")) vdf_get_node_value(node, type, charsmax(type)) else if (equali(key, "host")) vdf_get_node_value(node, host, charsmax(host)) else if (equali(key, "user")) vdf_get_node_value(node, user, charsmax(user)) else if (equali(key, "pass")) vdf_get_node_value(node, pass, charsmax(pass)) else if (equali(key, "db")) vdf_get_node_value(node, db, charsmax(db)) else if (equali(key, "timeout")) { vdf_get_node_value(node, tmp_str, charsmax(tmp_str)) timeout = 0 if (is_str_num(tmp_str)) timeout = str_to_num(tmp_str) } else if (equali(key, "autoconnect")) { vdf_get_node_value(node, tmp_str, charsmax(tmp_str)) autoconnect = 0 if (is_str_num(tmp_str)) autoconnect = str_to_num(tmp_str) } } while ((node = vdf_get_next_node(node))) sqlm_register_tuple(name, type, host, user, pass, db, timeout, autoconnect) } while ((tuple_node = vdf_get_next_node(tuple_node)))
} else { // if not exists "default" tuple vdf_set_default_tuple(tree, type, host, user, pass, db) sqlm_register_tuple(DEFAULT_TUPLE_NAME, type, host, user, pass, db) } } else { // if vdf is not exists tree = vdf_create_tree(filename) vdf_set_default_tuple(tree, type, host, user, pass, db) sqlm_register_tuple(DEFAULT_TUPLE_NAME, type, host, user, pass, db) }
set_tuple(DEFAULT_TUPLE_NAME)
log_sql("Configuration loaded") }
vdf_set_default_tuple(VdfTree:tree, type[], host[], user[], pass[], db[]) { new VdfNode:node = vdf_get_root_node(tree) node = vdf_append_node(tree, node, "Tuples") node = vdf_append_child_node(tree, node, DEFAULT_TUPLE_NAME)
vdf_append_child_node(tree, node, "type", type) vdf_append_child_node(tree, node, "host", host) vdf_append_child_node(tree, node, "user", user) vdf_append_child_node(tree, node, "pass", pass) vdf_append_child_node(tree, node, "db", db) vdf_append_child_node(tree, node, "timeout", "0") vdf_append_child_node(tree, node, "autoconnect", "0")
vdf_save(tree) } /*================================================================
Tuples
================================================================*/
bool:sqlm_register_tuple(const name[SQL_NAME], const driver[], const host[], const user[], const pass[], const db[], timeout = 0, autoconnect = 0) { static old_driver[SQL_NAME], old_tuple[SQL_NAME];
scopy(old_tuple, cur_SQLTuple[tuple_name]);
copy(cur_SQLTuple[tuple_name], charsmax(cur_SQLTuple[tuple_name]), name); copy(cur_SQLTuple[tuple_driver], charsmax(cur_SQLTuple[tuple_driver]), driver); copy(cur_SQLTuple[tuple_host], charsmax(cur_SQLTuple[tuple_host]), host); copy(cur_SQLTuple[tuple_user], charsmax(cur_SQLTuple[tuple_user]), user); copy(cur_SQLTuple[tuple_pass], charsmax(cur_SQLTuple[tuple_pass]), pass); copy(cur_SQLTuple[tuple_db], charsmax(cur_SQLTuple[tuple_db]), db); cur_SQLTuple[tuple_timeout] = timeout; cur_SQLTuple[tuple_autoconnect] = autoconnect; clr_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED);
SQL_GetAffinity(old_driver, charsmax(old_driver)); if (!equali(old_driver, driver) && !SQL_SetAffinity(driver)) { log_sql("ERROR: Driver '%s' is not loaded", driver); // SQL_SetAffinity(old_driver) } else {
if (HashXKeyExists(gSQLTuple, name)) log_sql("WARNING: Tuple name '%s' already еxists, reinit", name); else if (autoconnect >= 0) { log_sql("Attempt register tuple: '%s' driver: '%s', host: '%s', user: '%s', pass: ***, db: '%s'", name, driver, host, user, db); if ((cur_SQLTuple[tuple_handle] = fixed:SQL_MakeDbTuple(host, user, pass, db, timeout))) { set_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED); log_sql("Connection successful: %s", name); } else log_sql("Connection error: %s", name); } else log_sql("Tuple: '%s': disabled", name);
} HashXSetArray(gSQLTuple, name, cur_SQLTuple, sizeof(cur_SQLTuple));
if (get_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED)) { static rtv; ExecuteForward(fw_connection_established, rtv, name, cur_SQLTuple[tuple_driver]); } else { if (autoconnect > 0) sqlm_reconnect(); set_tuple(old_tuple); return false; }
return true; }
bool:set_tuple(const s_name[SQL_NAME]) { if (PLUGIN_END) return false
new name[SQL_NAME] scopy(name, s_name) trim(name)
// If name empty, use default if (name[0] == EOS) { log_sql("WARN: Tuple name is empty ('%s'), set to 'default'.", name) scopy(name, DEFAULT_TUPLE_NAME) // scopy(name, cur_SQLTuple[tuple_name]) // return false }
if (!HashXGetArray(gSQLTuple, name, tmp_SQLTuple, e_SQLTuple)) { log_sql("ERROR: Tuple '%s' is not defined.", name) return false }
#if defined SQL_TRACE // log_sql("Tuple Set to '%s:%s'.", tmp_SQLTuple[tuple_driver], name) #endif
if (!equal(cur_SQLTuple[tuple_name], name)) { if (SQL_SetAffinity(tmp_SQLTuple[tuple_driver])) {
cur_SQLTuple = tmp_SQLTuple HashXSetArray(gSQLTuple, name, cur_SQLTuple, e_SQLTuple) } else { log_sql("ERROR: Tuple '%s' is not defined.", name) return false } } else {
new old_driver[SQL_NAME] SQL_GetAffinity(old_driver, charsmax(old_driver)) if (!equali(old_driver, tmp_SQLTuple[tuple_driver]) && !SQL_SetAffinity(tmp_SQLTuple[tuple_driver])) { log_sql("ERROR: Cannot set affinity '%s', restore to '%s'.", tmp_SQLTuple[tuple_driver], old_driver) SQL_SetAffinity(old_driver) return false } }
// server_exec() return true }
Handle:get_tuple(const name[SQL_NAME]) { if (!equal(cur_SQLTuple[tuple_name], name)) { if (HashXGetArray(gSQLTuple, name, tmp_SQLTuple, e_SQLTuple)) return tmp_SQLTuple[tuple_handle] else { log_sql("ERROR: Tuple '%s' is not defined.", name) return Empty_Handle } }
return cur_SQLTuple[tuple_handle] }
public sqlm_close_tuple(id, const name[SQL_NAME]) {
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(name) && get_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED)) SQL_FreeHandle(cur_SQLTuple[tuple_handle])
set_tuple(old_tuple)
// if (HashXGetArray(gSQLTuple, name, tmp_SQLTuple, e_SQLTuple) && get_bits(tmp_SQLTuple[tuple_flags], FLAG_CONNECTED)) // SQL_FreeHandle(tmp_SQLTuple[tuple_handle])
}
/*============================================== Associate tuple name to handle ==============================================*/
store_tuple(const name[SQL_NAME], Handle:handle) { static key[8] num_to_str(fixed:handle, key, charsmax(key)) HashSetString(gSQLHandle, key, name) }
/*============================================== Find tuple name by handle ==============================================*/ find_tuple(Handle:handle) { static key[8], name[SQL_NAME] name[0] = EOS num_to_str(fixed:handle, key, charsmax(key)) HashGetString(gSQLHandle, key, name, charsmax(name)) return name }
// store current touple state in hash sqlm_sync(index = 0) { if (index) cur_SQLTuple[e_SQLTuple:index]++ HashXSetArray(gSQLTuple, cur_SQLTuple[tuple_name], cur_SQLTuple, e_SQLTuple) }
store_error(Handle:handle) { static err_str[SQL_ERROR_LENGTH], err_num err_num = SQL_QueryError(handle, err_str, charsmax(err_str)) log_sql("--> Error code: %d (Message: ^"%s^")", err_num, err_str)
if (!cur_SQLTuple[tuple_First_Error] && cur_SQLTuple[tuple_First_Error_Msg][0] == EOS) { cur_SQLTuple[tuple_First_Error] = err_num copy(cur_SQLTuple[tuple_First_Error_Msg], charsmax(cur_SQLTuple[tuple_First_Error_Msg]), err_str) }
cur_SQLTuple[tuple_Last_Error] = err_num copy(cur_SQLTuple[tuple_Last_Error_Msg], charsmax(cur_SQLTuple[tuple_Last_Error_Msg]), err_str)
sqlm_sync(tuple_Error_Query) }
/*=============================================================================
Reconnect feathures
=============================================================================*/
#define tuple_reconnecting() bool:(get_bits(cur_SQLTuple[tuple_flags], FLAG_RECONNECTING)) reconnect_attempt(const name[SQL_NAME]) { HashXGetArray(gSQLTuple, name, tmp_SQLTuple, e_SQLTuple) if (get_bits(tmp_SQLTuple[tuple_flags], FLAG_RECONNECTING) || PLUGIN_END) return set_bits(tmp_SQLTuple[tuple_flags], FLAG_RECONNECTING) HashXSetArray(gSQLTuple, name, tmp_SQLTuple, e_SQLTuple)
log_sql("Reconnect tuple <%s><%d> called as '%s'", tmp_SQLTuple[tuple_name], tmp_SQLTuple[tuple_handle], name) SQL_ThreadQuery(tmp_SQLTuple[tuple_handle], "reconnect_stub", ";", name, sizeof(name)) }
public reconnect_stub(failstate, Handle:query, err_str[], err_num, data[SQL_NAME], size, Float:queuetime) { if (PLUGIN_END) return
log_sql("Reconnect stub <%s> result: %d", data, failstate)
HashXGetArray(gSQLTuple, data, tmp_SQLTuple, e_SQLTuple) clr_bits(tmp_SQLTuple[tuple_flags], FLAG_RECONNECTING) HashXSetArray(gSQLTuple, data, tmp_SQLTuple, e_SQLTuple) if (failstate == TQUERY_CONNECT_FAILED) { store_error(query) reconnect_attempt(data) } else { set_bits(tmp_SQLTuple[tuple_flags], FLAG_CONNECTED) HashXSetArray(gSQLTuple, data, tmp_SQLTuple, e_SQLTuple) log_sql("Connection established: %s!", data) static rtv ExecuteForward(fw_connection_established, rtv, data, tmp_SQLTuple[tuple_driver]) } }
/*================================================================
Connections
================================================================*/
Handle:get_connection(const name[SQL_NAME] = DEFAULT_TUPLE_NAME) { set_tuple(name) /* if (cur_SQLTuple[tuple_cur_con_index] && ArrayGetArray(gSQLConnect, cur_SQLTuple[tuple_cur_con_index], cur_SQLConnect) && get_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED))
return cur_SQLTuple[tuple_cur_con_handle]
else new_connection(name) */ if (!get_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED)) sqlm_reconnect()
return cur_SQLTuple[tuple_connect_handle]
/* static Handle:conn_size, Handle:i, tmp_SQLConnect[e_SQLConnect] conn_size = Handle:ArraySize(gSQLConnect)
for (i = HANDLE_NULL; i < conn_size; i++) if (ArrayGetArray(gSQLConnect, fixed:i, tmp_SQLConnect) && equal(tmp_SQLConnect[owner_name], name) && set_connect(i)) return i
arrayset(cur_SQLConnect, 0, e_SQLConnect) copy(cur_SQLConnect[owner_name], charsmax(cur_SQLConnect[owner_name]), name) cur_SQLTuple[tuple_index] = ArraySize(gSQLConnect) set_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED) ArrayPushArray(gSQLConnect, cur_SQLConnect) set_task(0.1, "sqlm_reconnect", fixed:cur_SQLTuple[tuple_index]) return cur_SQLTuple[tuple_index] */ }
/* close_connect(Handle:handle) if (set_connection(handle)) { set_bits(cur_SQLTuple[tuple_flags], SQL_CONNECT_CLOSED); clr_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED) SQL_FreeHandle(cur_SQLTuple[tuple_connect_handle]) sqlm_sync() } */
bool:sqlm_connected(name[SQL_NAME] = DEFAULT_TUPLE_NAME) { if (HashXGetArray(gSQLTuple, name, tmp_SQLTuple, e_SQLTuple)) return bool:get_bits(tmp_SQLTuple[tuple_flags], FLAG_CONNECTED) return false }
//#define sqlm_connected() bool:(get_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED)) #define sqlm_get_handle() Handle:(cur_SQLTuple[tuple_connect_handle]) /* bool:set_connection(Handle:handle) { static name[SQL_NAME], key[sizeof(handle) + 1] num_to_str(fixed:handle, key, charsmax(key)) HashGetString(gSQLHandle, key, name, charsmax(name)) return get_connection(name) } */
// ЕÑли была оошибка ÑÐ¾ÐµÐ´Ð¸Ð½ÐµÐ½Ð¸Ñ Ñ‚Ð¾ Ñта Ñ„ÑƒÐ½ÐºÑ†Ð¸Ñ Ð¿Ð¾Ð¿Ñ€Ð¾Ð±ÑƒÐµÑ‚ воÑттановить Ñоединение моментально, вроде ничего и не произошло. // еÑли же Ñоединение не воÑÑтановилоÑÑŒ, то будут начаты потоковые попытки ÑоединитÑÑ Ñ Ñервером. bool:sqlm_reconnect() { static name[SQL_NAME] if (cur_SQLTuple[tuple_connect_handle] != Empty_Handle && !get_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED)) return false
static err_num, err_str[SQL_ERROR_LENGTH] clr_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED) if ((cur_SQLTuple[tuple_connect_handle] = fixed:SQL_Connect(cur_SQLTuple[tuple_handle], err_num, err_str, charsmax(err_str)))) { set_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED) } else { scopy(name, cur_SQLTuple[tuple_name]) reconnect_attempt(name) }
log_sql("Reconnect '%s' = %d", cur_SQLTuple[tuple_name], get_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED))
sqlm_sync(tuple_Reconnected)
return sqlm_connected() }
/*=============================================================================
NATIVES
=============================================================================*/
public plugin_natives() { register_library("sql_manager")
// register_native("sqlm_register_tuple", "_sqlm_register_tuple") register_native("sqlm_connected", "_sqlm_connected")
register_native("sqlm_quote_string", "_sqlm_quote_string") register_native("sqlm_prepare_query", "_sqlm_prepare_query") register_native("sqlm_exec", "_sqlm_exec") register_native("sqlm_simple_query", "_sqlm_simple_query") register_native("sqlm_simple_upd_ins", "_sqlm_simple_upd_ins") register_native("sqlm_thread_query", "_sqlm_thread_query")
register_native("sqlm_affected_rows", "_sqlm_affected_rows") register_native("sqlm_field_name_to_num", "_sqlm_field_name_to_num") register_native("sqlm_field_num_to_name", "_sqlm_field_num_to_name") register_native("sqlm_free_handle", "_sqlm_free_handle") register_native("sqlm_get_insert_id", "_sqlm_get_insert_id") register_native("sqlm_get_query_string", "_sqlm_get_query_string") register_native("sqlm_is_null", "_sqlm_is_null") register_native("sqlm_more_results", "_sqlm_more_results") register_native("sqlm_next_row", "_sqlm_next_row") register_native("sqlm_num_columns", "_sqlm_num_columns") register_native("sqlm_num_results", "_sqlm_num_results") register_native("sqlm_query_error", "_sqlm_query_error") register_native("sqlm_read_result", "_sqlm_read_result") }
/* //sqlm_register_tuple(const name[SQL_NAME], const driver[], const host[], const user[], const pass[], const db[], timeout = 0) public bool:_sqlm_register_tuple(plugin_id, param_count) { new name[SQL_NAME], driver[SQL_NAME], host[SQL_HOST], user[SQL_NAME], pass[SQL_NAME], db[SQL_NAME] get_string(1, name, charsmax(name)) get_string(2, driver, charsmax(driver)) get_string(3, host, charsmax(host)) get_string(4, user, charsmax(user)) get_string(5, pass, charsmax(pass)) get_string(6, db, charsmax(db)) return sqlm_register_tuple(name, driver, host, user, pass, db, get_param(7)) } */
public bool:_sqlm_connected(plugin_id, param_count) { static tuple[SQL_NAME] get_string(1, tuple, charsmax(tuple)) return sqlm_connected(tuple) }
//sqlm_quote_string(const name[SQL_NAME], buffer[], buflen, const fmt[], {Float,_}:...) { public _sqlm_quote_string(plugin_id, param_count) { static name[SQL_NAME], buffer[SQL_QUERY], buffer2[SQL_QUERY] vdformat(buffer2, charsmax(buffer2), 4, 5) get_string(1, name, charsmax(name))
// sqlm_quote_string(name, buffer, charsmax(buffer), buffer2)
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
set_tuple(name) static retvalue, Handle:connect connect = get_connection(name) if (!connect) { set_tuple(old_tuple) return -1 } retvalue = SQL_QuoteString(connect, buffer, charsmax(buffer), buffer2) if (retvalue < 0) reconnect_attempt(name)
set_string(2, buffer, get_param(3))
set_tuple(old_tuple) return retvalue }
//Handle:sqlm_prepare_query (const fmt[], {Float,_}:...) //Handle:sqlm_prepare_query (const name[SQL_NAME], const fmt[], {Float,_}:...) { public Handle:_sqlm_prepare_query(plugin_id, param_count) { static name[SQL_NAME], buffer[SQL_QUERY] get_string(1, name, charsmax(name))
static Handle:handle, Handle:connect
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
if ((connect = get_connection(name))) { vdformat(buffer, charsmax(buffer), 2, 3) handle = SQL_PrepareQuery(connect, buffer) store_tuple(name, handle) if (!handle) reconnect_attempt(name) set_tuple(old_tuple) return handle } set_tuple(old_tuple) return Empty_Handle }
//sqlm_exec(Handle:handle) public _sqlm_exec(plugin_id, param_count) { static retvalue, Handle:handle, name[SQL_NAME] handle = Handle:get_param(1) name = find_tuple(handle) retvalue = 0
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(name)) { if (!(retvalue = SQL_Execute(handle))) { store_error(handle) reconnect_attempt(name) } }
sqlm_sync(tuple_Direct_Query) #if defined SQL_TRACE static querystring[SQL_QUERY] SQL_GetQueryString(handle, querystring, charsmax(querystring)) log_sql("Execute query: (%s)%s", name, querystring) #endif
set_tuple(old_tuple) return retvalue }
/*----------------------------------------------------------------------------- Simple threaded query -----------------------------------------------------------------------------*/
//sqlm_simple_query(const name[SQL_NAME], const query_string[], {Float,_}:...) public _sqlm_simple_query(plugin_id, param_count) { static name[SQL_NAME] get_string(1, name, charsmax(name))
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
static retvalue retvalue = 0
if (set_tuple(name)) {
// if (tuple_reconnecting(name)) // return 0
sqlm_sync(tuple_Threaded_Query)
static querystring[SQL_QUERY] vdformat(querystring, charsmax(querystring), 2, 3)
log_sql("Threaded query: %s", querystring)
retvalue = SQL_ThreadQuery(get_tuple(name), "sqlm_simple_stub", querystring, name, sizeof(name)) }
set_tuple(old_tuple) return retvalue }
public sqlm_simple_stub(failstate, Handle:query, err_str[], err_num, data[SQL_NAME], size, Float:queuetime) { // set_tuple(name)
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
if (!set_tuple(data)) { log_sql("ERROR: Tuple name error! --> Error code: %d (Message: ^"%s^")", err_num, err_str) return PLUGIN_CONTINUE }
static querystring[SQL_QUERY] SQL_GetQueryString(query, querystring, charsmax(querystring))
#if defined SQL_TRACE log_sql("Resolved query %d of tuple '%s', took %f seconds", query, data, queuetime) log_sql("--> Original query: %s", querystring) #endif
if (failstate) { store_error(query) if (failstate == TQUERY_CONNECT_FAILED) { clr_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED) log_sql("ERROR: Connection failed!") if (querystring[0] == EOS) SQL_GetQueryString(query, querystring, charsmax(querystring)) SQL_FreeHandle(query) reconnect_attempt(data) // if (sqlm_reconnect(cur_SQLTuple[tuple_index])) // sqlm_thread_exec(querystring) } else if (failstate == TQUERY_QUERY_FAILED) log_sql("ERROR: Query failed!") log_sql("--> Error code: %d (Message: ^"%s^")", err_num, err_str) }
set_tuple(old_tuple) return PLUGIN_HANDLED }
/*----------------------------------------------------------------------------- UpdateOrInsert threaded query -----------------------------------------------------------------------------*/
//sqlm_simple_upd_ins(const name[SQL_NAME], const update_string[], const insert_string[]) { public _sqlm_simple_upd_ins(plugin_id, param_count) { new name[SQL_NAME], update_string[SQL_QUERY], insert_string[SQL_QUERY] get_string(1, name, charsmax(name)) get_string(2, update_string, charsmax(update_string)) get_string(3, insert_string, charsmax(insert_string))
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
static retvalue retvalue = 0
if (set_tuple(name)) { sqlm_sync(tuple_Threaded_Query) new data[SQL_NAME] formatex(data, charsmax(data), "%s;%s", name, insert_string)
retvalue = SQL_ThreadQuery(get_tuple(name), "insertStub", update_string, data, sizeof(data)) }
set_tuple(old_tuple) return retvalue }
public insertStub(failstate, Handle:query, err_str[], err_num, data[], size, Float:queuetime) { static delim, name[SQL_NAME] if ((delim = contain(data, ";")) > -1) { data[delim] = EOS scopy(name, data) }
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
set_tuple(name)
static querystring[SQL_QUERY] #if defined SQL_TRACE log_sql("--> UPDATE took %f seconds", queuetime) #endif
new insert_string[SQL_QUERY] copy(insert_string, charsmax(insert_string), data[delim + 1])
if (failstate) { store_error(query) if (failstate == TQUERY_CONNECT_FAILED) { clr_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED) log_sql("ERROR: Connection failed!") SQL_GetQueryString(query, querystring, charsmax(querystring)) SQL_FreeHandle(query) reconnect_attempt(name) // if (sqlm_reconnect(cur_SQLTuple[tuple_index])) // sqlm_thread_upd_ins(querystring, insert_string) } else if (failstate == TQUERY_QUERY_FAILED) log_sql("ERROR: Query failed!") log_sql("--> Error code: %d (Message: ^"%s^")", err_num, err_str)
} else if (!SQL_AffectedRows(query)) { log_sql("--> NOT UPDATE, attempt INSERT %s", insert_string) // sqlm_simple_query(name, insert_string)
sqlm_sync(tuple_Threaded_Query)
log_sql("Threaded query: %s", insert_string)
SQL_ThreadQuery(get_tuple(name), "sqlm_simple_stub", insert_string, name, sizeof(name)) }
set_tuple(old_tuple) return PLUGIN_HANDLED }
/*----------------------------------------------------------------------------- Callback threaded query -----------------------------------------------------------------------------*/ //sqlm_thread_query(const tuple[SQL_NAME], const func[], const query_string[], data) public _sqlm_thread_query(plugin_id, param_count) { static name[SQL_NAME], func[SQL_NAME], query_string[SQL_QUERY], l_data[2] get_string(1, tmp_thread[th_tuple_name], charsmax(tmp_thread[th_tuple_name])) scopy(name, tmp_thread[th_tuple_name])
static retvalue retvalue = 0
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(name)) {
tmp_thread[th_plugin_id] = plugin_id get_string(2, func, charsmax(func)) tmp_thread[th_func_id] = get_func_id(func, plugin_id) get_string(3, query_string, charsmax(query_string)) tmp_thread[th_data] = get_param(4) l_data[0] = ArraySize(gThreads) ArrayPushArray(gThreads, tmp_thread)
sqlm_sync(tuple_Threaded_Query)
retvalue = SQL_ThreadQuery(get_tuple(name), "sqlm_callback", query_string, l_data, sizeof(l_data)) }
set_tuple(old_tuple)
return retvalue }
public sqlm_callback(failstate, Handle:query, err_str[], err_num, data[], size, Float:queuetime) { if (gThreads && !ArrayGetArray(gThreads, data[0], tmp_thread)) { log_sql("ERROR: Data error!") return PLUGIN_CONTINUE }
new old_tuple[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name]) static name[SQL_NAME] scopy(name, tmp_thread[th_tuple_name])
if (!set_tuple(name)) { log_sql("ERROR: Tuple name error! --> Error code: %d (Message: ^"%s^")", err_num, err_str) return PLUGIN_CONTINUE }
static querystring[SQL_QUERY] SQL_GetQueryString(query, querystring, charsmax(querystring))
#if defined SQL_TRACE log_sql("Resolved query %d of tuple '%s', took %f seconds", query, name, queuetime) log_sql("--> Original query: %s", querystring) #endif
store_tuple(name, query)
if (failstate) { store_error(query) if (failstate == TQUERY_CONNECT_FAILED) { clr_bits(cur_SQLTuple[tuple_flags], FLAG_CONNECTED) log_sql("ERROR: Connection failed!") if (querystring[0] == EOS) SQL_GetQueryString(query, querystring, charsmax(querystring)) if (sqlm_reconnect()) SQL_ThreadQuery(get_tuple(name), "sqlm_callback", querystring, data, size) } else if (failstate == TQUERY_QUERY_FAILED) log_sql("ERROR: Query failed!") log_sql("--> Error code: %d (Message: ^"%s^")", err_num, err_str) } else { ArrayGetArray(gThreads, data[0], tmp_thread)
if (callfunc_begin_i(tmp_thread[th_func_id], tmp_thread[th_plugin_id]) == 1) { callfunc_push_int(failstate) callfunc_push_int(fixed:query) callfunc_push_str(err_str) callfunc_push_int(err_num) callfunc_push_int(tmp_thread[th_data]) callfunc_push_float(queuetime) callfunc_end() }
}
set_tuple(old_tuple)
return PLUGIN_HANDLED }
/*===================================================================================
FUNCTIONS for QUERY Handle
=====================================================================================*/
// SQL_AffectedRows // sqlm_affected_rows(Handle:query) public _sqlm_affected_rows(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0 scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) retvalue = SQL_AffectedRows(query)
set_tuple(old_tuple) return retvalue }
// SQL_FieldNameToNum // sqlm__field_name_to_num(Handle:query, const name[]) public _sqlm_field_name_to_num(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0, name[SQL_NAME] get_string(2, name, charsmax(name)) scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) retvalue = SQL_FieldNameToNum(query, name)
set_tuple(old_tuple) return retvalue }
// SQL_FieldNumToName // sqlm__field_num_to_name(Handle:query, num, name[], maxlength) public _sqlm_field_num_to_name(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0, name[SQL_NAME] scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query)) && (retvalue = SQL_FieldNumToName(query, get_param(2), name, charsmax(name)))) set_string(3, name, get_param(4))
set_tuple(old_tuple) return retvalue }
// SQL_FreeHandle // sqlm_free_handle(Handle:query) public _sqlm_free_handle(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0 scopy(old_tuple, cur_SQLTuple[tuple_name])
set_tuple(find_tuple(query)) retvalue = SQL_FreeHandle(query)
set_tuple(old_tuple) return retvalue }
// SQL_GetInsertId // sqlm_get_insert_id(Handle:query) public _sqlm_get_insert_id(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0 scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) retvalue = SQL_GetInsertId(query)
set_tuple(old_tuple) return retvalue }
// SQL_GetQueryString // sqlm_get_query_string(Handle:query, buffer[], maxlength) public _sqlm_get_query_string(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0, buffer[SQL_QUERY] scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) { retvalue = SQL_GetQueryString(query, buffer, charsmax(buffer)) set_string(2, buffer, get_param(3)) }
set_tuple(old_tuple) return retvalue }
// SQL_IsNull // sqlm_is_null(Handle:query, column) public _sqlm_is_null(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0 scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) retvalue = SQL_IsNull(query, get_param(2))
set_tuple(old_tuple) return retvalue }
// SQL_MoreResults // sqlm_more_results(Handle:query) public _sqlm_more_results(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0 scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) retvalue = SQL_MoreResults(query)
set_tuple(old_tuple) return retvalue }
// SQL_NextRow // sqlm_next_row(Handle:query) public _sqlm_next_row(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0 scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) { SQL_NextRow(query) retvalue = SQL_MoreResults(query) }
set_tuple(old_tuple) return retvalue }
// SQL_NumColumns // sqlm_num_columns(Handle:query) public _sqlm_num_columns(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0 scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) retvalue = SQL_NumColumns(query)
set_tuple(old_tuple) return retvalue }
// SQL_NumResults // sqlm_num_results(Handle:query) public _sqlm_num_results(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0 scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) retvalue = SQL_NumResults(query)
set_tuple(old_tuple) return retvalue }
// SQL_QueryError // sqlm_query_error(Handle:query, error[], maxlength) public _sqlm_query_error(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0, buffer[SQL_QUERY] scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) { retvalue = SQL_QueryError(query, buffer, charsmax(buffer)) set_string(2, buffer, get_param(3)) }
set_tuple(old_tuple) return retvalue }
// SQL_ReadResult // sqlm_read_result(Handle:query, column[], {Float,_}:...) public _sqlm_read_result(plugin_id, param_count) { new old_tuple[SQL_NAME], Handle:query = Handle:get_param(1), retvalue = 0, buffer[SQL_QUERY], Float:flt scopy(old_tuple, cur_SQLTuple[tuple_name])
if (set_tuple(find_tuple(query))) { switch (param_count) { case 2: retvalue = SQL_ReadResult(query, get_param(2)) case 3: { retvalue = SQL_ReadResult(query, get_param(2), flt) set_float_byref(3, flt) } case 4: { retvalue = SQL_ReadResult(query, get_param(2), buffer, charsmax(buffer)) set_string(3, buffer, get_param(4)) } } }
set_tuple(old_tuple) return retvalue }
[/quote]
As you can see the code is intact.
[QUOTE]// The last node has a value which's the command to be executed // This won't work with commandmenu.txt (that's not a vdf file)
"Tuples" { "default" { "type" "mysql" "host" "dbmy0024.whservidor.com" "db" "impactgami1_7" "user" "impactgami1_7" "pass" "ma******" "timeout" "0.000" "autoconnect" "1" } }
|