View Javadoc

1   // Copyright 2011 Leo Przybylski. All rights reserved.
2   //
3   // Redistribution and use in source and binary forms, with or without modification, are
4   // permitted provided that the following conditions are met:
5   //
6   //    1. Redistributions of source code must retain the above copyright notice, this list of
7   //       conditions and the following disclaimer.
8   //
9   //    2. Redistributions in binary form must reproduce the above copyright notice, this list
10  //       of conditions and the following disclaimer in the documentation and/or other materials
11  //       provided with the distribution.
12  //
13  // THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
14  // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15  // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
16  // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17  // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19  // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20  // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
21  // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  //
23  // The views and conclusions contained in the software and documentation are those of the
24  // authors and should not be interpreted as representing official policies, either expressed
25  // or implied, of Leo Przybylski.
26  package liquibase.sqlgenerator.ext;
27  
28  import liquibase.database.Database;
29  import liquibase.exception.ValidationErrors;
30  import liquibase.sql.Sql;
31  import liquibase.sql.UnparsedSql;
32  import liquibase.sqlgenerator.SqlGenerator;
33  import liquibase.sqlgenerator.SqlGeneratorChain;
34  import liquibase.sqlgenerator.core.AbstractSqlGenerator;
35  import liquibase.statement.core.CreateSequenceStatement;
36  
37  import java.util.List;
38  import java.util.ArrayList;
39  import java.util.Arrays;
40  
41  import static liquibase.ext.Constants.EXTENSION_PRIORITY;
42  
43  /**
44   * Sequence hack for MySQL
45   *
46   * @author Leo Przybylski (leo [at] rsmart.com)
47   */
48  public class MysqlSequenceGenerator extends AbstractSqlGenerator<CreateSequenceStatement> {
49      private static final String CREATE_SEQUENCE_STATEMENT = "CREATE TABLE IF NOT EXISTS %s (id bigint(19) NOT NULL auto_increment, PRIMARY KEY(id) )";
50      private static final String SET_START_VALUE_STATEMENT = "INSERT INTO %s VALUES (%s)";
51  
52      @Override
53      public int getPriority() {
54          return EXTENSION_PRIORITY;
55      }
56      
57      @Override
58      public boolean supports(final CreateSequenceStatement statement, final Database database) {
59          return ("mysql".equals(database.getTypeName()));
60      }
61  
62      @Override
63      public ValidationErrors validate(CreateSequenceStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
64          return new ValidationErrors();
65      }
66  
67      @Override
68      public Sql[] generateSql(CreateSequenceStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
69          List<Sql> list = new ArrayList<Sql>();
70          list.add(new UnparsedSql(String.format(CREATE_SEQUENCE_STATEMENT, statement.getSequenceName())));
71          /* This is already taken care of during data migration */
72          if (statement.getStartValue() != null) {
73              // System.out.println("Got start value " + statement.getStartValue());
74              list.add(new UnparsedSql(String.format(SET_START_VALUE_STATEMENT, statement.getSequenceName(), statement.getStartValue())));
75          }
76          
77          list.addAll(Arrays.asList(sqlGeneratorChain.generateSql(statement, database)));
78  
79          return list.toArray(new Sql[list.size()]);
80  
81      }
82  }