The Largest Online Resource for Oracle Articles, Oracle Tips, Oracle Scripts & Oracle Tools!!


Enter your Email:
 
Navigate at FreeMegaZone Home      Articles      Tools      Jobs      Games      Support      Submit Content      Advertise

Advertise at FreeMegaZone

Give tremendous boost to your business by advertising at FreeMegaZone. Contact webmaster@freemegazone.com

 
Rating: *****                                             Rate this article:    

Author:  Racus Melofire

Abstraction is the process of identifying the important aspects of an object and ignoring the non-essential aspects. Database abstraction hides the complexities of database by providing a database independent interface. All you need is to change the connection string and your application will be able to run no matter what database is running at the back end. You can easily switch form access to oracle, oracle to mysql, mysql to sybase and so on by changing a single string.

The database abstraction layer has its own pros and cons so depending on the requirements it must be decided if database abstraction layer should be used in an application or not. Mostly small to medium sized applications do not need database abstraction layer as portability is not an issue but if the objective of your application is to add flexibility and ease of maintenance then database abstraction layer can be a good choice for your application.

Pros and Cons of Database Abstraction: 

  1. The database abstraction layer enables us to write generic code that can be used to access different databases without rewriting the code.
  2. The database abstraction layer enables the application to run on different databases.
  3. The database abstraction layer minimizes development time if you have to switch to some other database.
  4. The database abstraction layer simplifies the complex tasks.
  5. The database abstraction layer helps in portability.
  6. The database abstraction layer help prevent SQL injections.
  7. Adding an additional layer affects the performance of the system as execution time will be increased.
  8. If any database specific function is written by mistake then it won't allow the application to run on multiple databases.
  9. The database abstraction layer holds the lowest common feature among all the databases it supports.

PEAR:DB – The Database Abstraction layer:

Consider the below code which retrieves and displays first name, last name and location from USER table of the database “myDatabase”.

<?php
//open database connection
$con = mysql_connect("localhost","root”,”mypassword”);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//select the database
mysql_select_db("myDatabase",$con) or die("can not select db");
// execute the query
$query = "SELECT firstName, lastName, location FROM USER";
$result = mysql_query($query) or die (mysql_error());
// iterate through rows and print column data in the form Name – Location
while ($arr = mysql_fetch_row($result))
{
echo $arr[0].” ”. $arr[1].” – “.$arr[2].”\n";
}
// close database connection
mysql_close($con);
?>

The output of this program will be

John Rediff – Nigeria
Lisa Backin – USA
George Michael – UK

The above code follows four simple steps

  1. Open database connection
  2. Connect to the database
  3. Execute query
  4. Retrieve and display results

There is no problem with the code but it will be problematic if after sometime the application needs to be ported to some other database. The database will crash in such situation. This is because the above code is using the SQL-specific functions which are not compatible with other databases like Access, Oracle, PostgreSQL, SQLite or Sybase.

Now let us add data abstraction to above code. The class file is included with the PEAR installation directory.

<?php
//include database abstraction layer

include “DB.php”;
//open database connection and connect to database

$con = DB::connect("mysql://root:mypassword@localhost/myDatabase");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// execute the query
$query = "SELECT * FROM USER";
$result = $con ->query($query);
// iterate through rows and print column data in the form Name – Location
while ($arr = $result->fetchRow() )
{
echo $arr[0].” ”. $arr[1].” – “.$arr[2].”\n";
}
// close database connection

$con ->disconnect();

?>

Now our code uses database independent functions and hence it enables our code to work with any database . The only change required is that a new connection string will be passed to connect() function.   For example you can use pgsql://user@server/mydatabase to connect to PostgreSQL database and odbc://user:password@server.com/mydatabase to connect to ODBC. ODBC is the platform independent implementation of database abstraction layer.  

PEAR:DB functions:  

Now let's have a look at a list of PEAR functions that can be used to maintain data abstraction layer and what they do  

 
Function
Usage
1 $con=DB:Connect(“connection string”) Connects to the database
2 $con ->commit() Commit the data to database
3 $con ->rollback() Roll back to previous state
4 $result = $con->query($query) Executes query
5 $num=$result->numRows() Returns number of rows in the result set
6 $num=$result->numCols() Returns number of columns in the result set
7 $arr = $result->fetchRow() Iterate through rows of the result set
8 $arr=$result->fetchRow(DB_FETCHMODE_ASSOC) Returns associative array with column names as keys
9 $arr=$result->fetchRow(DB_FETCHMODE_OBJECT) Fetch each row as an object and return associative array with object properties as key
10 $object=$result->limitQuery() Limit the number of results
11 $object=$result->tableInfo($con,”myDatabase”) Returns associative array of table
12 print_r($result->tableInfo($con,”myDatabase”)) Prints the array in reverse order
13 $con->isError($result) Returns true if error has occurred
14 DB::errorMessage($result) Displays the error
15 setErrorHandling()

Defines the error handling mechanism. It can accept any of below parameters

1. PEAR_ERROR_RETURN: does nothing
2. PEAR_ERROR_PRINT: prints the error message and continue execution
3. PEAR_ERROR_CALLBACK: calls another function to handle the error
4. PEAR_ERROR_DIE: terminates execution
5. PEAR_ERROR_TRIGGER: triggers a PHP error

16 $con ->prepare($query) Prepares a query. You can prepare a single query and execute it multiple times with different values.
17 $con ->execute($query,$value to be substituted in the query) Executes the prepared query
18 $con ->disconnect() Close database connection
 

DBMS support by Database Abstraction Layers:

Below is a table of databases and the database abstraction layers that support them.

 

ADOdb

Creole

DBX

PDO

Metabase

PEAR:DB

PEAR:MDB

PEAR:MDB2

Access

Yes

 

 

 

Yes

 

 

 

ADO

Yes

 

 

 

 

 

 

 

DB2

Yes

 

 

 

 

 

 

 

FreeTDS

 

 

 

Yes

 

 

 

 

Firebird

Yes

 

 

Yes

 

Yes

Yes

Yes

Foxpro

Yes

 

 

 

 

 

 

 

FrontBase

Yes

 

Yes

 

 

 

Yes

Yes

Informix

Yes

 

 

 

Yes

Yes

 

 

Interbase

Yes

 

 

Yes

Yes

Yes

Yes

Yes

LDAP

Yes

 

 

 

 

 

 

 

mSQL

 

 

 

 

Yes

Yes

 

 

MS SQL

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Yes

MySQL

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Netezza

Yes

 

 

 

 

 

 

 

ODBTP

Yes

 

 

 

 

 

 

 

ODBC

Yes

 

Yes

Yes

Yes

Yes

 

 

Oracle

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Yes

PostgreSQL

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Querysim

 

 

 

 

 

 

Yes

Yes

SAP DB

Yes

 

 

 

 

 

 

 

SQLite

Yes

Yes

Yes

Yes

Yes

Yes

 

Yes

Sybase

Yes

 

Yes

Yes

 

Yes

 

 

 More Database Articles
   Database Security: Step by step guideline
   DBA Tips for Statistics gathering on Oracle Data Dictionary Objects!!
   Oracle-SQL Guide: Look out for Fragmented Indexes
   Beware of Database Myths
   A Guideline for Oracle Instantiation with RMAN!


 

 
HOME      ABOUT US      SUPPORT      SITE MAP      PRIVACY POLICY      TERMS OF USE      SUBMIT CONTENT      ADVERTISE
Copyright © 2007 - 2010 Oriole Intellect Inc. All rights reserved.

The name Oracle is a trademark of Oracle Corporation. Any other names used on this website may be trademarks of their respective owners