Driver.php 2.2 KB

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