adapter.php 2.9 KB

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