Driver.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Driver\PDOSqlite;
  20. use Doctrine\DBAL\DBALException;
  21. use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
  22. use Doctrine\DBAL\Driver\PDOConnection;
  23. use PDOException;
  24. /**
  25. * The PDO Sqlite driver.
  26. *
  27. * @since 2.0
  28. */
  29. class Driver extends AbstractSQLiteDriver
  30. {
  31. /**
  32. * @var array
  33. */
  34. protected $_userDefinedFunctions = array(
  35. 'sqrt' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfSqrt'), 'numArgs' => 1),
  36. 'mod' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfMod'), 'numArgs' => 2),
  37. 'locate' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfLocate'), 'numArgs' => -1),
  38. );
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  43. {
  44. if (isset($driverOptions['userDefinedFunctions'])) {
  45. $this->_userDefinedFunctions = array_merge(
  46. $this->_userDefinedFunctions, $driverOptions['userDefinedFunctions']);
  47. unset($driverOptions['userDefinedFunctions']);
  48. }
  49. try {
  50. $pdo = new PDOConnection(
  51. $this->_constructPdoDsn($params),
  52. $username,
  53. $password,
  54. $driverOptions
  55. );
  56. } catch (PDOException $ex) {
  57. throw DBALException::driverException($this, $ex);
  58. }
  59. foreach ($this->_userDefinedFunctions as $fn => $data) {
  60. $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
  61. }
  62. return $pdo;
  63. }
  64. /**
  65. * Constructs the Sqlite PDO DSN.
  66. *
  67. * @param array $params
  68. *
  69. * @return string The DSN.
  70. */
  71. protected function _constructPdoDsn(array $params)
  72. {
  73. $dsn = 'sqlite:';
  74. if (isset($params['path'])) {
  75. $dsn .= $params['path'];
  76. } elseif (isset($params['memory'])) {
  77. $dsn .= ':memory:';
  78. }
  79. return $dsn;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getName()
  85. {
  86. return 'pdo_sqlite';
  87. }
  88. }