Driver.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /**
  21. * The PDO Sqlite driver.
  22. *
  23. * @since 2.0
  24. */
  25. class Driver implements \Doctrine\DBAL\Driver
  26. {
  27. /**
  28. * @var array
  29. */
  30. protected $_userDefinedFunctions = array(
  31. 'sqrt' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfSqrt'), 'numArgs' => 1),
  32. 'mod' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfMod'), 'numArgs' => 2),
  33. 'locate' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfLocate'), 'numArgs' => -1),
  34. );
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  39. {
  40. if (isset($driverOptions['userDefinedFunctions'])) {
  41. $this->_userDefinedFunctions = array_merge(
  42. $this->_userDefinedFunctions, $driverOptions['userDefinedFunctions']);
  43. unset($driverOptions['userDefinedFunctions']);
  44. }
  45. $pdo = new \Doctrine\DBAL\Driver\PDOConnection(
  46. $this->_constructPdoDsn($params),
  47. $username,
  48. $password,
  49. $driverOptions
  50. );
  51. foreach ($this->_userDefinedFunctions as $fn => $data) {
  52. $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
  53. }
  54. return $pdo;
  55. }
  56. /**
  57. * Constructs the Sqlite PDO DSN.
  58. *
  59. * @param array $params
  60. *
  61. * @return string The DSN.
  62. */
  63. protected function _constructPdoDsn(array $params)
  64. {
  65. $dsn = 'sqlite:';
  66. if (isset($params['path'])) {
  67. $dsn .= $params['path'];
  68. } else if (isset($params['memory'])) {
  69. $dsn .= ':memory:';
  70. }
  71. return $dsn;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getDatabasePlatform()
  77. {
  78. return new \Doctrine\DBAL\Platforms\SqlitePlatform();
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  84. {
  85. return new \Doctrine\DBAL\Schema\SqliteSchemaManager($conn);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getName()
  91. {
  92. return 'pdo_sqlite';
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  98. {
  99. $params = $conn->getParams();
  100. return isset($params['path']) ? $params['path'] : null;
  101. }
  102. }