View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.common.impex.schema.impl.mysql;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.kuali.common.impex.ProducerUtils;
22  import org.kuali.common.impex.model.Sequence;
23  import org.kuali.common.util.CollectionUtils;
24  
25  public class MySqlSequenceSqlProducer {
26  
27      protected static final String DROP_PREFIX = "DROP TABLE IF EXISTS ";
28  
29      protected static final String CREATE_PREFIX = "CREATE TABLE ";
30      protected static final String CREATE_SUFFIX = "\n" +
31              "(\n" +
32              "\tID BIGINT(19) NOT NULL AUTO_INCREMENT, PRIMARY KEY (ID)\n" +
33              ") ENGINE MyISAM\n";
34  
35      protected static final String ALTER_PREFIX = "ALTER TABLE ";
36  
37      protected static final String ALTER_INCREMENT_START = " AUTO_INCREMENT = ";
38  
39      public List<String> getSequencesSql(List<Sequence> sequences) {
40          List<String> results = new ArrayList<String>();
41  
42          for (Sequence seq : CollectionUtils.toEmptyList(sequences)) {
43              results.add(generateDropSequenceStatment(seq));
44  
45              results.add(generateCreateSequenceStatement(seq));
46  
47              results.add(generateAlterStatement(seq));
48          }
49  
50          return results;
51      }
52  
53      protected String generateDropSequenceStatment(Sequence sequence) {
54          StringBuilder sb = new StringBuilder();
55  
56          sb.append(DROP_PREFIX);
57          sb.append(sequence.getName());
58          sb.append(ProducerUtils.NEWLINE);
59  
60          return sb.toString();
61      }
62  
63      protected String generateCreateSequenceStatement(Sequence sequence) {
64          StringBuilder sb = new StringBuilder();
65  
66          sb.append(CREATE_PREFIX);
67          sb.append(sequence.getName());
68          sb.append(CREATE_SUFFIX);
69  
70          return sb.toString();
71      }
72  
73      protected String generateAlterStatement(Sequence sequence) {
74          StringBuilder sb = new StringBuilder();
75  
76          sb.append(ALTER_PREFIX);
77          sb.append(sequence.getName());
78          sb.append(ALTER_INCREMENT_START);
79          sb.append(sequence.getStartValue());
80          sb.append(ProducerUtils.NEWLINE);
81  
82          return sb.toString();
83      }
84  
85  }