/** * Tests an UPDATE TABLE query * @param string table The table name to get DDL * @param string dbType MySQL, MSSQL, etc. * @param string query The query to test. * @return string Non-empty if error found */ function testQueryUpdate($table, $dbType, $query) { logThis('verifying UPDATE TABLE statement...'); global $db; if (empty($db)) { $db =& DBManagerFactory::getInstance(); } $error = ''; switch ($dbType) { case 'mysql': // get DDL $q = "SHOW CREATE TABLE {$table}"; $r = $db->query($q); $a = $db->fetchByAssoc($r); // rewrite DDL with _temp name $cleanQuery = cleanQuery($a['Create Table']); $tempTableQuery = str_replace("CREATE TABLE `{$table}`", "CREATE TABLE `{$table}__uw_temp`", $cleanQuery); $r2 = $db->query($tempTableQuery); // get sample data into the temp table to test for data/constraint conflicts logThis('inserting temp dataset...'); $q3 = "INSERT INTO `{$table}__uw_temp` SELECT * FROM `{$table}` LIMIT 10"; $r3 = $db->query($q3, false, "Preflight Failed for: {$query}"); // test the query on the test table logThis('testing query: [' . $query . ']'); $tempTableTestQuery = str_replace("UPDATE `{$table}`", "UPDATE `{$table}__uw_temp`", $query); // make sure the test query is running against a temp table if (isRunningAgainstTrueTable($tempTableTestQuery)) { $error = getFormattedError('Could not use a temp table to test query!', $tempTableTestQuery); return $error; } $r4 = $db->query($tempTableTestQuery, false, "Preflight Failed for: {$query}"); $error = mysql_error(); // empty on no-errors if (!empty($error)) { logThis('*** ERROR: query failed.'); $error = getFormattedError($error, $query); } break; case 'mssql': break; case 'oci8': logThis('Oracle found: skipping test query - [' . $query . ']'); break; } logThis('verification done.'); return $error; }
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; }
/** * runs one line of sql * @param array $persistence * @return array $persistence */ function commitAjaxRunSql($persistence) { global $db; if (!isset($persistence['commit_sql_errors'])) { $persistence['commit_sql_errors'] = array(); } // This flag is determined by the preflight check in the installer if ($persistence['schema_change'] == 'sugar') { if (isset($persistence['sql_to_run']) && count($persistence['sql_to_run']) > 0 && !empty($persistence['sql_to_run'])) { $sql = array_shift($persistence['sql_to_run']); $sql = trim($sql); if (!empty($sql)) { logThis("[RUNNING SQL QUERY] {$sql}"); $db->query($sql); $error = ''; switch ($db->dbType) { case 'mysql': $error = mysql_error(); break; case 'oci8': break; case 'mssql': break; } if (!empty($error)) { logThis('************************************************************'); logThis('*** ERROR: SQL Commit Error!'); logThis('*** Query: [ ' . $sql . ' ]'); logThis('************************************************************'); $persistence['commit_sql_errors'][] = getFormattedError($error, $sql); } $persistence = ajaxSqlProgress($persistence, $sql, 'sql_to_run'); } } else { ob_start(); echo 'done'; ob_flush(); } } else { ob_start(); echo 'done'; ob_flush(); } return $persistence; }