public static function setDSN($dsn)
 {
     if (is_string($dsn)) {
         $dsn = Creole::parseDSN($dsn);
     }
     self::$dsn = $dsn;
 }
 public function addConfig()
 {
     if ($this->hasParameter('host')) {
         $this->setParameter('hostspec', $this->getParameter('host'));
     }
     if ($dsn = $this->getParameter('dsn')) {
         require_once 'creole/Creole.php';
         $params = Creole::parseDSN($dsn);
         $options = array('phptype', 'hostspec', 'database', 'username', 'password', 'port', 'protocol', 'encoding', 'persistent', 'socket', 'compat_assoc_lower', 'compat_rtrim_string');
         foreach ($options as $option) {
             if (!$this->getParameter($option) && isset($params[$option])) {
                 $this->setParameter($option, $params[$option]);
             }
         }
     }
     self::$config['propel']['datasources'][$this->getParameter('datasource')] = array('adapter' => $this->getParameter('phptype'), 'connection' => array('phptype' => $this->getParameter('phptype'), 'hostspec' => $this->getParameter('hostspec'), 'database' => $this->getParameter('database'), 'username' => $this->getParameter('username'), 'password' => $this->getParameter('password'), 'port' => $this->getParameter('port'), 'encoding' => $this->getParameter('encoding'), 'persistent' => $this->getParameter('persistent'), 'protocol' => $this->getParameter('protocol'), 'socket' => $this->getParameter('socket'), 'compat_assoc_lower' => $this->getParameter('compat_assoc_lower'), 'compat_rtrim_string' => $this->getParameter('compat_rtrim_string')));
 }
 /**
  * Test different URL DSNs to make sure they are being parsed correctly.
  */
 public function testParseDSN()
 {
     $dsn = "phptype://*****:*****@protocol+hostspec:110//usr/db_file.db?param1=value1&param2=value2";
     $dsninfo = Creole::parseDSN($dsn);
     $this->checkDSN($dsninfo, array('phptype', 'username', 'password', 'protocol', 'hostspec', 'port' => 110, 'database' => '/usr/db_file.db', 'param1' => 'value1', 'param2' => 'value2'));
     $dsn = 'phptype://*****:*****@hostspec/C:\\path\\to\\dbfile.db';
     $this->checkDSN(Creole::parseDSN($dsn), array('phptype', 'username', 'password', 'hostspec', 'database' => 'C:\\path\\to\\dbfile.db'));
     $dsn = "phptype://*****:*****@hostspec/database";
     $this->checkDSN(Creole::parseDSN($dsn), array('phptype', 'username', 'password', 'hostspec', 'database'));
     $dsn = "phptype://*****:*****@hostspec";
     $this->checkDSN(Creole::parseDSN($dsn), array('phptype', 'username', 'password', 'hostspec'));
     $dsn = "phptype://username@hostspec";
     $this->checkDSN(Creole::parseDSN($dsn), array('phptype', 'username', 'hostspec'));
     $dsn = "phptype://hostspec/database";
     $this->checkDSN(Creole::parseDSN($dsn), array('phptype', 'hostspec', 'database'));
     $dsn = "phptype";
     $this->checkDSN(Creole::parseDSN($dsn), array('phptype'));
 }
 /**
  * Creates a new Connection as using the driver, url, userid and password specified.
  * The calling method is responsible for closing the connection.
  * @return Connection the newly created connection.
  * @throws BuildException if the UserId/Password/Url is not set or there is no suitable driver or the driver fails to load.
  */
 protected function getConnection()
 {
     if ($this->url === null) {
         throw new BuildException("Url attribute must be set!", $this->location);
     }
     try {
         $this->log("Connecting to " . $this->getUrl(), PROJECT_MSG_VERBOSE);
         $info = new Properties();
         $dsn = Creole::parseDSN($this->url);
         if (!isset($dsn["username"]) && $this->userId === null) {
             throw new BuildException("Username must be in URL or userid attribute must be set.", $this->location);
         }
         if ($this->userId) {
             $dsn["username"] = $this->getUserId();
         }
         if ($this->password) {
             $dsn["password"] = $this->getPassword();
         }
         if ($this->driver) {
             Creole::registerDriver($dsn['phptype'], $this->driver);
         }
         $conn = Creole::getConnection($dsn);
         $conn->setAutoCommit($this->autocommit);
         return $conn;
     } catch (SQLException $e) {
         throw new BuildException($e->getMessage(), $this->location);
     }
 }
Esempio n. 5
0
 /**
  * Iterates through each datamodel/database, dumps the contents of all tables and creates a DOM XML doc.
  *
  * @return void
  * @throws BuildException
  */
 public function main()
 {
     $this->validate();
     $buf = "Database settings:\n" . " driver: " . ($this->databaseDriver ? $this->databaseDriver : "(default)") . "\n" . " URL: " . $this->databaseUrl . "\n" . ($this->databaseUser ? " user: "******"\n" : "") . ($this->databasePassword ? " password: "******"\n" : "");
     // deprecated
     $this->log($buf, PROJECT_MSG_VERBOSE);
     // 1) First create the Data XML -> database name map.
     $this->createDataDbMap();
     // 2) Now go create the XML files from teh database(s)
     foreach ($this->getDataModels() as $dataModel) {
         // there is really one 1 db per datamodel
         foreach ($dataModel->getDatabases() as $database) {
             // if database name is specified, then we only want to dump that one db.
             if (empty($this->databaseName) || $this->databaseName && $database->getName() == $this->databaseName) {
                 $outFile = $this->getMappedFile($dataModel->getName());
                 $this->log("Dumping data to XML for database: " . $database->getName());
                 $this->log("Writing to XML file: " . $outFile->getName());
                 try {
                     $url = str_replace("@DB@", $database->getName(), $this->databaseUrl);
                     $buf = "Database settings:\n" . " driver: " . ($this->databaseDriver ? $this->databaseDriver : "(default)") . "\n" . " URL: " . $url . "\n" . ($this->databaseUser ? " user: "******"\n" : "") . ($this->databasePassword ? " password: "******"\n" : "");
                     $this->log($buf, PROJECT_MSG_VERBOSE);
                     $dsn = Creole::parseDSN($url);
                     // deprecated, but here for BC
                     if ($this->databaseUser) {
                         $dsn['username'] = $this->databaseUser;
                     }
                     if ($this->databasePassword) {
                         $dsn['password'] = $this->databasePassword;
                     }
                     if ($this->databaseName) {
                         $dsn['database'] = $this->databaseName;
                     }
                     if ($this->databaseDriver) {
                         Creole::registerDriver($dsn['phptype'], $this->databaseDriver);
                     }
                     $this->conn = Creole::getConnection($dsn);
                     $doc = $this->createXMLDoc($database);
                     $doc->save($outFile->getAbsolutePath());
                 } catch (SQLException $se) {
                     $this->log("SQLException while connecting to DB: " . $se->getMessage(), PROJECT_MSG_ERR);
                     throw new BuildException($se);
                 }
             }
             // if databaseName && database->getName == databaseName
         }
         // foreach database
     }
     // foreach datamodel
 }
 /**
  * Take the base url, the target database and insert a set of SQL
  * files into the target database.
  *
  * @param      string $url
  * @param      string $database
  * @param      array $transactions
  */
 private function insertDatabaseSqlFiles($url, $database, $transactions)
 {
     $url = str_replace("@DB@", $database, $url);
     $this->log("Our new url -> " . $url);
     try {
         $buf = "Database settings:\n" . " driver: " . ($this->driver ? $this->driver : "(default)") . "\n" . " URL: " . $url . "\n" . ($this->userId ? " user: "******"\n" : "") . ($this->password ? " password: "******"\n" : "");
         $this->log($buf, PROJECT_MSG_VERBOSE);
         $dsn = Creole::parseDSN($url);
         if ($this->userId) {
             $dsn["username"] = $this->userId;
         }
         if ($this->password) {
             $dsn["password"] = $this->password;
         }
         if ($this->driver) {
             Creole::registerDriver($dsn['phptype'], $this->driver);
         }
         $this->conn = Creole::getConnection($dsn);
         $this->conn->setAutoCommit($this->autocommit);
         $this->statement = $this->conn->createStatement();
         $out = null;
         try {
             if ($this->output !== null) {
                 $this->log("Opening PrintStream to output file " . $this->output->__toString(), PROJECT_MSG_VERBOSE);
                 $out = new FileWriter($this->output);
             }
             // Process all transactions
             for ($i = 0, $size = count($transactions); $i < $size; $i++) {
                 $transactions[$i]->runTransaction($out);
                 if (!$this->autocommit) {
                     $this->log("Commiting transaction", PROJECT_MSG_VERBOSE);
                     $this->conn->commit();
                 }
             }
         } catch (Exception $e) {
             if ($out) {
                 $out->close();
             }
         }
     } catch (IOException $e) {
         if (!$this->autocommit && $this->conn !== null && $this->onError == "abort") {
             try {
                 $this->conn->rollback();
             } catch (SQLException $ex) {
                 // do nothing.
                 System::println("Rollback failed.");
             }
         }
         if ($this->statement) {
             $this->statement->close();
         }
         throw new BuildException($e);
     } catch (SQLException $e) {
         if (!$this->autocommit && $this->conn !== null && $this->onError == "abort") {
             try {
                 $this->conn->rollback();
             } catch (SQLException $ex) {
                 // do nothing.
                 System::println("Rollback failed");
             }
         }
         if ($this->statement) {
             $this->statement->close();
         }
         throw new BuildException($e);
     }
     $this->statement->close();
     $this->log($this->goodSql . " of " . $this->totalSql . " SQL statements executed successfully");
 }
 /**
  * Establishes a Creole database connection
  *
  * @return     object The connection
  */
 protected function getConnection()
 {
     // Attemtp to connect to a database.
     $this->dsn = Creole::parseDSN($this->dbUrl);
     if ($this->dbUser) {
         $this->dsn["username"] = $this->dbUser;
     }
     if ($this->dbPassword) {
         $this->dsn["password"] = $this->dbPassword;
     }
     if ($this->dbDriver) {
         Creole::registerDriver($this->dsn['phptype'], $this->dbDriver);
     }
     $con = Creole::getConnection($this->dsn);
     $this->log("DB connection established");
     return $con;
 }