| 1383 | | if ( ! $dsn = $this->getOption('dsn')) { |
| 1384 | | 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'); |
| 1385 | | } |
| 1386 | | |
| 1387 | | $manager = $this->getManager(); |
| 1388 | | |
| 1389 | | $info = $manager->parsePdoDsn($dsn); |
| 1390 | | $username = $this->getOption('username'); |
| 1391 | | $password = $this->getOption('password'); |
| 1392 | | |
| 1393 | | // Make connection without database specified so we can create it |
| 1394 | | $connect = $manager->openConnection(new PDO($info['scheme'] . ':host=' . $info['host'], $username, $password), 'tmp_connection', false); |
| 1395 | | |
| 1396 | | // Create database |
| | 1387 | // Parse pdo dsn so we are aware of the connection information parts |
| | 1388 | $info = $this->getManager()->parsePdoDsn($dsn); |
| | 1389 | |
| | 1390 | // Get the temporary connection to issue the drop database command |
| | 1391 | $connect = $this->getTmpConnection($info); |
| | 1392 | |
| | 1393 | // Issue create database command |
| 1399 | | // Close the tmp connection with no database |
| 1400 | | $manager->closeConnection($connect); |
| 1401 | | |
| 1402 | | // Close original connection |
| 1403 | | $manager->closeConnection($this); |
| 1404 | | |
| 1405 | | // Reopen original connection with newly created database |
| 1406 | | $manager->openConnection(new PDO($info['dsn'], $username, $password), $this->getName(), true); |
| | 1396 | // Close the temporary connection used to issue the drop database command |
| | 1397 | $this->getManager()->closeConnection($connect); |
| | 1398 | |
| | 1399 | // Close original |
| | 1400 | $this->getManager()->closeConnection($this); |
| | 1401 | |
| | 1402 | // Re-open connection with the newly created database |
| | 1403 | $this->getManager()->openConnection(new PDO($dsn, $this->getOption('username'), $this->getOption('password')), $this->getName(), true); |
| 1423 | | try { |
| 1424 | | if ( ! $dsn = $this->getOption('dsn')) { |
| 1425 | | 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'); |
| 1426 | | } |
| 1427 | | |
| 1428 | | $info = $this->getManager()->parsePdoDsn($dsn); |
| 1429 | | |
| 1430 | | $this->export->dropDatabase($info['dbname']); |
| 1431 | | |
| 1432 | | return 'Successfully dropped database for connection "' . $this->getName() . '" named "' . $info['dbname'] . '"'; |
| 1433 | | } catch (Exception $e) { |
| 1434 | | return $e; |
| 1435 | | } |
| | 1420 | if ( ! $dsn = $this->getOption('dsn')) { |
| | 1421 | 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'); |
| | 1422 | } |
| | 1423 | |
| | 1424 | try { |
| | 1425 | // Parse pdo dsn so we are aware of the connection information parts |
| | 1426 | $info = $this->getManager()->parsePdoDsn($dsn); |
| | 1427 | |
| | 1428 | // Get the temporary connection to issue the drop database command |
| | 1429 | $connect = $this->getTmpConnection($info); |
| | 1430 | |
| | 1431 | // Issue drop database command |
| | 1432 | $connect->export->dropDatabase($info['dbname']); |
| | 1433 | |
| | 1434 | // Close the temporary connection used to issue the drop database command |
| | 1435 | $this->getManager()->closeConnection($connect); |
| | 1436 | |
| | 1437 | return 'Successfully dropped database for connection "' . $this->getName() . '" named "' . $info['dbname'] . '"'; |
| | 1438 | } catch (Exception $e) { |
| | 1439 | return $e; |
| | 1440 | } |
| | 1441 | } |
| | 1442 | |
| | 1443 | /** |
| | 1444 | * getTmpConnection |
| | 1445 | * |
| | 1446 | * @param string $info |
| | 1447 | * @return void |
| | 1448 | */ |
| | 1449 | public function getTmpConnection($info) |
| | 1450 | { |
| | 1451 | $pdoDsn = $info['scheme'] . ':host=' . $info['host']; |
| | 1452 | |
| | 1453 | if (isset($this->export->tmpConnectionDatabase) && $this->export->tmpConnectionDatabase) { |
| | 1454 | $pdoDsn .= ';dbname=' . $this->export->tmpConnectionDatabase; |
| | 1455 | } |
| | 1456 | |
| | 1457 | $username = $this->getOption('username'); |
| | 1458 | $password = $this->getOption('password'); |
| | 1459 | |
| | 1460 | return $this->getManager()->openConnection(new PDO($pdoDsn, $username, $password), 'doctrine_tmp_connection', false); |