Search in sources :

Example 1 with AlterTableVisitor

use of liquibase.database.core.SQLiteDatabase.AlterTableVisitor in project liquibase by liquibase.

the class AddNotNullConstraintChange method generateStatementsForSQLiteDatabase.

private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
    // SQLite does not support this ALTER TABLE operation until now.
    // For more information see: http://www.sqlite.org/omitted.html.
    // This is a small work around...
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    if (defaultNullValue != null) {
        statements.add(new UpdateStatement(getCatalogName(), getSchemaName(), getTableName()).addNewColumnValue(getColumnName(), getDefaultNullValue()).setWhereClause(getColumnName() + " IS NULL"));
    }
    //		// ... test if column contains NULL values
    //		if (defaultNullValue == null) {
    //			List<Map> null_rows = null;
    //			try {
    //				null_rows = database.getExecutor().
    //					queryForList(new RawSqlStatement(
    //						"SELECT * FROM `"+
    //						database.escapeTableName(getSchemaName(), getTableName())+
    //						"` WHERE `"+getColumnName()+"` IS NULL;"));
    //			} catch (DatabaseException e) {
    //				e.printStackTrace();
    //			}
    //    		if (null_rows.size()>0) {
    //    			throw new UnsupportedChangeException(
    //    					"Failed to add a Not-Null-Constraint because " +
    //    					"some values are null. Use the " +
    //    					"defaultNullValue attribute to define default " +
    //    					"values for the existing null values.");
    //    		}
    //    	}
    // define alter table logic
    AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {

        @Override
        public ColumnConfig[] getColumnsToAdd() {
            return new ColumnConfig[0];
        }

        @Override
        public boolean copyThisColumn(ColumnConfig column) {
            return true;
        }

        @Override
        public boolean createThisColumn(ColumnConfig column) {
            if (column.getName().equals(getColumnName())) {
                column.getConstraints().setNullable(false);
            }
            return true;
        }

        @Override
        public boolean createThisIndex(Index index) {
            return true;
        }
    };
    try {
        // alter table
        statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getCatalogName(), getSchemaName(), getTableName()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) AlterTableVisitor(liquibase.database.core.SQLiteDatabase.AlterTableVisitor) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index)

Example 2 with AlterTableVisitor

use of liquibase.database.core.SQLiteDatabase.AlterTableVisitor in project liquibase by liquibase.

the class DropPrimaryKeyChange method generateStatementsForSQLiteDatabase.

private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
    // SQLite does not support this ALTER TABLE operation until now.
    // For more information see: http://www.sqlite.org/omitted.html.
    // This is a small work around...
    // Note: The attribute "constraintName" is used to pass the column 
    // name instead of the constraint name.
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    // define alter table logic
    AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {

        @Override
        public ColumnConfig[] getColumnsToAdd() {
            return new ColumnConfig[0];
        }

        @Override
        public boolean copyThisColumn(ColumnConfig column) {
            return true;
        }

        @Override
        public boolean createThisColumn(ColumnConfig column) {
            if (column.getName().equals(getConstraintName())) {
                column.getConstraints().setPrimaryKey(false);
            }
            return true;
        }

        @Override
        public boolean createThisIndex(Index index) {
            return true;
        }
    };
    try {
        // alter table
        statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getCatalogName(), getSchemaName(), getTableName()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) AlterTableVisitor(liquibase.database.core.SQLiteDatabase.AlterTableVisitor) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index)

Example 3 with AlterTableVisitor

use of liquibase.database.core.SQLiteDatabase.AlterTableVisitor in project liquibase by liquibase.

the class MergeColumnChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    AddColumnChange addNewColumnChange = new AddColumnChange();
    addNewColumnChange.setSchemaName(schemaName);
    addNewColumnChange.setTableName(getTableName());
    AddColumnConfig columnConfig = new AddColumnConfig();
    columnConfig.setName(getFinalColumnName());
    columnConfig.setType(getFinalColumnType());
    addNewColumnChange.addColumn(columnConfig);
    statements.addAll(Arrays.asList(addNewColumnChange.generateStatements(database)));
    String updateStatement = "UPDATE " + database.escapeTableName(getCatalogName(), getSchemaName(), getTableName()) + " SET " + database.escapeObjectName(getFinalColumnName(), Column.class) + " = " + database.getConcatSql(database.escapeObjectName(getColumn1Name(), Column.class), "'" + getJoinString() + "'", database.escapeObjectName(getColumn2Name(), Column.class));
    statements.add(new RawSqlStatement(updateStatement));
    if (database instanceof SQLiteDatabase) {
        // SQLite does not support this ALTER TABLE operation until now.
        // For more information see: http://www.sqlite.org/omitted.html
        // This is a small work around...
        // define alter table logic
        AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {

            @Override
            public ColumnConfig[] getColumnsToAdd() {
                ColumnConfig[] new_columns = new ColumnConfig[1];
                ColumnConfig new_column = new ColumnConfig();
                new_column.setName(getFinalColumnName());
                new_column.setType(getFinalColumnType());
                new_columns[0] = new_column;
                return new_columns;
            }

            @Override
            public boolean copyThisColumn(ColumnConfig column) {
                return !(column.getName().equals(getColumn1Name()) || column.getName().equals(getColumn2Name()));
            }

            @Override
            public boolean createThisColumn(ColumnConfig column) {
                return !(column.getName().equals(getColumn1Name()) || column.getName().equals(getColumn2Name()));
            }

            @Override
            public boolean createThisIndex(Index index) {
                return !(index.getColumnNames().contains(getColumn1Name()) || index.getColumnNames().contains(getColumn2Name()));
            }
        };
        try {
            // alter table
            statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getCatalogName(), getSchemaName(), getTableName()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        // ...if it is not a SQLite database 
        DropColumnChange dropColumn1Change = new DropColumnChange();
        dropColumn1Change.setSchemaName(schemaName);
        dropColumn1Change.setTableName(getTableName());
        dropColumn1Change.setColumnName(getColumn1Name());
        statements.addAll(Arrays.asList(dropColumn1Change.generateStatements(database)));
        DropColumnChange dropColumn2Change = new DropColumnChange();
        dropColumn2Change.setSchemaName(schemaName);
        dropColumn2Change.setTableName(getTableName());
        dropColumn2Change.setColumnName(getColumn2Name());
        statements.addAll(Arrays.asList(dropColumn2Change.generateStatements(database)));
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index) RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) Column(liquibase.structure.core.Column) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) AlterTableVisitor(liquibase.database.core.SQLiteDatabase.AlterTableVisitor)

Example 4 with AlterTableVisitor

use of liquibase.database.core.SQLiteDatabase.AlterTableVisitor in project liquibase by liquibase.

the class RenameColumnChange method generateStatementsForSQLiteDatabase.

private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
    // SQLite does not support this ALTER TABLE operation until now.
    // For more information see: http://www.sqlite.org/omitted.html.
    // This is a small work around...
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    // define alter table logic
    AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {

        @Override
        public ColumnConfig[] getColumnsToAdd() {
            return new ColumnConfig[0];
        }

        @Override
        public boolean copyThisColumn(ColumnConfig column) {
            return true;
        }

        @Override
        public boolean createThisColumn(ColumnConfig column) {
            if (column.getName().equals(getOldColumnName())) {
                column.setName(getNewColumnName());
            }
            return true;
        }

        @Override
        public boolean createThisIndex(Index index) {
            if (index.getColumnNames().contains(getOldColumnName())) {
                Iterator<Column> columnIterator = index.getColumns().iterator();
                while (columnIterator.hasNext()) {
                    Column column = columnIterator.next();
                    if (column.getName().equals(getOldColumnName())) {
                        columnIterator.remove();
                        break;
                    }
                }
                index.addColumn(new Column(getNewColumnName()).setRelation(index.getTable()));
            }
            return true;
        }
    };
    try {
        // alter table
        statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getCatalogName(), getSchemaName(), getTableName()));
    } catch (Exception e) {
        System.err.println(e);
        e.printStackTrace();
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) Column(liquibase.structure.core.Column) AlterTableVisitor(liquibase.database.core.SQLiteDatabase.AlterTableVisitor) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index)

Aggregations

ArrayList (java.util.ArrayList)4 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)4 SqlStatement (liquibase.statement.SqlStatement)4 Index (liquibase.structure.core.Index)4 Column (liquibase.structure.core.Column)2 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)1 RawSqlStatement (liquibase.statement.core.RawSqlStatement)1 UpdateStatement (liquibase.statement.core.UpdateStatement)1