Coverage Report - liquibase.sqlgenerator.core.AddAutoIncrementGeneratorSQLite
 
Classes in this File Line Coverage Branch Coverage Complexity
AddAutoIncrementGeneratorSQLite
23%
3/13
0%
0/2
1.714
AddAutoIncrementGeneratorSQLite$1
0%
0/8
0%
0/2
1.714
 
 1  
 package liquibase.sqlgenerator.core;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Arrays;
 5  
 import java.util.List;
 6  
 
 7  
 import liquibase.change.ColumnConfig;
 8  
 import liquibase.database.Database;
 9  
 import liquibase.database.core.SQLiteDatabase;
 10  
 import liquibase.database.structure.Index;
 11  
 import liquibase.exception.DatabaseException;
 12  
 import liquibase.exception.UnexpectedLiquibaseException;
 13  
 import liquibase.exception.UnsupportedChangeException;
 14  
 import liquibase.sql.Sql;
 15  
 import liquibase.sqlgenerator.SqlGeneratorChain;
 16  
 import liquibase.sqlgenerator.SqlGeneratorFactory;
 17  
 import liquibase.statement.SqlStatement;
 18  
 import liquibase.statement.core.AddAutoIncrementStatement;
 19  
 
 20  
 /**
 21  
  * SQLite does not support this ALTER TABLE operation until now. For more information see:
 22  
  * http://www.sqlite.org/omitted.html. This is a small work around...
 23  
  */
 24  56
 public class AddAutoIncrementGeneratorSQLite extends AddAutoIncrementGenerator {
 25  
 
 26  
     @Override
 27  
     public int getPriority() {
 28  1
         return PRIORITY_DATABASE;
 29  
     }
 30  
 
 31  
     @Override
 32  
     public boolean supports(AddAutoIncrementStatement statement, Database database) {
 33  46
         return database instanceof SQLiteDatabase;
 34  
     }
 35  
 
 36  
     @Override
 37  
     public Sql[] generateSql(final AddAutoIncrementStatement statement, Database database,
 38  
             SqlGeneratorChain sqlGeneratorChain) {
 39  0
         List<Sql> statements = new ArrayList<Sql>();
 40  
 
 41  
         // define alter table logic
 42  0
         SQLiteDatabase.AlterTableVisitor rename_alter_visitor = new SQLiteDatabase.AlterTableVisitor() {
 43  
             @Override
 44  
             public ColumnConfig[] getColumnsToAdd() {
 45  0
                 return new ColumnConfig[0];
 46  
             }
 47  
 
 48  
             @Override
 49  
             public boolean copyThisColumn(ColumnConfig column) {
 50  0
                 return true;
 51  
             }
 52  
 
 53  
             @Override
 54  
             public boolean createThisColumn(ColumnConfig column) {
 55  0
                 if (column.getName().equals(statement.getColumnName())) {
 56  0
                     column.setAutoIncrement(true);
 57  0
                     column.setType("INTEGER");
 58  
                 }
 59  0
                 return true;
 60  
             }
 61  
 
 62  
             @Override
 63  
             public boolean createThisIndex(Index index) {
 64  0
                 return true;
 65  
             }
 66  
         };
 67  
 
 68  
         try {
 69  
             // alter table
 70  0
             for (SqlStatement generatedStatement : SQLiteDatabase.getAlterTableStatements(rename_alter_visitor,
 71  
                     database, statement.getSchemaName(), statement.getTableName())) {
 72  0
                 statements.addAll(Arrays.asList(SqlGeneratorFactory.getInstance().generateSql(generatedStatement,
 73  
                         database)));
 74  
             }
 75  0
         } catch (DatabaseException e) {
 76  0
             e.printStackTrace();
 77  0
         } catch (UnsupportedChangeException e) {
 78  0
             throw new UnexpectedLiquibaseException(e);
 79  0
         }
 80  
 
 81  0
         return statements.toArray(new Sql[statements.size()]);
 82  
     }
 83  
 }