View Javadoc

1   package liquibase.sqlgenerator.core;
2   
3   import liquibase.change.ColumnConfig;
4   import liquibase.database.Database;
5   import liquibase.database.core.SQLiteDatabase;
6   import liquibase.exception.ValidationErrors;
7   import liquibase.sql.Sql;
8   import liquibase.sql.UnparsedSql;
9   import liquibase.sqlgenerator.SqlGenerator;
10  import liquibase.sqlgenerator.SqlGeneratorChain;
11  import liquibase.statement.core.CopyRowsStatement;
12  
13  public class CopyRowsGenerator extends AbstractSqlGenerator<CopyRowsStatement> {
14  
15      @Override
16      public boolean supports(CopyRowsStatement statement, Database database) {
17          return (database instanceof SQLiteDatabase);
18      }
19  
20      public ValidationErrors validate(CopyRowsStatement copyRowsStatement, Database database,
21              SqlGeneratorChain sqlGeneratorChain) {
22          ValidationErrors validationErrors = new ValidationErrors();
23          validationErrors.checkRequiredField("targetTable", copyRowsStatement.getTargetTable());
24          validationErrors.checkRequiredField("sourceTable", copyRowsStatement.getSourceTable());
25          validationErrors.checkRequiredField("copyColumns", copyRowsStatement.getCopyColumns());
26          return validationErrors;
27      }
28  
29      public Sql[] generateSql(CopyRowsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
30          StringBuffer sql = new StringBuffer();
31          if (database instanceof SQLiteDatabase) {
32              sql.append("INSERT INTO `").append(statement.getTargetTable()).append("` SELECT ");
33              for (int i = 0; i < statement.getCopyColumns().size(); i++) {
34                  ColumnConfig column = statement.getCopyColumns().get(i);
35                  if (i > 0) {
36                      sql.append(",");
37                  }
38                  sql.append("`").append(column.getName()).append("`");
39              }
40              sql.append(" FROM `").append(statement.getSourceTable()).append("`");
41          }
42  
43          return new Sql[] { new UnparsedSql(sql.toString()) };
44      }
45  }