File: MySqlBulkInsert.php Lines 10 to 29
10 * Bulk Insert (only MySQL)
11 *
12 * いずれ、バルクのチャンクサイズを指定できるようにしたい
13 * @version 0.0.1
14 */
15class MySqlBulkInsert implements \PHPUnit\DbUnit\Operation\Operation
16{
17    protected $operationName = 'MYSQL_BULK_INSERT';
18
Type Class Description
checkstyle TooLong Line exceeds 120 characters; contains 131 characters
pmd LongVariable Avoid excessively long variable names like $databaseTableMetaData. Keep variable name length under 20.
pmd UnusedFormalParameter Avoid unused parameters such as '$databaseTableMetaData'.
19
Type Class Description
checkstyle TooLong Line exceeds 120 characters; contains 131 characters
pmd LongVariable Avoid excessively long variable names like $databaseTableMetaData. Keep variable name length under 20.
pmd UnusedFormalParameter Avoid unused parameters such as '$databaseTableMetaData'.
    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 134
32
33            $query = "
34                INSERT INTO {$connection->quoteSchemaObject($table->getTableMetaData()->getTableName())}
35                ({$columns})
36                VALUES
37                ({$placeHolders})
38            ";
39
40            $bulk = '';
Type Class Description
checkstyle NoSpaceAfter Expected at least 1 space after "<"; 0 found
checkstyle NoSpaceAfter Expected at least 1 space after "="; 0 found
checkstyle NoSpaceBefore Expected at least 1 space before "<"; 0 found
checkstyle NoSpaceBefore Expected at least 1 space before "="; 0 found
41
Type Class Description
checkstyle NoSpaceAfter Expected at least 1 space after "<"; 0 found
checkstyle NoSpaceAfter Expected at least 1 space after "="; 0 found
checkstyle NoSpaceBefore Expected at least 1 space before "<"; 0 found
checkstyle NoSpaceBefore Expected at least 1 space before "="; 0 found
            for ($i=1; $i<$rowCount; $i++) {
42                $bulk .= ",({$placeHolders})\n                ";
43            }
44            $query .= $bulk;
45
46            return $query;
47        } else {
48            return false;
49        }
50    }
51
Type Class Description
pmd LongVariable Avoid excessively long variable names like $databaseTableMetaData. Keep variable name length under 20.
pmd UnusedFormalParameter Avoid unused parameters such as '$databaseTableMetaData'.
52
Type Class Description
pmd LongVariable Avoid excessively long variable names like $databaseTableMetaData. Keep variable name length under 20.
pmd UnusedFormalParameter Avoid unused parameters such as '$databaseTableMetaData'.
    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
Type Class Description
pmd LongVariable Avoid excessively long variable names like $databaseTableMetaData. Keep variable name length under 20.
pmd UnusedFormalParameter Avoid unused parameters such as '$connection'.
pmd UnusedFormalParameter Avoid unused parameters such as '$table'.
62
Type Class Description
pmd LongVariable Avoid excessively long variable names like $databaseTableMetaData. Keep variable name length under 20.
pmd UnusedFormalParameter Avoid unused parameters such as '$connection'.
pmd UnusedFormalParameter Avoid unused parameters such as '$table'.
    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     */
Type Class Description
pmd CyclomaticComplexity The method execute() has a Cyclomatic Complexity of 11. The configured cyclomatic complexity threshold is 10.
75
Type Class Description
pmd CyclomaticComplexity The method execute() has a Cyclomatic Complexity of 11. The configured cyclomatic complexity threshold is 10.
    public function execute(Connection $connection, IDataSet $dataSet)
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 */
Type Class Description
pmd LongVariable Avoid excessively long variable names like $databaseTableMetaData. Keep variable name length under 20.
89
Type Class Description
pmd LongVariable Avoid excessively long variable names like $databaseTableMetaData. Keep variable name length under 20.
            $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 = [];
Type Class Description
checkstyle NoSpaceAfter Expected at least 1 space after "<"; 0 found
checkstyle NoSpaceAfter Expected at least 1 space after "="; 0 found
checkstyle NoSpaceBefore Expected at least 1 space before "<"; 0 found
checkstyle NoSpaceBefore Expected at least 1 space before "="; 0 found
102
Type Class Description
checkstyle NoSpaceAfter Expected at least 1 space after "<"; 0 found
checkstyle NoSpaceAfter Expected at least 1 space after "="; 0 found
checkstyle NoSpaceBefore Expected at least 1 space before "<"; 0 found
checkstyle NoSpaceBefore Expected at least 1 space before "="; 0 found
                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) {
Type Class Description
checkstyle TooLong Line exceeds 120 characters; contains 163 characters
114
Type Class Description
checkstyle TooLong Line exceeds 120 characters; contains 163 characters
                        throw new \PHPUnit\DbUnit\Operation\Exception($this->operationName, '', [], $table, 'Rows requested for insert, but no columns provided!');
115                    }
116                    continue;
117                }
118
119                try {
120                    $statement = $connection->getConnection()->prepare($query);
121                    $statement->execute($args);
122                } catch (\Exception $e) {
123                    throw new \PHPUnit\DbUnit\Operation\Exception(
Type Class Description
checkstyle MultipleArguments Only one argument is allowed per line in a multi-line function call
checkstyle MultipleArguments Only one argument is allowed per line in a multi-line function call
checkstyle MultipleArguments Only one argument is allowed per line in a multi-line function call
checkstyle MultipleArguments Only one argument is allowed per line in a multi-line function call
124
Type Class Description
checkstyle MultipleArguments Only one argument is allowed per line in a multi-line function call
checkstyle MultipleArguments Only one argument is allowed per line in a multi-line function call
checkstyle MultipleArguments Only one argument is allowed per line in a multi-line function call
checkstyle MultipleArguments Only one argument is allowed per line in a multi-line function call
                        $this->operationName, $query, $args, $table, $e->getMessage()
125                    );
126                }
127
128                $bulkI += $i;
129            }
130
131            if ($disablePrimaryKeys) {
132                $connection->enablePrimaryKeys($databaseTableMetaData->getTableName());
133            }
134        }