View Javadoc
1   package edu.internet2.middleware.grouper.ext.org.apache.ddlutils.platform.mckoi;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  
27  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.Platform;
28  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.alteration.AddColumnChange;
29  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.alteration.ColumnAutoIncrementChange;
30  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.alteration.RemoveColumnChange;
31  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.alteration.TableChange;
32  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.model.Column;
33  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.model.Database;
34  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.model.Table;
35  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.platform.SqlBuilder;
36  
37  /**
38   * The SQL Builder for the Mckoi database.
39   * 
40   * @version $Revision: 463757 $
41   */
42  public class MckoiBuilder extends SqlBuilder
43  {
44      /**
45       * Creates a new builder instance.
46       * 
47       * @param platform The plaftform this builder belongs to
48       */
49      public MckoiBuilder(Platform platform)
50      {
51          super(platform);
52          // we need to handle the backslash first otherwise the other
53          // already escaped sequence would be affected
54          addEscapedCharSequence("\\", "\\\\");
55          addEscapedCharSequence("'",  "\\'");
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public void createTable(Database database, Table table, Map parameters) throws IOException
62      {
63          // we use sequences instead of the UNIQUEKEY function because this way
64          // we can read their values back
65          Column[] columns = table.getAutoIncrementColumns();
66  
67          for (int idx = 0; idx < columns.length; idx++)
68          {
69              createAutoIncrementSequence(table, columns[idx]);
70          }
71  
72          super.createTable(database, table, parameters);
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      public void dropTable(Table table) throws IOException
79      { 
80          print("DROP TABLE IF EXISTS ");
81          printIdentifier(getTableName(table));
82          printEndOfStatement();
83  
84          Column[] columns = table.getAutoIncrementColumns();
85  
86          for (int idx = 0; idx < columns.length; idx++)
87          {
88              dropAutoIncrementSequence(table, columns[idx]);
89          }
90      }
91  
92      /**
93       * Creates the sequence necessary for the auto-increment of the given column.
94       * 
95       * @param table  The table
96       * @param column The column
97       */
98      protected void createAutoIncrementSequence(Table  table,
99                                                 Column column) throws IOException
100     {
101         print("CREATE SEQUENCE ");
102         printIdentifier(getConstraintName("seq",
103                                           table,
104                                           column.getName(),
105                                           null));
106         printEndOfStatement();
107     }
108 
109     /**
110      * Drops the sequence used for the auto-increment of the given column.
111      * 
112      * @param table  The table
113      * @param column The column
114      */
115     protected void dropAutoIncrementSequence(Table  table,
116                                              Column column) throws IOException
117     {
118         print("DROP SEQUENCE ");
119         printIdentifier(getConstraintName("seq",
120                                           table,
121                                           column.getName(),
122                                           null));
123         printEndOfStatement();
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     protected void writeColumnDefaultValue(Table table, Column column) throws IOException
130     {
131         if (column.isAutoIncrement())
132         {
133             // we start at value 1 to avoid issues with jdbc
134             print("NEXTVAL('");
135             print(getConstraintName("seq", table, column.getName(), null));
136             print("')");
137         }
138         else
139         {
140             super.writeColumnDefaultValue(table, column);
141         }
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     public String getSelectLastIdentityValues(Table table)
148     {
149         Column[] columns = table.getAutoIncrementColumns();
150 
151         if (columns.length > 0)
152         {
153             StringBuffer result = new StringBuffer();
154 
155             result.append("SELECT ");
156             for (int idx = 0; idx < columns.length; idx++)
157             {
158                 if (idx > 0)
159                 {
160                     result.append(",");
161                 }
162                 result.append("CURRVAL('");
163                 result.append(getConstraintName("seq", table, columns[idx].getName(), null));
164                 result.append("')");
165             }
166             return result.toString();
167         }
168         else
169         {
170             return null;
171         }
172     }
173 
174     /**
175      * {@inheritDoc}
176      */
177     protected void processTableStructureChanges(Database currentModel,
178                                                 Database desiredModel,
179                                                 Table    sourceTable,
180                                                 Table    targetTable,
181                                                 Map      parameters,
182                                                 List     changes) throws IOException
183     {
184         // McKoi has this nice ALTER CREATE TABLE statement which saves us a lot of work
185         // We only have to handle auto-increment changes manually
186         for (Iterator it = changes.iterator(); it.hasNext();)
187         {
188             TableChange/../../../../../../../../edu/internet2/middleware/grouper/ext/org/apache/ddlutils/alteration/TableChange.html#TableChange">TableChange change = (TableChange)it.next();
189 
190             if (change instanceof ColumnAutoIncrementChange)
191             {
192                 Column column = ((ColumnAutoIncrementChange)change).getColumn();
193 
194                 // we have to defer removal of the sequences until they are no longer used
195                 if (!column.isAutoIncrement())
196                 {
197                     ColumnAutoIncrementChange../edu/internet2/middleware/grouper/ext/org/apache/ddlutils/alteration/ColumnAutoIncrementChange.html#ColumnAutoIncrementChange">ColumnAutoIncrementChange autoIncrChange = (ColumnAutoIncrementChange)change;
198 
199                     createAutoIncrementSequence(autoIncrChange.getChangedTable(),
200                                                 autoIncrChange.getColumn());
201                 }
202             }
203             else if (change instanceof AddColumnChange)
204             {
205                 AddColumnChange../../../../edu/internet2/middleware/grouper/ext/org/apache/ddlutils/alteration/AddColumnChange.html#AddColumnChange">AddColumnChange addColumnChange = (AddColumnChange)change;
206 
207                 if (addColumnChange.getNewColumn().isAutoIncrement())
208                 {
209                     createAutoIncrementSequence(addColumnChange.getChangedTable(),
210                                                 addColumnChange.getNewColumn());
211                 }
212             }
213         }
214 
215         print("ALTER ");
216         super.createTable(desiredModel, targetTable, parameters);
217 
218         for (Iterator it = changes.iterator(); it.hasNext();)
219         {
220             TableChange/../../../../../../../../edu/internet2/middleware/grouper/ext/org/apache/ddlutils/alteration/TableChange.html#TableChange">TableChange change = (TableChange)it.next();
221     
222             if (change instanceof ColumnAutoIncrementChange)
223             {
224                 Column column = ((ColumnAutoIncrementChange)change).getColumn();
225     
226                 if (column.isAutoIncrement())
227                 {
228                     ColumnAutoIncrementChange../edu/internet2/middleware/grouper/ext/org/apache/ddlutils/alteration/ColumnAutoIncrementChange.html#ColumnAutoIncrementChange">ColumnAutoIncrementChange autoIncrChange = (ColumnAutoIncrementChange)change;
229         
230                     dropAutoIncrementSequence(autoIncrChange.getChangedTable(),
231                                               autoIncrChange.getColumn());
232                 }
233             }
234             else if (change instanceof RemoveColumnChange)
235             {
236                 RemoveColumnChange../../edu/internet2/middleware/grouper/ext/org/apache/ddlutils/alteration/RemoveColumnChange.html#RemoveColumnChange">RemoveColumnChange removeColumnChange = (RemoveColumnChange)change;
237 
238                 if (removeColumnChange.getColumn().isAutoIncrement())
239                 {
240                     dropAutoIncrementSequence(removeColumnChange.getChangedTable(),
241                                               removeColumnChange.getColumn());
242                 }
243             }
244         }
245         changes.clear();
246     }
247 }
248