query($query))
or die($query."\n
".$pwg_db_link->lastErrorMsg());
}
}
else
{
($result = $pwg_db_link->query($query))
or die($query."\n
".$pwg_db_link->errorInfo());
}
$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)
and !isset($truncate_query))
{
$output.= "\n".'(affected rows : ';
$output.= pwg_db_changes($result).' )';
}
$output.= "
\n";
$debug .= $output;
}
return $result;
}
function pwg_db_nextval($column, $table)
{
$query = '
SELECT MAX('.$column.')+1
FROM '.$table;
list($next) = pwg_db_fetch_row(pwg_query($query));
if (is_null($next))
{
$next = 1;
}
return $next;
}
/**
*
* complex functions
*
*/
function pwg_db_changes(PDOStatement $result=null)
{
return $result->rowCount();
}
function pwg_db_num_rows(PDOStatement $result)
{
return $result->rowCount();
}
function pwg_db_fetch_assoc($result)
{
return $result->fetch(PDO::FETCH_ASSOC);
}
function pwg_db_fetch_row($result)
{
return $result->fetch(PDO::FETCH_NUM);
}
function pwg_db_fetch_object($result)
{
return $result;
}
function pwg_db_free_result($result)
{
}
function pwg_db_real_escape_string($s)
{
global $pwg_db_link;
return trim($pwg_db_link->quote($s), "'");
}
function pwg_db_insert_id($table=null, $column='id')
{
global $pwg_db_link;
return $pwg_db_link->lastInsertRowID();
}
/**
*
* 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 = pwg_db_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;
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);
}
}
}
/**
* inserts multiple lines in a table
*
* @param string table_name
* @param array dbfields
* @param array inserts
* @return void
*/
function mass_inserts($table_name, $dbfields, $datas)
{
if (count($datas) != 0)
{
$first = true;
$packet_size = 16777216;
$packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/
$query = '';
foreach ($datas as $insert)
{
if (strlen($query) >= $packet_size)
{
pwg_query($query);
$first = true;
}
if ($first)
{
$query = '
INSERT INTO '.$table_name.'
('.implode(',', $dbfields).')
VALUES';
$first = false;
}
else
{
$query .= '
, ';
}
$query .= '(';
foreach ($dbfields as $field_id => $dbfield)
{
if ($field_id > 0)
{
$query .= ',';
}
if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
{
$query .= 'NULL';
}
else
{
$query .= "'".$insert[$dbfield]."'";
}
}
$query .= ')';
}
pwg_query($query);
}
}
/**
* Do maintenance on all PWG tables
*
* @return none
*/
function do_maintenance_all_tables()
{
global $prefixeTable, $page;
$all_tables = array();
// List all tables
$query = 'SELECT name FROM SQLITE_MASTER
WHERE name LIKE \''.$prefixeTable.'%\'';
$all_tables = array_from_query($query, 'name');
foreach ($all_tables as $table_name)
{
$query = 'VACUUM '.$table_name.';';
$result = pwg_query($query);
}
array_push($page['infos'],
l10n('All optimizations have been successfully completed.')
);
}
function pwg_db_concat($array)
{
return implode($array, ' || ');
}
function pwg_db_concat_ws($array, $separator)
{
$glue = sprintf(' || \'%s\' || ', $separator);
return implode($array, $glue);
}
function pwg_db_cast_to_text($string)
{
return $string;
}
/**
* returns an array containing the possible values of an enum field
*
* @param string tablename
* @param string fieldname
*/
function get_enums($table, $field)
{
$Enums['categories']['status'] = array('public', 'private');
$Enums['history']['section'] = array('categories','tags','search','list','favorites','most_visited','best_rated','recent_pics','recent_cats');
$Enums['user_infos']['status'] = array('webmaster','admin','normal','generic','guest');
$Enums['image']['type'] = array('picture','high','other');
$Enums['plugins']['state'] = array('active', 'inactive');
$Enums['user_cache_image']['access_type'] = array('NOT IN','IN');
$table = str_replace($GLOBALS['prefixeTable'], '', $table);
if (isset($Enums[$table][$field])) {
return $Enums[$table][$field];
} else {
return array();
}
}
// get_boolean transforms a string to a boolean value. If the string is
// "false" (case insensitive), then the boolean value false is returned. In
// any other case, true is returned.
function get_boolean( $string )
{
$boolean = true;
if ('f' === $string || 'false' === $string)
{
$boolean = false;
}
return $boolean;
}
/**
* returns boolean string 'true' or 'false' if the given var is boolean
*
* @param mixed $var
* @return mixed
*/
function boolean_to_string($var)
{
if (is_bool($var))
{
return $var ? 'true' : 'false';
}
else
{
return $var;
}
}
/**
*
* interval and date functions
*
*/
function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE')
{
if ($date!='CURRENT_DATE')
{
$date = '\''.$date.'\'';
}
return 'date('.$date.',\''.-$period.' DAY\')';
}
function pwg_db_get_recent_period($period, $date='CURRENT_DATE')
{
$query = 'select '.pwg_db_get_recent_period_expression($period, $date);
list($d) = pwg_db_fetch_row(pwg_query($query));
return $d;
}
function pwg_db_get_hour($date)
{
return 'strftime(\'%H\', '.$date.')';
}
function pwg_db_get_date_YYYYMM($date)
{
return 'strftime(\'%Y%m\','.$date.')';
}
function pwg_db_get_date_MMDD($date)
{
return 'strftime(\'%m%d\','.$date.')';
}
function pwg_db_get_year($date)
{
return 'strftime(\'%Y\','.$date.')';
}
function pwg_db_get_month($date)
{
return 'strftime(\'%m\','.$date.')';
}
function pwg_db_get_week($date, $mode=null)
{
return 'strftime(\'%W\','.$date.')';
}
function pwg_db_get_dayofmonth($date)
{
return 'strftime(\'%d\','.$date.')';
}
function pwg_db_get_dayofweek($date)
{
return 'strftime(\'%w\','.$date.')';
}
function pwg_db_get_weekday($date)
{
return 'strftime(\'%w\',date('.$date.',\'-1 DAY\'))';
}
// my_error returns (or send to standard output) the message concerning the
// error occured for the last mysql query.
function my_error($header, $die)
{
global $pwg_db_link;
$error = '';
if (isset($pwg_db_link))
{
$error .= '[sqlite error]'.$pwg_db_link->errorInfo()."\n";
}
$error .= $header;
if ($die)
{
fatal_error($error);
}
echo("");
trigger_error($error, E_USER_WARNING);
echo("
");
}
// sqlite create functions
function pwg_now()
{
return date('Y-m-d H:i:s');
}
function pwg_unix_timestamp()
{
return time();
}
function pwg_if($expression, $value1, $value2)
{
if ($expression)
{
return $value1;
}
else
{
return $value2;
}
}
function pwg_regexp($pattern, $string)
{
$pattern = sprintf('`%s`', $pattern);
return preg_match($pattern, $string);
}
?>