Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ConnectionAdapter.php 5.8KB

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