Driver.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 LGPL. 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. * Tries to establish a database connection to SQLite.
  37. *
  38. * @param array $params
  39. * @param string $username
  40. * @param string $password
  41. * @param array $driverOptions
  42. * @return Connection
  43. */
  44. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  45. {
  46. if (isset($driverOptions['userDefinedFunctions'])) {
  47. $this->_userDefinedFunctions = array_merge(
  48. $this->_userDefinedFunctions, $driverOptions['userDefinedFunctions']);
  49. unset($driverOptions['userDefinedFunctions']);
  50. }
  51. $pdo = new \Doctrine\DBAL\Driver\PDOConnection(
  52. $this->_constructPdoDsn($params),
  53. $username,
  54. $password,
  55. $driverOptions
  56. );
  57. foreach ($this->_userDefinedFunctions AS $fn => $data) {
  58. $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
  59. }
  60. return $pdo;
  61. }
  62. /**
  63. * Constructs the Sqlite PDO DSN.
  64. *
  65. * @return string The DSN.
  66. * @override
  67. */
  68. protected function _constructPdoDsn(array $params)
  69. {
  70. $dsn = 'sqlite:';
  71. if (isset($params['path'])) {
  72. $dsn .= $params['path'];
  73. } else if (isset($params['memory'])) {
  74. $dsn .= ':memory:';
  75. }
  76. return $dsn;
  77. }
  78. /**
  79. * Gets the database platform that is relevant for this driver.
  80. */
  81. public function getDatabasePlatform()
  82. {
  83. return new \Doctrine\DBAL\Platforms\SqlitePlatform();
  84. }
  85. /**
  86. * Gets the schema manager that is relevant for this driver.
  87. *
  88. * @param Doctrine\DBAL\Connection $conn
  89. * @return Doctrine\DBAL\Schema\SqliteSchemaManager
  90. */
  91. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  92. {
  93. return new \Doctrine\DBAL\Schema\SqliteSchemaManager($conn);
  94. }
  95. public function getName()
  96. {
  97. return 'pdo_sqlite';
  98. }
  99. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  100. {
  101. $params = $conn->getParams();
  102. return isset($params['path']) ? $params['path'] : null;
  103. }
  104. }