PdoAdapter.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Adapter;
  11. use Doctrine\DBAL\Connection;
  12. use Doctrine\DBAL\DBALException;
  13. use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
  14. use Doctrine\DBAL\DriverManager;
  15. use Doctrine\DBAL\Exception;
  16. use Doctrine\DBAL\Exception\TableNotFoundException;
  17. use Doctrine\DBAL\ParameterType;
  18. use Doctrine\DBAL\Schema\Schema;
  19. use Doctrine\DBAL\Statement;
  20. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  21. use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
  22. use Symfony\Component\Cache\Marshaller\MarshallerInterface;
  23. use Symfony\Component\Cache\PruneableInterface;
  24. class PdoAdapter extends AbstractAdapter implements PruneableInterface
  25. {
  26. protected $maxIdLength = 255;
  27. private $marshaller;
  28. private $conn;
  29. private $dsn;
  30. private $driver;
  31. private $serverVersion;
  32. private $table = 'cache_items';
  33. private $idCol = 'item_id';
  34. private $dataCol = 'item_data';
  35. private $lifetimeCol = 'item_lifetime';
  36. private $timeCol = 'item_time';
  37. private $username = '';
  38. private $password = '';
  39. private $connectionOptions = [];
  40. private $namespace;
  41. /**
  42. * You can either pass an existing database connection as PDO instance or
  43. * a Doctrine DBAL Connection or a DSN string that will be used to
  44. * lazy-connect to the database when the cache is actually used.
  45. *
  46. * When a Doctrine DBAL Connection is passed, the cache table is created
  47. * automatically when possible. Otherwise, use the createTable() method.
  48. *
  49. * List of available options:
  50. * * db_table: The name of the table [default: cache_items]
  51. * * db_id_col: The column where to store the cache id [default: item_id]
  52. * * db_data_col: The column where to store the cache data [default: item_data]
  53. * * db_lifetime_col: The column where to store the lifetime [default: item_lifetime]
  54. * * db_time_col: The column where to store the timestamp [default: item_time]
  55. * * db_username: The username when lazy-connect [default: '']
  56. * * db_password: The password when lazy-connect [default: '']
  57. * * db_connection_options: An array of driver-specific connection options [default: []]
  58. *
  59. * @param \PDO|Connection|string $connOrDsn a \PDO or Connection instance or DSN string or null
  60. *
  61. * @throws InvalidArgumentException When first argument is not PDO nor Connection nor string
  62. * @throws InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
  63. * @throws InvalidArgumentException When namespace contains invalid characters
  64. */
  65. public function __construct($connOrDsn, string $namespace = '', int $defaultLifetime = 0, array $options = [], MarshallerInterface $marshaller = null)
  66. {
  67. if (isset($namespace[0]) && preg_match('#[^-+.A-Za-z0-9]#', $namespace, $match)) {
  68. throw new InvalidArgumentException(sprintf('Namespace contains "%s" but only characters in [-+.A-Za-z0-9] are allowed.', $match[0]));
  69. }
  70. if ($connOrDsn instanceof \PDO) {
  71. if (\PDO::ERRMODE_EXCEPTION !== $connOrDsn->getAttribute(\PDO::ATTR_ERRMODE)) {
  72. throw new InvalidArgumentException(sprintf('"%s" requires PDO error mode attribute be set to throw Exceptions (i.e. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)).', __CLASS__));
  73. }
  74. $this->conn = $connOrDsn;
  75. } elseif ($connOrDsn instanceof Connection) {
  76. $this->conn = $connOrDsn;
  77. } elseif (\is_string($connOrDsn)) {
  78. $this->dsn = $connOrDsn;
  79. } else {
  80. throw new InvalidArgumentException(sprintf('"%s" requires PDO or Doctrine\DBAL\Connection instance or DSN string as first argument, "%s" given.', __CLASS__, get_debug_type($connOrDsn)));
  81. }
  82. $this->table = $options['db_table'] ?? $this->table;
  83. $this->idCol = $options['db_id_col'] ?? $this->idCol;
  84. $this->dataCol = $options['db_data_col'] ?? $this->dataCol;
  85. $this->lifetimeCol = $options['db_lifetime_col'] ?? $this->lifetimeCol;
  86. $this->timeCol = $options['db_time_col'] ?? $this->timeCol;
  87. $this->username = $options['db_username'] ?? $this->username;
  88. $this->password = $options['db_password'] ?? $this->password;
  89. $this->connectionOptions = $options['db_connection_options'] ?? $this->connectionOptions;
  90. $this->namespace = $namespace;
  91. $this->marshaller = $marshaller ?? new DefaultMarshaller();
  92. parent::__construct($namespace, $defaultLifetime);
  93. }
  94. /**
  95. * Creates the table to store cache items which can be called once for setup.
  96. *
  97. * Cache ID are saved in a column of maximum length 255. Cache data is
  98. * saved in a BLOB.
  99. *
  100. * @throws \PDOException When the table already exists
  101. * @throws DBALException When the table already exists
  102. * @throws Exception When the table already exists
  103. * @throws \DomainException When an unsupported PDO driver is used
  104. */
  105. public function createTable()
  106. {
  107. // connect if we are not yet
  108. $conn = $this->getConnection();
  109. if ($conn instanceof Connection) {
  110. $schema = new Schema();
  111. $this->addTableToSchema($schema);
  112. foreach ($schema->toSql($conn->getDatabasePlatform()) as $sql) {
  113. if (method_exists($conn, 'executeStatement')) {
  114. $conn->executeStatement($sql);
  115. } else {
  116. $conn->exec($sql);
  117. }
  118. }
  119. return;
  120. }
  121. switch ($this->driver) {
  122. case 'mysql':
  123. // We use varbinary for the ID column because it prevents unwanted conversions:
  124. // - character set conversions between server and client
  125. // - trailing space removal
  126. // - case-insensitivity
  127. // - language processing like é == e
  128. $sql = "CREATE TABLE $this->table ($this->idCol VARBINARY(255) NOT NULL PRIMARY KEY, $this->dataCol MEDIUMBLOB NOT NULL, $this->lifetimeCol INTEGER UNSIGNED, $this->timeCol INTEGER UNSIGNED NOT NULL) COLLATE utf8mb4_bin, ENGINE = InnoDB";
  129. break;
  130. case 'sqlite':
  131. $sql = "CREATE TABLE $this->table ($this->idCol TEXT NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
  132. break;
  133. case 'pgsql':
  134. $sql = "CREATE TABLE $this->table ($this->idCol VARCHAR(255) NOT NULL PRIMARY KEY, $this->dataCol BYTEA NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
  135. break;
  136. case 'oci':
  137. $sql = "CREATE TABLE $this->table ($this->idCol VARCHAR2(255) NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
  138. break;
  139. case 'sqlsrv':
  140. $sql = "CREATE TABLE $this->table ($this->idCol VARCHAR(255) NOT NULL PRIMARY KEY, $this->dataCol VARBINARY(MAX) NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
  141. break;
  142. default:
  143. throw new \DomainException(sprintf('Creating the cache table is currently not implemented for PDO driver "%s".', $this->driver));
  144. }
  145. if (method_exists($conn, 'executeStatement')) {
  146. $conn->executeStatement($sql);
  147. } else {
  148. $conn->exec($sql);
  149. }
  150. }
  151. /**
  152. * Adds the Table to the Schema if the adapter uses this Connection.
  153. */
  154. public function configureSchema(Schema $schema, Connection $forConnection): void
  155. {
  156. // only update the schema for this connection
  157. if ($forConnection !== $this->getConnection()) {
  158. return;
  159. }
  160. if ($schema->hasTable($this->table)) {
  161. return;
  162. }
  163. $this->addTableToSchema($schema);
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function prune()
  169. {
  170. $deleteSql = "DELETE FROM $this->table WHERE $this->lifetimeCol + $this->timeCol <= :time";
  171. if ('' !== $this->namespace) {
  172. $deleteSql .= " AND $this->idCol LIKE :namespace";
  173. }
  174. $connection = $this->getConnection();
  175. $useDbalConstants = $connection instanceof Connection;
  176. try {
  177. $delete = $connection->prepare($deleteSql);
  178. } catch (TableNotFoundException $e) {
  179. return true;
  180. } catch (\PDOException $e) {
  181. return true;
  182. }
  183. $delete->bindValue(':time', time(), $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  184. if ('' !== $this->namespace) {
  185. $delete->bindValue(':namespace', sprintf('%s%%', $this->namespace), $useDbalConstants ? ParameterType::STRING : \PDO::PARAM_STR);
  186. }
  187. try {
  188. // Doctrine DBAL ^2.13 || >= 3.1
  189. if ($delete instanceof Statement && method_exists($delete, 'executeStatement')) {
  190. $delete->executeStatement();
  191. return true;
  192. }
  193. return $delete->execute();
  194. } catch (TableNotFoundException $e) {
  195. return true;
  196. } catch (\PDOException $e) {
  197. return true;
  198. }
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. protected function doFetch(array $ids)
  204. {
  205. $connection = $this->getConnection();
  206. $useDbalConstants = $connection instanceof Connection;
  207. $now = time();
  208. $expired = [];
  209. $sql = str_pad('', (\count($ids) << 1) - 1, '?,');
  210. $sql = "SELECT $this->idCol, CASE WHEN $this->lifetimeCol IS NULL OR $this->lifetimeCol + $this->timeCol > ? THEN $this->dataCol ELSE NULL END FROM $this->table WHERE $this->idCol IN ($sql)";
  211. $stmt = $connection->prepare($sql);
  212. $stmt->bindValue($i = 1, $now, $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  213. foreach ($ids as $id) {
  214. $stmt->bindValue(++$i, $id);
  215. }
  216. $result = $stmt->execute();
  217. if (\is_object($result)) {
  218. $result = $result->iterateNumeric();
  219. } else {
  220. $stmt->setFetchMode(\PDO::FETCH_NUM);
  221. $result = $stmt;
  222. }
  223. foreach ($result as $row) {
  224. if (null === $row[1]) {
  225. $expired[] = $row[0];
  226. } else {
  227. yield $row[0] => $this->marshaller->unmarshall(\is_resource($row[1]) ? stream_get_contents($row[1]) : $row[1]);
  228. }
  229. }
  230. if ($expired) {
  231. $sql = str_pad('', (\count($expired) << 1) - 1, '?,');
  232. $sql = "DELETE FROM $this->table WHERE $this->lifetimeCol + $this->timeCol <= ? AND $this->idCol IN ($sql)";
  233. $stmt = $connection->prepare($sql);
  234. $stmt->bindValue($i = 1, $now, $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  235. foreach ($expired as $id) {
  236. $stmt->bindValue(++$i, $id);
  237. }
  238. $stmt->execute();
  239. }
  240. }
  241. /**
  242. * {@inheritdoc}
  243. */
  244. protected function doHave(string $id)
  245. {
  246. $connection = $this->getConnection();
  247. $useDbalConstants = $connection instanceof Connection;
  248. $sql = "SELECT 1 FROM $this->table WHERE $this->idCol = :id AND ($this->lifetimeCol IS NULL OR $this->lifetimeCol + $this->timeCol > :time)";
  249. $stmt = $connection->prepare($sql);
  250. $stmt->bindValue(':id', $id);
  251. $stmt->bindValue(':time', time(), $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  252. $result = $stmt->execute();
  253. return (bool) (\is_object($result) ? $result->fetchOne() : $stmt->fetchColumn());
  254. }
  255. /**
  256. * {@inheritdoc}
  257. */
  258. protected function doClear(string $namespace)
  259. {
  260. $conn = $this->getConnection();
  261. if ('' === $namespace) {
  262. if ('sqlite' === $this->driver) {
  263. $sql = "DELETE FROM $this->table";
  264. } else {
  265. $sql = "TRUNCATE TABLE $this->table";
  266. }
  267. } else {
  268. $sql = "DELETE FROM $this->table WHERE $this->idCol LIKE '$namespace%'";
  269. }
  270. try {
  271. if (method_exists($conn, 'executeStatement')) {
  272. $conn->executeStatement($sql);
  273. } else {
  274. $conn->exec($sql);
  275. }
  276. } catch (TableNotFoundException $e) {
  277. } catch (\PDOException $e) {
  278. }
  279. return true;
  280. }
  281. /**
  282. * {@inheritdoc}
  283. */
  284. protected function doDelete(array $ids)
  285. {
  286. $sql = str_pad('', (\count($ids) << 1) - 1, '?,');
  287. $sql = "DELETE FROM $this->table WHERE $this->idCol IN ($sql)";
  288. try {
  289. $stmt = $this->getConnection()->prepare($sql);
  290. $stmt->execute(array_values($ids));
  291. } catch (TableNotFoundException $e) {
  292. } catch (\PDOException $e) {
  293. }
  294. return true;
  295. }
  296. /**
  297. * {@inheritdoc}
  298. */
  299. protected function doSave(array $values, int $lifetime)
  300. {
  301. if (!$values = $this->marshaller->marshall($values, $failed)) {
  302. return $failed;
  303. }
  304. $conn = $this->getConnection();
  305. $useDbalConstants = $conn instanceof Connection;
  306. $driver = $this->driver;
  307. $insertSql = "INSERT INTO $this->table ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (:id, :data, :lifetime, :time)";
  308. switch (true) {
  309. case 'mysql' === $driver:
  310. $sql = $insertSql." ON DUPLICATE KEY UPDATE $this->dataCol = VALUES($this->dataCol), $this->lifetimeCol = VALUES($this->lifetimeCol), $this->timeCol = VALUES($this->timeCol)";
  311. break;
  312. case 'oci' === $driver:
  313. // DUAL is Oracle specific dummy table
  314. $sql = "MERGE INTO $this->table USING DUAL ON ($this->idCol = ?) ".
  315. "WHEN NOT MATCHED THEN INSERT ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (?, ?, ?, ?) ".
  316. "WHEN MATCHED THEN UPDATE SET $this->dataCol = ?, $this->lifetimeCol = ?, $this->timeCol = ?";
  317. break;
  318. case 'sqlsrv' === $driver && version_compare($this->getServerVersion(), '10', '>='):
  319. // MERGE is only available since SQL Server 2008 and must be terminated by semicolon
  320. // It also requires HOLDLOCK according to http://weblogs.sqlteam.com/dang/archive/2009/01/31/UPSERT-Race-Condition-With-MERGE.aspx
  321. $sql = "MERGE INTO $this->table WITH (HOLDLOCK) USING (SELECT 1 AS dummy) AS src ON ($this->idCol = ?) ".
  322. "WHEN NOT MATCHED THEN INSERT ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (?, ?, ?, ?) ".
  323. "WHEN MATCHED THEN UPDATE SET $this->dataCol = ?, $this->lifetimeCol = ?, $this->timeCol = ?;";
  324. break;
  325. case 'sqlite' === $driver:
  326. $sql = 'INSERT OR REPLACE'.substr($insertSql, 6);
  327. break;
  328. case 'pgsql' === $driver && version_compare($this->getServerVersion(), '9.5', '>='):
  329. $sql = $insertSql." ON CONFLICT ($this->idCol) DO UPDATE SET ($this->dataCol, $this->lifetimeCol, $this->timeCol) = (EXCLUDED.$this->dataCol, EXCLUDED.$this->lifetimeCol, EXCLUDED.$this->timeCol)";
  330. break;
  331. default:
  332. $driver = null;
  333. $sql = "UPDATE $this->table SET $this->dataCol = :data, $this->lifetimeCol = :lifetime, $this->timeCol = :time WHERE $this->idCol = :id";
  334. break;
  335. }
  336. $now = time();
  337. $lifetime = $lifetime ?: null;
  338. try {
  339. $stmt = $conn->prepare($sql);
  340. } catch (TableNotFoundException $e) {
  341. if (!$conn->isTransactionActive() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true)) {
  342. $this->createTable();
  343. }
  344. $stmt = $conn->prepare($sql);
  345. } catch (\PDOException $e) {
  346. if (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true)) {
  347. $this->createTable();
  348. }
  349. $stmt = $conn->prepare($sql);
  350. }
  351. if ('sqlsrv' === $driver || 'oci' === $driver) {
  352. $stmt->bindParam(1, $id);
  353. $stmt->bindParam(2, $id);
  354. $stmt->bindParam(3, $data, $useDbalConstants ? ParameterType::LARGE_OBJECT : \PDO::PARAM_LOB);
  355. $stmt->bindValue(4, $lifetime, $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  356. $stmt->bindValue(5, $now, $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  357. $stmt->bindParam(6, $data, $useDbalConstants ? ParameterType::LARGE_OBJECT : \PDO::PARAM_LOB);
  358. $stmt->bindValue(7, $lifetime, $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  359. $stmt->bindValue(8, $now, $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  360. } else {
  361. $stmt->bindParam(':id', $id);
  362. $stmt->bindParam(':data', $data, $useDbalConstants ? ParameterType::LARGE_OBJECT : \PDO::PARAM_LOB);
  363. $stmt->bindValue(':lifetime', $lifetime, $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  364. $stmt->bindValue(':time', $now, $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  365. }
  366. if (null === $driver) {
  367. $insertStmt = $conn->prepare($insertSql);
  368. $insertStmt->bindParam(':id', $id);
  369. $insertStmt->bindParam(':data', $data, $useDbalConstants ? ParameterType::LARGE_OBJECT : \PDO::PARAM_LOB);
  370. $insertStmt->bindValue(':lifetime', $lifetime, $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  371. $insertStmt->bindValue(':time', $now, $useDbalConstants ? ParameterType::INTEGER : \PDO::PARAM_INT);
  372. }
  373. foreach ($values as $id => $data) {
  374. try {
  375. $result = $stmt->execute();
  376. } catch (TableNotFoundException $e) {
  377. if (!$conn->isTransactionActive() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true)) {
  378. $this->createTable();
  379. }
  380. $result = $stmt->execute();
  381. } catch (\PDOException $e) {
  382. if (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true)) {
  383. $this->createTable();
  384. }
  385. $result = $stmt->execute();
  386. }
  387. if (null === $driver && !(\is_object($result) ? $result->rowCount() : $stmt->rowCount())) {
  388. try {
  389. $insertStmt->execute();
  390. } catch (DBALException | Exception $e) {
  391. } catch (\PDOException $e) {
  392. // A concurrent write won, let it be
  393. }
  394. }
  395. }
  396. return $failed;
  397. }
  398. /**
  399. * @return \PDO|Connection
  400. */
  401. private function getConnection(): object
  402. {
  403. if (null === $this->conn) {
  404. if (strpos($this->dsn, '://')) {
  405. if (!class_exists(DriverManager::class)) {
  406. throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
  407. }
  408. $this->conn = DriverManager::getConnection(['url' => $this->dsn]);
  409. } else {
  410. $this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
  411. $this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  412. }
  413. }
  414. if (null === $this->driver) {
  415. if ($this->conn instanceof \PDO) {
  416. $this->driver = $this->conn->getAttribute(\PDO::ATTR_DRIVER_NAME);
  417. } else {
  418. $driver = $this->conn->getDriver();
  419. switch (true) {
  420. case $driver instanceof \Doctrine\DBAL\Driver\Mysqli\Driver:
  421. throw new \LogicException(sprintf('The adapter "%s" does not support the mysqli driver, use pdo_mysql instead.', static::class));
  422. case $driver instanceof \Doctrine\DBAL\Driver\AbstractMySQLDriver:
  423. $this->driver = 'mysql';
  424. break;
  425. case $driver instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver:
  426. case $driver instanceof \Doctrine\DBAL\Driver\PDO\SQLite\Driver:
  427. $this->driver = 'sqlite';
  428. break;
  429. case $driver instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver:
  430. case $driver instanceof \Doctrine\DBAL\Driver\PDO\PgSQL\Driver:
  431. $this->driver = 'pgsql';
  432. break;
  433. case $driver instanceof \Doctrine\DBAL\Driver\OCI8\Driver:
  434. case $driver instanceof \Doctrine\DBAL\Driver\PDOOracle\Driver:
  435. case $driver instanceof \Doctrine\DBAL\Driver\PDO\OCI\Driver:
  436. $this->driver = 'oci';
  437. break;
  438. case $driver instanceof \Doctrine\DBAL\Driver\SQLSrv\Driver:
  439. case $driver instanceof \Doctrine\DBAL\Driver\PDOSqlsrv\Driver:
  440. case $driver instanceof \Doctrine\DBAL\Driver\PDO\SQLSrv\Driver:
  441. $this->driver = 'sqlsrv';
  442. break;
  443. case $driver instanceof \Doctrine\DBAL\Driver:
  444. $this->driver = [
  445. 'mssql' => 'sqlsrv',
  446. 'oracle' => 'oci',
  447. 'postgresql' => 'pgsql',
  448. 'sqlite' => 'sqlite',
  449. 'mysql' => 'mysql',
  450. ][$driver->getDatabasePlatform()->getName()] ?? \get_class($driver);
  451. break;
  452. default:
  453. $this->driver = \get_class($driver);
  454. break;
  455. }
  456. }
  457. }
  458. return $this->conn;
  459. }
  460. private function getServerVersion(): string
  461. {
  462. if (null === $this->serverVersion) {
  463. $conn = $this->conn instanceof \PDO ? $this->conn : $this->conn->getWrappedConnection();
  464. if ($conn instanceof \PDO) {
  465. $this->serverVersion = $conn->getAttribute(\PDO::ATTR_SERVER_VERSION);
  466. } elseif ($conn instanceof ServerInfoAwareConnection) {
  467. $this->serverVersion = $conn->getServerVersion();
  468. } else {
  469. $this->serverVersion = '0';
  470. }
  471. }
  472. return $this->serverVersion;
  473. }
  474. private function addTableToSchema(Schema $schema): void
  475. {
  476. $types = [
  477. 'mysql' => 'binary',
  478. 'sqlite' => 'text',
  479. 'pgsql' => 'string',
  480. 'oci' => 'string',
  481. 'sqlsrv' => 'string',
  482. ];
  483. if (!isset($types[$this->driver])) {
  484. throw new \DomainException(sprintf('Creating the cache table is currently not implemented for PDO driver "%s".', $this->driver));
  485. }
  486. $table = $schema->createTable($this->table);
  487. $table->addColumn($this->idCol, $types[$this->driver], ['length' => 255]);
  488. $table->addColumn($this->dataCol, 'blob', ['length' => 16777215]);
  489. $table->addColumn($this->lifetimeCol, 'integer', ['unsigned' => true, 'notnull' => false]);
  490. $table->addColumn($this->timeCol, 'integer', ['unsigned' => true]);
  491. $table->setPrimaryKey([$this->idCol]);
  492. }
  493. }