Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
IacSQLMultipleInsert
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 15
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 valuesArray
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 valuesString
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 insertNow
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 get_valuesClauseNum
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_valuesCurrentNum
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_insertsOkNum
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_insertsErrorsNum
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_insertLast
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_insertLastOk
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_currentLength
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_insertIntoLength
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_simulate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_simulate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_allValues
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Iac\inc\sql;
3
4// @version 1.1
5// @TODO doc
6// @TODO hacer algo para coordinar inserts en varias tablas?
7// min php  5.2.0
8
9class IacSQLMultipleInsert {
10    protected $db;
11    protected $insertIntoClause;
12    protected $values = [];
13    protected $valuesNum = 0;
14    protected $insertsOk = 0;
15    protected $insertsErrors = 0;
16    protected $onDuplicateClause;
17    protected $currentLength;
18    protected $baseLength;
19    protected $maxLength;
20    protected $sqlBuilder;
21    protected $insertLast = '';
22    protected $insertLastOk = true;
23    protected $simulate = false; //Modificado por VCA
24    protected $allValues = []; //Modificado por VCA
25
26    public function __construct($db, $insertIntoClause, $onDuplicateClause = '', $maxLength = 5000, $simulate = false) {
27        $this->db = $db;
28        $this->insertIntoClause = $insertIntoClause;
29        $this->onDuplicateClause = $onDuplicateClause;
30        $this->currentLength = $this->baseLength = strlen($insertIntoClause) + strlen($onDuplicateClause) + 9;
31        $this->maxLength = $maxLength;
32        $this->simulate = $simulate;
33        $this->sqlBuilder = new IacSqlBuilder();
34    }
35
36    public function valuesArray($values, $fieldNameDontQuote=array()) {
37        return $this->valuesString( $this->sqlBuilder->insertValues($values, $fieldNameDontQuote) );
38    }
39
40    public function valuesString($valueString) {
41        $ok = null;
42        $len = strlen($valueString);
43        if($this->currentLength + $len >= $this->maxLength) {
44            $ok = $this->insertNow();
45        }
46        $this->currentLength += $len + 1;
47        $this->values[] = $valueString;
48        $this->valuesNum++;
49        return $ok;
50    }
51
52    public function insertNow() {
53        if(empty($this->values)) {
54            $this->currentLength = $this->baseLength;
55            $this->values = [];
56            return true;
57        }
58//        dd_($this->values);
59        $this->insertLast = $this->insertIntoClause . ' VALUES ' . implode(', ', $this->values) . ' ' . $this->onDuplicateClause;
60
61        //Modificado por VCA para que sólo regrese un array de INSERTS
62        if($this->simulate)
63        {
64            $this->allValues[]=$this->insertLast;
65            $this->insertLastOk = true;
66        }
67        else
68            $this->insertLastOk = $this->db->query($this->insertLast);
69
70        if($this->insertLastOk) {
71            $this->insertsOk++;
72        } else {
73            $this->insertsErrors++;
74        }
75        $this->currentLength = $this->baseLength;
76        $this->values = [];
77        return $this->insertLastOk;
78    }
79
80    // get info ________________________________________________________________________________________________________________
81        public function get_valuesClauseNum() {return $this->valuesNum;}
82
83        public function get_valuesCurrentNum() {return count($this->values);}
84
85        public function get_insertsOkNum() {return $this->insertsOk;}
86
87        public function get_insertsErrorsNum() {return $this->insertsErrors;}
88
89        public function get_insertLast() {return $this->insertLast;}
90
91        public function get_insertLastOk() {return $this->insertLastOk;}
92
93        public function get_currentLength() {return $this->currentLength;}
94
95        public function get_insertIntoLength() {return $this->baseLength;}
96
97        public function get_simulate() {return $this->simulate;}
98
99        public function set_simulate($simulate) {$this->simulate = $simulate; return $this->simulate;}
100
101        public function get_allValues() {return $this->allValues;}
102
103}