Driver.php 2.1 KB

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