=') ) { if (DB_CHARSET!='') { pwg_query('SET NAMES "'.DB_CHARSET.'"'); } } elseif ( strtolower(PWG_CHARSET)!='iso-8859-1' ) { fatal_error('PWG supports only iso-8859-1 charset on MySql version '.mysql_get_server_info()); } } function pwg_get_db_version() { list($mysql_version) = pwg_db_fetch_row(pwg_query('SELECT VERSION();')); return $mysql_version; } function pwg_query($query) { global $conf,$page,$debug,$t2; $start = get_moment(); ($result = mysql_query($query)) or my_error($query, $conf['die_on_sql_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.= mysql_num_rows($result).' )';
    }
    elseif ( $result!=null
      and preg_match('/\s*INSERT|UPDATE|REPLACE|DELETE\s+/i',$query) )
    {
      $output.= "\n".'(affected rows   : ';
      $output.= mysql_affected_rows().' )';
    }
    $output.= "
\n"; $debug .= $output; } return $result; } function pwg_db_changes($result) { return mysql_affected_rows($result); } function pwg_db_num_rows($result) { return mysql_num_rows($result); } function pwg_db_fetch_assoc($result) { return mysql_fetch_assoc($result); } function pwg_db_fetch_row($result) { return mysql_fetch_row($result); } function pwg_db_fetch_object($result) { return mysql_fetch_object($result); } function pwg_free_result($result) { return mysql_free_result($result); } function pwg_db_real_escape_string($s) { return mysql_real_escape_string($s); } function pwg_db_insert_id() { return mysql_insert_id(); } /** * * 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 = mysql_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 or version_compare(mysql_get_server_info(), '4.0.4') < 0) { // 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= $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 .= ')'; } Log::getInstance()->debug($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_assoc($result)) { array_push($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') { array_push($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) { array_push( $page['infos'], l10n('Optimizations completed') ); } else { array_push( $page['errors'], l10n('Optimizations errors') ); } } // 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("
"); } ?>