query($query); if ($my_profile) { $m2 = microtime(); list($usec, $sec) = explode(" ", $m1); list($usec2, $sec2) = explode(" ", $m2); $dt = $sec2 + $usec2 - $sec - $usec; $tt += $dt; echo "".htmlentities($query); echo " -> ".sprintf("%3f", $dt)." ($tt)
\n";
}
if ($s = $spip_mysql_link->error) {
if ($my_debug) {
echo _T('info_erreur_requete')." ".htmlentities($query)."
";
echo "« ".htmlentities($s)." »
"; } spip_log($GLOBALS['REQUEST_METHOD'].' '.$GLOBALS['REQUEST_URI'], 'mysql'); spip_log("$s - $query", 'mysql'); } # spip_log("$s - $query", 'mysql'); return $result; } // fonction appelant la precedente // specifiquement pour les select des squelettes // c'est une instance de spip_abstract_select, voir ses specs dans inc_calcul // les \n et \t sont utiles au debusqueur // traite_query pourrait y est fait d'avance, à moindre cout function spip_mysql_select($select, $from, $where, $groupby, $orderby, $limit, $sousrequete, $cpt, $table, $id, $serveur) { $q = "\nFROM " . join(",\n\t", $from) . ($where ? "\nWHERE " . join("\n\tAND ", $where) : '') . ($groupby ? "\nGROUP BY $groupby" : '') . ($orderby ? "\nORDER BY $orderby" : '') . ($limit ? "\nLIMIT $limit" : ''); if (!$sousrequete) $q = " SELECT ". join(", ", $select) . $q; else $q = " SELECT S_" . join(", S_", $select) . " FROM (" . join(", ", $select) . ", COUNT(".$sousrequete.") AS compteur " . $q .") AS S_$table WHERE compteur=" . $cpt; // Erreur ? C'est du debug de squelette, ou une erreur du serveur if ($GLOBALS['var_mode'] == 'debug') { boucle_debug_resultat($id, '', $q); } if (!($res = @spip_query($q))) { include_ecrire('inc_debug_sql.php'); echo erreur_requete_boucle($q, $id, $table); } # spip_log($serveur . spip_num_rows($res) . $q); return $res; } // // Passage d'une requete standardisee // Quand tous les appels SQL seront abstraits on pourra l'ameliorer function traite_query($query) { if ($GLOBALS['table_prefix']) $table_pref = $GLOBALS['table_prefix']."_"; else $table_pref = ""; if ($db = $GLOBALS['spip_mysql_db']) $table_pref = '`'.$db.'`.'.$table_pref; // changer les noms des tables ($table_prefix) if ($GLOBALS['flag_pcre']) { if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { $suite = strstr($query, $regs[0]); $query = substr($query, 0, -strlen($suite)); } $query = preg_replace('/([,\s])spip_/', '\1'.$table_pref, $query) . $suite; } else { if (eregi('[[:space:]](SET|VALUES|WHERE)[[:space:]]', $query, $regs)) { $suite = strstr($query, $regs[0]); $query = substr($query, 0, -strlen($suite)); } $query = ereg_replace('([[:space:],])spip_', '\1'.$table_pref, $query) . $suite; } return $query; } // // Connexion a la base // function spip_connect_db($host, $port, $login, $pass, $db) { global $spip_mysql_link, $spip_mysql_db; // pour connexions multiples if ($port > 0) $host = "$host:$port"; $spip_mysql_link = new mysqli($host, $login, $pass, $db, $port); if (mysqli_connect_errno()) { die("Connect failed: ".mysqli_connect_error()."\n"); } $spip_mysql_db= $db; } // // Recuperation des resultats // function spip_fetch_array($r) { if ($r) return mysqli_fetch_array($r); } /* Appels obsoletes function spip_fetch_object($r) { if ($r) return mysqli_fetch_object($r); } function spip_fetch_row($r) { if ($r) return mysqli_fetch_row($r); } */ function spip_sql_error() { global $spip_mysql_link; return $spip_mysql_link->error; } function spip_sql_errno() { global $spip_mysql_link; return $spip_mysql_link->errno; } function spip_num_rows($r) { if ($r) return mysqli_num_rows($r); } function spip_free_result($r) { if ($r) return mysqli_free_result($r); } function spip_mysql_insert($table, $champs, $valeurs) { spip_query("INSERT INTO $table $champs VALUES $valeurs"); return mysqli_insert_id(); } function spip_insert_id() { return mysqli_insert_id(); } // // Poser un verrou local a un SPIP donne // function spip_get_lock($nom, $timeout = 0) { global $spip_mysql_db, $table_prefix; if ($table_prefix) $nom = "$table_prefix:$nom"; if ($spip_mysql_db) $nom = "$spip_mysql_db:$nom"; $nom = addslashes($nom); $q = spip_query("SELECT GET_LOCK('$nom', $timeout)"); list($lock_ok) = spip_fetch_array($q); if (!$lock_ok) spip_log("pas de lock sql pour $nom"); return $lock_ok; } function spip_release_lock($nom) { global $spip_mysql_db, $table_prefix; if ($table_prefix) $nom = "$table_prefix:$nom"; if ($spip_mysql_db) $nom = "$spip_mysql_db:$nom"; $nom = addslashes($nom); spip_query("SELECT RELEASE_LOCK('$nom')"); } // // IN (...) est limite a 255 elements, d'ou cette fonction assistante // function calcul_mysql_in($val, $valeurs, $not='') { if (!$valeurs) return '0=0'; $n = $i = 0; $chunk = array(); while ($n = strpos($valeurs, ',', $n+1)) { if ((++$i) >= 255) { $chunk = substr($valeurs, 0, $n); $in_sql .= "($val $not IN (" . $chunk . "))\n" . ($not ? "AND\t" : "OR\t"); $valeurs = substr($valeurs, $n+1); $i = $n = 0; } } $in_sql .= "($val $not IN ($valeurs))"; return $in_sql; } ?>