Example #1
0
function dbQuery($sql, $parameters = array())
{
    global $fullSql;
    $fullSql = dbMakeQuery($sql, $parameters);
    if (OBS_DEBUG > 0) {
        // Pre query debug output
        if (is_cli()) {
            $debug_sql = explode(PHP_EOL, $fullSql);
            print_message(PHP_EOL . 'SQL[%y' . implode('%n' . PHP_EOL . '%y', $debug_sql) . '%n]', 'console', FALSE);
        } else {
            print_sql($fullSql);
        }
    }
    if (OBS_DEBUG > 0 || $GLOBALS['config']['profile_sql']) {
        $time_start = microtime(true);
    }
    $result = dbCallQuery($fullSql);
    // sets $this->result
    if (OBS_DEBUG > 0 || $GLOBALS['config']['profile_sql']) {
        $runtime = number_format(microtime(true) - $time_start, 8);
        $debug_msg .= 'SQL RUNTIME[' . ($runtime > 0.05 ? '%r' : '%g') . $runtime . 's%n]';
        if ($GLOBALS['config']['profile_sql']) {
            #fwrite($this->logFile, date('Y-m-d H:i:s') . "\n" . $fullSql . "\n" . number_format($time_end - $time_start, 8) . " seconds\n\n");
            $GLOBALS['sql_profile'][] = array('sql' => $fullSql, 'time' => $runtime);
        }
    }
    if (OBS_DEBUG > 0) {
        if ($result === FALSE && error_reporting() & 1) {
            $error_msg = 'Error in query: (' . dbError() . ') ' . dbErrorNo();
            $debug_msg .= PHP_EOL . 'ERROR[%r' . $error_msg . '%n]';
        }
        if (is_cli()) {
            if (OBS_DEBUG > 1) {
                $rows = dbAffectedRows();
                $debug_msg = 'ROWS[' . ($rows < 1 ? '%r' : '%g') . $rows . '%n]' . PHP_EOL . $debug_msg;
            }
            // After query debug output for cli
            print_message($debug_msg, 'console', FALSE);
        } else {
            print_error($error_msg);
        }
    }
    if ($result === FALSE && isset($GLOBALS['config']['db']['debug']) && $GLOBALS['config']['db']['debug']) {
        logfile('db.log', 'Failed dbQuery (#' . dbErrorNo() . ' - ' . dbError() . '), Query: ' . $fullSql);
    }
    return $result;
}
Example #2
0
require $base_dir . "/config.php";
// Base dir, if it's not set in config
if (!isset($config['install_dir'])) {
    $config['install_dir'] = $base_dir;
}
// Include necessary supporting files
require_once $config['install_dir'] . "/includes/functions.inc.php";
require $config['install_dir'] . "/includes/definitions.inc.php";
// Common functions, for is_ssl and print_warning/print_error
include_once $config['install_dir'] . '/includes/common.inc.php';
// Connect to database
if (!$GLOBALS[OBS_DB_LINK]) {
    if (defined('OBS_DB_SKIP') && OBS_DB_SKIP === TRUE) {
        print_warning("WARNING: In PHP Unit tests we can skip DB connect. But if you test db functions, check your configs.");
    } else {
        print_error("DB Error " . dbErrorNo() . ": " . dbError());
        die;
        // Die if not PHP Unit tests
    }
} else {
    if (!get_db_version() && !(isset($options['u']) || isset($options['V']))) {
        if (!dbQuery('SELECT 1 FROM `devices` LIMIT 1;')) {
            // DB schema not installed, install first
            print_error("DB schema not installed, first install it.");
            die;
        }
    } else {
        // Disable STRICT mode for DB session (we not fully support them)
        $db_modes = explode(',', dbFetchCell("SELECT @@SESSION.sql_mode;"));
        $db_mode_exclude = 'STRICT_TRANS_TABLES';
        if (in_array($db_mode_exclude, $db_modes)) {
Example #3
0
 // Skip comments
 if ($line[0] == '#' || $line[0] == '-' || $line[0] == '/') {
     if (strpos($line, 'ERROR_IGNORE') !== FALSE || strpos($line, 'IGNORE_ERROR') !== FALSE) {
         $error_ignore = TRUE;
     } else {
         if (strpos($line, 'NOTE') !== FALSE) {
             list(, $note) = explode('NOTE', $line, 2);
             echo '(' . trim($note) . ')';
         }
     }
     continue;
 }
 print_debug($line);
 $update = dbQuery($line);
 if (!$update) {
     $error_no = dbErrorNo();
     $error_msg = "({$error_no}) " . dbError();
     if ($error_no >= 2000) {
         // Critical errors, stop update
         echo " stopped. Critical error: " . $error_msg . PHP_EOL;
         // http://dev.mysql.com/doc/refman/5.6/en/error-messages-client.html
         logfile('update-errors.log', "====== Schema update " . sprintf("%03d", $db_rev) . " -> " . sprintf("%03d", $filename) . " ==============");
         logfile('update-errors.log', "Query: " . $line);
         logfile('update-errors.log', "Error: " . $error_msg);
         del_process_info(-1);
         // Remove process info
         exit(1);
     } else {
         echo 'F';
         $err++;
         $errors[] = array('query' => $line, 'error' => $error_msg);