diff options
| author | peterdd <ddpm@liscovius.de> | 2020-01-09 01:49:08 +0100 |
|---|---|---|
| committer | Mark Newnham <mark@newnhams.com> | 2020-01-08 17:49:08 -0700 |
| commit | cd60c508d6b55efa00d39579791088c152749aeb (patch) | |
| tree | 6c806387959fab46bbe4fd6873d5e48d65baaba4 /adodb-lib.inc.php | |
| parent | 9f026d00040582cfc48de633aa65325678b37935 (diff) | |
| download | adodb-cd60c508d6b55efa00d39579791088c152749aeb.tar.gz adodb-cd60c508d6b55efa00d39579791088c152749aeb.tar.bz2 adodb-cd60c508d6b55efa00d39579791088c152749aeb.zip | |
fix #572, also quote fieldnames when value set null, fix indentations (#585)
* quote fieldnames in _adodb_getupdatesql, fix indentations
* Fix my own conflict edits
Co-authored-by: Mark Newnham <mark@newnhams.com>
Diffstat (limited to 'adodb-lib.inc.php')
| -rw-r--r-- | adodb-lib.inc.php | 246 |
1 files changed, 124 insertions, 122 deletions
diff --git a/adodb-lib.inc.php b/adodb-lib.inc.php index 500bf0fd..ecdf9c0d 100644 --- a/adodb-lib.inc.php +++ b/adodb-lib.inc.php @@ -630,59 +630,57 @@ function _adodb_pageexecute_no_last_page(&$zthis, $sql, $nrows, $page, $inputarr return $rsreturn; } -function _adodb_getupdatesql(&$zthis,&$rs, $arrFields,$forceUpdate=false,$magicq=false,$force=2) +function _adodb_getupdatesql(&$zthis, &$rs, $arrFields, $forceUpdate=false, $magicq=false, $force=2) { global $ADODB_QUOTE_FIELDNAMES; - if (!$rs) { - printf(ADODB_BAD_RS,'GetUpdateSQL'); - return false; - } - - $fieldUpdatedCount = 0; - if (is_array($arrFields)) - $arrFields = array_change_key_case($arrFields,CASE_UPPER); - - $hasnumeric = isset($rs->fields[0]); - $setFields = ''; + if (!$rs) { + printf(ADODB_BAD_RS,'GetUpdateSQL'); + return false; + } - // Loop through all of the fields in the recordset - for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) { - // Get the field from the recordset - $field = $rs->FetchField($i); + $fieldUpdatedCount = 0; + if (is_array($arrFields)) + $arrFields = array_change_key_case($arrFields,CASE_UPPER); - // If the recordset field is one - // of the fields passed in then process. - $upperfname = strtoupper($field->name); - if (adodb_key_exists($upperfname,$arrFields,$force)) { + $hasnumeric = isset($rs->fields[0]); + $setFields = ''; - // If the existing field value in the recordset - // is different from the value passed in then - // go ahead and append the field name and new value to - // the update query. + // Loop through all of the fields in the recordset + for ($i=0, $max=$rs->fieldCount(); $i < $max; $i++) { + // Get the field from the recordset + $field = $rs->fetchField($i); - if ($hasnumeric) $val = $rs->fields[$i]; - else if (isset($rs->fields[$upperfname])) $val = $rs->fields[$upperfname]; - else if (isset($rs->fields[$field->name])) $val = $rs->fields[$field->name]; - else if (isset($rs->fields[strtolower($upperfname)])) $val = $rs->fields[strtolower($upperfname)]; - else $val = ''; + // If the recordset field is one + // of the fields passed in then process. + $upperfname = strtoupper($field->name); + if (adodb_key_exists($upperfname, $arrFields, $force)) { + // If the existing field value in the recordset + // is different from the value passed in then + // go ahead and append the field name and new value to + // the update query. - if ($forceUpdate || strcmp($val, $arrFields[$upperfname])) { - // Set the counter for the number of fields that will be updated. - $fieldUpdatedCount++; + if ($hasnumeric) $val = $rs->fields[$i]; + else if (isset($rs->fields[$upperfname])) $val = $rs->fields[$upperfname]; + else if (isset($rs->fields[$field->name])) $val = $rs->fields[$field->name]; + else if (isset($rs->fields[strtolower($upperfname)])) $val = $rs->fields[strtolower($upperfname)]; + else $val = ''; - // Based on the datatype of the field - // Format the value properly for the database - $type = $rs->MetaType($field->type); + if ($forceUpdate || strcmp($val, $arrFields[$upperfname])) { + // Set the counter for the number of fields that will be updated. + $fieldUpdatedCount++; + // Based on the datatype of the field + // Format the value properly for the database + $type = $rs->metaType($field->type); - if ($type == 'null') { - $type = 'C'; - } + if ($type == 'null') { + $type = 'C'; + } - if ((strpos($upperfname,' ') !== false) || ($ADODB_QUOTE_FIELDNAMES)) { - switch ($ADODB_QUOTE_FIELDNAMES) { + if ((strpos($upperfname,' ') !== false) || ($ADODB_QUOTE_FIELDNAMES)) { + switch ($ADODB_QUOTE_FIELDNAMES) { case 'BRACKETS': $fnameq = $zthis->leftBracket.$upperfname.$zthis->rightBracket;break; case 'LOWER': @@ -692,103 +690,107 @@ function _adodb_getupdatesql(&$zthis,&$rs, $arrFields,$forceUpdate=false,$magicq case 'UPPER': default: $fnameq = $zthis->nameQuote.$upperfname.$zthis->nameQuote;break; - } - } else - $fnameq = $upperfname; + } + } else { + $fnameq = $upperfname; + } - //********************************************************// - if (is_null($arrFields[$upperfname]) + //********************************************************// + if (is_null($arrFields[$upperfname]) || (empty($arrFields[$upperfname]) && strlen($arrFields[$upperfname]) == 0) - || $arrFields[$upperfname] === $zthis->null2null - ) - { - switch ($force) { + || $arrFields[$upperfname] === $zthis->null2null + ) { - //case 0: - // //Ignore empty values. This is already handled in "adodb_key_exists" function. - //break; + switch ($force) { - case 1: - //Set null - $setFields .= $field->name . " = null, "; - break; + //case 0: + // // Ignore empty values. This is already handled in "adodb_key_exists" function. + // break; + + case 1: + // set null + $setFields .= $fnameq . " = null, "; + break; + + case 2: + // set empty + $arrFields[$upperfname] = ""; + $setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname, $fnameq, $arrFields, $magicq); + break; - case 2: - //Set empty - $arrFields[$upperfname] = ""; - $setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname, $fnameq,$arrFields, $magicq); - break; default: - case 3: - //Set the value that was given in array, so you can give both null and empty values - if (is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === $zthis->null2null) { - $setFields .= $field->name . " = null, "; - } else { - $setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname, $fnameq,$arrFields, $magicq); - } - break; - case ADODB_FORCE_NULL_AND_ZERO: - - switch ($type) - { - case 'N': - case 'I': - case 'L': - $setFields .= $field->name . ' = 0, '; - break; - default: - $setFields .= $field->name . ' = null, '; - break; - } - break; - } - //********************************************************// - } else { - //we do this so each driver can customize the sql for - //DB specific column types. - //Oracle needs BLOB types to be handled with a returning clause - //postgres has special needs as well - $setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname, $fnameq, - $arrFields, $magicq); + case 3: + // set the value that was given in array, so you can give both null and empty values + if (is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === $zthis->null2null) { + $setFields .= $fnameq . " = null, "; + } else { + $setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname, $fnameq, $arrFields, $magicq); + } + break; + + case ADODB_FORCE_NULL_AND_ZERO: + + switch ($type) { + case 'N': + case 'I': + case 'L': + $setFields .= $fnameq . ' = 0, '; + break; + default: + $setFields .= $fnameq . ' = null, '; + break; + } + break; + } + //********************************************************// + } else { + // we do this so each driver can customize the sql for + // DB specific column types. + // Oracle needs BLOB types to be handled with a returning clause + // postgres has special needs as well + $setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname, $fnameq, $arrFields, $magicq); } } } + } - // If there were any modified fields then build the rest of the update query. - if ($fieldUpdatedCount > 0 || $forceUpdate) { - // Get the table name from the existing query. - if (!empty($rs->tableName)) $tableName = $rs->tableName; - else { - preg_match("/FROM\s+".ADODB_TABLE_REGEX."/is", $rs->sql, $tableName); - $tableName = $tableName[1]; - } - // Get the full where clause excluding the word "WHERE" from - // the existing query. - preg_match('/\sWHERE\s(.*)/is', $rs->sql, $whereClause); - - $discard = false; - // not a good hack, improvements? - if ($whereClause) { - #var_dump($whereClause); - if (preg_match('/\s(ORDER\s.*)/is', $whereClause[1], $discard)); - else if (preg_match('/\s(LIMIT\s.*)/is', $whereClause[1], $discard)); - else if (preg_match('/\s(FOR UPDATE.*)/is', $whereClause[1], $discard)); - else preg_match('/\s.*(\) WHERE .*)/is', $whereClause[1], $discard); # see https://sourceforge.net/p/adodb/bugs/37/ - } else - $whereClause = array(false,false); + // If there were any modified fields then build the rest of the update query. + if ($fieldUpdatedCount > 0 || $forceUpdate) { + // Get the table name from the existing query. + if (!empty($rs->tableName)) { + $tableName = $rs->tableName; + } else { + preg_match("/FROM\s+".ADODB_TABLE_REGEX."/is", $rs->sql, $tableName); + $tableName = $tableName[1]; + } - if ($discard) - $whereClause[1] = substr($whereClause[1], 0, strlen($whereClause[1]) - strlen($discard[1])); + // Get the full where clause excluding the word "WHERE" from the existing query. + preg_match('/\sWHERE\s(.*)/is', $rs->sql, $whereClause); - $sql = 'UPDATE '.$tableName.' SET '.substr($setFields, 0, -2); - if (strlen($whereClause[1]) > 0) - $sql .= ' WHERE '.$whereClause[1]; + $discard = false; + // not a good hack, improvements? + if ($whereClause) { + #var_dump($whereClause); + if (preg_match('/\s(ORDER\s.*)/is', $whereClause[1], $discard)); + else if (preg_match('/\s(LIMIT\s.*)/is', $whereClause[1], $discard)); + else if (preg_match('/\s(FOR UPDATE.*)/is', $whereClause[1], $discard)); + else preg_match('/\s.*(\) WHERE .*)/is', $whereClause[1], $discard); # see https://sourceforge.net/p/adodb/bugs/37/ + } else { + $whereClause = array(false, false); + } - return $sql; + if ($discard) { + $whereClause[1] = substr($whereClause[1], 0, strlen($whereClause[1]) - strlen($discard[1])); + } - } else { - return false; + $sql = 'UPDATE '.$tableName.' SET '.substr($setFields, 0, -2); + if (strlen($whereClause[1]) > 0) { + $sql .= ' WHERE '.$whereClause[1]; + } + return $sql; + } else { + return false; } } |
