WhereTest.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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 testBetweenStringWhere($type)
  103. {
  104. $this->setType($type);
  105. $this->database->select("account", "user_name", [
  106. "location[<>]" => ['New York', 'Santo']
  107. ]);
  108. $this->assertQuery(
  109. <<<EOD
  110. SELECT "user_name"
  111. FROM "account"
  112. WHERE
  113. ("location" BETWEEN 'New York' AND 'Santo')
  114. EOD,
  115. $this->database->queryString
  116. );
  117. }
  118. /**
  119. * @covers ::select()
  120. * @covers ::dataImplode()
  121. * @covers ::whereClause()
  122. * @dataProvider typesProvider
  123. */
  124. public function testBetweenRawWhere($type)
  125. {
  126. $this->setType($type);
  127. $this->database->select("account", "user_name", [
  128. "birthday[<>]" => [
  129. Medoo::raw("to_date(:from, 'YYYY-MM-DD')", [":from" => '2015/05/15']),
  130. Medoo::raw("to_date(:to, 'YYYY-MM-DD')", [":to" => '2025/05/15'])
  131. ]
  132. ]);
  133. $this->assertQuery(
  134. <<<EOD
  135. SELECT "user_name"
  136. FROM "account"
  137. WHERE
  138. ("birthday" BETWEEN to_date('2015/05/15', 'YYYY-MM-DD') AND to_date('2025/05/15', 'YYYY-MM-DD'))
  139. EOD,
  140. $this->database->queryString
  141. );
  142. }
  143. /**
  144. * @covers ::select()
  145. * @covers ::dataImplode()
  146. * @covers ::whereClause()
  147. * @dataProvider typesProvider
  148. */
  149. public function testGreaterDateTimeWhere($type)
  150. {
  151. $this->setType($type);
  152. $this->database->select("account", "user_name", [
  153. "birthday[>]" => date("Y-m-d", mktime(0, 0, 0, 1, 1, 2045))
  154. ]);
  155. $this->assertQuery(
  156. <<<EOD
  157. SELECT "user_name"
  158. FROM "account"
  159. WHERE "birthday" > '2045-01-01'
  160. EOD,
  161. $this->database->queryString
  162. );
  163. }
  164. /**
  165. * @covers ::select()
  166. * @covers ::dataImplode()
  167. * @covers ::whereClause()
  168. * @dataProvider typesProvider
  169. */
  170. public function testArrayIntValuesWhere($type)
  171. {
  172. $this->setType($type);
  173. $this->database->select("account", "user_name", [
  174. "user_id" => [2, 123, 234, 54]
  175. ]);
  176. $this->assertQuery(
  177. <<<EOD
  178. SELECT "user_name"
  179. FROM "account"
  180. WHERE
  181. "user_id" IN (2, 123, 234, 54)
  182. EOD,
  183. $this->database->queryString
  184. );
  185. }
  186. /**
  187. * @covers ::select()
  188. * @covers ::dataImplode()
  189. * @covers ::whereClause()
  190. * @dataProvider typesProvider
  191. */
  192. public function testArrayStringValuesWhere($type)
  193. {
  194. $this->setType($type);
  195. $this->database->select("account", "user_name", [
  196. "email" => ["foo@bar.com", "cat@dog.com", "admin@medoo.in"]
  197. ]);
  198. $this->assertQuery(
  199. <<<EOD
  200. SELECT "user_name"
  201. FROM "account"
  202. WHERE
  203. "email" IN ('foo@bar.com', 'cat@dog.com', 'admin@medoo.in')
  204. EOD,
  205. $this->database->queryString
  206. );
  207. }
  208. /**
  209. * @covers ::select()
  210. * @covers ::dataImplode()
  211. * @covers ::whereClause()
  212. * @dataProvider typesProvider
  213. */
  214. public function testRawArrayValuesWhere($type)
  215. {
  216. $this->setType($type);
  217. $this->database->select("account", "user_name", [
  218. 'id' => [
  219. Medoo::raw('LOWER("FOO")'),
  220. Medoo::raw('LOWER("BAR")')
  221. ]
  222. ]);
  223. $this->assertQuery(
  224. <<<EOD
  225. SELECT "user_name"
  226. FROM "account"
  227. WHERE
  228. "id" IN (LOWER("FOO"), LOWER("BAR"))
  229. EOD,
  230. $this->database->queryString
  231. );
  232. }
  233. /**
  234. * @covers ::select()
  235. * @covers ::dataImplode()
  236. * @covers ::whereClause()
  237. * @dataProvider typesProvider
  238. */
  239. public function testRawNotInArrayValuesWhere($type)
  240. {
  241. $this->setType($type);
  242. $this->database->select("account", "user_name", [
  243. 'id[!]' => [
  244. Medoo::raw('LOWER("FOO")'),
  245. Medoo::raw('LOWER("BAR")')
  246. ]
  247. ]);
  248. $this->assertQuery(
  249. <<<EOD
  250. SELECT "user_name"
  251. FROM "account"
  252. WHERE
  253. "id" NOT IN (LOWER("FOO"), LOWER("BAR"))
  254. EOD,
  255. $this->database->queryString
  256. );
  257. }
  258. /**
  259. * @covers ::select()
  260. * @covers ::dataImplode()
  261. * @covers ::whereClause()
  262. * @dataProvider typesProvider
  263. */
  264. public function testNegativeWhere($type)
  265. {
  266. $this->setType($type);
  267. $this->database->select("account", "user_name", [
  268. "AND" => [
  269. "user_name[!]" => "foo",
  270. "user_id[!]" => 1024,
  271. "email[!]" => ["foo@bar.com", "admin@medoo.in"],
  272. "city[!]" => null,
  273. "promoted[!]" => true,
  274. "location[!]" => Medoo::raw('LOWER("New York")')
  275. ]
  276. ]);
  277. $this->assertQuery(
  278. <<<EOD
  279. SELECT "user_name"
  280. FROM "account"
  281. WHERE
  282. ("user_name" != 'foo' AND
  283. "user_id" != 1024 AND
  284. "email" NOT IN ('foo@bar.com', 'admin@medoo.in') AND
  285. "city" IS NOT NULL AND
  286. "promoted" != 1 AND
  287. "location" != LOWER("New York"))
  288. EOD,
  289. $this->database->queryString
  290. );
  291. }
  292. /**
  293. * @covers ::select()
  294. * @covers ::dataImplode()
  295. * @covers ::whereClause()
  296. * @dataProvider typesProvider
  297. */
  298. public function testBasicAndRelativityWhere($type)
  299. {
  300. $this->setType($type);
  301. $this->database->select("account", "user_name", [
  302. "AND" => [
  303. "user_id[>]" => 200,
  304. "gender" => "female"
  305. ]
  306. ]);
  307. $this->assertQuery(
  308. <<<EOD
  309. SELECT "user_name"
  310. FROM "account"
  311. WHERE
  312. ("user_id" > 200 AND "gender" = 'female')
  313. EOD,
  314. $this->database->queryString
  315. );
  316. }
  317. /**
  318. * @covers ::select()
  319. * @covers ::dataImplode()
  320. * @covers ::whereClause()
  321. * @dataProvider typesProvider
  322. */
  323. public function testBasicSingleRelativityWhere($type)
  324. {
  325. $this->setType($type);
  326. $this->database->select("account", "user_name", [
  327. "user_id[>]" => 200,
  328. "gender" => "female"
  329. ]);
  330. $this->assertQuery(
  331. <<<EOD
  332. SELECT "user_name"
  333. FROM "account"
  334. WHERE
  335. "user_id" > 200 AND "gender" = 'female'
  336. EOD,
  337. $this->database->queryString
  338. );
  339. }
  340. /**
  341. * @covers ::select()
  342. * @covers ::dataImplode()
  343. * @covers ::whereClause()
  344. * @dataProvider typesProvider
  345. */
  346. public function testBasicOrRelativityWhere($type)
  347. {
  348. $this->setType($type);
  349. $this->database->select("account", "user_name", [
  350. "OR" => [
  351. "user_id[>]" => 200,
  352. "age[<>]" => [18, 25],
  353. "gender" => "female"
  354. ]
  355. ]);
  356. $this->assertQuery(
  357. <<<EOD
  358. SELECT "user_name"
  359. FROM "account"
  360. WHERE
  361. ("user_id" > 200 OR
  362. ("age" BETWEEN 18 AND 25) OR
  363. "gender" = 'female')
  364. EOD,
  365. $this->database->queryString
  366. );
  367. }
  368. /**
  369. * @covers ::select()
  370. * @covers ::dataImplode()
  371. * @covers ::whereClause()
  372. * @dataProvider typesProvider
  373. */
  374. public function testCompoundRelativityWhere($type)
  375. {
  376. $this->setType($type);
  377. $this->database->select("account", "user_name", [
  378. "AND" => [
  379. "OR" => [
  380. "user_name" => "foo",
  381. "email" => "foo@bar.com"
  382. ],
  383. "password" => "12345"
  384. ]
  385. ]);
  386. $this->assertQuery(
  387. <<<EOD
  388. SELECT "user_name"
  389. FROM "account"
  390. WHERE
  391. (("user_name" = 'foo' OR "email" = 'foo@bar.com') AND "password" = '12345')
  392. EOD,
  393. $this->database->queryString
  394. );
  395. }
  396. /**
  397. * @covers ::select()
  398. * @covers ::dataImplode()
  399. * @covers ::whereClause()
  400. * @dataProvider typesProvider
  401. */
  402. public function testCompoundDuplicatedKeysWhere($type)
  403. {
  404. $this->setType($type);
  405. $this->database->select("account", "user_name", [
  406. "AND #comment" => [
  407. "OR #first comment" => [
  408. "user_name" => "foo",
  409. "email" => "foo@bar.com"
  410. ],
  411. "OR #sencond comment" => [
  412. "user_name" => "bar",
  413. "email" => "bar@foo.com"
  414. ]
  415. ]
  416. ]);
  417. $this->assertQuery(
  418. <<<EOD
  419. SELECT "user_name"
  420. FROM "account"
  421. WHERE
  422. (("user_name" = 'foo' OR "email" = 'foo@bar.com') AND
  423. ("user_name" = 'bar' OR "email" = 'bar@foo.com'))
  424. EOD,
  425. $this->database->queryString
  426. );
  427. }
  428. /**
  429. * @covers ::select()
  430. * @covers ::dataImplode()
  431. * @covers ::whereClause()
  432. * @dataProvider typesProvider
  433. */
  434. public function testColumnsRelationshipWhere($type)
  435. {
  436. $this->setType($type);
  437. $this->database->select("post", [
  438. "[>]account" => "user_id",
  439. ], [
  440. "post.content"
  441. ], [
  442. "post.restrict[<]account.age",
  443. "post.type[=]account.type"
  444. ]);
  445. $this->assertQuery(
  446. <<<EOD
  447. SELECT "post"."content"
  448. FROM "post"
  449. LEFT JOIN "account"
  450. USING ("user_id")
  451. WHERE
  452. "post"."restrict" < "account"."age" AND
  453. "post"."type" = "account"."type"
  454. EOD,
  455. $this->database->queryString
  456. );
  457. }
  458. /**
  459. * @covers ::select()
  460. * @covers ::dataImplode()
  461. * @covers ::whereClause()
  462. * @dataProvider typesProvider
  463. */
  464. public function testBasicLikeWhere($type)
  465. {
  466. $this->setType($type);
  467. $this->database->select("account", "user_name", [
  468. "city[~]" => "lon",
  469. "name[~]" => "some-name"
  470. ]);
  471. $this->assertQuery(
  472. <<<EOD
  473. SELECT "user_name"
  474. FROM "account"
  475. WHERE
  476. ("city" LIKE '%lon%') AND
  477. ("name" LIKE '%some-name%')
  478. EOD,
  479. $this->database->queryString
  480. );
  481. }
  482. /**
  483. * @covers ::select()
  484. * @covers ::dataImplode()
  485. * @covers ::whereClause()
  486. * @dataProvider typesProvider
  487. */
  488. public function testGroupedLikeWhere($type)
  489. {
  490. $this->setType($type);
  491. $this->database->select("account", "user_name", [
  492. "city[~]" => ["lon", "foo", "bar"]
  493. ]);
  494. $this->assertQuery(
  495. <<<EOD
  496. SELECT "user_name"
  497. FROM "account"
  498. WHERE
  499. ("city" LIKE '%lon%' OR
  500. "city" LIKE '%foo%' OR
  501. "city" LIKE '%bar%')
  502. EOD,
  503. $this->database->queryString
  504. );
  505. }
  506. /**
  507. * @covers ::select()
  508. * @covers ::dataImplode()
  509. * @covers ::whereClause()
  510. * @dataProvider typesProvider
  511. */
  512. public function testNegativeLikeWhere($type)
  513. {
  514. $this->setType($type);
  515. $this->database->select("account", "user_name", [
  516. "city[!~]" => "lon"
  517. ]);
  518. $this->assertQuery(
  519. <<<EOD
  520. SELECT "user_name"
  521. FROM "account"
  522. WHERE
  523. ("city" NOT LIKE '%lon%')
  524. EOD,
  525. $this->database->queryString
  526. );
  527. }
  528. /**
  529. * @covers ::select()
  530. * @covers ::dataImplode()
  531. * @covers ::whereClause()
  532. * @dataProvider typesProvider
  533. */
  534. public function testNonEscapeLikeWhere($type)
  535. {
  536. $this->setType($type);
  537. $this->database->select("account", "user_name", [
  538. "city[~]" => "some_where",
  539. "county[~]" => "[a-f]stan"
  540. ]);
  541. $this->assertQuery(
  542. <<<EOD
  543. SELECT "user_name"
  544. FROM "account"
  545. WHERE
  546. ("city" LIKE 'some_where') AND
  547. ("county" LIKE '[a-f]stan')
  548. EOD,
  549. $this->database->queryString
  550. );
  551. }
  552. /**
  553. * @covers ::select()
  554. * @covers ::dataImplode()
  555. * @covers ::whereClause()
  556. * @dataProvider typesProvider
  557. */
  558. public function testEscapeLikeWhere($type)
  559. {
  560. $this->setType($type);
  561. $this->database->select("account", "user_name", [
  562. "city[~]" => "some\_where"
  563. ]);
  564. $this->assertQuery(
  565. <<<EOD
  566. SELECT "user_name"
  567. FROM "account"
  568. WHERE
  569. ("city" LIKE '%some\_where%')
  570. EOD,
  571. $this->database->queryString
  572. );
  573. }
  574. /**
  575. * @covers ::select()
  576. * @covers ::dataImplode()
  577. * @covers ::whereClause()
  578. * @dataProvider typesProvider
  579. */
  580. public function testCompoundLikeWhere($type)
  581. {
  582. $this->setType($type);
  583. $this->database->select("account", "user_name", [
  584. "content[~]" => ["AND" => ["lon", "on"]],
  585. "city[~]" => ["OR" => ["lon", "on"]]
  586. ]);
  587. $this->assertQuery(
  588. <<<EOD
  589. SELECT "user_name"
  590. FROM "account"
  591. WHERE
  592. ("content" LIKE '%lon%' AND "content" LIKE '%on%') AND
  593. ("city" LIKE '%lon%' OR "city" LIKE '%on%')
  594. EOD,
  595. $this->database->queryString
  596. );
  597. }
  598. /**
  599. * @covers ::select()
  600. * @covers ::dataImplode()
  601. * @covers ::whereClause()
  602. * @dataProvider typesProvider
  603. */
  604. public function testWildcardLikeWhere($type)
  605. {
  606. $this->setType($type);
  607. $this->database->select("account", "user_name", [
  608. "city[~]" => "%stan",
  609. "company[~]" => "Goo%",
  610. "location[~]" => "Londo_",
  611. "name[~]" => "[BCR]at",
  612. "nickname[~]" => "[!BCR]at"
  613. ]);
  614. $this->assertQuery(
  615. <<<EOD
  616. SELECT "user_name"
  617. FROM "account"
  618. WHERE
  619. ("city" LIKE '%stan') AND
  620. ("company" LIKE 'Goo%') AND
  621. ("location" LIKE 'Londo_') AND
  622. ("name" LIKE '[BCR]at') AND
  623. ("nickname" LIKE '[!BCR]at')
  624. EOD,
  625. $this->database->queryString
  626. );
  627. }
  628. /**
  629. * @covers ::select()
  630. * @covers ::dataImplode()
  631. * @covers ::whereClause()
  632. * @dataProvider typesProvider
  633. */
  634. public function testMultipleLikeWhere($type)
  635. {
  636. $this->setType($type);
  637. $words = [
  638. "one",
  639. "two",
  640. "three",
  641. "four",
  642. "five",
  643. "six",
  644. "seven",
  645. "eight",
  646. "nine",
  647. "ten",
  648. "eleven",
  649. "twelve"
  650. ];
  651. $this->database->select("account", ["title"], ["title[~]" => $words]);
  652. $this->assertQuery(
  653. <<<EOD
  654. SELECT "title"
  655. FROM "account"
  656. WHERE
  657. ("title" LIKE '%one%' OR "title" LIKE '%two%' OR "title" LIKE '%three%' OR "title" LIKE '%four%' OR "title" LIKE '%five%' OR "title" LIKE '%six%' OR "title" LIKE '%seven%' OR "title" LIKE '%eight%' OR "title" LIKE '%nine%' OR "title" LIKE '%ten%' OR "title" LIKE '%eleven%' OR "title" LIKE '%twelve%')
  658. EOD,
  659. $this->database->queryString
  660. );
  661. }
  662. /**
  663. * @covers ::select()
  664. * @covers ::dataImplode()
  665. * @covers ::whereClause()
  666. * @dataProvider typesProvider
  667. */
  668. public function testBasicOrderWhere($type)
  669. {
  670. $this->setType($type);
  671. $this->database->select("account", "user_name", [
  672. "ORDER" => "user_id"
  673. ]);
  674. $this->assertQuery(
  675. <<<EOD
  676. SELECT "user_name"
  677. FROM "account"
  678. ORDER BY "user_id"
  679. EOD,
  680. $this->database->queryString
  681. );
  682. }
  683. /**
  684. * @covers ::select()
  685. * @covers ::dataImplode()
  686. * @covers ::whereClause()
  687. * @dataProvider typesProvider
  688. */
  689. public function testMultipleOrderWhere($type)
  690. {
  691. $this->setType($type);
  692. $this->database->select("account", "user_name", [
  693. "ORDER" => [
  694. // Order by column with sorting by customized order.
  695. "user_id" => [43, 12, 57, 98, 144, 1],
  696. // Order by column.
  697. "register_date",
  698. // Order by column with descending sorting.
  699. "profile_id" => "DESC",
  700. // Order by column with ascending sorting.
  701. "date" => "ASC"
  702. ]
  703. ]);
  704. $this->assertQuery(
  705. <<<EOD
  706. SELECT "user_name"
  707. FROM "account"
  708. ORDER BY FIELD("user_id", 43,12,57,98,144,1),"register_date","profile_id" DESC,"date" ASC
  709. EOD,
  710. $this->database->queryString
  711. );
  712. }
  713. /**
  714. * @covers ::select()
  715. * @covers ::dataImplode()
  716. * @covers ::whereClause()
  717. * @dataProvider typesProvider
  718. */
  719. public function testOrderWithRawWhere($type)
  720. {
  721. $this->setType($type);
  722. $this->database->select("account", "user_name", [
  723. "ORDER" => Medoo::raw("<location>, <gender>")
  724. ]);
  725. $this->assertQuery(
  726. <<<EOD
  727. SELECT "user_name"
  728. FROM "account"
  729. ORDER BY "location", "gender"
  730. EOD,
  731. $this->database->queryString
  732. );
  733. }
  734. /**
  735. * @covers ::select()
  736. * @covers ::dataImplode()
  737. * @covers ::whereClause()
  738. */
  739. public function testFullTextSearchWhere()
  740. {
  741. $this->setType("mysql");
  742. $this->database->select("account", "user_name", [
  743. "MATCH" => [
  744. "columns" => ["content", "title"],
  745. "keyword" => "foo",
  746. "mode" => "natural"
  747. ]
  748. ]);
  749. $this->assertQuery(
  750. <<<EOD
  751. SELECT "user_name"
  752. FROM "account"
  753. WHERE MATCH (`content`, `title`) AGAINST ('foo' IN NATURAL LANGUAGE MODE)
  754. EOD,
  755. $this->database->queryString
  756. );
  757. }
  758. /**
  759. * @covers ::select()
  760. * @covers ::dataImplode()
  761. * @covers ::whereClause()
  762. * @dataProvider typesProvider
  763. */
  764. public function testRegularExpressionWhere($type)
  765. {
  766. $this->setType($type);
  767. $this->database->select("account", "user_name", [
  768. 'user_name[REGEXP]' => '[a-z0-9]*'
  769. ]);
  770. $this->assertQuery(
  771. <<<EOD
  772. SELECT "user_name"
  773. FROM "account"
  774. WHERE "user_name" REGEXP '[a-z0-9]*'
  775. EOD,
  776. $this->database->queryString
  777. );
  778. }
  779. /**
  780. * @covers ::select()
  781. * @covers ::dataImplode()
  782. * @covers ::whereClause()
  783. * @dataProvider typesProvider
  784. */
  785. public function testRawWhere($type)
  786. {
  787. $this->setType($type);
  788. $this->database->select("account", "user_name", [
  789. 'datetime' => Medoo::raw('NOW()')
  790. ]);
  791. $this->assertQuery(
  792. <<<EOD
  793. SELECT "user_name"
  794. FROM "account"
  795. WHERE "datetime" = NOW()
  796. EOD,
  797. $this->database->queryString
  798. );
  799. }
  800. /**
  801. * @covers ::select()
  802. * @covers ::dataImplode()
  803. * @covers ::whereClause()
  804. * @dataProvider typesProvider
  805. */
  806. public function testLimitWhere($type)
  807. {
  808. $this->setType($type);
  809. $this->database->select("account", "user_name", [
  810. 'LIMIT' => 100
  811. ]);
  812. $this->assertQuery([
  813. 'default' => <<<EOD
  814. SELECT "user_name"
  815. FROM "account"
  816. LIMIT 100
  817. EOD,
  818. 'mssql' => <<<EOD
  819. SELECT [user_name]
  820. FROM [account]
  821. ORDER BY (SELECT 0)
  822. OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY
  823. EOD,
  824. 'oracle' => <<<EOD
  825. SELECT "user_name"
  826. FROM "account"
  827. OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY
  828. EOD,
  829. ], $this->database->queryString);
  830. }
  831. /**
  832. * @covers ::select()
  833. * @covers ::dataImplode()
  834. * @covers ::whereClause()
  835. * @dataProvider typesProvider
  836. */
  837. public function testLimitOffsetWhere($type)
  838. {
  839. $this->setType($type);
  840. $this->database->select("account", "user_name", [
  841. 'LIMIT' => [20, 100]
  842. ]);
  843. $this->assertQuery([
  844. 'default' => <<<EOD
  845. SELECT "user_name"
  846. FROM "account"
  847. LIMIT 100 OFFSET 20
  848. EOD,
  849. 'mssql' => <<<EOD
  850. SELECT [user_name]
  851. FROM [account]
  852. ORDER BY (SELECT 0)
  853. OFFSET 20 ROWS FETCH NEXT 100 ROWS ONLY
  854. EOD,
  855. 'oracle' => <<<EOD
  856. SELECT "user_name"
  857. FROM "account"
  858. OFFSET 20 ROWS FETCH NEXT 100 ROWS ONLY
  859. EOD,
  860. ], $this->database->queryString);
  861. }
  862. /**
  863. * @covers ::select()
  864. * @covers ::dataImplode()
  865. * @covers ::whereClause()
  866. * @dataProvider typesProvider
  867. */
  868. public function testGroupWhere($type)
  869. {
  870. $this->setType($type);
  871. $this->database->select("account", "user_name", [
  872. 'GROUP' => 'type',
  873. ]);
  874. $this->assertQuery(
  875. <<<EOD
  876. SELECT "user_name"
  877. FROM "account"
  878. GROUP BY "type"
  879. EOD,
  880. $this->database->queryString
  881. );
  882. }
  883. /**
  884. * @covers ::select()
  885. * @covers ::dataImplode()
  886. * @covers ::whereClause()
  887. * @dataProvider typesProvider
  888. */
  889. public function testGroupWithArrayWhere($type)
  890. {
  891. $this->setType($type);
  892. $this->database->select("account", "user_name", [
  893. 'GROUP' => [
  894. 'type',
  895. 'age',
  896. 'gender'
  897. ]
  898. ]);
  899. $this->assertQuery(
  900. <<<EOD
  901. SELECT "user_name"
  902. FROM "account"
  903. GROUP BY "type","age","gender"
  904. EOD,
  905. $this->database->queryString
  906. );
  907. }
  908. /**
  909. * @covers ::select()
  910. * @covers ::dataImplode()
  911. * @covers ::whereClause()
  912. * @dataProvider typesProvider
  913. */
  914. public function testGroupWithRawWhere($type)
  915. {
  916. $this->setType($type);
  917. $this->database->select("account", "user_name", [
  918. 'GROUP' => Medoo::raw("<location>, <gender>")
  919. ]);
  920. $this->assertQuery(
  921. <<<EOD
  922. SELECT "user_name"
  923. FROM "account"
  924. GROUP BY "location", "gender"
  925. EOD,
  926. $this->database->queryString
  927. );
  928. }
  929. /**
  930. * @covers ::select()
  931. * @covers ::dataImplode()
  932. * @covers ::whereClause()
  933. * @dataProvider typesProvider
  934. */
  935. public function testHavingWhere($type)
  936. {
  937. $this->setType($type);
  938. $this->database->select("account", "user_name", [
  939. 'HAVING' => [
  940. 'user_id[>]' => 500
  941. ]
  942. ]);
  943. $this->assertQuery(
  944. <<<EOD
  945. SELECT "user_name"
  946. FROM "account"
  947. HAVING "user_id" > 500
  948. EOD,
  949. $this->database->queryString
  950. );
  951. }
  952. /**
  953. * @covers ::select()
  954. * @covers ::dataImplode()
  955. * @covers ::whereClause()
  956. * @dataProvider typesProvider
  957. */
  958. public function testHavingWithRawWhere($type)
  959. {
  960. $this->setType($type);
  961. $this->database->select("account", "user_name", [
  962. 'HAVING' => Medoo::raw('<location> = LOWER("NEW YORK")')
  963. ]);
  964. $this->assertQuery(
  965. <<<EOD
  966. SELECT "user_name"
  967. FROM "account"
  968. HAVING "location" = LOWER("NEW YORK")
  969. EOD,
  970. $this->database->queryString
  971. );
  972. }
  973. /**
  974. * @covers ::select()
  975. * @covers ::dataImplode()
  976. * @covers ::whereClause()
  977. * @dataProvider typesProvider
  978. */
  979. public function testHavingWithAggregateRawWhere($type)
  980. {
  981. $this->setType($type);
  982. $this->database->select("account", [
  983. "total" => Medoo::raw('SUM(<salary>)')
  984. ], [
  985. 'HAVING' => Medoo::raw('SUM(<salary>) > 1000')
  986. ]);
  987. $this->assertQuery(
  988. <<<EOD
  989. SELECT SUM("salary") AS "total"
  990. FROM "account"
  991. HAVING SUM("salary") > 1000
  992. EOD,
  993. $this->database->queryString
  994. );
  995. }
  996. /**
  997. * @covers ::select()
  998. * @covers ::dataImplode()
  999. * @covers ::whereClause()
  1000. * @dataProvider typesProvider
  1001. */
  1002. public function testRawWhereClause($type)
  1003. {
  1004. $this->setType($type);
  1005. $this->database->select(
  1006. "account",
  1007. "user_name",
  1008. Medoo::raw("WHERE <id> => 10")
  1009. );
  1010. $this->assertQuery(
  1011. <<<EOD
  1012. SELECT "user_name"
  1013. FROM "account"
  1014. WHERE "id" => 10
  1015. EOD,
  1016. $this->database->queryString
  1017. );
  1018. }
  1019. /**
  1020. * @covers ::select()
  1021. * @covers ::dataImplode()
  1022. * @covers ::whereClause()
  1023. * @dataProvider typesProvider
  1024. */
  1025. public function testRawWhereWithJoinClause($type)
  1026. {
  1027. $this->setType($type);
  1028. $this->database->select(
  1029. "post",
  1030. [
  1031. "[>]account" => "user_id",
  1032. ],
  1033. [
  1034. "post.content"
  1035. ],
  1036. Medoo::raw("WHERE <id> => 10")
  1037. );
  1038. $this->assertQuery(
  1039. <<<EOD
  1040. SELECT "post"."content"
  1041. FROM "post"
  1042. LEFT JOIN "account" USING ("user_id")
  1043. WHERE "id" => 10
  1044. EOD,
  1045. $this->database->queryString
  1046. );
  1047. }
  1048. }