/*
*	Created:			23.01.10
*	Author:				009
*	Last Modifed:		-
*	Description:		Нативы плагина mysql и мини функции 
*/

#if defined _mode_mysql_included
	#endinput
#endif

#define _mode_mysql_included
#pragma library mode_mysql

// --------------------------------------------------
// includes
// --------------------------------------------------
#include "mode_debug"
#include "mode_core"
#include "mode_files"

// --------------------------------------------------
// natives (plugin's)
// --------------------------------------------------
native samp_mysql_connect(server[], user[], password[]);
native samp_mysql_select_db(db[]);
native samp_mysql_query(query[]);
native samp_mysql_store_result();
native samp_mysql_fetch_row(line[]);
native samp_mysql_get_field(field[],value[]);
native samp_mysql_num_rows();
native samp_mysql_num_fields();
native samp_mysql_ping();
native samp_mysql_real_escape_string(src[], dest[]);
native samp_mysql_free_result();
native samp_mysql_strtok(dest[], separator[], src[]);
native samp_mysql_close();

// --------------------------------------------------
// enums
// --------------------------------------------------
enum MysqlConfigInfo {
	MYSQL_HOST,
	MYSQL_USER,
	MYSQL_PASS,
	MYSQL_DB
};

// --------------------------------------------------
// news
// --------------------------------------------------
new MySQL_Config[MysqlConfigInfo][MAX_STRING],
	query[MYSQL_MAX_QUERY],
	result[MYSQL_MAX_RESULT];

// --------------------------------------------------
// natives list (for pawno functions list)
// --------------------------------------------------
/*
native MySQL_GetMinimalFreeSlot(row[],table[]);
*/

// --------------------------------------------------
// stocks
// --------------------------------------------------
stock MySQL_GetMinimalFreeSlot(row[],table[])
{
	new old,
		now;
	format(query,sizeof(query),"SELECT `%s` FROM `%s` ORDER BY `%s` ASC",row,table,row);
	samp_mysql_query(query);
	samp_mysql_store_result();
	while(samp_mysql_fetch_row(result))
	{
		old = now;
		now = strval(result);
		if((now - old) > 1) return old;
	}
	return now;
}

// --------------------------------------------------
// Obligatory functions
// --------------------------------------------------
ModeMysql_OnGameModeInit()
{
	Debug(DEBUG_START,"ModeMysql_OnGameModeInit");
	if(!fexist(SETTINGS_FILE)) 
	{
		print("[ERROR] Mode mysql -> ModeMysql_OnGameModeInit (settings file not found)");
		Debug(DEBUG_END,"ModeMysql_OnGameModeInit (reason: error)");
		return 0;
	}
	Debug(DEBUG_ACTION,"read settings");
	new tmp[MAX_STRING],
		File:cfg_file = fopen(SETTINGS_FILE,io_read),
		pos;
	while(fread(cfg_file,tmp))
	{
		StripNL(tmp);
		if((pos = strfind(tmp,"=")) == -1) continue;	
		ReadFileDataStr(tmp,"MySQL host",pos,MySQL_Config[MYSQL_HOST]);
		ReadFileDataStr(tmp,"MySQL user",pos,MySQL_Config[MYSQL_USER]);
		ReadFileDataStr(tmp,"MySQL password",pos,MySQL_Config[MYSQL_PASS]);
		ReadFileDataStr(tmp,"MySQL base",pos,MySQL_Config[MYSQL_DB]);
	}
	Debug(DEBUG_ACTION,"check settings");
	for(new i = 0;i < MYSQL_CONFIG_SIZE;i++)
	{
		if(!MySQL_Config[i][0])
		{
			print("[ERROR] Mode mysql -> ModeMysql_OnGameModeInit (settings null)");
			Debug(DEBUG_END,"ModeMysql_OnGameModeInit (reason: error)");
			return 0;
		}
	}
	Debug(DEBUG_ACTION,"connect to base");
	samp_mysql_connect(MySQL_Config[MYSQL_HOST],MySQL_Config[MYSQL_USER],MySQL_Config[MYSQL_PASS]);
	samp_mysql_select_db(MySQL_Config[MYSQL_DB]);
	samp_mysql_query("SET NAMES cp1251");
	print("Mode mysql system loaded.");
	Debug(DEBUG_END,"ModeMysql_OnGameModeInit (reason: complete)");
	return 1;
}