1
2
3
4
5
6
7
8
9
10
11
12
13
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 }