|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Eccube\Doctrine\Common\CsvDataFixtures; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\DataFixtures\FixtureInterface; |
|
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* CSVファイルを扱うためのフィクスチャ. |
|
12
|
|
|
* |
|
13
|
|
|
* @see https://github.com/doctrine/data-fixtures/blob/master/lib/Doctrine/Common/DataFixtures/FixtureInterface.php |
|
14
|
|
|
*/ |
|
15
|
|
|
class CsvFixture implements FixtureInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var \SplFileObject $file */ |
|
18
|
|
|
protected $file; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(\SplFileObject $file) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$this->file = $file; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
public function load(ObjectManager $manager) |
|
29
|
|
|
{ |
|
30
|
|
|
// CSV Reader に設定 |
|
31
|
|
|
$this->file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); |
|
32
|
|
|
|
|
33
|
|
|
// ヘッダ行を取得 |
|
34
|
|
|
$headers = $this->file->current(); |
|
35
|
|
|
$this->file->next(); |
|
36
|
|
|
|
|
37
|
|
|
// ファイル名からテーブル名を取得 |
|
38
|
|
|
$table_name = str_replace('.'.$this->file->getExtension(), '', $this->file->getFilename()); |
|
39
|
|
|
$sql = $this->getSql($table_name, $headers); |
|
|
|
|
|
|
40
|
|
|
/** @var Connection $Connection */ |
|
41
|
|
|
$Connection = $manager->getConnection(); |
|
|
|
|
|
|
42
|
|
|
$Connection->beginTransaction(); |
|
43
|
|
|
|
|
44
|
|
|
// mysqlの場合はNO_AUTO_VALUE_ON_ZEROを設定 |
|
45
|
|
|
if ('mysql' === $Connection->getDatabasePlatform()->getName()) { |
|
46
|
|
|
$Connection->exec("SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';"); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// TODO エラーハンドリング |
|
50
|
|
|
$prepare = $Connection->prepare($sql); |
|
51
|
|
|
while (!$this->file->eof()) { |
|
52
|
|
|
$rows = $this->file->current(); |
|
53
|
|
|
$index = 1; |
|
54
|
|
|
// データ行をバインド |
|
55
|
|
|
foreach ($rows as $col) { |
|
|
|
|
|
|
56
|
|
|
$col = $col === '' ? null : $col; |
|
57
|
|
|
$prepare->bindValue($index, $col); |
|
58
|
|
|
$index++; |
|
59
|
|
|
} |
|
60
|
|
|
// batch insert |
|
61
|
|
|
$result = $prepare->execute(); |
|
|
|
|
|
|
62
|
|
|
$this->file->next(); |
|
63
|
|
|
} |
|
64
|
|
|
$Connection->commit(); |
|
65
|
|
|
|
|
66
|
|
|
// postgresqlの場合はシーケンスを振り直す |
|
67
|
|
|
if ('postgresql' === $Connection->getDatabasePlatform()->getName()) { |
|
68
|
|
|
// テーブル情報を取得 |
|
69
|
|
|
$sm = $Connection->getSchemaManager(); |
|
70
|
|
|
$table = $sm->listTableDetails($table_name); |
|
71
|
|
|
|
|
72
|
|
|
// 主キーがないテーブルはスキップ |
|
73
|
|
|
if (!$table->hasPrimaryKey()) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// 複合主キーのテーブルはスキップ |
|
78
|
|
|
$pkColumns = $table->getPrimaryKey()->getColumns(); |
|
79
|
|
|
if (count($pkColumns) != 1) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// シーケンス名を取得 |
|
84
|
|
|
$pk_name = $pkColumns[0]; |
|
85
|
|
|
$sequence_name = sprintf('%s_%s_seq', $table_name, $pk_name); |
|
86
|
|
|
|
|
87
|
|
|
// シーケンスの存在チェック |
|
88
|
|
|
$sql = 'SELECT COUNT(*) FROM information_schema.sequences WHERE sequence_name = ?'; |
|
89
|
|
|
$count = $Connection->fetchColumn($sql, [$sequence_name]); |
|
90
|
|
|
if ($count < 1) { |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// シーケンスを更新 |
|
95
|
|
|
$sql = sprintf('SELECT MAX(%s) FROM %s', $pk_name, $table_name); |
|
96
|
|
|
$max = $Connection->fetchColumn($sql); |
|
97
|
|
|
if (is_null($max)) { |
|
98
|
|
|
// レコードが無い場合は1を初期値に設定 |
|
99
|
|
|
$sql = sprintf("SELECT SETVAL('%s', 1, false)", $sequence_name); |
|
100
|
|
|
} else { |
|
101
|
|
|
// レコードがある場合は最大値を設定 |
|
102
|
|
|
$sql = sprintf("SELECT SETVAL('%s', %s)", $sequence_name, $max); |
|
103
|
|
|
} |
|
104
|
|
|
$Connection->executeQuery($sql); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* INSERT を生成する. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $table_name テーブル名 |
|
112
|
|
|
* @param array $headers カラム名の配列 |
|
|
|
|
|
|
113
|
|
|
* @return string INSERT 文 |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getSql($table_name, array $headers) |
|
116
|
|
|
{ |
|
117
|
|
|
return 'INSERT INTO '.$table_name.' ('.implode(', ', $headers).') VALUES ('.implode(', ', array_fill(0, count($headers), '?')).')'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* 保持している \SplFileObject を返す. |
|
122
|
|
|
* |
|
123
|
|
|
* @return \SplFileObject |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getFile() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->file; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|