FunctionBuilderTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. $query->orderBy('configkey', 'asc');
  48. }
  49. $query->select($query->func()->concat(...$arguments));
  50. $query->from('appconfig')
  51. ->setMaxResults(1);
  52. $result = $query->execute();
  53. $column = $result->fetchOne();
  54. $result->closeCursor();
  55. $this->assertEquals($return, $column);
  56. }
  57. public function providerTestConcatString(): array {
  58. return [
  59. '1 column: string param unicode' =>
  60. [function ($q) {
  61. return [false, [$q->createNamedParameter('👍')], '👍'];
  62. }],
  63. '2 columns: string param and string param' =>
  64. [function ($q) {
  65. return [false, [$q->createNamedParameter('foo'), $q->createNamedParameter('bar')], 'foobar'];
  66. }],
  67. '2 columns: string param and int literal' =>
  68. [function ($q) {
  69. return [false, [$q->createNamedParameter('foo'), $q->expr()->literal(1)], 'foo1'];
  70. }],
  71. '2 columns: string param and string literal' =>
  72. [function ($q) {
  73. return [false, [$q->createNamedParameter('foo'), $q->expr()->literal('bar')], 'foobar'];
  74. }],
  75. '2 columns: string real and int literal' =>
  76. [function ($q) {
  77. return [true, ['configkey', $q->expr()->literal(2)], '12'];
  78. }],
  79. '4 columns: string literal' =>
  80. [function ($q) {
  81. return [false, [$q->expr()->literal('foo'), $q->expr()->literal('bar'), $q->expr()->literal('foo'), $q->expr()->literal('bar')], 'foobarfoobar'];
  82. }],
  83. '4 columns: int literal' =>
  84. [function ($q) {
  85. return [false, [$q->expr()->literal(1), $q->expr()->literal(2), $q->expr()->literal(3), $q->expr()->literal(4)], '1234'];
  86. }],
  87. '5 columns: string param with special chars used in the function' =>
  88. [function ($q) {
  89. return [false, [$q->createNamedParameter("b"), $q->createNamedParameter("'"), $q->createNamedParameter('||'), $q->createNamedParameter(','), $q->createNamedParameter('a')], "b'||,a"];
  90. }],
  91. ];
  92. }
  93. protected function clearDummyData(): void {
  94. $delete = $this->connection->getQueryBuilder();
  95. $delete->delete('appconfig')
  96. ->where($delete->expr()->eq('appid', $delete->createNamedParameter('group_concat')));
  97. $delete->executeStatement();
  98. }
  99. protected function addDummyData(): void {
  100. $this->clearDummyData();
  101. $insert = $this->connection->getQueryBuilder();
  102. $insert->insert('appconfig')
  103. ->setValue('appid', $insert->createNamedParameter('group_concat'))
  104. ->setValue('configvalue', $insert->createNamedParameter('unittest'))
  105. ->setValue('configkey', $insert->createParameter('value'));
  106. $insert->setParameter('value', '1');
  107. $insert->executeStatement();
  108. $insert->setParameter('value', '3');
  109. $insert->executeStatement();
  110. $insert->setParameter('value', '2');
  111. $insert->executeStatement();
  112. }
  113. public function testGroupConcatWithoutSeparator(): void {
  114. $this->addDummyData();
  115. $query = $this->connection->getQueryBuilder();
  116. $query->select($query->func()->groupConcat('configkey'))
  117. ->from('appconfig')
  118. ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
  119. $result = $query->execute();
  120. $column = $result->fetchOne();
  121. $result->closeCursor();
  122. $this->assertStringContainsString(',', $column);
  123. $actual = explode(',', $column);
  124. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  125. }
  126. public function testGroupConcatWithSeparator(): void {
  127. $this->addDummyData();
  128. $query = $this->connection->getQueryBuilder();
  129. $query->select($query->func()->groupConcat('configkey', '#'))
  130. ->from('appconfig')
  131. ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
  132. $result = $query->execute();
  133. $column = $result->fetchOne();
  134. $result->closeCursor();
  135. $this->assertStringContainsString('#', $column);
  136. $actual = explode('#', $column);
  137. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  138. }
  139. public function testGroupConcatWithSingleQuoteSeparator(): void {
  140. $this->addDummyData();
  141. $query = $this->connection->getQueryBuilder();
  142. $query->select($query->func()->groupConcat('configkey', '\''))
  143. ->from('appconfig')
  144. ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
  145. $result = $query->execute();
  146. $column = $result->fetchOne();
  147. $result->closeCursor();
  148. $this->assertStringContainsString("'", $column);
  149. $actual = explode("'", $column);
  150. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  151. }
  152. public function testGroupConcatWithDoubleQuoteSeparator(): void {
  153. $this->addDummyData();
  154. $query = $this->connection->getQueryBuilder();
  155. $query->select($query->func()->groupConcat('configkey', '"'))
  156. ->from('appconfig')
  157. ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
  158. $result = $query->execute();
  159. $column = $result->fetchOne();
  160. $result->closeCursor();
  161. $this->assertStringContainsString('"', $column);
  162. $actual = explode('"', $column);
  163. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  164. }
  165. protected function clearIntDummyData(): void {
  166. $delete = $this->connection->getQueryBuilder();
  167. $delete->delete('systemtag')
  168. ->where($delete->expr()->eq('name', $delete->createNamedParameter('group_concat')));
  169. $delete->executeStatement();
  170. }
  171. protected function addIntDummyData(): void {
  172. $this->clearIntDummyData();
  173. $insert = $this->connection->getQueryBuilder();
  174. $insert->insert('systemtag')
  175. ->setValue('name', $insert->createNamedParameter('group_concat'))
  176. ->setValue('visibility', $insert->createNamedParameter(1))
  177. ->setValue('editable', $insert->createParameter('value'));
  178. $insert->setParameter('value', 1);
  179. $insert->executeStatement();
  180. $insert->setParameter('value', 2);
  181. $insert->executeStatement();
  182. $insert->setParameter('value', 3);
  183. $insert->executeStatement();
  184. }
  185. public function testIntGroupConcatWithoutSeparator(): void {
  186. $this->addIntDummyData();
  187. $query = $this->connection->getQueryBuilder();
  188. $query->select($query->func()->groupConcat('editable'))
  189. ->from('systemtag')
  190. ->where($query->expr()->eq('name', $query->createNamedParameter('group_concat')));
  191. $result = $query->execute();
  192. $column = $result->fetchOne();
  193. $result->closeCursor();
  194. $this->assertStringContainsString(',', $column);
  195. $actual = explode(',', $column);
  196. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  197. }
  198. public function testIntGroupConcatWithSeparator(): void {
  199. $this->addIntDummyData();
  200. $query = $this->connection->getQueryBuilder();
  201. $query->select($query->func()->groupConcat('editable', '#'))
  202. ->from('systemtag')
  203. ->where($query->expr()->eq('name', $query->createNamedParameter('group_concat')));
  204. $result = $query->execute();
  205. $column = $result->fetchOne();
  206. $result->closeCursor();
  207. $this->assertStringContainsString('#', $column);
  208. $actual = explode('#', $column);
  209. $this->assertEqualsCanonicalizing([1,2,3], $actual);
  210. }
  211. public function testMd5() {
  212. $query = $this->connection->getQueryBuilder();
  213. $query->select($query->func()->md5($query->createNamedParameter('foobar')));
  214. $query->from('appconfig')
  215. ->setMaxResults(1);
  216. $result = $query->execute();
  217. $column = $result->fetchOne();
  218. $result->closeCursor();
  219. $this->assertEquals(md5('foobar'), $column);
  220. }
  221. public function testSubstring() {
  222. $query = $this->connection->getQueryBuilder();
  223. $query->select($query->func()->substring($query->createNamedParameter('foobar'), new Literal(2), $query->createNamedParameter(2)));
  224. $query->from('appconfig')
  225. ->setMaxResults(1);
  226. $result = $query->execute();
  227. $column = $result->fetchOne();
  228. $result->closeCursor();
  229. $this->assertEquals('oo', $column);
  230. }
  231. public function testSubstringNoLength() {
  232. $query = $this->connection->getQueryBuilder();
  233. $query->select($query->func()->substring($query->createNamedParameter('foobar'), new Literal(2)));
  234. $query->from('appconfig')
  235. ->setMaxResults(1);
  236. $result = $query->execute();
  237. $column = $result->fetchOne();
  238. $result->closeCursor();
  239. $this->assertEquals('oobar', $column);
  240. }
  241. public function testLower() {
  242. $query = $this->connection->getQueryBuilder();
  243. $query->select($query->func()->lower($query->createNamedParameter('FooBar')));
  244. $query->from('appconfig')
  245. ->setMaxResults(1);
  246. $result = $query->execute();
  247. $column = $result->fetchOne();
  248. $result->closeCursor();
  249. $this->assertEquals('foobar', $column);
  250. }
  251. public function testAdd() {
  252. $query = $this->connection->getQueryBuilder();
  253. $query->select($query->func()->add($query->createNamedParameter(2, IQueryBuilder::PARAM_INT), new Literal(1)));
  254. $query->from('appconfig')
  255. ->setMaxResults(1);
  256. $result = $query->execute();
  257. $column = $result->fetchOne();
  258. $result->closeCursor();
  259. $this->assertEquals(3, $column);
  260. }
  261. public function testSubtract() {
  262. $query = $this->connection->getQueryBuilder();
  263. $query->select($query->func()->subtract($query->createNamedParameter(2, IQueryBuilder::PARAM_INT), new Literal(1)));
  264. $query->from('appconfig')
  265. ->setMaxResults(1);
  266. $result = $query->execute();
  267. $column = $result->fetchOne();
  268. $result->closeCursor();
  269. $this->assertEquals(1, $column);
  270. }
  271. public function testCount() {
  272. $query = $this->connection->getQueryBuilder();
  273. $query->select($query->func()->count('appid'));
  274. $query->from('appconfig')
  275. ->setMaxResults(1);
  276. $result = $query->execute();
  277. $column = $result->fetchOne();
  278. $result->closeCursor();
  279. $this->assertGreaterThan(1, $column);
  280. }
  281. public function octetLengthProvider() {
  282. return [
  283. ['', 0],
  284. ['foobar', 6],
  285. ['fé', 3],
  286. ['šđčćž', 10],
  287. ];
  288. }
  289. /**
  290. * @dataProvider octetLengthProvider
  291. */
  292. public function testOctetLength(string $str, int $bytes) {
  293. $query = $this->connection->getQueryBuilder();
  294. $query->select($query->func()->octetLength($query->createNamedParameter($str, IQueryBuilder::PARAM_STR)));
  295. $query->from('appconfig')
  296. ->setMaxResults(1);
  297. $result = $query->execute();
  298. $column = $result->fetchOne();
  299. $result->closeCursor();
  300. $this->assertEquals($bytes, $column);
  301. }
  302. public function charLengthProvider() {
  303. return [
  304. ['', 0],
  305. ['foobar', 6],
  306. ['fé', 2],
  307. ['šđčćž', 5],
  308. ];
  309. }
  310. /**
  311. * @dataProvider charLengthProvider
  312. */
  313. public function testCharLength(string $str, int $bytes) {
  314. $query = $this->connection->getQueryBuilder();
  315. $query->select($query->func()->charLength($query->createNamedParameter($str, IQueryBuilder::PARAM_STR)));
  316. $query->from('appconfig')
  317. ->setMaxResults(1);
  318. $result = $query->execute();
  319. $column = $result->fetchOne();
  320. $result->closeCursor();
  321. $this->assertEquals($bytes, $column);
  322. }
  323. private function setUpMinMax($value) {
  324. $query = $this->connection->getQueryBuilder();
  325. $query->insert('appconfig')
  326. ->values([
  327. 'appid' => $query->createNamedParameter('minmax'),
  328. 'configkey' => $query->createNamedParameter(uniqid()),
  329. 'configvalue' => $query->createNamedParameter((string)$value),
  330. ]);
  331. $query->execute();
  332. }
  333. private function clearMinMax() {
  334. $query = $this->connection->getQueryBuilder();
  335. $query->delete('appconfig')
  336. ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax')));
  337. $query->execute();
  338. }
  339. public function testMaxEmpty() {
  340. $this->clearMinMax();
  341. $query = $this->connection->getQueryBuilder();
  342. $query->select($query->func()->max($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)));
  343. $query->from('appconfig')
  344. ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax')))
  345. ->setMaxResults(1);
  346. $result = $query->execute();
  347. $row = $result->fetchOne();
  348. $result->closeCursor();
  349. $this->assertEquals(null, $row);
  350. }
  351. public function testMinEmpty() {
  352. $this->clearMinMax();
  353. $query = $this->connection->getQueryBuilder();
  354. $query->select($query->func()->min($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)));
  355. $query->from('appconfig')
  356. ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax')))
  357. ->setMaxResults(1);
  358. $result = $query->execute();
  359. $row = $result->fetchOne();
  360. $result->closeCursor();
  361. $this->assertEquals(null, $row);
  362. }
  363. public function testMax() {
  364. $this->clearMinMax();
  365. $this->setUpMinMax(10);
  366. $this->setUpMinMax(11);
  367. $this->setUpMinMax(20);
  368. $query = $this->connection->getQueryBuilder();
  369. $query->select($query->func()->max($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)));
  370. $query->from('appconfig')
  371. ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax')))
  372. ->setMaxResults(1);
  373. $result = $query->execute();
  374. $row = $result->fetchOne();
  375. $result->closeCursor();
  376. $this->assertEquals(20, $row);
  377. }
  378. public function testMin() {
  379. $this->clearMinMax();
  380. $this->setUpMinMax(10);
  381. $this->setUpMinMax(11);
  382. $this->setUpMinMax(20);
  383. $query = $this->connection->getQueryBuilder();
  384. $query->select($query->func()->min($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)));
  385. $query->from('appconfig')
  386. ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax')))
  387. ->setMaxResults(1);
  388. $result = $query->execute();
  389. $row = $result->fetchOne();
  390. $result->closeCursor();
  391. $this->assertEquals(10, $row);
  392. }
  393. public function testGreatest() {
  394. $query = $this->connection->getQueryBuilder();
  395. $query->select($query->func()->greatest($query->createNamedParameter(2, IQueryBuilder::PARAM_INT), new Literal(1)));
  396. $query->from('appconfig')
  397. ->setMaxResults(1);
  398. $result = $query->execute();
  399. $row = $result->fetchOne();
  400. $result->closeCursor();
  401. $this->assertEquals(2, $row);
  402. }
  403. public function testLeast() {
  404. $query = $this->connection->getQueryBuilder();
  405. $query->select($query->func()->least($query->createNamedParameter(2, IQueryBuilder::PARAM_INT), new Literal(1)));
  406. $query->from('appconfig')
  407. ->setMaxResults(1);
  408. $result = $query->execute();
  409. $row = $result->fetchOne();
  410. $result->closeCursor();
  411. $this->assertEquals(1, $row);
  412. }
  413. }