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