* SyncML/Calendar/PostgreSQL: fixed SQL error when searching participants (SELECT DISTINCT requires ORDER-BY-expressions to be in SELECT-list)

This commit is contained in:
Ralf Becker 2011-04-11 12:07:30 +00:00
parent 440bcb1235
commit 3915fb6f03

View File

@ -357,18 +357,25 @@ 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("/([a-zA-Z_.]+)<>''/",$order_by,$matches))
if ($this->db->Type != 'mysql' && preg_match_all("/([a-zA-Z_.]+) *(<> *''|IS NULL|IS NOT NULL)/u",$order_by,$all_matches,PREG_SET_ORDER))
{
if (!is_array($extra_cols)) $extra_cols = $extra_cols ? explode(',',$extra_cols) : array();
$table = $matches[1] == $this->extra_value ? $this->extra_table : $this->table_name;
$extra_cols[] = $table.'.'.$matches[1];
$extra_cols[] = $table.'.'.$matches[1]."<>''";
//_debug_array($matches[1]);
if (!empty($order_by) && $matches[1] != 'extra_order.contact_value' && stripos($matches[1],'.')===false) // postgres requires explizit order by
foreach($all_matches as $matches)
{
$order_by = str_replace($matches[1],'egw_addressbook.'.$matches[1],$order_by);
$table = '';
if (strpos($matches[1],'.') === false)
{
$table = ($matches[1] == $this->extra_value ? $this->extra_table : $this->table_name).'.';
}
//_debug_array($order_by);
$extra_cols[] = $table.$matches[1];
$extra_cols[] = $table.$matches[0];
//_debug_array($matches);
if (!empty($order_by) && $table) // postgres requires explizit order by
{
$order_by = str_replace($matches[0],$table.$matches[0],$order_by);
}
}
//_debug_array($order_by); _debug_array($extra_cols);
}
}
$rows =& parent::search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,$start,$filter,$join,$need_full_no_count);