Example #1
0
 /**
  * Update_Batch
  *
  * Compiles an update string and runs the query
  *
  * @param    array     an associative array of update values
  * @param    string    the where key
  * @param    int       The size of the batch to run
  * @param    bool      true means SQL is returned, false will execute the query
  *
  * @return    int    number of rows affected or FALSE on failure
  */
 public function updateBatch($set = null, $index = null, $batch_size = 100, $returnSQL = false)
 {
     if ($index === null) {
         if (CI_DEBUG) {
             throw new DatabaseException('You must specify an index to match on for batch updates.');
         }
         return false;
     }
     if ($set === null) {
         if (empty($this->QBSet)) {
             if (CI_DEBUG) {
                 throw new DatabaseException('You must use the "set" method to update an entry.');
             }
             return false;
         }
     } else {
         if (empty($set)) {
             if (CI_DEBUG) {
                 throw new DatabaseException('updateBatch() called with no data');
             }
             return false;
         }
         $this->setUpdateBatch($set, $index);
     }
     $table = $this->QBFrom[0];
     // Batch this baby
     $affected_rows = 0;
     $savedSQL = [];
     for ($i = 0, $total = count($this->QBSet); $i < $total; $i += $batch_size) {
         $sql = $this->_updateBatch($table, array_slice($this->QBSet, $i, $batch_size), $this->db->protectIdentifiers($index));
         if ($returnSQL) {
             $savedSQL[] = $sql;
         } else {
             $this->db->query($sql, $this->binds);
             $affected_rows += $this->db->affectedRows();
         }
         $this->QBWhere = [];
     }
     $this->resetWrite();
     return $returnSQL ? $savedSQL : $affected_rows;
 }