Skip to content

Commit 247e31c

Browse files
fix: apply the MySQL comment rules to Dialect.MARIADB (#2891)
The -- whitespace rule and executable comments were only lexed the MySQL way under Dialect.MYSQL. MariaDB follows the same rules and also runs /*M! ... */, so under MARIADB SQL the server executes was dropped as a comment: SELECT 1 /*M! UNION SELECT pw FROM secrets */ parsed as SELECT 1. The rules now apply to MARIADB as well, with MariaDB's own version handling: a sixth digit always belongs to the version, and /*!50700 through /*!99999 are skipped while /*M! ones are not.
1 parent 2119fea commit 247e31c

5 files changed

Lines changed: 74 additions & 12 deletions

File tree

‎src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ public P withLegacyMySqlGroupBy(boolean enabled) {
116116

117117
/**
118118
* Resolves MySQL executable comments against this server version (for example 90702 for 9.7.2).
119-
* Requires the MYSQL dialect. Rendered SQL reflects this version, without conditional wrappers.
120-
* Without an explicit version, conditional executable comments raise a lexical error.
119+
* Requires the MYSQL dialect, or MARIADB with the MariaDB version (for example 110402 for
120+
* 11.4.2). Rendered SQL reflects this version, without conditional wrappers. Without an
121+
* explicit version, conditional executable comments raise a lexical error.
121122
*
122123
* @param version major * 10000 + minor * 100 + patch, from 0 through 999999
123124
* @return this parser

‎src/main/java/net/sf/jsqlparser/parser/SimpleCharStream.java‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@ && peekCharacters(1).equals("/")) {
251251
buffer[bufpos] = DOUBLE_SLASH_SUBSTITUTION;
252252
c = DOUBLE_SLASH_SUBSTITUTION;
253253
} else if ((c == '-' || c == '/' || c == '*') && featureConfiguration != null
254-
&& AbstractJSqlParser.Dialect.MYSQL.name()
255-
.equals(featureConfiguration.getValue(Feature.dialect))) {
254+
&& isMySqlCommentDialect(featureConfiguration.getValue(Feature.dialect))) {
256255
c = mySqlCommentBoundary(c);
257256
} else if (c == '\n' && featureConfiguration != null
258257
&& !featureConfiguration
@@ -266,6 +265,12 @@ && peekCharacters(2).equals("\n\n")) {
266265
return c;
267266
}
268267

268+
/** MariaDB shares the MySQL rules for {@code --} and executable comments. */
269+
private static boolean isMySqlCommentDialect(Object dialect) {
270+
return AbstractJSqlParser.Dialect.MYSQL.name().equals(dialect)
271+
|| AbstractJSqlParser.Dialect.MARIADB.name().equals(dialect);
272+
}
273+
269274
/** Changes only token boundaries, never characters being scanned inside strings or names. */
270275
private char mySqlCommentBoundary(char c) throws java.io.IOException {
271276
if (c == '-') {
@@ -282,23 +287,31 @@ private char mySqlCommentBoundary(char c) throws java.io.IOException {
282287
return ' ';
283288
} else if (c == '/') {
284289
String following = peekCharacters(9);
285-
if (following.startsWith("*!")) {
290+
boolean mariaDb = AbstractJSqlParser.Dialect.MARIADB.name()
291+
.equals(featureConfiguration.getValue(Feature.dialect));
292+
// MariaDB also runs its own /*M! ... */ comments, which MySQL ignores.
293+
int marker = following.startsWith("*!") ? 2
294+
: mariaDb && following.startsWith("*M!") ? 3 : 0;
295+
if (marker > 0) {
286296
if (mySqlExecutableComment) {
287297
return mySqlCommentError("Nested MySQL executable comments are not supported");
288298
}
289-
int digits = mySqlCommentVersionLength(following);
299+
int digits = mariaDb ? mariaDbCommentVersionLength(following, marker)
300+
: mySqlCommentVersionLength(following);
290301
if (digits > 0) {
291302
long target = featureConfiguration.getAsLong(Feature.mySqlServerVersion);
292303
if (target < 0 || target > 999999) {
293304
return mySqlCommentError("MySQL conditional executable comments require "
294305
+ "withMySqlServerVersion(major * 10000 + minor * 100 + patch)");
295306
}
296-
int minimum = Integer.parseInt(following.substring(2, 2 + digits));
297-
if (minimum > target) {
307+
int minimum = Integer.parseInt(following.substring(marker, marker + digits));
308+
// MariaDB skips /*! comments for MySQL 5.7 and later, but not /*M! ones.
309+
if (minimum > target
310+
|| mariaDb && marker == 2 && minimum >= 50700 && minimum <= 99999) {
298311
return c;
299312
}
300313
}
301-
eraseCommentMarker(3 + digits);
314+
eraseCommentMarker(marker + 1 + digits);
302315
mySqlExecutableComment = true;
303316
return ' ';
304317
}
@@ -328,6 +341,20 @@ private static int mySqlCommentVersionLength(String following) {
328341
: 5;
329342
}
330343

344+
private static int mariaDbCommentVersionLength(String following, int marker) {
345+
if (following.length() < marker + 5) {
346+
return 0;
347+
}
348+
for (int i = marker; i < marker + 5; i++) {
349+
if (following.charAt(i) < '0' || following.charAt(i) > '9') {
350+
return 0;
351+
}
352+
}
353+
// MariaDB takes a sixth digit whenever there is one, whatever follows it.
354+
return following.length() > marker + 5 && following.charAt(marker + 5) >= '0'
355+
&& following.charAt(marker + 5) <= '9' ? 6 : 5;
356+
}
357+
331358
private String peekCharacters(int length) {
332359
StringBuilder result = new StringBuilder(length);
333360
try {

‎src/main/java/net/sf/jsqlparser/parser/feature/Feature.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,8 +1374,8 @@ public enum Feature {
13741374
/**
13751375
* Target MySQL version for executable comments, encoded as major * 10000 + minor * 100 + patch.
13761376
* A negative value leaves the version unspecified: conditional comments then fail instead of
1377-
* silently discarding SQL. Only used with the MYSQL dialect; unversioned comments need no
1378-
* value.
1377+
* silently discarding SQL. Only used with the MYSQL and MARIADB dialects (with MARIADB, the
1378+
* MariaDB version); unversioned comments need no value.
13791379
*/
13801380
mySqlServerVersion(-1),
13811381

‎src/site/sphinx/usage.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,12 @@ or ``FeatureConfiguration``. Other dialects retain their comment behavior.
17941794
Nested executable comments are rejected; nested-comment acceptance in other
17951795
dialects is not a guarantee that the same SQL is valid on MySQL.
17961796

1797+
``Dialect.MARIADB`` follows the same rules, with ``withMySqlServerVersion``
1798+
taking the MariaDB version (``110402`` for 11.4.2). As on the server,
1799+
``/*M! ... */`` and ``/*M!###### ... */`` are executable there too, a sixth
1800+
version digit is always part of the version, and ``/*!50700`` through
1801+
``/*!99999`` (MySQL 5.7 and later) are skipped.
1802+
17971803
ALTER column names
17981804
~~~~~~~~~~~~~~~~~~
17991805

‎src/test/java/net/sf/jsqlparser/parser/MySqlCommentTest.java‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.sf.jsqlparser.util.deparser.StatementDeParser;
2929
import org.junit.jupiter.api.Test;
3030
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.CsvSource;
3132
import org.junit.jupiter.params.provider.ValueSource;
3233

3334
class MySqlCommentTest {
@@ -242,11 +243,38 @@ void rejectsLiteralInternalMinusMarkerInEveryDialect() {
242243
}
243244
}
244245

246+
// Expected results checked against MariaDB 13.0.2
247+
@ParameterizedTest
248+
@CsvSource(delimiter = '|', value = {"SELECT 1--2|SELECT 1 - -2",
249+
"SELECT 1 /*! +2 */|SELECT 1 + 2", "SELECT 1 /*M! +2 */|SELECT 1 + 2",
250+
"SELECT 1 /*!50000 +2 */|SELECT 1 + 2", "SELECT 1 /*!50700 +2 */|SELECT 1",
251+
"SELECT 1 /*!99999 +2 */|SELECT 1", "SELECT 1 /*M!50700 +2 */|SELECT 1 + 2",
252+
"SELECT 1 /*!130002 +2 */|SELECT 1 + 2", "SELECT 1 /*!130003 +2 */|SELECT 1",
253+
"SELECT 1 /*M!130002 +2 */|SELECT 1 + 2", "SELECT 1 /*M!130003 +2 */|SELECT 1",
254+
"SELECT 1 /*!100000+2 */|SELECT 1 + 2", "SELECT 1 /*M!100000+2 */|SELECT 1 + 2",
255+
"SELECT 1 /*m! +2 */|SELECT 1",
256+
"SELECT 1 /*M! UNION SELECT pw FROM secrets */|SELECT 1 UNION SELECT pw FROM secrets"})
257+
void followsMariaDbCommentRules(String sql, String expected) throws Exception {
258+
assertEquals(expected, CCJSqlParserUtil.parse(sql,
259+
p -> p.withDialect(Dialect.MARIADB).withMySqlServerVersion(130002)).toString());
260+
}
261+
262+
@Test
263+
void requiresVersionForMariaDbConditionalComments() {
264+
JSQLParserException exception = assertThrows(JSQLParserException.class,
265+
() -> CCJSqlParserUtil.parse("SELECT 1 /*M!100000 +2 */",
266+
p -> p.withDialect(Dialect.MARIADB)));
267+
assertTrue(exception.getCause().getMessage().contains("withMySqlServerVersion"));
268+
assertThrows(JSQLParserException.class,
269+
() -> CCJSqlParserUtil.parse("SELECT /*M! 1 /*! +99 */ + 2 */",
270+
p -> p.withDialect(Dialect.MARIADB)));
271+
}
272+
245273
@Test
246274
void preservesOtherDialectCommentsAndQuotedBodies() throws Exception {
247275
assertEquals("SELECT 1", CCJSqlParserUtil.parse("SELECT 1--2").toString());
248276
for (Dialect dialect : Dialect.values()) {
249-
if (dialect != Dialect.MYSQL) {
277+
if (dialect != Dialect.MYSQL && dialect != Dialect.MARIADB) {
250278
assertEquals("SELECT 1", CCJSqlParserUtil.parse("SELECT 1 /*!90702 +2 */ --3",
251279
p -> p.withDialect(dialect)).toString(), dialect.name());
252280
}

0 commit comments

Comments
 (0)