Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

import java.util.List;

import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
import org.eclipse.daanse.sql.guard.api.elements.DatabaseCatalog;

public interface SqlGuardFactory {

SqlGuard create(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns);
SqlGuard create(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns, Dialect dialect);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SmartCity Jena - initial
* Stefan Bischof (bipolis.org) - initial
*/
package org.eclipse.daanse.sql.guard.jsqltranspiler;

import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;

import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.util.deparser.ExpressionDeParser;

public class DaanseExpressionDeParser extends ExpressionDeParser {

private Dialect dialect;

public DaanseExpressionDeParser(Dialect dialect) {
super();
this.dialect = dialect;
}

@Override
public <S> StringBuilder visit(Column tableColumn, S context) {
final Table table = tableColumn.getTable();
String tableName = null;
if (table != null) {
if (table.getAlias() != null) {
tableName = table.getAlias().getName();
} else {
tableName = table.getFullyQualifiedName();
}
}
if (tableName != null && !tableName.isEmpty()) {
dialect.quoteIdentifier(builder, tableName, tableColumn.getColumnName());
} else {
dialect.quoteIdentifier(builder, tableColumn.getColumnName());
}

if (tableColumn.getArrayConstructor() != null) {
tableColumn.getArrayConstructor().accept(this, context);
}

if (tableColumn.getCommentText() != null) {
builder.append(" /* ").append(tableColumn.getCommentText()).append("*/ ");
}

return builder;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SmartCity Jena - initial
* Stefan Bischof (bipolis.org) - initial
*/
package org.eclipse.daanse.sql.guard.jsqltranspiler;

import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;

import ai.starlake.transpiler.JSQLColumResolver;
import ai.starlake.transpiler.schema.JdbcMetaData;
import ai.starlake.transpiler.schema.JdbcResultSetMetaData;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.util.deparser.StatementDeParser;

public class DaanseJSQLColumResolver extends JSQLColumResolver {

final JdbcMetaData metaData;
private Dialect dialect;

public DaanseJSQLColumResolver(JdbcMetaData metaData, Dialect dialect) {
super(metaData);
this.metaData = metaData;
this.dialect = dialect;
}

public String getResolvedStatementText(String sqlStr) throws JSQLParserException {
StringBuilder builder = new StringBuilder();
StatementDeParser deParser = new DaanseStatementDeParser(builder, this.dialect);

Statement st = CCJSqlParserUtil.parse(sqlStr);
if (st instanceof Select) {
Select select = (Select) st;
select.accept((SelectVisitor<JdbcResultSetMetaData>) this, JdbcMetaData.copyOf(metaData));
}
st.accept(deParser);
return builder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SmartCity Jena - initial
* Stefan Bischof (bipolis.org) - initial
*/
package org.eclipse.daanse.sql.guard.jsqltranspiler;

import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;

import net.sf.jsqlparser.util.deparser.SelectDeParser;
import net.sf.jsqlparser.util.deparser.StatementDeParser;

public class DaanseStatementDeParser extends StatementDeParser {

public DaanseStatementDeParser(StringBuilder buffer, Dialect dialect) {
super(new DaanseExpressionDeParser(dialect), new SelectDeParser(), buffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Set;
import java.util.regex.Pattern;

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

public TranspilerSqlGuard(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns) {
public TranspilerSqlGuard(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns, Dialect dialect) {
this.whitelistFunctionsPatterns = whitelistFunctionsPatterns;
jdbcMetaDataToCopy = calculateMetaData(currentCatalogName, currentSchemaName, databaseCatalog);
this.dialect = dialect;

}

Expand Down Expand Up @@ -141,7 +144,8 @@ public String guard(String sqlStr) throws GuardException {
}

// we can finally resolve for the actually returned columns
JSQLColumResolver columResolver = new JSQLColumResolver(jdbcMetaDataToCopy);
JSQLColumResolver columResolver = new DaanseJSQLColumResolver(jdbcMetaDataToCopy, dialect);
columResolver.setCommentFlag(false);
columResolver.setErrorMode(JdbcMetaData.ErrorMode.STRICT);

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

} else {
throw new UnallowedStatementTypeGuardException(
st.getClass().getSimpleName().toUpperCase() + " is not permitted.");
st.getClass().getSimpleName().toUpperCase() + " is not permitted.");
}
}

catch (JSQLParserException ex) {
} catch (JSQLParserException ex) {
throw new UnparsableStatementGuardException();
} catch (CatalogNotFoundException | ColumnNotFoundException | SchemaNotFoundException
| TableNotDeclaredException | TableNotFoundException ex) {
| TableNotDeclaredException | TableNotFoundException ex) {
throw new UnresolvableObjectsGuardException(ex.getMessage());
}

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

List<JdbcColumn> jdbcColumns = table.getDatabaseColumns().parallelStream()
.map(c -> new JdbcColumn(c.getName())).toList();
.map(c -> new JdbcColumn(c.getName())).toList();
jdbcMetaData.addTable(schema.getName(), table.getName(), jdbcColumns);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.List;

import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
import org.eclipse.daanse.sql.guard.api.SqlGuard;
import org.eclipse.daanse.sql.guard.api.SqlGuardFactory;
import org.eclipse.daanse.sql.guard.api.elements.DatabaseCatalog;
Expand All @@ -25,8 +26,8 @@
public class TranspilerSqlGuardFactory implements SqlGuardFactory {

@Override
public SqlGuard create(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns) {
return new TranspilerSqlGuard(currentCatalogName, currentSchemaName, databaseCatalog, whitelistFunctionsPatterns);
public SqlGuard create(String currentCatalogName, String currentSchemaName, DatabaseCatalog databaseCatalog, List<String> whitelistFunctionsPatterns, Dialect dialect) {
return new TranspilerSqlGuard(currentCatalogName, currentSchemaName, databaseCatalog, whitelistFunctionsPatterns, dialect);
}

}
Loading
Loading