| 1 package com.daffodilwoods.daffodildb.server.sql99.common; 2 3 import java.util.*; 4 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 5 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 6 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 7 import com.daffodilwoods.daffodildb.server.sql99.expression.datetimevalueexpression.*; 8 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 9 import com.daffodilwoods.database.general.*; 10 import com.daffodilwoods.database.resource.*; 11 import com.daffodilwoods.database.utility.P; 12 13 public class TableDetails implements TypeConstants,java.io.Serializable { 14 15 private TableDetails[] underLyingFKeyTables; private int tableType = TypeConstants.TABLE; private QualifiedIdentifier qualifiedIdentifier; private ArrayList tableName; private String aliasName; private TreeMap columnNames; private Object [] originalMapping; public _ColumnCharacteristics cc; private String [] ccColumnNames; private long rowCount = -1; private String appropriateTableName; private boolean hasRecord; private datetimevalueexpression datetimeValueExpression; private booleanvalueexpression bve; private _TableInfo tableInfo; private ArrayList commonColumns; private boolean isFromSubQuery; 32 33 public TableDetails() throws DException { 34 tableName = new ArrayList(); 35 commonColumns = new ArrayList(); 36 rowCount = -1; 37 } 38 39 45 public void setTableName(String tableName[]) throws DException { 46 if ( !this.tableName.isEmpty()) 47 this.tableName.clear(); 48 this.tableName.addAll(Arrays.asList(tableName)); 49 } 50 51 56 public String [] getColumnsExcludingCommonColumns() throws DException { 57 if ( cc.getTableType() == TypeConstants.VIEW ) 58 return getColumnsExcludingCommonColumnsForView(); 59 if(ccColumnNames == null) 60 ccColumnNames = cc.getColumnNames(); 61 int length = ccColumnNames.length; 62 int commonLength=commonColumns.size(); 63 String [] allColumns = new String [length - commonLength-5]; 64 for(int i = 5,j = 0; i < length; i++){ 65 if(commonColumns.contains(ccColumnNames[i])) 66 continue; 67 allColumns[j++] = ccColumnNames[i]; 68 } 69 return allColumns; 70 } 71 72 private String [] getColumnsExcludingCommonColumnsForView() throws DException { 73 if(ccColumnNames == null) 74 ccColumnNames = cc.getColumnNames(); 75 int length = ccColumnNames.length; 76 int lenghtCommon = commonColumns.size(); 77 String [] allColumns = new String [length-lenghtCommon]; 78 for(int i = 0, j = 0 ; i < length; i++){ 79 if( commonColumns.contains(ccColumnNames[i])) 80 continue; 81 allColumns[j++] = ccColumnNames[i]; 82 } 83 return allColumns; 84 85 } 86 87 88 public void addCommonColumn(String commonColumnName) throws DException { 89 if(!commonColumns.contains(commonColumnName)) 90 commonColumns.add(commonColumnName); 91 } 92 93 94 99 public String [] getTableName() throws DException{ 100 if ( tableName.size() == 0 ) 101 throw new DException("DSE1137",null); 102 return (String [])tableName.toArray(new String [tableName.size()]); 103 } 104 105 110 public String getQualifiedTableName() throws DException { 111 String []nameOfTable = getAppropriateTableName(); 112 StringBuffer buffer = new StringBuffer (); 113 int index; 114 for(index = 0; index < nameOfTable.length - 1 ; ++index){ 115 buffer.append(nameOfTable[index]); 116 buffer.append("."); 117 } 118 buffer.append(nameOfTable[index]); 119 return buffer.toString(); 120 } 121 122 128 public String getActualName() throws DException{ if ( tableName.size() == 0 ) 130 throw new DException("DSE970",null); 131 if(aliasName != null) 132 return aliasName; 133 return (String )tableName.get(tableName.size() - 1); 134 } 135 136 public String getName() throws DException{ if ( tableName.size() == 0 ) 138 throw new DException("DSE970",null); 139 return (String )tableName.get(tableName.size() - 1); 140 } 141 142 143 144 public String [] getAppropriateTableName()throws DException{ String [] names = getTableName(); 146 if(aliasName != null) 147 names[names.length - 1] = aliasName; 148 return names; 149 } 150 151 public void setAliasName(String aliasName) throws DException { 152 this.aliasName = aliasName; 153 } 154 155 public String getAliasName() throws DException { 156 return aliasName; 157 } 158 159 public String getCatalogName() throws DException { 160 return (String )tableName.get(0); 161 } 162 163 public String getSchemaName() throws DException { 164 return (String )tableName.get(1); 165 } 166 167 public ColumnDetails[] getColumnPropertiesExcludingCommonColumns() throws DException { 168 if ( cc.getTableType() == TypeConstants.VIEW ) 169 return getDetailsOfAllColumnsInView(); 170 if(ccColumnNames == null) 171 ccColumnNames = cc.getColumnNames(); 172 int length = ccColumnNames.length; 173 int commonLength=commonColumns.size(); 174 ColumnDetails[] allColumns = new ColumnDetails[length - commonLength-5]; 175 for(int i = 5,j = 0; i < length; i++){ 176 if( commonColumns.contains(ccColumnNames[i])) 177 continue; 178 allColumns[j++] = getDetailsOfColumn(ccColumnNames[i],cc); 179 } 180 return allColumns; 181 } 182 183 public int getDataType(String columnName) throws DException { 184 try { 185 return cc.getColumnType(cc.getColumnIndex(columnName)); 186 } 187 catch (ArrayIndexOutOfBoundsException ex) { 188 throw ex; 189 } 190 } 191 192 public int getSize(String columnName) throws DException { 193 return cc.getSize(cc.getColumnIndex(columnName)); 194 } 195 196 private ColumnDetails getDetailsOfColumn(String columnName,_ColumnCharacteristics cc) throws DException { 197 ColumnDetails columnDetail = new ColumnDetails(); 198 String []tableName = getTableName(); 199 String []column = new String [4]; 200 System.arraycopy(tableName,0,column,0,tableName.length); 201 column[3] = columnName; 202 columnDetail.setColumnName(column); 203 columnDetail.setTableDetails(this); 204 columnDetail.setTableForDisplay(this); 205 int index = cc.getColumnIndex(columnName); 206 columnDetail.setDatatype(cc.getColumnType(index)); 207 columnDetail.setSize(cc.getSize(index)); 208 columnDetail.setType(REFERENCE); 209 columnDetail.setExpression(column[2]+"."+column[3]); 210 return columnDetail; 211 } 212 213 private ColumnDetails[] getDetailsOfAllColumnsInView() throws DException{ 214 if(ccColumnNames == null){ 215 ccColumnNames = cc.getColumnNames(); 216 } 217 int length = ccColumnNames.length; 218 int lenghtCommon = commonColumns.size(); 219 ColumnDetails[] allColumns = new ColumnDetails[length-lenghtCommon]; 220 for(int i = 0, j = 0 ; i < length; i++){ 221 if( commonColumns.contains(ccColumnNames[i])) 222 continue; 223 allColumns[j++] = getDetailsOfColumn(ccColumnNames[i],cc); 224 } 225 return allColumns; 226 } 227 228 public int getColumnCount()throws DException{ 229 return cc.getColumnCount(); 230 } 231 232 233 private void getColumns()throws DException{ 234 if(columnNames != null) 235 return; 236 if(ccColumnNames == null) 237 ccColumnNames = cc.getColumnNames(); 238 columnNames = new TreeMap(getComparator()); 239 int length = ccColumnNames.length; 240 int lenghtCommon = commonColumns.size(); 241 originalMapping = new Object [length - lenghtCommon]; 242 for(int index=0 , j = 0; index < length; ++index ){ 243 if (commonColumns.contains(ccColumnNames[index])) 244 continue; 245 String [] nameOfColumn = getAppropriateArray(ccColumnNames[index]); 246 Integer in = new Integer (j); 247 columnNames.put(nameOfColumn, in); int ii = cc.getColumnIndex(ccColumnNames[index]); 249 originalMapping[j++] = new Object [] { 250 new Integer (cc.getColumnType(ii)), new Integer (cc.getSize(ii))}; 251 } 252 } 253 254 private Comparator getComparator() throws DException { 255 return new TempComparator(); 256 } 257 258 private class TempComparator implements Comparator,java.io.Serializable { 259 TempComparator(){} 260 public int compare(Object object1,Object object2) { 261 if(object1 == null) 262 if(object2 == null) 263 return 0; 264 else 265 return -1; 266 else 267 if(object2 == null) 268 return 1; 269 else{ 270 Object [] result1 = (Object [])object1; 271 Object [] result2 = (Object [])object2; 272 int compare = 0; 273 for(int i = 0 ; i < result1.length && compare == 0 ; i++) 274 compare = String.CASE_INSENSITIVE_ORDER.compare(result1[i],result2[i]); 275 return compare; 276 } 277 } 278 } 279 280 private String [] getAppropriateArray(String column)throws DException{ 281 String []nameOfTable = getTableName(); 282 String [] result = new String [nameOfTable.length + 1]; 283 System.arraycopy(nameOfTable,0,result,0,nameOfTable.length); 284 result[result.length - 1] = column; 285 return result; 286 } 287 288 private String append()throws DException{ 289 String []nameOfTable = getAppropriateTableName(); 290 StringBuffer buffer = new StringBuffer (); 291 int index; 292 for(index = 0; index < nameOfTable.length - 1 ; ++index){ 293 buffer.append(nameOfTable[index]); 294 buffer.append("."); 295 } 296 buffer.append(nameOfTable[index]); 297 return buffer.toString(); 298 } 299 300 301 public String getUnderscoreTableName() throws DException{ 302 String []nameOfTable = getAppropriateTableName(); 303 StringBuffer buffer = new StringBuffer (); 304 int index; 305 for(index = 0; index < nameOfTable.length - 1 ; ++index){ 306 buffer.append(nameOfTable[index]); 307 buffer.append("_"); 308 } 309 buffer.append(nameOfTable[index]); 310 return buffer.toString(); 311 } 312 public int getDatatype(int key) throws DException { 313 return ((Object [])originalMapping[key])[0].hashCode(); 314 } 315 316 public int getSize(int key) throws DException { 317 return ((Object [])originalMapping[key])[1].hashCode(); 318 } 319 320 public String getNameOfTable()throws DException{ 321 if(appropriateTableName == null) 322 appropriateTableName = append(); 323 return appropriateTableName; 324 } 325 326 327 public int checkForColumn(String [] columnName)throws DException{ 328 getColumns(); 329 Object obj = columnNames.get(columnName) ; 330 return obj == null ? -1 : obj.hashCode(); 331 } 332 333 334 public String getAppropriateOnlyTableName(){ 335 return aliasName == null ? (String )tableName.get(tableName.size() - 1) : aliasName; 336 } 337 338 339 public void setRowCount(long rowCount) throws DException { 340 this.rowCount = rowCount; 341 } 342 343 public QualifiedIdentifier getQualifiedIdentifier() throws DException { 344 if ( qualifiedIdentifier == null ){ 345 String tableName[] = getTableName(); 346 if(tableName.length > 1) 347 qualifiedIdentifier = new QualifiedIdentifier(tableName[0],tableName[1],tableName[2]); 348 else 349 qualifiedIdentifier = new QualifiedIdentifier("users","users",tableName[0]); 350 } 351 return qualifiedIdentifier; 352 } 353 354 public long getRowCount() throws DException { 355 return rowCount; 356 } 357 358 public _ColumnCharacteristics getColumnCharacteristics() throws DException{ 359 return cc; 360 } 361 362 public void setHasRecord(boolean hasRecord0) throws DException{ 363 hasRecord = hasRecord0; 364 } 365 366 public boolean getHasRecord() throws DException{ 367 return hasRecord; 368 } 369 370 public void setTableType(int tableType0){ 371 tableType = tableType0; 372 } 373 374 public int getTableType() { 375 return tableType; 376 } 377 378 public void setUnderLyingFKeyTables(TableDetails[] tables0) throws DException { 379 underLyingFKeyTables = tables0; 380 } 381 382 public TableDetails[] getUnderLyingFKeyTables() throws DException { 383 return underLyingFKeyTables; 384 } 385 386 public boolean isDated() { 387 return (datetimeValueExpression != null || bve != null); 388 } 389 public java.sql.Date getDate(_VariableValues variableValue ) throws DException{ 390 return (java.sql.Date )datetimeValueExpression.run(variableValue); 391 } 392 public void setDatetimeValueExpression(datetimevalueexpression datetimeValueExpression0) { 393 datetimeValueExpression = datetimeValueExpression0; 394 } 395 396 public void setDatedBooleanValueExpression(booleanvalueexpression bve) { 397 this.bve = bve; 398 } 399 400 public booleanvalueexpression getDatedBooleanValueExpression() { 401 return bve; 402 } 403 404 public _TableInfo getTableInfo() { 405 return tableInfo; 406 } 407 408 public void setTableInfo(_TableInfo t) { 409 tableInfo = t; 410 } 411 412 public String toString() { 413 try { 414 String []nameOfTable = getAppropriateTableName(); 415 StringBuffer buffer = new StringBuffer (); 416 int index; 417 for(index = 0; index < nameOfTable.length - 1 ; ++index){ 418 buffer.append(nameOfTable[index]); 419 buffer.append("."); 420 } 421 buffer.append(nameOfTable[index]); 422 return buffer.toString(); 423 }catch(DException ex){ 424 throw new DRuntimeException(ex.getDseCode(),ex.getParameters()); 425 } 426 } 427 428 public boolean isFromSubQuery() throws DException { 429 return isFromSubQuery; 430 } 431 432 public void setAsFromSubQuery() throws DException { 433 isFromSubQuery = true; 434 } 435 } 436 | Popular Tags |