1
0

ConnectionAdapter.php 5.7 KB

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