net.sf.jsqlparser.util.TablesNamesFinder Java Examples

The following examples show how to use net.sf.jsqlparser.util.TablesNamesFinder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsqlParsertests.java    From sqlhelper with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void instrmentTenant(String sql) throws Throwable {
    Statement statement = CCJSqlParserUtil.parse(sql);
    if (statement instanceof Select) {
        Select select = (Select) statement;
        System.out.println("print parsed sql statement:");
        System.out.println(select.toString());
        System.out.println("show tables:");
        TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
        List<String> tableNames = tablesNamesFinder.getTableList(select);
        for (String tableName : tableNames) {
            System.out.println(tableName);
        }

        SQLStatementInstrumentor instrumentor = new SQLStatementInstrumentor();
        /*
        String tenantSql = instrumentor.instrumentTenantSql(sql, AndTenantBuilder.DEFAULT
                .column("tenant")
                .value("1")
                .build());
        System.out.println("print instrumented sql:");
        System.out.println(tenantSql);
        */
        System.out.println("====================================");
    }
}
 
Example #2
Source File: JsqlParsertests.java    From sqlhelper with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void instrmentOrderBy(String sql) throws Throwable {
    Statement statement = CCJSqlParserUtil.parse(sql);
    if (statement instanceof Select) {
        Select select = (Select) statement;
        System.out.println("print parsed sql statement:");
        System.out.println(select.toString());
        System.out.println("show tables:");
        TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
        List<String> tableNames = tablesNamesFinder.getTableList(select);
        for (String tableName : tableNames) {
            System.out.println(tableName);
        }

        SQLStatementInstrumentor instrumentor = new SQLStatementInstrumentor();
        String orderBySql = instrumentor.instrumentOrderBySql(sql, SqlStyleOrderByBuilder.DEFAULT.build("name asc, age desc"));

        System.out.println("print instrumented sql:");
        System.out.println(orderBySql);

        System.out.println("====================================");
    }
}
 
Example #3
Source File: JSQLParserTest.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    * @param args
    */
   public static void main(String[] args) throws Exception {

Statement statement = CCJSqlParserUtil.parse("SELECT * from public.CUSTOMER");
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(statement);
System.out.println(tableList);

if (statement instanceof Commit) {
    Commit commit = (Commit) statement;
	System.out.println(commit.getClass());
}
   }
 
Example #4
Source File: JSQLParserTest.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    * @throws JSQLParserException
    */
   public static void selectTest() throws JSQLParserException {
Statement statement = CCJSqlParserUtil.parse("SELECT * FROM customer where toto = 'titi' ");

Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
System.out.println(tableList);
   }
 
Example #5
Source File: CRUDParseUtils.java    From WeBASE-Front with Apache License 2.0 4 votes vote down vote up
public static void parseSelect(
        String sql, Table table, Condition condition, List<String> selectColumns)
        throws JSQLParserException, FrontException {
    Statement statement;
    statement = CCJSqlParserUtil.parse(sql);
    Select selectStatement = (Select) statement;

    // parse table name
    TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
    List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
    if (tableList.size() != 1) {
        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR, "Please provide only one table name.");
    }
    table.setTableName(tableList.get(0));

    // parse where clause
    PlainSelect selectBody = (PlainSelect) selectStatement.getSelectBody();
    if (selectBody.getOrderByElements() != null) {
        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR, "The order clause is not supported.");
    }
    if (selectBody.getGroupBy() != null) {
        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR, "The group clause is not supported.");
    }
    if (selectBody.getHaving() != null) {
        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR, "The having clause is not supported.");
    }
    if (selectBody.getJoins() != null) {
        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR, "The join clause is not supported.");
    }
    if (selectBody.getTop() != null) {
        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR, "The top clause is not supported.");
    }
    if (selectBody.getDistinct() != null) {
        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR, "The distinct clause is not supported.");
    }
    Expression expr = selectBody.getWhere();
    condition = handleExpression(condition, expr);

    Limit limit = selectBody.getLimit();
    if (limit != null) {
        parseLimit(condition, limit);
    }

    // parse select item
    List<SelectItem> selectItems = selectBody.getSelectItems();
    for (SelectItem item : selectItems) {
        if (item instanceof SelectExpressionItem) {
            SelectExpressionItem selectExpressionItem = (SelectExpressionItem) item;
            Expression expression = selectExpressionItem.getExpression();
            if (expression instanceof Function) {
                Function func = (Function) expression;
                throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR,
                        "The " + func.getName() + " function is not supported.");
            }
        }
        selectColumns.add(item.toString());
    }
}
 
Example #6
Source File: SQLUtils.java    From jobson with Apache License 2.0 4 votes vote down vote up
public static Set<String> tableRefsIn(Statement stmt) {
    final TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
    return new HashSet<>(tablesNamesFinder.getTableList(stmt));
}
 
Example #7
Source File: ObjPD.java    From openprodoc with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Search in ANY object using SQL Syntax subset, similar to CMIS SQL
 * @param SQL complete query
 * @return and opened @Cursor
 * @throws PDException In any Error
 */
public Cursor SearchSelect(String SQL) throws PDException
{
if (PDLog.isDebug())
    PDLog.Debug("ObjPD.SearchSelect>:"+SQL);
Query QBE=null;
try {
Select ParsedSQL = (Select) CCJSqlParserUtil.parse(SQL);
//-- Calculate Table Names ------------
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> TableListSQL = tablesNamesFinder.getTableList(ParsedSQL);
Vector <String> OPDTabs=CalculateTabs(TableListSQL);
//-- Calculate Fields -------------
List<SelectItem> selectItems = ((PlainSelect)ParsedSQL.getSelectBody()).getSelectItems();
Vector<String> Fields=new Vector<String>();
if (!( selectItems.get(0) instanceof AllColumns))
    for (int i = 0; i < selectItems.size(); i++)
        Fields.add(((SelectExpressionItem)selectItems.get(i)).getExpression().toString());      
Record Rec=CalculateRec(Fields, OPDTabs);
//-- Calculate Conds in Select ------------
Expression When = ((PlainSelect)ParsedSQL.getSelectBody()).getWhere();
Conditions CondSel=EvalExpr(When);

//-- Check Additional-Security Conditions ----
Conditions FinalConds;
Conditions AddedConds=NeededMoreConds(TableListSQL, OPDTabs);
if (AddedConds==null)
    FinalConds=CondSel;
else
    {
    FinalConds=new Conditions();
    FinalConds.addCondition(AddedConds);
    FinalConds.addCondition(CondSel);
    }
//-- Calculate Order ------------
Vector <String> Order=new Vector<String>();
Vector <Boolean> OrderAsc=new Vector<Boolean>();
List<OrderByElement> orderByElements = ((PlainSelect)ParsedSQL.getSelectBody()).getOrderByElements(); 
if (orderByElements!=null)
    for (int i = 0; i < orderByElements.size(); i++)
        {
        Order.add(orderByElements.get(i).getExpression().toString());
        OrderAsc.add(orderByElements.get(i).isAsc());
        }
//-- Create Query --------------
QBE=new Query(OPDTabs, Rec, FinalConds, Order, OrderAsc);
if (PDLog.isDebug())
    PDLog.Debug("ObjPD.SearchSelect <");
} catch (Exception Ex)
    {
    Ex.printStackTrace();
    PDException.GenPDException("Processing_SQL", Ex.getLocalizedMessage());
    }
return(getDrv().OpenCursor(QBE));
}