From 38c663c122598b4eee22a0466503014d66ced4e0 Mon Sep 17 00:00:00 2001
From: "Committo,Ergo:sum" <esj@rezo.net>
Date: Sun, 5 Aug 2007 14:24:53 +0000
Subject: [PATCH] =?UTF-8?q?#209:=20comme=20l'avait=20pr=C3=A9vu=20[9859],?=
 =?UTF-8?q?=20autre=20cas=20de=20nombres=20=C3=A0=20ne=20pas=20entourer=20?=
 =?UTF-8?q?d'apostrophies,=20les=20cl=C3=A9s=20de=20jointure.=20Il=20a=20f?=
 =?UTF-8?q?allu=20r=C3=A9fl=C3=A9chir=20longtemps,=20mais=20le=20changemen?=
 =?UTF-8?q?t=20est=20minimal,=20preuve=20que=20le=20compilateur=20a=20les?=
 =?UTF-8?q?=20bons=20utilitaires=20c'est=20rassurant.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Aussi dans ce dépot, quelques corrections  dans l'inteface PG:
	° [9861] était vraiment à l'arraché, il ne fallait pas oublier de supprimer les AS ingjctés dans le GROUPBY;
	* traitement du HAVING par la fonction pour PG pas pour MySQL;
	* inclusion inopérante du débusqueur.
---
 ecrire/base/db_pg.php      |  6 ++++--
 ecrire/public/compiler.php |  2 +-
 ecrire/public/criteres.php | 24 ++++++++++++++++--------
 3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/ecrire/base/db_pg.php b/ecrire/base/db_pg.php
index 749f14db4f..aa4acf2916 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 5911fd4c01..fc1fb6444f 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 db470e4025..4c12efc3e0 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
-- 
GitLab