1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2015 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
*/
|
8
|
|
|
abstract class AbstractImportWriter extends CComponent
|
9
|
|
|
{
|
10
|
|
|
/**
|
11
|
|
|
* @var ConsoleFileLogger
|
12
|
|
|
*/
|
13
|
|
|
public $logger;
|
14
|
|
|
|
15
|
|
|
public $clear = false;
|
16
|
|
|
|
17
|
|
|
public $clearTables = array();
|
18
|
|
|
|
19
|
|
|
public $uniqueAttribute = 'articul';
|
20
|
|
|
|
21
|
|
|
private $useCurrentTransaction;
|
22
|
|
|
|
23
|
|
|
public function __construct(ConsoleFileLogger $logger)
|
24
|
|
|
{
|
25
|
|
|
$this->logger = $logger;
|
26
|
|
|
}
|
27
|
|
|
|
28
|
|
|
public function init()
|
29
|
|
|
{
|
30
|
|
|
$this->clear();
|
31
|
|
|
}
|
32
|
|
|
|
33
|
|
|
public function clear()
|
34
|
|
|
{
|
35
|
|
|
if( $this->clear && !empty($this->clearTables) )
|
36
|
|
|
{
|
37
|
|
|
$this->logger->log('Очистка БД');
|
38
|
|
|
$this->clearTables();
|
39
|
|
|
$this->clear = false;
|
40
|
|
|
}
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
abstract public function writeAll(array $data);
|
44
|
|
|
|
45
|
|
|
abstract public function writePartial(array $data);
|
46
|
|
|
|
47
|
|
|
abstract public function showStatistics();
|
48
|
|
|
|
49
|
|
|
public function beginTransaction()
|
50
|
|
|
{
|
51
|
|
|
if( Yii::app()->db->getCurrentTransaction() && is_null($this->useCurrentTransaction) )
|
52
|
|
|
{
|
53
|
|
|
$this->useCurrentTransaction = false;
|
54
|
|
|
return;
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
if( is_null($this->useCurrentTransaction) )
|
58
|
|
|
{
|
59
|
|
|
Yii::app()->db->beginTransaction();
|
60
|
|
|
$this->useCurrentTransaction = true;
|
61
|
|
|
}
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
public function commitTransaction()
|
65
|
|
|
{
|
66
|
|
|
if( $this->useCurrentTransaction )
|
67
|
|
|
{
|
68
|
|
|
Yii::app()->db->currentTransaction->commit();
|
69
|
|
|
$this->useCurrentTransaction = null;
|
70
|
|
|
}
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
public function rollbackTransaction()
|
74
|
|
|
{
|
75
|
|
|
if( $this->useCurrentTransaction )
|
76
|
|
|
{
|
77
|
|
|
Yii::app()->db->currentTransaction->rollback();
|
78
|
|
|
$this->useCurrentTransaction = null;
|
79
|
|
|
}
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
protected function clearTables()
|
83
|
|
|
{
|
84
|
|
|
foreach($this->clearTables as $table)
|
85
|
|
|
{
|
86
|
|
|
if( strpos($table, 'product_param_name') !== false )
|
87
|
|
|
{
|
88
|
|
|
$this->clearParameterNames();
|
89
|
|
|
}
|
90
|
|
|
else
|
91
|
|
|
{
|
92
|
|
|
$command = Yii::app()->db->createCommand(Yii::app()->db->schema->truncateTable($table));
|
93
|
|
|
if( $command->execute() )
|
94
|
|
|
throw new WarningException("Не удаллсь очистить таблицу ".$table);
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
$this->logger->log('Таблица '.$table.' очищена');
|
98
|
|
|
}
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
protected function clearParameterNames()
|
102
|
|
|
{
|
103
|
|
|
$builder = new CDbCommandBuilder(Yii::app()->db->getSchema());
|
104
|
|
|
$builder->createSqlCommand("DELETE FROM `{{product_param_name}}` WHERE parent > 1")->execute();
|
105
|
|
|
$builder->createSqlCommand("DELETE FROM `{{product_param_name}}` WHERE id > 2")->execute();
|
106
|
|
|
$builder->createSqlCommand("ALTER TABLE `{{product_param_name}}` AUTO_INCREMENT = 3")->execute();
|
107
|
|
|
}
|
108
|
|
|
} |