Skip to content

Commit

Permalink
LDEV-4867 preserve query comments and fix parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Jun 7, 2024
1 parent 6c69d0b commit 698345e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
8 changes: 7 additions & 1 deletion core/src/main/java/lucee/runtime/tag/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,13 @@ else if (data.cachedWithin == null) {
// cache not found, process and cache result if needed
if (queryResult == null) {
// QoQ
if ("query".equals(data.dbtype)) {
if ("parseonly".equals(data.dbtype)) {
Struct sct = new StructImpl();
sct.setEL(KeyConstants._SQL, sqlQuery.getSQLString());
if (setVars) pageContext.setVariable(data.result, sct);
return EVAL_PAGE;
}
else if ("query".equals(data.dbtype)) {
QueryImpl q = executeQoQ(pageContext, data, sqlQuery, tl);
q.setTemplateLine(tl);
if (data.returntype == RETURN_TYPE_ARRAY) queryResult = QueryArray.toQueryArray(q); // TODO this should be done in queryExecute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private static SQL convert(String sql, List<SQLItems<SQLItem>> items, List<SQLIt
if (c == '/' && sql.charAt(i + 1) == '*') {
int end = sql.indexOf("*/", i + 2);
if (end != -1) {
sb.append(sql.substring(i, end+2));
i = end + 2;
if (i == sqlLen) break;
c = sql.charAt(i);
Expand All @@ -161,11 +162,11 @@ private static SQL convert(String sql, List<SQLItems<SQLItem>> items, List<SQLIt
if (c == '-' && i < (sqlLen - 1) && sql.charAt(i + 1) == '-') {
int end = sql.indexOf('\n', i + 1);
if (end != -1) {
i = end + 1;
if (i == sqlLen) break;
c = sql.charAt(i);
sb.append(sql.substring(i, end+1));
i = end;
continue;
}
else break;
//else break;
}
}

Expand All @@ -190,7 +191,7 @@ else if (!inQuotes) {
continue;
}

if (++_qm > initialParamSize) throw new ApplicationException("there are more question marks in the SQL than params defined", "SQL: " + sql + "");
if (++_qm > initialParamSize) throw new ApplicationException("There are more question marks ["+(qm+1)+"] in the SQL than params defined ["+initialParamSize+"], at position ["+ i +"]", "SQL: " + sql + ", ParsedSQL:" + sb.toString());
}
else if (c == ':') {

Expand Down
63 changes: 63 additions & 0 deletions test/tickets/LDEV4867.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
component extends="org.lucee.cfml.test.LuceeTestCase" labels="query" {

variables.ds = server.getDatasource( service="h2", dbFile=server._getTempDir( "LDEV4867" ) );
variables.params = { a:1, b:2 };

function run( testResults , testBox ) {
describe( title='LDEV-4867' , body=function(){
it( title='test query parsing, removing comments' , body=function() {
local.result = doQuery("
-- foo
/* bar */
SELECT 'test'
");
//systemOutput( local.result.sql, true );
expect ( local.result.sql ).toInclude( "-- foo" );
expect ( local.result.sql ).toInclude( "/* bar */" );
});

it( title='test query parsing, with a ? in a comment' , body=function() {
local.result = doQuery("
-- foo
/* bar? */
SELECT 'test'
");
//systemOutput( local.result.sql, true );
expect ( local.result.sql ).toInclude("-- foo");
expect ( local.result.sql ).toInclude("/* bar? */");
});

it( title='test query parsing, with a ? in a comment' , body=function() {
local.result = doQuery("-- foo
/* bar? */
SELECT 'test'");
//systemOutput( local.result.sql, true );
expect ( local.result.sql ).toInclude("-- foo");
expect ( local.result.sql ).toInclude("/* bar? */");
});

it( title='test query parsing, with a ? in a comment' , body=function() {
local.result = doQuery("-- foo ? :do
/* bar? :*/
SELECT 'test'");
//systemOutput( local.result.sql, true );
expect ( local.result.sql ).toInclude("-- foo ? :do");
expect ( local.result.sql ).toInclude("/* bar? :*/");
});
});
}

private function doQuery( sql ){
try {
query name="local.test" datasource="#ds#" params="#params#" dbtype="parseOnly" result="local.result" {
echo( sql );
}
} catch (e) {
if ( e.stackTrace.indexOf("lucee.runtime.exp.DatabaseException:") neq 0 )
rethrow;
systemOutput(e.stackTrace, true);
}
return result;
}

}

0 comments on commit 698345e

Please sign in to comment.