ElkArte Community

Elk Development => Feature Discussion => Exterminated Features => Topic started by: emanuele on January 10, 2014, 10:28:02 am

Title: db classes
Post by: emanuele on January 10, 2014, 10:28:02 am
In all the functions that instantiate/return a database-related class/object we have things like:
Code: [Select]
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')
{
require_once(SOURCEDIR . '/database/Db.php');
require_once(SOURCEDIR . '/database/Db-' . $db_type . '.class.php');

// quick 'n dirty initialization of the right database class.
if ($db_type == 'mysql')
return Database_MySQL::initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options);
elseif ($db_type == 'postgresql')
return Database_PostgreSQL::initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options);
elseif ($db_type == 'sqlite')
return Database_SQLite::initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options);
}
/me doesn't like it.

Assuming you are not going to have two different kind of DBMS running for the same application (and anyway it would be kind of difficult at the moment using our own functions) one way would be to define a constants in the Db-' . $db_type . '.class.php class file, something like:

Code: [Select]
if (!defined('DB_TYPE'))
define('DB_TYPE', 'MySQL');
And then change the function to something like:
Code: [Select]
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')
{
require_once(SOURCEDIR . '/database/Db.php');
require_once(SOURCEDIR . '/database/Db-' . $db_type . '.class.php');

return call_user_func_array(array('Database_' . DB_TYPE, 'initiate'), array($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options));
}

Is there any better way to do it?
Title: Re: db classes
Post by: Spuds on January 10, 2014, 08:03:09 pm
That seems like a reasonable approach to clean those functions up, after all it was quick and dirty :D
Title: Re: db classes
Post by: emanuele on February 01, 2014, 04:07:41 am
I'm pretty sure I did that as well.