Sqlite.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. class Sqlite extends Driver
  23. {
  24. use SqliteDialectTrait;
  25. /**
  26. * Base configuration settings for Sqlite driver
  27. *
  28. * - `mask` The mask used for created database
  29. *
  30. * @var array
  31. */
  32. protected $_baseConfig = [
  33. 'persistent' => false,
  34. 'username' => null,
  35. 'password' => null,
  36. 'database' => ':memory:',
  37. 'encoding' => 'utf8',
  38. 'mask' => 0644,
  39. 'flags' => [],
  40. 'init' => [],
  41. ];
  42. /**
  43. * Establishes a connection to the database server
  44. *
  45. * @return bool true on success
  46. */
  47. public function connect()
  48. {
  49. if ($this->_connection) {
  50. return true;
  51. }
  52. $config = $this->_config;
  53. $config['flags'] += [
  54. PDO::ATTR_PERSISTENT => $config['persistent'],
  55. PDO::ATTR_EMULATE_PREPARES => false,
  56. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  57. ];
  58. $databaseExists = file_exists($config['database']);
  59. $dsn = "sqlite:{$config['database']}";
  60. $this->_connect($dsn, $config);
  61. if (!$databaseExists && $config['database'] != ':memory:') {
  62. //@codingStandardsIgnoreStart
  63. @chmod($config['database'], $config['mask']);
  64. //@codingStandardsIgnoreEnd
  65. }
  66. if (!empty($config['init'])) {
  67. foreach ((array)$config['init'] as $command) {
  68. $this->getConnection()->exec($command);
  69. }
  70. }
  71. return true;
  72. }
  73. /**
  74. * Returns whether php is able to use this driver for connecting to database
  75. *
  76. * @return bool true if it is valid to use this driver
  77. */
  78. public function enabled()
  79. {
  80. return in_array('sqlite', PDO::getAvailableDrivers());
  81. }
  82. /**
  83. * Prepares a sql statement to be executed
  84. *
  85. * @param string|\Cake\Database\Query $query The query to prepare.
  86. * @return \Cake\Database\StatementInterface
  87. */
  88. public function prepare($query)
  89. {
  90. $this->connect();
  91. $isObject = $query instanceof Query;
  92. $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
  93. $result = new SqliteStatement(new PDOStatement($statement, $this), $this);
  94. if ($isObject && $query->isBufferedResultsEnabled() === false) {
  95. $result->bufferResults(false);
  96. }
  97. return $result;
  98. }
  99. /**
  100. * {@inheritDoc}
  101. */
  102. public function supportsDynamicConstraints()
  103. {
  104. return false;
  105. }
  106. }