Commit 217ffd0a authored by Carsten  Rose's avatar Carsten Rose
Browse files

4980 / Variables in Report: a) nested not replaced, b) 'rbeg' not replaced, c) missing unit tests.

Report.php: 'line.count' general available per row. 'line.insertId'. RBGD moved inside of processing result. Replace variables in RBEG.
Variables.php: RegExp changed from 'all variables in double curly braces' to 'line.varname' - the other variables are replaced in eval.
Evaluate.php: Fixed problem that 'Dropdown Select' with '{{! SE...' not recognized.
parent b9641fa5
......@@ -196,15 +196,14 @@ class Evaluate {
$token = trim($token);
// just to extract the first token: check if this is a SQL Statement
$arr = explode(' ', $token, 2);
if ($token[0] === '!') {
$token = substr($token, 1);
$arr[0] = substr($arr[0], 1);
$token = trim(substr($token, 1));
$sqlMode = ROW_REGULAR;
}
// just to extract the first token: check if this is a SQL Statement
$arr = explode(' ', $token, 2);
// SQL Statement?
if (in_array(strtoupper($arr[0] . ' '), $this->sqlKeywords)) {
$foundInStore = TOKEN_FOUND_IN_STORE_QUERY;
......@@ -215,10 +214,8 @@ class Evaluate {
// explode for: <key>:<store priority>:<sanitize class>:<escape>:<default>
$arr = explode(':', $token, 5);
$arr = array_merge($arr, [null, null, null, null, null]); // fake isset()
$escapeTypes = $arr[3];
if ($escapeTypes == '') {
$escapeTypes = $this->escapeTypeDefault;
}
$escapeTypes = (empty($arr[3])) ? $this->escapeTypeDefault : $arr[3];
// search for value in stores
$value = $this->store->getVar($arr[0], $arr[1], $arr[2], $foundInStore);
......
......@@ -357,14 +357,14 @@ class Report {
}
/**
* Executes the queries recursive. This Method is called for each Sublevel.
* Executes the queries recursive. This Method is called for each sublevel.
*
* ROOTLEVEL
* This method is called once from the main method.
* For the first call the method executes the rootlevels
*
* SUBLEVEL
* For each rootlevel the method calls it self whith the levelmode 0
* For each rootlevel the method calls it self with the level mode 0
* If the next Level is a Sublevel it will be executed and $this->counter will be added by 1
* The sublevel calls the method again for a following sublevel
*
......@@ -426,6 +426,7 @@ class Report {
// Prepare SQL: replace variables. Actual 'line.total' or 'line.count' will recalculated: don't replace them now!
unset($this->variables->resultArray[$full_level . ".line."]["total"]);
unset($this->variables->resultArray[$full_level . ".line."]["count"]);
$sql = $this->variables->doVariables($this->frArray[$full_level . "." . TOKEN_SQL]);
$this->store->setVar(SYSTEM_SQL_FINAL, $sql, STORE_SYSTEM);
......@@ -440,26 +441,27 @@ class Report {
$rowTotal = isset($stat[DB_NUM_ROWS]) ? $stat[DB_NUM_ROWS] : $stat[DB_AFFECTED_ROWS];
$this->variables->resultArray[$full_level . ".line."]["total"] = $rowTotal;
if (isset($stat[DB_INSERT_ID])) {
$this->variables->resultArray[$full_level . ".line."]["insertId"] = $stat[DB_INSERT_ID];
}
$this->variables->resultArray[$full_level . ".line."]["count"] = is_array($result) ? 1 : 0;
$this->variables->resultArray[$full_level . ".line."]["insertId"] = isset($stat[DB_INSERT_ID]) ? $stat[DB_INSERT_ID] : 0;
// HEAD: If there is at least one record, do 'head'.
if ($rowTotal > 0) {
$content .= $this->variables->doVariables($this->frArray[$full_level . "." . TOKEN_HEAD]);
}
// Prepare row alteration
$arrRbgd = explode("|", $this->frArray[$full_level . "." . TOKEN_RBGD]);
if (count($arrRbgd) < 2) {
$arrRbgd[] = '';
$arrRbgd[] = '';
}
if (is_array($result)) {
// Prepare row alteration
$arrRbgd = explode("|", $this->frArray[$full_level . "." . TOKEN_RBGD], 2);
if (count($arrRbgd) < 2) {
$arrRbgd[] = '';
$arrRbgd[] = '';
}
//---------------------------------
// Process each row of resultset
$columnValueSeperator = "";
$columnValueSeparator = "";
$rowIndex = 0;
foreach ($result as $row) {
// record number counter
......@@ -471,12 +473,15 @@ class Report {
$row[$ii] = str_replace("{{" . $full_level . ".line.total}}", $rowTotal, $row[$ii]);
}
// SEP set seperator (empty on first run)
$content .= $columnValueSeperator;
$columnValueSeperator = $this->variables->doVariables($this->frArray[$full_level . "." . TOKEN_RSEP]);
// SEP set separator (empty on first run)
$content .= $columnValueSeparator;
$columnValueSeparator = $this->variables->doVariables($this->frArray[$full_level . "." . TOKEN_RSEP]);
// RBEG
$rbeg = $this->variables->doVariables($this->frArray[$full_level . "." . TOKEN_RBEG]);
// RBGD: even/odd rows
$content .= str_replace(TOKEN_RBGD, $arrRbgd[$rowIndex % 2], $this->frArray[$full_level . "." . TOKEN_RBEG]);
$content .= str_replace(TOKEN_RBGD, $arrRbgd[$rowIndex % 2], $rbeg);
//-----------------------------
// COLUMNS: Collect all columns
......
......@@ -59,8 +59,18 @@ class Variables {
* @return mixed
*/
public function doVariables($text) {
if ($text == '') {
return '';
}
// $str = preg_replace_callback("/(~([a-zA-Z0-9._])*)/", array($this, 'replaceVariables'), $text);
$str = preg_replace_callback("/{{(([a-zA-Z0-9.:_])*)}}/", array($this, 'replaceVariables'), $text);
// $str = preg_replace_callback("/{{(([a-zA-Z0-9.:_])*)}}/", array($this, 'replaceVariables'), $text);
// Process all {{x[.x].name}}
$str = preg_replace_callback('/{{\s*(([0-9]+.)+[a-zA-Z0-9_.]+)\s*}}/', array($this, 'replaceVariables'), $text);
// Try the Stores
$str = $this->eval->parse($str);
return $str;
}
......@@ -93,11 +103,11 @@ class Variables {
}
// If not replaced, try the Stores
if ($data === $matches[0]) {
$dataTmp = $this->eval->parse($data);
if ($dataTmp !== false)
$data = $dataTmp;
}
// if ($data === $matches[0]) {
// $dataTmp = $this->eval->parse($data);
// if ($dataTmp !== false)
// $data = $dataTmp;
// }
return $data;
}
......
......@@ -10,6 +10,8 @@ require_once(__DIR__ . '/../../qfq/report/Report.php');
require_once(__DIR__ . '/../../qfq/Evaluate.php');
require_once(__DIR__ . '/../../qfq/store/Session.php');
const TOTAL_COUNT_PERSON_GENERIC_SQL = 2;
/**
* Created by PhpStorm.
* User: crose
......@@ -945,16 +947,97 @@ EOF;
/**
*
*/
public function testReportSurpress() {
public function testReportReplace() {
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id LIMIT 1");
$this->assertEquals("normal text ", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id LIMIT 1\n10.10.sql = SELECT '{{10.hidden}}'");
$this->assertEquals("normal text hidden", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id LIMIT 1\n10.10.sql = SELECT '{{10.unknown}}'");
$this->assertEquals("normal text {{10.unknown}}", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id LIMIT 1\n10.10.sql = SELECT '{{fake}}'");
$this->assertEquals("normal text {{fake}}", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id LIMIT 1\n10.10.sql = SELECT '{{fake:V}}'");
$this->assertEquals("normal text {{fake:V}}", $result);
$this->store->setVar('fake', 'hello world ', STORE_VAR);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden ' AS _hidden, 'text ' FROM Person ORDER BY id LIMIT 1\n10.10.sql = SELECT '{{fake:V}}'");
$this->assertEquals("normal text hello world ", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id\n10.10.sql = SELECT '{{fake:V}}'");
$this->assertEquals("normal text hello world normal text hello world ", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id\n10.10.sql = SELECT '{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}} '");
$this->assertEquals("normal text hello world -1-2-0 normal text hello world -2-2-0 ", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id\n10.10.sql = SELECT '{{fake:V}}-{{10.line.count}}-{{10.line.total}} '");
$this->assertEquals("normal text hello world -1-2 normal text hello world -2-2 ", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id\n10.10.sql = SELECT '{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.10.line.count}}-{{10.10.line.total}} '");
$this->assertEquals("normal text hello world -1-2-1-1 normal text hello world -2-2-1-1 ", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id\n10.10.sql = SELECT '{{fake:V:::not found}} '");
$this->assertEquals("normal text hello world normal text hello world ", $result);
$result = $this->report->process("10.sql = SELECT 'normal', 'hidden' AS _hidden, 'text' FROM Person ORDER BY id LIMIT 1");
$this->assertEquals("normaltext", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id\n10.10.sql = SELECT '{{fakeDontExist:V:::not found}} '");
$this->assertEquals("normal text not found normal text not found ", $result);
$result = $this->report->process("10.sql = SELECT 'normal ', 'hidden' AS _hidden, 'text ' FROM Person ORDER BY id\n10.10.sql = SELECT '{{fakeDontExist:V:::{{EDIT_FORM_PAGE:Y}}}} '");
$this->assertEquals("normal text form normal text form ", $result);
// store various
// store default
// head.tail,rbeg,.seop mit variabeln
// 5.sql im 10 head abragen
}
/**
*
*/
public function testReportPageWrapperReplace() {
$line = <<<EOF
10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2
10.head = h:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.tail = t:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.rbeg = rb:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.rend = re:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.renr = rr:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.fbeg = -fb:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.fend = -fe:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.rsep = rs:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.fsep = fs:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
EOF;
$this->store->setVar('fake', 'hello world', STORE_VAR);
$result = $this->report->process($line);
$expect = "h:hello world-1-2-0,rb:hello world-1-2-0,-fb:hello world-1-2-0,Doe-fe:hello world-1-2-0,fs:hello world-1-2-0,-fb:hello world-1-2-0,John-fe:hello world-1-2-0,re:hello world-1-2-0,rr:hello world-1-2-0,rs:hello world-1-2-0,rb:hello world-2-2-0,-fb:hello world-2-2-0,Smith-fe:hello world-2-2-0,fs:hello world-2-2-0,-fb:hello world-2-2-0,Jane-fe:hello world-2-2-0,re:hello world-2-2-0,rr:hello world-2-2-0,t:hello world-2-2-0,";
$this->assertEquals($expect, $result);
$line = <<<EOF
10.sql = SELECT name, firstname FROM Person ORDER BY id LIMIT 2
10.10.sql = SELECT ' blue '
10.10.head = h:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.10.tail = t:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.10.rbeg = rb:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.10.rend = re:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.10.renr = rr:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.10.fbeg = -fb:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.10.fend = -fe:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.10.rsep = rs:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
10.10.fsep = fs:{{fake:V}}-{{10.line.count}}-{{10.line.total}}-{{10.line.insertId}},
EOF;
$result = $this->report->process("10.sql = SELECT 'normal', 'hidden' AS _hidden, 'text' FROM Person ORDER BY id LIMIT 1\n10.10.sql = SELECT '{{10.hidden}}'");
$this->assertEquals("normaltexthidden", $result);
$this->store->setVar('fake', 'hello world', STORE_VAR);
$result = $this->report->process($line);
$expect = "DoeJohnh:hello world-1-2-0,rb:hello world-1-2-0,-fb:hello world-1-2-0, blue -fe:hello world-1-2-0,re:hello world-1-2-0,rr:hello world-1-2-0,t:hello world-1-2-0,SmithJaneh:hello world-2-2-0,rb:hello world-2-2-0,-fb:hello world-2-2-0, blue -fe:hello world-2-2-0,re:hello world-2-2-0,rr:hello world-2-2-0,t:hello world-2-2-0,";
$this->assertEquals($expect, $result);
$result = $this->report->process("10.sql = SELECT 'normal', 'hidden' AS _hidden, 'text' FROM Person ORDER BY id LIMIT 1\n10.10.sql = SELECT '{{10.unknown}}'");
$this->assertEquals("normaltext{{10.unknown}}", $result);
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment