Ejemplo n.º 1
0
 public function testQuery()
 {
     $q = new DropDatabaseQuery('test123123');
     $q->ifExists();
     $this->assertQuery($q);
     $q = new CreateDatabaseQuery('test123123');
     $q->characterSet('utf8');
     $this->assertSql("CREATE DATABASE `test123123` CHARACTER SET 'utf8'", $q);
     $this->assertQuery($q);
     $q = new DropDatabaseQuery('test123123');
     $this->assertSql("DROP DATABASE `test123123`", $q);
     $this->assertQuery($q);
 }
Ejemplo n.º 2
0
 public function execute()
 {
     $configLoader = $this->getConfigLoader(true);
     $dsId = $this->getCurrentDataSourceId();
     $ds = $configLoader->getDataSource($dsId);
     if (!isset($ds['dsn'])) {
         throw new Exception("Attribute 'dsn' undefined in data source settings.");
     }
     $dsnParser = new DSNParser();
     $dsn = $dsnParser->parse($ds['dsn']);
     $dbName = $dsn->getAttribute('dbname');
     $dsn->removeAttribute('dbname');
     $this->logger->debug("Connection DSN: " . $dsn);
     $pdo = new PDO($dsn, @$ds['user'], @$ds['pass'], @$ds['connection_options']);
     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $queryDriver = PDODriverFactory::create($pdo);
     if ($queryDriver instanceof PDOSQLiteDriver) {
         $this->logger->info('Create database query is not supported by sqlite. ths sqlite database shall have been created.');
         return true;
     }
     $q = new CreateDatabaseQuery($dbName);
     $q->ifNotExists();
     if (isset($ds['charset'])) {
         $q->characterSet($ds['charset']);
     } else {
         $q->characterSet('utf8');
     }
     $sql = $q->toSql($queryDriver, new ArgumentArray());
     $this->logger->info($sql);
     if ($pdo->query($sql) === false) {
         list($statusCode, $errorCode, $message) = $pdo->errorInfo();
         $this->logger->error("{$statusCode}:{$errorCode} {$message}");
         return false;
     }
     $this->logger->info('Database created successfully.');
 }