1 package com.daffodilwoods.daffodildb.server.sql99.dml; 2 3 import com.daffodilwoods.database.resource.*; 4 import com.daffodilwoods.daffodildb.server.serversystem.*; 5 import java.util.*; 6 import com.daffodilwoods.daffodildb.server.sql99.common.*; 7 import com.daffodilwoods.database.general.*; 8 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 9 10 23 public class DmlStaticFunctionality implements TypeConstants { 24 53 54 public static void setTableNamesAndDatatypesOfAllColumns(_ServerSession serverSession, ColumnDetails[] columnDetails, TableDetails[] tableDetails, ArrayList listOfUnderLyingReferences, ArrayList listOfColumnsInJoin) throws com.daffodilwoods.database.resource.DException { 55 ArrayList aList = new ArrayList(3); 56 for (int indexOfColumnDetails = 0; indexOfColumnDetails < columnDetails.length; ++indexOfColumnDetails) { 57 int type = columnDetails[indexOfColumnDetails].getType(); 58 if (type == CONSTANT && !columnDetails[indexOfColumnDetails].getUnderLyingReference()) { 59 if ( ( (String []) columnDetails[indexOfColumnDetails].getColumnName()).length == 1) { 60 if ( ( (String ) columnDetails[indexOfColumnDetails].getColumn()).equals("*")) { 61 setCount(serverSession, tableDetails, columnDetails[indexOfColumnDetails]); 62 } 63 } 64 continue; 65 } 66 if (type == SCALARFUNCTION || type == GROUPING || type == CASEEXPRESSION || type == FUNCTIONAL) { 67 setTableNamesAndDatatypesOfAllColumns(serverSession, columnDetails[indexOfColumnDetails].getExistingColumnDetails(), tableDetails, listOfUnderLyingReferences, listOfColumnsInJoin); 68 continue; 69 } 70 checkForEachTable(serverSession, columnDetails[indexOfColumnDetails], tableDetails, listOfUnderLyingReferences, listOfColumnsInJoin); 71 } 72 } 73 74 private static void setCount(_ServerSession serverSession, TableDetails[] tableDetail, ColumnDetails columnDetails) throws com.daffodilwoods.database.resource.DException { 75 ArrayList aList = new ArrayList(); for (int i = 0; i < tableDetail.length; ++i) { 77 aList.addAll(Arrays.asList(tableDetail[i].getColumnPropertiesExcludingCommonColumns())); 78 } 79 columnDetails.setObject( (ColumnDetails[]) aList.toArray(new ColumnDetails[0])); 80 } 81 82 private static void checkForEachTable(_ServerSession serverSession, ColumnDetails columnDetail, TableDetails[] tableDetails, ArrayList listOfUnderLyingReferences, ArrayList listOfColumnsInJoin) throws com.daffodilwoods.database.resource.DException { 83 String [] keyArray; 84 boolean check = false; Object [] result = new Object [4]; 86 for (int indexOfTableDetails = 0; indexOfTableDetails < tableDetails.length; ++indexOfTableDetails) { 87 TableDetails tempTableDetails = tableDetails[indexOfTableDetails]; 88 String columnName[] = columnDetail.getColumnName(); 89 if (columnDetail.getType() == GROUPING) { 90 return; 91 } 92 keyArray = getDetailedColumn(columnName, tempTableDetails, columnDetail, serverSession, listOfColumnsInJoin); 93 if (keyArray == null) { 94 continue; } 96 if (columnDetail.getTableAliasArray() != null) { 97 columnDetail.setColumn(keyArray); 98 return; 99 } 100 if (keyArray[keyArray.length - 1].equals("*")) { columnDetail.setColumn(keyArray); 102 columnDetail.setTableDetails(tempTableDetails); 103 columnDetail.setObject(tempTableDetails.getColumnPropertiesExcludingCommonColumns()); 104 columnDetail.setTableForDisplay(tempTableDetails); 105 return; 106 } 107 int key = tempTableDetails.checkForColumn(keyArray); 108 if (key != -1) { 109 if (check) { throw new DException("DSE17", new Object [] {columnDetail.getColumn()}); } 112 columnDetail.setTableDetails(tempTableDetails); columnDetail.setTableForDisplay(tempTableDetails); 114 check = true; 115 result[0] = columnDetail; 116 result[1] = keyArray; 117 result[2] = new Integer (tempTableDetails.getDatatype(key)); 118 result[3] = new Integer (tempTableDetails.getSize(key)); 119 } 120 } 121 if (!check) { listOfUnderLyingReferences.add(columnDetail); return; 124 } 125 126 127 133 134 135 ( (ColumnDetails) result[0]).setColumn( (String []) result[1]); 136 ( (ColumnDetails) result[0]).setDatatype(result[2].hashCode()); 137 ( (ColumnDetails) result[0]).setSize(result[3].hashCode()); 138 } 139 140 141 private static String [] getDetailedColumn(String columnName[], TableDetails tableDetails, ColumnDetails columnDetails, _ServerSession serverSession, ArrayList listOfColumnsInJoin) throws DException { 142 String [] appropriateTableName = tableDetails.getAppropriateTableName(); 143 String [] originalTableName = tableDetails.getTableName(); if (check(appropriateTableName, columnName, tableDetails, columnDetails, serverSession, listOfColumnsInJoin)) { 145 return append(originalTableName, columnName); 146 } 147 return null; 148 } 149 150 private static boolean check(String [] tableName, String [] columnName, TableDetails tableDetails, ColumnDetails columnDetail, _ServerSession serverSession, ArrayList listOfColumnsInJoin) throws DException { 151 for (int i = columnName.length - 2, j = tableName.length - 1, length = columnName.length; i >= 0 && j >= 0; i--, j--) { 152 if (!tableName[j].equalsIgnoreCase(columnName[i])) { 153 if (length - 2 != 0 && i == length - 2 && j == tableName.length - 1) { 154 String aliasName = tableDetails.getAliasName(); 155 if (aliasName != null && aliasName.equalsIgnoreCase(columnName[0])) { 156 checkForeignKeyCC(tableDetails, columnDetail, serverSession, columnName, listOfColumnsInJoin); 157 return true; 158 } 159 } 160 return false; 161 } 162 } 163 return true; 164 } 165 166 private static void checkForeignKeyCC(TableDetails tableDetails, ColumnDetails columnDetail, _ServerSession serverSession, String [] columnName, ArrayList listOfColumnsInJoin) throws DException { 167 int length = columnName.length; 168 QualifiedIdentifier identifier = tableDetails.getQualifiedIdentifier(); 169 String [] tableAliasArray = new String [length - 2]; 170 _ColumnCharacteristics columnCharacteristics = serverSession.getColumnCharacteristics(identifier); 171 ArrayList listOfFKeyTables = new ArrayList(); 172 TableDetails tempTable = tableDetails; 173 for (int i = 1, j = 0; i < length - 1; i++, j++) { 174 if (!ifExistsColumnInCC(columnName[i], columnCharacteristics.getColumnNames())) { 175 throw new DException("DSE255", new Object [] {columnName[i], identifier.getIdentifier()}); } 177 Object [] object = serverSession.getForeignConstraintCharacteritics(identifier, columnName[i]); 178 179 identifier = (QualifiedIdentifier) object[0]; 180 columnCharacteristics = (_ColumnCharacteristics) object[1]; 181 182 TableDetails table = new TableDetails(); 183 table.setTableName(identifier.getTableName()); 184 table.cc = columnCharacteristics; 185 listOfFKeyTables.add(table); 186 187 tempTable = table; 188 189 String [] referencedColumns = (String []) object[2]; 190 tableAliasArray[j] = columnName[i]; 191 } 192 columnDetail.setTableAliasArray(tableAliasArray); 193 tableDetails.setUnderLyingFKeyTables( (TableDetails[]) listOfFKeyTables.toArray(new TableDetails[listOfFKeyTables.size()])); 194 if (columnName[length - 1].equalsIgnoreCase("*")) { 195 initializeAllColumnsOfForeign(columnDetail, columnCharacteristics, tableDetails, tableAliasArray); 196 } else { 197 if (!ifExistsColumnInCC(columnName[length - 1], columnCharacteristics.getColumnNames())) { 198 throw new DException("DSE255", new Object [] {columnName[length - 1], identifier.getIdentifier()}); } 200 columnDetail.setDatatype(columnCharacteristics.getColumnType(columnCharacteristics.getColumnIndex(columnName[length - 1]))); 201 columnDetail.setSize(columnCharacteristics.getSize(columnCharacteristics.getColumnIndex(columnName[length - 1]))); 202 columnDetail.setTableDetails(tableDetails); 203 columnDetail.setQualifiedIdentifier(new QualifiedIdentifier(columnCharacteristics.getCatalogName(0), columnCharacteristics.getSchemaName(0), columnCharacteristics.getTableName(0))); 204 columnDetail.setChainedColumnCC(columnCharacteristics); 205 } 206 } 207 208 private static void initializeAllColumnsOfForeign(ColumnDetails columnDetail, _ColumnCharacteristics columnCharacteristics, TableDetails table, String [] tableAliasArray) throws DException { 209 String [] ccColumnNames = columnCharacteristics.getColumnNames(); 210 int length = ccColumnNames.length; 211 QualifiedIdentifier identifier = new QualifiedIdentifier(columnCharacteristics.getCatalogName(0), columnCharacteristics.getSchemaName(0), columnCharacteristics.getTableName(0)); 212 ColumnDetails[] newColumns = new ColumnDetails[length - 5]; 213 for (int i = 5, j = 0; i < length; i++, j++) { 214 ColumnDetails columnDetails = new ColumnDetails(); 215 String [] tableName = table.getTableName(); 216 String [] column = new String [4]; 217 System.arraycopy(tableName, 0, column, 0, tableName.length); 218 column[3] = ccColumnNames[i]; 219 columnDetails.setColumn(column); 220 columnDetails.setChainedColumnCC(columnCharacteristics); 221 columnDetails.setDatatype(columnCharacteristics.getColumnType(i)); 222 columnDetails.setSize(columnCharacteristics.getSize(i)); 223 columnDetails.setTableDetails(table); 224 columnDetails.setTableForDisplay(table); 225 columnDetails.setType(REFERENCE); 226 columnDetails.setTableAliasArray(tableAliasArray); 227 columnDetails.setQualifiedIdentifier(identifier); 228 newColumns[j] = columnDetails; 229 } 230 columnDetail.setObject(newColumns); 231 columnDetail.setTableDetails(table); 232 } 233 234 private static boolean ifExistsColumnInCC(String columnName, String [] columns) { 235 int length = columns.length; 236 for (int i = 0; i < length; i++) { 237 if (columns[i].equalsIgnoreCase(columnName)) { 238 return true; 239 } 240 } 241 return false; 242 } 243 244 private static String [] append(String [] tableName, String [] columnName) throws DException { 245 String [] result = new String [tableName.length + 1]; 246 System.arraycopy(tableName, 0, result, 0, tableName.length); 247 result[result.length - 1] = columnName[columnName.length - 1]; 248 return result; 249 } 250 251 257 public static void checkColumnUniqueness(ColumnDetails[] cd) throws DException{ 258 HashMap h = new HashMap(); 259 for (int i = 0; i < cd.length; i++) { 260 Object obj = h.put(cd[i].getColumn().toLowerCase(), ""); 261 if (obj != null) { 262 throw new DException("DSE5548", new Object [] {cd[i].getQualifiedColumnName()}); 263 } 264 265 } 266 } 267 } 268 | Popular Tags |