@@ -51,6 +51,12 @@ public class TestUtils {
51
51
private static final Pattern SQL_COMMENT_PATTERN = Pattern .
52
52
compile ("(--.*$)|(/\\ *.*?\\ */)" , Pattern .MULTILINE );
53
53
54
+ private static final Pattern SQL_SANITATION_PATTERN
55
+ = Pattern .compile ("(\\ s+)" , Pattern .MULTILINE );
56
+
57
+ private static final Pattern SQL_SANITATION_PATTERN2
58
+ = Pattern .compile ("\\ s*([!/,()=+\\ -*|\\ ]<>])\\ s*" , Pattern .MULTILINE );
59
+
54
60
/**
55
61
* @param statement
56
62
* @return the parsed {@link Statement}
@@ -94,10 +100,17 @@ public static void assertStatementCanBeDeparsedAs(Statement parsed, String state
94
100
}
95
101
96
102
public static void assertStatementCanBeDeparsedAs (Statement parsed , String statement , boolean laxDeparsingCheck ) {
97
- assertEquals (buildSqlString (statement , laxDeparsingCheck ),
98
- buildSqlString (parsed .toString (), laxDeparsingCheck ));
103
+ String sanitizedInputSqlStr = buildSqlString (parsed .toString (), laxDeparsingCheck );
104
+ String sanitizedStatementStr = buildSqlString (statement , laxDeparsingCheck );
105
+
106
+ assertEquals (sanitizedStatementStr , sanitizedInputSqlStr );
99
107
100
- assertDeparse (parsed , statement , laxDeparsingCheck );
108
+ StringBuilder builder = new StringBuilder ();
109
+ parsed .accept ( new StatementDeParser (builder ) );
110
+
111
+ String sanitizedDeparsedStr = buildSqlString (builder .toString (), laxDeparsingCheck );
112
+
113
+ assertEquals (sanitizedStatementStr , sanitizedDeparsedStr );
101
114
}
102
115
103
116
/**
@@ -245,12 +258,19 @@ public static void assertDeparse(Statement stmt, String statement, boolean laxDe
245
258
}
246
259
247
260
public static String buildSqlString (final String originalSql , boolean laxDeparsingCheck ) {
248
- String sql = SQL_COMMENT_PATTERN .matcher (originalSql ).replaceAll ("" );
249
261
if (laxDeparsingCheck ) {
250
- return sql .replaceAll ("\\ s" , " " ).replaceAll ("\\ s+" , " " ).
251
- replaceAll ("\\ s*([!/,()=+\\ -*|\\ ]<>])\\ s*" , "$1" ).toLowerCase ().trim ();
262
+ // remove comments
263
+ String sanitizedSqlStr = SQL_COMMENT_PATTERN .matcher (originalSql ).replaceAll ("" );
264
+
265
+ // redundant white space
266
+ sanitizedSqlStr = SQL_SANITATION_PATTERN .matcher (sanitizedSqlStr ).replaceAll (" " );
267
+
268
+ // replace some more stuff
269
+ sanitizedSqlStr = SQL_SANITATION_PATTERN2 .matcher (sanitizedSqlStr ).replaceAll ("$1" );
270
+ return sanitizedSqlStr .trim ().toLowerCase ();
252
271
} else {
253
- return sql ;
272
+ // remove comments only
273
+ return SQL_COMMENT_PATTERN .matcher (originalSql ).replaceAll ("" );
254
274
}
255
275
}
256
276
0 commit comments