$value)
{
$separator = $is_first ? '' : ",\n ";
if (isset($value) and $value !== '')
{
$query.= $separator.$key.' = \''.$value.'\'';
}
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 ($where_fields as $key => $value)
{
if (!$is_first)
{
$query.= ' AND ';
}
if ( isset($value) )
{
$query.= $key.' = \''.$value.'\'';
}
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, $options=array())
{
$ignore = '';
if (isset($options['ignore']) and $options['ignore'])
{
$ignore = 'IGNORE';
}
if (count($datas) != 0)
{
$first = true;
$query = 'SHOW VARIABLES LIKE \'max_allowed_packet\'';
list(, $packet_size) = pwg_db_fetch_row(pwg_query($query));
$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 '.$ignore.' 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);
}
}
/**
* inserts one line in a table
*
* @param string table_name
* @param array dbfields
* @param array insert
* @return void
*/
function single_insert($table_name, $data)
{
if (count($data) != 0)
{
$query = '
INSERT INTO '.$table_name.'
('.implode(',', array_keys($data)).')
VALUES';
$query .= '(';
$is_first = true;
foreach ($data as $key => $value)
{
if (!$is_first)
{
$query .= ',';
}
else
{
$is_first = false;
}
if ($value === '')
{
$query .= 'NULL';
}
else
{
$query .= "'".$value."'";
}
}
$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 = 'SHOW TABLES LIKE \''.$prefixeTable.'%\'';
$result = pwg_query($query);
while ($row = pwg_db_fetch_row($result))
{
$all_tables[] = $row[0];
}
// Repair all tables
$query = 'REPAIR TABLE '.implode(', ', $all_tables);
$mysql_rc = pwg_query($query);
// Re-Order all tables
foreach ($all_tables as $table_name)
{
$all_primary_key = array();
$query = 'DESC '.$table_name.';';
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
if ($row['Key'] == 'PRI')
{
$all_primary_key[] = $row['Field'];
}
}
if (count($all_primary_key) != 0)
{
$query = 'ALTER TABLE '.$table_name.' ORDER BY '.implode(', ', $all_primary_key).';';
$mysql_rc = $mysql_rc && pwg_query($query);
}
}
// Optimize all tables
$query = 'OPTIMIZE TABLE '.implode(', ', $all_tables);
$mysql_rc = $mysql_rc && pwg_query($query);
if ($mysql_rc)
{
$page['infos'][] = l10n('All optimizations have been successfully completed.');
}
else
{
$page['errors'][] = l10n('Optimizations have been completed with some errors.');
}
}
function pwg_db_concat($array)
{
$string = implode($array, ',');
return 'CONCAT('. $string.')';
}
function pwg_db_concat_ws($array, $separator)
{
$string = implode($array, ',');
return 'CONCAT_WS(\''.$separator.'\','. $string.')';
}
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)
{
// retrieving the properties of the table. Each line represents a field :
// columns are 'Field', 'Type'
$result = pwg_query('desc '.$table);
while ($row = pwg_db_fetch_assoc($result))
{
// we are only interested in the the field given in parameter for the
// function
if ($row['Field'] == $field)
{
// retrieving possible values of the enum field
// enum('blue','green','black')
$options = explode(',', substr($row['Type'], 5, -1));
foreach ($options as $i => $option)
{
$options[$i] = str_replace("'", '',$option);
}
}
}
pwg_db_free_result($result);
return $options;
}
/**
* Smartly checks if a variable is equivalent to true or false
*
* @param mixed input
* @return bool
*/
function get_boolean($input)
{
if ('false' === strtolower($input))
{
return false;
}
return (bool)$input;
}
/**
* 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 'SUBDATE('.$date.',INTERVAL '.$period.' DAY)';
}
function pwg_db_get_recent_period($period, $date='CURRENT_DATE')
{
$query = '
SELECT '.pwg_db_get_recent_period_expression($period);
list($d) = pwg_db_fetch_row(pwg_query($query));
return $d;
}
function pwg_db_get_flood_period_expression($seconds)
{
return 'SUBDATE(now(), INTERVAL '.$seconds.' SECOND)';
}
function pwg_db_get_hour($date)
{
return 'hour('.$date.')';
}
function pwg_db_get_date_YYYYMM($date)
{
return 'DATE_FORMAT('.$date.', \'%Y%m\')';
}
function pwg_db_get_date_MMDD($date)
{
return 'DATE_FORMAT('.$date.', \'%m%d\')';
}
function pwg_db_get_year($date)
{
return 'YEAR('.$date.')';
}
function pwg_db_get_month($date)
{
return 'MONTH('.$date.')';
}
function pwg_db_get_week($date, $mode=null)
{
if ($mode)
{
return 'WEEK('.$date.', '.$mode.')';
}
else
{
return 'WEEK('.$date.')';
}
}
function pwg_db_get_dayofmonth($date)
{
return 'DAYOFMONTH('.$date.')';
}
function pwg_db_get_dayofweek($date)
{
return 'DAYOFWEEK('.$date.')';
}
function pwg_db_get_weekday($date)
{
return 'WEEKDAY('.$date.')';
}
function pwg_db_date_to_ts($date)
{
return 'UNIX_TIMESTAMP('.$date.')';
}
// my_error returns (or send to standard output) the message concerning the
// error occured for the last mysql query.
function my_error($header, $die)
{
$error = "[mysql error ".mysql_errno().'] '.mysql_error()."\n";
$error .= $header;
if ($die)
{
fatal_error($error);
}
echo("");
trigger_error($error, E_USER_WARNING);
echo("
");
}
/**
* Builds an data array from a SQL query.
* Depending on $key_name and $value_name it can return :
*
* - an array of arrays of all fields (key=null, value=null)
* array(
* array('id'=>1, 'name'=>'DSC8956', ...),
* array('id'=>2, 'name'=>'DSC8957', ...),
* ...
* )
*
* - an array of a single field (key=null, value='...')
* array('DSC8956', 'DSC8957', ...)
*
* - an associative array of array of all fields (key='...', value=null)
* array(
* 'DSC8956' => array('id'=>1, 'name'=>'DSC8956', ...),
* 'DSC8957' => array('id'=>2, 'name'=>'DSC8957', ...),
* ...
* )
*
* - an associative array of a single field (key='...', value='...')
* array(
* 'DSC8956' => 1,
* 'DSC8957' => 2,
* ...
* )
*
* @since 2.6
*
* @param string $query
* @param string $key_name
* @param string $value_name
* @return array
*/
function query2array($query, $key_name=null, $value_name=null)
{
$result = pwg_query($query);
$data = array();
if (isset($key_name))
{
if (isset($value_name))
{
while ($row = pwg_db_fetch_assoc($result))
$data[ $row[$key_name] ] = $row[$value_name];
}
else
{
while ($row = pwg_db_fetch_assoc($result))
$data[ $row[$key_name] ] = $row;
}
}
else
{
if (isset($value_name))
{
while ($row = pwg_db_fetch_assoc($result))
$data[] = $row[$value_name];
}
else
{
while ($row = pwg_db_fetch_assoc($result))
$data[] = $row;
}
}
return $data;
}
?>