Skip to main content
Topic: db classes (Read 1217 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

db classes

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);
}
 emanuele 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?
Bugs creator.
Features destroyer.
Template killer.

Re: db classes

Reply #1
That seems like a reasonable approach to clean those functions up, after all it was quick and dirty :D

 

Re: db classes

Reply #2
I'm pretty sure I did that as well.
Bugs creator.
Features destroyer.
Template killer.