|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Actions\Processors\AbstractBaseProcessor |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Actions\Processors; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\EntityStatus; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* An abstract processor implementation provide basic CRUD functionality. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Tim Wagner <[email protected]> |
|
29
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/techdivision/import |
|
32
|
|
|
* @link http://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract class AbstractBaseProcessor extends AbstractProcessor |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The array with the statements to be prepared. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $statements = array(); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The array with the prepared statements. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $preparedStatements = array(); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Return's the array with the SQL statements that has to be prepared. |
|
53
|
|
|
* |
|
54
|
|
|
* @return array The SQL statements to be prepared |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function getStatements() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->statements; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Add's the prepared statement. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $name The unique name of the prepared statement |
|
65
|
|
|
* @param \PDOStatement $preparedStatement The prepared statement |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function addPreparedStatement($name, \PDOStatement $preparedStatement) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->preparedStatements[$name] = $preparedStatement; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return's the prepared statement. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $name The name of the prepared statement to return |
|
78
|
|
|
* |
|
79
|
|
|
* @return \PDOStatement The prepared statement |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getPreparedStatement($name = null) |
|
82
|
|
|
{ |
|
83
|
|
|
|
|
84
|
|
|
// try to load the prepared statement, or use the default one |
|
85
|
|
|
if (isset($this->preparedStatements[$name])) { |
|
86
|
|
|
return $this->preparedStatements[$name]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// return the first (default) prepared statement |
|
90
|
|
|
return reset($this->preparedStatements); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Qeuery whether or not the prepared statement is available or not. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $name The nqme of the prepared statement |
|
97
|
|
|
* |
|
98
|
|
|
* @return boolean TRUE if the prepared statement is available, else FALSE |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function hasPreparedStatement($name) |
|
101
|
|
|
{ |
|
102
|
|
|
return isset($this->preparedStatements[$name]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* The array with the prepared statements. |
|
107
|
|
|
* |
|
108
|
|
|
* @return array The prepared statments |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function getPreparedStatements() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->preparedStatements; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Prepare's and return's the passed row by removing the |
|
117
|
|
|
* entity status. |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $row The row to prepare |
|
120
|
|
|
* |
|
121
|
|
|
* @return array The prepared row |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function prepareRow(array $row) |
|
124
|
|
|
{ |
|
125
|
|
|
|
|
126
|
|
|
// remove the entity status |
|
127
|
|
|
unset($row[EntityStatus::MEMBER_NAME]); |
|
128
|
|
|
|
|
129
|
|
|
// return the prepared row |
|
130
|
|
|
return $row; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Implements the CRUD functionality the processor is responsible for, |
|
135
|
|
|
* can be one of CREATE, READ, UPDATE or DELETE a entity. |
|
136
|
|
|
* |
|
137
|
|
|
* @param array $row The data to handle |
|
138
|
|
|
* @param string|null $name The name of the prepared statement to execute |
|
139
|
|
|
* |
|
140
|
|
|
* @return void |
|
141
|
|
|
*/ |
|
142
|
|
|
public function execute($row, $name = null) |
|
143
|
|
|
{ |
|
144
|
|
|
try { |
|
145
|
|
|
// finally execute the prepared statement |
|
146
|
|
|
$this->getPreparedStatement($name)->execute($this->prepareRow($row)); |
|
147
|
|
|
|
|
|
|
|
|
|
148
|
|
|
} catch (\PDOException $pdoe) { |
|
149
|
|
|
// initialize the SQL statement with the placeholders |
|
150
|
|
|
$sql = $this->getPreparedStatement($name)->queryString; |
|
151
|
|
|
|
|
152
|
|
|
// replace the placeholders with the values |
|
153
|
|
|
foreach ($row as $key => $value) { |
|
154
|
|
|
$sql = str_replace(sprintf(':%s', $key), $value, $sql); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// prepare the error message itself |
|
158
|
|
|
$message = sprintf('%s when executing SQL "%s"', $pdoe->getMessage(), preg_replace('/\r\n\s\s+/', ' ', $sql)); |
|
159
|
|
|
|
|
160
|
|
|
// re-throw the exception with a more detailed error message |
|
161
|
|
|
throw new \PDOException($message, null, $pdoe); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Initializes the proceessor with the prepared statements. |
|
167
|
|
|
* |
|
168
|
|
|
* @return void |
|
169
|
|
|
*/ |
|
170
|
|
|
public function init() |
|
171
|
|
|
{ |
|
172
|
|
|
foreach ($this->getStatements() as $name => $statement) { |
|
173
|
|
|
$this->addPreparedStatement($name, $this->getConnection()->prepare($statement)); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|