View Javadoc

1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.fatwire.gst.foundation.facade.sql.table;
18  
19  import COM.FutureTense.Interfaces.FTValList;
20  import COM.FutureTense.Interfaces.ICS;
21  
22  import com.fatwire.gst.foundation.CSRuntimeException;
23  
24  /**
25   * Facade over table create and delete CatalogManager operations
26   * 
27   * @author Dolf Dijkstra
28   * 
29   */
30  
31  public class TableCreator {
32  
33      private final ICS ics;
34  
35      public TableCreator(final ICS ics) {
36          super();
37          this.ics = ics;
38      }
39  
40      /**
41       * 
42       * Delete a table
43       * 
44       * @param name the name of the table to delete
45       */
46      public void delteTable(String name) {
47          ics.ClearErrno();
48          final FTValList list = new FTValList();
49  
50          list.setValString("ftcmd", "deletetable");
51          list.setValString("tablename", name);
52          if (!ics.CatalogManager(list)) {
53              throw new CSRuntimeException("Error deleting table " + name, ics.GetErrno());
54          }
55  
56      }
57  
58      /**
59       * Create a table
60       * 
61       * @param table the table to create as defined by its TableDef
62       */
63      public void createTable(TableDef table) {
64          ics.ClearErrno();
65          final FTValList list = new FTValList();
66  
67          list.setValString("ftcmd", "createtable");
68          list.setValString("tablename", table.getName());
69          list.setValString("systable", table.getType());
70          list.setValString("aclList", table.getAcl());
71          int i = 0;
72          for (TableColumn col : table.getColumns()) {
73  
74              list.setValString("colname" + i, col.getName());
75              StringBuilder val = new StringBuilder();
76              val.append(ics.GetProperty(col.getType().getProperty()));
77              switch (col.getType()) {
78                  case ccdouble:
79                  case ccchar:
80                  case ccnumeric:
81                  case ccvarchar:
82                  case ccdatetime:
83                      if (col.getLength() > 0) {
84                          val.append(" (");
85                          val.append(Integer.toString(col.getLength()));
86                          switch (col.getType()) {
87                              case ccdouble:
88                                  val.append(",").append(Integer.toString(col.getDecimal()));
89                                  break;
90                              default:
91                                  break;
92                          }
93  
94                          val.append(")");
95                      }
96                      break;
97                  default:
98                      break;
99  
100             }
101             if (col.isPrimary()) {
102                 val.append(" ");
103                 val.append(ics.GetProperty("cc.primary"));
104             } else if (col.isNullable()) {
105                 val.append(" ");
106                 val.append(ics.GetProperty("cc.null"));
107             } else {
108                 val.append(" NOT NULL");
109             }
110 
111             list.setValString("colvalue" + i, val.toString());
112             i++;
113         }
114 
115         if (!ics.CatalogManager(list)) {
116             throw new CSRuntimeException("Error creating table " + table.getName(), ics.GetErrno());
117         }
118 
119     }
120 
121 }