Very basic question, I am making functions that will generate queries to database. I have two approaches to this and I don't know which is better.
First is making one bigger function to generate any query I need, so basically:
function generateQuery($object, $queryType, $tableName, $conditions = null) {
//Then in here some more complex code that generates query,
//some of stuff like double if shorthands etc, for example:
$string = ($queryType === "INSERT") ? "INSERT INTO {$table} (" : (($queryType === "UPDATE") ? "UPDATE {$table} SET" : "SELECT");
//So all the code for generating INSERT, UPDATE and SELECT in one function
//full of conditions.
}
Or I could just write seperate function for each type of query.
In the end, the first solution is not that complicated at all, and the second one would make some repetition in code, but still it does feel kind of wrong.
Then again I want to use PDO transactions later, so I will have a function like so:
function launchQueries($list) {
$this->handler->beginTransaction();
for($i = 0; $i < count($list); $i++)
{
$this ->launchQuery($list[i]->object, $list[i]->$table, $list[i]->queryType, $list[i]->conditions);
}
$this->handler->commit();
}
Or something like that, not sure this exact code would work, since I haven't made it yet.
Anyway, with query generation in one function I can just pass queries in list and do one loop to generate and execute them all. But it still feels wrong. Is it done like this at all, or am I missing different, better solutions?