Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 37 |
| MultipleInsert | |
0.00% |
0 / 1 |
|
0.00% |
0 / 5 |
506.00 | |
0.00% |
0 / 37 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 8 |
|||
| valuesArray | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| insertAllArrayOfValues | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 3 |
|||
| valuesString | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 8 |
|||
| insertNow | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 17 |
|||
| get_valuesClauseNum | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_valuesCurrentNum | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_insertsOkNum | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_insertsErrorsNum | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_insertLast | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_insertLastOk | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_currentLength | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_insertIntoLength | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_simulate | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| set_simulate | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| get_allInserts | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
| <?php | |
| namespace ia\Sql\Mysql; | |
| /* | |
| * changelog | |
| * 2018-05-10 Victor, added: $simulate, en vez de hacer insert guarda en array allInserts, para tomarlo despues y correr juntos los n inserts | |
| * 2018-05-21 added insertAllArrayOfValues | |
| */ | |
| // @version 1.3 | |
| // @TODO doc | |
| // @TODO hacer algo para coordinar inserts en varias tablas? | |
| // min php 5.2.0 | |
| class MultipleInsert { | |
| /** @var IaMysqli $db */ | |
| protected $db; | |
| protected $insertIntoClause; | |
| protected $values = []; | |
| protected $valuesNum = 0; | |
| protected $insertsOk = 0; | |
| protected $insertsErrors = 0; | |
| protected $onDuplicateClause; | |
| protected $currentLength; | |
| protected $baseLength; | |
| protected $maxLength; | |
| protected $sqlBuilder; | |
| protected $insertLast = ''; | |
| protected $insertLastOk = true; | |
| protected $simulate = false; //Modificado por VCA | |
| protected $allInserts = []; //Modificado por VCA | |
| /** | |
| * MultipleInsert::__construct() | |
| * | |
| * @param IaMysqli $db | |
| * @param string $insertIntoClause | |
| * @param string $onDuplicateClause | |
| * @param integer $maxLength | |
| * @param bool $simulate | |
| * @return | |
| */ | |
| public function __construct($db, $insertIntoClause, $onDuplicateClause = '', $maxLength = 5000, $simulate = false) { | |
| $this->db = $db; | |
| $this->insertIntoClause = $insertIntoClause; | |
| $this->onDuplicateClause = $onDuplicateClause; | |
| $this->currentLength = $this->baseLength = strlen($insertIntoClause) + strlen($onDuplicateClause) + 9; | |
| $this->maxLength = $maxLength; | |
| $this->simulate = $simulate; | |
| $this->sqlBuilder = new SqlBuilder(); | |
| } | |
| /** | |
| * Add 1 value clause from an array ['fieldName'=>value] | |
| * | |
| * @param array $values | |
| * @param array $fieldNameDontQuote | |
| * @return bool | |
| */ | |
| public function valuesArray($values, $fieldNameDontQuote=array()) { | |
| return $this->valuesString( $this->sqlBuilder->insertValues($values, $fieldNameDontQuote) ); | |
| } | |
| /** | |
| * An array of values to insert [[field1=>1,field2=>2,...],[field1=>11,field2=>12,...],...] | |
| * | |
| * @param array $array | |
| * @param array $fieldNameDontQuote | |
| * @return bool | |
| */ | |
| public function insertAllArrayOfValues($array, $fieldNameDontQuote=array()) { | |
| foreach($array as $values) { | |
| $this->valuesString( $this->sqlBuilder->insertValues($values, $fieldNameDontQuote) ); | |
| } | |
| return $this->insertNow(); | |
| } | |
| /** | |
| * MultipleInsert::valuesString() | |
| * | |
| * @param string $valueString | |
| * @return bool | |
| */ | |
| public function valuesString($valueString) { | |
| $ok = null; | |
| $len = strlen($valueString); | |
| if($this->currentLength + $len >= $this->maxLength) { | |
| $ok = $this->insertNow(); | |
| } | |
| $this->currentLength += $len + 1; | |
| $this->values[] = $valueString; | |
| $this->valuesNum++; | |
| return $ok; | |
| } | |
| /** | |
| * Inserts all values received | |
| * | |
| * @return | |
| */ | |
| public function insertNow() { | |
| if(empty($this->values)) { | |
| $this->currentLength = $this->baseLength; | |
| $this->values = []; | |
| return true; | |
| } | |
| $this->insertLast = $this->insertIntoClause . ' VALUES' . implode(',', $this->values) . ' ' . $this->onDuplicateClause; | |
| //Modificado por VCA para que sólo regrese un array de INSERTS | |
| if($this->simulate) { | |
| $this->allInserts[]=$this->insertLast; | |
| $this->insertLastOk = true; | |
| } else { | |
| try { | |
| $this->insertLastOk = $this->db->query($this->insertLast); | |
| } catch (IacSqlException $e) { | |
| $this->insertLastOk = false; //@TODO @DUDA or throw exception? | |
| } | |
| } | |
| if($this->insertLastOk) { | |
| $this->insertsOk++; | |
| } else { | |
| $this->insertsErrors++; | |
| } | |
| $this->currentLength = $this->baseLength; | |
| $this->values = []; | |
| return $this->insertLastOk; | |
| } | |
| // get info ________________________________________________________________________________________________________________ | |
| /** | |
| * MultipleInsert::get_valuesClauseNum() | |
| * | |
| * @return int | |
| */ | |
| public function get_valuesClauseNum() {return $this->valuesNum;} | |
| /** | |
| * MultipleInsert::get_valuesCurrentNum() | |
| * | |
| * @return int | |
| */ | |
| public function get_valuesCurrentNum() {return count($this->values);} | |
| /** | |
| * MultipleInsert::get_insertsOkNum() | |
| * | |
| * @return int | |
| */ | |
| public function get_insertsOkNum() {return $this->insertsOk;} | |
| /** | |
| * MultipleInsert::get_insertsErrorsNum() | |
| * | |
| * @return int | |
| */ | |
| public function get_insertsErrorsNum() {return $this->insertsErrors;} | |
| /** | |
| * MultipleInsert::get_insertLast() | |
| * | |
| * @return string | |
| */ | |
| public function get_insertLast() {return $this->insertLast;} | |
| /** | |
| * MultipleInsert::get_insertLastOk() | |
| * | |
| * @return string | |
| */ | |
| public function get_insertLastOk() {return $this->insertLastOk;} | |
| /** | |
| * MultipleInsert::get_currentLength() | |
| * | |
| * @return int | |
| */ | |
| public function get_currentLength() {return $this->currentLength;} | |
| /** | |
| * MultipleInsert::get_insertIntoLength() | |
| * | |
| * @return int | |
| */ | |
| public function get_insertIntoLength() {return $this->baseLength;} | |
| /** | |
| * MultipleInsert::get_simulate() | |
| * | |
| * @return bool | |
| */ | |
| public function get_simulate() {return $this->simulate;} | |
| /** | |
| * MultipleInsert::set_simulate() | |
| * | |
| * @param bool $simulate | |
| * @return bool | |
| */ | |
| public function set_simulate($simulate) {$this->simulate = $simulate; return $this->simulate;} | |
| /** | |
| * MultipleInsert::get_allInserts() | |
| * | |
| * @return array | |
| */ | |
| public function get_allInserts() {return $this->allInserts;} | |
| } |