查询语言.bind

Testing Is Documentation

tests/Database/Query/BindTest.php查询语言.bind - 图1

Uses

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

命名参数绑定

  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` = :id",
  7. {
  8. "id": [
  9. 1
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->table('test_query')
  20. ->bind('id', 1)
  21. ->where('id', '=', Condition::raw(':id'))
  22. ->findAll(true)
  23. )
  24. );
  25. }

命名参数绑定,支持绑定类型

  1. public function testBindWithType(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :id",
  7. {
  8. "id": [
  9. 1,
  10. 1
  11. ]
  12. },
  13. false
  14. ]
  15. eot;
  16. $this->assertSame(
  17. $sql,
  18. $this->varJson(
  19. $connect
  20. ->table('test_query')
  21. ->bind('id', 1, PDO::PARAM_INT)
  22. ->where('id', '=', Condition::raw(':id'))
  23. ->findAll(true),
  24. 1
  25. )
  26. );
  27. }

命名参数绑定,绑定值支持类型定义

  1. public function testWithTypeAndValueCanBeArray(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :id",
  7. {
  8. "id": [
  9. 1,
  10. 1
  11. ]
  12. },
  13. false
  14. ]
  15. eot;
  16. $this->assertSame(
  17. $sql,
  18. $this->varJson(
  19. $connect
  20. ->table('test_query')
  21. ->bind('id', [1, PDO::PARAM_INT])
  22. ->where('id', '=', Condition::raw(':id'))
  23. ->findAll(true),
  24. 2
  25. )
  26. );
  27. }

命名参数绑定,支持多个字段绑定

  1. public function testNameBind(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = :id AND `test_query`.`hello` LIKE :name",
  7. {
  8. "id": [
  9. 1,
  10. 1
  11. ],
  12. "name": [
  13. "小鸭子"
  14. ]
  15. },
  16. false
  17. ]
  18. eot;
  19. $this->assertSame(
  20. $sql,
  21. $this->varJson(
  22. $connect
  23. ->table('test_query')
  24. ->bind(['id' => [1, PDO::PARAM_INT], 'name'=>'小鸭子'])
  25. ->where('id', '=', Condition::raw(':id'))
  26. ->where('hello', 'like', Condition::raw(':name'))
  27. ->findAll(true),
  28. 3
  29. )
  30. );
  31. }

问号 ? 参数绑定,支持多个字段绑定

  1. public function testQuestionMarkBind(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` WHERE `test_query`.`id` = ? AND `test_query`.`hello` LIKE ?",
  7. [
  8. [
  9. 5,
  10. 1
  11. ],
  12. [
  13. "小鸭子"
  14. ]
  15. ],
  16. false
  17. ]
  18. eot;
  19. $this->assertSame(
  20. $sql,
  21. $this->varJson(
  22. $connect
  23. ->table('test_query')
  24. ->bind([[5, PDO::PARAM_INT], '小鸭子'])
  25. ->where('id', '=', Condition::raw('?'))
  26. ->where('hello', 'like', Condition::raw('?'))
  27. ->findAll(true),
  28. 4
  29. )
  30. );
  31. }