Search in sources :

Example 1 with CreateView

use of net.sf.jsqlparser.statement.create.view.CreateView in project JSqlParser by JSQLParser.

the class CreateViewTest method testCreateView5.

public void testCreateView5() throws JSQLParserException {
    String statement = "CREATE VIEW myview AS (SELECT * FROM mytab)";
    String statement2 = "CREATE VIEW myview AS (SELECT * FROM mytab)";
    CreateView createView = (CreateView) parserManager.parse(new StringReader(statement));
    assertFalse(createView.isOrReplace());
    assertEquals("myview", createView.getView().getName());
    assertEquals("mytab", ((Table) ((PlainSelect) createView.getSelectBody()).getFromItem()).getName());
    assertEquals(statement2, createView.toString());
}
Also used : StringReader(java.io.StringReader) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) CreateView(net.sf.jsqlparser.statement.create.view.CreateView)

Example 2 with CreateView

use of net.sf.jsqlparser.statement.create.view.CreateView in project JSqlParser by JSQLParser.

the class CreateViewDeParserTest method testUseExtrnalExpressionDeparser.

/**
 * Test of deParse method, of class CreateViewDeParser.
 */
@Test
public void testUseExtrnalExpressionDeparser() throws JSQLParserException {
    StringBuilder b = new StringBuilder();
    SelectDeParser selectDeParser = new SelectDeParser();
    selectDeParser.setBuffer(b);
    ExpressionDeParser expressionDeParser = new ExpressionDeParser(selectDeParser, b) {

        @Override
        public void visit(Column tableColumn) {
            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()) {
                getBuffer().append("\"").append(tableName).append("\"").append(".");
            }
            getBuffer().append("\"").append(tableColumn.getColumnName()).append("\"");
        }
    };
    selectDeParser.setExpressionVisitor(expressionDeParser);
    CreateViewDeParser instance = new CreateViewDeParser(b, selectDeParser);
    CreateView vc = (CreateView) CCJSqlParserUtil.parse("CREATE VIEW test AS SELECT a, b FROM mytable");
    instance.deParse(vc);
    assertEquals("CREATE VIEW test AS SELECT a, b FROM mytable", vc.toString());
    assertEquals("CREATE VIEW test AS SELECT \"a\", \"b\" FROM mytable", instance.getBuffer().toString());
}
Also used : Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) CreateView(net.sf.jsqlparser.statement.create.view.CreateView) Test(org.junit.Test)

Example 3 with CreateView

use of net.sf.jsqlparser.statement.create.view.CreateView in project JSqlParser by JSQLParser.

the class CreateViewTest method testCreateView.

public void testCreateView() throws JSQLParserException {
    String statement = "CREATE VIEW myview AS SELECT * FROM mytab";
    CreateView createView = (CreateView) parserManager.parse(new StringReader(statement));
    assertFalse(createView.isOrReplace());
    assertEquals("myview", createView.getView().getName());
    assertEquals("mytab", ((Table) ((PlainSelect) createView.getSelectBody()).getFromItem()).getName());
    assertEquals(statement, createView.toString());
}
Also used : StringReader(java.io.StringReader) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) CreateView(net.sf.jsqlparser.statement.create.view.CreateView)

Example 4 with CreateView

use of net.sf.jsqlparser.statement.create.view.CreateView 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

CreateView (net.sf.jsqlparser.statement.create.view.CreateView)4 StringReader (java.io.StringReader)2 Table (net.sf.jsqlparser.schema.Table)2 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Column (net.sf.jsqlparser.schema.Column)1 Alter (net.sf.jsqlparser.statement.alter.Alter)1 CreateIndex (net.sf.jsqlparser.statement.create.index.CreateIndex)1 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)1 Delete (net.sf.jsqlparser.statement.delete.Delete)1 Drop (net.sf.jsqlparser.statement.drop.Drop)1 Insert (net.sf.jsqlparser.statement.insert.Insert)1 Update (net.sf.jsqlparser.statement.update.Update)1 DBException (org.jkiss.dbeaver.DBException)1 Test (org.junit.Test)1