*
* Copyright 1999-2001, VA Linux Systems, Inc.
* Copyright 2002, GForge, LLC
+ * Copyright 2009, Roland Mas
*
* This file is part of FusionForge.
*
}
//$GLOBALS['G_DEBUGQUERY'] .= $qstring .' |<font size="-2">'.$dbserver.'</font>'. "<p>\n";
- $res = @pg_exec($dbserver,$qstring);
+ $res = @pg_query($dbserver,$qstring);
+ if (!$res) {
+ error_log('SQL: '. preg_replace('/\n\t+/', ' ',$qstring));
+ error_log('SQL> '.db_error());
+ }
//echo "\n<br />|*| [$qstring]: ".db_error();
return $res;
}
+/**
+ * db_query_params() - Query the database, with parameters
+ *
+ * @param text SQL statement.
+ * @param array parameters
+ * @param int How many rows do you want returned.
+ * @param int Of matching rows, return only rows starting here.
+ * @param int ability to spread load to multiple db servers.
+ * @return int result set handle.
+ */
+function db_query_params($qstring,$params,$limit='-1',$offset=0,$dbserver=SYS_DB_PRIMARY) {
+ global $QUERY_COUNT;
+ $QUERY_COUNT++;
+
+ if (get_magic_quotes_gpc() == true) {
+ $params = array_map('stripslashes',$params);
+ }
+ if (!$limit || !is_numeric($limit) || $limit < 0) {
+ $limit=0;
+ }
+ if ($limit > 0) {
+ if (!$offset || !is_numeric($offset) || $offset < 0) {
+ $offset=0;
+ }
+ $qstring=$qstring." LIMIT $limit OFFSET $offset";
+ }
+
+ $res = @pg_query_params($dbserver,$qstring,$params);
+ if (!$res) {
+ error_log('SQL: '. preg_replace('/\n\t+/', ' ',$qstring));
+ error_log('SQL> '.db_error());
+ }
+ return $res;
+}
+
+/**
+ * db_query_params() - Query the database, with a query+params array
+ *
+ * @param array array(query, array(parameters...))
+ * @param int How many rows do you want returned.
+ * @param int Of matching rows, return only rows starting here.
+ * @param int ability to spread load to multiple db servers.
+ * @return int result set handle.
+ */
+function db_query_qpa ($qpa,$limit='-1',$offset=0,$dbserver=SYS_DB_PRIMARY) {
+ $sql = $qpa[0] ;
+ $params = $qpa[1] ;
+ return db_query_params ($sql, $params, $limit, $offset, $dbserver) ;
+}
+
/**
* db_mquery() - Query the database.
*
// programmatical transaction
$_sys_db_transaction_level++;
if ($_sys_db_transaction_level == 1) {
- return db_query("BEGIN WORK", -1, 0, $dbserver);
+ return db_query_params ("BEGIN WORK", array(), -1, 0, $dbserver);
}
return true;
// programmatical transaction ends
$_sys_db_transaction_level--;
if ($_sys_db_transaction_level == 0) {
- return db_query("COMMIT", -1, 0, $dbserver);
+ return db_query_params ("COMMIT", array(), -1, 0, $dbserver);
}
return true;
// programmatical transaction ends
$_sys_db_transaction_level--;
if ($_sys_db_transaction_level == 0) {
- return db_query("ROLLBACK", -1, 0, $dbserver);
+ return db_query_params ("ROLLBACK", array(), -1, 0, $dbserver);
}
return true;
function db_insertid($qhandle,$table_name,$pkey_field_name,$dbserver=SYS_DB_PRIMARY) {
$sql="SELECT max($pkey_field_name) AS id FROM $table_name";
//echo $sql;
- $res=db_query($sql, -1, 0, $dbserver);
+ $res = db_query_params ($sql, array(), -1, 0, $dbserver);
if (db_numrows($res) >0) {
return db_result($res,0,'id');
} else {
global $_sys_db_transaction_level;
if ($_sys_db_transaction_level > 0) {
echo "Open transaction detected!!!";
- db_query("ROLLBACK");
+ db_query_params ("ROLLBACK", array ());
}
}
function db_drop_table_if_exists ($tn) {
- $sql = "SELECT COUNT(*) FROM pg_class WHERE relname='$tn';";
- $rel = db_query($sql);
+ $rel = db_query_params ('SELECT COUNT(*) FROM pg_class WHERE relname=$1 and relkind=$2',
+ array ($tn, 'r'));
echo db_error();
$count = db_result($rel,0,0);
if ($count != 0) {
- $sql = "DROP TABLE $tn;";
- $rel = db_query ($sql);
+ $sql = "DROP TABLE $tn";
+ $rel = db_query_params ($sql, array ());
echo db_error();
}
}
function db_drop_sequence_if_exists ($tn) {
- $sql = "SELECT COUNT(*) FROM pg_class WHERE relname='$tn';";
- $rel = db_query($sql);
+ $rel = db_query_params ('SELECT COUNT(*) FROM pg_class WHERE relname=$1 and relkind=$2',
+ array ($tn, 'S'));
echo db_error();
$count = db_result($rel,0,0);
if ($count != 0) {
- $sql = "DROP SEQUENCE $tn;";
- $rel = db_query ($sql);
+ $sql = "DROP SEQUENCE $tn";
+ $rel = db_query_params ($sql, array ());
echo db_error();
}
}
+function db_int_array_to_any_clause ($arr) {
+ $arr2 = array () ;
+ foreach ($arr as $cur) {
+ if (is_numeric($cur)) {
+ $arr2[] = $cur ;
+ }
+ }
+ $res = '{' . implode (',', $arr2) . '}' ;
+ return $res ;
+}
+
+function db_string_array_to_any_clause ($arr) {
+ $arr2 = array () ;
+ foreach ($arr as $cur) {
+ $arr2[] = pg_escape_string ($cur) ;
+ }
+ $res = '{"' . implode ('","', $arr2) . '"}' ;
+ return $res ;
+}
+
+function db_construct_qpa ($old_qpa = false, $new_sql = '', $new_params = array ()) {
+ if (!is_array($old_qpa) || count ($old_qpa) < 3) {
+ $old_qpa = array ('', array(), 0) ;
+ }
+ $old_sql = $old_qpa[0] ;
+ $old_params = $old_qpa[1] ;
+ $old_max = $old_qpa[2] ;
+
+ $sql = $old_sql ;
+ $params = $old_params ;
+ $max = $old_max ;
+
+ foreach ($new_params as $index => $value) {
+ $i = count ($new_params) - $index ;
+ $new_sql = preg_replace ('/\\$'.$i.'(?!\d)/', '$_'.($i + $old_max), $new_sql) ;
+ $params[] = $value ;
+ $max++ ;
+ }
+ $new_sql = str_replace ('$_', '$', $new_sql) ;
+
+ $sql .= $new_sql ;
+
+ return array ($sql, $params, $max) ;
+}
+
+function db_join_qpa ($old_qpa = false, $new_qpa = false) {
+ return db_construct_qpa ($old_qpa, $new_qpa[0], $new_qpa[1]) ;
+}
+
// Local Variables:
// mode: php
// c-file-style: "bsd"