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

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


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

Featured Database Articles

PHP

Posted Aug 27, 2002

Faking It - Simulated Database Queries - Page 3

By DatabaseJournal.com 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">
    <responsibilities>
        I fetch information about users and their groups.
    </responsibilities>
    <properties>
        <history author="Alan Richmond" type="Create" />
    </properties>
    <io>
        <in>
            <file path="pear/DB.php" 
              action="include" 
              comments="require_once.  
              PEAR is in our include path." />
        </in>
        <out>
            <recordset name="user" primarykeys="userID">
                <number name="userID" precision="Integer" />
                <string name="firstName" />
                <string name="lastName" />
                <number name="userGroups" precision="Integer" />
            </recordset>
        </out>
    </io>
</fusedoc>

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


<?php 
require_once 'DB.php';
?>

Getting Connected

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


<?php
phptype
://username:password@protocol+hostspec/database
?>

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:


<?php
$conn 
DB::connect('querysim');
?>

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


<?php
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:


<?php
$user 
$conn->query('
    userID,firstName,lastName,userGroups
    101|Stan|Cox|33
    102|Hal|Helms|22
    103|Bert|Dawson|11
'
);
?>

Again, we check for errors:


<?php
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.