2 declare(strict_types = 1);
18 use Doctrine\Common\EventManager;
19 use Doctrine\DBAL\Configuration;
20 use Doctrine\DBAL\Driver;
21 use Doctrine\DBAL\Driver\Statement;
22 use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
23 use Doctrine\DBAL\Platforms\SQLServer2012Platform;
24 use Psr\Log\LoggerAwareInterface;
25 use Psr\Log\LoggerAwareTrait;
30 class Connection extends \Doctrine\DBAL\Connection implements LoggerAwareInterface
79 public function __construct(array $params, Driver $driver, Configuration $config =
null, EventManager $em =
null)
81 parent::__construct($params, $driver, $config, $em);
82 $this->_expr = GeneralUtility::makeInstance(ExpressionBuilder::class, $this);
93 if (!parent::connect()) {
97 foreach ($this->prepareConnectionCommands as $command) {
98 if ($this->executeUpdate($command) ===
false) {
99 $this->logger->critical(
'Could not initialize DB connection with query "' . $command .
'": ' . $this->errorInfo());
113 return GeneralUtility::makeInstance(QueryBuilder::class, $this);
129 if ($identifier ===
'*') {
133 return parent::quoteIdentifier($identifier);
147 return array_map([$this,
'quoteIdentifier'], $input);
162 return array_combine($this->
quoteIdentifiers(array_keys($input)), array_values($input));
175 if (!is_string(key($input))) {
194 public function insert($tableName, array $data, array $types = []): int
196 return parent::insert(
216 public function bulkInsert(
string $tableName, array $data, array $columns = [], array $types = []): int
218 $query = GeneralUtility::makeInstance(Query\BulkInsertQuery::class, $this, $tableName, $columns);
219 foreach ($data as $values) {
220 $query->addValues($values, $types);
223 return $query->execute();
245 array $identifiers = [],
252 $query->select(...$columns)
255 foreach ($identifiers as $identifier => $value) {
256 $query->andWhere($query->expr()->eq($identifier, $query->createNamedParameter($value)));
259 foreach ($orderBy as $fieldName => $order) {
260 $query->addOrderBy($fieldName, $order);
263 if (!empty($groupBy)) {
264 $query->groupBy(...$groupBy);
268 $query->setMaxResults($limit);
269 $query->setFirstResult($offset);
272 return $query->execute();
288 public function update($tableName, array $data, array $identifier, array $types = []): int
290 return parent::update(
310 public function delete($tableName, array $identifier, array $types = []):
int
312 return parent::delete(
330 public function truncate(
string $tableName,
bool $cascade =
false): int
332 return $this->executeUpdate(
333 $this->getDatabasePlatform()->getTruncateTableSQL(
349 public function count(
string $item,
string $tableName, array $identifiers): int
355 foreach ($identifiers as $identifier => $value) {
356 $query->andWhere($query->expr()->eq($identifier, $query->createNamedParameter($value)));
359 return (
int)$query->execute()->fetchColumn(0);
373 $version = $this->getDatabasePlatform()->getName();
377 case 'drizzle_pdo_mysql':
381 case 'pdo_postgresql':
382 $version =
'PostgreSQL';
390 $version =
'SQLServer';
395 if (!$this->getDriver() instanceof \Doctrine\DBAL\VersionAwarePlatformDriver) {
399 if ($this->getWrappedConnection() instanceof \Doctrine\DBAL\Driver\ServerInfoAwareConnection
400 && !$this->getWrappedConnection()->requiresQueryForServerVersion()
402 $version .=
' ' . $this->getWrappedConnection()->getServerVersion();
415 if (empty($commands)) {
419 $this->prepareConnectionCommands = GeneralUtility::trimExplode(
440 public function lastInsertId($tableName =
null,
string $fieldName =
'uid'): string
442 $databasePlatform = $this->getDatabasePlatform();
443 if ($databasePlatform instanceof PostgreSqlPlatform) {
444 return parent::lastInsertId(trim(implode(
'_', [$tableName, $fieldName,
'seq']),
'_'));
446 if ($databasePlatform instanceof SQLServer2012Platform) {
450 return (
string)parent::lastInsertId();
453 return (
string)parent::lastInsertId($tableName);