1: <?php
2: namespace Datadepo\Api\DataStores;
3: use Datadepo\Api;
4:
5: class PdoDataStore implements IDataStore
6: {
7:
8:
9: protected $pdo;
10:
11:
12: protected $iniConfiguration;
13:
14: 15: 16:
17: public function __construct(\PDO $pdo = NULL)
18: {
19: $this->pdo = $pdo;
20: }
21:
22: 23: 24:
25: public function setIniConfiguration(Api\IniConfiguration $iniConfiguration)
26: {
27: $this->iniConfiguration = $iniConfiguration;
28: }
29:
30: 31:
32: public function connect()
33: {
34:
35: if ($this->pdo instanceof \PDO) {
36: return;
37: }
38:
39:
40: $settings = $this->iniConfiguration->get('PDO');
41: $this->pdo = new \PDO($settings['dsn'], $settings['user'], $settings['pwd'], $this->getPdoOptions($settings));
42: $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
43: }
44:
45: 46: 47:
48: public function getConfig($names)
49: {
50: $names = func_get_args();
51: $query = $this->pdo->query("SELECT name, value
52: FROM datadepo_config
53: WHERE name IN (" . $this->extractForIn($names) . ")");
54:
55: $values = array();
56: foreach ($query->fetchAll() as $row) {
57: $values[$row['name']] = $row['value'];
58: }
59: return $values;
60: }
61:
62: 63: 64: 65:
66: public function setConfig($name, $value)
67: {
68: $this->insertOrUpdate('datadepo_config', array('name' => $name, 'value' => $value));
69: }
70:
71: 72: 73: 74:
75: public function getChecksums($name, $codes)
76: {
77: $keyColumnName = 'code';
78: if (in_array($name, array('suppliers', 'categories'))) {
79: $keyColumnName = 'datadepo_id';
80: }
81:
82: $query = $this->pdo->query("SELECT $keyColumnName, checksum
83: FROM `datadepo_$name`
84: WHERE $keyColumnName IN (". $this->extractForIn($codes).")");
85:
86: $rows = array();
87: while($row = $query->fetch(\PDO::FETCH_ASSOC)) {
88: $rows[$row[$keyColumnName]] = $row['checksum'];
89: }
90: return $rows;
91: }
92:
93:
94:
95:
96: 97:
98: public function startChunkProcess()
99: {
100: $this->pdo->beginTransaction();
101: }
102:
103: 104:
105: public function endChunkProcess()
106: {
107: $this->pdo->commit();
108: }
109:
110: 111: 112:
113: public function insertRow(Api\Structures\AbstractStructure $line)
114: {
115: $table = NULL;
116: switch (TRUE) {
117: case $line instanceof Api\Structures\DataLine:
118: $this->processDataLine($line);
119: break;
120: case $line instanceof Api\Structures\BusinessLine:
121: $this->processBusinessLine($line);
122: break;
123: case $line instanceof Api\Structures\SupplierLine:
124: $this->processSupplierLine($line);
125: break;
126: case $line instanceof Api\Structures\CategoryLine:
127: $this->processCategoryLine($line);
128: break;
129: }
130: }
131:
132: 133: 134:
135: public function updateRow(Api\Structures\AbstractStructure $line)
136: {
137: $this->insertRow($line);
138: }
139:
140: 141: 142:
143: protected function processDataLine(Api\Structures\DataLine $line)
144: {
145: if ($line->getDeleted()) {
146: $data = array('code' => $line->getPrimary(),
147: 'json' => $line->getJson(),
148: 'checksum' => $line->getChecksum(),
149: 'deleted' => 1);
150: }
151: else {
152: $data = array('project' => $line->getProject(),
153: 'code' => $line->getPrimary(),
154: 'name' => $line->getName(),
155: 'name_sub' => $line->getNameSub(),
156: 'pair_value' => $line->getPairValue(),
157: 'ean' => $line->getEan(),
158: 'isbn' => $line->getIsbn(),
159: 'description' => $line->getDescription(),
160: 'category_id' => $line->getCategoryId(),
161: 'json' => $line->getJson(),
162: 'deleted' => 0,
163:
164:
165: 'checksum' => $line->getChecksum(),
166: 'checksum_images' => $line->getChecksumImages(),
167: 'checksum_parameters' => $line->getChecksumParameters(),
168: 'checksum_related' => $line->getChecksumRelated());
169: }
170: $this->insertOrUpdate('datadepo_data', $data);
171: }
172:
173: 174: 175:
176: protected function processBusinessLine(Api\Structures\BusinessLine $line)
177: {
178: $data = array('code' => $line->getPrimary(),
179: 'supplier_set' => $line->getSupplierSet(),
180: 'json' => $line->getJson(),
181: 'checksum' => $line->getChecksum());
182: $this->insertOrUpdate('datadepo_business', $data);
183: }
184:
185: 186: 187:
188: protected function processSupplierLine(Api\Structures\SupplierLine $line)
189: {
190: $data = array('project' => $line->getProject(),
191: 'datadepo_id' => $line->getDataDepoId(),
192: 'name' => $line->getName(),
193: 'deleted' => intval($line->getDeleted()),
194: 'json' => $line->getJson(),
195: 'checksum' => $line->getChecksum());
196: $this->insertOrUpdate('datadepo_suppliers', $data);
197: }
198:
199: 200: 201:
202: protected function processCategoryLine(Api\Structures\CategoryLine $line)
203: {
204: $data = array('project' => $line->getProject(),
205: 'datadepo_id' => $line->getDataDepoId(),
206: 'parent_id' => $line->getParentId(),
207: 'position' => $line->getPosition(),
208: 'name' => $line->getName(),
209: 'name_path' => $line->getNamePath(),
210: 'active' => intval($line->getActive()),
211: 'deleted' => intval($line->getDeleted()),
212: 'json' => $line->getJson(),
213: 'checksum' => $line->getChecksum());
214: $this->insertOrUpdate('datadepo_categories', $data);
215: }
216:
217:
218:
219:
220:
221:
222: 223: 224: 225:
226: protected function getPdoOptions($settings)
227: {
228: $opts = array();
229: if (isset($settings['opts'])) {
230: foreach ($settings['opts'] as $const => $value) {
231: $opts[constant('\PDO::' . $const)] = $value;
232: }
233: }
234: return $opts;
235: }
236:
237: 238: 239: 240:
241: protected function extractForIn($values)
242: {
243: $pdo = $this->pdo;
244: return implode(', ', array_map(function($i) use ($pdo) { return $pdo->quote($i); }, $values));
245: }
246:
247: 248: 249: 250:
251: protected function insertOrUpdate($table, $data)
252: {
253: $binds = array_combine(array_map(function($input) { return ':' . $input; }, array_keys($data)), $data);
254: $updateBinds = array_map(function($input) { return $input . ' = :' . $input; }, array_keys($data));
255:
256:
257: $query = $this->pdo->prepare("INSERT INTO $table (". implode(', ', array_keys($data)).")
258: VALUES (". implode(', ', array_keys($binds)).")
259: ON DUPLICATE KEY UPDATE " . implode(', ', $updateBinds));
260: $query->execute($binds);
261: }
262:
263: }