* PostgreSQL/Addressbook: fixed SQL error when sorting by a custom field

This commit is contained in:
Ralf Becker 2013-10-24 14:57:56 +00:00
parent 04981e9eea
commit acf0d0fc3f
2 changed files with 16 additions and 5 deletions

View File

@ -259,8 +259,8 @@ class addressbook_sql extends so_sql_cf
*/
function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='',$need_full_no_count=false)
{
if ((int) $this->debug >= 4) echo '<p>'.__METHOD__.'('.array2string($criteria,true).','.array2string($only_keys).",'$order_by','$extra_cols','$wildcard','$empty','$op',$start,".array2string($filter,true).",'$join')</p>\n";
//error_log(__METHOD__.'('.array2string($criteria,true).','.array2string($only_keys).",'$order_by','$extra_cols','$wildcard','$empty','$op',$start,".array2string($filter,true).",'$join')");
if ((int) $this->debug >= 4) echo '<p>'.__METHOD__.'('.array2string($criteria).','.array2string($only_keys).",'$order_by','$extra_cols','$wildcard','$empty','$op',$start,".array2string($filter).",'$join')</p>\n";
//error_log(__METHOD__.'('.array2string($criteria,true).','.array2string($only_keys).",'$order_by', ".array2string($extra_cols).",'$wildcard','$empty','$op',$start,".array2string($filter).",'$join')");
$owner = isset($filter['owner']) ? $filter['owner'] : (isset($criteria['owner']) ? $criteria['owner'] : null);
@ -359,13 +359,14 @@ class addressbook_sql extends so_sql_cf
// fall through
}
// postgres requires that expressions in order by appear in the columns of a distinct select
if ($this->db->Type != 'mysql' && preg_match_all("/([a-zA-Z_.]+) *(<> *''|IS NULL|IS NOT NULL)? *(ASC|DESC)?(,|$)/ui",$order_by,$all_matches,PREG_SET_ORDER))
if ($this->db->Type != 'mysql' && preg_match_all("/(#?[a-zA-Z_.]+) *(<> *''|IS NULL|IS NOT NULL)? *(ASC|DESC)?(,|$)/ui",$order_by,$all_matches,PREG_SET_ORDER))
{
if (!is_array($extra_cols)) $extra_cols = $extra_cols ? explode(',',$extra_cols) : array();
foreach($all_matches as $matches)
{
$table = '';
$column = $matches[1];
if ($column[0] == '#') continue; // order by custom field is handeled in so_sql_cf anyway
if (($key = array_search($column, $this->db_cols)) !== false) $column = $key;
if (strpos($column,'.') === false)
{

View File

@ -460,7 +460,7 @@ class so_sql_cf extends so_sql
// replace ambiguous column with (an exact match of) table_name.column
foreach($criteria as $name => $val)
{
$extra_columns = $this->db->get_table_definitions($app, $this->extra_table);
$extra_columns = $this->db->get_table_definitions($this->app, $this->extra_table);
if(is_string($name) && $extra_columns['fd'][array_search($name, $this->db_cols)])
{
$criteria[] = $this->db->expression($this->table_name,$this->table_name.'.',array(
@ -524,6 +524,16 @@ class so_sql_cf extends so_sql
$orderDir = array_pop($buff);
$key = trim(implode(' ',$buff));
$order_by = str_replace($v,'extra_order.'.$this->extra_value.' IS NULL,extra_order.'.$this->extra_value.' '.$orderDir,$order_by);
// postgres requires that expressions in order by appear in the columns of a distinct select
if ($this->db->Type != 'mysql')
{
if (!is_array($extra_cols))
{
$extra_cols = $extra_cols ? explode(',', $extra_cols) : array();
}
$extra_cols[] = 'extra_order.'.$this->extra_value;
$extra_cols[] = 'extra_order.'.$this->extra_value.' IS NULL';
}
$join .= $this->extra_join_order.' AND extra_order.'.$this->extra_key.'='.$this->db->quote(substr($key,1));
}
}
@ -548,7 +558,7 @@ class so_sql_cf extends so_sql
// replace ambiguous column with (an exact match of) table_name.column
elseif (is_string($name) && $val!=null && in_array($name, $this->db_cols))
{
$extra_columns = $this->db->get_table_definitions($app, $this->extra_table);
$extra_columns = $this->db->get_table_definitions($this->app, $this->extra_table);
if ($extra_columns['fd'][array_search($name, $this->db_cols)])
{
$filter[] = $this->db->expression($this->table_name,$this->table_name.'.',array(