Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
tmptable.php
1<?
3
6
12final class TmpTable
13{
14 protected $name = 'b_sale_loc_map_tmp';
15 protected $connection = null;
16 protected $serviceId = 0;
17
24 public function __construct($serviceId, $tableName = "")
25 {
26 if(intval($serviceId) <= 0)
27 throw new ArgumentNullException('serviceId');
28
29 if($tableName <> '')
30 $this->name = $tableName;
31
32 $this->serviceId = intval($serviceId);
33 $this->connection = \Bitrix\Main\Application::getConnection();
34 }
35
40 public function getUnmappedLocations($startId = 0)
41 {
42 $query = "
43 SELECT
44 TMP.*
45 FROM
46 ".$this->name." AS TMP
47 WHERE
48 TMP.LOCATION_ID IS NULL
49 ";
50
51 if(intval($startId) > 0)
52 $query .= " AND TMP.ID > ".intval($startId);
53
54 $query .= " ORDER BY ID ASC";
55 return $this->connection->query($query);
56 }
57
63 public function markMapped($locationId, $xmlId)
64 {
65 if(intval($locationId) <= 0)
66 throw new ArgumentNullException('locationId');
67
68 if($xmlId == '')
69 throw new ArgumentNullException('xmlId');
70
71 $sqlHelper = $this->connection->getSqlHelper();
72
73 $this->connection->queryExecute("
74 UPDATE
75 ".$this->name."
76 SET
77 LOCATION_ID=".intval($locationId)."
78 WHERE
79 XML_ID = '".$sqlHelper->forSql($xmlId)."'"
80 );
81 }
82
86 public function markAllMapped()
87 {
88 set_time_limit(0);
89
90 $this->connection->queryExecute("
91 UPDATE
92 ".$this->name." AS TMP
93 INNER JOIN
94 b_sale_loc_ext AS E ON TMP.XML_ID = E.XML_ID AND E.SERVICE_ID = ".$this->serviceId."
95 SET
96 TMP.LOCATION_ID = E.LOCATION_ID
97 ");
98 }
99
106 public function create(array $data)
107 {
108 if(empty($data))
109 throw new ArgumentNullException('data');
110
111 if(!is_array(current($data)))
112 throw new ArgumentTypeException('current(data)', 'array');
113
114 $sqlHelper = $this->connection->getSqlHelper();
115 $cols = '';
116
117 foreach(current($data) as $key => $val)
118 $cols .= $sqlHelper->forSql($key)." VARCHAR(255) NULL,\n";
119
120 return $this->connection->queryExecute('
121 CREATE TABLE '.$this->name.' (
122 ID INT NOT NULL AUTO_INCREMENT,
123 XML_ID VARCHAR (100) NOT NULL,
124 '.$cols.'
125 LOCATION_ID INT NULL,
126 PRIMARY KEY (ID)
127 )'
128 );
129 }
130
134 public function drop()
135 {
136 $this->connection->queryExecute('DROP TABLE '.$this->name);
137 }
138
142 public function isExist()
143 {
144 return $this->connection->isTableExists($this->name);
145 }
146
153 public function saveData(array $data)
154 {
155 if(empty($data))
156 return 0;
157
158 set_time_limit(0);
159
160 $sqlHelper = $this->connection->getSqlHelper();
161 $queryBegin = '';
162
163 foreach(current($data) as $key => $val)
164 {
165 if($queryBegin <> '')
166 $queryBegin .= ', ';
167
168 $queryBegin .= $sqlHelper->forSql($key);
169 }
170
171 $queryBegin .= ', XML_ID';
172 $queryBegin = "INSERT INTO ".$this->name."(".$queryBegin.") VALUES ";
173 $imported = 0;
174 $i = 0;
175 $values = '';
176 $INSERT_BLOCK_SIZE = 100;
177
178 foreach($data as $xmlId => $row)
179 {
180 if($values <> '')
181 $values .= ', ';
182
183 $rowValues = '';
184
185 foreach($row as $col)
186 {
187 if($rowValues <> '')
188 $rowValues .= ', ';
189
190 $rowValues .= "'".$sqlHelper->forSql($col)."'";
191 }
192
193 $values .= "(".$rowValues.", '".$sqlHelper->forSql($xmlId)."')";
194
195 if($i >= $INSERT_BLOCK_SIZE)
196 {
197 $this->connection->queryExecute($queryBegin.$values);
198 $i = 0;
199 $values = '';
200 }
201
202 $i++;
203 $imported++;
204 }
205
206 if($values <> '')
207 $this->connection->queryExecute($queryBegin.$values);
208
209 $this->connection->queryExecute("CREATE INDEX IX_BSDTMP_XML_ID ON ".$this->name." (XML_ID)");
210 return $imported;
211 }
212
216 public function getMaxId()
217 {
218 $result = 0;
219 $res = $this->connection->query("SELECT MAX(ID) AS MAX FROM ".$this->name." WHERE LOCATION_ID IS NULL");
220
221 if($loc = $res->fetch())
222 $result = $loc['MAX'];
223
224 return $result;
225 }
226}
__construct($serviceId, $tableName="")
Definition tmptable.php:24