database.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /**
  3. * @file
  4. * Database interface code for SQLite embedded database engine.
  5. */
  6. /**
  7. * @addtogroup database
  8. * @{
  9. */
  10. include_once DRUPAL_ROOT . '/includes/database/prefetch.inc';
  11. /**
  12. * Specific SQLite implementation of DatabaseConnection.
  13. */
  14. class DatabaseConnection_sqlite extends DatabaseConnection {
  15. /**
  16. * Whether this database connection supports savepoints.
  17. *
  18. * Version of sqlite lower then 3.6.8 can't use savepoints.
  19. * See http://www.sqlite.org/releaselog/3_6_8.html
  20. *
  21. * @var boolean
  22. */
  23. protected $savepointSupport = FALSE;
  24. /**
  25. * Whether or not the active transaction (if any) will be rolled back.
  26. *
  27. * @var boolean
  28. */
  29. protected $willRollback;
  30. /**
  31. * All databases attached to the current database. This is used to allow
  32. * prefixes to be safely handled without locking the table
  33. *
  34. * @var array
  35. */
  36. protected $attachedDatabases = array();
  37. /**
  38. * Whether or not a table has been dropped this request: the destructor will
  39. * only try to get rid of unnecessary databases if there is potential of them
  40. * being empty.
  41. *
  42. * This variable is set to public because DatabaseSchema_sqlite needs to
  43. * access it. However, it should not be manually set.
  44. *
  45. * @var boolean
  46. */
  47. var $tableDropped = FALSE;
  48. public function __construct(array $connection_options = array()) {
  49. // We don't need a specific PDOStatement class here, we simulate it below.
  50. $this->statementClass = NULL;
  51. // This driver defaults to transaction support, except if explicitly passed FALSE.
  52. $this->transactionSupport = $this->transactionalDDLSupport = !isset($connection_options['transactions']) || $connection_options['transactions'] !== FALSE;
  53. $this->connectionOptions = $connection_options;
  54. // Allow PDO options to be overridden.
  55. $connection_options += array(
  56. 'pdo' => array(),
  57. );
  58. $connection_options['pdo'] += array(
  59. // Convert numeric values to strings when fetching.
  60. PDO::ATTR_STRINGIFY_FETCHES => TRUE,
  61. );
  62. parent::__construct('sqlite:' . $connection_options['database'], '', '', $connection_options['pdo']);
  63. // Attach one database for each registered prefix.
  64. $prefixes = $this->prefixes;
  65. foreach ($prefixes as $table => &$prefix) {
  66. // Empty prefix means query the main database -- no need to attach anything.
  67. if (!empty($prefix)) {
  68. // Only attach the database once.
  69. if (!isset($this->attachedDatabases[$prefix])) {
  70. $this->attachedDatabases[$prefix] = $prefix;
  71. $this->query('ATTACH DATABASE :database AS :prefix', array(':database' => $connection_options['database'] . '-' . $prefix, ':prefix' => $prefix));
  72. }
  73. // Add a ., so queries become prefix.table, which is proper syntax for
  74. // querying an attached database.
  75. $prefix .= '.';
  76. }
  77. }
  78. // Regenerate the prefixes replacement table.
  79. $this->setPrefix($prefixes);
  80. // Detect support for SAVEPOINT.
  81. $version = $this->query('SELECT sqlite_version()')->fetchField();
  82. $this->savepointSupport = (version_compare($version, '3.6.8') >= 0);
  83. // Create functions needed by SQLite.
  84. $this->sqliteCreateFunction('if', array($this, 'sqlFunctionIf'));
  85. $this->sqliteCreateFunction('greatest', array($this, 'sqlFunctionGreatest'));
  86. $this->sqliteCreateFunction('pow', 'pow', 2);
  87. $this->sqliteCreateFunction('length', 'strlen', 1);
  88. $this->sqliteCreateFunction('md5', 'md5', 1);
  89. $this->sqliteCreateFunction('concat', array($this, 'sqlFunctionConcat'));
  90. $this->sqliteCreateFunction('substring', array($this, 'sqlFunctionSubstring'), 3);
  91. $this->sqliteCreateFunction('substring_index', array($this, 'sqlFunctionSubstringIndex'), 3);
  92. $this->sqliteCreateFunction('rand', array($this, 'sqlFunctionRand'));
  93. // Enable the Write-Ahead Logging (WAL) option for SQLite if supported.
  94. // @see https://www.drupal.org/node/2348137
  95. // @see https://sqlite.org/wal.html
  96. if (version_compare($version, '3.7') >= 0) {
  97. $connection_options += array(
  98. 'init_commands' => array(),
  99. );
  100. $connection_options['init_commands'] += array(
  101. 'wal' => "PRAGMA journal_mode=WAL",
  102. );
  103. }
  104. // Execute sqlite init_commands.
  105. if (isset($connection_options['init_commands'])) {
  106. $this->connection->exec(implode('; ', $connection_options['init_commands']));
  107. }
  108. }
  109. /**
  110. * Destructor for the SQLite connection.
  111. *
  112. * We prune empty databases on destruct, but only if tables have been
  113. * dropped. This is especially needed when running the test suite, which
  114. * creates and destroy databases several times in a row.
  115. */
  116. public function __destruct() {
  117. if ($this->tableDropped && !empty($this->attachedDatabases)) {
  118. foreach ($this->attachedDatabases as $prefix) {
  119. // Check if the database is now empty, ignore the internal SQLite tables.
  120. try {
  121. $count = $this->query('SELECT COUNT(*) FROM ' . $prefix . '.sqlite_master WHERE type = :type AND name NOT LIKE :pattern', array(':type' => 'table', ':pattern' => 'sqlite_%'))->fetchField();
  122. // We can prune the database file if it doesn't have any tables.
  123. if ($count == 0 && $this->connectionOptions['database'] != ':memory:') {
  124. // Detaching the database fails at this point, but no other queries
  125. // are executed after the connection is destructed so we can simply
  126. // remove the database file.
  127. unlink($this->connectionOptions['database'] . '-' . $prefix);
  128. }
  129. }
  130. catch (Exception $e) {
  131. // Ignore the exception and continue. There is nothing we can do here
  132. // to report the error or fail safe.
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Gets all the attached databases.
  139. *
  140. * @return array
  141. * An array of attached database names.
  142. *
  143. * @see DatabaseConnection_sqlite::__construct().
  144. */
  145. public function getAttachedDatabases() {
  146. return $this->attachedDatabases;
  147. }
  148. /**
  149. * SQLite compatibility implementation for the IF() SQL function.
  150. */
  151. public function sqlFunctionIf($condition, $expr1, $expr2 = NULL) {
  152. return $condition ? $expr1 : $expr2;
  153. }
  154. /**
  155. * SQLite compatibility implementation for the GREATEST() SQL function.
  156. */
  157. public function sqlFunctionGreatest() {
  158. $args = func_get_args();
  159. foreach ($args as $k => $v) {
  160. if (!isset($v)) {
  161. unset($args);
  162. }
  163. }
  164. if (count($args)) {
  165. return max($args);
  166. }
  167. else {
  168. return NULL;
  169. }
  170. }
  171. /**
  172. * SQLite compatibility implementation for the CONCAT() SQL function.
  173. */
  174. public function sqlFunctionConcat() {
  175. $args = func_get_args();
  176. return implode('', $args);
  177. }
  178. /**
  179. * SQLite compatibility implementation for the SUBSTRING() SQL function.
  180. */
  181. public function sqlFunctionSubstring($string, $from, $length) {
  182. return substr($string, $from - 1, $length);
  183. }
  184. /**
  185. * SQLite compatibility implementation for the SUBSTRING_INDEX() SQL function.
  186. */
  187. public function sqlFunctionSubstringIndex($string, $delimiter, $count) {
  188. // If string is empty, simply return an empty string.
  189. if (empty($string)) {
  190. return '';
  191. }
  192. $end = 0;
  193. for ($i = 0; $i < $count; $i++) {
  194. $end = strpos($string, $delimiter, $end + 1);
  195. if ($end === FALSE) {
  196. $end = strlen($string);
  197. }
  198. }
  199. return substr($string, 0, $end);
  200. }
  201. /**
  202. * SQLite compatibility implementation for the RAND() SQL function.
  203. */
  204. public function sqlFunctionRand($seed = NULL) {
  205. if (isset($seed)) {
  206. mt_srand($seed);
  207. }
  208. return mt_rand() / mt_getrandmax();
  209. }
  210. /**
  211. * SQLite-specific implementation of DatabaseConnection::prepare().
  212. *
  213. * We don't use prepared statements at all at this stage. We just create
  214. * a DatabaseStatement_sqlite object, that will create a PDOStatement
  215. * using the semi-private PDOPrepare() method below.
  216. */
  217. public function prepare($query, $options = array()) {
  218. return new DatabaseStatement_sqlite($this, $query, $options);
  219. }
  220. /**
  221. * NEVER CALL THIS FUNCTION: YOU MIGHT DEADLOCK YOUR PHP PROCESS.
  222. *
  223. * This is a wrapper around the parent PDO::prepare method. However, as
  224. * the PDO SQLite driver only closes SELECT statements when the PDOStatement
  225. * destructor is called and SQLite does not allow data change (INSERT,
  226. * UPDATE etc) on a table which has open SELECT statements, you should never
  227. * call this function and keep a PDOStatement object alive as that can lead
  228. * to a deadlock. This really, really should be private, but as
  229. * DatabaseStatement_sqlite needs to call it, we have no other choice but to
  230. * expose this function to the world.
  231. */
  232. public function PDOPrepare($query, array $options = array()) {
  233. return $this->connection->prepare($query, $options);
  234. }
  235. public function queryRange($query, $from, $count, array $args = array(), array $options = array()) {
  236. return $this->query($query . ' LIMIT ' . (int) $from . ', ' . (int) $count, $args, $options);
  237. }
  238. public function queryTemporary($query, array $args = array(), array $options = array()) {
  239. // Generate a new temporary table name and protect it from prefixing.
  240. // SQLite requires that temporary tables to be non-qualified.
  241. $tablename = $this->generateTemporaryTableName();
  242. $prefixes = $this->prefixes;
  243. $prefixes[$tablename] = '';
  244. $this->setPrefix($prefixes);
  245. $this->query('CREATE TEMPORARY TABLE ' . $tablename . ' AS ' . $query, $args, $options);
  246. return $tablename;
  247. }
  248. public function driver() {
  249. return 'sqlite';
  250. }
  251. public function databaseType() {
  252. return 'sqlite';
  253. }
  254. public function mapConditionOperator($operator) {
  255. // We don't want to override any of the defaults.
  256. static $specials = array(
  257. 'LIKE' => array('postfix' => " ESCAPE '\\'"),
  258. 'NOT LIKE' => array('postfix' => " ESCAPE '\\'"),
  259. );
  260. return isset($specials[$operator]) ? $specials[$operator] : NULL;
  261. }
  262. public function prepareQuery($query) {
  263. return $this->prepare($this->prefixTables($query));
  264. }
  265. public function nextId($existing_id = 0) {
  266. $transaction = $this->startTransaction();
  267. // We can safely use literal queries here instead of the slower query
  268. // builder because if a given database breaks here then it can simply
  269. // override nextId. However, this is unlikely as we deal with short strings
  270. // and integers and no known databases require special handling for those
  271. // simple cases. If another transaction wants to write the same row, it will
  272. // wait until this transaction commits.
  273. $stmt = $this->query('UPDATE {sequences} SET value = GREATEST(value, :existing_id) + 1', array(
  274. ':existing_id' => $existing_id,
  275. ));
  276. if (!$stmt->rowCount()) {
  277. $this->query('INSERT INTO {sequences} (value) VALUES (:existing_id + 1)', array(
  278. ':existing_id' => $existing_id,
  279. ));
  280. }
  281. // The transaction gets committed when the transaction object gets destroyed
  282. // because it gets out of scope.
  283. return $this->query('SELECT value FROM {sequences}')->fetchField();
  284. }
  285. public function rollback($savepoint_name = 'drupal_transaction') {
  286. if ($this->savepointSupport) {
  287. return parent::rollBack($savepoint_name);
  288. }
  289. if (!$this->inTransaction()) {
  290. throw new DatabaseTransactionNoActiveException();
  291. }
  292. // A previous rollback to an earlier savepoint may mean that the savepoint
  293. // in question has already been rolled back.
  294. if (!in_array($savepoint_name, $this->transactionLayers)) {
  295. return;
  296. }
  297. // We need to find the point we're rolling back to, all other savepoints
  298. // before are no longer needed.
  299. while ($savepoint = array_pop($this->transactionLayers)) {
  300. if ($savepoint == $savepoint_name) {
  301. // Mark whole stack of transactions as needed roll back.
  302. $this->willRollback = TRUE;
  303. // If it is the last the transaction in the stack, then it is not a
  304. // savepoint, it is the transaction itself so we will need to roll back
  305. // the transaction rather than a savepoint.
  306. if (empty($this->transactionLayers)) {
  307. break;
  308. }
  309. return;
  310. }
  311. }
  312. if ($this->supportsTransactions()) {
  313. $this->connection->rollBack();
  314. }
  315. }
  316. public function pushTransaction($name) {
  317. if ($this->savepointSupport) {
  318. return parent::pushTransaction($name);
  319. }
  320. if (!$this->supportsTransactions()) {
  321. return;
  322. }
  323. if (isset($this->transactionLayers[$name])) {
  324. throw new DatabaseTransactionNameNonUniqueException($name . " is already in use.");
  325. }
  326. if (!$this->inTransaction()) {
  327. $this->connection->beginTransaction();
  328. }
  329. $this->transactionLayers[$name] = $name;
  330. }
  331. public function popTransaction($name) {
  332. if ($this->savepointSupport) {
  333. return parent::popTransaction($name);
  334. }
  335. if (!$this->supportsTransactions()) {
  336. return;
  337. }
  338. if (!$this->inTransaction()) {
  339. throw new DatabaseTransactionNoActiveException();
  340. }
  341. // Commit everything since SAVEPOINT $name.
  342. while($savepoint = array_pop($this->transactionLayers)) {
  343. if ($savepoint != $name) continue;
  344. // If there are no more layers left then we should commit or rollback.
  345. if (empty($this->transactionLayers)) {
  346. // If there was any rollback() we should roll back whole transaction.
  347. if ($this->willRollback) {
  348. $this->willRollback = FALSE;
  349. $this->connection->rollBack();
  350. }
  351. elseif (!$this->connection->commit()) {
  352. throw new DatabaseTransactionCommitFailedException();
  353. }
  354. }
  355. else {
  356. break;
  357. }
  358. }
  359. }
  360. public function utf8mb4IsActive() {
  361. return TRUE;
  362. }
  363. public function utf8mb4IsSupported() {
  364. return TRUE;
  365. }
  366. }
  367. /**
  368. * Specific SQLite implementation of DatabaseConnection.
  369. *
  370. * See DatabaseConnection_sqlite::PDOPrepare() for reasons why we must prefetch
  371. * the data instead of using PDOStatement.
  372. *
  373. * @see DatabaseConnection_sqlite::PDOPrepare()
  374. */
  375. class DatabaseStatement_sqlite extends DatabaseStatementPrefetch implements Iterator, DatabaseStatementInterface {
  376. /**
  377. * SQLite specific implementation of getStatement().
  378. *
  379. * The PDO SQLite layer doesn't replace numeric placeholders in queries
  380. * correctly, and this makes numeric expressions (such as COUNT(*) >= :count)
  381. * fail. We replace numeric placeholders in the query ourselves to work
  382. * around this bug.
  383. *
  384. * See http://bugs.php.net/bug.php?id=45259 for more details.
  385. */
  386. protected function getStatement($query, &$args = array()) {
  387. if (count($args)) {
  388. // Check if $args is a simple numeric array.
  389. if (range(0, count($args) - 1) === array_keys($args)) {
  390. // In that case, we have unnamed placeholders.
  391. $count = 0;
  392. $new_args = array();
  393. foreach ($args as $value) {
  394. if (is_float($value) || is_int($value)) {
  395. if (is_float($value)) {
  396. // Force the conversion to float so as not to loose precision
  397. // in the automatic cast.
  398. $value = sprintf('%F', $value);
  399. }
  400. $query = substr_replace($query, $value, strpos($query, '?'), 1);
  401. }
  402. else {
  403. $placeholder = ':db_statement_placeholder_' . $count++;
  404. $query = substr_replace($query, $placeholder, strpos($query, '?'), 1);
  405. $new_args[$placeholder] = $value;
  406. }
  407. }
  408. $args = $new_args;
  409. }
  410. else {
  411. // Else, this is using named placeholders.
  412. foreach ($args as $placeholder => $value) {
  413. if (is_float($value) || is_int($value)) {
  414. if (is_float($value)) {
  415. // Force the conversion to float so as not to loose precision
  416. // in the automatic cast.
  417. $value = sprintf('%F', $value);
  418. }
  419. // We will remove this placeholder from the query as PDO throws an
  420. // exception if the number of placeholders in the query and the
  421. // arguments does not match.
  422. unset($args[$placeholder]);
  423. // PDO allows placeholders to not be prefixed by a colon. See
  424. // http://marc.info/?l=php-internals&m=111234321827149&w=2 for
  425. // more.
  426. if ($placeholder[0] != ':') {
  427. $placeholder = ":$placeholder";
  428. }
  429. // When replacing the placeholders, make sure we search for the
  430. // exact placeholder. For example, if searching for
  431. // ':db_placeholder_1', do not replace ':db_placeholder_11'.
  432. $query = preg_replace('/' . preg_quote($placeholder) . '\b/', $value, $query);
  433. }
  434. }
  435. }
  436. }
  437. return $this->dbh->PDOPrepare($query);
  438. }
  439. public function execute($args = array(), $options = array()) {
  440. try {
  441. $return = parent::execute($args, $options);
  442. }
  443. catch (PDOException $e) {
  444. if (!empty($e->errorInfo[1]) && $e->errorInfo[1] === 17) {
  445. // The schema has changed. SQLite specifies that we must resend the query.
  446. $return = parent::execute($args, $options);
  447. }
  448. else {
  449. // Rethrow the exception.
  450. throw $e;
  451. }
  452. }
  453. // In some weird cases, SQLite will prefix some column names by the name
  454. // of the table. We post-process the data, by renaming the column names
  455. // using the same convention as MySQL and PostgreSQL.
  456. $rename_columns = array();
  457. foreach ($this->columnNames as $k => $column) {
  458. // In some SQLite versions, SELECT DISTINCT(field) will return "(field)"
  459. // instead of "field".
  460. if (preg_match("/^\((.*)\)$/", $column, $matches)) {
  461. $rename_columns[$column] = $matches[1];
  462. $this->columnNames[$k] = $matches[1];
  463. $column = $matches[1];
  464. }
  465. // Remove "table." prefixes.
  466. if (preg_match("/^.*\.(.*)$/", $column, $matches)) {
  467. $rename_columns[$column] = $matches[1];
  468. $this->columnNames[$k] = $matches[1];
  469. }
  470. }
  471. if ($rename_columns) {
  472. // DatabaseStatementPrefetch already extracted the first row,
  473. // put it back into the result set.
  474. if (isset($this->currentRow)) {
  475. $this->data[0] = &$this->currentRow;
  476. }
  477. // Then rename all the columns across the result set.
  478. foreach ($this->data as $k => $row) {
  479. foreach ($rename_columns as $old_column => $new_column) {
  480. $this->data[$k][$new_column] = $this->data[$k][$old_column];
  481. unset($this->data[$k][$old_column]);
  482. }
  483. }
  484. // Finally, extract the first row again.
  485. $this->currentRow = $this->data[0];
  486. unset($this->data[0]);
  487. }
  488. return $return;
  489. }
  490. }
  491. /**
  492. * @} End of "addtogroup database".
  493. */