Database Abstraction Layers PHP

Imagine creating a Web application that uses MySQL and later being asked to make it work with Oracle. All the PHP functions are different, so you'd have to change every one. In addition, as MySQL and Oracle each use slightly different SQL, you will probably have to change most of your queries. One solution to this problem is adding an abstraction layer. This separates your business logic—the rules of your application— from the code that interfaces with the database. A single function calls the right function based on the type of database you need to query.

Perhaps the most popular database abstraction layer is part of the PHP Base Library. This library also contains code for session management. Another abstraction layer is Metabase, available at the PHP Classes Repository.

Despite abstraction layers, incompatibilities between databases continue to offer challenges. MySQL uses a special qualifier for column definitions called AUTO_INCREMENT. It causes a column to be populated automatically with integers in ascending order. In Oracle this functionality can be approximated using a sequence and a trigger. The differences are difficult to reconcile systematically. In 1999, Scott Ambler proposed a solution in his white paper "The Design of a Robust Persistence Layer for Relational Databases" A careful analysis of the problems is explored as well as a detailed design, neither of which I can do justice to in the context of this chapter.

An abstraction layer trades some performance in favor of robustness. Certain unique, high-performance features of each database must be abandoned. The abstraction layer will provide the common set of functionality. But what you gain is independence from any particular database.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics