Ubiquity  2.0.0
php rapid development framework
DataExport.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
7 class DataExport {
8  protected $batchSize;
9 
10  public function __construct($batchSize=20) {
11  $this->batchSize=$batchSize;
12  }
13 
14  protected function generateInsert($table, $fields, $datas) {
15  $result=[ ];
16  $batchRows=[ ];
17  $rows=[ ];
18  $batch=0;
19  foreach ( $datas as $row ) {
20  if ($batch < $this->batchSize) {
21  $rows[]=$row;
22  $batch++;
23  } else {
24  $batchRows[]=$this->batchRows($rows, $fields);
25  $batch=0;
26  $rows=[ ];
27  }
28  }
29  if (\sizeof($rows) > 0) {
30  $batchRows[]=$this->batchRows($rows, $fields);
31  }
32  foreach ( $batchRows as $batchRow ) {
33  $result[]=$this->generateOneInsert($table, $fields, $batchRow);
34  }
35  return \implode(";\n", $result) . ";";
36  }
37 
38  protected function generateOneInsert($table, $fields, $datas) {
39  return "INSERT INTO `" . $table . "` (" . SqlUtils::getFieldList($fields) . ") VALUES " . $datas;
40  }
41 
42  protected function batchRows($rows, $fields) {
43  $result=[ ];
44  foreach ( $rows as $row ) {
45  $result[]="(" . $this->batchOneRow($row, $fields) . ")";
46  }
47  return \implode(",", $result);
48  }
49 
50  protected function batchOneRow($row, $fields) {
51  $result=[ ];
52  foreach ( $fields as $field ) {
53  $result[]="'" . $row->_rest[$field] . "'";
54  }
55  return \implode(",", $result);
56  }
57 }
static getFieldList($fields)
Definition: SqlUtils.php:89
generateInsert($table, $fields, $datas)
Definition: DataExport.php:14
generateOneInsert($table, $fields, $datas)
Definition: DataExport.php:38