/**
   * Generates the SQL statements required to run the change.
   *
   * @param database databasethe target {@link liquibase.database.Database} associated to this
   *     change's statements
   * @return an array of {@link String}s with the statements
   */
  public SqlStatement[] generateStatements(Database database) {
    final InsertStatement insertDefinition =
        new InsertStatement(database.getDefaultSchemaName(), "krim_attr_defn_t");
    final SqlStatement getId =
        new RuntimeStatement() {
          public Sql[] generate(Database database) {
            return new Sql[] {
              new UnparsedSql("insert into krim_attr_defn_id_s values(null)"),
              new UnparsedSql("select max(id) from krim_attr_defn_id_s")
            };
          }
        };

    try {
      final BigInteger id =
          (BigInteger)
              ExecutorService.getInstance()
                  .getExecutor(database)
                  .queryForObject(getId, BigInteger.class);

      insertDefinition.addColumnValue("KIM_ATTR_DEFN_ID", id);
      insertDefinition.addColumnValue("nmspc_cd", getNamespace());
      insertDefinition.addColumnValue("NM", getName());
      insertDefinition.addColumnValue("LBL", getLabel());
      insertDefinition.addColumnValue("actv_ind", getActive());
      insertDefinition.addColumnValue("CMPNT_NM", getComponent());
      insertDefinition.addColumnValue("ver_nbr", 1);
      insertDefinition.addColumnValue("obj_id", "sys_guid()");
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    return new SqlStatement[] {insertDefinition};
  }
예제 #2
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};
  }
  @Override
  public Sql[] generateSql(
      MarkChangeSetRanStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String dateValue = database.getCurrentDateTimeFunction();

    ChangeSet changeSet = statement.getChangeSet();

    SqlStatement runStatement;
    try {
      if (statement.getExecType().equals(ChangeSet.ExecType.FAILED)
          || statement.getExecType().equals(ChangeSet.ExecType.SKIPPED)) {
        return new Sql[0]; // don't mark
      } else if (statement.getExecType().ranBefore) {
        runStatement =
            new UpdateStatement(
                    database.getLiquibaseCatalogName(),
                    database.getLiquibaseSchemaName(),
                    database.getDatabaseChangeLogTableName())
                .addNewColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue))
                .addNewColumnValue("MD5SUM", changeSet.generateCheckSum().toString())
                .addNewColumnValue("EXECTYPE", statement.getExecType().value)
                .setWhereClause("ID=? AND AUTHOR=? AND FILENAME=?")
                .addWhereParameters(
                    changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath());
      } else {
        runStatement =
            new InsertStatement(
                    database.getLiquibaseCatalogName(),
                    database.getLiquibaseSchemaName(),
                    database.getDatabaseChangeLogTableName())
                .addColumnValue("ID", changeSet.getId())
                .addColumnValue("AUTHOR", changeSet.getAuthor())
                .addColumnValue("FILENAME", changeSet.getFilePath())
                .addColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue))
                .addColumnValue(
                    "ORDEREXECUTED",
                    ChangeLogHistoryServiceFactory.getInstance()
                        .getChangeLogService(database)
                        .getNextSequenceValue())
                .addColumnValue("MD5SUM", changeSet.generateCheckSum().toString())
                .addColumnValue("DESCRIPTION", limitSize(changeSet.getDescription()))
                .addColumnValue(
                    "COMMENTS",
                    limitSize(
                        database.escapeStringForDatabase(
                            StringUtils.trimToEmpty(changeSet.getComments()))))
                .addColumnValue("EXECTYPE", statement.getExecType().value)
                .addColumnValue(
                    "LIQUIBASE", LiquibaseUtil.getBuildVersion().replaceAll("SNAPSHOT", "SNP"));

        String tag = null;
        List<Change> changes = changeSet.getChanges();
        if (changes != null && changes.size() == 1) {
          Change change = changes.get(0);
          if (change instanceof TagDatabaseChange) {
            TagDatabaseChange tagChange = (TagDatabaseChange) change;
            tag = tagChange.getTag();
          }
        }
        if (tag != null) {
          ((InsertStatement) runStatement).addColumnValue("TAG", tag);
        }
      }
    } catch (LiquibaseException e) {
      throw new UnexpectedLiquibaseException(e);
    }

    return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
  }