Allow SQL within SQL to render values (for defaults and list of options)
This commit is contained in:
parent
8a68af7e06
commit
17b457f527
113
pages/run.php
113
pages/run.php
@ -43,17 +43,52 @@
|
|||||||
catch (Exception $x) {}
|
catch (Exception $x) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WARNING: Can be abused.
|
||||||
|
// Translate occurrences of {{ SQL: select 'test' }} into the string value of the first cell of the first row:
|
||||||
|
// Can be used in string literals or default values in comments!
|
||||||
|
preg_match_all(
|
||||||
|
"/\{\{\s*SQL\:\s*(.*)\s+?\}\}/i",
|
||||||
|
$strFileData,
|
||||||
|
$varSQLReplacements);
|
||||||
|
|
||||||
|
for ($i = 0; $i < count($varSQLReplacements[0]); $i++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$strMatch = $varSQLReplacements[0][$i];
|
||||||
|
$strMatchSafe = preg_quote($strMatch);
|
||||||
|
$strSQLString = $varSQLReplacements[1][$i];
|
||||||
|
$varTempRows = $c->query($strSQLString);
|
||||||
|
|
||||||
|
//Respond::json($varTempRows);
|
||||||
|
|
||||||
|
$strFirstCell = "";
|
||||||
|
if (count($varTempRows) > 0)
|
||||||
|
foreach ($varTempRows[0] as $k => $v)
|
||||||
|
{
|
||||||
|
$strFirstCell = $varTempRows[0][$k];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$strFileData = preg_replace(
|
||||||
|
"/{$strMatchSafe}/i",
|
||||||
|
$strFirstCell,
|
||||||
|
$strFileData);
|
||||||
|
}
|
||||||
|
catch (Exception $x) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Respond::json($strFileData);
|
||||||
|
|
||||||
//foreach ($varReplacements as $k => $v)
|
//foreach ($varReplacements as $k => $v)
|
||||||
// $strFileData = preg_replace("/\{\{{$k}\}\}/i", $v, $strFileData);
|
// $strFileData = preg_replace("/\{\{{$k}\}\}/i", $v, $strFileData);
|
||||||
|
|
||||||
// Get the inputs:
|
// Get the inputs:
|
||||||
preg_match_all(
|
preg_match_all(
|
||||||
"/declare\s+\@([A-Za-z0-9]{1,})\s+(.+)\s+=\s+\?;(\s+-- Default\:\s(.*))?/i",
|
"/declare\s+\@([A-Za-z0-9]{1,})\s+(.+)\s+=\s+\?;(\s+-- ([A-Za-z0-9]{1,})\:\s(.*))?/i",
|
||||||
$strFileData,
|
$strFileData,
|
||||||
$varInputs);
|
$varInputs);
|
||||||
|
|
||||||
//Respond::json($varInputs);
|
|
||||||
|
|
||||||
// Get the options defined in comments:
|
// Get the options defined in comments:
|
||||||
preg_match_all(
|
preg_match_all(
|
||||||
"/--\s+([A-Za-z0-9]{1,})\:\s+(.+)/i",
|
"/--\s+([A-Za-z0-9]{1,})\:\s+(.+)/i",
|
||||||
@ -95,19 +130,28 @@
|
|||||||
$intAllowed = 1;
|
$intAllowed = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
$intRun = Request::getParam("run");
|
$intRun = Request::getParam("run");
|
||||||
|
$strFormat = Request::getParam("format");
|
||||||
|
|
||||||
//if (Request::posts("run_query"))
|
//if (Request::posts("run_query"))
|
||||||
if ($intRun !== null && $intRun == 1 && $intAllowed == 1)
|
if ($intRun !== null && $intRun == 1 && $intAllowed == 1)
|
||||||
{
|
{
|
||||||
$varPosted = Request::getParams();
|
$varPosted = Request::getParams();
|
||||||
unset($varPosted["run"]);
|
|
||||||
|
// Remove these keys from submitted fields:
|
||||||
|
foreach (["run", "format"] as $k)
|
||||||
|
if (array_key_exists($k, $varPosted))
|
||||||
|
unset($varPosted[$k]);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$varRows = $c->query("{$strQueryDir}/{$strSelection}", $varPosted);
|
$varRows = $c->query("{$strQueryDir}/{$strSelection}", $varPosted);
|
||||||
$strView = "files/views/{$strSelection}.php";
|
$strView = "files/views/{$strSelection}.php";
|
||||||
|
|
||||||
|
// Allow returning the data as JSON for APIs, maybe:
|
||||||
|
if (strtolower($strFormat) == "json")
|
||||||
|
Respond::json($varRows);
|
||||||
|
|
||||||
// Load a custom view if there is one:
|
// Load a custom view if there is one:
|
||||||
if (file_exists($strView))
|
if (file_exists($strView))
|
||||||
{
|
{
|
||||||
@ -249,8 +293,16 @@
|
|||||||
$strInputTypeHTML = "date";
|
$strInputTypeHTML = "date";
|
||||||
|
|
||||||
$strInputDefaultValue = "";
|
$strInputDefaultValue = "";
|
||||||
if (strlen($varInputs[4][$i]) > 0)
|
if (strtolower($varInputs[4][$i]) == "default")
|
||||||
$strInputDefaultValue = $varInputs[4][$i];
|
if (strlen($varInputs[5][$i]) > 0)
|
||||||
|
$strInputDefaultValue = $varInputs[5][$i];
|
||||||
|
|
||||||
|
$varOptions = [];
|
||||||
|
if (strtolower($varInputs[4][$i]) == "options")
|
||||||
|
{
|
||||||
|
$strOptionsList = $varInputs[5][$i];
|
||||||
|
$varOptions = explode(",", $strOptionsList);
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
@ -258,10 +310,37 @@
|
|||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label><?= $strInputName; ?> <code><?= $strInputType; ?></code></label>
|
<label><?= $strInputName; ?> <code><?= $strInputType; ?></code></label>
|
||||||
|
|
||||||
<div class="input-group">
|
<?php if (count($varOptions) > 0): ?>
|
||||||
<span class="input-group-text"><i class="fa fa-fw fa-at"></i></span>
|
|
||||||
<input type="<?= $strInputTypeHTML; ?>" class="form-control" name="<?= $strInputName; ?>" value="<?= Request::getParam($strInputName) ?? $strInputDefaultValue; ?>" />
|
<select class="form-select" name="<?= $strInputName; ?>">
|
||||||
</div>
|
<?php foreach ($varOptions as $o): ?>
|
||||||
|
<?php
|
||||||
|
$strLabel = $o;
|
||||||
|
$strValue = $o;
|
||||||
|
|
||||||
|
if (preg_match("/=.+$/i", $o))
|
||||||
|
{
|
||||||
|
preg_match("/(.+)=(.+)$/i", $o, $varTempMatches);
|
||||||
|
$strLabel = $varTempMatches[1];
|
||||||
|
$strValue = $varTempMatches[2];
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
<option
|
||||||
|
value="<?= $strValue; ?>"
|
||||||
|
<?= Request::getParam($strInputName) == $strValue? "selected": ""; ?>
|
||||||
|
><?= $strLabel; ?></option>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<?php else: ?>
|
||||||
|
|
||||||
|
<div class="input-group">
|
||||||
|
<span class="input-group-text"><i class="fa fa-fw fa-at"></i></span>
|
||||||
|
<input type="<?= $strInputTypeHTML; ?>" class="form-control" name="<?= $strInputName; ?>" value="<?= Request::getParam($strInputName) ?? $strInputDefaultValue; ?>" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -448,3 +527,15 @@
|
|||||||
|
|
||||||
<?php DataTable::js("#table"); ?>
|
<?php DataTable::js("#table"); ?>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
|
|
||||||
|
<?php $intAutorun = Request::getParam("autorun"); ?>
|
||||||
|
<?php if ($intAutorun !== null && $intAutorun == 1): ?>
|
||||||
|
<script>
|
||||||
|
$(function() {
|
||||||
|
$("#page-input form")
|
||||||
|
.first()
|
||||||
|
.submit();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<?php endif; ?>
|
||||||
|
Loading…
Reference in New Issue
Block a user