Skip to main content
summaryrefslogtreecommitdiffstats
blob: e06d9b33449ce31e1e88a1671238db48b54c0547 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.database.schema.internal.sql;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.osee.database.schema.internal.data.AppliesToClause;
import org.eclipse.osee.database.schema.internal.data.ColumnMetadata;
import org.eclipse.osee.database.schema.internal.data.ConstraintElement;
import org.eclipse.osee.database.schema.internal.data.ForeignKey;
import org.eclipse.osee.database.schema.internal.data.IndexElement;
import org.eclipse.osee.database.schema.internal.data.ReferenceClause;
import org.eclipse.osee.database.schema.internal.data.ReferenceClause.OnDeleteEnum;
import org.eclipse.osee.database.schema.internal.data.ReferenceClause.OnUpdateEnum;
import org.eclipse.osee.database.schema.internal.data.TableElement;
import org.eclipse.osee.database.schema.internal.data.TableElement.ColumnFields;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.logger.Log;

/**
 * @author Roberto E. Escobar
 */
public class HyperSqlManager extends SqlManagerImpl {

   public HyperSqlManager(Log logger, SqlDataType sqlDataType) {
      super(logger, sqlDataType);
   }

   private String handleColumnCreationSection(Map<String, ColumnMetadata> columns) {
      List<String> lines = new ArrayList<String>();
      for (String key : columns.keySet()) {
         Map<ColumnFields, String> column = columns.get(key).getColumnFields();
         lines.add(columnDataToSQL(column));
      }
      return org.eclipse.osee.framework.jdk.core.util.Collections.toString(",\n", lines);
   }

   @Override
   protected String handleConstraintCreationSection(List<? extends ConstraintElement> constraints, String tableId) {
      List<String> constraintStatements = new ArrayList<String>();
      for (ConstraintElement constraint : constraints) {
         constraintStatements.add(constraintDataToSQL(constraint, tableId));
      }
      StringBuilder toExecute = new StringBuilder();
      if (constraintStatements.isEmpty()) {
         toExecute.append("");
      } else {
         toExecute.append(",\n");
      }
      toExecute.append(join(constraintStatements, ",\n"));
      return toExecute.toString();
   }

   @Override
   public String constraintDataToSQL(ConstraintElement constraint, String tableID) {
      StringBuilder toReturn = new StringBuilder();
      String id = formatQuotedString(constraint.getId(), "\\.");
      String type = constraint.getConstraintType().toString();
      String appliesTo = formatQuotedString(constraint.getCommaSeparatedColumnsList(), ",");

      if (Strings.isValid(id) && Strings.isValid(appliesTo)) {
         toReturn.append("CONSTRAINT ").append(id).append(" ").append(type).append(" (").append(appliesTo).append(")");

         if (constraint instanceof ForeignKey) {
            ForeignKey fk = (ForeignKey) constraint;
            List<ReferenceClause> refs = fk.getReferences();

            for (ReferenceClause ref : refs) {
               String refTable = formatQuotedString(ref.getFullyQualifiedTableName(), "\\.");
               String refColumns = formatQuotedString(ref.getCommaSeparatedColumnsList(), ",");

               String onUpdate = "";
               if (!ref.getOnUpdateAction().equals(OnUpdateEnum.UNSPECIFIED)) {
                  onUpdate = "ON UPDATE " + ref.getOnUpdateAction().toString();
               }

               String onDelete = "";
               if (!ref.getOnDeleteAction().equals(OnDeleteEnum.UNSPECIFIED)) {
                  onDelete = "ON DELETE " + ref.getOnDeleteAction().toString();
               }

               if (refTable != null && refColumns != null && !refTable.equals("") && !refColumns.equals("")) {
                  toReturn.append(" REFERENCES ").append(refTable).append(" (").append(refColumns).append(")");
                  if (!onUpdate.equals("")) {
                     toReturn.append(" ").append(onUpdate);
                  }

                  if (!onDelete.equals("")) {
                     toReturn.append(" ").append(onDelete);
                  }

               }

               else {
                  getLogger().warn("Skipping CONSTRAINT at Table: %s\n\t %s", tableID, fk);
               }
            }
         }
      } else {
         getLogger().warn("Skipping CONSTRAINT at Table: %s\n\t %s", tableID, constraint);
      }
      return toReturn.toString();
   }

   @Override
   public void createIndex(TableElement tableDef) throws OseeCoreException {
      List<IndexElement> tableIndices = tableDef.getIndexData();
      String indexId = null;
      StringBuilder appliesTo = new StringBuilder();
      String tableName = formatQuotedString(tableDef.getFullyQualifiedTableName(), "\\.");
      for (IndexElement iData : tableIndices) {
         if (iData.ignoreMySql()) {
            continue;
         }
         indexId = iData.getId();
         appliesTo.delete(0, appliesTo.length());

         List<AppliesToClause> appliesToList = iData.getAppliesToList();
         for (int index = 0; index < appliesToList.size(); index++) {
            AppliesToClause record = appliesToList.get(index);
            appliesTo.append(record.getColumnName());

            if (index + 1 < appliesToList.size()) {
               appliesTo.append(", ");
            }
         }
         String toExecute = String.format("%s INDEX %s ON %s (%s)", CREATE_STRING, indexId, tableName, appliesTo);
         toExecute = createIndexPostProcess(iData, toExecute);
         getLogger().debug(toExecute);
         ConnectionHandler.runPreparedUpdate(toExecute);
      }
   }

   @Override
   public void createTable(TableElement tableDef) throws OseeCoreException {
      StringBuilder builder = new StringBuilder();
      builder.append("CREATE TABLE ").append(tableDef.getName()).append(" ( \n");
      builder.append(handleColumnCreationSection(tableDef.getColumns()));
      builder.append(handleConstraintCreationSection(tableDef.getConstraints(), tableDef.getFullyQualifiedTableName()));
      builder.append(handleConstraintCreationSection(tableDef.getForeignKeyConstraints(),
         tableDef.getFullyQualifiedTableName()));
      builder.append(" \n)\n");
      getLogger().debug("Creating Table: [%s]", tableDef.getFullyQualifiedTableName());
      ConnectionHandler.runPreparedUpdate(builder.toString());
   }

   @Override
   public void dropIndex(TableElement tableDef) {
      // Do Nothing -- Indexes are dropped during table drop
   }

}

Back to the top