Beispiel #1
0
/**
 * tries to validate the query based on type
 * @param string query The query to verify
 * @param string dbType The DB type
 * @return string error Non-empty string on error
 */
function verifySqlStatement($query, $dbType, &$newTables)
{
    $error = '';
    logThis('verifying SQL statement');
    $table = getTableFromQuery($query);
    switch (strtoupper(substr($query, 0, 10))) {
        // ignore DROPs
        case 'ALTER TABL':
            // get ddl
            $error = testQueryAlter($table, $dbType, strtoupper($query), $newTables);
            break;
        case 'CREATE TAB':
            $error = testQueryCreate($table, $dbType, $query, $newTables);
            break;
        case 'DELETE FRO':
            $error = testQueryDelete($table, $dbType, $query);
            break;
        case 'DROP TABLE':
            $error = testQueryDrop($table, $dbType, $query);
            break;
        case 'INSERT INT':
            $error = testQueryInsert($table, $dbType, $query);
            break;
        case strtoupper(substr($query, 0, 6)) == 'UPDATE':
            $error = testQueryUpdate($table, $dbType, $query);
            break;
    }
    return $error;
}
Beispiel #2
0
function preflightCheckJsonSchemaCheck($persistence)
{
    global $mod_strings;
    global $db;
    if (!isset($persistence['sql_check_done']) || $persistence['sql_check_done'] != true) {
        // must keep sql in order
        $completeLine = array_shift($persistence['sql_to_check']);
        $whatsLeft = count($persistence['sql_to_check']);
        // populate newTables array to prevent "getting sample data" from non-existent tables
        $newTables = array();
        if (strtoupper(substr($completeLine, 1, 5)) == 'CREAT') {
            $newTables[] = getTableFromQuery($completeLine);
        }
        logThis('Verifying statement: ' . $completeLine);
        $bad = $db->verifySQLStatement($completeLine, $newTables);
        if (!empty($bad)) {
            logThis('*** ERROR: schema change script has errors: ' . $completeLine);
            logThis('*** ' . $bad);
            $persistence['sql_errors'][] = getFormattedError($bad, $completeLine);
        }
        $persistence = ajaxSqlProgress($persistence, $completeLine, 'sql_to_check');
    } else {
        $persistence['sql_to_check'] = $persistence['sql_to_check_backup'];
        echo 'done';
    }
    return $persistence;
}
logThis('looking for schema script at: ' . $sqlScript);
if (is_file($sqlScript)) {
    logThis('found schema upgrade script: ' . $sqlScript);
    logThis('schema preflight using MySQL');
    $fp = fopen($sqlScript, 'r');
    $contents = fread($fp, filesize($sqlScript));
    if (rewind($fp)) {
        $completeLine = '';
        while ($line = fgets($fp)) {
            if (strpos($line, '--') === false) {
                $completeLine .= " " . trim($line);
                if (strpos($line, ';') !== false) {
                    $completeLine = str_replace(';', '', $completeLine);
                    // populate newTables array to prevent "getting sample data" from non-existent tables
                    if (strtoupper(substr($completeLine, 1, 5)) == 'CREAT') {
                        $newTables[] = getTableFromQuery($completeLine);
                    }
                    $bad = verifySqlStatement(trim($completeLine), $db->dbType, $newTables);
                    if (!empty($bad)) {
                        logThis('*** ERROR: schema change script has errors - stopping execution');
                        $sqlErrors[] = $bad;
                    }
                    // reset for next SQL query
                    $completeLine = '';
                }
            }
        }
    } else {
        logThis('*** ERROR: could not read schema script: ' . $sqlScript);
        $sqlErrors[] = $mod_strings['ERR_UW_FILE_NOT_READABLE'] . '::' . $sqlScript;
    }