ConnectionAdapter.php 6.5 KB

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