Search in sources :

Example 6 with CreateIndex

use of net.sf.jsqlparser.statement.create.index.CreateIndex in project JSqlParser by JSQLParser.

the class CreateIndexTest method testCreateIndex5.

@Test
public void testCreateIndex5() throws JSQLParserException {
    String statement = "CREATE mytype INDEX myindex ON mytab (mycol ASC, mycol2 (75), mycol3) mymodifiers";
    CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement));
    assertEquals(3, createIndex.getIndex().getColumnsNames().size());
    assertEquals("myindex", createIndex.getIndex().getName());
    assertEquals("mytype", createIndex.getIndex().getType());
    assertEquals("mytab", createIndex.getTable().getFullyQualifiedName());
    assertEquals("mycol3", createIndex.getIndex().getColumnsNames().get(2));
}
Also used : CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 7 with CreateIndex

use of net.sf.jsqlparser.statement.create.index.CreateIndex in project JSqlParser by JSQLParser.

the class CreateIndexTest method testCreateIndex2.

@Test
public void testCreateIndex2() throws JSQLParserException {
    String statement = "CREATE mytype INDEX myindex ON mytab (mycol, mycol2)";
    CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement));
    assertEquals(2, createIndex.getIndex().getColumnsNames().size());
    assertEquals("myindex", createIndex.getIndex().getName());
    assertEquals("mytype", createIndex.getIndex().getType());
    assertEquals("mytab", createIndex.getTable().getFullyQualifiedName());
    assertEquals("mycol2", createIndex.getIndex().getColumnsNames().get(1));
    assertEquals(statement, "" + createIndex);
}
Also used : CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 8 with CreateIndex

use of net.sf.jsqlparser.statement.create.index.CreateIndex in project JSqlParser by JSQLParser.

the class CreateIndexDeParser method deParse.

public void deParse(CreateIndex createIndex) {
    Index index = createIndex.getIndex();
    buffer.append("CREATE ");
    if (index.getType() != null) {
        buffer.append(index.getType());
        buffer.append(" ");
    }
    buffer.append("INDEX ");
    buffer.append(index.getName());
    buffer.append(" ON ");
    buffer.append(createIndex.getTable().getFullyQualifiedName());
    if (index.getColumnsNames() != null) {
        buffer.append(" (");
        for (Iterator iter = index.getColumnsNames().iterator(); iter.hasNext(); ) {
            String columnName = (String) iter.next();
            buffer.append(columnName);
            if (iter.hasNext()) {
                buffer.append(", ");
            }
        }
        buffer.append(")");
    }
}
Also used : Iterator(java.util.Iterator) Index(net.sf.jsqlparser.statement.create.table.Index) CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex)

Example 9 with CreateIndex

use of net.sf.jsqlparser.statement.create.index.CreateIndex in project herddb by diennea.

the class SQLPlanner method buildCreateIndexStatement.

private Statement buildCreateIndexStatement(String defaultTableSpace, CreateIndex s) throws StatementExecutionException {
    try {
        String tableSpace = s.getTable().getSchemaName();
        if (tableSpace == null) {
            tableSpace = defaultTableSpace;
        }
        String tableName = s.getTable().getName();
        String indexName = s.getIndex().getName().toLowerCase();
        String indexType = convertIndexType(s.getIndex().getType());
        herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).uuid(UUID.randomUUID().toString()).type(indexType).table(tableName).tablespace(tableSpace);
        AbstractTableManager tableDefinition = manager.getTableSpaceManager(tableSpace).getTableManager(tableName);
        if (tableDefinition == null) {
            throw new TableDoesNotExistException("no such table " + tableName + " in tablespace " + tableSpace);
        }
        for (String columnName : s.getIndex().getColumnsNames()) {
            columnName = columnName.toLowerCase();
            Column column = tableDefinition.getTable().getColumn(columnName);
            if (column == null) {
                throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
            }
            builder.column(column.name, column.type);
        }
        CreateIndexStatement statement = new CreateIndexStatement(builder.build());
        return statement;
    } catch (IllegalArgumentException err) {
        throw new StatementExecutionException("bad index definition: " + err.getMessage(), err);
    }
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) AbstractTableManager(herddb.core.AbstractTableManager) Column(herddb.model.Column) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) Index(net.sf.jsqlparser.statement.create.table.Index) StatementExecutionException(herddb.model.StatementExecutionException)

Example 10 with CreateIndex

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

CreateIndex (net.sf.jsqlparser.statement.create.index.CreateIndex)11 StringReader (java.io.StringReader)5 Test (org.junit.Test)5 Alter (net.sf.jsqlparser.statement.alter.Alter)3 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)3 Index (net.sf.jsqlparser.statement.create.table.Index)3 Delete (net.sf.jsqlparser.statement.delete.Delete)3 Drop (net.sf.jsqlparser.statement.drop.Drop)3 Insert (net.sf.jsqlparser.statement.insert.Insert)3 Update (net.sf.jsqlparser.statement.update.Update)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Table (net.sf.jsqlparser.schema.Table)2 CreateView (net.sf.jsqlparser.statement.create.view.CreateView)2 DBException (org.jkiss.dbeaver.DBException)2 AbstractTableManager (herddb.core.AbstractTableManager)1 Column (herddb.model.Column)1 ExecutionPlan (herddb.model.ExecutionPlan)1 StatementExecutionException (herddb.model.StatementExecutionException)1 TableDoesNotExistException (herddb.model.TableDoesNotExistException)1