1
0

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