diff --git a/ecrire/base/db_pg.php b/ecrire/base/db_pg.php index 749f14db4f471807d652b094f60879d64164b81b..aa4acf29166d491912e750589bbb0ec13a822e16 100644 --- a/ecrire/base/db_pg.php +++ b/ecrire/base/db_pg.php @@ -90,7 +90,7 @@ function spip_pg_select($select, $from, $where, (!is_array($from) ? $from : spip_pg_select_as($from)))) . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? $where : (join("\n\tAND ", array_map('calculer_pg_where', $where)))))) . spip_pg_groupby($groupby, $from, $select) - . (!$having ? '' : "\nHAVING " . (!is_array($having) ? $having : (join("\n\tAND ", array_map('calculer_where', $having))))) + . (!$having ? '' : "\nHAVING " . (!is_array($having) ? $having : (join("\n\tAND ", array_map('calculer_pg_where', $having))))) . ($orderby ? ("\nORDER BY " . spip_pg_nocast($orderby)) :'') . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); $q = " SELECT ". $q; @@ -100,6 +100,7 @@ function spip_pg_select($select, $from, $where, // actuellement dans erreur_requete_boucle if ($GLOBALS['var_mode'] == 'debug') { + include_spip('public/debug'); boucle_debug_resultat($id, '', $q); } @@ -121,7 +122,8 @@ function spip_pg_groupby($groupby, $from, $select) $join = is_array($from) ? (count($from) > 1) : strpos($from, ","); if ($join) $join = !is_array($select) ? $select : join(", ", $select); if ($join) $groupby = $groupby ? "$groupby, $join" : $join; - return ($groupby ? "\nGROUP BY $groupby" : ''); + $groupby = preg_replace('/\s+AS\s+\w+/','', $groupby); + return (!$groupby ? '' : ("\nGROUP BY " . spip_pg_nocast($groupby))); } // 0+x avec un champ x commencant par des chiffres est converti par MySQL diff --git a/ecrire/public/compiler.php b/ecrire/public/compiler.php index 5911fd4c01026d0085d8d3c53e69c1fd88defe45..fc1fb6444f324c52e97f31c8f58384af387e6377 100644 --- a/ecrire/public/compiler.php +++ b/ecrire/public/compiler.php @@ -341,7 +341,7 @@ function calculer_dump_array($a) function calculer_dump_join($a) { $res = ""; - foreach($a as $k => $v) $res .= ", $k => array('$v[0]', '$v[1]')"; + foreach($a as $k => $v) $res .= ", $k => array($v[0], $v[1])"; return 'array(' . substr($res,2) . ')'; } diff --git a/ecrire/public/criteres.php b/ecrire/public/criteres.php index db470e40259ea5518c1b7e72f74f96ccb3d98282..4c12efc3e0afa265f22496dbe318a940094ebf60 100644 --- a/ecrire/public/criteres.php +++ b/ecrire/public/criteres.php @@ -859,7 +859,10 @@ function trouver_champ($champ, $where) } } -// deduction automatique des jointures +// deduction automatique d'une chaine de jointures +// Pour chaque cle de jointure il faut regarder si le type est numerique +// car PG ne veut pas d'apostrophe + // http://doc.spip.org/@calculer_jointure function calculer_jointure(&$boucle, $depart, $arrivee, $col='', $cond=false) { @@ -873,8 +876,10 @@ function calculer_jointure(&$boucle, $depart, $arrivee, $col='', $cond=false) $cpt = &$num[$boucle->descr['nom']][$boucle->id_boucle]; foreach($res as $r) { list($d, $a, $j) = $r; + if (!$id_table) $id_table = $d; + $type = $a[1]['field'][$j]; $n = ++$cpt; - $boucle->join[$n]= array(($id_table ? $id_table : $d), $j); + $boucle->join[$n]= array("'$id_table'", test_sql_int($type) ? $j : "'$j'"); $boucle->from[$id_table = "L$n"] = $a[0]; } @@ -1119,17 +1124,20 @@ function calculer_critere_infixe_ops($idb, &$boucles, $crit) if ($op == '=' OR in_array($op, $table_criteres_infixes)) { list($nom, $desc) = trouver_def_table($boucles[$idb]->id_table, $boucles[$idb]); $type = $desc['field'][$col]; - if (strpos($val[0], '_q(') === 0 - AND (strpos($type, 'bigint') === 0 - OR strpos($type, 'int') === 0 - OR strpos($type, 'tinyint') === 0)) - - $val[0] = 'intval' . substr($val[0],2); + if (strpos($val[0], '_q(') === 0 AND test_sql_int($type)) + $val[0] = 'intval' . substr($val[0],2); } return array($fct, $col, $op, $val, $args_sql); } +function test_sql_int($type) +{ + return (strpos($type, 'bigint') === 0 + OR strpos($type, 'int') === 0 + OR strpos($type, 'tinyint') === 0); +} + // compatibilite ancienne version // http://doc.spip.org/@calculer_vieux_in