adapter.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * Insert a row if the matching row does not exists.
  56. *
  57. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  58. * @param array $input data that should be inserted into the table (column name => value)
  59. * @param array|null $compare List of values that should be checked for "if not exists"
  60. * If this is null or an empty array, all keys of $input will be compared
  61. * Please note: text fields (clob) must not be used in the compare array
  62. * @return int number of inserted rows
  63. * @throws \Doctrine\DBAL\DBALException
  64. */
  65. public function insertIfNotExist($table, $input, array $compare = null) {
  66. if (empty($compare)) {
  67. $compare = array_keys($input);
  68. }
  69. $query = 'INSERT INTO `' .$table . '` (`'
  70. . implode('`,`', array_keys($input)) . '`) SELECT '
  71. . str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative?
  72. . 'FROM `' . $table . '` WHERE ';
  73. $inserts = array_values($input);
  74. foreach($compare as $key) {
  75. $query .= '`' . $key . '`';
  76. if (is_null($input[$key])) {
  77. $query .= ' IS NULL AND ';
  78. } else {
  79. $inserts[] = $input[$key];
  80. $query .= ' = ? AND ';
  81. }
  82. }
  83. $query = substr($query, 0, strlen($query) - 5);
  84. $query .= ' HAVING COUNT(*) = 0';
  85. return $this->conn->executeUpdate($query, $inserts);
  86. }
  87. }