ConnectionAdapter.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\DB;
  25. use Doctrine\DBAL\Exception;
  26. use Doctrine\DBAL\Platforms\AbstractPlatform;
  27. use Doctrine\DBAL\Schema\Schema;
  28. use OC\DB\Exceptions\DbalException;
  29. use OCP\DB\IPreparedStatement;
  30. use OCP\DB\IResult;
  31. use OCP\DB\QueryBuilder\IQueryBuilder;
  32. use OCP\IDBConnection;
  33. /**
  34. * Adapts the public API to our internal DBAL connection wrapper
  35. */
  36. class ConnectionAdapter implements IDBConnection {
  37. /** @var Connection */
  38. private $inner;
  39. public function __construct(Connection $inner) {
  40. $this->inner = $inner;
  41. }
  42. public function getQueryBuilder(): IQueryBuilder {
  43. return $this->inner->getQueryBuilder();
  44. }
  45. public function prepare($sql, $limit = null, $offset = null): IPreparedStatement {
  46. try {
  47. return new PreparedStatement(
  48. $this->inner->prepare($sql, $limit, $offset)
  49. );
  50. } catch (Exception $e) {
  51. throw DbalException::wrap($e);
  52. }
  53. }
  54. public function executeQuery(string $sql, array $params = [], $types = []): IResult {
  55. try {
  56. return new ResultAdapter(
  57. $this->inner->executeQuery($sql, $params, $types)
  58. );
  59. } catch (Exception $e) {
  60. throw DbalException::wrap($e);
  61. }
  62. }
  63. public function executeUpdate(string $sql, array $params = [], array $types = []): int {
  64. try {
  65. return $this->inner->executeUpdate($sql, $params, $types);
  66. } catch (Exception $e) {
  67. throw DbalException::wrap($e);
  68. }
  69. }
  70. public function executeStatement($sql, array $params = [], array $types = []): int {
  71. try {
  72. return $this->inner->executeStatement($sql, $params, $types);
  73. } catch (Exception $e) {
  74. throw DbalException::wrap($e);
  75. }
  76. }
  77. public function lastInsertId(string $table): int {
  78. try {
  79. return (int)$this->inner->lastInsertId($table);
  80. } catch (Exception $e) {
  81. throw DbalException::wrap($e);
  82. }
  83. }
  84. public function insertIfNotExist(string $table, array $input, array $compare = null) {
  85. try {
  86. return $this->inner->insertIfNotExist($table, $input, $compare);
  87. } catch (Exception $e) {
  88. throw DbalException::wrap($e);
  89. }
  90. }
  91. public function insertIgnoreConflict(string $table, array $values): int {
  92. try {
  93. return $this->inner->insertIgnoreConflict($table, $values);
  94. } catch (Exception $e) {
  95. throw DbalException::wrap($e);
  96. }
  97. }
  98. public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int {
  99. try {
  100. return $this->inner->setValues($table, $keys, $values, $updatePreconditionValues);
  101. } catch (Exception $e) {
  102. throw DbalException::wrap($e);
  103. }
  104. }
  105. public function lockTable($tableName): void {
  106. try {
  107. $this->inner->lockTable($tableName);
  108. } catch (Exception $e) {
  109. throw DbalException::wrap($e);
  110. }
  111. }
  112. public function unlockTable(): void {
  113. try {
  114. $this->inner->unlockTable();
  115. } catch (Exception $e) {
  116. throw DbalException::wrap($e);
  117. }
  118. }
  119. public function beginTransaction(): void {
  120. try {
  121. $this->inner->beginTransaction();
  122. } catch (Exception $e) {
  123. throw DbalException::wrap($e);
  124. }
  125. }
  126. public function inTransaction(): bool {
  127. return $this->inner->inTransaction();
  128. }
  129. public function commit(): void {
  130. try {
  131. $this->inner->commit();
  132. } catch (Exception $e) {
  133. throw DbalException::wrap($e);
  134. }
  135. }
  136. public function rollBack(): void {
  137. try {
  138. $this->inner->rollBack();
  139. } catch (Exception $e) {
  140. throw DbalException::wrap($e);
  141. }
  142. }
  143. public function getError(): string {
  144. return $this->inner->getError();
  145. }
  146. public function errorCode() {
  147. return $this->inner->errorCode();
  148. }
  149. public function errorInfo() {
  150. return $this->inner->errorInfo();
  151. }
  152. public function connect(): bool {
  153. try {
  154. return $this->inner->connect();
  155. } catch (Exception $e) {
  156. throw DbalException::wrap($e);
  157. }
  158. }
  159. public function close(): void {
  160. $this->inner->close();
  161. }
  162. public function quote($input, $type = IQueryBuilder::PARAM_STR) {
  163. return $this->inner->quote($input, $type);
  164. }
  165. /**
  166. * @todo we are leaking a 3rdparty type here
  167. */
  168. public function getDatabasePlatform(): AbstractPlatform {
  169. return $this->inner->getDatabasePlatform();
  170. }
  171. public function dropTable(string $table): void {
  172. try {
  173. $this->inner->dropTable($table);
  174. } catch (Exception $e) {
  175. throw DbalException::wrap($e);
  176. }
  177. }
  178. public function tableExists(string $table): bool {
  179. try {
  180. return $this->inner->tableExists($table);
  181. } catch (Exception $e) {
  182. throw DbalException::wrap($e);
  183. }
  184. }
  185. public function escapeLikeParameter(string $param): string {
  186. return $this->inner->escapeLikeParameter($param);
  187. }
  188. public function supports4ByteText(): bool {
  189. return $this->inner->supports4ByteText();
  190. }
  191. /**
  192. * @todo leaks a 3rdparty type
  193. */
  194. public function createSchema(): Schema {
  195. try {
  196. return $this->inner->createSchema();
  197. } catch (Exception $e) {
  198. throw DbalException::wrap($e);
  199. }
  200. }
  201. public function migrateToSchema(Schema $toSchema): void {
  202. try {
  203. $this->inner->migrateToSchema($toSchema);
  204. } catch (Exception $e) {
  205. throw DbalException::wrap($e);
  206. }
  207. }
  208. public function getInner(): Connection {
  209. return $this->inner;
  210. }
  211. }