What is the point behind building an abstraction layer PDO Adapter class instead of using native PDO?

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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);
}
</code>
<code>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); } </code>
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

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$countUsers = $this->data->countAllUsers();
</code>
<code>$countUsers = $this->data->countAllUsers(); </code>
$countUsers = $this->data->countAllUsers();

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class SqlServerDatabase implements IDatabase
{
...
public function countAllUsers()
{
$query = 'select count(1) from [Community].[User]';
$statement = $this->connection->prepare($query);
$statement->execute();
return $statement->fetchColumn();
}
...
}
</code>
<code>class SqlServerDatabase implements IDatabase { ... public function countAllUsers() { $query = 'select count(1) from [Community].[User]'; $statement = $this->connection->prepare($query); $statement->execute(); return $statement->fetchColumn(); } ... } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class SqlServerDatabase implements IDatabase
{
...
public function countAllUsers()
{
$query = 'select count(*) from `user`';
$statement = $this->connection->prepare($query);
$statement->execute();
return $statement->fetchColumn();
}
...
}
</code>
<code>class SqlServerDatabase implements IDatabase { ... public function countAllUsers() { $query = 'select count(*) from `user`'; $statement = $this->connection->prepare($query); $statement->execute(); return $statement->fetchColumn(); } ... } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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'));
}
}
</code>
<code>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')); } } </code>
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'));
    }
}

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật