コード例 #1
0
 /**
  * create object first time
  *
  * @access public
  * @author GDev Team <*****@*****.**>
  * @since  23/08/2005
  * @param  mixed  $dsn  connection data as string or array
  * @param  array  $options  runtime options
  * @return mixed  database object or exception
  */
 public static function factory($dsn, $options = false)
 {
     /**
      * create options array
      */
     if (!is_array($options)) {
         $options = array('persistent' => $options);
     }
     /**
      * set the dsn array
      */
     try {
         $mydsn = self::parse_dsn($dsn);
     } catch (gdatabase_exception $e) {
         printf('database dsn: %s', $e->custom_message());
     }
     /**
      * make dbtype lower case
      */
     $mydbtype = strtolower($mydsn['dbtype']);
     /**
      * include specific database class file
      */
     if (isset($options['debug']) and $options['debug'] > 0) {
         require_once LIB_PATH . 'gdatabase/gdatabase_' . $mydbtype . '.class.php';
     } else {
         @(require_once LIB_PATH . 'gdatabase/gdatabase_' . $mydbtype . '.class.php');
     }
     /**
      * define class name
      */
     $classname = 'gdatabase_' . $mydbtype;
     /**
      * if class does not exist throw new exception
      */
     if (!class_exists($classname)) {
         throw new gdatabase_exception('specific database class does not exist');
     }
     /**
      * create object of the specific database class
      */
     try {
         @($obj = new $classname());
     } catch (gdatabase_exception $e) {
         printf('database %s: %s', $mydbtype, $e->custom_message());
     }
     /**
      * set the options
      */
     try {
         foreach ($options as $option => $value) {
             $obj->set_option($option, $value);
         }
     } catch (gdatabase_exception $e) {
         printf('database options: %s', $e->custom_message());
     }
     /**
      * write object to instance holder
      */
     return self::$ginstance = $obj;
 }