*/ class PEAR2_LightDBProperties { /** * Default DSN information */ public static $defaultDSN = array( 'phptype' => false, 'dbsyntax' => false, 'username' => false, 'password' => false, 'protocol' => false, 'hostspec' => false, 'port' => false, 'socket' => false, 'database' => false, 'mode' => false, ); /** * Are we connected ? Hmm can't remember, this will help me do so. */ public static $isConnected = false; /** * The fetchmodes */ const FETCHMODE_ASSOC = 1; const FETCHMODE_ARRAY = 2; const FETCHMOD_OBJECT = 3; } // }}} // {{{ class PEAR2_LightDB /** * PEAR2_LightDB * * This class provides an easy and fast access to a small and simple database * wrapper with not many features to support cross features accross database * platforms. The main goal of this database access layer is to make easy to * create applications and switch database to execute queries. * * @version $Id$ * @author David Coallier */ class PEAR2_LightDB { // {{{ Properties /** * Data source * * @access protected */ protected $_dsn; // }}} // {{{ Constructor ($dsn) /** * Constructor * * @param mixed $dsn The datasource. */ public function __construct($dsn) { $this->_dsn = self::parseDSN($dsn); } // }}} // {{{ public static function parseDSN($dsn) /** * Parse DSN * * Parse the DSN to connect to the user's datasource * * @param mixed $dsn The datasource as an array or string. * @see LightDBProperties */ public static function parseDSN($dsn) { $parsed = PEAR2_LightDBProperties::$defaultDSN; if (is_array($dsn)) { $dsn = array_merge($parsed, $dsn); if (!$dsn['dbsyntax']) { $dsn['dbsyntax'] = $dsn['phptype']; } return $dsn; } } // }}} // {{{ public static function factory($dsn, $options = false) /** * Factory method * * This method returns an object of the driver we want to use. */ public static function factory($dsn, $options = false) { $dsninfo = PEAR2_LightDB::parseDSN($dsn); if (empty($dsninfo['phptype'])) { throw new PEAR2_LightDBException('No RDBMS Driver specified'); } $db = PEAR2_LightDBUtilities::loadClass($dsninfo['phptype']); $db->setDSN($dsninfo); return $db; } // }}} // {{{ public static function connect /** * Connect * * @throws LightDBException */ public static function connect($dsn, $options = false) { $db = self::factory($dsn, $options); if (!$db->connect()) { throw new PEAR2_LightDBException('Could not connect'); } return $db; } // }}} } // }}}