Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions classes/local/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,24 +674,33 @@ public static function save(\stdClass $data): int {
''
);
}
// SQL change while published: drop view + report so they get rebuilt on next publish.
// A transaction here would be illusory — tear_down() issues DROP VIEW via
// change_database_structure(), and DDL implicitly commits on MySQL. So demote the
// record to draft *first*, then tear down: if the teardown fails partway, the record
// already reads draft rather than claiming published over a destroyed view/report.
// SQL change while published: rebuild the view in place and keep the report (and its id, audiences,
// filters and blocks) so that existing links and embeds keep working. Columns that no longer exist
// are removed from the report by republish().
if ($existing->status === self::STATUS_PUBLISHED && $existing->querysql !== $sql) {
$record->status = self::STATUS_DRAFT;
$record->viewname = null;
$record->reportid = null;
$record->chartreportid = null;
$record->columnsmeta = null;
// Old column names no longer apply once the view is rebuilt.
$record->useridcolumn = null;
$record->coursecolumn = null;
$record->pagecoursecolumn = null;
$DB->update_record(self::TABLE, $record);
self::save_description_files((int) $record->id, $descdraftid, $desctext, $descformat);
self::tear_down((int) $existing->id, $existing);
try {
self::get((int) $record->id)->republish();
} catch (\Throwable $e) {
// The new SQL can't be compiled, so restore the old SQL and view.
$DB->update_record(self::TABLE, (object) [
'id' => $existing->id,
'querysql' => $existing->querysql,
'useridcolumn' => $existing->useridcolumn,
'coursecolumn' => $existing->coursecolumn,
'pagecoursecolumn' => $existing->pagecoursecolumn,
]);
try {
view::create_or_replace((int) $existing->id, $existing->querysql, (int) $existing->courseid);
} catch (\moodle_exception $restore) {
debugging('report_sql: failed to restore view: ' . $restore->getMessage());
}
throw $e;
}
\report_sql\event\query_updated::create_and_trigger($record->id, $record->name);
return $record->id;
}
Expand Down Expand Up @@ -874,6 +883,26 @@ public function publish(): void {
\report_sql\event\query_published::create_and_trigger($this->id(), $this->name());
}

/**
* Rebuild the view of a published query after its SQL changed, keeping every bound report.
*
* Report columns, filters and conditions which refer to view columns that no longer exist are removed.
*/
private function republish(): void {
$oldcolumns = array_keys($this->columns_meta());

// PostgreSQL can't rename or remove columns of an existing view with CREATE OR REPLACE.
view::drop($this->id());
$this->publish();

$newcolumns = array_keys($this->columns_meta());
foreach (array_diff($oldcolumns, $newcolumns) as $removed) {
foreach (self::bound_report_ids($this->id()) as $reportid) {
self::purge_report_column($reportid, $removed);
}
}
}

/**
* Drop view + RB report, set status to draft.
*/
Expand Down
31 changes: 30 additions & 1 deletion classes/local/sql/validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ public static function validate(string $sql): string {
$tree = [];
if (!$postgres) {
$compiled = view::compile(rtrim($sql, "; \t\n\r"));
$tree = self::parse($compiled);
// The MySQL parser misreads comments (e.g. as extra table references), so parse without them.
$tree = self::parse(self::strip_comments($compiled));
self::check_statement_type($tree);
mysql_validator::validate($tree, $compiled, self::denied_tables(), self::denied_columns());
}
Expand Down Expand Up @@ -413,6 +414,34 @@ private static function check_join_conditions(array $tree, string $stripped): vo
}
}

/**
* Remove comments from SQL, leaving quoted strings and identifiers untouched.
*
* This follows MySQL, as only its parser needs comment-free SQL: block comments don't nest, and "--" only starts a
* comment when followed by whitespace (so "1--1" is arithmetic).
*
* @param string $sql
* @return string
* @throws \moodle_exception for MySQL executable comments, as the database would run what the parser can't see.
*/
public static function strip_comments(string $sql): string {
$result = preg_replace_callback(
'/(\'(?:[^\'\\\\]|\\\\.|\'\')*\'|"(?:[^"]|"")*"|`[^`]*`)|\/\*.*?\*\/|--(?:[ \t\r\n\f]|$)[^\n]*|#[^\n]*/s',
static function (array $match): string {
if (isset($match[1]) && $match[1] !== '') {
return $match[1];
}
if (str_starts_with($match[0], '/*!')) {
throw new \moodle_exception('errdeniedkeyword', 'report_sql', '', '/*!');
}
return ' ';
},
$sql
);

return $result ?? $sql;
}

/**
* Replace string literals and comments with whitespace so the keyword scan can't be evaded
* via comments or string content.
Expand Down
3 changes: 2 additions & 1 deletion classes/local/sql/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ private static function issue_create_or_replace(string $viewname, string $valida
* @return string The compiled SQL sent to CREATE OR REPLACE VIEW.
*/
public static function compile(string $validatedsql, int $courseid = 0): string {
return self::normalise_aliases(self::resolve_placeholders($validatedsql, $courseid));
// End with a newline so a trailing "-- comment" can't swallow the ")" of a query wrapping this one.
return rtrim(self::normalise_aliases(self::resolve_placeholders($validatedsql, $courseid))) . "\n";
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/analyser_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public function test_invalid_sql_reports_error(): void {
$this->assertNotEmpty($result['error']);
}

/**
* A trailing line comment must not break the queries that wrap the SQL.
*/
public function test_trailing_comment(): void {
$this->resetAfterTest();
$result = analyser::analyse("SELECT\n id\nFROM\n {user}\n\n-- stuff");
$this->assertTrue($result['ok'], (string) ($result['error'] ?? ''));
}

/**
* SQL that passes static validation but references a non-existent table fails the live
* dry-run with ok=false and a message, rather than silently reporting rowcount -1.
Expand Down
47 changes: 38 additions & 9 deletions tests/query_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,24 +290,53 @@ public function test_unpublish_reverts_to_draft_and_clears_artefacts(): void {
$this->assertNull($record->columnsmeta);
}

public function test_sql_edit_while_published_demotes_to_draft(): void {
public function test_sql_edit_while_published_keeps_report(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();

$id = query::save($this->formdata());
$id = query::save($this->formdata(['querysql' => 'SELECT id, username FROM {user}']));
query::get($id)->publish();
$reportid = (int) query::get($id)->reportid();

// Changing the SQL on a published query tears down view + report and returns to draft.
query::save($this->formdata(['id' => $id, 'querysql' => 'SELECT id, username FROM {user}']));
$columns = \core_reportbuilder\local\models\column::get_records(['reportid' => $reportid]);
$this->assertNotEmpty($columns);

// Changing the SQL on a published query rebuilds the view but keeps the same report.
query::save($this->formdata(['id' => $id, 'querysql' => 'SELECT id, firstname FROM {user}']));

$record = $DB->get_record(query::TABLE, ['id' => $id], '*', MUST_EXIST);
$this->assertSame(query::STATUS_DRAFT, $record->status);
$this->assertNull($record->viewname);
$this->assertNull($record->reportid);
// The bound report config key is cleaned up by tear_down().
$this->assertFalse(get_config('report_sql', 'queryid_for_report_' . $reportid));
$this->assertSame(query::STATUS_PUBLISHED, $record->status);
$this->assertNotNull($record->viewname);
$this->assertEquals($reportid, $record->reportid);
$this->assertEquals($id, get_config('report_sql', 'queryid_for_report_' . $reportid));
$this->assertEqualsCanonicalizing(['id', 'firstname'], array_keys(query::get($id)->columns_meta()));

// The column that disappeared is removed from the report, the others are kept.
$identifiers = array_map(
fn($column) => $column->get('uniqueidentifier'),
\core_reportbuilder\local\models\column::get_records(['reportid' => $reportid])
);
$this->assertContains('adhoc:id', $identifiers);
$this->assertNotContains('adhoc:username', $identifiers);
}

public function test_sql_edit_while_published_with_invalid_sql_keeps_old_sql(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();

$id = query::save($this->formdata());
query::get($id)->publish();

try {
query::save($this->formdata(['id' => $id, 'querysql' => 'SELECT id, nosuchcolumn FROM {user}']));
$this->fail('Expected exception');
} catch (\Throwable $e) {
$record = $DB->get_record(query::TABLE, ['id' => $id], '*', MUST_EXIST);
$this->assertSame('SELECT id FROM {user}', $record->querysql);
$this->assertSame(query::STATUS_PUBLISHED, $record->status);
}
}

public function test_duplicate_creates_draft_copy(): void {
Expand Down
70 changes: 70 additions & 0 deletions tests/sql_validator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,4 +561,74 @@ public function test_function_policy_preserves_plain_xml_and_names(): void {
$this->assertNotEmpty(validator::validate('SELECT 1 AS query_to_xml'));
$this->assertNotEmpty(validator::validate("SELECT xmlconcat('<a/>'::xml, '<b/>'::xml) AS result"));
}

/**
* Comment and quoting tricks, as MySQL reads them (the only dialect which needs the comments removed for parsing)
*
* @return array[] [sql, expected result with comments replaced by a space]
*/
public static function strip_comments_provider(): array {
return [
'line comment' => ["SELECT 1 -- x", "SELECT 1 "],
'line comment with tab' => ["SELECT 1 --\tx\nFROM t", "SELECT 1 \nFROM t"],
'line comment at end of input' => ["SELECT 1 --", "SELECT 1 "],
'line comment with crlf' => ["SELECT 1 -- x\r\nFROM t", "SELECT 1 \nFROM t"],
'hash comment' => ["SELECT 1 # x\nFROM t", "SELECT 1 \nFROM t"],
'dashes without space are arithmetic' => ["SELECT 1--1 FROM t", "SELECT 1--1 FROM t"],
'dashes with letters are arithmetic' => ["SELECT 1--x INTO OUTFILE 'a'", "SELECT 1--x INTO OUTFILE 'a'"],
'block comment as whitespace' => ["SELECT/**/1", "SELECT 1"],
'block comment splitting a keyword' => ["SEL/**/ECT 1", "SEL ECT 1"],
'multiline block comment' => ["SELECT 1 /* a\nb */ FROM t", "SELECT 1 FROM t"],
'block comments do not nest' => ["SELECT 1 /* a /* b */ c */", "SELECT 1 c */"],
'comment hiding a second statement' => ["SELECT 1 /* ; DROP TABLE t; */", "SELECT 1 "],
'comment start inside a block comment' => ["SELECT /* -- */ 1", "SELECT 1"],
'unterminated block comment is left alone' => ["SELECT 1 /* x", "SELECT 1 /* x"],
'dashes in single quotes' => ["SELECT '-- x' FROM t", "SELECT '-- x' FROM t"],
'hash in single quotes' => ["SELECT '# x' FROM t", "SELECT '# x' FROM t"],
'block comment in single quotes' => ["SELECT '/* x */' FROM t", "SELECT '/* x */' FROM t"],
'dashes in double quotes' => ['SELECT "-- x" FROM t', 'SELECT "-- x" FROM t'],
'comment markers in backticks' => ["SELECT `a -- /* #` FROM t", "SELECT `a -- /* #` FROM t"],
'doubled single quote' => ["SELECT 'it''s -- not' FROM t -- yes", "SELECT 'it''s -- not' FROM t "],
'backslash escaped quote' => ["SELECT 'it\\'s -- not' FROM t -- yes", "SELECT 'it\\'s -- not' FROM t "],
'quote in a comment' => ["SELECT 1 -- it's\nFROM t", "SELECT 1 \nFROM t"],
'quote in a block comment' => ["SELECT /* ' */ 1 /* ' */", "SELECT 1 "],
'string after a comment' => ["SELECT 1 /* a */, '/* b */'", "SELECT 1 , '/* b */'"],
];
}

/**
* Comments are removed, but not when they are inside strings or quoted identifiers
*
* @param string $sql
* @param string $expected
* @dataProvider strip_comments_provider
*/
public function test_strip_comments(string $sql, string $expected): void {
$this->assertSame($expected, validator::strip_comments($sql));
}

/**
* MySQL runs executable comments, which the parser would not see
*
* @param string $sql
* @dataProvider executable_comments_provider
*/
public function test_strip_comments_rejects_executable_comments(string $sql): void {
$this->expectException(\moodle_exception::class);
validator::strip_comments($sql);
}

/**
* Executable comment variants
*
* @return string[][]
*/
public static function executable_comments_provider(): array {
return [
'versioned' => ['SELECT /*!50000 1 */ FROM t'],
'unversioned' => ['SELECT /*! 1 */ FROM t'],
'after a string' => ["SELECT 'a' /*!50000 , 2 */ FROM t"],
'after a line comment' => ["SELECT 1 -- x\n/*! , 2 */"],
];
}
}
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'report_sql';
$plugin->release = 2026091012;
$plugin->version = 2026091012;
$plugin->release = 2026091013;
$plugin->version = 2026091013;
$plugin->requires = 2024100700; // Moodle 4.5 LTS — the Reportbuilder API this plugin uses is stable from 4.5.
$plugin->maturity = MATURITY_STABLE;
$plugin->supported = [405, 503];
Loading