@Test
  public void testGeneralteAlterStatementMultipleColumns() {
    ColumnConfig col1 = new ColumnConfig();
    col1.setName("col1_test");
    c.addColumn(col1);
    ColumnConfig col2 = new ColumnConfig();
    col2.setName("col2_test");
    c.addColumn(col2);

    Assert.assertEquals("DROP COLUMN col1_test, DROP COLUMN col2_test", c.generateAlterStatement());
  }
Пример #2
0
  protected void addForeignKeyStatements(
      AddColumnStatement statement, Database database, List<Sql> returnSql) {
    for (ColumnConstraint constraint : statement.getConstraints()) {
      if (constraint instanceof ForeignKeyConstraint) {
        ForeignKeyConstraint fkConstraint = (ForeignKeyConstraint) constraint;
        String refSchemaName = null;
        String refTableName;
        String refColName;
        if (fkConstraint.getReferences() != null) {
          Matcher referencesMatcher =
              Pattern.compile("([\\w\\._]+)\\(([\\w_]+)\\)").matcher(fkConstraint.getReferences());
          if (!referencesMatcher.matches()) {
            throw new UnexpectedLiquibaseException(
                "Don't know how to find table and column names from "
                    + fkConstraint.getReferences());
          }
          refTableName = referencesMatcher.group(1);
          refColName = referencesMatcher.group(2);
        } else {
          refTableName = ((ForeignKeyConstraint) constraint).getReferencedTableName();
          refColName = ((ForeignKeyConstraint) constraint).getReferencedColumnNames();
        }

        if (refTableName.indexOf(".") > 0) {
          refSchemaName = refTableName.split("\\.")[0];
          refTableName = refTableName.split("\\.")[1];
        }

        AddForeignKeyConstraintStatement addForeignKeyConstraintStatement =
            new AddForeignKeyConstraintStatement(
                fkConstraint.getForeignKeyName(),
                statement.getCatalogName(),
                statement.getSchemaName(),
                statement.getTableName(),
                ColumnConfig.arrayFromNames(statement.getColumnName()),
                null,
                refSchemaName,
                refTableName,
                ColumnConfig.arrayFromNames(refColName));
        returnSql.addAll(
            Arrays.asList(
                SqlGeneratorFactory.getInstance()
                    .generateSql(addForeignKeyConstraintStatement, database)));
      }
    }
  }
Пример #3
0
 protected void addUniqueConstrantStatements(
     AddColumnStatement statement, Database database, List<Sql> returnSql) {
   if (statement.isUnique()) {
     AddUniqueConstraintStatement addConstraintStmt =
         new AddUniqueConstraintStatement(
             statement.getCatalogName(),
             statement.getSchemaName(),
             statement.getTableName(),
             ColumnConfig.arrayFromNames(statement.getColumnName()),
             statement.getUniqueStatementName());
     returnSql.addAll(
         Arrays.asList(
             SqlGeneratorFactory.getInstance().generateSql(addConstraintStmt, database)));
   }
 }
Пример #4
0
  public SqlStatement[] generateStatements(Database database) {

    boolean needsPreparedStatement = false;
    for (ColumnConfig column : columns) {
      if (column.getValueBlobFile() != null) {
        needsPreparedStatement = true;
      }
      if (column.getValueClobFile() != null) {
        needsPreparedStatement = true;
      }
      //            if (column.getValueText() != null && database instanceof InformixDatabase) {
      //                needsPreparedStatement = true;
      //            }
    }

    if (needsPreparedStatement) {
      return new SqlStatement[] {
        new InsertExecutablePreparedStatement(database, catalogName, schemaName, tableName, columns)
      };
    }

    InsertStatement statement =
        new InsertStatement(getCatalogName(), getSchemaName(), getTableName());

    for (ColumnConfig column : columns) {

      if (database.supportsAutoIncrement()
          && column.isAutoIncrement() != null
          && column.isAutoIncrement()) {
        // skip auto increment columns as they will be generated by the database
        continue;
      }

      statement.addColumnValue(column.getName(), column.getValueObject());
    }
    return new SqlStatement[] {statement};
  }