| | 1300 | public function createDatabase() |
| | 1301 | { |
| | 1302 | try { |
| | 1303 | if ( ! $dsn = $this->getOption('dsn')) { |
| | 1304 | 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'); |
| | 1305 | } |
| | 1306 | |
| | 1307 | $manager = $this->getManager(); |
| | 1308 | |
| | 1309 | $info = $manager->parsePdoDsn($dsn); |
| | 1310 | $username = $this->getOption('username'); |
| | 1311 | $password = $this->getOption('password'); |
| | 1312 | |
| | 1313 | // Make connection without database specified so we can create it |
| | 1314 | $connect = $manager->openConnection(new PDO($info['scheme'] . ':host=' . $info['host'], $username, $password), 'tmp_connection', false); |
| | 1315 | |
| | 1316 | // Create database |
| | 1317 | $connect->export->createDatabase($info['dbname']); |
| | 1318 | |
| | 1319 | // Close the tmp connection with no database |
| | 1320 | $manager->closeConnection($connect); |
| | 1321 | |
| | 1322 | // Close original connection |
| | 1323 | $manager->closeConnection($this); |
| | 1324 | |
| | 1325 | // Reopen original connection with newly created database |
| | 1326 | $manager->openConnection(new PDO($info['dsn'], $username, $password), $this->getName(), true); |
| | 1327 | |
| | 1328 | return 'Successfully created database for connection "' . $this->getName() . '" named "' . $info['dbname'] . '"'; |
| | 1329 | } catch (Exception $e) { |
| | 1330 | return $e; |
| | 1331 | } |
| | 1332 | } |
| | 1333 | |
| | 1334 | /** |
| | 1335 | * dropDatabase |
| | 1336 | * |
| | 1337 | * Method for dropping the database for the connection instance |
| | 1338 | * |
| | 1339 | * @return mixed Will return an instance of the exception thrown if the drop database fails, otherwise it returns a string detailing the success |
| | 1340 | */ |
| | 1341 | public function dropDatabase() |
| | 1342 | { |
| | 1343 | try { |
| | 1344 | if ( ! $dsn = $this->getOption('dsn')) { |
| | 1345 | 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'); |
| | 1346 | } |
| | 1347 | |
| | 1348 | $info = $this->getManager()->parsePdoDsn($dsn); |
| | 1349 | |
| | 1350 | $this->export->dropDatabase($info['dbname']); |
| | 1351 | |
| | 1352 | return 'Successfully dropped database for connection "' . $this->getName() . '" named "' . $info['dbname'] . '"'; |
| | 1353 | } catch (Exception $e) { |
| | 1354 | return $e; |
| | 1355 | } |
| | 1356 | } |
| | 1357 | |