
public database_error(Handle:query)
{
	SQL_QueryError(query,g_sql_error,255);
	server_print("[DB - query]: %s",g_sql_string);
	server_print("[DB - error]: %s",g_sql_error);
}

//---------------------------------------------------------------------

#include "vault.inl"
#include "database\mysql.inl"
#include "database\sqlite.inl"

//---------------------------------------------------------------------

public database_init()
{
	if(g_database_initialized) return PLUGIN_CONTINUE;
	
	if(get_pcvar_num(cvar_savexp) == 2)
	{
		get_pcvar_string(cvar_sql_table,g_sql_table,63);
		switch(get_pcvar_num(cvar_sql_type))
		{
			case DB_MYSQL:	mysql_init();
			case DB_SQLITE:	sqlite_init();
		}
		
		g_database_initialized=true;
	}
	
	return PLUGIN_CONTINUE;
}

public database_close()
{
	if(!g_database_initialized) return PLUGIN_CONTINUE;
	else
	{
		g_database_initialized=false;
	}
	
	switch(get_pcvar_num(cvar_sql_type))
	{
		case DB_MYSQL:	mysql_close();
		case DB_SQLITE:	mysql_close();
	}
	
	return PLUGIN_CONTINUE;
}

public database_save(id)
{
	if(!g_database_initialized) return PLUGIN_CONTINUE;
	
	switch(get_pcvar_num(cvar_sql_type))
	{
		case DB_MYSQL:	mysql_save(id);
		case DB_SQLITE:	mysql_save(id);
	}
	
	return PLUGIN_CONTINUE;
}

public database_load(id)
{
	if(!g_database_initialized) return PLUGIN_CONTINUE;
	
	switch(get_pcvar_num(cvar_sql_type))
	{
		case DB_MYSQL:	mysql_load(id);
		case DB_SQLITE:	mysql_load(id);
	}
	
	return PLUGIN_CONTINUE;
}

public database_truncate()
{
	if(!g_database_initialized) return PLUGIN_CONTINUE;
	
	switch(get_pcvar_num(cvar_sql_type))
	{
		case DB_MYSQL:	mysql_truncate();
		case DB_SQLITE:	mysql_truncate();
	}
	
	return PLUGIN_CONTINUE;
}

//---------------------------------------------------------------------

public save_player(id)
{
	if(g_player_class[id] == 0 ||  is_user_bot(id)) return PLUGIN_CONTINUE;
	

	switch(get_pcvar_num(cvar_savexp))
	{
		case SAVE_VAULT:	vault_save(id);
		case SAVE_DB:		database_save(id);
	}

	return PLUGIN_CONTINUE;
}

public load_player(id)
{
	if(is_user_bot(id)) return PLUGIN_CONTINUE;
	
	switch(get_pcvar_num(cvar_savexp))
	{
		case SAVE_VAULT:	vault_load(id);
		case SAVE_DB:		database_load(id);
	}
	
	return PLUGIN_CONTINUE;
}

public save_all_players()
{
	if(get_pcvar_num(cvar_savexp) == 0) return PLUGIN_CONTINUE;
	
	new players[MAXPLAYERS];
	static num;
	
	get_players(players,num,"ch");
	for (new i; i < num; ++i)
	{
		save_player(players[i]);
	}
	
	return PLUGIN_CONTINUE;
}
