Search in sources :

Example 1 with Drop

use of net.sf.jsqlparser.statement.drop.Drop in project herddb by diennea.

the class SQLPlanner method plan.

@Override
public ExecutionPlan plan(String defaultTableSpace, net.sf.jsqlparser.statement.Statement stmt, boolean scan, boolean returnValues, int maxRows) {
    verifyJdbcParametersIndexes(stmt);
    ExecutionPlan result;
    if (stmt instanceof CreateTable) {
        result = ExecutionPlan.simple(buildCreateTableStatement(defaultTableSpace, (CreateTable) stmt));
    } else if (stmt instanceof CreateIndex) {
        result = ExecutionPlan.simple(buildCreateIndexStatement(defaultTableSpace, (CreateIndex) stmt));
    } else if (stmt instanceof Insert) {
        result = buildInsertStatement(defaultTableSpace, (Insert) stmt, returnValues);
    } else if (stmt instanceof Delete) {
        result = buildDeleteStatement(defaultTableSpace, (Delete) stmt, returnValues);
    } else if (stmt instanceof Update) {
        result = buildUpdateStatement(defaultTableSpace, (Update) stmt, returnValues);
    } else if (stmt instanceof Select) {
        result = buildSelectStatement(defaultTableSpace, (Select) stmt, scan, maxRows);
    } else if (stmt instanceof Execute) {
        result = ExecutionPlan.simple(buildExecuteStatement(defaultTableSpace, (Execute) stmt));
    } else if (stmt instanceof Alter) {
        result = ExecutionPlan.simple(buildAlterStatement(defaultTableSpace, (Alter) stmt));
    } else if (stmt instanceof Drop) {
        result = ExecutionPlan.simple(buildDropStatement(defaultTableSpace, (Drop) stmt));
    } else if (stmt instanceof Truncate) {
        result = ExecutionPlan.simple(buildTruncateStatement(defaultTableSpace, (Truncate) stmt));
    } else {
        return null;
    }
    return result;
}
Also used : Delete(net.sf.jsqlparser.statement.delete.Delete) Alter(net.sf.jsqlparser.statement.alter.Alter) ExecutionPlan(herddb.model.ExecutionPlan) CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) Execute(net.sf.jsqlparser.statement.execute.Execute) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) Truncate(net.sf.jsqlparser.statement.truncate.Truncate) Insert(net.sf.jsqlparser.statement.insert.Insert) Update(net.sf.jsqlparser.statement.update.Update) Drop(net.sf.jsqlparser.statement.drop.Drop)

Example 2 with Drop

use of net.sf.jsqlparser.statement.drop.Drop in project herddb by diennea.

the class TableSpaceMapper method getTableSpace.

public String getTableSpace(Statement statement) throws ScriptException {
    String tableName = null;
    if (statement instanceof Insert) {
        Insert st = (Insert) statement;
        tableName = st.getTable().getFullyQualifiedName();
    } else if (statement instanceof Update) {
        Update st = (Update) statement;
        tableName = st.getTables().get(0).getFullyQualifiedName();
    } else if (statement instanceof Delete) {
        Delete st = (Delete) statement;
        tableName = st.getTables().get(0).getFullyQualifiedName();
    } else if (statement instanceof Truncate) {
        Truncate st = (Truncate) statement;
        tableName = st.getTable().getFullyQualifiedName();
    } else if (statement instanceof Drop) {
        Drop st = (Drop) statement;
        tableName = st.getName().getFullyQualifiedName();
    } else if (statement instanceof CreateTable) {
        CreateTable st = (CreateTable) statement;
        tableName = st.getTable().getFullyQualifiedName();
    } else if (statement instanceof Alter) {
        Alter st = (Alter) statement;
        tableName = st.getTable().getFullyQualifiedName();
    } else {
        return null;
    }
    return mapTableNameToSchema(tableName);
}
Also used : Delete(net.sf.jsqlparser.statement.delete.Delete) Alter(net.sf.jsqlparser.statement.alter.Alter) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) Truncate(net.sf.jsqlparser.statement.truncate.Truncate) Insert(net.sf.jsqlparser.statement.insert.Insert) Update(net.sf.jsqlparser.statement.update.Update) Drop(net.sf.jsqlparser.statement.drop.Drop)

Example 3 with Drop

use of net.sf.jsqlparser.statement.drop.Drop in project JSqlParser by JSQLParser.

the class DropTest method testDrop.

@Test
public void testDrop() throws JSQLParserException {
    String statement = "DROP TABLE mytab";
    Drop drop = (Drop) parserManager.parse(new StringReader(statement));
    assertEquals("TABLE", drop.getType());
    assertEquals("mytab", drop.getName().getFullyQualifiedName());
    assertEquals(statement, "" + drop);
    statement = "DROP INDEX myindex CASCADE";
    drop = (Drop) parserManager.parse(new StringReader(statement));
    assertEquals("INDEX", drop.getType());
    assertEquals("myindex", drop.getName().getFullyQualifiedName());
    assertEquals("CASCADE", drop.getParameters().get(0));
    assertEquals(statement, "" + drop);
}
Also used : StringReader(java.io.StringReader) Drop(net.sf.jsqlparser.statement.drop.Drop) Test(org.junit.Test)

Example 4 with Drop

use of net.sf.jsqlparser.statement.drop.Drop in project JSqlParser by JSQLParser.

the class DropTest method testDrop2.

@Test
public void testDrop2() throws JSQLParserException {
    Drop drop = (Drop) parserManager.parse(new StringReader("DROP TABLE \"testtable\""));
    assertEquals("TABLE", drop.getType());
    assertEquals("\"testtable\"", drop.getName().getFullyQualifiedName());
}
Also used : StringReader(java.io.StringReader) Drop(net.sf.jsqlparser.statement.drop.Drop) Test(org.junit.Test)

Example 5 with Drop

use of net.sf.jsqlparser.statement.drop.Drop in project dbeaver by serge-rider.

the class SQLQuery method parseQuery.

private void parseQuery() {
    if (parsed) {
        return;
    }
    parsed = true;
    try {
        if (CommonUtils.isEmpty(text)) {
            this.statement = null;
            this.parseError = new DBException("Empty query");
            return;
        }
        statement = SQLSemanticProcessor.parseQuery(dataSource == null ? null : dataSource.getSQLDialect(), text);
        if (statement instanceof Select) {
            type = SQLQueryType.SELECT;
            // Detect single source table (no joins, no group by, no sub-selects)
            SelectBody selectBody = ((Select) statement).getSelectBody();
            if (selectBody instanceof PlainSelect) {
                PlainSelect plainSelect = (PlainSelect) selectBody;
                if (plainSelect.getFromItem() instanceof Table && CommonUtils.isEmpty(plainSelect.getJoins()) && (plainSelect.getGroupBy() == null || CommonUtils.isEmpty(plainSelect.getGroupBy().getGroupByExpressions())) && CommonUtils.isEmpty(plainSelect.getIntoTables())) {
                    boolean hasSubSelects = false;
                    for (SelectItem si : plainSelect.getSelectItems()) {
                        if (si instanceof SelectExpressionItem && ((SelectExpressionItem) si).getExpression() instanceof SubSelect) {
                            hasSubSelects = true;
                            break;
                        }
                    }
                    if (!hasSubSelects) {
                        fillSingleSource((Table) plainSelect.getFromItem());
                    }
                }
                // Extract select items info
                final List<SelectItem> items = plainSelect.getSelectItems();
                if (items != null && !items.isEmpty()) {
                    selectItems = new ArrayList<>();
                    for (SelectItem item : items) {
                        selectItems.add(new SQLSelectItem(item));
                    }
                }
            }
        } else if (statement instanceof Insert) {
            type = SQLQueryType.INSERT;
            fillSingleSource(((Insert) statement).getTable());
        } else if (statement instanceof Update) {
            type = SQLQueryType.UPDATE;
            Table table = ((Update) statement).getTable();
            if (table != null) {
                fillSingleSource(table);
            }
        } else if (statement instanceof Delete) {
            type = SQLQueryType.DELETE;
            if (((Delete) statement).getTable() != null) {
                fillSingleSource(((Delete) statement).getTable());
            } else {
                List<Table> tables = ((Delete) statement).getTables();
                if (tables != null && tables.size() == 1) {
                    fillSingleSource(tables.get(0));
                }
            }
        } else if (statement instanceof Alter || statement instanceof CreateTable || statement instanceof CreateView || statement instanceof Drop || statement instanceof CreateIndex) {
            type = SQLQueryType.DDL;
        } else {
            type = SQLQueryType.UNKNOWN;
        }
    } catch (Throwable e) {
        this.type = SQLQueryType.UNKNOWN;
        this.parseError = e;
    // log.debug("Error parsing SQL query [" + query + "]:" + CommonUtils.getRootCause(e).getMessage());
    }
}
Also used : Delete(net.sf.jsqlparser.statement.delete.Delete) DBException(org.jkiss.dbeaver.DBException) Table(net.sf.jsqlparser.schema.Table) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) Insert(net.sf.jsqlparser.statement.insert.Insert) Update(net.sf.jsqlparser.statement.update.Update) CreateView(net.sf.jsqlparser.statement.create.view.CreateView) Drop(net.sf.jsqlparser.statement.drop.Drop) Alter(net.sf.jsqlparser.statement.alter.Alter) CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Drop (net.sf.jsqlparser.statement.drop.Drop)5 Alter (net.sf.jsqlparser.statement.alter.Alter)3 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)3 Delete (net.sf.jsqlparser.statement.delete.Delete)3 Insert (net.sf.jsqlparser.statement.insert.Insert)3 Update (net.sf.jsqlparser.statement.update.Update)3 StringReader (java.io.StringReader)2 CreateIndex (net.sf.jsqlparser.statement.create.index.CreateIndex)2 Truncate (net.sf.jsqlparser.statement.truncate.Truncate)2 Test (org.junit.Test)2 ExecutionPlan (herddb.model.ExecutionPlan)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Table (net.sf.jsqlparser.schema.Table)1 CreateView (net.sf.jsqlparser.statement.create.view.CreateView)1 Execute (net.sf.jsqlparser.statement.execute.Execute)1 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)1 Select (net.sf.jsqlparser.statement.select.Select)1 DBException (org.jkiss.dbeaver.DBException)1