1 2 12 package com.versant.core.jdbc.metadata; 13 14 import com.versant.core.metadata.ClassMetaData; 15 import com.versant.core.metadata.FieldMetaData; 16 import com.versant.core.common.OID; 17 import com.versant.core.jdbc.sql.JdbcNameGenerator; 18 import com.versant.core.jdbc.sql.SqlDriver; 19 import com.versant.core.jdbc.sql.exp.ColumnExp; 20 import com.versant.core.jdbc.sql.exp.SelectExp; 21 import com.versant.core.jdbc.sql.exp.SqlExp; 22 import com.versant.core.jdbc.JdbcOID; 23 import com.versant.core.util.CharBuf; 24 import com.versant.core.common.State; 25 import com.versant.core.common.Debug; 26 27 import java.util.ArrayList ; 28 import java.io.PrintStream ; 29 import java.sql.PreparedStatement ; 30 import java.sql.SQLException ; 31 32 import com.versant.core.common.BindingSupportImpl; 33 34 37 public class JdbcRefField extends JdbcField { 38 39 44 public JdbcColumn[] cols; 45 49 public ClassMetaData targetClass; 50 53 public JdbcConstraint constraint; 54 58 public JdbcFKCollectionField masterCollectionField; 59 public String constraintName; 60 public boolean createConstraint; 61 62 public String toString() { 63 StringBuffer s = new StringBuffer (); 64 s.append(super.toString()); 65 switch (useJoin) { 66 case USE_JOIN_INNER: 67 s.append(" INNER"); 68 case USE_JOIN_OUTER: 69 s.append(" OUTER"); 70 } 71 return s.toString(); 72 } 73 74 77 public void setMainTable(JdbcTable table) { 78 super.setMainTable(table); 79 for (int i = 0; i < cols.length; i++) { 80 cols[i].setTable(table); 81 } 82 } 83 84 87 public void initMainTableCols() { 88 mainTableCols = cols; 89 super.initMainTableCols(); 90 } 91 92 95 public void addMainTableCols(ArrayList a) { 96 int n = cols.length; 97 for (int i = 0; i < n; i++) a.add(cols[i]); 98 } 99 100 103 public void nameColumns(String tableName, JdbcNameGenerator nameGen) { 104 int refs = 0; 106 FieldMetaData[] fields = fmd.classMetaData.fields; 107 for (int i = fields.length - 1; i >= 0; i--) { 108 JdbcField f = (JdbcField)fields[i].storeField; 109 if (f instanceof JdbcRefField) { 110 JdbcRefField rf = (JdbcRefField)f; 111 if (rf.targetClass == targetClass) { 112 if (++refs == 2) break; 113 } 114 } 115 } 116 boolean otherRefs = refs >= 2; 117 118 String [] names = new String [cols.length]; 121 String [] refPkNames = null; 122 JdbcClass tjc = (JdbcClass)targetClass.storeClass; 123 if (tjc != null) { 124 refPkNames = new String [cols.length]; 125 for (int i = 0; i < cols.length; i++) { 126 names[i] = cols[i].name; 127 refPkNames[i] = tjc.table.pk[i].name; 128 } 129 } 130 131 if (tjc == null) { 133 nameGen.generateRefFieldColumnNames(tableName, fmd.name, names, 134 null, null, otherRefs); 135 } else { 136 nameGen.generateRefFieldColumnNames(tableName, fmd.name, names, 137 tjc.tableName, refPkNames, otherRefs); 138 } 139 140 for (int i = 0; i < cols.length; i++) cols[i].name = names[i]; 142 } 143 144 148 public void addConstraints(ArrayList cons) { 149 if (constraint != null) cons.add(constraint); 150 } 151 152 public void dump(PrintStream out, String indent) { 153 super.dump(out, indent); 154 String is = indent + " "; 155 out.println(is + "targetClass = " + targetClass); 156 out.println(is + "constraint = " + constraint); 157 if (cols != null) { 158 out.println(is + cols.length + " cols(s)"); 159 for (int i = 0; i < cols.length; i++) { 160 out.println(is + "[" + i + "] " + cols[i]); 161 } 162 } 163 } 164 165 168 public boolean appendUpdate(CharBuf s, State state) { 169 int nc = mainTableColsForUpdate.length; 170 s.append(mainTableColsForUpdate[0].name); 171 s.append("=?"); 172 for (int i = 1; i < nc; i++) { 173 s.append(", "); 174 s.append(mainTableColsForUpdate[i].name); 175 s.append("=?"); 176 } 177 return false; 178 } 179 180 184 public void appendWhere(CharBuf s, SqlDriver sqlDriver) { 185 int nc = mainTableColsForUpdate.length; 186 JdbcColumn c = mainTableColsForUpdate[0]; 187 s.append(c.name); 188 s.append('='); 189 sqlDriver.appendWhereParam(s, c); 190 for (int i = 1; i < nc; i++) { 191 c = mainTableColsForUpdate[i]; 192 s.append(" and "); 193 s.append(c.name); 194 s.append('='); 195 sqlDriver.appendWhereParam(s, c); 196 } 197 } 198 199 204 public void appendWhereIsNull(CharBuf s, SqlDriver sqlDriver) { 205 int nc = mainTableColsForUpdate.length; 206 JdbcColumn c = mainTableColsForUpdate[0]; 207 s.append(c.name); 208 s.append(" is null"); 209 for (int i = 1; i < nc; i++) { 210 c = mainTableColsForUpdate[i]; 211 s.append(" and "); 212 s.append(c.name); 213 s.append(" is null"); 214 } 215 } 216 217 220 public void appendInsertColumnList(CharBuf s) { 221 int nc = mainTableColsForUpdate.length; 222 s.append(mainTableColsForUpdate[0].name); 223 for (int i = 1; i < nc; i++) { 224 s.append(", "); 225 s.append(mainTableColsForUpdate[i].name); 226 } 227 } 228 229 234 public boolean appendInsertValueList(CharBuf s, State state) { 235 s.append('?'); 236 int nc = mainTableColsForUpdate.length; 237 for (int i = 1; i < nc; i++) s.append(", ?"); 238 return false; 239 } 240 241 245 public static boolean isSubTableOf(JdbcTable table, ClassMetaData cmd) { 246 if (table == ((JdbcClass)cmd.storeClass).table) return true; 247 ClassMetaData[] pcSubs = cmd.pcSubclasses; 248 for (int i = 0; i < pcSubs.length; i++) { 249 ClassMetaData pcSub = pcSubs[i]; 250 if (((JdbcClass)pcSub.storeClass).table == table) { 251 return true; 252 } 253 } 254 return false; 255 } 256 257 261 public ColumnExp toColumnExp(SelectExp se, boolean joinToSuper) { 262 if (Debug.DEBUG) { 263 if (!isSubTableOf(se.table, fmd.classMetaData)) { 264 throw BindingSupportImpl.getInstance().internal("The table '" + se.table.name 265 + "'of the suplied selectExp is not equal or a subClass " + 266 "table of table '" 267 + ((JdbcClass)fmd.classMetaData.storeClass).table.name + "'"); 268 } 269 } 270 if (joinToSuper) se = SelectExp.createJoinToSuperTable(se, this); 271 return createOwningTableColumnExpList(se); 272 } 273 274 public ColumnExp createOwningTableColumnExpList(SelectExp se) { 275 ColumnExp ans = new ColumnExp(cols[0], se, this); 276 SqlExp e = ans; 277 int nc = cols.length; 278 for (int i = 1; i < nc; i++) { 279 e = e.next = new ColumnExp(cols[i], se, this); 280 } 281 return ans; 282 } 283 284 290 public ColumnExp toColumnExpForNullLiteralCompare(SelectExp se) { 291 se = SelectExp.createJoinToSuperTable(se, this); 292 293 if (mainTableColsForUpdate == null) return toColumnExp(se, true); 294 ColumnExp ans = new ColumnExp(mainTableColsForUpdate[0], se, this); 295 SqlExp e = ans; 296 int nc = mainTableColsForUpdate.length; 297 for (int i = 1; i < nc; i++) { 298 e = e.next = new ColumnExp(mainTableColsForUpdate[i], se, this); 299 } 300 return ans; 301 } 302 303 309 public int setQueryParam(PreparedStatement ps, int firstParam, 310 Object value) 311 throws SQLException { 312 OID oid = (OID)value; 313 if (oid != null) { 314 firstParam = ((JdbcOID)oid).setParams(ps, firstParam); 315 } else { 316 int nc = cols.length; 317 for (int i = 0; i < nc; i++) { 318 ps.setNull(firstParam++, cols[i].jdbcType); 319 } 320 } 321 return firstParam; 322 } 323 } 324 325 326 | Popular Tags |