1 2 12 package com.versant.core.jdbc; 13 14 import com.versant.core.metadata.ClassMetaData; 15 import com.versant.core.metadata.MDStatics; 16 import com.versant.core.jdbc.metadata.JdbcField; 17 import com.versant.core.jdbc.metadata.JdbcClass; 18 import com.versant.core.jdbc.metadata.JdbcColumn; 19 20 import java.util.ArrayList ; 21 import java.util.Comparator ; 22 import java.util.Collections ; 23 24 30 public class SharedColumnChooser implements Comparator { 31 32 public SharedColumnChooser() { 33 } 34 35 38 public void chooseSharedColumns(ClassMetaData cmd) { 39 JdbcClass jdbcClass = (JdbcClass)cmd.storeClass; 40 ArrayList fieldList = new ArrayList (); 41 42 for (int i = 0; i < jdbcClass.fields.length; i++) { 43 JdbcField f = jdbcClass.fields[i]; 44 if (f == null) continue; 45 JdbcColumn[] mtc = f.mainTableCols; 46 if (mtc == null) continue; 47 48 for (int j = 0; j < mtc.length; j++) { 50 JdbcColumn c = mtc[j]; 51 if (c.shared) continue; 53 if (c.pk) { 56 jdbcClass.markColumnsShared(c.name, jdbcClass.table); 57 } else { 58 59 fieldList.clear(); 63 jdbcClass.findFieldsForColumn(f.fmd.classMetaData, c.name, 64 fieldList); 65 int n = fieldList.size(); 66 if (n > 1) { 67 Collections.sort(fieldList, this); 68 for (int k = 1; k < n; k++) { 69 JdbcField kf = ((JdbcField)fieldList.get(k)); 70 kf.findMainTableColumn(c.name).setShared(true); 71 } 72 } 73 } 74 75 jdbcClass.markSubclassColumnsShared(c.name); 77 } 78 } 79 80 if (((JdbcClass)cmd.storeClass).classIdCol != null) { 81 jdbcClass.markColumnsShared(jdbcClass.classIdCol.name, jdbcClass.table); 82 } 83 84 86 if (cmd.pcSubclasses != null) { 88 for (int i = 0; i < cmd.pcSubclasses.length; i++) { 89 chooseSharedColumns(cmd.pcSubclasses[i]); 90 } 91 } 92 } 93 94 98 public int compare(Object o1, Object o2) { 99 JdbcField a = (JdbcField)o1; 100 JdbcField b = (JdbcField)o2; 101 102 boolean simpleA = a.fmd.category == MDStatics.CATEGORY_SIMPLE; 104 boolean simpleB = b.fmd.category == MDStatics.CATEGORY_SIMPLE; 105 if (simpleA && !simpleB) return -1; 106 if (!simpleA && simpleB) return +1; 107 108 int diff = a.mainTableCols.length - b.mainTableCols.length; 110 if (diff != 0) return diff < 0 ? -1 : +1; 111 112 return a.fmd.fieldNo - b.fmd.fieldNo; 114 } 115 116 private void dump(JdbcClass jdbcClass) { 117 System.out.println("\nSharedColumnChooser.dump " + jdbcClass); 118 for (int i = 0; i < jdbcClass.fields.length; i++) { 119 JdbcField f = jdbcClass.fields[i]; 120 System.out.println("fields[" + i + "] = " + f); 121 if (f == null) continue; 122 JdbcColumn[] mtc = f.mainTableCols; 123 if (mtc == null) continue; 124 for (int j = 0; j < mtc.length; j++) { 125 JdbcColumn c = mtc[j]; 126 System.out.println(" mtc[" + j + "] = " + c); 127 } 128 } 129 System.out.println("---\n"); 130 } 131 132 } 133 134 135 | Popular Tags |