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 tools.gsf.facade.sql.table;
18  
19  import COM.FutureTense.Interfaces.FTValList;
20  import COM.FutureTense.Interfaces.ICS;
21  import tools.gsf.runtime.CSRuntimeException;
22  
23  /**
24   * Facade over table create and delete CatalogManager operations
25   *
26   * @author Dolf Dijkstra
27   */
28  
29  public class TableCreator {
30  
31      private final ICS ics;
32  
33      public TableCreator(final ICS ics) {
34          super();
35          this.ics = ics;
36      }
37  
38      /**
39       * Delete a table
40       *
41       * @param name the name of the table to delete
42       */
43      public void delteTable(String name) {
44          ics.ClearErrno();
45          final FTValList list = new FTValList();
46  
47          list.setValString("ftcmd", "deletetable");
48          list.setValString("tablename", name);
49          if (!ics.CatalogManager(list)) {
50              throw new CSRuntimeException("Error deleting table " + name, ics.GetErrno());
51          }
52  
53      }
54  
55      /**
56       * Create a table
57       *
58       * @param table the table to create as defined by its TableDef
59       */
60      public void createTable(TableDef table) {
61          ics.ClearErrno();
62          final FTValList list = new FTValList();
63  
64          list.setValString("ftcmd", "createtable");
65          list.setValString("tablename", table.getName());
66          list.setValString("systable", table.getType());
67          list.setValString("aclList", table.getAcl());
68          int i = 0;
69          for (TableColumn col : table.getColumns()) {
70  
71              list.setValString("colname" + i, col.getName());
72              StringBuilder val = new StringBuilder();
73              val.append(ics.GetProperty(col.getType().getProperty()));
74              switch (col.getType()) {
75                  case ccdouble:
76                  case ccchar:
77                  case ccnumeric:
78                  case ccvarchar:
79                  case ccdatetime:
80                      if (col.getLength() > 0) {
81                          val.append(" (");
82                          val.append(Integer.toString(col.getLength()));
83                          switch (col.getType()) {
84                              case ccdouble:
85                                  val.append(",").append(Integer.toString(col.getDecimal()));
86                                  break;
87                              default:
88                                  break;
89                          }
90  
91                          val.append(")");
92                      }
93                      break;
94                  default:
95                      break;
96  
97              }
98              if (col.isPrimary()) {
99                  val.append(" ");
100                 val.append(ics.GetProperty("cc.primary"));
101             } else if (col.isNullable()) {
102                 val.append(" ");
103                 val.append(ics.GetProperty("cc.null"));
104             } else {
105                 val.append(" NOT NULL");
106             }
107 
108             list.setValString("colvalue" + i, val.toString());
109             i++;
110         }
111 
112         if (!ics.CatalogManager(list)) {
113             throw new CSRuntimeException("Error creating table " + table.getName(), ics.GetErrno());
114         }
115 
116     }
117 
118 }