Database Abstraction Idea

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I've been working on a project that was started from scratch. I'm a minimalist, so I like to keep things as simple as possible. I've been using this idea for a database abstraction, and I thought I'd see if I could get some constructive criticism.

Here is an example of how I use this first:

$table = 'customers';

// validation is handled before this
// the class also uses PDO, so escaping isn't a problem
$id = $_CLEAN['customer_id'];

$customer = DB::loadRecord($table, $id);

echo $customer->first_name; // Bob
echo $customer->last_name; // Jones

// etc.

$customer->first_name = 'Robert';

try {
	DB::updateRecord($table, $customer)
} catch (e) {
	// handle error
}

##### END CODE ####

There are other methods as well. I used a static class to kind of simulate a namespace. The database connection is handled elsewhere. I didn't want to make a singleton and have to pass it around within functions and stuff.

The methods are:

query_result &query($sql) // for complicated reports mostly

DBRecord loadRecord($table, $id, $fields = null) // fields can be an array of fields you want back. It always gives you an ID though

bool updateRecord($table, DBRecord $record)

DBRecord newRecord($table) // returns a blank record that can be inserted later

bool insertRecord($table, DBRecord $record)

query_result find($table, $whereField, $whereOperator, $whereValue)
// whereOperator is basically an Enum

getFieldInfo($table, $field)
// returns the fetch_object of SHOW COLUMNS FROM $table LIKE $field
// For maxlength and other things in my UI class.

The DBRecord is basically the object representation of the table and doesn't have to be all the fields. Example is

tableName->columnName

I'm pretty happy with how it's working now, but I'm concerned I might have misses some sort of security issue. I only had a work week to finish the entire project. We only have MySQL databases, so I didn't need to abstract different types of databases or anything.

Thanks,
--
Ray Hauge
www.primateapplications.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux