Search in sources :

Example 6 with InsertOrUpdateStatement

use of liquibase.statement.core.InsertOrUpdateStatement in project liquibase by liquibase.

the class InsertOrUpdateGeneratorOracleTest method ContainsInsertStatement.

@Test
public void ContainsInsertStatement() {
    OracleDatabase database = new OracleDatabase();
    InsertOrUpdateGeneratorOracle generator = new InsertOrUpdateGeneratorOracle();
    InsertOrUpdateStatement statement = new InsertOrUpdateStatement("mycatalog", "myschema", "mytable", "pk_col1");
    statement.addColumnValue("pk_col1", "value1");
    statement.addColumnValue("col2", "value2");
    Sql[] sql = generator.generateSql(statement, database, null);
    String theSql = sql[0].toSql();
    assertTrue(theSql.contains("INSERT INTO mycatalog.mytable (pk_col1, col2) VALUES ('value1', 'value2');"));
    assertTrue(theSql.contains("UPDATE mycatalog.mytable"));
    String[] sqlLines = theSql.split("\n");
    int lineToCheck = 0;
    assertEquals("DECLARE", sqlLines[lineToCheck].trim());
    lineToCheck++;
    assertEquals("v_reccount NUMBER := 0;", sqlLines[lineToCheck].trim());
    lineToCheck++;
    assertEquals("BEGIN", sqlLines[lineToCheck].trim());
    lineToCheck++;
    assertEquals("SELECT COUNT(*) INTO v_reccount FROM mycatalog.mytable WHERE pk_col1 = 'value1';", sqlLines[lineToCheck].trim());
    lineToCheck++;
    assertEquals("IF v_reccount = 0 THEN", sqlLines[lineToCheck].trim());
    lineToCheck++;
    assertEquals("INSERT INTO mycatalog.mytable (pk_col1, col2) VALUES ('value1', 'value2');", sqlLines[lineToCheck]);
    lineToCheck++;
    assertEquals("ELSIF v_reccount = 1 THEN", sqlLines[lineToCheck].trim());
    lineToCheck++;
    assertEquals("UPDATE mycatalog.mytable SET col2 = 'value2' WHERE pk_col1 = 'value1';", sqlLines[lineToCheck].trim());
    lineToCheck++;
    assertEquals("END IF;", sqlLines[lineToCheck].trim());
    lineToCheck++;
    assertEquals("END;", sqlLines[lineToCheck].trim());
/*
DECLARE
  v_prodcount NUMBER := 0;
BEGIN
  -- Check if product with this name already exists
  SELECT COUNT (*)
  INTO   v_prodcount
   FROM books WHERE isbn = 12345678;
  -- Product does not exist
  IF v_prodcount = 0 THEN
   -- Insert row into PRODUCT based on arguments passed
   INSERT INTO books
   VALUES
         ( 12345678,
           98765432,
           'Working with Liquibase');
  -- Product with this name already exists
  ELSIF v_prodcount = 1 THEN
    -- Update the existing product with values
    -- passed as arguments
    UPDATE books
    SET    author_id = 98765432,
           title = 'Working with liquibase'
    WHERE  isbn = 12345678;
  END IF;
END;*/
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) InsertOrUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Example 7 with InsertOrUpdateStatement

use of liquibase.statement.core.InsertOrUpdateStatement in project liquibase by liquibase.

the class InsertOrUpdateGenerator method getUpdateStatement.

/**
     * 
     * @param insertOrUpdateStatement
     * @param database
     * @param whereClause
     * @param sqlGeneratorChain
     * @return the update statement, if there is nothing to update return null
     */
protected String getUpdateStatement(InsertOrUpdateStatement insertOrUpdateStatement, Database database, String whereClause, SqlGeneratorChain sqlGeneratorChain) throws LiquibaseException {
    StringBuffer updateSqlString = new StringBuffer();
    UpdateGenerator update = new UpdateGenerator();
    UpdateStatement updateStatement = new UpdateStatement(insertOrUpdateStatement.getCatalogName(), insertOrUpdateStatement.getSchemaName(), insertOrUpdateStatement.getTableName());
    if (!(database instanceof OracleDatabase && insertOrUpdateStatement.getOnlyUpdate() != null && insertOrUpdateStatement.getOnlyUpdate())) {
        whereClause += ";\n";
    }
    updateStatement.setWhereClause(whereClause);
    String[] pkFields = insertOrUpdateStatement.getPrimaryKey().split(",");
    HashSet<String> hashPkFields = new HashSet<String>(Arrays.asList(pkFields));
    for (String columnKey : insertOrUpdateStatement.getColumnValues().keySet()) {
        if (!hashPkFields.contains(columnKey)) {
            updateStatement.addNewColumnValue(columnKey, insertOrUpdateStatement.getColumnValue(columnKey));
        }
    }
    // this isn't very elegant but the code fails above without any columns to update
    if (updateStatement.getNewColumnValues().isEmpty()) {
        throw new LiquibaseException("No fields to update in set clause");
    }
    Sql[] updateSql = update.generateSql(updateStatement, database, sqlGeneratorChain);
    for (Sql s : updateSql) {
        updateSqlString.append(s.toSql());
        updateSqlString.append(";");
    }
    updateSqlString.deleteCharAt(updateSqlString.lastIndexOf(";"));
    updateSqlString.append("\n");
    return updateSqlString.toString();
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) InsertOrUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) LiquibaseException(liquibase.exception.LiquibaseException) HashSet(java.util.HashSet) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 8 with InsertOrUpdateStatement

use of liquibase.statement.core.InsertOrUpdateStatement in project liquibase by liquibase.

the class InsertOrUpdateGeneratorMSSQLTest method getElse.

@Test
public void getElse() {
    InsertOrUpdateGeneratorMSSQL generator = new InsertOrUpdateGeneratorMSSQL();
    MSSQLDatabase database = new MSSQLDatabase();
    InsertOrUpdateStatement statement = new InsertOrUpdateStatement("mycatalog", "myschema", "mytable", "pk_col1");
    statement.addColumnValue("pk_col1", "value1");
    statement.addColumnValue("col2", "value2");
    String where = "1 = 1";
    Class c = InsertOrUpdateGenerator.class.getClass();
    //InsertOrUpdateStatement insertOrUpdateStatement, Database database, SqlGeneratorChain sqlGeneratorChain
    String insertStatement = (String) invokePrivateMethod(generator, "getElse", new Object[] { database });
    Integer lineNumber = 0;
    String[] lines = insertStatement.split("\n");
    assertEquals("ELSE", lines[lineNumber]);
}
Also used : InsertOrUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Test(org.junit.Test)

Example 9 with InsertOrUpdateStatement

use of liquibase.statement.core.InsertOrUpdateStatement in project liquibase by liquibase.

the class InsertOrUpdateGeneratorOracleTest method testOnlyUpdateFlag.

@Test
public void testOnlyUpdateFlag() {
    OracleDatabase database = new OracleDatabase();
    InsertOrUpdateGeneratorOracle generator = new InsertOrUpdateGeneratorOracle();
    InsertOrUpdateStatement statement = new InsertOrUpdateStatement("mycatalog", "myschema", "mytable", "pk_col1", true);
    statement.addColumnValue("pk_col1", "value1");
    statement.addColumnValue("col2", "value2");
    Sql[] sql = generator.generateSql(statement, database, null);
    String theSql = sql[0].toSql();
    assertFalse("should not have had insert statement", theSql.contains("INSERT INTO mycatalog.mytable (pk_col1, col2) VALUES ('value1', 'value2');"));
    assertTrue("missing update statement", theSql.contains("UPDATE mycatalog.mytable"));
    String[] sqlLines = theSql.split("\n");
    int lineToCheck = 0;
    assertEquals("UPDATE mycatalog.mytable SET col2 = 'value2' WHERE pk_col1 = 'value1'", sqlLines[lineToCheck].trim());
    lineToCheck++;
    assertEquals("Wrong number of lines", 1, sqlLines.length);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) InsertOrUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Aggregations

InsertOrUpdateStatement (liquibase.statement.core.InsertOrUpdateStatement)9 Test (org.junit.Test)6 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)5 OracleDatabase (liquibase.database.core.OracleDatabase)3 Sql (liquibase.sql.Sql)3 SqlStatement (liquibase.statement.SqlStatement)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ColumnConfig (liquibase.change.ColumnConfig)1 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)1 MySQLDatabase (liquibase.database.core.MySQLDatabase)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 LiquibaseException (liquibase.exception.LiquibaseException)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 Logger (liquibase.logging.Logger)1 UnparsedSql (liquibase.sql.UnparsedSql)1 InsertExecutablePreparedStatement (liquibase.statement.InsertExecutablePreparedStatement)1 DeleteStatement (liquibase.statement.core.DeleteStatement)1 InsertSetStatement (liquibase.statement.core.InsertSetStatement)1