Search in sources :

Example 1 with ColumnDefinition

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

use of net.sf.jsqlparser.statement.create.table.ColumnDefinition 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)

Example 3 with ColumnDefinition

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

the class CreateTableDeParser method deParse.

public void deParse(CreateTable createTable) {
    buffer.append("CREATE ");
    if (createTable.isUnlogged()) {
        buffer.append("UNLOGGED ");
    }
    String params = PlainSelect.getStringList(createTable.getCreateOptionsStrings(), false, false);
    if (!"".equals(params)) {
        buffer.append(params).append(' ');
    }
    buffer.append("TABLE ");
    if (createTable.isIfNotExists()) {
        buffer.append("IF NOT EXISTS ");
    }
    buffer.append(createTable.getTable().getFullyQualifiedName());
    if (createTable.getSelect() != null) {
        buffer.append(" AS ");
        if (createTable.isSelectParenthesis()) {
            buffer.append("(");
        }
        buffer.append(createTable.getSelect().toString());
        if (createTable.isSelectParenthesis()) {
            buffer.append(")");
        }
    } else {
        if (createTable.getColumnDefinitions() != null) {
            buffer.append(" (");
            for (Iterator<ColumnDefinition> iter = createTable.getColumnDefinitions().iterator(); iter.hasNext(); ) {
                ColumnDefinition columnDefinition = iter.next();
                buffer.append(columnDefinition.getColumnName());
                buffer.append(" ");
                buffer.append(columnDefinition.getColDataType().toString());
                if (columnDefinition.getColumnSpecStrings() != null) {
                    for (String s : columnDefinition.getColumnSpecStrings()) {
                        buffer.append(" ");
                        buffer.append(s);
                    }
                }
                if (iter.hasNext()) {
                    buffer.append(", ");
                }
            }
            if (createTable.getIndexes() != null) {
                for (Iterator<Index> iter = createTable.getIndexes().iterator(); iter.hasNext(); ) {
                    buffer.append(", ");
                    Index index = iter.next();
                    buffer.append(index.toString());
                }
            }
            buffer.append(")");
        }
    }
    params = PlainSelect.getStringList(createTable.getTableOptionsStrings(), false, false);
    if (!"".equals(params)) {
        buffer.append(' ').append(params);
    }
}
Also used : Index(net.sf.jsqlparser.statement.create.table.Index) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition)

Aggregations

ColumnDefinition (net.sf.jsqlparser.statement.create.table.ColumnDefinition)3 Index (net.sf.jsqlparser.statement.create.table.Index)3 ArrayList (java.util.ArrayList)2 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)2 Column (herddb.model.Column)1 StatementExecutionException (herddb.model.StatementExecutionException)1 Table (herddb.model.Table)1 CreateTableStatement (herddb.model.commands.CreateTableStatement)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 JSQLParserException (net.sf.jsqlparser.JSQLParserException)1 CreateIndex (net.sf.jsqlparser.statement.create.index.CreateIndex)1 TestException (net.sf.jsqlparser.test.TestException)1 TablesNamesFinder (net.sf.jsqlparser.util.TablesNamesFinder)1