| 1409 | | $manager = $this->getManager(); |
| 1410 | | |
| 1411 | | $info = $manager->parsePdoDsn($this->getOption('dsn')); |
| 1412 | | $username = $this->getOption('username'); |
| 1413 | | $password = $this->getOption('password'); |
| 1414 | | |
| 1415 | | // Make connection without database specified so we can create it |
| 1416 | | $connect = $manager->openConnection(new PDO($info['scheme'] . ':host=' . $info['host'], $username, $password), 'tmp_connection', false); |
| 1417 | | |
| | 1411 | try { |
| | 1412 | if ( ! $dsn = $this->getOption('dsn')) { |
| | 1413 | throw new Doctrine_Connection_Exception('You must create your Doctrine_Connection by using a valid Doctrine style dsn in order to use the create/drop database functionality'); |
| | 1414 | } |
| | 1415 | |
| | 1416 | $manager = $this->getManager(); |
| | 1417 | |
| | 1418 | $info = $manager->parsePdoDsn($dsn); |
| | 1419 | $username = $this->getOption('username'); |
| | 1420 | $password = $this->getOption('password'); |
| | 1421 | |
| | 1422 | // Make connection without database specified so we can create it |
| | 1423 | $connect = $manager->openConnection(new PDO($info['scheme'] . ':host=' . $info['host'], $username, $password), 'tmp_connection', false); |
| | 1424 | |
| | 1425 | // Create database |
| | 1426 | $connect->export->createDatabase($info['dbname']); |
| | 1427 | |
| | 1428 | // Close the tmp connection with no database |
| | 1429 | $manager->closeConnection($connect); |
| | 1430 | |
| | 1431 | // Close original connection |
| | 1432 | $manager->closeConnection($this); |
| | 1433 | |
| | 1434 | // Reopen original connection with newly created database |
| | 1435 | $manager->openConnection(new PDO($info['dsn'], $username, $password), $this->getName(), true); |
| | 1436 | |
| | 1437 | return 'Successfully created database for connection "' . $this->getName() . '" named "' . $info['dbname'] . '"'; |
| | 1438 | } catch (Exception $e) { |
| | 1439 | return $e; |
| | 1440 | } |
| | 1441 | } |
| | 1442 | |
| | 1443 | /** |
| | 1444 | * dropDatabase |
| | 1445 | * |
| | 1446 | * Method for dropping the database for the connection instance |
| | 1447 | * |
| | 1448 | * @return mixed Will return an instance of the exception thrown if the drop database fails, otherwise it returns a string detailing the success |
| | 1449 | */ |
| | 1450 | public function dropDatabase() |
| | 1451 | { |
| 1419 | | // Create database |
| 1420 | | $connect->export->createDatabase($info['dbname']); |
| 1421 | | |
| 1422 | | // Close the tmp connection with no database |
| 1423 | | $manager->closeConnection($connect); |
| 1424 | | |
| 1425 | | // Close original connection |
| 1426 | | $manager->closeConnection($this); |
| 1427 | | |
| 1428 | | // Reopen original connection with newly created database |
| 1429 | | $manager->openConnection(new PDO($info['dsn'], $username, $password), $this->getName(), true); |
| 1430 | | |
| 1431 | | return 'Successfully created database for connection "' . $this->getName() . '" named "' . $info['dbname'] . '"'; |
| 1432 | | } catch (Exception $e) { |
| 1433 | | return $e; |
| 1434 | | } |
| 1435 | | } |
| 1436 | | |
| 1437 | | /** |
| 1438 | | * dropDatabase |
| 1439 | | * |
| 1440 | | * @return void |
| 1441 | | */ |
| 1442 | | public function dropDatabase() |
| 1443 | | { |
| 1444 | | try { |
| 1445 | | $info = $this->getManager()->parsePdoDsn($this->getOption('dsn')); |
| | 1453 | if ( ! $dsn = $this->getOption('dsn')) { |
| | 1454 | throw new Doctrine_Connection_Exception('You must create your Doctrine_Connection by using a valid Doctrine style dsn in order to use the create/drop database functionality'); |
| | 1455 | } |
| | 1456 | |
| | 1457 | $info = $this->getManager()->parsePdoDsn($dsn); |