debug($query);
$start = get_moment();
($result = pg_query($query)) or die($query."\n
".pg_last_error());
$time = get_moment() - $start;
if (!isset($page['count_queries']))
{
$page['count_queries'] = 0;
$page['queries_time'] = 0;
}
$page['count_queries']++;
$page['queries_time']+= $time;
if ($conf['show_queries'])
{
$output = '';
$output.= '
['.$page['count_queries'].'] ';
$output.= "\n".$query;
$output.= "\n".'(this query time : ';
$output.= ''.number_format($time, 3, '.', ' ').' s)';
$output.= "\n".'(total SQL time : ';
$output.= number_format($page['queries_time'], 3, '.', ' ').' s)';
$output.= "\n".'(total time : ';
$output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)';
if ( $result!=null and preg_match('/\s*SELECT\s+/i',$query) )
{
$output.= "\n".'(num rows : ';
$output.= pwg_db_num_rows($result).' )';
}
elseif ( $result!=null
and preg_match('/\s*INSERT|UPDATE|REPLACE|DELETE\s+/i',$query) )
{
$output.= "\n".'(affected rows : ';
$output.= pwg_db_changes($result).' )';
}
$output.= "\n";
$debug .= $output;
}
return $result;
}
function pwg_db_nextval($column, $table)
{
$query = '
SELECT nextval(\''.$table.'_'.$column.'_seq\')';
list($next) = pwg_db_fetch_row(pwg_query($query));
return $next;
}
/**
*
* complex functions
*
*/
function pwg_db_changes($result)
{
return pg_affected_rows($result);
}
function pwg_db_num_rows($result)
{
return pg_num_rows($result);
}
function pwg_db_fetch_assoc($result)
{
return pg_fetch_assoc($result);
}
function pwg_db_fetch_row($result)
{
return pg_fetch_row($result);
}
function pwg_db_fetch_object($result)
{
return pg_fetch_object($result);
}
function pwg_db_free_result($result)
{
return pg_free_result($result);
}
function pwg_db_real_escape_string($s)
{
return pg_escape_string($s);
}
function pwg_db_insert_id()
{
// select currval('piwigo_user_id_seq');
}
/**
*
* complex functions
*
*/
/**
* creates an array based on a query, this function is a very common pattern
* used here
*
* @param string $query
* @param string $fieldname
* @return array
*/
function array_from_query($query, $fieldname)
{
$array = array();
$result = pwg_query($query);
while ($row = pg_fetch_assoc($result))
{
array_push($array, $row[$fieldname]);
}
return $array;
}
define('MASS_UPDATES_SKIP_EMPTY', 1);
/**
* updates multiple lines in a table
*
* @param string table_name
* @param array dbfields
* @param array datas
* @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
* @return void
*/
function mass_updates($tablename, $dbfields, $datas, $flags=0)
{
if (count($datas) == 0)
return;
// depending on the MySQL version, we use the multi table update or N update queries
if (count($datas) < 10)
{ // MySQL is prior to version 4.0.4, multi table update feature is not available
foreach ($datas as $data)
{
$query = '
UPDATE '.$tablename.'
SET ';
$is_first = true;
foreach ($dbfields['update'] as $key)
{
$separator = $is_first ? '' : ",\n ";
if (isset($data[$key]) and $data[$key] != '')
{
$query.= $separator.$key.' = \''.$data[$key].'\'';
}
else
{
if ($flags & MASS_UPDATES_SKIP_EMPTY )
continue; // next field
$query.= "$separator$key = NULL";
}
$is_first = false;
}
if (!$is_first)
{// only if one field at least updated
$query.= '
WHERE ';
$is_first = true;
foreach ($dbfields['primary'] as $key)
{
if (!$is_first)
{
$query.= ' AND ';
}
if ( isset($data[$key]) )
{
$query.= $key.' = \''.$data[$key].'\'';
}
else
{
$query.= $key.' IS NULL';
}
$is_first = false;
}
pwg_query($query);
}
} // foreach update
} // if mysql_ver or count");
trigger_error($error, E_USER_WARNING);
echo("");
}
?>