Php – Designing a universal database driver class

databasenosqlPHPsql

I have a homebrew MVC framework that I have been using for a while, and it has been working great, but I am planning an upgrade to it for its ORM classes. So far, it only supports SQL databases by creating statements dependent on what function is being called. The ORM class includes field structure and validation, computed fields, indices and joined classes (for associations, 1-1, 1-*, *-1 and *-*), and that provides enough functionality for selecting any data and fetching associated rows in the database.

But I would like to extend it to support XML, LDAP and NoSQL databases (i.e. MongoDB). My ideas for implementing this would be to modify the functions within the ORM class to interact with a custom driver (for each database type) in a common format. Unfortunately, I haven't had enough experience with using these other formats enough to adequately design an interface class that could be used by the ORM for any type.

So, are there any universal database drivers that would support all of these database types so I can learn by example?

The current interface I am working by is:

interface DBDriver {
  function connect($host, $un, $pw, $persist = false);
  function disconnect();
  function selectDB($db);
  function charset($charset);
  function insert($table, $values);
  function select($table, $fields, $joins, $where, $group, $having, $order, $limit);
  function update($table, $where, $values, $where, $order, $limit);
  function delete($table, $where, $order, $limit);
  function transactionBegin();
  function transactionCommit();
  function transactionRollback();
}

But I am concerned that although this will provide enough for creating SQL statements, it might not be sufficient, or perhaps even overkill for other methods such as XML or NoSQL.

Best Answer

It would be so abstract that it wouldn't do anything useful (analysis paralysis / abstraction inaction). The best thing to do would be to define interfaces for CRUD/BREAD/etc and implement them as they are needed. If you're using PHP 5.4 you can take advantage of traits so you can cook up drivers piecemeal for shared behavior.

interface Create { }
interface Read { }
interface Update { }
interface Delete { }
interface Browse { }
interface Edit { }
interface Add { }
interface CRUD extends Create, Read, Update, Delete { }
interface BREAD extends Browse, Read, Edit, Add, Delete { }
interface Transactional { }