Changeset 10430


Ignore:
Timestamp:
Apr 16, 2011, 11:22:09 PM (13 years ago)
Author:
plg
Message:

bug 1818 fixed: fix "select distinct" queries for PostgreSQL. Patch by leloupv

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2/include/dblayer/functions_pgsql.inc.php

    r8728 r10430  
    9292  global $conf,$page,$debug,$t2;
    9393
    94   $replace_pattern = '`REPLACE INTO\s(\S*)\s*([^)]*\))\s*VALUES\(([^,]*),(.*)\)\s*`mi'; 
     94  $replace_pattern = '`REPLACE INTO\s(\S*)\s*([^)]*\))\s*VALUES\(([^,]*),(.*)\)\s*`mi';
     95  $select_distinct_pattern = '/SELECT\s+DISTINCT\s*(\S[^;]*\S)\s*(FROM[^(;]+WHERE[^;]+)\s+ORDER\s+BY\s+([^;]*\S)\s*;?/i';
    9596
    9697  $start = get_moment();
     
    117118    }
    118119    ( $result = pg_query($query)) or die($query."\n<br>".pg_last_error());     
     120  }
     121  elseif (preg_match($select_distinct_pattern, $query, $matches))
     122  {
     123    $select_fields_string='';
     124    $distinct_fields_string='';
     125    $orderby_fields_string='';
     126   
     127    foreach (preg_split( '/\s*,\s*/', $matches[1]) as $field)
     128    {
     129      $split_field = preg_split( '/\s*AS\s*/i', $field);
     130      if (isset($split_field[1]))
     131      {
     132         $distinct_fields[ $split_field[1] ] = $field;
     133      }
     134      else
     135      {
     136        $distinct_fields[ $field ] = $field;
     137      }
     138    }
     139   
     140    foreach (preg_split( '/\s*,\s*/', $matches[3]) as $field)
     141    {
     142      $kv = preg_split( '/\s+/', $field );
     143      $orderby_fields[ $kv[0] ] = $kv[1];
     144    }
     145   
     146    foreach ($distinct_fields as $as_field => $field)
     147    {
     148      if ($distinct_fields_string)
     149      {
     150        $distinct_fields_string=$distinct_fields_string.', ';
     151      }
     152     
     153      $distinct_fields_string=$distinct_fields_string.$as_field;
     154     
     155      if ($select_fields_string)
     156      {
     157        $select_fields_string=$select_fields_string.', ';
     158      }
     159     
     160      $select_fields_string=$select_fields_string.$field;
     161     
     162      if ($orderby_fields_string)
     163      {
     164        $orderby_fields_string=$orderby_fields_string.', ';
     165      }
     166     
     167      $orderby_fields_string=$orderby_fields_string.$as_field.' ';
     168     
     169      if (isset($orderby_fields[$as_field]))
     170      {
     171        $orderby_fields_string=$orderby_fields_string.$orderby_fields[$as_field];
     172        unset($orderby_fields[$as_field]);
     173      }
     174      else
     175      {
     176        $orderby_fields_string=$orderby_fields_string.'ASC';
     177      }
     178     }
     179   
     180     foreach ($orderby_fields as $field => $order)
     181     {
     182       $orderby_fields_string=$orderby_fields_string.', '.$field.' '.$order;
     183     }
     184     
     185     $query = '
     186SELECT DISTINCT ON ('.$distinct_fields_string.') '.$select_fields_string.'
     187   '.$matches[2].'
     188   ORDER BY '.$orderby_fields_string;
     189     ($result = pg_query($query)) or die($query."\n<br>".pg_last_error());
    119190  }
    120191  else
Note: See TracChangeset for help on using the changeset viewer.