Search in sources :

Example 1 with ColDataType

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

the class DeclareStatementTest method testDeclareTable.

@Test
public void testDeclareTable() throws JSQLParserException {
    String statement = "DECLARE @MyTableVar TABLE (EmpID int NOT NULL, OldVacationHours int, NewVacationHours int, ModifiedDate datetime)";
    Statement parsed = assertSqlCanBeParsedAndDeparsed(statement);
    DeclareStatement created = new DeclareStatement().withUserVariable(new UserVariable("MyTableVar")).withColumnDefinitions(new ArrayList<>()).addColumnDefinitions(new ColumnDefinition("EmpID", new ColDataType().withDataType("int"), asList("NOT", "NULL")), new ColumnDefinition("OldVacationHours", new ColDataType("int"))).addColumnDefinitions(asList(new ColumnDefinition("NewVacationHours", new ColDataType("int")), new ColumnDefinition("ModifiedDate", new ColDataType("datetime")))).withDeclareType(DeclareType.TABLE);
    assertDeparse(created, statement);
    assertEqualsObjectTree(parsed, created);
}
Also used : ArrayList(java.util.ArrayList) ColDataType(net.sf.jsqlparser.statement.create.table.ColDataType) UserVariable(net.sf.jsqlparser.expression.UserVariable) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 2 with ColDataType

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

the class CreateTableTest method testExcludeWhereConstraint.

@Test
public void testExcludeWhereConstraint() throws JSQLParserException {
    String statement = "CREATE TABLE foo (col1 integer, EXCLUDE WHERE (col1 > 100))";
    assertSqlCanBeParsedAndDeparsed(statement);
    assertDeparse(new CreateTable().withTable(new Table("foo")).addIndexes(new ExcludeConstraint().withExpression(new GreaterThan().withLeftExpression(new Column("col1")).withRightExpression(new LongValue(100)))).addColumnDefinitions(new ColumnDefinition("col1", new ColDataType("integer"))), statement);
}
Also used : Table(net.sf.jsqlparser.schema.Table) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) Column(net.sf.jsqlparser.schema.Column) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) LongValue(net.sf.jsqlparser.expression.LongValue) ExcludeConstraint(net.sf.jsqlparser.statement.create.table.ExcludeConstraint) ColDataType(net.sf.jsqlparser.statement.create.table.ColDataType) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with ColDataType

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

the class CreateTableTest method testCreateTableIssue113.

@Test
public void testCreateTableIssue113() throws JSQLParserException {
    String statement = "CREATE TABLE foo (reason character varying (255) DEFAULT 'Test' :: character varying NOT NULL)";
    assertSqlCanBeParsedAndDeparsed(statement);
    assertDeparse(new CreateTable().withTable(new Table().withName("foo")).withColumnDefinitions(Arrays.asList(new ColumnDefinition().withColumnName("reason").withColDataType(new ColDataType().withDataType("character varying").addArgumentsStringList(Arrays.asList("255"))).addColumnSpecs("DEFAULT 'Test' :: character varying", "NOT NULL"))), statement);
}
Also used : Table(net.sf.jsqlparser.schema.Table) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) ColDataType(net.sf.jsqlparser.statement.create.table.ColDataType) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 4 with ColDataType

use of net.sf.jsqlparser.statement.create.table.ColDataType in project canal-utils by wz2cool.

the class BaseAlterSqlConverter method getAddColumnExpressions.

private List<AlterColumnExpression> getAddColumnExpressions(final AlterOperation alterOperation, final String tableName, final List<AlterExpression.ColumnDataType> columnDataTypes) {
    List<AlterColumnExpression> result = new ArrayList<>();
    if (alterOperation != AlterOperation.ADD || columnDataTypes == null || columnDataTypes.isEmpty()) {
        return result;
    }
    for (AlterExpression.ColumnDataType columnDataType : columnDataTypes) {
        AlterColumnExpression addColumnExpression = new AlterColumnExpression();
        String columnName = columnDataType.getColumnName();
        ColDataType mysqlColDataType = columnDataType.getColDataType();
        Optional<ColDataType> covColDataTypeOptional = getColDataTypeConverter().convert(mysqlColDataType);
        if (!covColDataTypeOptional.isPresent()) {
            String errorMsg = String.format("[Add Column] Cannot convert data type: %s", mysqlColDataType.getDataType());
            throw new NotSupportDataTypeException(errorMsg);
        }
        final boolean unsignedFlag = columnDataType.getColumnSpecs().stream().anyMatch("unsigned"::equalsIgnoreCase);
        addColumnExpression.setTableName(cleanText(tableName));
        addColumnExpression.setColumnName(cleanText(columnName));
        addColumnExpression.setOperation(EnhancedAlterOperation.ADD_COLUMN);
        addColumnExpression.setColDataType(covColDataTypeOptional.get());
        addColumnExpression.setUnsignedFlag(unsignedFlag);
        result.add(addColumnExpression);
    }
    return result;
}
Also used : NotSupportDataTypeException(com.github.wz2cool.canal.utils.model.exception.NotSupportDataTypeException) AlterColumnExpression(com.github.wz2cool.canal.utils.model.AlterColumnExpression) ArrayList(java.util.ArrayList) ColDataType(net.sf.jsqlparser.statement.create.table.ColDataType) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression)

Example 5 with ColDataType

use of net.sf.jsqlparser.statement.create.table.ColDataType in project canal-utils by wz2cool.

the class Db2AlterSqlConverter method convertToAddColumnSql.

@Override
protected Optional<String> convertToAddColumnSql(AlterColumnExpression alterColumnExpression) {
    String tableName = alterColumnExpression.getTableName();
    String columnName = alterColumnExpression.getColumnName();
    ColDataType colDataType = alterColumnExpression.getColDataType();
    String dataTypeString = getDataTypeString(colDataType);
    String sql = String.format("ALTER TABLE %s ADD COLUMN %s %s", tableName, columnName, dataTypeString);
    return Optional.of(sql);
}
Also used : ColDataType(net.sf.jsqlparser.statement.create.table.ColDataType)

Aggregations

ColDataType (net.sf.jsqlparser.statement.create.table.ColDataType)64 Test (org.junit.Test)24 ArrayList (java.util.ArrayList)14 Test (org.junit.jupiter.api.Test)8 MysqlDataType (com.github.wz2cool.canal.utils.model.MysqlDataType)6 ColumnDefinition (net.sf.jsqlparser.statement.create.table.ColumnDefinition)5 UserVariable (net.sf.jsqlparser.expression.UserVariable)4 Table (net.sf.jsqlparser.schema.Table)4 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)4 AlterColumnExpression (com.github.wz2cool.canal.utils.model.AlterColumnExpression)3 NotSupportDataTypeException (com.github.wz2cool.canal.utils.model.exception.NotSupportDataTypeException)3 TypeDefExpr (net.sf.jsqlparser.statement.DeclareStatement.TypeDefExpr)3 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)3 Cast (com.revolsys.record.query.Cast)1 QueryValue (com.revolsys.record.query.QueryValue)1 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)1 CastExpression (net.sf.jsqlparser.expression.CastExpression)1 Expression (net.sf.jsqlparser.expression.Expression)1 LongValue (net.sf.jsqlparser.expression.LongValue)1 NotExpression (net.sf.jsqlparser.expression.NotExpression)1