ConnectionAdapter.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\DB;
  8. use Doctrine\DBAL\Exception;
  9. use Doctrine\DBAL\Platforms\AbstractPlatform;
  10. use Doctrine\DBAL\Schema\Schema;
  11. use OC\DB\Exceptions\DbalException;
  12. use OC\DB\QueryBuilder\Sharded\CrossShardMoveHelper;
  13. use OC\DB\QueryBuilder\Sharded\ShardDefinition;
  14. use OCP\DB\IPreparedStatement;
  15. use OCP\DB\IResult;
  16. use OCP\DB\QueryBuilder\IQueryBuilder;
  17. use OCP\IDBConnection;
  18. /**
  19. * Adapts the public API to our internal DBAL connection wrapper
  20. */
  21. class ConnectionAdapter implements IDBConnection {
  22. /** @var Connection */
  23. private $inner;
  24. public function __construct(Connection $inner) {
  25. $this->inner = $inner;
  26. }
  27. public function getQueryBuilder(): IQueryBuilder {
  28. return $this->inner->getQueryBuilder();
  29. }
  30. public function prepare($sql, $limit = null, $offset = null): IPreparedStatement {
  31. try {
  32. return new PreparedStatement(
  33. $this->inner->prepare($sql, $limit, $offset)
  34. );
  35. } catch (Exception $e) {
  36. throw DbalException::wrap($e);
  37. }
  38. }
  39. public function executeQuery(string $sql, array $params = [], $types = []): IResult {
  40. try {
  41. return new ResultAdapter(
  42. $this->inner->executeQuery($sql, $params, $types)
  43. );
  44. } catch (Exception $e) {
  45. throw DbalException::wrap($e);
  46. }
  47. }
  48. public function executeUpdate(string $sql, array $params = [], array $types = []): int {
  49. try {
  50. return $this->inner->executeUpdate($sql, $params, $types);
  51. } catch (Exception $e) {
  52. throw DbalException::wrap($e);
  53. }
  54. }
  55. public function executeStatement($sql, array $params = [], array $types = []): int {
  56. try {
  57. return $this->inner->executeStatement($sql, $params, $types);
  58. } catch (Exception $e) {
  59. throw DbalException::wrap($e);
  60. }
  61. }
  62. public function lastInsertId(string $table): int {
  63. try {
  64. return $this->inner->lastInsertId($table);
  65. } catch (Exception $e) {
  66. throw DbalException::wrap($e);
  67. }
  68. }
  69. public function insertIfNotExist(string $table, array $input, ?array $compare = null) {
  70. try {
  71. return $this->inner->insertIfNotExist($table, $input, $compare);
  72. } catch (Exception $e) {
  73. throw DbalException::wrap($e);
  74. }
  75. }
  76. public function insertIgnoreConflict(string $table, array $values): int {
  77. try {
  78. return $this->inner->insertIgnoreConflict($table, $values);
  79. } catch (Exception $e) {
  80. throw DbalException::wrap($e);
  81. }
  82. }
  83. public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int {
  84. try {
  85. return $this->inner->setValues($table, $keys, $values, $updatePreconditionValues);
  86. } catch (Exception $e) {
  87. throw DbalException::wrap($e);
  88. }
  89. }
  90. public function lockTable($tableName): void {
  91. try {
  92. $this->inner->lockTable($tableName);
  93. } catch (Exception $e) {
  94. throw DbalException::wrap($e);
  95. }
  96. }
  97. public function unlockTable(): void {
  98. try {
  99. $this->inner->unlockTable();
  100. } catch (Exception $e) {
  101. throw DbalException::wrap($e);
  102. }
  103. }
  104. public function beginTransaction(): void {
  105. try {
  106. $this->inner->beginTransaction();
  107. } catch (Exception $e) {
  108. throw DbalException::wrap($e);
  109. }
  110. }
  111. public function inTransaction(): bool {
  112. return $this->inner->inTransaction();
  113. }
  114. public function commit(): void {
  115. try {
  116. $this->inner->commit();
  117. } catch (Exception $e) {
  118. throw DbalException::wrap($e);
  119. }
  120. }
  121. public function rollBack(): void {
  122. try {
  123. $this->inner->rollBack();
  124. } catch (Exception $e) {
  125. throw DbalException::wrap($e);
  126. }
  127. }
  128. public function getError(): string {
  129. return $this->inner->getError();
  130. }
  131. public function errorCode() {
  132. return $this->inner->errorCode();
  133. }
  134. public function errorInfo() {
  135. return $this->inner->errorInfo();
  136. }
  137. public function connect(): bool {
  138. try {
  139. return $this->inner->connect();
  140. } catch (Exception $e) {
  141. throw DbalException::wrap($e);
  142. }
  143. }
  144. public function close(): void {
  145. $this->inner->close();
  146. }
  147. public function quote($input, $type = IQueryBuilder::PARAM_STR) {
  148. return $this->inner->quote($input, $type);
  149. }
  150. /**
  151. * @todo we are leaking a 3rdparty type here
  152. */
  153. public function getDatabasePlatform(): AbstractPlatform {
  154. return $this->inner->getDatabasePlatform();
  155. }
  156. public function dropTable(string $table): void {
  157. try {
  158. $this->inner->dropTable($table);
  159. } catch (Exception $e) {
  160. throw DbalException::wrap($e);
  161. }
  162. }
  163. public function tableExists(string $table): bool {
  164. try {
  165. return $this->inner->tableExists($table);
  166. } catch (Exception $e) {
  167. throw DbalException::wrap($e);
  168. }
  169. }
  170. public function escapeLikeParameter(string $param): string {
  171. return $this->inner->escapeLikeParameter($param);
  172. }
  173. public function supports4ByteText(): bool {
  174. return $this->inner->supports4ByteText();
  175. }
  176. /**
  177. * @todo leaks a 3rdparty type
  178. */
  179. public function createSchema(): Schema {
  180. try {
  181. return $this->inner->createSchema();
  182. } catch (Exception $e) {
  183. throw DbalException::wrap($e);
  184. }
  185. }
  186. public function migrateToSchema(Schema $toSchema): void {
  187. try {
  188. $this->inner->migrateToSchema($toSchema);
  189. } catch (Exception $e) {
  190. throw DbalException::wrap($e);
  191. }
  192. }
  193. public function getInner(): Connection {
  194. return $this->inner;
  195. }
  196. /**
  197. * @return self::PLATFORM_MYSQL|self::PLATFORM_ORACLE|self::PLATFORM_POSTGRES|self::PLATFORM_SQLITE
  198. */
  199. public function getDatabaseProvider(): string {
  200. return $this->inner->getDatabaseProvider();
  201. }
  202. /**
  203. * @internal Should only be used inside the QueryBuilder, ExpressionBuilder and FunctionBuilder
  204. * All apps and API code should not need this and instead use provided functionality from the above.
  205. */
  206. public function getServerVersion(): string {
  207. return $this->inner->getServerVersion();
  208. }
  209. public function logDatabaseException(\Exception $exception) {
  210. $this->inner->logDatabaseException($exception);
  211. }
  212. public function getShardDefinition(string $name): ?ShardDefinition {
  213. return $this->inner->getShardDefinition($name);
  214. }
  215. public function getCrossShardMoveHelper(): CrossShardMoveHelper {
  216. return $this->inner->getCrossShardMoveHelper();
  217. }
  218. }