Completed
Push — master ( 499e80...f97bb0 )
by Jean-Christophe
01:49
created

DataExport::batchRows()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Ubiquity\db\export;
4
5
use Ubiquity\db\SqlUtils;
6
7
class DataExport {
8
	protected $batchSize;
9
10
	public function __construct($batchSize=20){
11
		$this->batchSize=$batchSize;
12
	}
13
14
15
16
	protected function generateInsert($table,$fields,$datas){
17
		$result=[];
18
		$batchRows=[];
19
		$rows=[];
20
		$batch=0;
21
		foreach ($datas as $row){
22
			if($batch<$this->batchSize){
23
				$rows[]=$row;
24
				$batch++;
25
			}else{
26
				$batchRows[]=$this->batchRows($rows,$fields);
27
				$batch=0;
28
				$rows=[];
29
			}
30
		}
31
		if(\sizeof($rows)>0){
32
			$batchRows[]=$this->batchRows($rows,$fields);
33
		}
34
		foreach ($batchRows as $batchRow){
35
			$result[]=$this->generateOneInsert($table, $fields, $batchRow);
36
		}
37
		return \implode(";\n", $result).";";
38
	}
39
40
	protected function generateOneInsert($table,$fields,$datas){
41
		return "INSERT INTO `".$table."` (".SqlUtils::getFieldList($fields).") VALUES ".$datas;
42
	}
43
44 View Code Duplication
	protected function batchRows($rows,$fields){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
		$result=[];
46
		foreach ($rows as $row){
47
			$result[]="(".$this->batchOneRow($row,$fields).")";
48
		}
49
		return \implode(",", $result);
50
	}
51
52 View Code Duplication
	protected function batchOneRow($row,$fields){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
		$result=[];
54
		foreach ($fields as $field){
55
			$result[]="'".$row->_rest[$field]."'";
56
		}
57
		return \implode(",", $result);
58
	}
59
}