I've got an old project that fetch data this stupid way:
$db = new Database($server, $name, $pass);
$where = 'country = "UK"';
if ($_GET['gender']) { $where .= ' AND gender = "' . $_GET['gender'] . '"'; }
$sql = "SELECT * FROM users WHERE $where ORDER BY name";
$users = $db->fetchAll($sql);
I would like to improve it with an SQL builder (on other projects I use Zend's DB Tables), but problem is, I need to keep the Database
class because it's fetch*()
methods does more than just loading data (e.g. debugging, performance, statistics, etc.).
I have looked at Zend_Db_Table
and this post but all these query builders are attached to a DB connector with own fetch()
or execute()
methods. But I would need a builder that just creates SQL query that can be used in existing method:
$query = new MySqlQuery();
$query
->select('*')
->from('users')
->orderBy('name')
->where('country', 'UK');
if ($_GET['gender']) {
$query->where('gender', $_GET['gender']);
}
$sql = $query->toString();
$db = new Database($server, $name, $pass);
$users = $db->fetchAll($sql);
I was thinking about simply stealing the Zend classes and rewriting them so they don't need the DB connection, but I would like to see if these is something already done.