library/database/class.sqldriver.php
public function Replace($Table = '', $Set = NULL, $Where, $CheckExisting = FALSE) {
      if(count($this->_Sets) > 0) {
         foreach($this->_Sets as $Key => $Value) {
            if(array_key_exists($Value, $this->_NamedParameters)) {
               $Set[$Key] = $this->_NamedParameters[$Value];
               unset($this->_NamedParameters[$Value]);
            } else {
               $Set[$Key] = $Value;
            }
         }
         $this->_Sets = array();
      }
      
      // Check to see if there is a row in the table like this.
      if ($CheckExisting) {
         $Row = $this->GetWhere($Table, $Where)->FirstRow(DATASET_TYPE_ARRAY);

         $Update = FALSE;
         if ($Row) {
            $Update = TRUE;
            foreach ($Set as $Key => $Value) {
               unset($Set[$Key]);
               $Key = trim($Key, '`');
               
               if (!$this->CaptureModifications && !array_key_exists($Key,$Row))
                  continue;

               if (in_array($Key, array('DateInserted', 'InsertUserID', 'DateUpdated', 'UpdateUserID')))
                  continue;


               // We are assuming here that if the existing record doesn't contain the column then it's just been added.
               if (preg_match('/^`(.+)`$/', $Value, $Matches)) {
                  if (!array_key_exists($Key, $Row) || $Row[$Key] != $Row[$Matches[1]])
                     $this->Set('`'.$Key.'`', $Value, FALSE);
               } elseif (!array_key_exists($Key, $Row) || $Row[$Key] != $Value) {
                  $this->Set('`'.$Key.'`', $Value);
               }
               
            }
            if (count($this->_Sets) == 0) {
               $this->Reset();
               return;
            }
         }
      } else {
         $Count = $this->GetCount($Table, $Where);
         $Update = $Count > 0;
      }

      if($Update) {
         // Update the table.
         $this->Put($Table, $Set, $Where);
      } else {
         // Insert the table.
         $Set = array_merge($Set, $Where);
         $this->Insert($Table, $Set);
      }
   }
public function Replace($Table = '', $Set = NULL, $Where, $CheckExisting = FALSE) {
      if(count($this->_Sets) > 0) {
         foreach($this->_Sets as $Key => $Value) {
            if(array_key_exists($Value, $this->_NamedParameters)) {
               $Set[$Key] = $this->_NamedParameters[$Value];
               unset($this->_NamedParameters[$Value]);
            } else {
               $Set[$Key] = $Value;
            }
         }
         $this->_Sets = array();
      }
      
      // Check to see if there is a row in the table like this.
      if ($CheckExisting) {
         $Row = $this->GetWhere($Table, $Where)->FirstRow(DATASET_TYPE_ARRAY);

         $Update = FALSE;
         if ($Row) {
            $Update = TRUE;
            foreach ($Set as $Key => $Value) {
               unset($Set[$Key]);
               $Key = trim($Key, '`');
               
               if (!$this->CaptureModifications && !array_key_exists($Key,$Row))
                  continue;

               if (in_array($Key, array('DateInserted', 'InsertUserID', 'DateUpdated', 'UpdateUserID')))
                  continue;


               // We are assuming here that if the existing record doesn't contain the column then it's just been added.
               if (preg_match('/^`(.+)`$/', $Value, $Matches)) {
                  if (!array_key_exists($Key, $Row) || $Row[$Key] != $Row[$Matches[1]])
                     $this->Set('`'.$Key.'`', $Value, FALSE);
               } elseif (!array_key_exists($Key, $Row) || $Row[$Key] != $Value) {
                  $this->Set('`'.$Key.'`', $Value);
               }
               
            }
            if (count($this->_Sets) == 0) {
               $this->Reset();
               return;
            }
         }
      } else {
         $Count = $this->GetCount($Table, $Where);
         $Update = $Count > 0;
      }

      if($Update) {
         // Update the table.
         $this->Put($Table, $Set, $Where);
      } else {
         // Insert the table.
         $Set = array_merge($Set, $Where);
         $this->Insert($Table, $Set);
      }
   }
public function Replace ( $ Table = '' , $ Set = NULL , $ Where , $ CheckExisting = FALSE ) { if ( count ( $ this -> _Sets ) > 0 ) { foreach ( $ this -> _Sets as $ Key => $ Value ) { if ( array_key_exists ( $ Value , $ this -> _NamedParameters ) ) { $ Set [ $ Key ] = $ this -> _NamedParameters [ $ Value ] ; unset ( $ this -> _NamedParameters [ $ Value ] ) ; } else { $ Set [ $ Key ] = $ Value ; } } $ this -> _Sets = array ( ) ; } // Check to see if there is a row in the table like this. if ( $ CheckExisting ) { $ Row = $ this -> GetWhere ( $ Table , $ Where ) -> FirstRow ( DATASET_TYPE_ARRAY ) ; $ Update = FALSE ; if ( $ Row ) { $ Update = TRUE ; foreach ( $ Set as $ Key => $ Value ) { unset ( $ Set [ $ Key ] ) ; $ Key = trim ( $ Key , '`' ) ; if ( ! $ this -> CaptureModifications && ! array_key_exists ( $ Key , $ Row ) ) continue ; if ( in_array ( $ Key , array ( 'DateInserted' , 'InsertUserID' , 'DateUpdated' , 'UpdateUserID' ) ) ) continue ; // We are assuming here that if the existing record doesn't contain the column then it's just been added. if ( preg_match ( '/^`(.+)`$/' , $ Value , $ Matches ) ) { if ( ! array_key_exists ( $ Key , $ Row ) || $ Row [ $ Key ] != $ Row [ $ Matches [ 1 ] ] ) $ this -> Set ( '`' . $ Key . '`' , $ Value , FALSE ) ; } elseif ( ! array_key_exists ( $ Key , $ Row ) || $ Row [ $ Key ] != $ Value ) { $ this -> Set ( '`' . $ Key . '`' , $ Value ) ; } } if ( count ( $ this -> _Sets ) == 0 ) { $ this -> Reset ( ) ; return ; } } } else { $ Count = $ this -> GetCount ( $ Table , $ Where ) ; $ Update = $ Count > 0 ; } if ( $ Update ) { // Update the table. $ this -> Put ( $ Table , $ Set , $ Where ) ; } else { // Insert the table. $ Set = array_merge ( $ Set , $ Where ) ; $ this -> Insert ( $ Table , $ Set ) ; } }
Inserts or updates values in the table depending on whether they are already there.

@param string $Table The name of the table to insert/update.
@param array $Set The columns to update.
@param array $Where The columns to find the row to update.
If a row is not found then one is inserted and the items in this array are merged with $Set.
Inserts or updates values in the table depending on whether they are already there .