2006-01-15 15:53:55 PHP Database API
Trying to set up a flexible API for the root database object. Specific dataset objects would inherit from this.
( php )
  1  class Database {
2
3 protected $host = '…';
4 protected $pass = '…';
5 protected $user = 'www';
6 protected $database;
7 protected $connection;
8
9 protected $tableList = array();
10 protected $tableFields = array();
11
12 public function __construct( $database='(default)' ) { }
13
14 // Internal methods used to check data integrity
15 public function checkFieldsForTable( $fields, $table ) { }
16 public function fieldExistsInTable( $field, $value, $table ) { }
17 public function findFieldsForTable( $table ) { }
18
19 // Public methods to be used by inherited objects, etc
20 public function deleteFieldForValueInTable( $field, $value, $table ) { }
21 public function editFieldsForIdInTable( $fields, $id, $table ) { }
22 public function fieldsForTableWhere( $fields, $table, $where='', $byIndex='', $addenda='' ) { }
23 public function insertFieldsIntoTable( $fields, $table ) { }
24
25 // The sign that this object is woefully unfinished:
26 public function rawSQL( $sql ) { }
27 }

Thoughts on ways to improve this? Should there be a default database, or should it simply return false in order to prevent bad assumptions?
  • Jeff (Sun, January 15th, 2006, 4:07pm UTC)
    Example of a rawSQL call:
    ( sql )
      1  select count(forumcomments.id)
    2 from forum, forumcomments
    3 where forumcomments.forumID=forum.id and forum.name="blogID_'.$blogID.'"

    which may be an indication of a few things:

    • A nice count function is needed
    • Much more importantly, there is no table joining mechanism, which really should be addressed (perhaps a la LINQ?)

  • Jeff (Sun, January 15th, 2006, 4:16pm UTC)
    Notes on the naming scheme above (just a personal convention, borrowed from Obj-C-type namings): The name of the function should list the action (optional ??? only if it's doing something to the database), the subject (like "fields") to be gotten or manipulated, and all required fields (in order of their call). This seems optimally self-explanatory for the code that calls the function. That said, I should really make the "where" argument in "fieldsForTableWhere" be required, and have a function "fieldsForTable" when requesting the whole enchilada.

  • whitepaige (Mon, November 2nd, 2009, 5:13am UTC)
    thanks alot i will bookmark this site,because i want to used that code in the future

  • Terry Lawton (Sat, February 11th, 2012, 11:54am UTC)
    Fantastic post. Here’s a tool that lets your build your online database without programming. There is no need to hand code PHP. Cut your development time by 90%
    http://www.caspio.com/

Leave a comment