1
0

ExpressionBuilderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\DB\QueryBuilder;
  8. use Doctrine\DBAL\Query\Expression\ExpressionBuilder as DoctrineExpressionBuilder;
  9. use OC\DB\QueryBuilder\ExpressionBuilder\ExpressionBuilder;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use Test\TestCase;
  12. /**
  13. * Class ExpressionBuilderTest
  14. *
  15. * @group DB
  16. *
  17. * @package Test\DB\QueryBuilder
  18. */
  19. class ExpressionBuilderTest extends TestCase {
  20. /** @var ExpressionBuilder */
  21. protected $expressionBuilder;
  22. /** @var DoctrineExpressionBuilder */
  23. protected $doctrineExpressionBuilder;
  24. /** @var \OCP\IDBConnection */
  25. protected $connection;
  26. /** @var \Doctrine\DBAL\Connection */
  27. protected $internalConnection;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->connection = \OC::$server->getDatabaseConnection();
  31. $this->internalConnection = \OC::$server->get(\OC\DB\Connection::class);
  32. $queryBuilder = $this->createMock(IQueryBuilder::class);
  33. $this->expressionBuilder = new ExpressionBuilder($this->connection, $queryBuilder);
  34. $this->doctrineExpressionBuilder = new DoctrineExpressionBuilder($this->internalConnection);
  35. }
  36. public function dataComparison() {
  37. $valueSets = $this->dataComparisons();
  38. $comparisonOperators = ['=', '<>', '<', '>', '<=', '>='];
  39. $testSets = [];
  40. foreach ($comparisonOperators as $operator) {
  41. foreach ($valueSets as $values) {
  42. $testSets[] = array_merge([$operator], $values);
  43. }
  44. }
  45. return $testSets;
  46. }
  47. /**
  48. * @dataProvider dataComparison
  49. *
  50. * @param string $comparison
  51. * @param mixed $input1
  52. * @param bool $isInput1Literal
  53. * @param mixed $input2
  54. * @param bool $isInput2Literal
  55. */
  56. public function testComparison($comparison, $input1, $isInput1Literal, $input2, $isInput2Literal) {
  57. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  58. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  59. $this->assertEquals(
  60. $this->doctrineExpressionBuilder->comparison($doctrineInput1, $comparison, $doctrineInput2),
  61. $this->expressionBuilder->comparison($ocInput1, $comparison, $ocInput2)
  62. );
  63. }
  64. public function dataComparisons() {
  65. return [
  66. ['value', false, 'value', false],
  67. ['value', false, 'value', true],
  68. ['value', true, 'value', false],
  69. ['value', true, 'value', true],
  70. ];
  71. }
  72. /**
  73. * @dataProvider dataComparisons
  74. *
  75. * @param mixed $input1
  76. * @param bool $isInput1Literal
  77. * @param mixed $input2
  78. * @param bool $isInput2Literal
  79. */
  80. public function testEquals($input1, $isInput1Literal, $input2, $isInput2Literal) {
  81. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  82. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  83. $this->assertEquals(
  84. $this->doctrineExpressionBuilder->eq($doctrineInput1, $doctrineInput2),
  85. $this->expressionBuilder->eq($ocInput1, $ocInput2)
  86. );
  87. }
  88. /**
  89. * @dataProvider dataComparisons
  90. *
  91. * @param mixed $input1
  92. * @param bool $isInput1Literal
  93. * @param mixed $input2
  94. * @param bool $isInput2Literal
  95. */
  96. public function testNotEquals($input1, $isInput1Literal, $input2, $isInput2Literal) {
  97. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  98. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  99. $this->assertEquals(
  100. $this->doctrineExpressionBuilder->neq($doctrineInput1, $doctrineInput2),
  101. $this->expressionBuilder->neq($ocInput1, $ocInput2)
  102. );
  103. }
  104. /**
  105. * @dataProvider dataComparisons
  106. *
  107. * @param mixed $input1
  108. * @param bool $isInput1Literal
  109. * @param mixed $input2
  110. * @param bool $isInput2Literal
  111. */
  112. public function testLowerThan($input1, $isInput1Literal, $input2, $isInput2Literal) {
  113. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  114. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  115. $this->assertEquals(
  116. $this->doctrineExpressionBuilder->lt($doctrineInput1, $doctrineInput2),
  117. $this->expressionBuilder->lt($ocInput1, $ocInput2)
  118. );
  119. }
  120. /**
  121. * @dataProvider dataComparisons
  122. *
  123. * @param mixed $input1
  124. * @param bool $isInput1Literal
  125. * @param mixed $input2
  126. * @param bool $isInput2Literal
  127. */
  128. public function testLowerThanEquals($input1, $isInput1Literal, $input2, $isInput2Literal) {
  129. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  130. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  131. $this->assertEquals(
  132. $this->doctrineExpressionBuilder->lte($doctrineInput1, $doctrineInput2),
  133. $this->expressionBuilder->lte($ocInput1, $ocInput2)
  134. );
  135. }
  136. /**
  137. * @dataProvider dataComparisons
  138. *
  139. * @param mixed $input1
  140. * @param bool $isInput1Literal
  141. * @param mixed $input2
  142. * @param bool $isInput2Literal
  143. */
  144. public function testGreaterThan($input1, $isInput1Literal, $input2, $isInput2Literal) {
  145. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  146. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  147. $this->assertEquals(
  148. $this->doctrineExpressionBuilder->gt($doctrineInput1, $doctrineInput2),
  149. $this->expressionBuilder->gt($ocInput1, $ocInput2)
  150. );
  151. }
  152. /**
  153. * @dataProvider dataComparisons
  154. *
  155. * @param mixed $input1
  156. * @param bool $isInput1Literal
  157. * @param mixed $input2
  158. * @param bool $isInput2Literal
  159. */
  160. public function testGreaterThanEquals($input1, $isInput1Literal, $input2, $isInput2Literal) {
  161. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  162. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  163. $this->assertEquals(
  164. $this->doctrineExpressionBuilder->gte($doctrineInput1, $doctrineInput2),
  165. $this->expressionBuilder->gte($ocInput1, $ocInput2)
  166. );
  167. }
  168. public function testIsNull() {
  169. $this->assertEquals(
  170. $this->doctrineExpressionBuilder->isNull('`test`'),
  171. $this->expressionBuilder->isNull('test')
  172. );
  173. }
  174. public function testIsNotNull() {
  175. $this->assertEquals(
  176. $this->doctrineExpressionBuilder->isNotNull('`test`'),
  177. $this->expressionBuilder->isNotNull('test')
  178. );
  179. }
  180. public function dataLike() {
  181. return [
  182. ['value', false],
  183. ['value', true],
  184. ];
  185. }
  186. /**
  187. * @dataProvider dataLike
  188. *
  189. * @param mixed $input
  190. * @param bool $isLiteral
  191. */
  192. public function testLike($input, $isLiteral) {
  193. [$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
  194. $this->assertEquals(
  195. $this->doctrineExpressionBuilder->like('`test`', $doctrineInput),
  196. $this->expressionBuilder->like('test', $ocInput)
  197. );
  198. }
  199. /**
  200. * @dataProvider dataLike
  201. *
  202. * @param mixed $input
  203. * @param bool $isLiteral
  204. */
  205. public function testNotLike($input, $isLiteral) {
  206. [$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
  207. $this->assertEquals(
  208. $this->doctrineExpressionBuilder->notLike('`test`', $doctrineInput),
  209. $this->expressionBuilder->notLike('test', $ocInput)
  210. );
  211. }
  212. public function dataIn() {
  213. return [
  214. ['value', false],
  215. ['value', true],
  216. [['value'], false],
  217. [['value'], true],
  218. ];
  219. }
  220. /**
  221. * @dataProvider dataIn
  222. *
  223. * @param mixed $input
  224. * @param bool $isLiteral
  225. */
  226. public function testIn($input, $isLiteral) {
  227. [$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
  228. $this->assertEquals(
  229. $this->doctrineExpressionBuilder->in('`test`', $doctrineInput),
  230. $this->expressionBuilder->in('test', $ocInput)
  231. );
  232. }
  233. /**
  234. * @dataProvider dataIn
  235. *
  236. * @param mixed $input
  237. * @param bool $isLiteral
  238. */
  239. public function testNotIn($input, $isLiteral) {
  240. [$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
  241. $this->assertEquals(
  242. $this->doctrineExpressionBuilder->notIn('`test`', $doctrineInput),
  243. $this->expressionBuilder->notIn('test', $ocInput)
  244. );
  245. }
  246. protected function helpWithLiteral($input, $isLiteral) {
  247. if ($isLiteral) {
  248. if (is_array($input)) {
  249. $doctrineInput = array_map(function ($ident) {
  250. return $this->doctrineExpressionBuilder->literal($ident);
  251. }, $input);
  252. $ocInput = array_map(function ($ident) {
  253. return $this->expressionBuilder->literal($ident);
  254. }, $input);
  255. } else {
  256. $doctrineInput = $this->doctrineExpressionBuilder->literal($input);
  257. $ocInput = $this->expressionBuilder->literal($input);
  258. }
  259. } else {
  260. if (is_array($input)) {
  261. $doctrineInput = array_map(function ($input) {
  262. return '`' . $input . '`';
  263. }, $input);
  264. $ocInput = $input;
  265. } else {
  266. $doctrineInput = '`' . $input . '`';
  267. $ocInput = $input;
  268. }
  269. }
  270. return [$doctrineInput, $ocInput];
  271. }
  272. public function dataLiteral() {
  273. return [
  274. ['value', null],
  275. ['1', null],
  276. [1, null],
  277. [1, 'string'],
  278. [1, 'integer'],
  279. [1, IQueryBuilder::PARAM_INT],
  280. ];
  281. }
  282. /**
  283. * @dataProvider dataLiteral
  284. *
  285. * @param mixed $input
  286. * @param string|null $type
  287. */
  288. public function testLiteral($input, $type) {
  289. /** @var \OC\DB\QueryBuilder\Literal $actual */
  290. $actual = $this->expressionBuilder->literal($input, $type);
  291. $this->assertInstanceOf('\OC\DB\QueryBuilder\Literal', $actual);
  292. $this->assertEquals(
  293. $this->doctrineExpressionBuilder->literal($input, $type),
  294. $actual->__toString()
  295. );
  296. }
  297. public function dataClobComparisons() {
  298. return [
  299. ['eq', '5', IQueryBuilder::PARAM_STR, false, 3],
  300. ['eq', '5', IQueryBuilder::PARAM_STR, true, 1],
  301. ['neq', '5', IQueryBuilder::PARAM_STR, false, 8],
  302. ['neq', '5', IQueryBuilder::PARAM_STR, true, 6],
  303. ['lt', '5', IQueryBuilder::PARAM_STR, false, 3],
  304. ['lt', '5', IQueryBuilder::PARAM_STR, true, 1],
  305. ['lte', '5', IQueryBuilder::PARAM_STR, false, 6],
  306. ['lte', '5', IQueryBuilder::PARAM_STR, true, 4],
  307. ['gt', '5', IQueryBuilder::PARAM_STR, false, 5],
  308. ['gt', '5', IQueryBuilder::PARAM_STR, true, 1],
  309. ['gte', '5', IQueryBuilder::PARAM_STR, false, 8],
  310. ['gte', '5', IQueryBuilder::PARAM_STR, true, 4],
  311. ['like', '%5%', IQueryBuilder::PARAM_STR, false, 3],
  312. ['like', '%5%', IQueryBuilder::PARAM_STR, true, 1],
  313. ['like', 'under_%', IQueryBuilder::PARAM_STR, false, 2],
  314. ['like', 'under\_%', IQueryBuilder::PARAM_STR, false, 1],
  315. ['notLike', '%5%', IQueryBuilder::PARAM_STR, false, 8],
  316. ['notLike', '%5%', IQueryBuilder::PARAM_STR, true, 6],
  317. ['in', ['5'], IQueryBuilder::PARAM_STR_ARRAY, false, 3],
  318. ['in', ['5'], IQueryBuilder::PARAM_STR_ARRAY, true, 1],
  319. ['notIn', ['5'], IQueryBuilder::PARAM_STR_ARRAY, false, 8],
  320. ['notIn', ['5'], IQueryBuilder::PARAM_STR_ARRAY, true, 6],
  321. ];
  322. }
  323. /**
  324. * @dataProvider dataClobComparisons
  325. * @param string $function
  326. * @param mixed $value
  327. * @param mixed $type
  328. * @param bool $compareKeyToValue
  329. * @param int $expected
  330. */
  331. public function testClobComparisons($function, $value, $type, $compareKeyToValue, $expected) {
  332. $appId = $this->getUniqueID('testing');
  333. $this->createConfig($appId, 1, 4);
  334. $this->createConfig($appId, 2, 5);
  335. $this->createConfig($appId, 3, 6);
  336. $this->createConfig($appId, 4, 4);
  337. $this->createConfig($appId, 5, 5);
  338. $this->createConfig($appId, 6, 6);
  339. $this->createConfig($appId, 7, 4);
  340. $this->createConfig($appId, 8, 5);
  341. $this->createConfig($appId, 9, 6);
  342. $this->createConfig($appId, 10, 'under_score');
  343. $this->createConfig($appId, 11, 'underscore');
  344. $query = $this->connection->getQueryBuilder();
  345. $query->select($query->func()->count('*', 'count'))
  346. ->from('appconfig')
  347. ->where($query->expr()->eq('appid', $query->createNamedParameter($appId)))
  348. ->andWhere(call_user_func([$query->expr(), $function], 'configvalue', $query->createNamedParameter($value, $type), IQueryBuilder::PARAM_STR));
  349. if ($compareKeyToValue) {
  350. $query->andWhere(call_user_func([$query->expr(), $function], 'configkey', 'configvalue', IQueryBuilder::PARAM_STR));
  351. }
  352. $result = $query->execute();
  353. $this->assertEquals(['count' => $expected], $result->fetch());
  354. $result->closeCursor();
  355. $query = $this->connection->getQueryBuilder();
  356. $query->delete('appconfig')
  357. ->where($query->expr()->eq('appid', $query->createNamedParameter($appId)))
  358. ->execute();
  359. }
  360. protected function createConfig($appId, $key, $value) {
  361. $query = $this->connection->getQueryBuilder();
  362. $query->insert('appconfig')
  363. ->values([
  364. 'appid' => $query->createNamedParameter($appId),
  365. 'configkey' => $query->createNamedParameter((string) $key),
  366. 'configvalue' => $query->createNamedParameter((string) $value),
  367. ])
  368. ->execute();
  369. }
  370. }