Skip to content

Commit d049294

Browse files
dbulahovstbischof
authored andcommitted
add dialect to guard
Signed-off-by: <[email protected]>
1 parent f0d9139 commit d049294

File tree

9 files changed

+243
-74
lines changed

9 files changed

+243
-74
lines changed

guard/api/src/main/java/org/eclipse/daanse/sql/guard/api/SqlGuardFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
import java.util.List;
1717

18+
import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
1819
import org.eclipse.daanse.sql.guard.api.elements.DatabaseCatalog;
1920

2021
public interface SqlGuardFactory {
2122

22-
SqlGuard create(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns);
23+
SqlGuard create(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns, Dialect dialect);
2324
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* SmartCity Jena - initial
12+
* Stefan Bischof (bipolis.org) - initial
13+
*/
14+
package org.eclipse.daanse.sql.guard.jsqltranspiler;
15+
16+
import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
17+
18+
import net.sf.jsqlparser.schema.Column;
19+
import net.sf.jsqlparser.schema.Table;
20+
import net.sf.jsqlparser.util.deparser.ExpressionDeParser;
21+
22+
public class DaanseExpressionDeParser extends ExpressionDeParser {
23+
24+
private Dialect dialect;
25+
26+
public DaanseExpressionDeParser(Dialect dialect) {
27+
super();
28+
this.dialect = dialect;
29+
}
30+
31+
@Override
32+
public <S> StringBuilder visit(Column tableColumn, S context) {
33+
final Table table = tableColumn.getTable();
34+
String tableName = null;
35+
if (table != null) {
36+
if (table.getAlias() != null) {
37+
tableName = table.getAlias().getName();
38+
} else {
39+
tableName = table.getFullyQualifiedName();
40+
}
41+
}
42+
if (tableName != null && !tableName.isEmpty()) {
43+
dialect.quoteIdentifier(builder, tableName, tableColumn.getColumnName());
44+
} else {
45+
dialect.quoteIdentifier(builder, tableColumn.getColumnName());
46+
}
47+
48+
if (tableColumn.getArrayConstructor() != null) {
49+
tableColumn.getArrayConstructor().accept(this, context);
50+
}
51+
52+
if (tableColumn.getCommentText() != null) {
53+
builder.append(" /* ").append(tableColumn.getCommentText()).append("*/ ");
54+
}
55+
56+
return builder;
57+
}
58+
59+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* SmartCity Jena - initial
12+
* Stefan Bischof (bipolis.org) - initial
13+
*/
14+
package org.eclipse.daanse.sql.guard.jsqltranspiler;
15+
16+
import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
17+
18+
import ai.starlake.transpiler.JSQLColumResolver;
19+
import ai.starlake.transpiler.schema.JdbcMetaData;
20+
import ai.starlake.transpiler.schema.JdbcResultSetMetaData;
21+
import net.sf.jsqlparser.JSQLParserException;
22+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
23+
import net.sf.jsqlparser.statement.Statement;
24+
import net.sf.jsqlparser.statement.select.Select;
25+
import net.sf.jsqlparser.statement.select.SelectVisitor;
26+
import net.sf.jsqlparser.util.deparser.StatementDeParser;
27+
28+
public class DaanseJSQLColumResolver extends JSQLColumResolver {
29+
30+
final JdbcMetaData metaData;
31+
private Dialect dialect;
32+
33+
public DaanseJSQLColumResolver(JdbcMetaData metaData, Dialect dialect) {
34+
super(metaData);
35+
this.metaData = metaData;
36+
this.dialect = dialect;
37+
}
38+
39+
public String getResolvedStatementText(String sqlStr) throws JSQLParserException {
40+
StringBuilder builder = new StringBuilder();
41+
StatementDeParser deParser = new DaanseStatementDeParser(builder, this.dialect);
42+
43+
Statement st = CCJSqlParserUtil.parse(sqlStr);
44+
if (st instanceof Select) {
45+
Select select = (Select) st;
46+
select.accept((SelectVisitor<JdbcResultSetMetaData>) this, JdbcMetaData.copyOf(metaData));
47+
}
48+
st.accept(deParser);
49+
return builder.toString();
50+
}
51+
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* SmartCity Jena - initial
12+
* Stefan Bischof (bipolis.org) - initial
13+
*/
14+
package org.eclipse.daanse.sql.guard.jsqltranspiler;
15+
16+
import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
17+
18+
import net.sf.jsqlparser.util.deparser.SelectDeParser;
19+
import net.sf.jsqlparser.util.deparser.StatementDeParser;
20+
21+
public class DaanseStatementDeParser extends StatementDeParser {
22+
23+
public DaanseStatementDeParser(StringBuilder buffer, Dialect dialect) {
24+
super(new DaanseExpressionDeParser(dialect), new SelectDeParser(), buffer);
25+
}
26+
}

guard/jsqltranspiler/src/main/java/org/eclipse/daanse/sql/guard/jsqltranspiler/TranspilerSqlGuard.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Set;
2020
import java.util.regex.Pattern;
2121

22+
import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
2223
import org.eclipse.daanse.sql.guard.api.SqlGuard;
2324
import org.eclipse.daanse.sql.guard.api.elements.DatabaseCatalog;
2425
import org.eclipse.daanse.sql.guard.api.elements.DatabaseSchema;
@@ -57,10 +58,12 @@ public class TranspilerSqlGuard implements SqlGuard {
5758
private static final Logger LOGGER = LoggerFactory.getLogger(TranspilerSqlGuard.class);
5859
private JdbcMetaData jdbcMetaDataToCopy;
5960
private List<String> whitelistFunctionsPatterns = new ArrayList<String>();
61+
private Dialect dialect;
6062

61-
public TranspilerSqlGuard(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns) {
63+
public TranspilerSqlGuard(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns, Dialect dialect) {
6264
this.whitelistFunctionsPatterns = whitelistFunctionsPatterns;
6365
jdbcMetaDataToCopy = calculateMetaData(currentCatalogName, currentSchemaName, databaseCatalog);
66+
this.dialect = dialect;
6467

6568
}
6669

@@ -141,7 +144,8 @@ public String guard(String sqlStr) throws GuardException {
141144
}
142145

143146
// we can finally resolve for the actually returned columns
144-
JSQLColumResolver columResolver = new JSQLColumResolver(jdbcMetaDataToCopy);
147+
JSQLColumResolver columResolver = new DaanseJSQLColumResolver(jdbcMetaDataToCopy, dialect);
148+
columResolver.setCommentFlag(false);
145149
columResolver.setErrorMode(JdbcMetaData.ErrorMode.STRICT);
146150

147151
String rewritten = columResolver.getResolvedStatementText(sqlStr);
@@ -159,14 +163,12 @@ public String guard(String sqlStr) throws GuardException {
159163

160164
} else {
161165
throw new UnallowedStatementTypeGuardException(
162-
st.getClass().getSimpleName().toUpperCase() + " is not permitted.");
166+
st.getClass().getSimpleName().toUpperCase() + " is not permitted.");
163167
}
164-
}
165-
166-
catch (JSQLParserException ex) {
168+
} catch (JSQLParserException ex) {
167169
throw new UnparsableStatementGuardException();
168170
} catch (CatalogNotFoundException | ColumnNotFoundException | SchemaNotFoundException
169-
| TableNotDeclaredException | TableNotFoundException ex) {
171+
| TableNotDeclaredException | TableNotFoundException ex) {
170172
throw new UnresolvableObjectsGuardException(ex.getMessage());
171173
}
172174

@@ -180,7 +182,7 @@ private static JdbcMetaData calculateMetaData(String currentCatalogName, String
180182
for (DatabaseTable table : schema.getDatabaseTables()) {
181183

182184
List<JdbcColumn> jdbcColumns = table.getDatabaseColumns().parallelStream()
183-
.map(c -> new JdbcColumn(c.getName())).toList();
185+
.map(c -> new JdbcColumn(c.getName())).toList();
184186
jdbcMetaData.addTable(schema.getName(), table.getName(), jdbcColumns);
185187
}
186188
}

guard/jsqltranspiler/src/main/java/org/eclipse/daanse/sql/guard/jsqltranspiler/TranspilerSqlGuardFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.util.List;
1717

18+
import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
1819
import org.eclipse.daanse.sql.guard.api.SqlGuard;
1920
import org.eclipse.daanse.sql.guard.api.SqlGuardFactory;
2021
import org.eclipse.daanse.sql.guard.api.elements.DatabaseCatalog;
@@ -25,8 +26,8 @@
2526
public class TranspilerSqlGuardFactory implements SqlGuardFactory {
2627

2728
@Override
28-
public SqlGuard create(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns) {
29-
return new TranspilerSqlGuard(currentCatalogName, currentSchemaName, databaseCatalog, whitelistFunctionsPatterns);
29+
public SqlGuard create(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns, Dialect dialect) {
30+
return new TranspilerSqlGuard(currentCatalogName, currentSchemaName, databaseCatalog, whitelistFunctionsPatterns, dialect);
3031
}
3132

3233
}

0 commit comments

Comments
 (0)