Driver.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Doctrine\DBAL\Driver\PDOSqlite;
  3. use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
  4. use Doctrine\DBAL\Driver\PDO;
  5. use Doctrine\DBAL\Exception;
  6. use Doctrine\DBAL\Platforms\SqlitePlatform;
  7. use Doctrine\Deprecations\Deprecation;
  8. use PDOException;
  9. use function array_merge;
  10. /**
  11. * The PDO Sqlite driver.
  12. *
  13. * @deprecated Use {@link PDO\SQLite\Driver} instead.
  14. */
  15. class Driver extends AbstractSQLiteDriver
  16. {
  17. /** @var mixed[] */
  18. protected $_userDefinedFunctions = [
  19. 'sqrt' => ['callback' => [SqlitePlatform::class, 'udfSqrt'], 'numArgs' => 1],
  20. 'mod' => ['callback' => [SqlitePlatform::class, 'udfMod'], 'numArgs' => 2],
  21. 'locate' => ['callback' => [SqlitePlatform::class, 'udfLocate'], 'numArgs' => -1],
  22. ];
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
  27. {
  28. if (isset($driverOptions['userDefinedFunctions'])) {
  29. $this->_userDefinedFunctions = array_merge(
  30. $this->_userDefinedFunctions,
  31. $driverOptions['userDefinedFunctions']
  32. );
  33. unset($driverOptions['userDefinedFunctions']);
  34. }
  35. try {
  36. $pdo = new PDO\Connection(
  37. $this->_constructPdoDsn($params),
  38. $username,
  39. $password,
  40. $driverOptions
  41. );
  42. } catch (PDOException $ex) {
  43. throw Exception::driverException($this, $ex);
  44. }
  45. foreach ($this->_userDefinedFunctions as $fn => $data) {
  46. $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
  47. }
  48. return $pdo;
  49. }
  50. /**
  51. * Constructs the Sqlite PDO DSN.
  52. *
  53. * @param mixed[] $params
  54. *
  55. * @return string The DSN.
  56. */
  57. protected function _constructPdoDsn(array $params)
  58. {
  59. $dsn = 'sqlite:';
  60. if (isset($params['path'])) {
  61. $dsn .= $params['path'];
  62. } elseif (isset($params['memory'])) {
  63. $dsn .= ':memory:';
  64. }
  65. return $dsn;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. *
  70. * @deprecated
  71. */
  72. public function getName()
  73. {
  74. Deprecation::trigger(
  75. 'doctrine/dbal',
  76. 'https://github.com/doctrine/dbal/issues/3580',
  77. 'Driver::getName() is deprecated'
  78. );
  79. return 'pdo_sqlite';
  80. }
  81. }