/** * When entity have columns for required associations, this will fail. * Calls $em->flush(). * * Warning: You must NOT use the passed entity further in your application. * Use the returned one instead! * * @todo fix error codes! PDO is returning database-specific codes * * @param object $entity * @throws \Doctrine\DBAL\DBALException * @throws \Exception * @return bool|object */ public function persist($entity) { $this->db->beginTransaction(); try { $persisted = $this->doInsert($entity); $this->db->commit(); return $persisted; } catch (Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) { $this->db->rollback(); return FALSE; } catch (Kdyby\Doctrine\DuplicateEntryException $e) { $this->db->rollback(); return FALSE; } catch (DBALException $e) { $this->db->rollback(); if ($this->isUniqueConstraintViolation($e)) { return FALSE; } throw $this->db->resolveException($e); } catch (\Exception $e) { $this->db->rollback(); throw $e; } catch (\Throwable $e) { $this->db->rollback(); throw $e; } }
public function __construct(array $params, DBAL\Driver $driver, DBAL\Configuration $config = NULL, Common\EventManager $eventManager = NULL) { $container = \Testbench\ContainerFactory::create(FALSE); $this->onConnect[] = function (DoctrineConnectionMock $connection) use($container) { if ($this->__testbench_databaseName !== NULL) { //already initialized (needed for pgsql) return; } try { $config = $container->parameters['testbench']; if ($config['shareDatabase'] === TRUE) { $registry = new \Testbench\DatabasesRegistry(); $dbName = $container->parameters['testbench']['dbprefix'] . getenv(\Tester\Environment::THREAD); if ($registry->registerDatabase($dbName)) { $this->__testbench_database_setup($connection, $container, TRUE); } else { $this->__testbench_databaseName = $dbName; $this->__testbench_database_change($connection, $container); } } else { // always create new test database $this->__testbench_database_setup($connection, $container); } } catch (\Exception $e) { \Tester\Assert::fail($e->getMessage()); } }; parent::__construct($params, $driver, $config, $eventManager); }
private function tearDownDatabase() { if ($this->databaseName !== '') { $schemaManager = $this->connection->getSchemaManager(); $schemaManager->dropDatabase($this->databaseName); } }
private function createDatabase() { $databaseName = $this->namePrefix . '_shared'; $schemaManager = $this->connection->getSchemaManager(); $this->lock->acquire(__FILE__); $isDatabaseCreated = in_array($databaseName, $schemaManager->listDatabases(), TRUE); if ($isDatabaseCreated) { $this->lock->release(__FILE__); $this->connection->exec("USE `{$databaseName}`"); } else { try { $schemaManager->createDatabase($databaseName); $this->connection->exec("USE `{$databaseName}`"); $this->dataLoader->loadFiles($this->connection, $this->schemaFiles); } catch (\Exception $e) { $schemaManager->dropDatabase($databaseName); throw $e; } finally { $this->lock->release(__FILE__); } } }
/** * do not use, only used by Statement-class * needs to be public for access from the Statement-class * * @deprecated */ public function prepareUnwrapped($sql) { // returns the actual statement return parent::prepare($sql); }
/** * @author David Grudl * @see https://github.com/dg/dibi/blob/cde5af7cbe02d231fe2d3f904fc2c3d3eeda66f0/dibi/libs/DibiConnection.php#L630 */ public static function loadFromFile(Connection $connection, $file, $callback = NULL) { @set_time_limit(0); // intentionally @ if (!($handle = @fopen($file, 'r'))) { // intentionally @ throw new InvalidArgumentException("Cannot open file '{$file}'."); } $count = 0; $delimiter = ';'; $sql = ''; while (!feof($handle)) { $s = rtrim(fgets($handle)); if (substr($s, 0, 10) === 'DELIMITER ') { $delimiter = substr($s, 10); } elseif (substr($s, -strlen($delimiter)) === $delimiter) { $sql .= substr($s, 0, -strlen($delimiter)); if ($callback) { call_user_func($callback, $sql, ftell($handle)); } $connection->query($sql); $sql = ''; $count++; } else { $sql .= $s . "\n"; } } if (trim($sql) !== '') { if ($callback) { call_user_func($callback, $sql, ftell($handle)); } $connection->query($sql); $count++; } fclose($handle); return $count; }
public function unwrap() { parent::rollBack(); parent::setTransactionIsolation($this->oldIsolationLevel); $this->isWrapped = FALSE; }
public function connect() { if (parent::connect()) { $this->onConnect($this); } }