@Override public ValidationErrors validate( AddAutoIncrementStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { ValidationErrors validationErrors = new ValidationErrors(); validationErrors.checkRequiredField("columnName", statement.getColumnName()); validationErrors.checkRequiredField("tableName", statement.getTableName()); return validationErrors; }
@Override public Sql[] generateSql( final AddAutoIncrementStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { List<Sql> statements = new ArrayList<Sql>(); // define alter table logic SQLiteDatabase.AlterTableVisitor rename_alter_visitor = new SQLiteDatabase.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(statement.getColumnName())) { column.setAutoIncrement(true); column.setType("INTEGER"); } return true; } @Override public boolean createThisIndex(Index index) { return true; } }; try { // alter table for (SqlStatement generatedStatement : SQLiteDatabase.getAlterTableStatements( rename_alter_visitor, database, statement.getCatalogName(), statement.getSchemaName(), statement.getTableName())) { statements.addAll( Arrays.asList( SqlGeneratorFactory.getInstance().generateSql(generatedStatement, database))); } } catch (DatabaseException e) { e.printStackTrace(); } return statements.toArray(new Sql[statements.size()]); }
@Override public Sql[] generateSql( AddAutoIncrementStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { return new Sql[] { new UnparsedSql( "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) + " ALTER COLUMN " + database.escapeColumnName( statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " SET " + database.getAutoIncrementClause( statement.getStartWith(), statement.getIncrementBy()), new Column() .setTable(new Table(statement.getTableName()).setSchema(statement.getSchemaName())) .setName(statement.getColumnName())) }; }
@Override public ValidationErrors validate( AddAutoIncrementStatement addAutoIncrementStatement, Database database, SqlGeneratorChain sqlGeneratorChain) { ValidationErrors validationErrors = super.validate(addAutoIncrementStatement, database, sqlGeneratorChain); validationErrors.checkRequiredField( "columnDataType", addAutoIncrementStatement.getColumnDataType()); return validationErrors; }
@Override public Sql[] generateSql( AddAutoIncrementStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { return new Sql[] { new UnparsedSql( "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) + " ALTER COLUMN " + database.escapeColumnName( statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " SET GENERATED ALWAYS AS IDENTITY", new Column() .setTable(new Table(statement.getTableName()).setSchema(statement.getSchemaName())) .setName(statement.getColumnName())) }; }
@Override public Sql[] generateSql( AddAutoIncrementStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { return new Sql[] { new UnparsedSql( "ALTER TABLE " + database.escapeTableName( statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " MODIFY " + database.escapeColumnName( statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + DataTypeFactory.getInstance() .fromDescription(statement.getColumnDataType() + "{autoIncrement:true}") .toDatabaseDataType(database), getAffectedColumn(statement)) }; }