
public mysql_init()
{
	static host[64],user[32],password[32],database[128],error_num;

	get_pcvar_string(cvar_sql_host,host,63);
	get_pcvar_string(cvar_sql_user,user,31);
	get_pcvar_string(cvar_sql_password,password,31);
	get_pcvar_string(cvar_sql_database,database,127);
	
	g_sql_tuple = SQL_MakeDbTuple(host,user,password,database);
	g_sql_connect = SQL_Connect(g_sql_tuple,error_num,g_sql_error,255);

	if(g_sql_connect == Empty_Handle)
	{
		g_sql_available = false;
		server_print(g_sql_error);
		return PLUGIN_CONTINUE;
	}
	
	g_sql_available = true;
	
	new sql_path[64] = "%s/sql/bleach.sql";
	format(sql_path,63,sql_path,amxx_data_dir);
	if(file_exists(sql_path))
	{
		new sql_table[512],line[256],len,len2,i = 0;
		
		while(read_file(sql_path,i++,line,255,len))
		{
			read_file(sql_path,i,line,255,len);
			len2 += formatex(sql_table[len2],511-len2,line);
		}
		
		g_sql_query = SQL_PrepareQuery(g_sql_connect,sql_table);
		
		if (!SQL_Execute(g_sql_query))
		{
			database_error(g_sql_query);
		}

		SQL_FreeHandle(g_sql_query);
	}

	
	return PLUGIN_CONTINUE;
}

public mysql_close()
{
	if(g_sql_tuple)
	{
		SQL_FreeHandle(g_sql_tuple);
	}

	if(g_sql_connect)
	{
		SQL_FreeHandle(g_sql_connect);
	}
}

public mysql_save(id)
{
	if(!g_sql_available) return PLUGIN_CONTINUE;
	
	static authid[33];
	get_user_authid(id,authid,32);
	
	//Select Character
	format(g_sql_string,255,"SELECT * FROM `%s` WHERE authid='%s' AND class=%d",g_sql_table,authid,g_player_class[id]);
	g_sql_query = SQL_PrepareQuery(g_sql_connect,g_sql_string);
	if(!SQL_Execute(g_sql_query))
	{
		database_error(g_sql_query);
	}
	
	new found;
	if(SQL_MoreResults(g_sql_query))
	{
		found=SQL_ReadResult(g_sql_query,0);
	}
	SQL_FreeHandle(g_sql_query);
	
	//Update/Insert Character
	if(found)
	{
		format(g_sql_string,255,"UPDATE `%s` SET experience=%d,level=%d,attributepoints=%d,skillpoints=%d,spirit=%d WHERE authid='%s' AND class=%d",
		g_sql_table,g_player_experience[id],g_player_level[id],g_player_attributepoints[id],g_player_skillpoints[id],g_player_spirit[id],authid,g_player_class[id]);
	}	
	else
	{
		format(g_sql_string,255,"INSERT into `%s` (`authid`,`class`,`experience`,`level`,`attributepoints`,`skillpoints`,`spirit`) values ('%s','%d','%d','%d','%d','%d','%d')",
		g_sql_table,authid,g_player_class[id],g_player_experience[id],g_player_level[id],g_player_attributepoints[id],g_player_skillpoints[id],g_player_spirit[id]);
	}
	
	g_sql_query = SQL_PrepareQuery(g_sql_connect,g_sql_string);
	if(!SQL_Execute(g_sql_query))
	{
		database_error(g_sql_query);
	}
	
	SQL_FreeHandle(g_sql_query);
	
	//Update Attributes
	format(g_sql_string,255,"UPDATE `%s` SET athletic='%f',acrobatic='%f',endurance='%d' WHERE authid='%s' AND class=%d",g_sql_table,g_player_athletic[id],g_player_acrobatic[id],g_player_endurance[id],authid,g_player_class[id]);
	g_sql_query = SQL_PrepareQuery(g_sql_connect,g_sql_string);
	if(!SQL_Execute(g_sql_query))
	{
		database_error(g_sql_query);
	}
	SQL_FreeHandle(g_sql_query);
	
	//Update Skills
	for (new i = 0; i < BLEACH_MAX_SKILLS; i++)
	{
		format(g_sql_string,255,"UPDATE `%s` SET skill_%d=%d WHERE authid='%s' AND class=%d",g_sql_table,i,g_skill[id][i],authid,g_player_class[id]);
		g_sql_query = SQL_PrepareQuery(g_sql_connect,g_sql_string);	
		if(!SQL_Execute(g_sql_query))
		{
			database_error(g_sql_query);
		}
		SQL_FreeHandle(g_sql_query);
	}
	
	return PLUGIN_CONTINUE;
}

public mysql_load(id)
{
	if(!g_sql_available) return PLUGIN_CONTINUE;
	
	static authid[33];
	get_user_authid(id,authid,32);
	
	format(g_sql_string,255,"SELECT * FROM `%s` WHERE authid='%s' AND class=%d",g_sql_table,authid,g_player_class[id]);
	g_sql_query = SQL_PrepareQuery(g_sql_connect,g_sql_string);
	if(!SQL_Execute(g_sql_query))
	{
		database_error(g_sql_query);
	}
	
	new found;
	if(SQL_MoreResults(g_sql_query))
	{
		found=SQL_ReadResult(g_sql_query,0);
	}
	
	if(found)
	{
		g_player_experience[id]=SQL_ReadResult(g_sql_query,3);
		g_player_level[id]=SQL_ReadResult(g_sql_query,4);
		g_player_attributepoints[id]=SQL_ReadResult(g_sql_query,5);
		g_player_skillpoints[id]=SQL_ReadResult(g_sql_query,6);
		g_player_spirit[id]=SQL_ReadResult(g_sql_query,7);

		SQL_ReadResult(g_sql_query,8,g_player_athletic[id]);
		SQL_ReadResult(g_sql_query,9,g_player_acrobatic[id]);
		
		g_player_endurance[id]=SQL_ReadResult(g_sql_query,10);
	
		g_skill[id][P_SKILL_1]=SQL_ReadResult(g_sql_query,11);
		g_skill[id][P_SKILL_2]=SQL_ReadResult(g_sql_query,12);
		g_skill[id][P_SKILL_3]=SQL_ReadResult(g_sql_query,13);
		g_skill[id][P_SKILL_4]=SQL_ReadResult(g_sql_query,14);
		g_skill[id][P_SKILL_5]=SQL_ReadResult(g_sql_query,15);
		g_skill[id][P_SKILL_6]=SQL_ReadResult(g_sql_query,16);
	}
	else
	{
		reset_player(id);
		reset_player_attributes(id);
		reset_player_skills(id);
		restore_cooldowns(id);
		restore_player(id);
	}
	
	SQL_FreeHandle(g_sql_query);
	
	return PLUGIN_CONTINUE;
}

public mysql_truncate()
{
	if(!g_sql_available) return PLUGIN_CONTINUE;
	format(g_sql_string,255,"TRUNCATE TABLE %s",g_sql_table);
	g_sql_query = SQL_PrepareQuery(g_sql_connect,g_sql_string);
	
	if(!SQL_Execute(g_sql_query))
	{
		database_error(g_sql_query);
	}
	SQL_FreeHandle(g_sql_query);
	
	return PLUGIN_CONTINUE;
}
