1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ddeboer\DataImport\Writer; |
4
|
|
|
|
5
|
|
|
use Ddeboer\DataImport\Exception\WriterException; |
6
|
|
|
use Ddeboer\DataImport\Writer; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Write data into a specific database table using a PDO instance. |
10
|
|
|
* |
11
|
|
|
* IMPORTANT: If your PDO instance does not have ERRMODE_EXCEPTION any write failure will be silent or logged to |
12
|
|
|
* stderr only. It is strongly recomended you enable Exceptions with: |
13
|
|
|
* |
14
|
|
|
* $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
15
|
|
|
* |
16
|
|
|
* @author Stefan Warman |
17
|
|
|
*/ |
18
|
|
|
class PdoWriter implements Writer, FlushableWriter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \PDO |
22
|
|
|
*/ |
23
|
|
|
protected $pdo; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $tableName; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \PDOStatement |
32
|
|
|
*/ |
33
|
|
|
protected $statement; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $stack; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Note if your table name is a reserved word for your target DB you should quote it in the appropriate way e.g. |
42
|
|
|
* for MySQL enclose the name in `backticks`. |
43
|
|
|
* |
44
|
|
|
* @param \PDO $pdo |
45
|
|
|
* @param string $tableName |
46
|
|
|
*/ |
47
|
6 |
|
public function __construct(\PDO $pdo, $tableName) |
48
|
|
|
{ |
49
|
6 |
|
$this->pdo = $pdo; |
50
|
6 |
|
$this->tableName = $tableName; |
51
|
|
|
|
52
|
6 |
|
if (\PDO::ERRMODE_EXCEPTION !== $this->pdo->getAttribute(\PDO::ATTR_ERRMODE)) { |
53
|
2 |
|
throw new WriterException('Please set the pdo error mode to PDO::ERRMODE_EXCEPTION'); |
54
|
|
|
} |
55
|
4 |
|
} |
56
|
|
|
|
57
|
4 |
|
public function prepare() |
58
|
|
|
{ |
59
|
4 |
|
$this->stack = []; |
60
|
4 |
|
$this->statement = null; |
61
|
4 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
4 |
|
public function writeItem(array $item) |
67
|
|
|
{ |
68
|
4 |
|
if (null === $this->statement) { |
69
|
|
|
try { |
70
|
4 |
|
$this->statement = $this->pdo->prepare(sprintf( |
71
|
4 |
|
'INSERT INTO %s (%s) VALUES (%s)', |
72
|
4 |
|
$this->tableName, |
73
|
4 |
|
implode(',', array_keys($item)), |
74
|
4 |
|
substr(str_repeat('?,', count($item)), 0, -1) |
75
|
4 |
|
)); |
76
|
4 |
|
} catch (\PDOException $e) { |
77
|
2 |
|
throw new WriterException('Failed to send query', null, $e); |
78
|
|
|
} |
79
|
2 |
|
} |
80
|
|
|
|
81
|
2 |
|
$this->stack[] = array_values($item); |
82
|
2 |
|
} |
83
|
|
|
|
84
|
2 |
|
public function finish() |
85
|
|
|
{ |
86
|
2 |
|
$this->flush(); |
87
|
|
|
|
88
|
2 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
public function flush() |
92
|
|
|
{ |
93
|
2 |
|
$this->pdo->beginTransaction(); |
94
|
|
|
|
95
|
|
|
try { |
96
|
2 |
|
foreach ($this->stack as $data) { |
97
|
2 |
|
$this->statement->execute($data); |
98
|
2 |
|
} |
99
|
2 |
|
$this->stack = []; |
100
|
|
|
|
101
|
2 |
|
$this->pdo->commit(); |
102
|
2 |
|
} catch (\PDOException $e) { |
103
|
|
|
throw new WriterException('Failed to write to database', null, $e); |
104
|
|
|
} |
105
|
2 |
|
} |
106
|
|
|
} |
107
|
|
|
|