forked from ruskid/yii2-csv-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultipleImportStrategy.php
155 lines (135 loc) · 4.59 KB
/
MultipleImportStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* @copyright Copyright Victor Demin, 2015
* @license https://github.com/ruskid/yii2-csv-importer/LICENSE
* @link https://github.com/ruskid/yii2-csv-importer#README
*/
namespace ruskid\csvimporter;
use yii\base\Exception;
use ruskid\csvimporter\ImportInterface;
use ruskid\csvimporter\BaseImportStrategy;
/**
* Will import from CSV. This will batch insert the rows, no validation is performed.
* This is the fastest way to insert big amounts of data.
*
* @author Victor Demin <[email protected]>
*/
class MultipleImportStrategy extends BaseImportStrategy implements ImportInterface {
/**
* Table name where to import data
* @var string
*/
public $tableName;
/**
* Limit items per single insert query.
* On batch insert the single query can become huge and you can get mysql exception:
* "Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes".
*
* This parameter will divide array of values in chunks and then execute multiple
* insert queries. If you don't want this limit, set it to 0 (not recommended).
* @var integer
*/
public $maxItemsPerInsert = 10000;
/**
* @throws Exception
*/
public function __construct() {
$arguments = func_get_args();
if (!empty($arguments)) {
foreach ($arguments[0] as $key => $property) {
if (property_exists($this, $key)) {
$this->{$key} = $property;
}
}
}
if ($this->tableName === null) {
throw new Exception(__CLASS__ . ' tableName is required.');
}
if ($this->configs === null) {
throw new Exception(__CLASS__ . ' configs is required.');
}
}
/**
* Will multiple import data into table
* @param array $data CSV data passed by reference to save memory.
* @return integer number of rows affected
*/
public function import(&$data) {
$attributes = $this->getAttributes();
$values = $this->getValues($data);
print_r($values);exit();
$countInserts = 0;
$chunks = array_chunk($values, $this->maxItemsPerInsert);
foreach ($chunks as $chunk) {//Execute multiple queries
for ($i = 0; $i <= count($chunk); $i++) {
//try {
print_r($chunk[$i]);
$countInserts += \Yii::$app->db->createCommand()
->upsert($this->tableName, $chunk[$i])->execute();
/*$db = \Yii::$app->db;
$sql = $db->queryBuilder->batchInsert($this->tableName, $attributes, $chunk);
echo $sql;
$countInserts += $db->createCommand($sql . ' ON DUPLICATE KEY UPDATE')->execute();*/
//} catch(PDOException $e) {}
}
}
return $countInserts;
}
/**
* Will get attribute list from the config
* @return array
*/
private function getAttributes() {
$attributes = [];
foreach ($this->configs as $config) {
$attributes[] = $config['attribute'];
}
return $attributes;
}
/**
* Will get value list from the config
* @param array $data CSV data
* @return array
*/
private function getValues(&$data) {
$values = [];
foreach ($data as $i => $row) {
$skipImport = isset($this->skipImport) ? call_user_func($this->skipImport, $row) : false;
if (!$skipImport) {
foreach ($this->configs as $config) {
$value = call_user_func($config['value'], $row);
$values[$i][$config['attribute']] = $value;
}
}
}
//Filter unique values by unique attributes
$values = $this->filterUniqueValues($values);
return $values;
}
/**
* Will filter values per unique parameters. Config array can receive 1+ unique parameters.
* @param array $values
*/
private function filterUniqueValues($values) {
//Get unique attributes
$uniqueAttributes = [];
foreach ($this->configs as $config) {
if (isset($config['unique']) && $config['unique']) {
$uniqueAttributes[] = $config['attribute'];
}
}
if (empty($uniqueAttributes)) {
return $values; //Return all values
}
//Filter values per unique attributes
$uniqueValues = [];
foreach ($values as $value) {
$hash = ""; //generate hash per 1+ unique parameters
foreach ($uniqueAttributes as $ua) {
$hash .= $value[$ua];
}
$uniqueValues[$hash] = $value;
}
return $uniqueValues;
}
}