Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

Building a Database Abstraction Class

Databases can make it hard to create durable and transportable code. If database code is built tightly into a project it can be difficult to migrate from one database server to an other, for example.

In this sample we are going to create a basic utility class that separates a lot of the database facing code from the logic of a project as a whole. The class will broadly achieve two things. First it will present a conduit between a program and the database via which SQL queries can be passed. Second, it will automate the generation of SQL for a range of frequently performed operations, such as simple SELECT and UPDATE statements. In the case of SELECT queries, the result set will be provided in the form of an array of associative arrays.

The class should provide two main benefits for the client coder. First, in automating simple queries it should save the need to construct SQL statements on the fly. Second, in providing a clear interface to its functionality it should safeguard portability. If the project is to be moved to a different database server, then an alternative class can be written that maintains the same functionality but with a different implementation behind the scene.

It must be noted, however that all database abstraction schemes (including Perl's famous database independent interface or DBI library) suffer from one major drawback. Different database engines tend to support different SQL syntaxes and features. This means that SQL statements written to work with MySQL may not work with Oracle, for example. For simple projects you can go some way towards dealing with this problem by using SQL features that are as 'standard' as possible, and avoiding the use of features specific to a database application.

Connecting to the Database

To start with, let's build the methods to connect to a database server, and select a database. Along the way, we can look at the method we are going to use for reporting errors.

class DataLayer {
    var $link;   
    var $errors = array();
    var $debug = false;

    function DataLayer( ) {
    }    
    
    function connect( $host, $name, $pass, $db ) {
        $link = mysql_connect( $host, $name, $pass );
        if ( ! $link ) {
            $this->setError("Couldn't connect to database server");
            return false;
        }

        if ( ! mysql_select_db( $db, $this->link ) ) {
            $this->setError("Couldn't select database: $db");
            return false;
        }
        $this->link = $link;
        return true;
    }

    function getError( ) {
        return $this->errors[count($this->errors)-1];
    }

    function setError( $str ) {
        array_push( $this->errors, $str );
    }
...

We have called our class DataLayer. We establish three properties; $link will store our database resource, $errors will store an array of error messages, and $debug is another flag which will help us to monitor the behavior of our class.

The connect() method simply uses the now familiar mysql_connect() and mysql_ select_db() functions to connect to the server and choose a database. If you implement this class yourself, you might consider storing the $host, $name, $pass and $db argument variables in class properties. We have chosen not to in this example. If we encounter any problems with connection we call the setError() function which maintains a stack of error messages. If all goes well, however, we store the database resource returned by mysql_connect in our $link property.

Making the Query

We're now ready to build the query methods. We split these up into three:

    function _query( $query ) {
        if ( ! $this->link ) {
            $this->setError("No active db connection");
            return false;
        }
        $result = mysql_query( $query, $this->link );
        if ( ! $result )
            $this->setError("error: ".mysql_error());
        return $result;
    }

    function setQuery( $query ) {
        if (! $result = $this->_query( $query ) )
            return false;
        return mysql_affected_rows( $this->link );
    }

    function getQuery( $query ) {
        if (! $result = $this->_query( $query ) )
            return false;
        $ret = array();
        while ( $row = mysql_fetch_assoc( $result ) )
            $ret[] = $row;
        return $ret;
    }

_query() performs some basic checks, but all it really does is to pass a string containing an SQL query to the mysql_query() function, returning a mysql result resource if all goes well. Both setQuery() and getQuery() call _query(), passing it an SQL string. They differ in what they return to the user, however. setQuery() is designed for SQL statements that act upon a database. UPDATE statements, for example. It returns an integer representing the number of affected rows. getQuery() is designed primarily for SELECT statements. It builds an array of the result set which it returns to the calling code. We're now in a position to test our class.

Testing the Basic Class

For the test, we assume the presence of a table called test_table. The CREATE statement illustrates its structure:

      CREATE TABLE test_table (
           id INT NOT NULL AUTO_INCREMENT,
           PRIMARY KEY( id ),
           name VARCHAR(255),
           age INT,
           description BLOB
           );

Our test code simply connects to the database, adds some data, and then requests it back again, looping through the returned array and printing an HTML table to the browser.

$dl = new DataLayer( );
$dl->connect( "localhost", "", "", "test" ) or die ( $dl->getError() );
$dl->setQuery("DELETE FROM test_table");
$dl->setQuery("INSERT INTO test_table (name, age, description) 
        VALUES('bob', 20, 'student')");
$dl->setQuery("INSERT INTO test_table (name, age, description) 
        VALUES('mary', 66, 'librarian')");
$dl->setQuery("INSERT INTO test_table (name, age, description) 
        VALUES('su', 31, 'producer')");
$dl->setQuery("INSERT INTO test_table (name, age, description) 
        VALUES('percy', 45, 'civil servant')");
$table = $dl->getQuery("SELECT * from test_table");

print "<table border=\"1\">";
foreach( $table as $d_row ) {
    print "<tr>";
    foreach ( $d_row as $field=>$val )
        print "<td>$val</td>";
    print "</tr>";
}
print "</table>";

Automating SQL Statements

SQL can be a highly complex affair, and it is not our purpose to reinvent it. However, some fairly basic operations are performed over and over again, SQL statements can be tedious to construct within a script. These methods should simplify some of these tasks.

Let's illustrate the technique by looking at a method for automating basic SELECT statements.

    function select( $table, $condition="", $sort="" ) {
        $query = "SELECT * FROM $table";
        $query .= $this->_makeWhereList( $condition );
        if ( $sort != "" )
            $query .= " order by $sort";
        $this->debug( $query );
        return $this->getQuery( $query, $error );
    }
    
    function _makeWhereList( $condition ) {
        if ( empty( $condition ) )
            return "";
        $retstr = " WHERE ";
        if ( is_array( $condition ) ) {
            $cond_pairs=array();
            foreach( $condition as $field=>$val )
                array_push( $cond_pairs, "$field=".$this->_quote_val( $val ) );
            $retstr .= implode( " AND ", $cond_pairs );
        } elseif ( is_string( $condition ) && ! empty( $condition ) )
            $retstr .= $condition;
        return $retstr;
    } 

The select() method requires at least a table name. It will also optionally accept condition and sort arguments. The sort argument should be a string such as "age DESC, name". The condition argument can be either an associative array or a string. A condition passed as an associative arrays will be used to construct a WHERE clause with the keys representing field names. So

array( name=>"bob", age=>20 )

will resolve to

WHERE name='bob' AND age=20

Where more complex conditions are required, such as

WHERE name='bob' AND age<25

the condition argument should be passed as string containing the valid SQL fragment. The construction of the WHERE clause takes place in the _makeWhereList() method. If no condition is required an empty string is returned. If the condition is a string, it is simply tacked onto the string "WHERE" and returned. If the condition is an array however, the fieldname/value pairs are first constructed and stored in an array called $cond_pairs. The implode() function is then used to join the new array into a single string, the fieldname/value pairs separated by the string " AND ".

Notice that we call a utility method called quote_val() when we are building our string. This is used to add backslashes to special characters (such as single quotes) within values. It also surrounds strings in quotes, though it leaves numbers alone.

    function _quote_val( $val ) {
        if ( is_numeric( $val ) )
            return $val;
        return "'".addslashes($val)."'";
    }

The addslashes() function built-in to PHP. It accepts a string and returns another with special characters backslashed. This is useful for us, because we must surround strings sent to MySQL with single quotes.

To get an array containing a complete listing of our table we can now use the select() method.

$table = $dl->select("test_table");

To get all rows with an age of less than 40:

$table = $dl->select("test_table", "age<40");

To pull out information about people called bob

$table = $dl->select("test_table", array('name'=>"bob") );

Bringing It All Together

Listing 12.7 represents the complete DataLayer class. It includes the methods update(), delete(), and insert() that are similar to select() in that they simply construct SQL statements.

Listing 12.7 The DataLayer Class

 1: <?
 2: class DataLayer {
 3:   var $link;  
 4:   var $errors = array();
 5:   var $debug = false;
 6: 
 7:   function DataLayer( ) {
 8:   }
 9:   
 10:   function connect( $host, $name, $pass, $db ) {
 11:     $link = mysql_connect( $host, $name, $pass );
 12:     if ( ! $link ) {
 13:       $this->setError("Couldn't connect to database server");
 14:       return false;
 15:     }
 16: 
 17:       if ( ! mysql_select_db( $db, $link ) ) { 
 18:       $this->setError("Couldn't select database: $db");
 19:       return false;
 20:     }
 21:     $this->link = $link;
 22:     return true;  
 23:   }
 24: 
 25:   function getError( ) {
 26:     return $this->errors[count($this->errors)-1];
 27:   }
 28: 
 29:   function setError( $str ) {
 30:     array_push( $this->errors, $str );
 31:   }
 32: 
 33:   function _query( $query ) {
 34:     if ( ! $this->link ) {
 35:       $this->setError("No active db connection");
 36:       return false;
 37:     }
 38:     $result = mysql_query( $query, $this->link );
 39:     if ( ! $result )
 40:       $this->setError("error: ".mysql_error());
 41:     return $result;
 42:   }
 43: 
 44:   function setQuery( $query ) {
 45:       if (! $result = $this->_query( $query ) ) 
 46:       return false;  
 47:     return mysql_affected_rows( $this->link );
 48:   }
 49: 
 50:   function getQuery( $query ) {
 51:       if (! $result = $this->_query( $query ) ) 
 52:       return false;  
 53:     $ret = array();
 54:     while ( $row = mysql_fetch_assoc( $result ) )
 55:       $ret[] = $row;
 56:     return $ret;
 57:   }
 58: 
 59:   function getResource( ) {
 60:     return $this->link;
 61:   }
 62: 
 63:   function select( $table, $condition="", $sort="" ) {
 64:         $query = "SELECT * FROM $table";
 65:     $query .= $this->_makeWhereList( $condition );  
 66:     if ( $sort != "" )
 67:       $query .= " order by $sort";
 68:     $this->debug( $query );
 69:         return $this->getQuery( $query, $error );
 70:   }
 71: 
 72:   function insert( $table, $add_array ) {
 73:         $add_array = $this->_quote_vals( $add_array );
 74:         $keys = "(".implode( array_keys( $add_array ), ", ").")";
 75:         $values = "values (".implode( array_values( $add_array ), ", ").")";
 76:         $query = "INSERT INTO $table $keys $values";
 77:     $this->debug( $query );
 78:         return $this->setQuery( $query );
 79:   }
 80: 
 81:   function update( $table, $update_array, $condition="" ) {
 82:     $update_pairs=array();
 83:     foreach( $update_array as $field=>$val )
 84:       array_push( $update_pairs, "$field=".$this->_quote_val( $val ) );
 85: 
 86:         $query = "UPDATE $table set ";
 87:     $query .= implode( ", ", $update_pairs );
 88:     $query .= $this->_makeWhereList( $condition );  
 89:     $this->debug( $query );
 90:         return $this->setQuery( $query );
 91:   }
 92: 
 93:   function delete( $table, $condition="" ) {
 94:         $query = "DELETE FROM $table";
 95:     $query .= $this->_makeWhereList( $condition );  
 96:     $this->debug( $query );
 97:         return $this->setQuery( $query, $error );
 98:   }
 99:   
100:   function debug( $msg ) {
101:     if ( $this->debug )
102:       print "$msg<br>";
103:   }
104: 
105:   function _makeWhereList( $condition ) {
106:     if ( empty( $condition ) )
107:       return "";
108:     $retstr = " WHERE ";
109:     if ( is_array( $condition ) ) {
110:       $cond_pairs=array();
111:       foreach( $condition as $field=>$val )
112:         array_push( $cond_pairs, "$field=".$this->_quote_val( $val ) );
113:       $retstr .= implode( " and ", $cond_pairs );
114:     } elseif ( is_string( $condition ) && ! empty( $condition ) )
115:       $retstr .= $condition;
116:     return $retstr;
117:   }
118: 
119:   function _quote_val( $val ) {
120:     if ( is_numeric( $val ) )
121:       return $val;
122:     return "'".addslashes($val)."'";
123:   }
124: 
125:   function _quote_vals( $array ) {
126:     foreach( $array as $key=>$val )
127:       $ret[$key]=$this->_quote_val( $val );
128:     return $ret;
129:   }
130: }  
131: ?>

As you should see from the code, update() (line 81), delete() (line 93), and insert() (line 72) provide a similar interface to that provided by select() (line 63).

update() which is declared on line 81 requires a string representing the table to be worked with. It also requires an associative array of keys and values. The keys should be the field name to be altered, and the value should be the new content for the field. Finally, an optional condition argument is accepted. This follows the same logic as the condition argument in the select() method. It can be a string or an array.

delete() which is declared on line 93 requires a table name, and an optional condition argument.

insert() is declared on line 72 and requires a table name, and an associative array of fields to add to the row. The keys should be the field name to be altered, and the value should be the new content for the field.

We had better see the code in action. Listing 12.8 is a simple test script that populates and manipulates a table in a database.

Listing 12.8 Working with the DataLayer Class

 1: <html>
 2: <head>
 3: <title>Listing 12.8 Working with the DataLayer Class</title>
 4: </head>
 5: <body>
 6: <?php
 7: include("listing12.9.php");
 8: $people = array( 
 9:   array( 'name'=>"bob", 'age'=>20, 'description'=>"student" ),
10:   array( 'name'=>"mary", 'age'=>66, 'description'=>"librarian" ),
11:   array( 'name'=>"su", 'age'=>31, 'description'=>"producer" ),
12:   array( 'name'=>"percy", 'age'=>45, 'description'=>"civil servant" )
13:   );
14: 
15: $dl = new DataLayer( );
16: $dl->debug=true;
17: $dl->connect( "localhost", "", "", "test" ) or die ( $dl->getError() );
18: 
19: $dl->delete( "test_table" );
20: 
21: foreach ( $people as $person ) {
22:   $dl->insert( "test_table", $person ) or die( $dl->getError() );
23: }
24: 
25: foreach ( $people as $person ) {
26:   $person['age']++;
27:   $dl->update( "test_table", $person, array( 'name'=>$person['name'] ) );
28: }
29: 
30: $dl->delete( "test_table", "age < 25" );
31: $table = $dl->select( "test_table" );
32: 
33: print "<table border=\"1\">";
34: foreach( $table as $d_row ) {
35:   print "<tr>";
36:   foreach ( $d_row as $field=>$val )
37:     print "<td>$val</td>";
38:   print "</tr>";
39: }
40: print "</table>";
41: ?>
42: </body>
43: </html>

We initialize our data using an associative array beginning on line 8. We instantiate a DataLayer object on line 15, set the object's $debug property to true on line 16, and connect to the database on line 17. Because the object is in debug mode, all the SQL we generate will be output to the browser after being sent to the database.

We call the delete() method on line 19, to clear any data in the table, before populating it by looping through our $people on line 21 array and calling the insert() method (line 22) for each person. DataLayer is designed to work with associative arrays, so we only need to pass each element of the $people array to insert() in order to populate the table.

We then decide that we wish to alter the age field of each row. Once again we loop through the people array (line 25), incrementing each element before calling update() on line 27. We can pass the entire $person array to update(). Although this will mean that most fields in the row will be updated with their own value, this is quick and easy from the client coder's perspective. The third argument to update() is a condition array containing the name key and value. In a real world example we would probably use the id field to ensure that we are updating only one row.

Finally, we call delete() once again (line 30), this time including a condition argument. Because we wish to delete all rows with an age value of less than 25, we pass a condition string rather than an array. Remember that condition arrays are only useful for demanding equivalence, so we must 'hardcode' the 'less then' comparison into a string.

You can see the output from Listing 12.8 in Figure 12.1. Because the object was in debug mode, notice that the SQL has been output to the browser.

Figure 12.1 Working with the DataLayer Class.

  • + Share This
  • 🔖 Save To Your Account