FunctionBuilderTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. /**
  3. * @author Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license AGPL-3.0
  6. *
  7. * This code is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License, version 3,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License, version 3,
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. *
  19. */
  20. namespace Test\DB\QueryBuilder;
  21. use OC\DB\QueryBuilder\Literal;
  22. use OCP\DB\QueryBuilder\IQueryBuilder;
  23. use Test\TestCase;
  24. /**
  25. * Class FunctionBuilderTest
  26. *
  27. * @group DB
  28. *
  29. * @package Test\DB\QueryBuilder
  30. */
  31. class FunctionBuilderTest extends TestCase {
  32. /** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */
  33. protected $connection;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->connection = \OC::$server->getDatabaseConnection();
  37. }
  38. /**
  39. * @dataProvider providerTestConcatString
  40. */
  41. public function testConcatString($closure) {
  42. $query = $this->connection->getQueryBuilder();
  43. [$real, $arguments, $return] = $closure($query);
  44. if ($real) {
  45. $this->addDummyData();
  46. $query->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
  47. }
  48. $query->select($query->func()->concat(...$arguments));
  49. $query->from('appconfig')
  50. ->setMaxResults(1);
  51. $result = $query->execute();
  52. $column = $result->fetchOne();
  53. $result->closeCursor();
  54. $this->assertEquals($return, $column);
  55. }
  56. public function providerTestConcatString(): array {
  57. return [
  58. '1 column: string param unicode' =>
  59. [function ($q) {
  60. return [false, [$q->createNamedParameter('👍')], '👍'];
  61. }],
  62. '2 columns: string param and string param' =>
  63. [function ($q) {
  64. return [false, [$q->createNamedParameter('foo'), $q->createNamedParameter('bar')], 'foobar'];
  65. }],
  66. '2 columns: string param and int literal' =>
  67. [function ($q) {
  68. return [false, [$q->createNamedParameter('foo'), $q->expr()->literal(1)], 'foo1'];
  69. }],
  70. '2 columns: string param and string literal' =>
  71. [function ($q) {
  72. return [false, [$q->createNamedParameter('foo'), $q->expr()->literal('bar')], 'foobar'];
  73. }],
  74. '2 columns: string real and int literal' =>
  75. [function ($q) {
  76. return [true, ['configkey', $q->expr()->literal(2)], '12'];
  77. }],
  78. '4 columns: string literal' =>
  79. [function ($q) {
  80. return [false, [$q->expr()->literal('foo'), $q->expr()->literal('bar'), $q->expr()->literal('foo'), $q->expr()->literal('bar')], 'foobarfoobar'];
  81. }],
  82. '4 columns: int literal' =>
  83. [function ($q) {
  84. return [false, [$q->expr()->literal(1), $q->expr()->literal(2), $q->expr()->literal(3), $q->expr()->literal(4)], '1234'];
  85. }],
  86. '5 columns: string param with special chars used in the function' =>
  87. [function ($q) {
  88. return [false, [$q->createNamedParameter("b"), $q->createNamedParameter("'"), $q->createNamedParameter('||'), $q->createNamedParameter(','), $q->createNamedParameter('a')], "b'||,a"];
  89. }],
  90. ];
  91. }
  92. protected function clearDummyData(): void {
  93. $delete = $this->connection->getQueryBuilder();
  94. $delete->delete('appconfig')
  95. ->where($delete->expr()->eq('appid', $delete->createNamedParameter('group_concat')));
  96. $delete->executeStatement();
  97. }
  98. protected function addDummyData(): void {
  99. $this->clearDummyData();
  100. $insert = $this->connection->getQueryBuilder();
  101. $insert->insert('appconfig')
  102. ->setValue('appid', $insert->createNamedParameter('group_concat'))
  103. ->setValue('configvalue', $insert->createNamedParameter('unittest'))
  104. ->setValue('configkey', $insert->createParameter('value'));
  105. $insert->setParameter('value', '1');
  106. $insert->executeStatement();
  107. $insert->setParameter('value', '3');
  108. $insert->executeStatement();
  109. $insert->setParameter('value', '2');
  110. $insert->executeStatement();
  111. }
  112. public function testGroupConcatWithoutSeparator(): void {
  113. $this->addDummyData();
  114. $query = $this->connection->getQueryBuilder();
  115. $query->select($query->func()->groupConcat('configkey'))
  116. ->from('appconfig')
  117. ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
  118. $result = $query->execute();
  119. $column = $result->fetchOne();
  120. $result->closeCursor();
  121. $this->assertStringContainsString(',', $column);
  122. $actual = explode(',', $column);
  123. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  124. }
  125. public function testGroupConcatWithSeparator(): void {
  126. $this->addDummyData();
  127. $query = $this->connection->getQueryBuilder();
  128. $query->select($query->func()->groupConcat('configkey', '#'))
  129. ->from('appconfig')
  130. ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
  131. $result = $query->execute();
  132. $column = $result->fetchOne();
  133. $result->closeCursor();
  134. $this->assertStringContainsString('#', $column);
  135. $actual = explode('#', $column);
  136. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  137. }
  138. public function testGroupConcatWithSingleQuoteSeparator(): void {
  139. $this->addDummyData();
  140. $query = $this->connection->getQueryBuilder();
  141. $query->select($query->func()->groupConcat('configkey', '\''))
  142. ->from('appconfig')
  143. ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
  144. $result = $query->execute();
  145. $column = $result->fetchOne();
  146. $result->closeCursor();
  147. $this->assertStringContainsString("'", $column);
  148. $actual = explode("'", $column);
  149. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  150. }
  151. public function testGroupConcatWithDoubleQuoteSeparator(): void {
  152. $this->addDummyData();
  153. $query = $this->connection->getQueryBuilder();
  154. $query->select($query->func()->groupConcat('configkey', '"'))
  155. ->from('appconfig')
  156. ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
  157. $result = $query->execute();
  158. $column = $result->fetchOne();
  159. $result->closeCursor();
  160. $this->assertStringContainsString('"', $column);
  161. $actual = explode('"', $column);
  162. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  163. }
  164. protected function clearIntDummyData(): void {
  165. $delete = $this->connection->getQueryBuilder();
  166. $delete->delete('systemtag')
  167. ->where($delete->expr()->eq('name', $delete->createNamedParameter('group_concat')));
  168. $delete->executeStatement();
  169. }
  170. protected function addIntDummyData(): void {
  171. $this->clearIntDummyData();
  172. $insert = $this->connection->getQueryBuilder();
  173. $insert->insert('systemtag')
  174. ->setValue('name', $insert->createNamedParameter('group_concat'))
  175. ->setValue('visibility', $insert->createNamedParameter(1))
  176. ->setValue('editable', $insert->createParameter('value'));
  177. $insert->setParameter('value', 1);
  178. $insert->executeStatement();
  179. $insert->setParameter('value', 2);
  180. $insert->executeStatement();
  181. $insert->setParameter('value', 3);
  182. $insert->executeStatement();
  183. }
  184. public function testIntGroupConcatWithoutSeparator(): void {
  185. $this->addIntDummyData();
  186. $query = $this->connection->getQueryBuilder();
  187. $query->select($query->func()->groupConcat('editable'))
  188. ->from('systemtag')
  189. ->where($query->expr()->eq('name', $query->createNamedParameter('group_concat')));
  190. $result = $query->execute();
  191. $column = $result->fetchOne();
  192. $result->closeCursor();
  193. $this->assertStringContainsString(',', $column);
  194. $actual = explode(',', $column);
  195. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  196. }
  197. public function testIntGroupConcatWithSeparator(): void {
  198. $this->addIntDummyData();
  199. $query = $this->connection->getQueryBuilder();
  200. $query->select($query->func()->groupConcat('editable', '#'))
  201. ->from('systemtag')
  202. ->where($query->expr()->eq('name', $query->createNamedParameter('group_concat')));
  203. $result = $query->execute();
  204. $column = $result->fetchOne();
  205. $result->closeCursor();
  206. $this->assertStringContainsString('#', $column);
  207. $actual = explode('#', $column);
  208. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  209. }
  210. public function testMd5() {
  211. $query = $this->connection->getQueryBuilder();
  212. $query->select($query->func()->md5($query->createNamedParameter('foobar')));
  213. $query->from('appconfig')
  214. ->setMaxResults(1);
  215. $result = $query->execute();
  216. $column = $result->fetchOne();
  217. $result->closeCursor();
  218. $this->assertEquals(md5('foobar'), $column);
  219. }
  220. public function testSubstring() {
  221. $query = $this->connection->getQueryBuilder();
  222. $query->select($query->func()->substring($query->createNamedParameter('foobar'), new Literal(2), $query->createNamedParameter(2)));
  223. $query->from('appconfig')
  224. ->setMaxResults(1);
  225. $result = $query->execute();
  226. $column = $result->fetchOne();
  227. $result->closeCursor();
  228. $this->assertEquals('oo', $column);
  229. }
  230. public function testSubstringNoLength() {
  231. $query = $this->connection->getQueryBuilder();
  232. $query->select($query->func()->substring($query->createNamedParameter('foobar'), new Literal(2)));
  233. $query->from('appconfig')
  234. ->setMaxResults(1);
  235. $result = $query->execute();
  236. $column = $result->fetchOne();
  237. $result->closeCursor();
  238. $this->assertEquals('oobar', $column);
  239. }
  240. public function testLower() {
  241. $query = $this->connection->getQueryBuilder();
  242. $query->select($query->func()->lower($query->createNamedParameter('FooBar')));
  243. $query->from('appconfig')
  244. ->setMaxResults(1);
  245. $result = $query->execute();
  246. $column = $result->fetchOne();
  247. $result->closeCursor();
  248. $this->assertEquals('foobar', $column);
  249. }
  250. public function testAdd() {
  251. $query = $this->connection->getQueryBuilder();
  252. $query->select($query->func()->add($query->createNamedParameter(2, IQueryBuilder::PARAM_INT), new Literal(1)));
  253. $query->from('appconfig')
  254. ->setMaxResults(1);
  255. $result = $query->execute();
  256. $column = $result->fetchOne();
  257. $result->closeCursor();
  258. $this->assertEquals(3, $column);
  259. }
  260. public function testSubtract() {
  261. $query = $this->connection->getQueryBuilder();
  262. $query->select($query->func()->subtract($query->createNamedParameter(2, IQueryBuilder::PARAM_INT), new Literal(1)));
  263. $query->from('appconfig')
  264. ->setMaxResults(1);
  265. $result = $query->execute();
  266. $column = $result->fetchOne();
  267. $result->closeCursor();
  268. $this->assertEquals(1, $column);
  269. }
  270. public function testCount() {
  271. $query = $this->connection->getQueryBuilder();
  272. $query->select($query->func()->count('appid'));
  273. $query->from('appconfig')
  274. ->setMaxResults(1);
  275. $result = $query->execute();
  276. $column = $result->fetchOne();
  277. $result->closeCursor();
  278. $this->assertGreaterThan(1, $column);
  279. }
  280. public function octetLengthProvider() {
  281. return [
  282. ['', 0],
  283. ['foobar', 6],
  284. ['fé', 3],
  285. ['šđčćž', 10],
  286. ];
  287. }
  288. /**
  289. * @dataProvider octetLengthProvider
  290. */
  291. public function testOctetLength(string $str, int $bytes) {
  292. $query = $this->connection->getQueryBuilder();
  293. $query->select($query->func()->octetLength($query->createNamedParameter($str, IQueryBuilder::PARAM_STR)));
  294. $query->from('appconfig')
  295. ->setMaxResults(1);
  296. $result = $query->execute();
  297. $column = $result->fetchOne();
  298. $result->closeCursor();
  299. $this->assertEquals($bytes, $column);
  300. }
  301. public function charLengthProvider() {
  302. return [
  303. ['', 0],
  304. ['foobar', 6],
  305. ['fé', 2],
  306. ['šđčćž', 5],
  307. ];
  308. }
  309. /**
  310. * @dataProvider charLengthProvider
  311. */
  312. public function testCharLength(string $str, int $bytes) {
  313. $query = $this->connection->getQueryBuilder();
  314. $query->select($query->func()->charLength($query->createNamedParameter($str, IQueryBuilder::PARAM_STR)));
  315. $query->from('appconfig')
  316. ->setMaxResults(1);
  317. $result = $query->execute();
  318. $column = $result->fetchOne();
  319. $result->closeCursor();
  320. $this->assertEquals($bytes, $column);
  321. }
  322. private function setUpMinMax($value) {
  323. $query = $this->connection->getQueryBuilder();
  324. $query->insert('appconfig')
  325. ->values([
  326. 'appid' => $query->createNamedParameter('minmax'),
  327. 'configkey' => $query->createNamedParameter(uniqid()),
  328. 'configvalue' => $query->createNamedParameter((string)$value),
  329. ]);
  330. $query->execute();
  331. }
  332. private function clearMinMax() {
  333. $query = $this->connection->getQueryBuilder();
  334. $query->delete('appconfig')
  335. ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax')));
  336. $query->execute();
  337. }
  338. public function testMaxEmpty() {
  339. $this->clearMinMax();
  340. $query = $this->connection->getQueryBuilder();
  341. $query->select($query->func()->max($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)));
  342. $query->from('appconfig')
  343. ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax')))
  344. ->setMaxResults(1);
  345. $result = $query->execute();
  346. $row = $result->fetchOne();
  347. $result->closeCursor();
  348. $this->assertEquals(null, $row);
  349. }
  350. public function testMinEmpty() {
  351. $this->clearMinMax();
  352. $query = $this->connection->getQueryBuilder();
  353. $query->select($query->func()->min($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)));
  354. $query->from('appconfig')
  355. ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax')))
  356. ->setMaxResults(1);
  357. $result = $query->execute();
  358. $row = $result->fetchOne();
  359. $result->closeCursor();
  360. $this->assertEquals(null, $row);
  361. }
  362. public function testMax() {
  363. $this->clearMinMax();
  364. $this->setUpMinMax(10);
  365. $this->setUpMinMax(11);
  366. $this->setUpMinMax(20);
  367. $query = $this->connection->getQueryBuilder();
  368. $query->select($query->func()->max($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)));
  369. $query->from('appconfig')
  370. ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax')))
  371. ->setMaxResults(1);
  372. $result = $query->execute();
  373. $row = $result->fetchOne();
  374. $result->closeCursor();
  375. $this->assertEquals(20, $row);
  376. }
  377. public function testMin() {
  378. $this->clearMinMax();
  379. $this->setUpMinMax(10);
  380. $this->setUpMinMax(11);
  381. $this->setUpMinMax(20);
  382. $query = $this->connection->getQueryBuilder();
  383. $query->select($query->func()->min($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)));
  384. $query->from('appconfig')
  385. ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax')))
  386. ->setMaxResults(1);
  387. $result = $query->execute();
  388. $row = $result->fetchOne();
  389. $result->closeCursor();
  390. $this->assertEquals(10, $row);
  391. }
  392. public function testGreatest() {
  393. $query = $this->connection->getQueryBuilder();
  394. $query->select($query->func()->greatest($query->createNamedParameter(2, IQueryBuilder::PARAM_INT), new Literal(1)));
  395. $query->from('appconfig')
  396. ->setMaxResults(1);
  397. $result = $query->execute();
  398. $row = $result->fetchOne();
  399. $result->closeCursor();
  400. $this->assertEquals(2, $row);
  401. }
  402. public function testLeast() {
  403. $query = $this->connection->getQueryBuilder();
  404. $query->select($query->func()->least($query->createNamedParameter(2, IQueryBuilder::PARAM_INT), new Literal(1)));
  405. $query->from('appconfig')
  406. ->setMaxResults(1);
  407. $result = $query->execute();
  408. $row = $result->fetchOne();
  409. $result->closeCursor();
  410. $this->assertEquals(1, $row);
  411. }
  412. }