Sqlite.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Driver;
  16. use Cake\Database\Dialect\SqliteDialectTrait;
  17. use Cake\Database\Driver;
  18. use Cake\Database\Query;
  19. use Cake\Database\Statement\PDOStatement;
  20. use Cake\Database\Statement\SqliteStatement;
  21. use PDO;
  22. /**
  23. * Class Sqlite
  24. */
  25. class Sqlite extends Driver
  26. {
  27. use SqliteDialectTrait;
  28. /**
  29. * Base configuration settings for Sqlite driver
  30. *
  31. * - `mask` The mask used for created database
  32. *
  33. * @var array
  34. */
  35. protected $_baseConfig = [
  36. 'persistent' => false,
  37. 'username' => null,
  38. 'password' => null,
  39. 'database' => ':memory:',
  40. 'encoding' => 'utf8',
  41. 'mask' => 0644,
  42. 'flags' => [],
  43. 'init' => [],
  44. ];
  45. /**
  46. * Establishes a connection to the database server
  47. *
  48. * @return bool true on success
  49. */
  50. public function connect()
  51. {
  52. if ($this->_connection) {
  53. return true;
  54. }
  55. $config = $this->_config;
  56. $config['flags'] += [
  57. PDO::ATTR_PERSISTENT => $config['persistent'],
  58. PDO::ATTR_EMULATE_PREPARES => false,
  59. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  60. ];
  61. $databaseExists = file_exists($config['database']);
  62. $dsn = "sqlite:{$config['database']}";
  63. $this->_connect($dsn, $config);
  64. if (!$databaseExists && $config['database'] != ':memory:') {
  65. //@codingStandardsIgnoreStart
  66. @chmod($config['database'], $config['mask']);
  67. //@codingStandardsIgnoreEnd
  68. }
  69. if (!empty($config['init'])) {
  70. foreach ((array)$config['init'] as $command) {
  71. $this->getConnection()->exec($command);
  72. }
  73. }
  74. return true;
  75. }
  76. /**
  77. * Returns whether php is able to use this driver for connecting to database
  78. *
  79. * @return bool true if it is valid to use this driver
  80. */
  81. public function enabled()
  82. {
  83. return in_array('sqlite', PDO::getAvailableDrivers());
  84. }
  85. /**
  86. * Prepares a sql statement to be executed
  87. *
  88. * @param string|\Cake\Database\Query $query The query to prepare.
  89. * @return \Cake\Database\StatementInterface
  90. */
  91. public function prepare($query)
  92. {
  93. $this->connect();
  94. $isObject = $query instanceof Query;
  95. $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
  96. $result = new SqliteStatement(new PDOStatement($statement, $this), $this);
  97. if ($isObject && $query->isBufferedResultsEnabled() === false) {
  98. $result->bufferResults(false);
  99. }
  100. return $result;
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. public function supportsDynamicConstraints()
  106. {
  107. return false;
  108. }
  109. }