1
0

ExpressionBuilderDBTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\DB\QueryBuilder;
  22. use Doctrine\DBAL\Schema\SchemaException;
  23. use Doctrine\DBAL\Types\Types;
  24. use OC\DB\QueryBuilder\Literal;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IConfig;
  27. use OCP\Server;
  28. use Test\TestCase;
  29. /**
  30. * @group DB
  31. */
  32. class ExpressionBuilderDBTest extends TestCase {
  33. /** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */
  34. protected $connection;
  35. protected $schemaSetup = false;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $this->connection = \OC::$server->getDatabaseConnection();
  39. $this->prepareTestingTable();
  40. }
  41. public function likeProvider() {
  42. $connection = \OC::$server->getDatabaseConnection();
  43. return [
  44. ['foo', 'bar', false],
  45. ['foo', 'foo', true],
  46. ['foo', 'f%', true],
  47. ['foo', '%o', true],
  48. ['foo', '%', true],
  49. ['foo', 'fo_', true],
  50. ['foo', 'foo_', false],
  51. ['foo', $connection->escapeLikeParameter('fo_'), false],
  52. ['foo', $connection->escapeLikeParameter('f%'), false],
  53. ];
  54. }
  55. /**
  56. * @dataProvider likeProvider
  57. *
  58. * @param string $param1
  59. * @param string $param2
  60. * @param boolean $match
  61. */
  62. public function testLike($param1, $param2, $match) {
  63. $query = $this->connection->getQueryBuilder();
  64. $query->select(new Literal('1'))
  65. ->from('users')
  66. ->where($query->expr()->like($query->createNamedParameter($param1), $query->createNamedParameter($param2)));
  67. $result = $query->execute();
  68. $column = $result->fetchOne();
  69. $result->closeCursor();
  70. $this->assertEquals($match, $column);
  71. }
  72. public function ilikeProvider() {
  73. $connection = \OC::$server->getDatabaseConnection();
  74. return [
  75. ['foo', 'bar', false],
  76. ['foo', 'foo', true],
  77. ['foo', 'Foo', true],
  78. ['foo', 'f%', true],
  79. ['foo', '%o', true],
  80. ['foo', '%', true],
  81. ['foo', 'fo_', true],
  82. ['foo', 'foo_', false],
  83. ['foo', $connection->escapeLikeParameter('fo_'), false],
  84. ['foo', $connection->escapeLikeParameter('f%'), false],
  85. ];
  86. }
  87. /**
  88. * @dataProvider ilikeProvider
  89. *
  90. * @param string $param1
  91. * @param string $param2
  92. * @param boolean $match
  93. */
  94. public function testILike($param1, $param2, $match) {
  95. $query = $this->connection->getQueryBuilder();
  96. $query->select(new Literal('1'))
  97. ->from('users')
  98. ->where($query->expr()->iLike($query->createNamedParameter($param1), $query->createNamedParameter($param2)));
  99. $result = $query->execute();
  100. $column = $result->fetchOne();
  101. $result->closeCursor();
  102. $this->assertEquals($match, $column);
  103. }
  104. public function testCastColumn(): void {
  105. $appId = $this->getUniqueID('testing');
  106. $this->createConfig($appId, '1', '4');
  107. $query = $this->connection->getQueryBuilder();
  108. $query->update('appconfig')
  109. ->set('configvalue',
  110. $query->expr()->castColumn(
  111. $query->createFunction(
  112. '(' . $query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)
  113. . ' + 1)'
  114. ), IQueryBuilder::PARAM_STR
  115. )
  116. )
  117. ->where($query->expr()->eq('appid', $query->createNamedParameter($appId)))
  118. ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('1')));
  119. $result = $query->executeStatement();
  120. $this->assertEquals(1, $result);
  121. }
  122. public function testLongText(): void {
  123. $appId = $this->getUniqueID('testing');
  124. $this->createConfig($appId, 'mykey', 'myvalue');
  125. $query = $this->connection->getQueryBuilder();
  126. $query->select('*')
  127. ->from('appconfig')
  128. ->where($query->expr()->eq('appid', $query->createNamedParameter($appId)))
  129. ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('mykey')))
  130. ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('myvalue', IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR));
  131. $result = $query->executeQuery();
  132. $entries = $result->fetchAll();
  133. $result->closeCursor();
  134. self::assertCount(1, $entries);
  135. self::assertEquals('myvalue', $entries[0]['configvalue']);
  136. }
  137. public function testDateTimeEquals() {
  138. $dateTime = new \DateTime('2023-01-01');
  139. $insert = $this->connection->getQueryBuilder();
  140. $insert->insert('testing')
  141. ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)])
  142. ->executeStatement();
  143. $query = $this->connection->getQueryBuilder();
  144. $result = $query->select('*')
  145. ->from('testing')
  146. ->where($query->expr()->eq('datetime', $query->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)))
  147. ->executeQuery();
  148. $entries = $result->fetchAll();
  149. $result->closeCursor();
  150. self::assertCount(1, $entries);
  151. }
  152. public function testDateTimeLess() {
  153. $dateTime = new \DateTime('2022-01-01');
  154. $dateTimeCompare = new \DateTime('2022-01-02');
  155. $insert = $this->connection->getQueryBuilder();
  156. $insert->insert('testing')
  157. ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)])
  158. ->executeStatement();
  159. $query = $this->connection->getQueryBuilder();
  160. $result = $query->select('*')
  161. ->from('testing')
  162. ->where($query->expr()->lt('datetime', $query->createNamedParameter($dateTimeCompare, IQueryBuilder::PARAM_DATE)))
  163. ->executeQuery();
  164. $entries = $result->fetchAll();
  165. $result->closeCursor();
  166. self::assertCount(1, $entries);
  167. }
  168. public function testDateTimeGreater() {
  169. $dateTime = new \DateTime('2023-01-02');
  170. $dateTimeCompare = new \DateTime('2023-01-01');
  171. $insert = $this->connection->getQueryBuilder();
  172. $insert->insert('testing')
  173. ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)])
  174. ->executeStatement();
  175. $query = $this->connection->getQueryBuilder();
  176. $result = $query->select('*')
  177. ->from('testing')
  178. ->where($query->expr()->gt('datetime', $query->createNamedParameter($dateTimeCompare, IQueryBuilder::PARAM_DATE)))
  179. ->executeQuery();
  180. $entries = $result->fetchAll();
  181. $result->closeCursor();
  182. self::assertCount(1, $entries);
  183. }
  184. protected function createConfig($appId, $key, $value) {
  185. $query = $this->connection->getQueryBuilder();
  186. $query->insert('appconfig')
  187. ->values([
  188. 'appid' => $query->createNamedParameter($appId),
  189. 'configkey' => $query->createNamedParameter((string) $key),
  190. 'configvalue' => $query->createNamedParameter((string) $value),
  191. ])
  192. ->execute();
  193. }
  194. protected function prepareTestingTable(): void {
  195. if ($this->schemaSetup) {
  196. $this->connection->getQueryBuilder()->delete('testing')->executeStatement();
  197. }
  198. $prefix = Server::get(IConfig::class)->getSystemValueString('dbtableprefix', 'oc_');
  199. $schema = $this->connection->createSchema();
  200. try {
  201. $schema->getTable($prefix . 'testing');
  202. $this->connection->getQueryBuilder()->delete('testing')->executeStatement();
  203. } catch (SchemaException $e) {
  204. $this->schemaSetup = true;
  205. $table = $schema->createTable($prefix . 'testing');
  206. $table->addColumn('id', Types::BIGINT, [
  207. 'autoincrement' => true,
  208. 'notnull' => true,
  209. ]);
  210. $table->addColumn('datetime', Types::DATETIME_MUTABLE, [
  211. 'notnull' => false,
  212. ]);
  213. $table->setPrimaryKey(['id']);
  214. $this->connection->migrateToSchema($schema);
  215. }
  216. }
  217. }