<?php

$SPADE['template']->_tpl_vars['pgLayout'] = 'file:layouts/admin.tpl';

// Here we can choose a different DB object if necessary...
$dbObject = &$SPADE['db'];

// May as well leave debug mode on, since the user BETTER have credidentials.
$dbObject->debugModeOn();

if ($_FILES['sqlFile']['name']) {
	if (preg_match("/\.sql$/i", $_FILES['sqlFile']['name'])) {
		$sqlContent = implode("", file($_FILES['sqlFile']['tmp_name']));
		if (!$sqlContent) {
			logAlert('An error occurred during file transfer or the file was empty.');
		}
	} else {
		logAlert('Uploaded file must be a SQL file (.sql extension).');
	}
} elseif ($_REQUEST['sqlFileOnServer']) {
	if (!preg_match("/\.sql$/i", $_REQUEST['sqlFileOnServer'])) {
		logAlert('File on server must be a SQL file.');
	} elseif (file_exists($_REQUEST['sqlFileOnServer'])) {
		$sqlContent = implode("", file($_REQUEST['sqlFileOnServer']));
	} else {
		logAlert('File not found: '.$_REQUEST['sqlFileOnServer']);
	}
} elseif ($_REQUEST['sqlQuery']) {
	$sqlContent = stripslashes($_REQUEST['sqlQuery']);
}

if ($_REQUEST['isConvertMySQLtoMSSQL']) {
	$sqlContent = mysql_dump_to_mssql($sqlContent);

}

if ($sqlContent) {
	$_REQUEST['sqlQuery'] = $sqlContent;
	if (!$_REQUEST['isConvertMySQLtoMSSQL'] || $_REQUEST['isExecuteConverted']) {
		// EXECUTE the QUERY
		$queryOutput = $dbObject->batchQuery($sqlContent);
		
		echo '<b>Query Output:</b><hr noshade size="1" />';
		if (is_array($queryOutput)) {
			echo_table($queryOutput);
		} else {
			echo '<pre>'.$queryOutput.'</pre>';
		}
		echo '<hr noshade size="1" />';
	}
}
	

?>

<form name="freeQuery" method="post" action="index.php" style="margin:0px;" enctype="multipart/form-data">
<input type="hidden" name="pg" value="<?php echo $SPADE['page']['name']; ?>">
<input type="hidden" name="editPreview" value="<?php echo $_REQUEST['editPreview']; ?>">

<b>SQL:</b><br />
Query:<br />
<textarea name="sqlQuery" rows="14" cols="75" wrap="off"><?php echo htmlspecialchars($_REQUEST['sqlQuery']); ?></textarea><br />
<br />
<i>or</i> path to SQL file on server:<br />
<input type="text" name="sqlFileOnServer" size="40" value="<?php echo $_REQUEST['sqlFileOnServer']; ?>"><br />
<br />
<i>or</i> SQL file to upload:<br />
<input type="file" name="sqlFile"> &nbsp; <input type="submit" value=" &nbsp; Go! &nbsp; "><br />
<br />

<table style="border:1px solid black;"><tr><td>
<span>
<b>SQL Conversion:</b><br />
<!-- I am not sure if we should have PHP save last input on this... after one conversion it's not likely you'll want to convert again, is it? -->
Convert MySQL to MSSQL: <input type="radio" name="isConvertMySQLtoMSSQL" value="1"<?php //if ($_REQUEST['isConvertMySQLtoMSSQL']) echo " checked"; ?>> yes <input type="radio" name="isConvertMySQLtoMSSQL" value=""<?php //if (!$_REQUEST['isConvertMySQLtoMSSQL']) echo " checked"; ?> checked> no<br />
<!--Show converted SQL: <input type="checkbox" name="isShowConverted" value="1" checked><br /> -->
Execute converted SQL: <input type="checkbox" name="isExecuteConverted" value="1"<?php if ($_REQUEST['isExecuteConverted']) echo " checked"; ?>><br />
</td></tr></table>

</span>
</form>
