查询语言.flow

Testing Is Documentation

tests/Database/Query/FlowTest.php查询语言.flow - 图1

QueryPHP 数据构造器支持条件运算符,可以根据不同条件做不同的事情,支持所有的构造器函数,即返回 $this

Uses

  1. <?php
  2. use Tests\Database\DatabaseTestCase as TestCase;

limit 限制条数

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id ORDER BY `test_query`.`name` DESC LIMIT 1",
  7. {
  8. "test_query_id": [
  9. 2
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $id = 2;
  16. $this->assertSame(
  17. $sql,
  18. $this->varJson(
  19. $connect
  20. ->table('test_query')
  21. ->if(1 === $id)
  22. ->where('id', 1)
  23. ->elif(2 === $id)
  24. ->where('id', 2)
  25. ->orderBy('name DESC')
  26. ->elif(3 === $id)
  27. ->where('id', 3)
  28. ->where('id', 1111)
  29. ->elif(4 === $id)
  30. ->where('id', 4)
  31. ->fi()
  32. ->findOne(true)
  33. )
  34. );
  35. $sql = <<<'eot'
  36. [
  37. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id LIMIT 1",
  38. {
  39. "test_query_id": [
  40. 1
  41. ]
  42. },
  43. false
  44. ]
  45. eot;
  46. $id = 1;
  47. $this->assertSame(
  48. $sql,
  49. $this->varJson(
  50. $connect
  51. ->table('test_query')
  52. ->if(1 === $id)
  53. ->where('id', 1)
  54. ->elif(2 === $id)
  55. ->where('id', 2)
  56. ->orderBy('name DESC')
  57. ->elif(3 === $id)
  58. ->where('id', 3)
  59. ->where('id', 1111)
  60. ->elif(4 === $id)
  61. ->where('id', 4)
  62. ->fi()
  63. ->findOne(true),
  64. 1
  65. )
  66. );
  67. $sql = <<<'eot'
  68. [
  69. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id AND `test_query`.`id` = :test_query_id_1 LIMIT 1",
  70. {
  71. "test_query_id": [
  72. 3
  73. ],
  74. "test_query_id_1": [
  75. 1111
  76. ]
  77. },
  78. false
  79. ]
  80. eot;
  81. $id = 3;
  82. $this->assertSame(
  83. $sql,
  84. $this->varJson(
  85. $connect
  86. ->table('test_query')
  87. ->if(1 === $id)
  88. ->where('id', 1)
  89. ->elif(2 === $id)
  90. ->where('id', 2)
  91. ->orderBy('name DESC')
  92. ->elif(3 === $id)
  93. ->where('id', 3)
  94. ->where('id', 1111)
  95. ->elif(4 === $id)
  96. ->where('id', 4)
  97. ->fi()
  98. ->findOne(true),
  99. 2
  100. )
  101. );
  102. $sql = <<<'eot'
  103. [
  104. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id LIMIT 1",
  105. {
  106. "test_query_id": [
  107. 4
  108. ]
  109. },
  110. false
  111. ]
  112. eot;
  113. $id = 4;
  114. $this->assertSame(
  115. $sql,
  116. $this->varJson(
  117. $connect
  118. ->table('test_query')
  119. ->if(1 === $id)
  120. ->where('id', 1)
  121. ->elif(2 === $id)
  122. ->where('id', 2)
  123. ->orderBy('name DESC')
  124. ->elif(3 === $id)
  125. ->where('id', 3)
  126. ->where('id', 1111)
  127. ->elif(4 === $id)
  128. ->where('id', 4)
  129. ->fi()
  130. ->findOne(true),
  131. 3
  132. )
  133. );
  134. }

else 浅记忆

else 仅仅能记忆上一次 if,elif 的结果,上一次的反向结果就是 else 的条件值,我们建议不要在 SQL 链式中使用过度的条件判断。

  1. public function testElse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id AND `test_query`.`id` = :test_query_id_1 ORDER BY `test_query`.`name` DESC LIMIT 1",
  7. {
  8. "test_query_id": [
  9. 2
  10. ],
  11. "test_query_id_1": [
  12. 4
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $id = 2;
  19. $this->assertSame(
  20. $sql,
  21. $this->varJson(
  22. $connect
  23. ->table('test_query')
  24. ->if(1 === $id)
  25. ->where('id', 1)
  26. ->elif(2 === $id)
  27. ->where('id', 2)
  28. ->orderBy('name DESC')
  29. ->elif(3 === $id)
  30. ->where('id', 3)
  31. ->where('id', 1111)
  32. ->else() // else 仅仅能记忆上一次 if,elif 的结果,上一次的反向结果就是 else 的条件值,其等价于 elif($id != 3)
  33. ->where('id', 4)
  34. ->fi()
  35. ->findOne(true)
  36. )
  37. );
  38. $sql = <<<'eot'
  39. [
  40. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id AND `test_query`.`id` = :test_query_id_1 LIMIT 1",
  41. {
  42. "test_query_id": [
  43. 3
  44. ],
  45. "test_query_id_1": [
  46. 1111
  47. ]
  48. },
  49. false
  50. ]
  51. eot;
  52. $id = 3;
  53. $this->assertSame(
  54. $sql,
  55. $this->varJson(
  56. $connect
  57. ->table('test_query')
  58. ->if(1 === $id)
  59. ->where('id', 1)
  60. ->elif(2 === $id)
  61. ->where('id', 2)
  62. ->orderBy('name DESC')
  63. ->elif(3 === $id)
  64. ->where('id', 3)
  65. ->where('id', 1111)
  66. ->else() // else 仅仅能记忆上一次 if,elif 的结果,上一次的反向结果就是 else 的条件值,其等价于 elif($id != 3)
  67. ->where('id', 4)
  68. ->fi()
  69. ->findOne(true),
  70. 1
  71. )
  72. );
  73. $sql = <<<'eot'
  74. [
  75. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :test_query_id LIMIT 1",
  76. {
  77. "test_query_id": [
  78. 4
  79. ]
  80. },
  81. false
  82. ]
  83. eot;
  84. $id = 5;
  85. $this->assertSame(
  86. $sql,
  87. $this->varJson(
  88. $connect
  89. ->table('test_query')
  90. ->if(1 === $id)
  91. ->where('id', 1)
  92. ->elif(2 === $id)
  93. ->where('id', 2)
  94. ->orderBy('name DESC')
  95. ->elif(3 === $id)
  96. ->where('id', 3)
  97. ->where('id', 1111)
  98. ->else() // else 仅仅能记忆上一次 if,elif 的结果,上一次的反向结果就是 else 的条件值,其等价于 elif($id != 3)
  99. ->where('id', 4)
  100. ->fi()
  101. ->findOne(true),
  102. 2
  103. )
  104. );
  105. }

TIP

命令遵循 shell 命令风格,即 if,elif,else,fi。