
/* AMX Mod script.
*
* (c) Copyright 2002, Noplay
* This file is provided as is (no warranties).
*
*/

/*

This AMX plugin requires MySQL MetaSmall module.

For this to work, you might create your MySQL admins table with this SQL query :

  CREATE TABLE cvars (
    cvar varchar(32) NOT NULL default '',
    value varchar(32) NOT NULL default ''
  ) TYPE=MyISAM;


Notes :
. check $moddir/addons/amx/mysql.cfg for MySQL access configuration

*/

#include <amxmod>
#include <mysql>

public cvar_sql()
{
	new mysql,cnt
	new cvar[33]
	new value[33]
	new error[257]
	new host[65]
	new user[33]
	new pass[33]
	new db[33]
	
	get_cvar_string("amx_mysql_host",host,64)
	get_cvar_string("amx_mysql_user",user,32)
	get_cvar_string("amx_mysql_pass",pass,32)
	get_cvar_string("amx_mysql_db",db,32)

	mysql = mysql_connect(host,user,pass,db,error,256)
	if(mysql>0)
	{
		if(mysql_query(mysql,"SELECT cvar,value FROM cvars")>0)
		{
			cnt = 0
			while(mysql_nextrow(mysql)>0)
			{
				mysql_getfield(mysql,1,cvar,32)
				mysql_getfield(mysql,2,value,32)
				server_cmd("%s ^"%s^"",cvar,value)
				
				cnt++
			}
			server_print("[AMX] Loaded %d cvar(s) from database",cnt)
		}
		else
		{
			mysql_error(mysql,error,32)
			server_print("MySQL error : could not load cvar : '%s'",error)
		}
		mysql_close(mysql)
	}
	else
	{
		server_print("MySQL error : could not connect : '%s'",error)
	}
	
	return PLUGIN_HANDLED
}

public plugin_init()
{
	register_plugin("Cvars from MySQL","0.1","Noplay")
	register_srvcmd("amx_sqlcvars","cvar_sql")
	register_cvar("amx_mysql_host","127.0.0.1")
	register_cvar("amx_mysql_user","root")
	register_cvar("amx_mysql_pass","")
	register_cvar("amx_mysql_db","amx")
	server_cmd("exec addons/amx/mysql.cfg;amx_sqlcvars")
	return PLUGIN_CONTINUE
}

