I made a small Database Handling class, which has been extremely helpful. I'm also growing it to make it more easy to abstract (i.e. add other database systems), and more powerful (i.e. more queries generated).
It goes like this:
Code: Select all
$insert = new Insert('table', $database_handler); # The database handler is optional. If a database handler is found, and if it is connected to a database, there is no need to provide the database handler, just like PHP does for the optional link identifier on MySQL. The database handler also tells the Insert class what the current database is, so the Insert class can find a MySQL Insert or a SQLite Insert, or whatever insert is needed.
$insert->field('FIELD','VALUE');
if ($insert->execute()) {
# Query was executed properly. Continue.
} else {
# Query failed.
$insert->error; # Contains the error message given by the database.
}
# You can also use method chaining.
if ($insert->field('FIELD','VALUE')->execute()) {
# Query was executed properly. Continue. You can also execute more queries with the same object instance, if you want to, to save on performance.
}
So, what is your view on SQL code generation? Currently, they work properly and have been a blessing (in my opinion). It's also less error-prone than writing my own SQL.
I am trying to make the move to PDO, and just generate the SQL code for the PDO Statements. It's just an idea, but I think it's better to use an already made and tested database abstraction class than try to write my own.