ExpressionBuilderDBTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\DB\QueryBuilder;
  7. use Doctrine\DBAL\Schema\SchemaException;
  8. use Doctrine\DBAL\Types\Types;
  9. use OC\DB\QueryBuilder\Literal;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IConfig;
  12. use OCP\Server;
  13. use Test\TestCase;
  14. /**
  15. * @group DB
  16. */
  17. class ExpressionBuilderDBTest extends TestCase {
  18. /** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */
  19. protected $connection;
  20. protected $schemaSetup = false;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->connection = \OC::$server->getDatabaseConnection();
  24. $this->prepareTestingTable();
  25. }
  26. public function likeProvider() {
  27. $connection = \OC::$server->getDatabaseConnection();
  28. return [
  29. ['foo', 'bar', false],
  30. ['foo', 'foo', true],
  31. ['foo', 'f%', true],
  32. ['foo', '%o', true],
  33. ['foo', '%', true],
  34. ['foo', 'fo_', true],
  35. ['foo', 'foo_', false],
  36. ['foo', $connection->escapeLikeParameter('fo_'), false],
  37. ['foo', $connection->escapeLikeParameter('f%'), false],
  38. ];
  39. }
  40. /**
  41. * @dataProvider likeProvider
  42. *
  43. * @param string $param1
  44. * @param string $param2
  45. * @param boolean $match
  46. */
  47. public function testLike($param1, $param2, $match): void {
  48. $query = $this->connection->getQueryBuilder();
  49. $query->select(new Literal('1'))
  50. ->from('users')
  51. ->where($query->expr()->like($query->createNamedParameter($param1), $query->createNamedParameter($param2)));
  52. $result = $query->execute();
  53. $column = $result->fetchOne();
  54. $result->closeCursor();
  55. $this->assertEquals($match, $column);
  56. }
  57. public function ilikeProvider() {
  58. $connection = \OC::$server->getDatabaseConnection();
  59. return [
  60. ['foo', 'bar', false],
  61. ['foo', 'foo', true],
  62. ['foo', 'Foo', true],
  63. ['foo', 'f%', true],
  64. ['foo', '%o', true],
  65. ['foo', '%', true],
  66. ['foo', 'fo_', true],
  67. ['foo', 'foo_', false],
  68. ['foo', $connection->escapeLikeParameter('fo_'), false],
  69. ['foo', $connection->escapeLikeParameter('f%'), false],
  70. ];
  71. }
  72. /**
  73. * @dataProvider ilikeProvider
  74. *
  75. * @param string $param1
  76. * @param string $param2
  77. * @param boolean $match
  78. */
  79. public function testILike($param1, $param2, $match): void {
  80. $query = $this->connection->getQueryBuilder();
  81. $query->select(new Literal('1'))
  82. ->from('users')
  83. ->where($query->expr()->iLike($query->createNamedParameter($param1), $query->createNamedParameter($param2)));
  84. $result = $query->execute();
  85. $column = $result->fetchOne();
  86. $result->closeCursor();
  87. $this->assertEquals($match, $column);
  88. }
  89. public function testCastColumn(): void {
  90. $appId = $this->getUniqueID('testing');
  91. $this->createConfig($appId, '1', '4');
  92. $query = $this->connection->getQueryBuilder();
  93. $query->update('appconfig')
  94. ->set('configvalue',
  95. $query->expr()->castColumn(
  96. $query->createFunction(
  97. '(' . $query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)
  98. . ' + 1)'
  99. ), IQueryBuilder::PARAM_STR
  100. )
  101. )
  102. ->where($query->expr()->eq('appid', $query->createNamedParameter($appId)))
  103. ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('1')));
  104. $result = $query->executeStatement();
  105. $this->assertEquals(1, $result);
  106. }
  107. public function testLongText(): void {
  108. $appId = $this->getUniqueID('testing');
  109. $this->createConfig($appId, 'mykey', 'myvalue');
  110. $query = $this->connection->getQueryBuilder();
  111. $query->select('*')
  112. ->from('appconfig')
  113. ->where($query->expr()->eq('appid', $query->createNamedParameter($appId)))
  114. ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('mykey')))
  115. ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('myvalue', IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR));
  116. $result = $query->executeQuery();
  117. $entries = $result->fetchAll();
  118. $result->closeCursor();
  119. self::assertCount(1, $entries);
  120. self::assertEquals('myvalue', $entries[0]['configvalue']);
  121. }
  122. public function testDateTimeEquals(): void {
  123. $dateTime = new \DateTime('2023-01-01');
  124. $insert = $this->connection->getQueryBuilder();
  125. $insert->insert('testing')
  126. ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)])
  127. ->executeStatement();
  128. $query = $this->connection->getQueryBuilder();
  129. $result = $query->select('*')
  130. ->from('testing')
  131. ->where($query->expr()->eq('datetime', $query->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)))
  132. ->executeQuery();
  133. $entries = $result->fetchAll();
  134. $result->closeCursor();
  135. self::assertCount(1, $entries);
  136. }
  137. public function testDateTimeLess(): void {
  138. $dateTime = new \DateTime('2022-01-01');
  139. $dateTimeCompare = new \DateTime('2022-01-02');
  140. $insert = $this->connection->getQueryBuilder();
  141. $insert->insert('testing')
  142. ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)])
  143. ->executeStatement();
  144. $query = $this->connection->getQueryBuilder();
  145. $result = $query->select('*')
  146. ->from('testing')
  147. ->where($query->expr()->lt('datetime', $query->createNamedParameter($dateTimeCompare, IQueryBuilder::PARAM_DATE)))
  148. ->executeQuery();
  149. $entries = $result->fetchAll();
  150. $result->closeCursor();
  151. self::assertCount(1, $entries);
  152. }
  153. public function testDateTimeGreater(): void {
  154. $dateTime = new \DateTime('2023-01-02');
  155. $dateTimeCompare = new \DateTime('2023-01-01');
  156. $insert = $this->connection->getQueryBuilder();
  157. $insert->insert('testing')
  158. ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)])
  159. ->executeStatement();
  160. $query = $this->connection->getQueryBuilder();
  161. $result = $query->select('*')
  162. ->from('testing')
  163. ->where($query->expr()->gt('datetime', $query->createNamedParameter($dateTimeCompare, IQueryBuilder::PARAM_DATE)))
  164. ->executeQuery();
  165. $entries = $result->fetchAll();
  166. $result->closeCursor();
  167. self::assertCount(1, $entries);
  168. }
  169. protected function createConfig($appId, $key, $value) {
  170. $query = $this->connection->getQueryBuilder();
  171. $query->insert('appconfig')
  172. ->values([
  173. 'appid' => $query->createNamedParameter($appId),
  174. 'configkey' => $query->createNamedParameter((string)$key),
  175. 'configvalue' => $query->createNamedParameter((string)$value),
  176. ])
  177. ->execute();
  178. }
  179. protected function prepareTestingTable(): void {
  180. if ($this->schemaSetup) {
  181. $this->connection->getQueryBuilder()->delete('testing')->executeStatement();
  182. }
  183. $prefix = Server::get(IConfig::class)->getSystemValueString('dbtableprefix', 'oc_');
  184. $schema = $this->connection->createSchema();
  185. try {
  186. $schema->getTable($prefix . 'testing');
  187. $this->connection->getQueryBuilder()->delete('testing')->executeStatement();
  188. } catch (SchemaException $e) {
  189. $this->schemaSetup = true;
  190. $table = $schema->createTable($prefix . 'testing');
  191. $table->addColumn('id', Types::BIGINT, [
  192. 'autoincrement' => true,
  193. 'notnull' => true,
  194. ]);
  195. $table->addColumn('datetime', Types::DATETIME_MUTABLE, [
  196. 'notnull' => false,
  197. ]);
  198. $table->setPrimaryKey(['id']);
  199. $this->connection->migrateToSchema($schema);
  200. }
  201. }
  202. }