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.oracle;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.kuali.common.impex.model.Sequence;
22  import org.kuali.common.util.CollectionUtils;
23  
24  public class OracleSequenceSqlProducer {
25  
26      protected static final String DROP_PREFIX =
27              "DECLARE temp NUMBER;\n" +
28              "BEGIN\n" +
29              "\tSELECT COUNT(*) INTO temp FROM user_sequences WHERE sequence_name = '";
30      protected static final String DROP_MIDDLE = "';\n" +
31              "\tIF temp > 0 THEN EXECUTE IMMEDIATE 'DROP SEQUENCE ";
32      protected static final String DROP_SUFFIX =
33              "'; END IF;\n" +
34              "END;\n";
35  
36      protected static final String CREATE_PREFIX = "CREATE SEQUENCE ";
37      protected static final String CREATE_INCREMENT_START = " INCREMENT BY 1 START WITH ";
38      protected static final String CREATE_SUFFIX =
39              " NOMAXVALUE NOCYCLE NOCACHE ORDER\n";
40  
41      public List<String> getSequencesSql(List<Sequence> sequences) {
42          List<String> results = new ArrayList<String>();
43  
44          for (Sequence seq : CollectionUtils.toEmptyList(sequences)) {
45              results.add(generateDropSequenceStatment(seq));
46  
47              results.add(generateCreateSequenceStatement(seq));
48          }
49  
50          return results;
51      }
52  
53      protected String generateCreateSequenceStatement(Sequence sequence) {
54          StringBuilder sb = new StringBuilder();
55  
56          sb.append(CREATE_PREFIX);
57          sb.append(sequence.getName());
58          sb.append(CREATE_INCREMENT_START);
59          sb.append(sequence.getStartValue());
60          sb.append(CREATE_SUFFIX);
61  
62          return sb.toString();
63      }
64  
65      protected String generateDropSequenceStatment(Sequence sequence) {
66          StringBuilder sb = new StringBuilder();
67  
68          sb.append(DROP_PREFIX);
69          sb.append(sequence.getName());
70          sb.append(DROP_MIDDLE);
71          sb.append(sequence.getName());
72          sb.append(DROP_SUFFIX);
73  
74          return sb.toString();
75      }
76  }