File: MySqlBulkInsert.php Lines 10 to 29 |
|
10 | * Bulk Insert (only MySQL) |
|
11 | * |
|
12 | * いずれ、バルクのチャンクサイズを指定できるようにしたい |
|
13 | * @version 0.0.1 |
|
14 | */ |
|
15 | class MySqlBulkInsert implements \PHPUnit\DbUnit\Operation\Operation |
|
16 | { |
|
17 | protected $operationName = 'MYSQL_BULK_INSERT'; |
|
18 | |
| | protected function buildOperationQuery(ITableMetadata $databaseTableMetaData, ITable $table, Connection $connection, $rowCount) |
|
20 | { |
|
21 | $columnCount = \count($table->getTableMetaData()->getColumns()); |
|
22 | |
|
23 | if ($columnCount > 0) { |
|
24 | $placeHolders = \implode(', ', \array_fill(0, $columnCount, '?')); |
|
25 | |
|
26 | $columns = ''; |
|
27 | foreach ($table->getTableMetaData()->getColumns() as $column) { |
|
28 | $columns .= $connection->quoteSchemaObject($column) . ', '; |
|
29 | } |
|
File: MySqlBulkInsert.php Lines 32 to 138 |
|
32 | |
|
33 | $query = " |
|
34 | INSERT INTO {$connection->quoteSchemaObject($table->getTableMetaData()->getTableName())} |
|
35 | ({$columns}) |
|
36 | VALUES |
|
37 | ({$placeHolders}) |
|
38 | "; |
|
39 | |
|
40 | $bulk = ''; |
| | for ($i=1; $i<$rowCount; $i++) { |
|
42 | $bulk .= ",({$placeHolders})\n "; |
|
43 | } |
|
44 | $query .= $bulk; |
|
45 | |
|
46 | return $query; |
|
47 | } |
|
48 | |
|
49 | return false; |
|
50 | } |
|
51 | |
| | protected function buildOperationArguments(ITableMetadata $databaseTableMetaData, ITable $table, $row) |
|
53 | { |
|
54 | $args = []; |
|
55 | foreach ($table->getTableMetaData()->getColumns() as $columnName) { |
|
56 | $args[] = $table->getValue($row, $columnName); |
|
57 | } |
|
58 | |
|
59 | return $args; |
|
60 | } |
|
61 | |
| | protected function disablePrimaryKeys(ITableMetadata $databaseTableMetaData, ITable $table, Connection $connection) |
|
63 | { |
|
64 | if (\count($databaseTableMetaData->getPrimaryKeys())) { |
|
65 | return true; |
|
66 | } |
|
67 | |
|
68 | return false; |
|
69 | } |
|
70 | |
|
71 | /** |
|
72 | * @param Connection $connection |
|
73 | * @param IDataSet $dataSet |
|
74 | */ |
| | public function execute(Connection $connection, IDataSet $dataSet): void |
|
76 | { |
|
77 | $databaseDataSet = $connection->createDataSet(); |
|
78 | |
|
79 | $dsIterator = $dataSet->getIterator(); |
|
80 | |
|
81 | foreach ($dsIterator as $table) { |
|
82 | $rowCount = $table->getRowCount(); |
|
83 | |
|
84 | if ($rowCount == 0) { |
|
85 | continue; |
|
86 | } |
|
87 | |
|
88 | /* @var $table ITable */ |
| | $databaseTableMetaData = $databaseDataSet->getTableMetaData($table->getTableMetaData()->getTableName()); |
|
90 | |
|
91 | $disablePrimaryKeys = $this->disablePrimaryKeys($databaseTableMetaData, $table, $connection); |
|
92 | |
|
93 | if ($disablePrimaryKeys) { |
|
94 | $connection->disablePrimaryKeys($databaseTableMetaData->getTableName()); |
|
95 | } |
|
96 | |
|
97 | |
|
98 | $bulkI = 0; |
|
99 | while ($bulkI < $rowCount) { |
|
100 | $chunkLen = 0; |
|
101 | $args = []; |
| | for ($i=0; $i<100; $i++) { |
|
103 | $rowNum = $bulkI + $i; |
|
104 | if ($rowNum >= $rowCount) { |
|
105 | break; |
|
106 | } |
|
107 | $args = array_merge($args, $this->buildOperationArguments($databaseTableMetaData, $table, $rowNum)); |
|
108 | $chunkLen++; |
|
109 | } |
|
110 | |
|
111 | $query = $this->buildOperationQuery($databaseTableMetaData, $table, $connection, $chunkLen); |
|
112 | if ($query === false) { |
|
113 | if ($table->getRowCount() > 0) { |
| | throw new \PHPUnit\DbUnit\Operation\Exception($this->operationName, '', [], $table, 'Rows requested for insert, but no columns provided!'); |
|
115 | } |
|
116 | continue; |
|
117 | } |
|
118 | |
|
119 | if ($disablePrimaryKeys) { |
|
120 | $connection->disablePrimaryKeys($databaseTableMetaData->getTableName()); |
|
121 | } |
|
122 | |
|
123 | try { |
|
124 | $statement = $connection->getConnection()->prepare($query); |
|
125 | $statement->execute($args); |
|
126 | } catch (\Exception $e) { |
|
127 | throw new \PHPUnit\DbUnit\Operation\Exception( |
| | $this->operationName, $query, $args, $table, $e->getMessage() |
|
129 | ); |
|
130 | } |
|
131 | |
|
132 | $bulkI += $i; |
|
133 | } |
|
134 | |
|
135 | if ($disablePrimaryKeys) { |
|
136 | $connection->enablePrimaryKeys($databaseTableMetaData->getTableName()); |
|
137 | } |
|
138 | } |
|