net.sf.jsqlparser.statement.create.table.CreateTable Java Examples

The following examples show how to use net.sf.jsqlparser.statement.create.table.CreateTable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DDLSQLPlanner.java    From herddb with Apache License 2.0 6 votes vote down vote up
private ExecutionPlan plan(
        String defaultTableSpace, net.sf.jsqlparser.statement.Statement stmt,
        boolean scan, boolean returnValues, int maxRows
) {
    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 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;
}
 
Example #2
Source File: CloudSpannerPreparedStatementTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private static void testCreateTableStatement(String sql) throws SQLException {
  boolean isDDL = isDDLStatement(sql);
  Assert.assertTrue(isDDL);
  Statement statement = null;
  try {
    statement = CCJSqlParserUtil.parse(sql);
  } catch (JSQLParserException e) {
    throw new CloudSpannerSQLException("Could not parse SQL statement", Code.INVALID_ARGUMENT, e);
  }
  Assert.assertNotNull(statement);
  Assert.assertEquals(CreateTable.class, statement.getClass());
}
 
Example #3
Source File: TablesNamesFinder.java    From evosql with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(CreateTable create) {
    tables.add(create.getTable().getFullyQualifiedName());
    if (create.getSelect() != null) {
        create.getSelect().accept(this);
    }
}
 
Example #4
Source File: ScaffoldCommandRegister.java    From enkan with Eclipse Public License 1.0 5 votes vote down vote up
private Generator tableGenerator(String sql, DataSource ds) {
    try {
        CreateTable stmt = (CreateTable) CCJSqlParserUtil.parse("CREATE TABLE " + sql);
        return new Generator()
                .writing("migration", g -> g.task(
                        new FlywayTask("src/main/java", stmt.getTable().getName(), "CREATE TABLE " + sql)));
    } catch (JSQLParserException e) {
        throw new IllegalArgumentException("Statement generating a table is wrong syntax.", e);
    }
}
 
Example #5
Source File: TableRenameVisitor.java    From compass with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(CreateTable create) 
{
    create.getTable().accept(this);
    
    if (create.getSelect() != null)
    {
        create.getSelect().accept(this);
    }
}
 
Example #6
Source File: CRUDParseUtils.java    From WeBASE-Front with Apache License 2.0 4 votes vote down vote up
public static void parseCreateTable(String sql, Table table)
        throws JSQLParserException, FrontException {
    Statement statement = CCJSqlParserUtil.parse(sql);
    CreateTable createTable = (CreateTable) statement;

    // parse table name
    String tableName = createTable.getTable().getName();
    table.setTableName(tableName);

    // parse key from index
    boolean keyFlag = false;
    List<Index> indexes = createTable.getIndexes();
    if (indexes != null) {
        if (indexes.size() > 1) {
            throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR,
                    "Please provide only one primary key for the table.");
        }
        keyFlag = true;
        Index index = indexes.get(0);
        String type = index.getType().toLowerCase();
        if (PRIMARY_KEY.equals(type)) {
            table.setKey(index.getColumnsNames().get(0));
        } else {
            throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR,
                    "Please provide only one primary key for the table.");
        }
    }
    List<ColumnDefinition> columnDefinitions = createTable.getColumnDefinitions();
    // parse key from ColumnDefinition
    for (int i = 0; i < columnDefinitions.size(); i++) {
        List<String> columnSpecStrings = columnDefinitions.get(i).getColumnSpecStrings();
        if (columnSpecStrings == null) {
            continue;
        } else {
            if (columnSpecStrings.size() == 2
                    && "primary".equals(columnSpecStrings.get(0))
                    && "key".equals(columnSpecStrings.get(1))) {
                String key = columnDefinitions.get(i).getColumnName();
                if (keyFlag) {
                    if (!table.getKey().equals(key)) {
                        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR,
                                "Please provide only one primary key for the table.");
                    }
                } else {
                    keyFlag = true;
                    table.setKey(key);
                }
                break;
            }
        }
    }
    if (!keyFlag) {
        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR, "Please provide a primary key for the table.");
    }
    // parse value field
    List<String> fieldsList = new ArrayList<>();
    for (int i = 0; i < columnDefinitions.size(); i++) {
        String columnName = columnDefinitions.get(i).getColumnName();
        if (fieldsList.contains(columnName)) {
            throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR,
                    "Please provide the field '" + columnName + "' only once.");
        } else {
            fieldsList.add(columnName);
        }
    }
    if (!fieldsList.contains(table.getKey())) {
        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR,
                "Please provide the field '" + table.getKey() + "' in column definition.");
    } else {
        fieldsList.remove(table.getKey());
    }
    StringBuffer fields = new StringBuffer();
    for (int i = 0; i < fieldsList.size(); i++) {
        fields.append(fieldsList.get(i));
        if (i != fieldsList.size() - 1) {
            fields.append(",");
        }
    }
    table.setValueFields(fields.toString());
}
 
Example #7
Source File: AllColumnRefsFinder.java    From jobson with Apache License 2.0 4 votes vote down vote up
public void visit(CreateTable createTable) {
    throw new UnsupportedSQLFeatureException("Feature CreateTable not supported");
}
 
Example #8
Source File: DefaultParser.java    From CodeGen with MIT License 4 votes vote down vote up
@Override
public List<Table> parseSQLs(String sqls) {
    if (StringUtils.isBlank(sqls)) {
        return null;
    }

    List<Table> result = new ArrayList<>();
    // 解析sql语句
    try {
        List<Statement> statements = CCJSqlParserUtil.parseStatements(sqls).getStatements();
        if (statements == null || statements.isEmpty()) {
            throw new RuntimeException("Nothing in parse !!!");
        }
        List<CreateTable> createTables = new ArrayList<>();
        for (Statement statement: statements) {
            if (statement instanceof CreateTable) {
                createTables.add((CreateTable) statement);
            }
        }
        if (createTables.isEmpty()) {
            throw new RuntimeException("Only support create table statement !!!");
        }

        for(CreateTable createTable: createTables) {
            List<Field> fields = new ArrayList<>();
            Table table = new Table(fields);
            table.setTableName(removeQuotes(createTable.getTable().getName()));
            createTable.getColumnDefinitions().forEach(it -> {
                Field field = new Field();
                // 字段名称
                String columnName = removeQuotes(it.getColumnName());
                // 同时设置了 FieldName
                field.setColumn(columnName);

                // 字段类型
                ColDataType colDataType = it.getColDataType();
                // 同时设置了字段类型
                field.setColumnType(colDataType.getDataType());
                field.setColumnSize(firstOrNull(colDataType.getArgumentsStringList()));

                // comment注释
                field.setComment(getColumnComment(it.getColumnSpecStrings()));

                fields.add(field);
            });

            if (table.getFields() != null && !table.getFields().isEmpty()) {
                result.add(table);
            }
        }
        return result;
    } catch (Exception ignore) {
    }
    return null;
}
 
Example #9
Source File: SqlElementVisitor.java    From foxtrot with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(CreateTable createTable) {
    //supported construct
}
 
Example #10
Source File: TableVisitor.java    From DDF with Apache License 2.0 2 votes vote down vote up
@Override
public void visit(CreateTable createTable) throws Exception {

}