Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
» Sitemap
Free Newsletters:
News Via RSS Feed

follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles


Posted Aug 27, 2002

Faking It - Simulated Database Queries - Page 3

By Staff

A Little Project

Suppose we're working on the admin module of an application built on the Fusebox framework. We've finished architecting our circuit and the Fusedocs are done. We have been assigned the fuseaction showUserGroups. It is comprised of two fuses: qry_UserGroups.php and dsp_UserGroups.php. Now we just need to code the fuses.

Unfortunately, our DBA is finishing up another project right now and hasn't started on the database for the application we're working on. But this time we're not going to wait for her, we're going to use QuerySim to pretend the database is already done.

Here's the Fusedoc from the qry_UserGroups.php fuse stub:

<fusedoc fuse="qry_UserGroups.php" specification="2.0" language="PHP">
        I fetch information about users and their groups.
        <history author="Alan Richmond" type="Create" />
            <file path="pear/DB.php" 
              PEAR is in our include path." />
            <recordset name="user" primarykeys="userID">
                <number name="userID" precision="Integer" />
                <string name="firstName" />
                <string name="lastName" />
                <number name="userGroups" precision="Integer" />

Now that we know what the fuse needs to do we can get started. First we'll include the DB class:

require_once 'DB.php';

Getting Connected

As a reminder, here's the syntax for the PEAR DB connect string:


PEAR DB sees QuerySim as just another database driver, so the phptype of our connect string is querysim. We can ignore the rest. (See, I told you this was easy.)

Our connection looks like:


At this point a real database would either return a connect object or an error object. So, we check for any errors:

if (DB::isError($conn)) {
    die (
'Cannot connect: '.$conn->getMessage()."\n<br />\n<pre>".$conn->getUserInfo()."\n</pre>\n<br />");

Obviously, since QuerySim isn't really a database and there isn't really a connection, DB should return an error. But, our sneaky little QuerySim driver tells DB it 'connected' without any problem and DB goes ahead and creates our connect object. We're doing the error check for two reasons: First, we'll eventually be moving to a real database that might fail to connect and we want a seamless switch over when the time comes. Second, when our QuerySim text resides in an external file, QuerySim will check for a valid file and that sufficient permissions exist to open it, and will throw an error we can catch on failure.

Talking to the 'Database'

Now that we have our simulated connection, we can simulate our query:


Again, we check for errors:

if (DB::isError($user)){
    die (
'Database Error: '.$user->getMessage()."\n<br />\n<pre>".$user->getUserInfo()."\n</pre>\n<br />");

The most common QuerySim error you might see here is a syntax error indicating malformed QuerySim text. By calling getUserInfo(), QuerySim will tell us exactly what is wrong.

PHP Archives

Comment and Contribute


(Maximum characters: 1200). You have characters left.