查询语言.table

Testing Is Documentation

tests/Database/Query/TableTest.php查询语言.table - 图1

Uses

  1. <?php
  2. use Leevel\Database\Condition;
  3. use stdClass;
  4. use Tests\Database\DatabaseTestCase as TestCase;

Table 查询数据库表

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query`",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->findAll(true)
  17. )
  18. );
  19. }

Table 查询指定数据库的表

  1. public function testWithDatabaseName(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test`.`test_query`",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test.test_query')
  16. ->findAll(true),
  17. 1
  18. )
  19. );
  20. }

Table 查询数据库表,表支持别名

  1. public function testWithAlias(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `p`.* FROM `test`.`test_query` `p`",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table(['p' => 'test.test_query'])
  16. ->findAll(true),
  17. 2
  18. )
  19. );
  20. }

Table 查询数据库表指定字段

  1. public function testField(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.`title`,`test_query`.`body` FROM `test_query`",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query', 'title,body')
  16. ->findAll(true)
  17. )
  18. );
  19. }

Table 查询数据库表指定字段,字段支持别名

  1. public function testWithFieldAlias(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.`title` AS `t`,`test_query`.`name`,`test_query`.`remark`,`test_query`.`value` FROM `test`.`test_query`",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test.test_query', [
  16. 't' => 'title', 'name', 'remark,value',
  17. ])
  18. ->findAll(true),
  19. 1
  20. )
  21. );
  22. }

Table 查询数据库表支持子查询

  1. public function testSub(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $subSql = $connect->table('test_query')->makeSql(true);
  5. $sql = <<<'eot'
  6. [
  7. "SELECT `a`.* FROM (SELECT `test_query`.* FROM `test_query`) a",
  8. [],
  9. false
  10. ]
  11. eot;
  12. $this->assertSame(
  13. $sql,
  14. $this->varJson(
  15. $connect
  16. ->table($subSql.' as a')
  17. ->findAll(true)
  18. )
  19. );
  20. }

Table 查询数据库表支持子查询,子查询可以为数据库查询对象

  1. public function testSubIsSelect(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $subSql = $connect->table('test_query');
  5. $sql = <<<'eot'
  6. [
  7. "SELECT `bb`.* FROM (SELECT `test_query`.* FROM `test_query`) bb",
  8. [],
  9. false
  10. ]
  11. eot;
  12. $this->assertSame(
  13. $sql,
  14. $this->varJson(
  15. $connect
  16. ->table(['bb' => $subSql])
  17. ->findAll(true)
  18. )
  19. );
  20. }

Table 查询数据库表支持子查询,子查询可以为数据库条件对象

  1. public function testSubIsCondition(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $subSql = $connect->table('test_query')->databaseCondition();
  5. $sql = <<<'eot'
  6. [
  7. "SELECT `bb`.* FROM (SELECT `test_query`.* FROM `test_query`) bb",
  8. [],
  9. false
  10. ]
  11. eot;
  12. $this->assertSame(
  13. $sql,
  14. $this->varJson(
  15. $connect
  16. ->table(['bb' => $subSql])
  17. ->findAll(true)
  18. )
  19. );
  20. }

Table 查询数据库表支持子查询,子查询可以为闭包

  1. public function testSubIsClosure(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `b`.* FROM (SELECT `test_query`.* FROM `test_query`) b",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table(['b'=> function ($select) {
  16. $select->table('test_query');
  17. }])
  18. ->findAll(true)
  19. )
  20. );
  21. }

Table 查询数据库表支持子查询,子查询可以为闭包,未指定别名默认为自身

  1. public function testSubIsClosureWithItSeltAsAlias(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `guest_book`.* FROM (SELECT `guest_book`.* FROM `guest_book`) guest_book",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table(function ($select) {
  16. $select->table('guest_book');
  17. })
  18. ->findAll(true)
  19. )
  20. );
  21. }

Table 查询数据库表支持子查询,子查询可以为闭包,还可以进行 join 查询

  1. public function testSubIsClosureWithJoin(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.`remark`,`test_query_subsql`.`name`,`test_query_subsql`.`value` FROM (SELECT `test_query`.* FROM `test_query`) test_query INNER JOIN `test_query_subsql` ON `test_query_subsql`.`name` = `test_query`.`name`",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table(function ($select) {
  16. $select->table('test_query');
  17. }, 'remark')
  18. ->join('test_query_subsql', 'name,value', 'name', '=', Condition::raw('[test_query.name]'))
  19. ->findAll(true)
  20. )
  21. );
  22. }