Ejemplo n.º 1
0
 /**
  * Adds a database to the facade, afterwards you can select the database using
  * selectDatabase($key), where $key is the name you assigned to this database.
  *
  * Usage:
  *
  * R::addDatabase( 'database-1', 'sqlite:/tmp/db1.txt' );
  * R::selectDatabase( 'database-1' ); //to select database again
  *
  * This method allows you to dynamically add (and select) new databases
  * to the facade. Adding a database with the same key will cause an exception.
  *
  * @param string      $key    ID for the database
  * @param string      $dsn    DSN for the database
  * @param string      $user   user for connection
  * @param NULL|string $pass   password for connection
  * @param bool        $frozen whether this database is frozen or not
  *
  * @return void
  */
 public static function addDatabase($key, $dsn, $user = NULL, $pass = NULL, $frozen = FALSE)
 {
     if (isset(self::$toolboxes[$key])) {
         throw new RedException('A database has already be specified for this key.');
     }
     if (is_object($dsn)) {
         $db = new RPDO($dsn);
         $dbType = $db->getDatabaseType();
     } else {
         $db = new RPDO($dsn, $user, $pass, TRUE);
         $dbType = substr($dsn, 0, strpos($dsn, ':'));
     }
     $adapter = new DBAdapter($db);
     $writers = array('pgsql' => 'PostgreSQL', 'sqlite' => 'SQLiteT', 'cubrid' => 'CUBRID', 'mysql' => 'MySQL', 'sqlsrv' => 'SQLServer');
     $wkey = trim(strtolower($dbType));
     if (!isset($writers[$wkey])) {
         trigger_error('Unsupported DSN: ' . $wkey);
     }
     $writerClass = '\\RedBeanPHP\\QueryWriter\\' . $writers[$wkey];
     $writer = new $writerClass($adapter);
     $redbean = new OODB($writer, $frozen);
     self::$toolboxes[$key] = new ToolBox($redbean, $adapter, $writer);
 }