QueryBuilderTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace EasySwoole\Mysqli\Tests;
  3. use EasySwoole\Mysqli\QueryBuilder;
  4. use PHPUnit\Framework\TestCase;
  5. class QueryBuilderTest extends TestCase
  6. {
  7. protected $builder;
  8. public function __construct($name = null, array $data = [], $dataName = '')
  9. {
  10. $this->builder = new QueryBuilder();
  11. parent::__construct($name, $data, $dataName);
  12. }
  13. function testGet()
  14. {
  15. $this->builder->get('get');
  16. $this->assertEquals('SELECT * FROM `get`',$this->builder->getLastPrepareQuery());
  17. $this->assertEquals('SELECT * FROM `get`',$this->builder->getLastQuery());
  18. $this->assertEquals([],$this->builder->getLastBindParams());
  19. $this->builder->get('get',1);
  20. $this->assertEquals('SELECT * FROM `get` LIMIT 1',$this->builder->getLastPrepareQuery());
  21. $this->assertEquals('SELECT * FROM `get` LIMIT 1',$this->builder->getLastQuery());
  22. $this->assertEquals([],$this->builder->getLastBindParams());
  23. $this->builder->get('get',[2,10]);
  24. $this->assertEquals('SELECT * FROM `get` LIMIT 2, 10',$this->builder->getLastPrepareQuery());
  25. $this->assertEquals('SELECT * FROM `get` LIMIT 2, 10',$this->builder->getLastQuery());
  26. $this->assertEquals([],$this->builder->getLastBindParams());
  27. $this->builder->get('get',null,['col1','col2']);
  28. $this->assertEquals('SELECT col1, col2 FROM `get`',$this->builder->getLastPrepareQuery());
  29. $this->assertEquals('SELECT col1, col2 FROM `get`',$this->builder->getLastQuery());
  30. $this->assertEquals([],$this->builder->getLastBindParams());
  31. $this->builder->get('get',1,['col1','col2']);
  32. $this->assertEquals('SELECT col1, col2 FROM `get` LIMIT 1',$this->builder->getLastPrepareQuery());
  33. $this->assertEquals('SELECT col1, col2 FROM `get` LIMIT 1',$this->builder->getLastQuery());
  34. $this->assertEquals([],$this->builder->getLastBindParams());
  35. $this->builder->get('get',[2,10],['col1','col2']);
  36. $this->assertEquals('SELECT col1, col2 FROM `get` LIMIT 2, 10',$this->builder->getLastPrepareQuery());
  37. $this->assertEquals('SELECT col1, col2 FROM `get` LIMIT 2, 10',$this->builder->getLastQuery());
  38. $this->assertEquals([],$this->builder->getLastBindParams());
  39. $this->builder->get('get',[2,10],['distinct col1','col2']);
  40. $this->assertEquals('SELECT distinct col1, col2 FROM `get` LIMIT 2, 10',$this->builder->getLastPrepareQuery());
  41. $this->assertEquals('SELECT distinct col1, col2 FROM `get` LIMIT 2, 10',$this->builder->getLastQuery());
  42. $this->assertEquals([],$this->builder->getLastBindParams());
  43. }
  44. function testGetOne()
  45. {
  46. $this->builder->getOne('get');
  47. $this->assertEquals('SELECT * FROM `get` LIMIT 1',$this->builder->getLastPrepareQuery());
  48. $this->assertEquals('SELECT * FROM `get` LIMIT 1',$this->builder->getLastQuery());
  49. $this->assertEquals([],$this->builder->getLastBindParams());
  50. }
  51. function testGetColumn()
  52. {
  53. $this->builder->getColumn('get');
  54. $this->assertEquals('SELECT * FROM `get`',$this->builder->getLastPrepareQuery());
  55. $this->assertEquals('SELECT * FROM `get`',$this->builder->getLastQuery());
  56. $this->assertEquals([],$this->builder->getLastBindParams());
  57. $this->builder->fields('testcolumn')->getColumn('get');
  58. $this->assertEquals('SELECT testcolumn FROM `get`',$this->builder->getLastPrepareQuery());
  59. $this->assertEquals('SELECT testcolumn FROM `get`',$this->builder->getLastQuery());
  60. $this->assertEquals([],$this->builder->getLastBindParams());
  61. $this->builder->fields('testcolumn1, testcolumn2')->getColumn('get');
  62. $this->assertEquals('SELECT testcolumn1 FROM `get`',$this->builder->getLastPrepareQuery());
  63. $this->assertEquals('SELECT testcolumn1 FROM `get`',$this->builder->getLastQuery());
  64. $this->assertEquals([],$this->builder->getLastBindParams());
  65. $this->builder->fields(['testcolumn1', 'testcolumn2'])->getColumn('get');
  66. $this->assertEquals('SELECT testcolumn1 FROM `get`',$this->builder->getLastPrepareQuery());
  67. $this->assertEquals('SELECT testcolumn1 FROM `get`',$this->builder->getLastQuery());
  68. $this->assertEquals([],$this->builder->getLastBindParams());
  69. $this->builder->getColumn('get', 'testcolumn');
  70. $this->assertEquals('SELECT testcolumn FROM `get`',$this->builder->getLastPrepareQuery());
  71. $this->assertEquals('SELECT testcolumn FROM `get`',$this->builder->getLastQuery());
  72. $this->assertEquals([],$this->builder->getLastBindParams());
  73. $this->builder->getColumn('get', 'testcolumn', 1);
  74. $this->assertEquals('SELECT testcolumn FROM `get` LIMIT 1',$this->builder->getLastPrepareQuery());
  75. $this->assertEquals('SELECT testcolumn FROM `get` LIMIT 1',$this->builder->getLastQuery());
  76. $this->assertEquals([],$this->builder->getLastBindParams());
  77. $this->builder->getColumn('get', 'testcolumn', [0, 10]);
  78. $this->assertEquals('SELECT testcolumn FROM `get` LIMIT 0, 10',$this->builder->getLastPrepareQuery());
  79. $this->assertEquals('SELECT testcolumn FROM `get` LIMIT 0, 10',$this->builder->getLastQuery());
  80. $this->assertEquals([],$this->builder->getLastBindParams());
  81. }
  82. function testGetScalar()
  83. {
  84. $this->builder->getScalar('get', 'testscalar');
  85. $this->assertEquals('SELECT testscalar FROM `get` LIMIT 1',$this->builder->getLastPrepareQuery());
  86. $this->assertEquals('SELECT testscalar FROM `get` LIMIT 1',$this->builder->getLastQuery());
  87. $this->assertEquals([],$this->builder->getLastBindParams());
  88. $this->builder->fields('testscalar')->getScalar('get');
  89. $this->assertEquals('SELECT testscalar FROM `get` LIMIT 1',$this->builder->getLastPrepareQuery());
  90. $this->assertEquals('SELECT testscalar FROM `get` LIMIT 1',$this->builder->getLastQuery());
  91. $this->assertEquals([],$this->builder->getLastBindParams());
  92. $this->builder->fields('testcolumn1, testcolumn2')->getScalar('get');
  93. $this->assertEquals('SELECT testcolumn1 FROM `get` LIMIT 1',$this->builder->getLastPrepareQuery());
  94. $this->assertEquals('SELECT testcolumn1 FROM `get` LIMIT 1',$this->builder->getLastQuery());
  95. $this->assertEquals([],$this->builder->getLastBindParams());
  96. $this->builder->fields(['testcolumn1', 'testcolumn2'])->getScalar('get');
  97. $this->assertEquals('SELECT testcolumn1 FROM `get` LIMIT 1',$this->builder->getLastPrepareQuery());
  98. $this->assertEquals('SELECT testcolumn1 FROM `get` LIMIT 1',$this->builder->getLastQuery());
  99. $this->assertEquals([],$this->builder->getLastBindParams());
  100. }
  101. function testWhereGet()
  102. {
  103. $this->builder->where('col1',2)->get('whereGet');
  104. $this->assertEquals('SELECT * FROM `whereGet` WHERE `col1` = ? ',$this->builder->getLastPrepareQuery());
  105. $this->assertEquals("SELECT * FROM `whereGet` WHERE `col1` = 2 ",$this->builder->getLastQuery());
  106. $this->assertEquals([2],$this->builder->getLastBindParams());
  107. $this->builder->where('col1',2,">")->get('whereGet');
  108. $this->assertEquals('SELECT * FROM `whereGet` WHERE `col1` > ? ',$this->builder->getLastPrepareQuery());
  109. $this->assertEquals("SELECT * FROM `whereGet` WHERE `col1` > 2 ",$this->builder->getLastQuery());
  110. $this->assertEquals([2],$this->builder->getLastBindParams());
  111. $this->builder->where('col1',2)->where('col2','str')->get('whereGet');
  112. $this->assertEquals('SELECT * FROM `whereGet` WHERE `col1` = ? AND `col2` = ? ',$this->builder->getLastPrepareQuery());
  113. $this->assertEquals("SELECT * FROM `whereGet` WHERE `col1` = 2 AND `col2` = 'str' ",$this->builder->getLastQuery());
  114. $this->assertEquals([2,'str'],$this->builder->getLastBindParams());
  115. $this->builder->where('col3',[1,2,3],'IN')->get('whereGet');
  116. $this->assertEquals('SELECT * FROM `whereGet` WHERE `col3` IN ( ?, ?, ? ) ',$this->builder->getLastPrepareQuery());
  117. $this->assertEquals('SELECT * FROM `whereGet` WHERE `col3` IN ( 1, 2, 3 ) ',$this->builder->getLastQuery());
  118. $this->assertEquals([1,2,3],$this->builder->getLastBindParams());
  119. $this->builder->where("find_in_set(1, test)")->get('whereGet');
  120. $this->assertEquals('SELECT * FROM `whereGet` WHERE find_in_set(1, test)', $this->builder->getLastPrepareQuery());
  121. $this->builder->where("find_in_set(?, test)", [1])->get('whereGet');
  122. $this->assertEquals('SELECT * FROM `whereGet` WHERE find_in_set(1, test)', $this->builder->getLastQuery());
  123. $this->builder->where("(id = ? or id = ?)", [1,3])->get('whereGet');
  124. $this->assertEquals('SELECT * FROM `whereGet` WHERE (id = 1 or id = 3)', $this->builder->getLastQuery());
  125. }
  126. function testJoinGet()
  127. {
  128. $this->builder->join('table2','table2.col1 = getTable.col2')->get('getTable');
  129. $this->assertEquals('SELECT * FROM `getTable` JOIN table2 on table2.col1 = getTable.col2',$this->builder->getLastPrepareQuery());
  130. $this->assertEquals('SELECT * FROM `getTable` JOIN table2 on table2.col1 = getTable.col2',$this->builder->getLastQuery());
  131. $this->assertEquals([],$this->builder->getLastBindParams());
  132. $this->builder->join('table2','table2.col1 = getTable.col2','LEFT')->get('getTable');
  133. $this->assertEquals('SELECT * FROM `getTable` LEFT JOIN table2 on table2.col1 = getTable.col2',$this->builder->getLastPrepareQuery());
  134. $this->assertEquals('SELECT * FROM `getTable` LEFT JOIN table2 on table2.col1 = getTable.col2',$this->builder->getLastQuery());
  135. $this->assertEquals([],$this->builder->getLastBindParams());
  136. }
  137. function testGroup()
  138. {
  139. $this->builder->groupBy("user_id")->get("test_table");
  140. $this->assertEquals('SELECT * FROM `test_table` GROUP BY user_id ',$this->builder->getLastPrepareQuery());
  141. $this->builder->groupBy("FROM_UNIXTIME(create_time, '%Y%m')")->get("test_table");
  142. $this->assertEquals('SELECT * FROM `test_table` GROUP BY FROM_UNIXTIME(create_time, \'%Y%m\') ',$this->builder->getLastPrepareQuery());
  143. $this->builder->fields([
  144. "DATE_FORMAT(create_time, '%Y%m') AS month",
  145. "sum(age)"
  146. ])->groupBy("month")->get("test_table");
  147. $this->assertEquals('SELECT DATE_FORMAT(create_time, \'%Y%m\') AS month, sum(age) FROM `test_table` GROUP BY month ',$this->builder->getLastPrepareQuery());
  148. }
  149. function testJoinWhereGet()
  150. {
  151. $this->builder->join('table2','table2.col1 = getTable.col2')->where('table2.col1',2)->get('getTable');
  152. $this->assertEquals('SELECT * FROM `getTable` JOIN table2 on table2.col1 = getTable.col2 WHERE `table2`.`col1` = ? ',$this->builder->getLastPrepareQuery());
  153. $this->assertEquals('SELECT * FROM `getTable` JOIN table2 on table2.col1 = getTable.col2 WHERE `table2`.`col1` = 2 ',$this->builder->getLastQuery());
  154. $this->assertEquals([2],$this->builder->getLastBindParams());
  155. }
  156. function testUpdate()
  157. {
  158. $this->builder->update('updateTable', ['a' => 1]);
  159. $this->assertEquals('UPDATE `updateTable` SET `a` = ?', $this->builder->getLastPrepareQuery());
  160. $this->assertEquals('UPDATE `updateTable` SET `a` = 1', $this->builder->getLastQuery());
  161. $this->assertEquals([1], $this->builder->getLastBindParams());
  162. }
  163. function testLimitUpdate()
  164. {
  165. $this->builder->update('updateTable', ['a' => 1], 5);
  166. $this->assertEquals('UPDATE `updateTable` SET `a` = ? LIMIT 5', $this->builder->getLastPrepareQuery());
  167. $this->assertEquals('UPDATE `updateTable` SET `a` = 1 LIMIT 5', $this->builder->getLastQuery());
  168. $this->assertEquals([1], $this->builder->getLastBindParams());
  169. }
  170. function testWhereUpdate()
  171. {
  172. $this->builder->where('whereUpdate', 'whereValue')->update('updateTable', ['a' => 1]);
  173. $this->assertEquals('UPDATE `updateTable` SET `a` = ? WHERE `whereUpdate` = ? ', $this->builder->getLastPrepareQuery());
  174. $this->assertEquals("UPDATE `updateTable` SET `a` = 1 WHERE `whereUpdate` = 'whereValue' ", $this->builder->getLastQuery());
  175. $this->assertEquals([1, 'whereValue'], $this->builder->getLastBindParams());
  176. }
  177. /**
  178. * @throws \Exception
  179. */
  180. function testLockWhereLimitUpdate()
  181. {
  182. $this->builder->setQueryOption("FOR UPDATE")->where('whereUpdate', 'whereValue')->update('updateTable', ['a' => 1], 2);
  183. $this->assertEquals('UPDATE `updateTable` SET `a` = ? WHERE `whereUpdate` = ? LIMIT 2 FOR UPDATE', $this->builder->getLastPrepareQuery());
  184. $this->assertEquals("UPDATE `updateTable` SET `a` = 1 WHERE `whereUpdate` = 'whereValue' LIMIT 2 FOR UPDATE", $this->builder->getLastQuery());
  185. $this->assertEquals([1, 'whereValue'], $this->builder->getLastBindParams());
  186. }
  187. function testForUpdate()
  188. {
  189. $this->builder->selectForUpdate(true)->where('name',1)->get('test');
  190. $this->assertEquals('SELECT * FROM `test` WHERE `name` = ? FOR UPDATE', $this->builder->getLastPrepareQuery());
  191. $this->assertEquals('SELECT * FROM `test` WHERE `name` = 1 FOR UPDATE', $this->builder->getLastQuery());
  192. $this->builder->selectForUpdate(true, 'NOWAIT')->where('name',1)->get('test');
  193. $this->assertEquals('SELECT * FROM `test` WHERE `name` = ? FOR UPDATE NOWAIT', $this->builder->getLastPrepareQuery());
  194. $this->assertEquals('SELECT * FROM `test` WHERE `name` = 1 FOR UPDATE NOWAIT', $this->builder->getLastQuery());
  195. $this->builder->selectForUpdate(true, 'WAIT 5')->where('name',1)->get('test');
  196. $this->assertEquals('SELECT * FROM `test` WHERE `name` = ? FOR UPDATE WAIT 5', $this->builder->getLastPrepareQuery());
  197. $this->assertEquals('SELECT * FROM `test` WHERE `name` = 1 FOR UPDATE WAIT 5', $this->builder->getLastQuery());
  198. $this->builder->selectForUpdate(true, 'SKIP LOCKED')->where('name',1)->get('test');
  199. $this->assertEquals('SELECT * FROM `test` WHERE `name` = ? FOR UPDATE SKIP LOCKED', $this->builder->getLastPrepareQuery());
  200. $this->assertEquals('SELECT * FROM `test` WHERE `name` = 1 FOR UPDATE SKIP LOCKED', $this->builder->getLastQuery());
  201. }
  202. function testDelete()
  203. {
  204. $this->builder->delete('deleteTable');
  205. $this->assertEquals('DELETE FROM `deleteTable`', $this->builder->getLastPrepareQuery());
  206. $this->assertEquals('DELETE FROM `deleteTable`', $this->builder->getLastQuery());
  207. $this->assertEquals([], $this->builder->getLastBindParams());
  208. }
  209. function testLimitDelete()
  210. {
  211. $this->builder->delete('deleteTable', 1);
  212. $this->assertEquals('DELETE FROM `deleteTable` LIMIT 1', $this->builder->getLastPrepareQuery());
  213. $this->assertEquals('DELETE FROM `deleteTable` LIMIT 1', $this->builder->getLastQuery());
  214. $this->assertEquals([], $this->builder->getLastBindParams());
  215. }
  216. function testWhereDelete()
  217. {
  218. $this->builder->where('whereDelete', 'whereValue')->delete('deleteTable');
  219. $this->assertEquals('DELETE FROM `deleteTable` WHERE `whereDelete` = ? ', $this->builder->getLastPrepareQuery());
  220. $this->assertEquals("DELETE FROM `deleteTable` WHERE `whereDelete` = 'whereValue' ", $this->builder->getLastQuery());
  221. $this->assertEquals(['whereValue'], $this->builder->getLastBindParams());
  222. }
  223. function testInsert()
  224. {
  225. $this->builder->insert('insertTable', ['a' => 1, 'b' => "b"]);
  226. $this->assertEquals('INSERT INTO `insertTable` (`a`, `b`) VALUES (?, ?)', $this->builder->getLastPrepareQuery());
  227. $this->assertEquals("INSERT INTO `insertTable` (`a`, `b`) VALUES (1, 'b')", $this->builder->getLastQuery());
  228. $this->assertEquals([1,'b'], $this->builder->getLastBindParams());
  229. $this->builder->insert('insertTable', ['a' => "Don't worry"]);
  230. $this->assertEquals('INSERT INTO `insertTable` (`a`) VALUES (?)', $this->builder->getLastPrepareQuery());
  231. $this->assertEquals("INSERT INTO `insertTable` (`a`) VALUES ('Don\'t worry')", $this->builder->getLastQuery());
  232. $this->assertEquals(["Don't worry"], $this->builder->getLastBindParams());
  233. }
  234. function testInsertAll()
  235. {
  236. $this->builder->insertAll('insertTable', [
  237. ['a' => 1, 'b' => "a"],
  238. ['a' => 2, 'b' => "b"],
  239. ]);
  240. $this->assertEquals('INSERT INTO `insertTable` (`a`, `b`) VALUES (?, ?),(?, ?)', $this->builder->getLastPrepareQuery());
  241. $this->assertEquals("INSERT INTO `insertTable` (`a`, `b`) VALUES (1, 'a'),(2, 'b')", $this->builder->getLastQuery());
  242. }
  243. function testSubQuery()
  244. {
  245. $sub = $this->builder::subQuery();
  246. $sub->where ("qty", 2, ">");
  247. $sub->get ("products", null, "userId");
  248. $this->builder->where ("id", $sub, 'in')->get('users');
  249. $this->assertEquals('SELECT * FROM `users` WHERE `id` in ( (SELECT userId FROM `products` WHERE `qty` > ? ) ) ',$this->builder->getLastPrepareQuery());
  250. $this->assertEquals('SELECT * FROM `users` WHERE `id` in ( (SELECT userId FROM `products` WHERE `qty` > 2 ) ) ',$this->builder->getLastQuery());
  251. $this->assertEquals([2],$this->builder->getLastBindParams());
  252. $sub = $this->builder::subQuery();
  253. $sub->where ("qty", 2, ">");
  254. $sub->get ("products", null, "userId");
  255. $this->builder->where('col2',1)->where ("id", $sub, 'in')->get('users');
  256. $this->assertEquals('SELECT * FROM `users` WHERE `col2` = ? AND `id` in ( (SELECT userId FROM `products` WHERE `qty` > ? ) ) ',$this->builder->getLastPrepareQuery());
  257. $this->assertEquals('SELECT * FROM `users` WHERE `col2` = 1 AND `id` in ( (SELECT userId FROM `products` WHERE `qty` > 2 ) ) ',$this->builder->getLastQuery());
  258. $this->assertEquals([1,2],$this->builder->getLastBindParams());
  259. $userIdQ = $this->builder::subQuery();
  260. $userIdQ->where ("id", 6);
  261. $userIdQ->getOne ("users", "name");
  262. $data = Array (
  263. "productName" => "test product",
  264. "userId" => $userIdQ,
  265. "lastUpdated" => $this->builder->now()
  266. );
  267. $this->builder->insert ("products", $data);
  268. $this->assertEquals('INSERT INTO `products` (`productName`, `userId`, `lastUpdated`) VALUES (?, (SELECT name FROM `users` WHERE `id` = ? LIMIT 1) , NOW())',$this->builder->getLastPrepareQuery());
  269. $this->assertEquals("INSERT INTO `products` (`productName`, `userId`, `lastUpdated`) VALUES ('test product', (SELECT name FROM `users` WHERE `id` = 6 LIMIT 1) , NOW())",$this->builder->getLastQuery());
  270. $this->assertEquals(["test product",6],$this->builder->getLastBindParams());
  271. $usersQ = $this->builder::subQuery ("u");
  272. $usersQ->where ("active", 1);
  273. $usersQ->get ("users");
  274. $this->builder->join($usersQ, "p.userId=u.id", "LEFT");
  275. $this->builder->get ("products p", null, "u.login, p.productName");
  276. $this->assertEquals('SELECT u.login, p.productName FROM products p LEFT JOIN (SELECT * FROM `users` WHERE `active` = ? ) u on p.userId=u.id',$this->builder->getLastPrepareQuery());
  277. $this->assertEquals('SELECT u.login, p.productName FROM products p LEFT JOIN (SELECT * FROM `users` WHERE `active` = 1 ) u on p.userId=u.id',$this->builder->getLastQuery());
  278. $this->assertEquals([1],$this->builder->getLastBindParams());
  279. }
  280. public function testUnion()
  281. {
  282. $this->builder->union((new QueryBuilder)->where('userName', 'user')->get('user'))->where('adminUserName', 'admin')->get('admin');
  283. $this->assertEquals('SELECT * FROM `admin` WHERE `adminUserName` = ? UNION SELECT * FROM `user` WHERE `userName` = ? ', $this->builder->getLastPrepareQuery());
  284. $this->assertEquals("SELECT * FROM `admin` WHERE `adminUserName` = 'admin' UNION SELECT * FROM `user` WHERE `userName` = 'user' ", $this->builder->getLastQuery());
  285. $this->assertEquals(['admin','user'],$this->builder->getLastBindParams());
  286. }
  287. public function testRaw()
  288. {
  289. $this->builder->raw("SELECT * FROM `siam` ");
  290. $this->assertEquals("SELECT * FROM `siam` ", $this->builder->getLastQuery());
  291. $this->builder->raw("SELECT * FROM `siam` WHERE `siam_name` = ? AND `age` = ?", ['siam', 321]);
  292. $this->assertEquals("SELECT * FROM `siam` WHERE `siam_name` = 'siam' AND `age` = 321", $this->builder->getLastQuery());
  293. }
  294. }