Skip to content

Commit

Permalink
manifold-sql: support start symbols other than 'select' to indicate a…
Browse files Browse the repository at this point in the history
… "query" as opposed to a "command", such as duckdb's 'pivot'
  • Loading branch information
rsmckinney committed Jul 7, 2024
1 parent b8b82a3 commit aadeabd
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,29 @@
import manifold.sql.query.type.SqlScope;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.*;

import java.util.Arrays;
import java.util.List;

public class JdbcSqlAnalyzer implements SqlAnalyzer
{
private static final List<String> _queryStarts = Arrays.asList( "select", "with", "from", "pivot", "unpivot" );
@Override
public Statement makeStatement( String queryName, SqlScope scope, String sql )
{
boolean isQuery;
try
{
// using this parser only to distinguish between SELECT statements and NON-SELECT statements
// using this parser only to distinguish between query statements and non-query statements
net.sf.jsqlparser.statement.Statement statement = CCJSqlParserUtil.parse( sql );
isQuery = statement instanceof Select;
isQuery = statement instanceof Select || statement instanceof Pivot || statement instanceof UnPivot;
}
catch( JSQLParserException e )
{
// treat as query and pass through to JDBC for error handling.
// todo: Maybe do light parsing to determine type of statement (Insert, Update, Delete, ..., or Select)?
isQuery = sql.trim().toLowerCase().startsWith( "select" );
// todo: Maybe do light parsing to determine type of statement
isQuery = _queryStarts.stream()
.anyMatch( start -> sql.trim().toLowerCase().startsWith( start ) );
}

return isQuery
Expand Down

0 comments on commit aadeabd

Please sign in to comment.