批量写入数据.insertAll

Testing Is Documentation

tests/Database/Create/InsertAllTest.php批量写入数据.insertAll - 图1

Uses

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

insertAll 基本用法

写入成功后,返回 lastInsertId

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "INSERT INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:pdonamedparameter_value),(:pdonamedparameter_name_1,:pdonamedparameter_value_1),(:pdonamedparameter_name_2,:pdonamedparameter_value_2),(:pdonamedparameter_name_3,:pdonamedparameter_value_3)",
  7. {
  8. "pdonamedparameter_name": [
  9. "小鸭子1"
  10. ],
  11. "pdonamedparameter_value": [
  12. "呱呱呱1"
  13. ],
  14. "pdonamedparameter_name_1": [
  15. "小鸭子2"
  16. ],
  17. "pdonamedparameter_value_1": [
  18. "呱呱呱2"
  19. ],
  20. "pdonamedparameter_name_2": [
  21. "小鸭子3"
  22. ],
  23. "pdonamedparameter_value_2": [
  24. "呱呱呱3"
  25. ],
  26. "pdonamedparameter_name_3": [
  27. "小鸭子4"
  28. ],
  29. "pdonamedparameter_value_3": [
  30. "呱呱呱4"
  31. ]
  32. },
  33. false
  34. ]
  35. eot;
  36. $data = [
  37. ['name' => '小鸭子1', 'value' => '呱呱呱1'],
  38. ['name' => '小鸭子2', 'value' => '呱呱呱2'],
  39. ['name' => '小鸭子3', 'value' => '呱呱呱3'],
  40. ['name' => '小鸭子4', 'value' => '呱呱呱4'],
  41. ];
  42. $this->assertSame(
  43. $sql,
  44. $this->varJson(
  45. $connect
  46. ->sql()
  47. ->table('test_query')
  48. ->insertAll($data)
  49. )
  50. );
  51. }

insertAll 绑定参数

  1. public function testBind(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "INSERT INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:pdonamedparameter_value),(:pdonamedparameter_name_1,:pdopositional2namedparameter_0_1),(:pdonamedparameter_name_2,:pdonamedparameter_value_2),(:pdonamedparameter_name_3,:pdopositional2namedparameter_1_3)",
  7. {
  8. "pdonamedparameter_name": [
  9. "小鸭子1"
  10. ],
  11. "pdonamedparameter_value": [
  12. "呱呱呱1"
  13. ],
  14. "pdonamedparameter_name_1": [
  15. "小鸭子2"
  16. ],
  17. "pdopositional2namedparameter_0_1": [
  18. "吃肉1"
  19. ],
  20. "pdonamedparameter_name_2": [
  21. "小鸭子3"
  22. ],
  23. "pdonamedparameter_value_2": [
  24. "呱呱呱3"
  25. ],
  26. "pdonamedparameter_name_3": [
  27. "小鸭子4"
  28. ],
  29. "pdopositional2namedparameter_1_3": [
  30. "吃肉2"
  31. ]
  32. },
  33. false
  34. ]
  35. eot;
  36. $data = [
  37. ['name' => '小鸭子1', 'value' => '呱呱呱1'],
  38. ['name' => '小鸭子2', 'value' => Condition::raw('?')],
  39. ['name' => '小鸭子3', 'value' => '呱呱呱3'],
  40. ['name' => '小鸭子4', 'value' => Condition::raw('?')],
  41. ];
  42. $this->assertSame(
  43. $sql,
  44. $this->varJson(
  45. $connect
  46. ->sql()
  47. ->table('test_query')
  48. ->insertAll($data, ['吃肉1', '吃肉2'])
  49. )
  50. );
  51. $sql = <<<'eot'
  52. [
  53. "INSERT INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:pdonamedparameter_value),(:pdonamedparameter_name_1,:hello),(:pdonamedparameter_name_2,:pdonamedparameter_value_2),(:pdonamedparameter_name_3,:world)",
  54. {
  55. "pdonamedparameter_name": [
  56. "小鸭子1"
  57. ],
  58. "pdonamedparameter_value": [
  59. "呱呱呱1"
  60. ],
  61. "pdonamedparameter_name_1": [
  62. "小鸭子2"
  63. ],
  64. "pdonamedparameter_name_2": [
  65. "小鸭子3"
  66. ],
  67. "pdonamedparameter_value_2": [
  68. "呱呱呱3"
  69. ],
  70. "pdonamedparameter_name_3": [
  71. "小鸭子4"
  72. ],
  73. "hello": "hello 吃肉",
  74. "world": "world 喝汤"
  75. },
  76. false
  77. ]
  78. eot;
  79. $data = [
  80. ['name' => '小鸭子1', 'value' => '呱呱呱1'],
  81. ['name' => '小鸭子2', 'value' => Condition::raw(':hello')],
  82. ['name' => '小鸭子3', 'value' => '呱呱呱3'],
  83. ['name' => '小鸭子4', 'value' => Condition::raw(':world')],
  84. ];
  85. $this->assertSame(
  86. $sql,
  87. $this->varJson(
  88. $connect
  89. ->sql()
  90. ->table('test_query')
  91. ->insertAll($data, ['hello' => 'hello 吃肉', 'world' => 'world 喝汤']),
  92. 1
  93. )
  94. );
  95. }

bind.insertAll 绑定参数批量写入数据

  1. public function testWithBindFunction(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "INSERT INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:pdonamedparameter_value),(:pdonamedparameter_name_1,:pdopositional2namedparameter_0_1),(:pdonamedparameter_name_2,:pdonamedparameter_value_2),(:pdonamedparameter_name_3,:pdopositional2namedparameter_1_3)",
  7. {
  8. "pdonamedparameter_name": [
  9. "小鸭子1"
  10. ],
  11. "pdonamedparameter_value": [
  12. "呱呱呱1"
  13. ],
  14. "pdonamedparameter_name_1": [
  15. "小鸭子2"
  16. ],
  17. "pdopositional2namedparameter_0_1": [
  18. "吃鱼"
  19. ],
  20. "pdonamedparameter_name_2": [
  21. "小鸭子3"
  22. ],
  23. "pdonamedparameter_value_2": [
  24. "呱呱呱3"
  25. ],
  26. "pdonamedparameter_name_3": [
  27. "小鸭子4"
  28. ],
  29. "pdopositional2namedparameter_1_3": [
  30. "吃肉"
  31. ]
  32. },
  33. false
  34. ]
  35. eot;
  36. $data = [
  37. ['name' => '小鸭子1', 'value' => '呱呱呱1'],
  38. ['name' => '小鸭子2', 'value' => Condition::raw('?')],
  39. ['name' => '小鸭子3', 'value' => '呱呱呱3'],
  40. ['name' => '小鸭子4', 'value' => Condition::raw('?')],
  41. ];
  42. $this->assertSame(
  43. $sql,
  44. $this->varJson(
  45. $connect
  46. ->sql()
  47. ->table('test_query')
  48. ->bind(['吃鱼', '吃肉'])
  49. ->insertAll($data)
  50. )
  51. );
  52. }

insertAll 支持 replace 用法

  1. public function testReplace(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "REPLACE INTO `test_query` (`test_query`.`name`,`test_query`.`value`) VALUES (:pdonamedparameter_name,:pdonamedparameter_value),(:pdonamedparameter_name_1,:pdopositional2namedparameter_0_1),(:pdonamedparameter_name_2,:pdonamedparameter_value_2),(:pdonamedparameter_name_3,:pdopositional2namedparameter_1_3)",
  7. {
  8. "pdonamedparameter_name": [
  9. "小鸭子1"
  10. ],
  11. "pdonamedparameter_value": [
  12. "呱呱呱1"
  13. ],
  14. "pdonamedparameter_name_1": [
  15. "小鸭子2"
  16. ],
  17. "pdopositional2namedparameter_0_1": [
  18. "吃鱼"
  19. ],
  20. "pdonamedparameter_name_2": [
  21. "小鸭子3"
  22. ],
  23. "pdonamedparameter_value_2": [
  24. "呱呱呱3"
  25. ],
  26. "pdonamedparameter_name_3": [
  27. "小鸭子4"
  28. ],
  29. "pdopositional2namedparameter_1_3": [
  30. "吃肉"
  31. ]
  32. },
  33. false
  34. ]
  35. eot;
  36. $data = [
  37. ['name' => '小鸭子1', 'value' => '呱呱呱1'],
  38. ['name' => '小鸭子2', 'value' => Condition::raw('?')],
  39. ['name' => '小鸭子3', 'value' => '呱呱呱3'],
  40. ['name' => '小鸭子4', 'value' => Condition::raw('?')],
  41. ];
  42. $this->assertSame(
  43. $sql,
  44. $this->varJson(
  45. $connect
  46. ->sql()
  47. ->table('test_query')
  48. ->bind(['吃鱼', '吃肉'])
  49. ->insertAll($data, [], true)
  50. )
  51. );
  52. }

insertAll 空数据批量写入示例

  1. public function testInsertWithEmptyData(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "INSERT INTO `test_query` () VALUES (),(),(),()",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $data = [
  12. [],
  13. [],
  14. [],
  15. [],
  16. ];
  17. $this->assertSame(
  18. $sql,
  19. $this->varJson(
  20. $connect
  21. ->sql()
  22. ->table('test_query')
  23. ->insertAll($data)
  24. )
  25. );
  26. }

insertAll.replace 空数据写入示例

  1. public function testReplaceWithEmptyData(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "REPLACE INTO `test_query` () VALUES (),(),(),()",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $data = [
  12. [],
  13. [],
  14. [],
  15. [],
  16. ];
  17. $this->assertSame(
  18. $sql,
  19. $this->varJson(
  20. $connect
  21. ->sql()
  22. ->table('test_query')
  23. ->insertAll($data, [], true)
  24. )
  25. );
  26. }