WhereTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. <?php
  2. namespace Medoo\Tests;
  3. use Medoo\Medoo;
  4. /**
  5. * @coversDefaultClass \Medoo\Medoo
  6. */
  7. class WhereTest extends MedooTestCase
  8. {
  9. /**
  10. * @covers ::select()
  11. * @covers ::dataImplode()
  12. * @covers ::typeMap()
  13. * @covers ::whereClause()
  14. * @dataProvider typesProvider
  15. */
  16. public function testBasicWhere($type)
  17. {
  18. $this->setType($type);
  19. $this->database->select("account", "user_name", [
  20. "email" => "foo@bar.com",
  21. "user_id" => 200,
  22. "user_id[>]" => 200,
  23. "user_id[>=]" => 200,
  24. "user_id[!]" => 200,
  25. "age[<>]" => [200, 500],
  26. "age[><]" => [200, 500],
  27. "income[>]" => Medoo::raw("COUNT(<average>)"),
  28. "remote_id" => Medoo::raw("UUID()"),
  29. "location" => null,
  30. "is_selected" => true
  31. ]);
  32. $this->assertQuery(
  33. <<<EOD
  34. SELECT "user_name"
  35. FROM "account"
  36. WHERE
  37. "email" = 'foo@bar.com' AND
  38. "user_id" = 200 AND
  39. "user_id" > 200 AND
  40. "user_id" >= 200 AND
  41. "user_id" != 200 AND
  42. ("age" BETWEEN 200 AND 500) AND
  43. ("age" NOT BETWEEN 200 AND 500) AND
  44. "income" > COUNT("average") AND
  45. "remote_id" = UUID() AND
  46. "location" IS NULL AND
  47. "is_selected" = 1
  48. EOD,
  49. $this->database->queryString
  50. );
  51. }
  52. /**
  53. * @covers ::select()
  54. * @covers ::dataImplode()
  55. * @covers ::whereClause()
  56. * @dataProvider typesProvider
  57. */
  58. public function testBetweenDateTimeWhere($type)
  59. {
  60. $this->setType($type);
  61. $this->database->select("account", "user_name", [
  62. "birthday[<>]" => [date("Y-m-d", mktime(0, 0, 0, 1, 1, 2015)), date("Y-m-d", mktime(0, 0, 0, 1, 1, 2045))]
  63. ]);
  64. $this->assertQuery(
  65. <<<EOD
  66. SELECT "user_name"
  67. FROM "account"
  68. WHERE
  69. ("birthday" BETWEEN '2015-01-01' AND '2045-01-01')
  70. EOD,
  71. $this->database->queryString
  72. );
  73. }
  74. /**
  75. * @covers ::select()
  76. * @covers ::dataImplode()
  77. * @covers ::whereClause()
  78. * @dataProvider typesProvider
  79. */
  80. public function testNotBetweenDateTimeWhere($type)
  81. {
  82. $this->setType($type);
  83. $this->database->select("account", "user_name", [
  84. "birthday[><]" => [date("Y-m-d", mktime(0, 0, 0, 1, 1, 2015)), date("Y-m-d", mktime(0, 0, 0, 1, 1, 2045))]
  85. ]);
  86. $this->assertQuery(
  87. <<<EOD
  88. SELECT "user_name"
  89. FROM "account"
  90. WHERE
  91. ("birthday" NOT BETWEEN '2015-01-01' AND '2045-01-01')
  92. EOD,
  93. $this->database->queryString
  94. );
  95. }
  96. /**
  97. * @covers ::select()
  98. * @covers ::dataImplode()
  99. * @covers ::whereClause()
  100. * @dataProvider typesProvider
  101. */
  102. public function testGreaterDateTimeWhere($type)
  103. {
  104. $this->setType($type);
  105. $this->database->select("account", "user_name", [
  106. "birthday[>]" => date("Y-m-d", mktime(0, 0, 0, 1, 1, 2045))
  107. ]);
  108. $this->assertQuery(
  109. <<<EOD
  110. SELECT "user_name"
  111. FROM "account"
  112. WHERE "birthday" > '2045-01-01'
  113. EOD,
  114. $this->database->queryString
  115. );
  116. }
  117. /**
  118. * @covers ::select()
  119. * @covers ::dataImplode()
  120. * @covers ::whereClause()
  121. * @dataProvider typesProvider
  122. */
  123. public function testArrayIntValuesWhere($type)
  124. {
  125. $this->setType($type);
  126. $this->database->select("account", "user_name", [
  127. "user_id" => [2, 123, 234, 54]
  128. ]);
  129. $this->assertQuery(
  130. <<<EOD
  131. SELECT "user_name"
  132. FROM "account"
  133. WHERE
  134. "user_id" IN (2, 123, 234, 54)
  135. EOD,
  136. $this->database->queryString
  137. );
  138. }
  139. /**
  140. * @covers ::select()
  141. * @covers ::dataImplode()
  142. * @covers ::whereClause()
  143. * @dataProvider typesProvider
  144. */
  145. public function testArrayStringValuesWhere($type)
  146. {
  147. $this->setType($type);
  148. $this->database->select("account", "user_name", [
  149. "email" => ["foo@bar.com", "cat@dog.com", "admin@medoo.in"]
  150. ]);
  151. $this->assertQuery(
  152. <<<EOD
  153. SELECT "user_name"
  154. FROM "account"
  155. WHERE
  156. "email" IN ('foo@bar.com', 'cat@dog.com', 'admin@medoo.in')
  157. EOD,
  158. $this->database->queryString
  159. );
  160. }
  161. /**
  162. * @covers ::select()
  163. * @covers ::dataImplode()
  164. * @covers ::whereClause()
  165. * @dataProvider typesProvider
  166. */
  167. public function testNegativeWhere($type)
  168. {
  169. $this->setType($type);
  170. $this->database->select("account", "user_name", [
  171. "AND" => [
  172. "user_name[!]" => "foo",
  173. "user_id[!]" => 1024,
  174. "email[!]" => ["foo@bar.com", "admin@medoo.in"],
  175. "city[!]" => null,
  176. "promoted[!]" => true,
  177. "location[!]" => Medoo::raw('LOWER("New York")')
  178. ]
  179. ]);
  180. $this->assertQuery(
  181. <<<EOD
  182. SELECT "user_name"
  183. FROM "account"
  184. WHERE
  185. ("user_name" != 'foo' AND
  186. "user_id" != 1024 AND
  187. "email" NOT IN ('foo@bar.com', 'admin@medoo.in') AND
  188. "city" IS NOT NULL AND
  189. "promoted" != 1 AND
  190. "location" != LOWER("New York"))
  191. EOD,
  192. $this->database->queryString
  193. );
  194. }
  195. /**
  196. * @covers ::select()
  197. * @covers ::dataImplode()
  198. * @covers ::whereClause()
  199. * @dataProvider typesProvider
  200. */
  201. public function testBasicAndRelativityWhere($type)
  202. {
  203. $this->setType($type);
  204. $this->database->select("account", "user_name", [
  205. "AND" => [
  206. "user_id[>]" => 200,
  207. "gender" => "female"
  208. ]
  209. ]);
  210. $this->assertQuery(
  211. <<<EOD
  212. SELECT "user_name"
  213. FROM "account"
  214. WHERE
  215. ("user_id" > 200 AND "gender" = 'female')
  216. EOD,
  217. $this->database->queryString
  218. );
  219. }
  220. /**
  221. * @covers ::select()
  222. * @covers ::dataImplode()
  223. * @covers ::whereClause()
  224. * @dataProvider typesProvider
  225. */
  226. public function testBasicSingleRelativityWhere($type)
  227. {
  228. $this->setType($type);
  229. $this->database->select("account", "user_name", [
  230. "user_id[>]" => 200,
  231. "gender" => "female"
  232. ]);
  233. $this->assertQuery(
  234. <<<EOD
  235. SELECT "user_name"
  236. FROM "account"
  237. WHERE
  238. "user_id" > 200 AND "gender" = 'female'
  239. EOD,
  240. $this->database->queryString
  241. );
  242. }
  243. /**
  244. * @covers ::select()
  245. * @covers ::dataImplode()
  246. * @covers ::whereClause()
  247. * @dataProvider typesProvider
  248. */
  249. public function testBasicOrRelativityWhere($type)
  250. {
  251. $this->setType($type);
  252. $this->database->select("account", "user_name", [
  253. "OR" => [
  254. "user_id[>]" => 200,
  255. "age[<>]" => [18, 25],
  256. "gender" => "female"
  257. ]
  258. ]);
  259. $this->assertQuery(
  260. <<<EOD
  261. SELECT "user_name"
  262. FROM "account"
  263. WHERE
  264. ("user_id" > 200 OR
  265. ("age" BETWEEN 18 AND 25) OR
  266. "gender" = 'female')
  267. EOD,
  268. $this->database->queryString
  269. );
  270. }
  271. /**
  272. * @covers ::select()
  273. * @covers ::dataImplode()
  274. * @covers ::whereClause()
  275. * @dataProvider typesProvider
  276. */
  277. public function testCompoundRelativityWhere($type)
  278. {
  279. $this->setType($type);
  280. $this->database->select("account", "user_name", [
  281. "AND" => [
  282. "OR" => [
  283. "user_name" => "foo",
  284. "email" => "foo@bar.com"
  285. ],
  286. "password" => "12345"
  287. ]
  288. ]);
  289. $this->assertQuery(
  290. <<<EOD
  291. SELECT "user_name"
  292. FROM "account"
  293. WHERE
  294. (("user_name" = 'foo' OR "email" = 'foo@bar.com') AND "password" = '12345')
  295. EOD,
  296. $this->database->queryString
  297. );
  298. }
  299. /**
  300. * @covers ::select()
  301. * @covers ::dataImplode()
  302. * @covers ::whereClause()
  303. * @dataProvider typesProvider
  304. */
  305. public function testCompoundDuplicatedKeysWhere($type)
  306. {
  307. $this->setType($type);
  308. $this->database->select("account", "user_name", [
  309. "AND #comment" => [
  310. "OR #first comment" => [
  311. "user_name" => "foo",
  312. "email" => "foo@bar.com"
  313. ],
  314. "OR #sencond comment" => [
  315. "user_name" => "bar",
  316. "email" => "bar@foo.com"
  317. ]
  318. ]
  319. ]);
  320. $this->assertQuery(
  321. <<<EOD
  322. SELECT "user_name"
  323. FROM "account"
  324. WHERE
  325. (("user_name" = 'foo' OR "email" = 'foo@bar.com') AND
  326. ("user_name" = 'bar' OR "email" = 'bar@foo.com'))
  327. EOD,
  328. $this->database->queryString
  329. );
  330. }
  331. /**
  332. * @covers ::select()
  333. * @covers ::dataImplode()
  334. * @covers ::whereClause()
  335. * @dataProvider typesProvider
  336. */
  337. public function testColumnsRelationshipWhere($type)
  338. {
  339. $this->setType($type);
  340. $this->database->select("post", [
  341. "[>]account" => "user_id",
  342. ], [
  343. "post.content"
  344. ], [
  345. "post.restrict[<]account.age"
  346. ]);
  347. $this->assertQuery(
  348. <<<EOD
  349. SELECT "post"."content"
  350. FROM "post"
  351. LEFT JOIN "account"
  352. USING ("user_id")
  353. WHERE "post"."restrict" < "account"."age"
  354. EOD,
  355. $this->database->queryString
  356. );
  357. }
  358. /**
  359. * @covers ::select()
  360. * @covers ::dataImplode()
  361. * @covers ::whereClause()
  362. * @dataProvider typesProvider
  363. */
  364. public function testBasicLikeWhere($type)
  365. {
  366. $this->setType($type);
  367. $this->database->select("account", "user_name", [
  368. "city[~]" => "lon"
  369. ]);
  370. $this->assertQuery(
  371. <<<EOD
  372. SELECT "user_name"
  373. FROM "account"
  374. WHERE
  375. ("city" LIKE '%lon%')
  376. EOD,
  377. $this->database->queryString
  378. );
  379. }
  380. /**
  381. * @covers ::select()
  382. * @covers ::dataImplode()
  383. * @covers ::whereClause()
  384. * @dataProvider typesProvider
  385. */
  386. public function testGroupedLikeWhere($type)
  387. {
  388. $this->setType($type);
  389. $this->database->select("account", "user_name", [
  390. "city[~]" => ["lon", "foo", "bar"]
  391. ]);
  392. $this->assertQuery(
  393. <<<EOD
  394. SELECT "user_name"
  395. FROM "account"
  396. WHERE
  397. ("city" LIKE '%lon%' OR
  398. "city" LIKE '%foo%' OR
  399. "city" LIKE '%bar%')
  400. EOD,
  401. $this->database->queryString
  402. );
  403. }
  404. /**
  405. * @covers ::select()
  406. * @covers ::dataImplode()
  407. * @covers ::whereClause()
  408. * @dataProvider typesProvider
  409. */
  410. public function testNegativeLikeWhere($type)
  411. {
  412. $this->setType($type);
  413. $this->database->select("account", "user_name", [
  414. "city[!~]" => "lon"
  415. ]);
  416. $this->assertQuery(
  417. <<<EOD
  418. SELECT "user_name"
  419. FROM "account"
  420. WHERE
  421. ("city" NOT LIKE '%lon%')
  422. EOD,
  423. $this->database->queryString
  424. );
  425. }
  426. /**
  427. * @covers ::select()
  428. * @covers ::dataImplode()
  429. * @covers ::whereClause()
  430. * @dataProvider typesProvider
  431. */
  432. public function testNonEscapeLikeWhere($type)
  433. {
  434. $this->setType($type);
  435. $this->database->select("account", "user_name", [
  436. "city[~]" => "some_where"
  437. ]);
  438. $this->assertQuery(
  439. <<<EOD
  440. SELECT "user_name"
  441. FROM "account"
  442. WHERE
  443. ("city" LIKE 'some_where')
  444. EOD,
  445. $this->database->queryString
  446. );
  447. }
  448. /**
  449. * @covers ::select()
  450. * @covers ::dataImplode()
  451. * @covers ::whereClause()
  452. * @dataProvider typesProvider
  453. */
  454. public function testEscapeLikeWhere($type)
  455. {
  456. $this->setType($type);
  457. $this->database->select("account", "user_name", [
  458. "city[~]" => "some\_where"
  459. ]);
  460. $this->assertQuery(
  461. <<<EOD
  462. SELECT "user_name"
  463. FROM "account"
  464. WHERE
  465. ("city" LIKE '%some\_where%')
  466. EOD,
  467. $this->database->queryString
  468. );
  469. }
  470. /**
  471. * @covers ::select()
  472. * @covers ::dataImplode()
  473. * @covers ::whereClause()
  474. * @dataProvider typesProvider
  475. */
  476. public function testCompoundLikeWhere($type)
  477. {
  478. $this->setType($type);
  479. $this->database->select("account", "user_name", [
  480. "content[~]" => ["AND" => ["lon", "on"]],
  481. "city[~]" => ["OR" => ["lon", "on"]]
  482. ]);
  483. $this->assertQuery(
  484. <<<EOD
  485. SELECT "user_name"
  486. FROM "account"
  487. WHERE
  488. ("content" LIKE '%lon%' AND "content" LIKE '%on%') AND
  489. ("city" LIKE '%lon%' OR "city" LIKE '%on%')
  490. EOD,
  491. $this->database->queryString
  492. );
  493. }
  494. /**
  495. * @covers ::select()
  496. * @covers ::dataImplode()
  497. * @covers ::whereClause()
  498. * @dataProvider typesProvider
  499. */
  500. public function testWildcardLikeWhere($type)
  501. {
  502. $this->setType($type);
  503. $this->database->select("account", "user_name", [
  504. "city[~]" => "%stan",
  505. "location[~]" => "Londo_",
  506. "name[~]" => "[BCR]at",
  507. "nickname[~]" => "[!BCR]at"
  508. ]);
  509. $this->assertQuery(
  510. <<<EOD
  511. SELECT "user_name"
  512. FROM "account"
  513. WHERE
  514. ("city" LIKE '%stan') AND
  515. ("location" LIKE 'Londo_') AND
  516. ("name" LIKE '[BCR]at') AND
  517. ("nickname" LIKE '[!BCR]at')
  518. EOD,
  519. $this->database->queryString
  520. );
  521. }
  522. /**
  523. * @covers ::select()
  524. * @covers ::dataImplode()
  525. * @covers ::whereClause()
  526. * @dataProvider typesProvider
  527. */
  528. public function testBasicOrderWhere($type)
  529. {
  530. $this->setType($type);
  531. $this->database->select("account", "user_name", [
  532. "ORDER" => "user_id"
  533. ]);
  534. $this->assertQuery(
  535. <<<EOD
  536. SELECT "user_name"
  537. FROM "account"
  538. ORDER BY "user_id"
  539. EOD,
  540. $this->database->queryString
  541. );
  542. }
  543. /**
  544. * @covers ::select()
  545. * @covers ::dataImplode()
  546. * @covers ::whereClause()
  547. * @dataProvider typesProvider
  548. */
  549. public function testMultipleOrderWhere($type)
  550. {
  551. $this->setType($type);
  552. $this->database->select("account", "user_name", [
  553. "ORDER" => [
  554. // Order by column with sorting by customized order.
  555. "user_id" => [43, 12, 57, 98, 144, 1],
  556. // Order by column.
  557. "register_date",
  558. // Order by column with descending sorting.
  559. "profile_id" => "DESC",
  560. // Order by column with ascending sorting.
  561. "date" => "ASC"
  562. ]
  563. ]);
  564. $this->assertQuery(
  565. <<<EOD
  566. SELECT "user_name"
  567. FROM "account"
  568. ORDER BY FIELD("user_id", 43,12,57,98,144,1),"register_date","profile_id" DESC,"date" ASC
  569. EOD,
  570. $this->database->queryString
  571. );
  572. }
  573. /**
  574. * @covers ::select()
  575. * @covers ::dataImplode()
  576. * @covers ::whereClause()
  577. * @dataProvider typesProvider
  578. */
  579. public function testOrderWithRawWhere($type)
  580. {
  581. $this->setType($type);
  582. $this->database->select("account", "user_name", [
  583. "ORDER" => Medoo::raw("<location>, <gender>")
  584. ]);
  585. $this->assertQuery(
  586. <<<EOD
  587. SELECT "user_name"
  588. FROM "account"
  589. ORDER BY "location", "gender"
  590. EOD,
  591. $this->database->queryString
  592. );
  593. }
  594. /**
  595. * @covers ::select()
  596. * @covers ::dataImplode()
  597. * @covers ::whereClause()
  598. */
  599. public function testFullTextSearchWhere()
  600. {
  601. $this->setType("mysql");
  602. $this->database->select("account", "user_name", [
  603. "MATCH" => [
  604. "columns" => ["content", "title"],
  605. "keyword" => "foo",
  606. "mode" => "natural"
  607. ]
  608. ]);
  609. $this->assertQuery(
  610. <<<EOD
  611. SELECT "user_name"
  612. FROM "account"
  613. WHERE MATCH (`content`, `title`) AGAINST ('foo' IN NATURAL LANGUAGE MODE)
  614. EOD,
  615. $this->database->queryString
  616. );
  617. }
  618. /**
  619. * @covers ::select()
  620. * @covers ::dataImplode()
  621. * @covers ::whereClause()
  622. * @dataProvider typesProvider
  623. */
  624. public function testRegularExpressionWhere($type)
  625. {
  626. $this->setType($type);
  627. $this->database->select("account", "user_name", [
  628. 'user_name[REGEXP]' => '[a-z0-9]*'
  629. ]);
  630. $this->assertQuery(
  631. <<<EOD
  632. SELECT "user_name"
  633. FROM "account"
  634. WHERE "user_name" REGEXP '[a-z0-9]*'
  635. EOD,
  636. $this->database->queryString
  637. );
  638. }
  639. /**
  640. * @covers ::select()
  641. * @covers ::dataImplode()
  642. * @covers ::whereClause()
  643. * @dataProvider typesProvider
  644. */
  645. public function testRawWhere($type)
  646. {
  647. $this->setType($type);
  648. $this->database->select("account", "user_name", [
  649. 'datetime' => Medoo::raw('NOW()')
  650. ]);
  651. $this->assertQuery(
  652. <<<EOD
  653. SELECT "user_name"
  654. FROM "account"
  655. WHERE "datetime" = NOW()
  656. EOD,
  657. $this->database->queryString
  658. );
  659. }
  660. /**
  661. * @covers ::select()
  662. * @covers ::dataImplode()
  663. * @covers ::whereClause()
  664. * @dataProvider typesProvider
  665. */
  666. public function testLimitWhere($type)
  667. {
  668. $this->setType($type);
  669. $this->database->select("account", "user_name", [
  670. 'LIMIT' => 100
  671. ]);
  672. $this->assertQuery([
  673. 'default' => <<<EOD
  674. SELECT "user_name"
  675. FROM "account"
  676. LIMIT 100
  677. EOD,
  678. 'mssql' => <<<EOD
  679. SELECT [user_name]
  680. FROM [account]
  681. ORDER BY (SELECT 0)
  682. OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY
  683. EOD,
  684. 'oracle' => <<<EOD
  685. SELECT "user_name"
  686. FROM "account"
  687. OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY
  688. EOD,
  689. ], $this->database->queryString);
  690. }
  691. /**
  692. * @covers ::select()
  693. * @covers ::dataImplode()
  694. * @covers ::whereClause()
  695. * @dataProvider typesProvider
  696. */
  697. public function testLimitOffsetWhere($type)
  698. {
  699. $this->setType($type);
  700. $this->database->select("account", "user_name", [
  701. 'LIMIT' => [20, 100]
  702. ]);
  703. $this->assertQuery([
  704. 'default' => <<<EOD
  705. SELECT "user_name"
  706. FROM "account"
  707. LIMIT 100 OFFSET 20
  708. EOD,
  709. 'mssql' => <<<EOD
  710. SELECT [user_name]
  711. FROM [account]
  712. ORDER BY (SELECT 0)
  713. OFFSET 20 ROWS FETCH NEXT 100 ROWS ONLY
  714. EOD,
  715. 'oracle' => <<<EOD
  716. SELECT "user_name"
  717. FROM "account"
  718. OFFSET 20 ROWS FETCH NEXT 100 ROWS ONLY
  719. EOD,
  720. ], $this->database->queryString);
  721. }
  722. /**
  723. * @covers ::select()
  724. * @covers ::dataImplode()
  725. * @covers ::whereClause()
  726. * @dataProvider typesProvider
  727. */
  728. public function testGroupWhere($type)
  729. {
  730. $this->setType($type);
  731. $this->database->select("account", "user_name", [
  732. 'GROUP' => 'type',
  733. ]);
  734. $this->assertQuery(
  735. <<<EOD
  736. SELECT "user_name"
  737. FROM "account"
  738. GROUP BY "type"
  739. EOD,
  740. $this->database->queryString
  741. );
  742. }
  743. /**
  744. * @covers ::select()
  745. * @covers ::dataImplode()
  746. * @covers ::whereClause()
  747. * @dataProvider typesProvider
  748. */
  749. public function testGroupWithArrayWhere($type)
  750. {
  751. $this->setType($type);
  752. $this->database->select("account", "user_name", [
  753. 'GROUP' => [
  754. 'type',
  755. 'age',
  756. 'gender'
  757. ]
  758. ]);
  759. $this->assertQuery(
  760. <<<EOD
  761. SELECT "user_name"
  762. FROM "account"
  763. GROUP BY "type","age","gender"
  764. EOD,
  765. $this->database->queryString
  766. );
  767. }
  768. /**
  769. * @covers ::select()
  770. * @covers ::dataImplode()
  771. * @covers ::whereClause()
  772. * @dataProvider typesProvider
  773. */
  774. public function testGroupWithRawWhere($type)
  775. {
  776. $this->setType($type);
  777. $this->database->select("account", "user_name", [
  778. 'GROUP' => Medoo::raw("<location>, <gender>")
  779. ]);
  780. $this->assertQuery(
  781. <<<EOD
  782. SELECT "user_name"
  783. FROM "account"
  784. GROUP BY "location", "gender"
  785. EOD,
  786. $this->database->queryString
  787. );
  788. }
  789. /**
  790. * @covers ::select()
  791. * @covers ::dataImplode()
  792. * @covers ::whereClause()
  793. * @dataProvider typesProvider
  794. */
  795. public function testHavingWhere($type)
  796. {
  797. $this->setType($type);
  798. $this->database->select("account", "user_name", [
  799. 'HAVING' => [
  800. 'user_id[>]' => 500
  801. ]
  802. ]);
  803. $this->assertQuery(
  804. <<<EOD
  805. SELECT "user_name"
  806. FROM "account"
  807. HAVING "user_id" > 500
  808. EOD,
  809. $this->database->queryString
  810. );
  811. }
  812. /**
  813. * @covers ::select()
  814. * @covers ::dataImplode()
  815. * @covers ::whereClause()
  816. * @dataProvider typesProvider
  817. */
  818. public function testHavingWithRawWhere($type)
  819. {
  820. $this->setType($type);
  821. $this->database->select("account", "user_name", [
  822. 'HAVING' => Medoo::raw('<location> = LOWER("NEW YORK")')
  823. ]);
  824. $this->assertQuery(
  825. <<<EOD
  826. SELECT "user_name"
  827. FROM "account"
  828. HAVING "location" = LOWER("NEW YORK")
  829. EOD,
  830. $this->database->queryString
  831. );
  832. }
  833. /**
  834. * @covers ::select()
  835. * @covers ::dataImplode()
  836. * @covers ::whereClause()
  837. * @dataProvider typesProvider
  838. */
  839. public function testHavingWithAggregateRawWhere($type)
  840. {
  841. $this->setType($type);
  842. $this->database->select("account", [
  843. "total" => Medoo::raw('SUM(<salary>)')
  844. ], [
  845. 'HAVING' => Medoo::raw('SUM(<salary>) > 1000')
  846. ]);
  847. $this->assertQuery(
  848. <<<EOD
  849. SELECT SUM("salary") AS "total"
  850. FROM "account"
  851. HAVING SUM("salary") > 1000
  852. EOD,
  853. $this->database->queryString
  854. );
  855. }
  856. /**
  857. * @covers ::select()
  858. * @covers ::dataImplode()
  859. * @covers ::whereClause()
  860. * @dataProvider typesProvider
  861. */
  862. public function testRawWhereClause($type)
  863. {
  864. $this->setType($type);
  865. $this->database->select(
  866. "account",
  867. "user_name",
  868. Medoo::raw("WHERE <id> => 10")
  869. );
  870. $this->assertQuery(
  871. <<<EOD
  872. SELECT "user_name"
  873. FROM "account"
  874. WHERE "id" => 10
  875. EOD,
  876. $this->database->queryString
  877. );
  878. }
  879. }