Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Controller/Admin/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ private function upsertMasterFromCsv($em, $dir, $csvName, $tableName, array $opt
$updateSql = implode(', ', array_map(function ($c) {
return '"' . $c . '" = EXCLUDED."' . $c . '"';
}, $updateCols));
$sql = 'INSERT INTO ' . $tableName . ' (' . $colsSql . ') VALUES (' . $placeholders . ') ON CONFLICT (id) DO UPDATE SET ' . $updateSql;
$sql = 'INSERT INTO ' . $em->quoteIdentifier($tableName) . ' (' . $colsSql . ') VALUES (' . $placeholders . ') ON CONFLICT (id) DO UPDATE SET ' . $updateSql;
$em->prepare($sql)->executeStatement(array_values($insertValues));
$rowCount++;
}
Expand Down Expand Up @@ -1397,7 +1397,7 @@ private function saveStock($em)
$builder = new BulkInsertQuery($em, $tableName);
$builder->setColumns($listTableColumns);

$em->exec('DELETE FROM ' . $tableName);
$em->exec('DELETE FROM ' . $em->quoteIdentifier($tableName));

$i = 1;
$batchSize = 20;
Expand Down Expand Up @@ -1436,7 +1436,7 @@ private function saveProductImage($em)
$builder = new BulkInsertQuery($em, $tableName);
$builder->setColumns($listTableColumns);

$em->exec('DELETE FROM ' . $tableName);
$em->exec('DELETE FROM ' . $em->quoteIdentifier($tableName));

$i = 1;
$batchSize = 20;
Expand Down Expand Up @@ -2037,7 +2037,7 @@ private function saveOrderItem($em)
$builder = new BulkInsertQuery($em, $tableName);
$builder->setColumns($listTableColumns);

$i = $em->fetchOne('SELECT max(id) + 1 FROM ' . $tableName);
$i = $em->fetchOne('SELECT max(id) + 1 FROM ' . $em->quoteIdentifier($tableName));
$batchSize = 20;
foreach ($this->order_item as $order_id => $type) {
foreach ($type as $key => $value) {
Expand Down Expand Up @@ -2296,7 +2296,7 @@ private function fix4x($em, $tmpDir, $csvName)
$updateSet = array_map(fn($c) => '"' . $c . '" = EXCLUDED."' . $c . '"', $updateCols);
$conflictCols = array_map(fn($c) => '"' . $c . '"', $primaryKeys);

$sql = 'INSERT INTO "' . $tableName . '" (' . implode(', ', $cols) . ') ' .
$sql = 'INSERT INTO ' . $em->quoteIdentifier($tableName) . ' (' . implode(', ', $cols) . ') ' .
'VALUES (' . implode(', ', $placeholders) . ') ' .
'ON CONFLICT (' . implode(', ', $conflictCols) . ') ' .
'DO UPDATE SET ' . implode(', ', $updateSet);
Expand Down
34 changes: 19 additions & 15 deletions Service/DataMigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,23 @@ public function updateEnv($newMagicValue)
file_put_contents($envFile, $env);
}

public function resetTable(Connection $em, $tableName)
/**
* テーブル名のバリデーション
*/
private function validateTableName(string $tableName): void
{
$platform = $em->getDatabasePlatform()->getName();

if ($platform == 'mysql') {
$em->exec('DELETE FROM ' . $tableName);
} elseif ($platform == 'postgresql') {
// PostgreSQLでは fix4x() はUPSERTを使うため、このメソッドは呼ばれない
// saveToC() などから呼ばれる場合はDELETEを実行
$em->exec('DELETE FROM "' . $tableName . '"');
} else {
$em->exec('DELETE FROM ' . $tableName);
if (!preg_match('/^[a-zA-Z_][a-zA-Z0-9_]{0,63}$/', $tableName)) {
throw new \InvalidArgumentException('Invalid table name: ' . $tableName);
}
}

public function resetTable(Connection $em, $tableName)
{
$this->validateTableName($tableName);
$quoted = $em->quoteIdentifier($tableName);
$em->exec('DELETE FROM ' . $quoted);
}

public function convertNULL($data)
{
foreach ($data as &$v) {
Expand Down Expand Up @@ -305,7 +307,7 @@ public function begin($em, $context = NULL)
foreach ($targetTables as $t) {
$exists = $em->fetchOne("SELECT 1 FROM pg_tables WHERE schemaname='public' AND tablename=?", [$t]);
if ($exists) {
$existing[] = '"' . str_replace('"', '""', $t) . '"';
$existing[] = $em->quoteIdentifier($t);
}
}
if ($existing) {
Expand All @@ -324,11 +326,13 @@ public function begin($em, $context = NULL)

public function setIdSeq($em, $tableName)
{
$max = $em->fetchOne('SELECT coalesce(max(id), 0) + 1 FROM ' . $tableName);
$this->validateTableName($tableName);
$quoted = $em->quoteIdentifier($tableName);
$max = $em->fetchOne('SELECT coalesce(max(id), 0) + 1 FROM ' . $quoted);
$seq = $tableName . '_id_seq';
$count = $em->fetchOne("select count(*) from pg_class where relname = '$seq';");
$count = $em->fetchOne("select count(*) from pg_class where relname = ?", [$seq]);
if ($count) {
$em->exec("SELECT setval('$seq', $max);");
$em->executeStatement("SELECT setval(?, ?)", [$seq, $max]);
}
}

Expand Down