1
0

Adapter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jonny007-MKD <1-23-4-5@web.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\DB;
  28. /**
  29. * This handles the way we use to write queries, into something that can be
  30. * handled by the database abstraction layer.
  31. */
  32. class Adapter {
  33. /**
  34. * @var \OC\DB\Connection $conn
  35. */
  36. protected $conn;
  37. public function __construct($conn) {
  38. $this->conn = $conn;
  39. }
  40. /**
  41. * @param string $table name
  42. * @return int id of last insert statement
  43. */
  44. public function lastInsertId($table) {
  45. return $this->conn->realLastInsertId($table);
  46. }
  47. /**
  48. * @param string $statement that needs to be changed so the db can handle it
  49. * @return string changed statement
  50. */
  51. public function fixupStatement($statement) {
  52. return $statement;
  53. }
  54. /**
  55. * Create an exclusive read+write lock on a table
  56. *
  57. * @param string $tableName
  58. * @since 9.1.0
  59. */
  60. public function lockTable($tableName) {
  61. $this->conn->beginTransaction();
  62. $this->conn->executeUpdate('LOCK TABLE `' .$tableName . '` IN EXCLUSIVE MODE');
  63. }
  64. /**
  65. * Release a previous acquired lock again
  66. *
  67. * @since 9.1.0
  68. */
  69. public function unlockTable() {
  70. $this->conn->commit();
  71. }
  72. /**
  73. * Insert a row if the matching row does not exists.
  74. *
  75. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  76. * @param array $input data that should be inserted into the table (column name => value)
  77. * @param array|null $compare List of values that should be checked for "if not exists"
  78. * If this is null or an empty array, all keys of $input will be compared
  79. * Please note: text fields (clob) must not be used in the compare array
  80. * @return int number of inserted rows
  81. * @throws \Doctrine\DBAL\DBALException
  82. */
  83. public function insertIfNotExist($table, $input, array $compare = null) {
  84. if (empty($compare)) {
  85. $compare = array_keys($input);
  86. }
  87. $query = 'INSERT INTO `' .$table . '` (`'
  88. . implode('`,`', array_keys($input)) . '`) SELECT '
  89. . str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative?
  90. . 'FROM `' . $table . '` WHERE ';
  91. $inserts = array_values($input);
  92. foreach($compare as $key) {
  93. $query .= '`' . $key . '`';
  94. if (is_null($input[$key])) {
  95. $query .= ' IS NULL AND ';
  96. } else {
  97. $inserts[] = $input[$key];
  98. $query .= ' = ? AND ';
  99. }
  100. }
  101. $query = substr($query, 0, strlen($query) - 5);
  102. $query .= ' HAVING COUNT(*) = 0';
  103. return $this->conn->executeUpdate($query, $inserts);
  104. }
  105. }