implemented automatic truncate of varchars to their max size for postgres, to fix SQL errors which cause eg. SyncML to fail under Postgres

This commit is contained in:
Ralf Becker 2007-06-20 18:49:00 +00:00
parent ff26c43642
commit 4ab35f71ef

View File

@ -127,6 +127,7 @@
'name_case' => 'upper', // case of returned column- and table-names: upper, lower(pgSql), preserv(MySQL) 'name_case' => 'upper', // case of returned column- and table-names: upper, lower(pgSql), preserv(MySQL)
'client_encoding' => false, // db uses a changeable clientencoding 'client_encoding' => false, // db uses a changeable clientencoding
'case_insensitive_like' => 'LIKE', // case insensitive version of like, eg. ILIKE for postgres 'case_insensitive_like' => 'LIKE', // case insensitive version of like, eg. ILIKE for postgres
'require_truncate_varchar' => false,// DB requires varchar columns to be truncated to the max. size (eg. Postgres)
'order_on_text' => true, // is the DB able to order by a given text column, boolean or 'order_on_text' => true, // is the DB able to order by a given text column, boolean or
); // string for sprintf for a cast (eg. 'CAST(%s AS varchar)') ); // string for sprintf for a cast (eg. 'CAST(%s AS varchar)')
@ -346,6 +347,7 @@
$this->capabilities['client_encoding'] = (float) $db_version >= 7.4; $this->capabilities['client_encoding'] = (float) $db_version >= 7.4;
$this->capabilities['outer_join'] = true; $this->capabilities['outer_join'] = true;
$this->capabilities['case_insensitive_like'] = 'ILIKE'; $this->capabilities['case_insensitive_like'] = 'ILIKE';
$this->capabilities['require_truncate_varchar'] = true;
break; break;
case 'mssql': case 'mssql':
@ -1141,11 +1143,12 @@
* Int and Auto types are casted to int: quote('1','int') === 1, quote('','int') === 0, quote('Hello','int') === 0 * Int and Auto types are casted to int: quote('1','int') === 1, quote('','int') === 0, quote('Hello','int') === 0
* *
* @param mixed $value the value to be escaped * @param mixed $value the value to be escaped
* @param string/boolean $type string the type of the db-column, default False === varchar * @param string/boolean $type=false string the type of the db-column, default False === varchar
* @param boolean $not_null is column NOT NULL, default true, else php null values are written as SQL NULL * @param boolean $not_null=true is column NOT NULL, default true, else php null values are written as SQL NULL
* @param int $length=null length of the varchar column, to truncate it if the database requires it (eg. Postgres)
* @return string escaped sting * @return string escaped sting
*/ */
function quote($value,$type=False,$not_null=true) function quote($value,$type=False,$not_null=true,$length=null)
{ {
if ($this->Debug) echo "<p>db::quote(".(is_null($value)?'NULL':"'$value'").",'$type','$not_null')</p>\n"; if ($this->Debug) echo "<p>db::quote(".(is_null($value)?'NULL':"'$value'").",'$type','$not_null')</p>\n";
@ -1188,6 +1191,10 @@
case 'timestamp': case 'timestamp':
return $this->Link_ID->DBTimeStamp($value); return $this->Link_ID->DBTimeStamp($value);
} }
if (!is_null($length) && strlen($value) > $length)
{
$value = substr($value,0,$length);
}
return $this->Link_ID->qstr($value); return $this->Link_ID->qstr($value);
} }
@ -1225,6 +1232,9 @@
} }
if ($this->Debug) echo "<p>db::column_data_implode('$glue',".print_r($array,True).",'$use_key',".print_r($only,True).",<pre>".print_r($column_definitions,True)."</pre>\n"; if ($this->Debug) echo "<p>db::column_data_implode('$glue',".print_r($array,True).",'$use_key',".print_r($only,True).",<pre>".print_r($column_definitions,True)."</pre>\n";
// do we need to truncate varchars to their max length (INSERT and UPDATE on Postgres)
$truncate_varchar = $glue == ',' && $this->capabilities['require_truncate_varchar'];
$keys = $values = array(); $keys = $values = array();
foreach($array as $key => $data) foreach($array as $key => $data)
{ {
@ -1241,6 +1251,10 @@
$column_type = is_array($column_definitions) ? @$column_definitions[$key]['type'] : False; $column_type = is_array($column_definitions) ? @$column_definitions[$key]['type'] : False;
$not_null = is_array($column_definitions) && isset($column_definitions[$key]['nullable']) ? !$column_definitions[$key]['nullable'] : false; $not_null = is_array($column_definitions) && isset($column_definitions[$key]['nullable']) ? !$column_definitions[$key]['nullable'] : false;
if ($truncate_varchar)
{
$maxlength = $column_definitions[$key]['type'] == 'varchar' ? $column_definitions[$key]['precision'] : null;
}
if (is_array($data)) if (is_array($data))
{ {
$or_null = ''; $or_null = '';
@ -1252,7 +1266,7 @@
unset($data[$k]); unset($data[$k]);
continue; continue;
} }
$data[$k] = $this->quote($v,$column_type,$not_null); $data[$k] = $this->quote($v,$column_type,$not_null,$maxlength);
} }
$values[] = ($or_null?'(':'').(!count($data) ? '' : $values[] = ($or_null?'(':'').(!count($data) ? '' :
($use_key===True ? $this->name_quote($key).' IN ' : '') . ($use_key===True ? $this->name_quote($key).' IN ' : '') .
@ -1268,7 +1282,7 @@
} }
else else
{ {
$values[] = ($use_key===True ? $this->name_quote($key) . '=' : '') . $this->quote($data,$column_type,$not_null); $values[] = ($use_key===True ? $this->name_quote($key) . '=' : '') . $this->quote($data,$column_type,$not_null,$maxlength);
} }
} }
} }