1

I have built a PDO adapter class because I thought, at the time anyway, it would be a good idea. After fighting with it, it makes no sense to me. Isn't the design of PDO the way it is to keep you from having to create special adapters for a given database?

For a connection, I understand, but I seem to be replacing PDO with my own version of PDO.

I do not understand why I would need this extra layer of abstraction.

Here are some examples:

public function prepare($sql, array $options = array()) {
    $this->connect();
    try {
        $this->statement = $this->connection->prepare($sql, 
            $options);
        return $this;
    }
    catch (PDOException $e) {
        throw new RunTimeException($e->getMessage());
    }
}

public function execute(array $parameters = array()) {
    try {
        $this->getStatement()->execute($parameters);
        return $this;
    }
    catch (PDOException $e) {
        throw new RunTimeException($e->getMessage());
    }
}

public function countAffectedRows() {
    try {
        return $this->getStatement()->rowCount();
    }
    catch (PDOException $e) {
        throw new RunTimeException($e->getMessage());
    }
}

public function getLastInsertId($name = null) {
    $this->connect();
    return $this->connection->lastInsertId($name);
}

EDIT: I found this. I think I may be in overkill. I could possibly have this class for other things, but not native PDO without a more compelling reason.

https://stackoverflow.com/questions/20664450/is-a-pdo-wrapper-really-overkill

3

Indeed, you could have been using PDO directly. The only real thing it does is to rethrow PDOException as RunTimeException, a practice which should be avoided at all costs.

This being said, PDO doesn't keep you from having to create special adapters for a given database, since different databases have different syntax and functionality which cannot possibly be handled by PDO. It works for simple stuff like PDOStatement::rowCount(), but PDO won't help you if, for example, you need to LIMIT/OFFSET the number of results in MySQL and Microsoft SQL, forcing you to write LIMIT/OFFSET query for one and WHERE rowNumber BETWEEN ... AND ... for another.

Moreover, the presence of PDO doesn't mean you shouldn't have a database layer either: your business layer shouldn't call PDO directly.

Example

If a website needs to display the number of users, business layer will call database layer IDatabase similarly to this:

$countUsers = $this->data->countAllUsers();

Then, you'll have an implementation for Microsoft SQL Server:

class SqlServerDatabase implements IDatabase
{
    ...
    public function countAllUsers()
    {
        $query = 'select count(1) from [Community].[User]';
        $statement = $this->connection->prepare($query);
        $statement->execute();
        return $statement->fetchColumn();
    }
    ...
}

and another one for MySQL:

class SqlServerDatabase implements IDatabase
{
    ...
    public function countAllUsers()
    {
        $query = 'select count(*) from `user`';
        $statement = $this->connection->prepare($query);
        $statement->execute();
        return $statement->fetchColumn();
    }
    ...
}

If you have a lot of table-counting, you may want to refactor that to reduce code duplication. Without PDO, you won't be able to do that. With PDO, you can:

abstract class DatabaseCommon
{
    protected function countAllRows(TableName $tableName, $preferOneToAsterisk = false)
    {
        $query = $preferOneToAsterisk ?
            'select count(1) from ' . $tableName->sanitize() :
            'select count(*) from ' . $tableName->sanitize();

        $statement = $this->connection->prepare($query);
        $statement->execute();
        return $statement->fetchColumn();
    }
}

class SqlServerDatabase extends DatabaseCommon implements IDatabase
{
    public function countAllUsers()
    {
        return $this->countAllRows(
            new MicrosoftSqlTableName('Community', 'User'),
            true
        );
    }
}

class SqlServerDatabase extends DatabaseCommon implements IDatabase
{
    public function countAllUsers()
    {
        return $this->countAllRows(new MySqlTableName('user'));
    }
}
| improve this answer | |
  • If I use straight PDO, how can I pass that around as an object? If I don't use a wrapper around PDO, does that mean I'd have to include the PDO connection in an require/include or in the constructor of each class that uses it? – johnny Oct 23 '14 at 19:05
  • @johnny: I'm not sure I understand your comment. PDO is used only in data layer. Objects from data layer can be constructed at the beginning, and then passed to classes from business layer which need to access the database. – Arseni Mourzenko Oct 23 '14 at 19:19
  • I'm strictly talking about the model layer that uses the database. I have what you are describing. I have a BLL (I call that part of the model) that has findSomething($id). That calls the database, in my other non-model VC areas I just call findSomething($id). In my model later the function findSomething($id) I have the actual SQL. I pass that the PDO connection. If I don't have a wrapper class, how can I pass that PDO connection around? Just include a file that has the raw PDO connection in it? – johnny Oct 23 '14 at 19:26
  • @johnny: I still don't understand. $this->connection in my example is initialized inside the constructor. For that, the constructor receives a connection string from the caller (the caller being somewhere in code which runs at the beginning of every request to the server). There are no additional files. – Arseni Mourzenko Oct 23 '14 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.