Search in sources :

Example 1 with Index

use of net.sf.jsqlparser.statement.create.table.Index in project herddb by diennea.

the class SQLPlanner method buildCreateTableStatement.

private Statement buildCreateTableStatement(String defaultTableSpace, CreateTable s) throws StatementExecutionException {
    String tableSpace = s.getTable().getSchemaName();
    String tableName = s.getTable().getName();
    if (tableSpace == null) {
        tableSpace = defaultTableSpace;
    }
    try {
        boolean foundPk = false;
        Table.Builder tablebuilder = Table.builder().uuid(UUID.randomUUID().toString()).name(tableName).tablespace(tableSpace);
        Set<String> primaryKey = new HashSet<>();
        if (s.getIndexes() != null) {
            for (Index index : s.getIndexes()) {
                if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
                    for (String n : index.getColumnsNames()) {
                        n = n.toLowerCase();
                        tablebuilder.primaryKey(n);
                        primaryKey.add(n);
                        foundPk = true;
                    }
                }
            }
        }
        int position = 0;
        for (ColumnDefinition cf : s.getColumnDefinitions()) {
            String columnName = cf.getColumnName().toLowerCase();
            int type;
            String dataType = cf.getColDataType().getDataType();
            type = sqlDataTypeToColumnType(dataType, cf.getColDataType().getArgumentsStringList());
            tablebuilder.column(columnName, type, position++);
            if (cf.getColumnSpecStrings() != null) {
                List<String> columnSpecs = decodeColumnSpecs(cf.getColumnSpecStrings());
                boolean auto_increment = decodeAutoIncrement(columnSpecs);
                if (columnSpecs.contains("PRIMARY")) {
                    foundPk = true;
                    tablebuilder.primaryKey(columnName, auto_increment);
                }
                if (auto_increment && primaryKey.contains(cf.getColumnName())) {
                    tablebuilder.primaryKey(columnName, auto_increment);
                }
            }
        }
        if (!foundPk) {
            tablebuilder.column("_pk", ColumnTypes.LONG, position++);
            tablebuilder.primaryKey("_pk", true);
        }
        Table table = tablebuilder.build();
        List<herddb.model.Index> otherIndexes = new ArrayList<>();
        if (s.getIndexes() != null) {
            for (Index index : s.getIndexes()) {
                if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
                } else if (index.getType().equalsIgnoreCase("INDEX")) {
                    String indexName = index.getName().toLowerCase();
                    String indexType = convertIndexType(null);
                    herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).type(indexType).uuid(UUID.randomUUID().toString()).table(tableName).tablespace(tableSpace);
                    for (String columnName : index.getColumnsNames()) {
                        columnName = columnName.toLowerCase();
                        Column column = table.getColumn(columnName);
                        if (column == null) {
                            throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
                        }
                        builder.column(column.name, column.type);
                    }
                    otherIndexes.add(builder.build());
                }
            }
        }
        CreateTableStatement statement = new CreateTableStatement(table, otherIndexes);
        return statement;
    } catch (IllegalArgumentException err) {
        throw new StatementExecutionException("bad table definition: " + err.getMessage(), err);
    }
}
Also used : Table(herddb.model.Table) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) CreateTableStatement(herddb.model.commands.CreateTableStatement) ArrayList(java.util.ArrayList) CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) Index(net.sf.jsqlparser.statement.create.table.Index) StatementExecutionException(herddb.model.StatementExecutionException) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition) Column(herddb.model.Column) HashSet(java.util.HashSet)

Example 2 with Index

use of net.sf.jsqlparser.statement.create.table.Index in project herddb by diennea.

the class JdbcQueryRewriter method visit.

@Override
public void visit(CreateIndex createIndex) {
    visit(createIndex.getTable());
    Index index = createIndex.getIndex();
    visit(index);
}
Also used : CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) Index(net.sf.jsqlparser.statement.create.table.Index)

Example 3 with Index

use of net.sf.jsqlparser.statement.create.table.Index in project herddb by diennea.

the class JSQLParserPlanner method buildCreateTableStatement.

private Statement buildCreateTableStatement(String defaultTableSpace, CreateTable s) throws StatementExecutionException {
    String tableSpace = fixMySqlBackTicks(s.getTable().getSchemaName());
    String tableName = fixMySqlBackTicks(s.getTable().getName());
    if (tableSpace == null) {
        tableSpace = defaultTableSpace;
    }
    if (s.getColumnDefinitions() == null) {
        throw new StatementExecutionException("A table must have at least 1 column");
    }
    final boolean isNotExsists = s.isIfNotExists();
    try {
        boolean foundPk = false;
        Table.Builder tablebuilder = Table.builder().uuid(UUID.randomUUID().toString()).name(tableName).tablespace(tableSpace);
        Set<String> primaryKey = new HashSet<>();
        Set<String> simpleUniqueFields = new HashSet<>();
        if (s.getIndexes() != null) {
            for (Index index : s.getIndexes()) {
                if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
                    for (String n : index.getColumnsNames()) {
                        n = fixMySqlBackTicks(n.toLowerCase());
                        tablebuilder.primaryKey(n);
                        primaryKey.add(n);
                        foundPk = true;
                    }
                }
            }
        }
        int position = 0;
        for (ColumnDefinition cf : s.getColumnDefinitions()) {
            String columnName = fixMySqlBackTicks(cf.getColumnName().toLowerCase());
            int type;
            String dataType = cf.getColDataType().getDataType();
            List<String> columnSpecs = decodeColumnSpecs(cf.getColumnSpecs());
            type = sqlDataTypeToColumnType(dataType, cf.getColDataType().getArgumentsStringList(), columnSpecs);
            Bytes defaultValue = decodeDefaultValue(cf, type);
            if (!columnSpecs.isEmpty()) {
                boolean auto_increment = decodeAutoIncrement(columnSpecs);
                if (columnSpecs.contains("PRIMARY")) {
                    foundPk = true;
                    tablebuilder.primaryKey(columnName, auto_increment);
                }
                if (auto_increment && primaryKey.contains(columnName)) {
                    tablebuilder.primaryKey(columnName, auto_increment);
                }
                boolean isUnique = columnSpecs.contains("UNIQUE");
                if (isUnique) {
                    simpleUniqueFields.add(columnName);
                }
            }
            tablebuilder.column(columnName, type, position++, defaultValue);
        }
        if (!foundPk) {
            tablebuilder.column("_pk", ColumnTypes.LONG, position++, null);
            tablebuilder.primaryKey("_pk", true);
        }
        Table table = tablebuilder.build();
        List<herddb.model.Index> otherIndexes = new ArrayList<>();
        List<herddb.model.ForeignKeyDef> foreignKeys = new ArrayList<>();
        if (s.getIndexes() != null) {
            for (Index index : s.getIndexes()) {
                if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
                } else if (index.getType().equalsIgnoreCase("INDEX") || index.getType().equalsIgnoreCase("KEY") || index.getType().equalsIgnoreCase("UNIQUE KEY")) {
                    String indexName = fixMySqlBackTicks(index.getName().toLowerCase());
                    String indexType = convertIndexType(null);
                    boolean unique = index.getType().equalsIgnoreCase("UNIQUE KEY");
                    herddb.model.Index.Builder builder = herddb.model.Index.builder().onTable(table).name(indexName).unique(unique).type(indexType).uuid(UUID.randomUUID().toString());
                    for (String columnName : index.getColumnsNames()) {
                        columnName = fixMySqlBackTicks(columnName.toLowerCase());
                        Column column = table.getColumn(columnName);
                        if (column == null) {
                            throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
                        }
                        builder.column(column.name, column.type);
                    }
                    otherIndexes.add(builder.build());
                } else if (index.getType().equals("FOREIGN KEY")) {
                    ForeignKeyIndex fk = (ForeignKeyIndex) index;
                    ForeignKeyDef fkDef = parseForeignKeyIndex(fk, table, tableName, tableSpace);
                    foreignKeys.add(fkDef);
                } else {
                    throw new StatementExecutionException("Unsupported index type " + index.getType());
                }
            }
        }
        for (String col : simpleUniqueFields) {
            herddb.model.Index.Builder builder = herddb.model.Index.builder().onTable(table).name(table.name + "_unique_" + col).unique(true).type(herddb.model.Index.TYPE_BRIN).uuid(UUID.randomUUID().toString()).column(col, table.getColumn(col).type);
            otherIndexes.add(builder.build());
        }
        if (!foreignKeys.isEmpty()) {
            table = table.withForeignKeys(foreignKeys.toArray(new ForeignKeyDef[0]));
        }
        CreateTableStatement statement = new CreateTableStatement(table, otherIndexes, isNotExsists);
        return statement;
    } catch (IllegalArgumentException err) {
        throw new StatementExecutionException("bad table definition: " + err.getMessage(), err);
    }
}
Also used : Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) CreateTableStatement(herddb.model.commands.CreateTableStatement) ArrayList(java.util.ArrayList) CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) ForeignKeyIndex(net.sf.jsqlparser.statement.create.table.ForeignKeyIndex) Index(net.sf.jsqlparser.statement.create.table.Index) StatementExecutionException(herddb.model.StatementExecutionException) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition) Bytes(herddb.utils.Bytes) Column(herddb.model.Column) ForeignKeyDef(herddb.model.ForeignKeyDef) ForeignKeyIndex(net.sf.jsqlparser.statement.create.table.ForeignKeyIndex) HashSet(java.util.HashSet)

Example 4 with Index

use of net.sf.jsqlparser.statement.create.table.Index in project herddb by diennea.

the class JSQLParserPlanner method buildCreateIndexStatement.

private Statement buildCreateIndexStatement(String defaultTableSpace, CreateIndex s) throws StatementExecutionException {
    try {
        String tableSpace = s.getTable().getSchemaName();
        if (tableSpace == null) {
            tableSpace = defaultTableSpace;
        }
        tableSpace = fixMySqlBackTicks(tableSpace);
        String tableName = fixMySqlBackTicks(s.getTable().getName().toLowerCase());
        String indexName = fixMySqlBackTicks(s.getIndex().getName().toLowerCase());
        boolean unique = isUnique(s.getIndex().getType());
        String indexType = convertIndexType(s.getIndex().getType());
        herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).uuid(UUID.randomUUID().toString()).type(indexType).unique(unique).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 = fixMySqlBackTicks(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) ForeignKeyIndex(net.sf.jsqlparser.statement.create.table.ForeignKeyIndex) Index(net.sf.jsqlparser.statement.create.table.Index) StatementExecutionException(herddb.model.StatementExecutionException)

Example 5 with Index

use of net.sf.jsqlparser.statement.create.table.Index in project JSqlParser by JSQLParser.

the class CreateTableTest method testRUBiSCreateList.

public void testRUBiSCreateList() throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(CreateTableTest.class.getResourceAsStream("/RUBiS-create-requests.txt")));
    TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
    try {
        int numSt = 1;
        while (true) {
            String line = getLine(in);
            if (line == null) {
                break;
            }
            if (!"#begin".equals(line)) {
                break;
            }
            line = getLine(in);
            StringBuilder buf = new StringBuilder(line);
            while (true) {
                line = getLine(in);
                if ("#end".equals(line)) {
                    break;
                }
                buf.append("\n");
                buf.append(line);
            }
            String query = buf.toString();
            if (!getLine(in).equals("true")) {
                continue;
            }
            String tableName = getLine(in);
            String cols = getLine(in);
            try {
                CreateTable createTable = (CreateTable) parserManager.parse(new StringReader(query));
                String[] colsList = null;
                if ("null".equals(cols)) {
                    colsList = new String[0];
                } else {
                    StringTokenizer tokenizer = new StringTokenizer(cols, " ");
                    List colsListList = new ArrayList();
                    while (tokenizer.hasMoreTokens()) {
                        colsListList.add(tokenizer.nextToken());
                    }
                    colsList = (String[]) colsListList.toArray(new String[colsListList.size()]);
                }
                List colsFound = new ArrayList();
                if (createTable.getColumnDefinitions() != null) {
                    for (Iterator iter = createTable.getColumnDefinitions().iterator(); iter.hasNext(); ) {
                        ColumnDefinition columnDefinition = (ColumnDefinition) iter.next();
                        String colName = columnDefinition.getColumnName();
                        boolean unique = false;
                        if (createTable.getIndexes() != null) {
                            for (Iterator iterator = createTable.getIndexes().iterator(); iterator.hasNext(); ) {
                                Index index = (Index) iterator.next();
                                if (index.getType().equals("PRIMARY KEY") && index.getColumnsNames().size() == 1 && index.getColumnsNames().get(0).equals(colName)) {
                                    unique = true;
                                }
                            }
                        }
                        if (!unique) {
                            if (columnDefinition.getColumnSpecStrings() != null) {
                                for (Iterator iterator = columnDefinition.getColumnSpecStrings().iterator(); iterator.hasNext(); ) {
                                    String par = (String) iterator.next();
                                    if (par.equals("UNIQUE")) {
                                        unique = true;
                                    } else if (par.equals("PRIMARY") && iterator.hasNext() && iterator.next().equals("KEY")) {
                                        unique = true;
                                    }
                                }
                            }
                        }
                        if (unique) {
                            colName += ".unique";
                        }
                        colsFound.add(colName.toLowerCase());
                    }
                }
                assertEquals("stm:" + query, colsList.length, colsFound.size());
                for (int i = 0; i < colsList.length; i++) {
                    assertEquals("stm:" + query, colsList[i], colsFound.get(i));
                }
            } catch (Exception e) {
                throw new TestException("error at stm num: " + numSt + "  " + query, e);
            }
            numSt++;
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TestException(net.sf.jsqlparser.test.TestException) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) ArrayList(java.util.ArrayList) Index(net.sf.jsqlparser.statement.create.table.Index) TablesNamesFinder(net.sf.jsqlparser.util.TablesNamesFinder) TestException(net.sf.jsqlparser.test.TestException) JSQLParserException(net.sf.jsqlparser.JSQLParserException) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition) StringTokenizer(java.util.StringTokenizer) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Index (net.sf.jsqlparser.statement.create.table.Index)10 CreateIndex (net.sf.jsqlparser.statement.create.index.CreateIndex)6 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)5 Column (herddb.model.Column)4 StatementExecutionException (herddb.model.StatementExecutionException)4 ColumnDefinition (net.sf.jsqlparser.statement.create.table.ColumnDefinition)4 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)3 AbstractTableManager (herddb.core.AbstractTableManager)2 Table (herddb.model.Table)2 TableDoesNotExistException (herddb.model.TableDoesNotExistException)2 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)2 CreateTableStatement (herddb.model.commands.CreateTableStatement)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 ForeignKeyIndex (net.sf.jsqlparser.statement.create.table.ForeignKeyIndex)2 ForeignKeyDef (herddb.model.ForeignKeyDef)1 ShowCreateTableCalculator.calculateShowCreateTable (herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable)1 Bytes (herddb.utils.Bytes)1 BufferedReader (java.io.BufferedReader)1