/*
	PostNuke MySQL
	Date: 24.07.2011
	Author: NeoPhoenix
*/
#if defined included_pnwl_mysql
	#endinput
#else
	#define included_pnwl_mysql
#endif
#include <a_samp>
#include <a_mysql>
#define mysql_ mysql_
#define mysql_query_once(%0) mysql_query(%0); mysql_free_result()
#define MAX_MYSQL_CONNECTION_TRIES	(5)

new query[256],
	DB:db,
	DBResult:db_result;
/*
	native Connect_To_Database(mysql_Host, mysql_User, Datenbank, Passwort);
	native mysql_GetStringByString(Table[], Field[], Where[], Where2[]);
	native mysql_GetStringByInt(Table[], Field[], Where[], Where2);
	native mysql_GetIntByString(Table[], Field[], Where[], Where2[]);
	native mysql_GetIntByInt(Table[], Field[], Where[], Where2);
	native Float:mysql_GetFloatByInt(Table[], Field[], Where[], Where2);
	native Float:mysql_GetFloatByString(Table[], Field[], Where[], Where2[]);
	native mysql_CountDB(Tabelle[]);
	native mysql_GetMaxInt(Field[], Table[]);
	native mysql_SetIntByString(Table[], Field[], To, Where[], Where2[]);
	native mysql_SetIntByInt(Table[], Field[], To, Where[], Where2);
	native mysql_SetStringByString(Table[], Field[], To[], Where[], Where2[]);
	native mysql_SetStringByInt(Table[], Field[], To[], Where[], Where2);
	native mysql_SetFloatByInt(Table[], Field[], Float:To, Where[], Where2);
	native mysql_SetFloatByString(Table[], Field[], Float:To, Where[], Where2[]);
*/

stock db_query_once(qstr[])
{
	new DBResult:res = db_query(db,qstr);
	db_free_result(res);
	return ;
}

//public OnqueryFinish(_query[], resultid, extraid, connectionHandle)
public OnQueryError(errorid, error[], resultid, extraid, callback[], _query[], connectionHandle)
{
	new date[3],time[3];
	getdate(date[2],date[1],date[0]);
	gettime(time[2],time[1],time[0]);
	format(string,sizeof(string),"[%d-%d-%d, %d:%0.2d-%d] Error \"%s\" (ID %d, res %d, ext %d, hand %d) in query (\"%s\", Callback \"%s\")\r\n",date[0],date[1],date[2],time[2],time[1],time[0],error,errorid,resultid,extraid,connectionHandle,_query,callback);
	print(string);
	new File:errorfile=fopen(#ERROR_PATH"/mysql.txt",io_append);
	fwrite(errorfile,string);
	fclose(errorfile);
	switch(errorid)
	{
		case 2006:
		{
		}
		case 2013:
		{//Lost connection to MySQL server during query
			CORE_OnMySQLInit(MYSQL_CONNECTION_FILE);
			mysql_query(_query);//Sending query again
		}
	}
	return 1;
}

Connect_To_Database(MySql_Host[], MySql_User[], MySql_Database[], MySql_Password[])
{
	mysql_connect(MySql_Host, MySql_User, MySql_Database, MySql_Password);
	new tries=0;
	while(++tries <= MAX_MYSQL_CONNECTION_TRIES)
	{
		if(mysql_ping() == 1)
		{
			printf("::CORE (MySQL-Verbindung wurde nach dem %d versuch hergestellt)",tries);
			return true;
		}
		else
		{
			printf("::CORE (MySQL-Verbindungsversuch %d gescheitert)",tries);
		}
	}
	printf("::CORE MySQL-Verbindung konnte nicht hergestellt werden. Server wird heruntergefahren");
	SendRconCommand("exit");	
	return 1;
}

stock mysql_GetStringByString(Table[], Field[], Where[], Where2[])
{
	format(query,sizeof(query), "SELECT %s FROM %s WHERE %s='%s'", Field, Table, Where, Where2);
	mysql_query(query);
	mysql_store_result();
	format(query,sizeof(query),"");
	mysql_fetch_row(query);
	mysql_free_result();
	return query;
}
stock mysql_GetStringByInt(Table[], Field[], Where[], Where2)
{
	format(query,sizeof(query), "SELECT %s FROM %s WHERE %s='%d'", Field, Table, Where, Where2);
	mysql_query(query);
	mysql_store_result();
	format(query,sizeof(query),"");
	mysql_fetch_row(query);
	mysql_free_result();
	return query;
}
stock mysql_GetIntByString(Table[], Field[], Where[], Where2[])
{
	format(query,sizeof(query), "SELECT %s FROM %s WHERE %s='%s'", Field, Table, Where, Where2);
	mysql_query(query);
	mysql_store_result();
	format(query,sizeof(query),"");
	mysql_fetch_row(query);
	new var = strval(query);
	mysql_free_result();
	return var;
}
stock mysql_GetIntByInt(Table[], Field[], Where[], Where2)
{
	format(query,sizeof(query), "SELECT %s FROM %s WHERE %s='%d'", Field, Table, Where, Where2);
	mysql_query(query);
	mysql_store_result();
	format(query,sizeof(query),"");
	mysql_fetch_row(query);
	new var=strval(query);
	mysql_free_result();
	return var;
}
stock Float:mysql_GetFloatByInt(Table[], Field[], Where[], Where2)
{
	format(query,sizeof(query), "SELECT %s FROM %s WHERE %s='%d'", Field, Table, Where, Where2);
	mysql_query(query);
	mysql_store_result();
	format(query,sizeof(query),"");
	mysql_fetch_row(query);
	new Float:var=floatstr(query);
	mysql_free_result();
	return var;
}
stock Float:mysql_GetFloatByString(Table[], Field[], Where[], Where2[])
{
	format(query,sizeof(query), "SELECT %s FROM %s WHERE %s='%s'", Field, Table, Where, Where2);
	mysql_query(query);
	mysql_store_result();
	format(query,sizeof(query),"");
	mysql_fetch_row(query);
	new Float:var=floatstr(query);
	mysql_free_result();
	return var;
}
stock mysql_CountDB(Tabelle[])
{
	new Rows;
	format(query, sizeof(query), "SELECT * FROM `%s`",Tabelle);
	mysql_query(query);
	format(query,sizeof(query),"");
	mysql_store_result();
	Rows=mysql_NumRows();
	mysql_free_result();
	return Rows;
}
stock mysql_GetMaxInt(Field[], Table[])
{
	format(query,sizeof(query), "SELECT MAX(%s) FROM %s", Field, Table);
	mysql_query(query);
	mysql_store_result();
	format(query,sizeof(query),"");
	mysql_fetch_row(query);
	new var=strval(query);
	mysql_free_result();
	return strval(var);
}
stock mysql_SetIntByString(Table[], Field[], To, Where[], Where2[])
{
	format(query,sizeof(query), "UPDATE %s SET %s='%d' WHERE %s='%s'", Table, Field, To, Where, Where2);
	mysql_query(query);
	format(query,sizeof(query),"");
	mysql_free_result();
	return true;
}
stock mysql_SetIntByInt(Table[], Field[], To, Where[], Where2)
{
	format(query,sizeof(query), "UPDATE %s SET %s='%d' WHERE %s='%d'", Table, Field, To, Where, Where2);
	mysql_query(query);
	format(query,sizeof(query),"");
	mysql_free_result();
	return true;
}
stock mysql_SetFloatByInt(Table[], Field[], Float:To, Where[], Where2)
{
	format(query,sizeof(query), "UPDATE %s SET %s='%f' WHERE %s='%d'", Table, Field, To, Where, Where2);
	mysql_query(query);
	format(query,sizeof(query),"");
	mysql_free_result();
	return true;
}
stock mysql_SetFloatByString(Table[], Field[], Float:To, Where[], Where2[])
{
	format(query,sizeof(query), "UPDATE %s SET %s='%f' WHERE %s='%s'", Table, Field, To, Where, Where2);
	mysql_query(query);
	format(query,sizeof(query),"");
	mysql_free_result();
	return true;
}
stock mysql_SetStringByString(Table[], Field[], To[], Where[], Where2[])
{
	format(query,sizeof(query), "UPDATE %s SET %s='%s' WHERE %s='%s'", Table, Field, To, Where, Where2);
	mysql_query(query);
	format(query,sizeof(query),"");
	mysql_free_result();
	return true;
}
stock mysql_SetStringByInt(Table[], Field[], To[], Where[], Where2)
{
	format(query,sizeof(query), "UPDATE %s SET %s='%s' WHERE %s='%d'", Table, Field, To, Where, Where2);
	mysql_query(query);
	format(query,sizeof(query),"");
	mysql_free_result();
	return true;
}
stock mysql_GetResultAsInt(qstr[])
{
	mysql_query(qstr);
	mysql_store_result();
	format(query,sizeof(query),"");
	mysql_fetch_row(query);
	new var = strval(query);
	mysql_free_result();
	return var;
}
stock mysql_GetResult(qstr[])
{
	mysql_query(qstr);
	mysql_store_result();
	format(query,sizeof(query),"");
	mysql_fetch_row(query);
	mysql_free_result();
	return query;
}