示例#1
0
文件: DBUtil.php 项目: rmaiwald/core
 /**
  * Generate and execute an update SQL statement for the given object.
  *
  * @param array   &$object  The object we wish to update.
  * @param string  $table    The treated table reference.
  * @param string  $where    The where clause (optional) (default='').
  * @param string  $idfield  The column which stores the primary key (optional) (default='id').
  * @param boolean $force    Whether or not to insert empty values as NULL (optional) (default=false).
  * @param boolean $updateid Allow primary key to be updated (default=false).
  *
  * @return integer The result set from the update operation
  * @deprecated
  * @see    Doctrine_Record::save()
  * @throws Exception If parameters not set or column or column_def not in array.
  */
 public static function updateObject(array &$object, $table, $where = '', $idfield = 'id', $force = false, $updateid = false)
 {
     if (!isset($object[$idfield]) && !$where) {
         throw new Exception(__('Neither object ID nor where parameters are provided'));
     }
     $tables = self::getTables();
     $tableName = $tables[$table];
     $sql = "UPDATE {$tableName} SET ";
     // set standard architecture fields
     ObjectUtil::setStandardFieldsOnObjectUpdate($object, $force);
     // build the column list
     $columnList = $tables["{$table}_column"];
     if (!is_array($columnList)) {
         throw new Exception(__f('%s_column is not an array', $table));
     }
     // build the column definition list
     $columnDefList = $tables["{$table}_column_def"];
     if (!is_array($columnDefList)) {
         throw new Exception(__f('%s_column_def is not an array', $table));
     }
     // grab each key and value and append to the sql string
     $tArray = array();
     $search = array('+', '-', '*', '/', '%');
     $replace = array('');
     foreach ($columnList as $key => $val) {
         $hasMath = (bool) strcmp($val, str_replace($search, $replace, $val));
         if ($hasMath) {
             continue;
         }
         if ($key != $idfield || $key == $idfield && $updateid == true) {
             if ($force || array_key_exists($key, $object)) {
                 $skip = false;
                 $columnDefinition = $columnDefList[$key];
                 $columnDefFields = explode(' ', $columnDefinition);
                 $colType = substr($columnDefinition, 0, 1);
                 // ensure that international float numbers are stored with '.' rather than ',' for decimal separator
                 if ($colType == 'F' || $colType == 'N') {
                     if (is_float($object[$key]) || is_double($object[$key])) {
                         $object[$key] = number_format($object[$key], 8, '.', '');
                     }
                 }
                 // generate the actual update values
                 if (!$skip) {
                     $dbDriverName = strtolower(Doctrine_Manager::getInstance()->getCurrentConnection()->getDriverName());
                     if (isset($object[$key]) && ($dbDriverName == 'derby' || $dbDriverName == 'splice' || $dbDriverName == 'jdbcbridge') && (strtoupper($columnDefFields[0]) != 'XL' || strtoupper($columnDefFields[0]) != 'B') && strlen($object[$key]) > 32000) {
                         $chunks = str_split($object[$key], 32000);
                         $str = '';
                         foreach ($chunks as $chunk) {
                             if ($str) {
                                 $str .= ' || ';
                             }
                             $str .= "CAST (" . self::_formatForStore($chunk) . " AS CLOB)";
                         }
                         $tArray[] = "{$val}={$str}";
                     } else {
                         $tArray[] = "{$val}=" . (isset($object[$key]) ? self::_typesafeQuotedValue($table, $key, $object[$key]) : 'NULL');
                     }
                 }
             }
         }
     }
     if ($tArray) {
         if (!$where) {
             $_where = " WHERE {$columnList[$idfield]} = " . self::_typesafeQuotedValue($table, $idfield, $object[$idfield]);
         } else {
             $_where = self::_checkWhereClause($where);
         }
         $sql .= implode(',', $tArray) . ' ' . $_where;
         $res = self::executeSQL($sql);
         if ($res === false) {
             return $res;
         }
     }
     self::flushCache($table);
     $object = self::_savePostProcess($object, $table, $idfield, true);
     return $object;
 }
示例#2
0
 /**
  * Generate and execute an update SQL statement for the given object.
  *
  * @param array   &$object  The object we wish to update.
  * @param string  $table    The treated table reference.
  * @param string  $where    The where clause (optional) (default='').
  * @param string  $idfield  The column which stores the primary key (optional) (default='id').
  * @param boolean $force    Whether or not to insert empty values as NULL (optional) (default=false).
  * @param boolean $updateid Allow primary key to be updated (default=false).
  *
  * @return integer The result set from the update operation
  * @deprecated
  * @see    Doctrine_Record::save()
  * @throws Exception If parameters not set or column or column_def not in array.
  */
 public static function updateObject(array &$object, $table, $where = '', $idfield = 'id', $force = false, $updateid = false)
 {
     if (!isset($object[$idfield]) && !$where) {
         throw new Exception(__('Neither object ID nor where parameters are provided'));
     }
     $tables = self::getTables();
     $tableName = $tables[$table];
     $sql = "UPDATE {$tableName} SET ";
     // set standard architecture fields
     ObjectUtil::setStandardFieldsOnObjectUpdate($object, $force);
     // build the column list
     $columnList = $tables["{$table}_column"];
     if (!is_array($columnList)) {
         throw new Exception(__f('%s_column is not an array', $table));
     }
     // build the column definition list
     $columnDefList = $tables["{$table}_column_def"];
     if (!is_array($columnDefList)) {
         throw new Exception(__f('%s_column_def is not an array', $table));
     }
     // grab each key and value and append to the sql string
     $tArray = array();
     $search = array('+', '-', '*', '/', '%');
     $replace = array('');
     foreach ($columnList as $key => $val) {
         $hasMath = (bool) strcmp($val, str_replace($search, $replace, $val));
         if ($hasMath) {
             continue;
         }
         if ($key != $idfield || $key == $idfield && $updateid == true) {
             if ($force || array_key_exists($key, $object)) {
                 $skip = false;
                 $columnDefinition = $columnDefList[$key];
                 $colType = substr($columnDefinition, 0, 1);
                 // ensure that international float numbers are stored with '.' rather than ',' for decimal separator
                 if ($colType == 'F' || $colType == 'N') {
                     if (is_float($object[$key]) || is_double($object[$key])) {
                         $object[$key] = number_format($object[$key], 8, '.', '');
                     }
                 }
                 // generate the actual update values
                 if (!$skip) {
                     $tArray[] = "{$val}=" . (isset($object[$key]) ? self::_formatForStore($object[$key]) : 'NULL');
                 }
             }
         }
     }
     if ($tArray) {
         if (!$where) {
             $_where = " WHERE {$columnList[$idfield]} = '" . DataUtil::formatForStore($object[$idfield]) . "'";
         } else {
             $_where = self::_checkWhereClause($where);
         }
         $sql .= implode(',', $tArray) . ' ' . $_where;
         $res = self::executeSQL($sql);
         if ($res === false) {
             return $res;
         }
     }
     self::flushCache($table);
     $object = self::_savePostProcess($object, $table, $idfield, true);
     return $object;
 }