KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > JdbcStateGenerator


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc;
13
14 import com.versant.core.metadata.generator.StateSrcGenerator;
15 import com.versant.core.metadata.ClassMetaData;
16 import com.versant.core.metadata.FieldMetaData;
17 import com.versant.core.metadata.MDStatics;
18 import com.versant.core.common.BindingSupportImpl;
19 import com.versant.core.compiler.ClassSpec;
20 import com.versant.core.jdbc.metadata.*;
21
22 import java.util.List JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.math.BigInteger JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 /**
31  * Adds JDBC specific stuff to the standard generated State class.
32  */

33 public class JdbcStateGenerator extends StateSrcGenerator {
34
35     public JdbcStateGenerator() {
36         super();
37     }
38
39     public ClassSpec generateState(ClassMetaData cmd) {
40         ClassSpec spec = super.generateState(cmd);
41
42         spec.addImport(JdbcState.class.getName());
43         spec.addImport(JdbcOID.class.getName());
44         spec.addImport(ResultSet JavaDoc.class.getName());
45         spec.addImport(PreparedStatement JavaDoc.class.getName());
46         spec.addImport(SQLException JavaDoc.class.getName());
47         spec.addImport(JdbcColumn.class.getName());
48         spec.addImport(JdbcUtils.class.getName());
49         spec.addImport(JdbcField.class.getName());
50         spec.addImport(JdbcPolyRefField.class.getName());
51         spec.addImport(JdbcSimpleField.class.getName());
52
53         spec.addInterface("JdbcState");
54
55         addJdbcHelpers();
56         addCopyPass1Fields(); //cool
57
addSetOracleStyleLOBs();
58         addCopyPass1Fields2();
59         addSetOptimisticLockingParams();//don't know
60
addSetParams(); //cool
61
addSetParamsChangedAndNotNull(); //cool
62

63         return spec;
64     }
65
66     protected void addInitStaticsBody(StringBuffer JavaDoc buf) {
67         super.addInitStaticsBody(buf);
68         // early exit from method if storeClass is null (i.e. remote PMF)
69
buf.append("\t\tif (cmd.storeClass == null) return true;\n");
70         FieldMetaData[] fields = cmd.stateFields;
71         boolean first = true;
72         for (int j = 0; j < fields.length; j++) {
73             JdbcField f = (JdbcField)fields[j].storeField;
74             if (f instanceof JdbcSimpleField) {
75                 JdbcSimpleField sf = (JdbcSimpleField)f;
76                 if (sf.col.converter != null) {
77                     if (first) {
78                         buf.append("\t\tFieldMetaData[] fields = cmd.stateFields;\n");
79                         buf.append("\t\tJdbcSimpleField sf;\n");
80                         first = false;
81                     }
82                     buf.append("\t\tsf = (JdbcSimpleField)fields[" + j + "].storeField;\n");
83                     buf.append("\t\t" + StateSrcGenerator.JDBC_CONVERTER_FIELD_PREFIX + j +
84                             " = (" + sf.col.converter.getClass().getName() + ")sf.col.converter;\n");
85                     buf.append("\t\tjdbcCol_" + j + " = sf.col;\n");
86                 }
87             }
88         }
89     }
90
91     private void addJdbcHelpers() {
92         spec.addMethod(
93                 "private OID getPolyRefOID(\n" +
94                 " FieldMetaData fmd,\n" +
95                 " ResultSet rs,\n" +
96                 " int firstCol) throws SQLException {\n" +
97                 " return getPolyRefOID((JdbcField)fmd.storeField, rs, firstCol);\n" +
98                 "}");
99         spec.addMethod(
100                 "private OID getPolyRefOID(\n" +
101                 " JdbcField f,\n" +
102                 " ResultSet rs,\n" +
103                 " int firstCol)\n" +
104                 " throws SQLException {\n" +
105                 " JdbcPolyRefField pf =\n" +
106                 " (JdbcPolyRefField)f;\n" +
107                 " return pf.getData(rs, firstCol);\n" +
108                 "}");
109         spec.addMethod(
110                 "private int setPolyRefData(\n" +
111                 " FieldMetaData fmd,\n" +
112                 " OID oid,\n" +
113                 " ClassMetaData cmd,\n" +
114                 " PreparedStatement ps,\n" +
115                 " int firstParam) throws SQLException {\n" +
116                 " return setPolyRefData((JdbcField)fmd.storeField, oid, cmd, ps, firstParam);\n" +
117                 "}");
118         spec.addMethod(
119                 "public static int setPolyRefData(\n" +
120                 " JdbcField f,\n" +
121                 " OID oid,\n" +
122                 " ClassMetaData cmd,\n" +
123                 " PreparedStatement ps,\n" +
124                 " int firstParam) throws SQLException {\n" +
125                 " JdbcPolyRefField pf =\n" +
126                 " (JdbcPolyRefField)f;\n" +
127                 " return pf.setData(ps, firstParam, oid);\n" +
128                 "}");
129     }
130
131
132     private void addSetOracleStyleLOBs() {
133         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
134         buf.append("\n\tpublic final void setOracleStyleLOBs(ResultSet rs, int stateFieldNos[], int numFieldNos, int firstCol) throws SQLException {\n");
135         List JavaDoc oracleStyleList = getOracleStyleLOBFieldsMetaData();
136         if (!oracleStyleList.isEmpty()) {
137             buf.append("\t\tfor (int i = 0; i < numFieldNos; i++) {\n");
138             buf.append("\t\t\tswitch (stateFieldNos[i]) {\n");
139             for (Iterator JavaDoc iter = oracleStyleList.iterator();iter.hasNext();) {
140                 FieldMetaData fmd = (FieldMetaData) iter.next();
141                 int fieldNo = fmd.stateFieldNo;
142                 buf.append("\t\t\t\tcase "+ fieldNo +":\n");
143                 buf.append("\t\t\t\t\tjdbcConverter_"+ fieldNo +".set(rs, firstCol++, jdbcCol_"+ fieldNo +", _"+ fieldNo +");\n");
144                 buf.append("\t\t\t\t\tbreak;\n\n");
145             }
146             buf.append("\t\t\t}\n");
147             buf.append("\t\t}\n");
148         }
149         buf.append("\t}\n");
150         spec.addMethod(buf.toString());
151     }
152
153     private void addCopyPass1Fields() {
154         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
155         buf.append("\n\tpublic final void copyPass1Fields(ResultSet rs, FetchGroup fetchGroup, int firstCol) throws SQLException {\n");
156         buf.append("\t\tint fgn[] = fetchGroup.stateFieldNos;\n");
157         buf.append("\t\tfor (int i = 0; i < fgn.length; i++) {\n");
158         buf.append("\t\t\tswitch (fgn[i]) {\n");
159         List JavaDoc pass1List = getPass1FieldsMetaData();
160         for (Iterator JavaDoc iterator = pass1List.iterator(); iterator.hasNext();) {
161             FieldMetaData fmd = (FieldMetaData) iterator.next();
162             int fieldNo = fmd.stateFieldNo;
163             buf.append("\t\t\t\tcase "+ fieldNo +":\n");
164             getCopyPass1FieldfromSQL(fmd,fieldNo, buf);
165         }
166         buf.append("\t\t\t\tdefault:\n");
167         buf.append("\t\t\t\t\tbreak;\n\n");
168         buf.append("\t\t\t}\n");
169         buf.append("\t\t}\n");
170         buf.append("\t}\n");
171         spec.addMethod(buf.toString());
172     }
173
174
175     private void addCopyPass1Fields2() {
176         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
177         buf.append("\n\tpublic final void copyPass1Fields(ResultSet rs, JdbcField fields[]) {\n");
178         List JavaDoc pass1List = getPass1FieldsMetaData();
179         if (!pass1List.isEmpty()){
180             buf.append("\t\tJdbcField field = null;\n");
181             buf.append("\t\ttry {\n");
182             buf.append("\t\t\tfor (int i = 0; i < fields.length; i++) {\n");
183             buf.append("\t\t\t\tfield = fields[i];\n");
184             buf.append("\t\t\t\tif (field != null && cmd.stateFields[field.stateFieldNo] == field.fmd) {\n");
185             buf.append("\t\t\t\t\tswitch (field.stateFieldNo) {\n");
186             for (Iterator JavaDoc iter = pass1List.iterator(); iter.hasNext();) {
187                 FieldMetaData fmd = (FieldMetaData) iter.next();
188                 int fieldNo = fmd.stateFieldNo;
189                 buf.append("\t\t\t\t\t\tcase "+ fieldNo +":\n");
190                 getCopyPass1FieldfromSQL2(fmd, fieldNo, buf);
191             }
192             buf.append("\t\t\t\t\t}\n");
193             buf.append("\t\t\t\t}\n");
194             buf.append("\t\t\t}\n");
195             buf.append("\t\t} catch (SQLException e) {\n");
196             buf.append("\t\t\tthrow com.versant.core.common.BindingSupportImpl.getInstance().datastore(\"Error reading field \" + field.fmd.getQName() + \" from ResultSet: \" + e, e);\n");
197             buf.append("\t\t}\n");
198         }
199         buf.append("\t}\n");
200         spec.addMethod(buf.toString());
201     }
202
203     private void getCopyPass1FieldfromSQL2(FieldMetaData fmd, int fieldNo,
204             StringBuffer JavaDoc buf) {
205         JdbcField f = (JdbcField)fmd.storeField;
206         if (f instanceof JdbcSimpleField) {
207             JdbcColumn c = ((JdbcSimpleField) f).col;
208             Class JavaDoc fieldType = c.javaType;
209             boolean isPrim = c.javaType.isPrimitive();
210             if (c.converter != null) {// converter
211
// _2 = (Date)jdbcConverter_2.get(rs, firstCol++, jdbcCol_2);
212
if (isExternalized(fmd) || isPrimitiveArray(fmd)) {
213                     buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = jdbcConverter_" + fieldNo + ".get(rs, (i + 1), jdbcCol_" + fieldNo + ");\n");
214                 } else if (isPrim) {
215                     String JavaDoc wrapper = (String JavaDoc) primativeTypesToWrapper.get(fieldType);
216                     String JavaDoc toValue = (String JavaDoc) wrapperStringToValue.get(wrapper);
217                     buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = ((" + wrapper + ")jdbcConverter_" + fieldNo + ".get(rs, (i + 1), jdbcCol_" + fieldNo + "))." + toValue + "();\n");
218                 } else {
219                     buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = (" + fieldType.getName() + ")jdbcConverter_" + fieldNo + ".get(rs, (i + 1), jdbcCol_" + fieldNo + ");\n");
220                 }
221
222             } else { // no converter
223
Class JavaDoc prim = (Class JavaDoc) wrapperTypesToPrimative.get(fieldType);
224                 if (prim != null) { // its a wrapper class
225
// _0 = new Integer(rs.getInt(firstCol++));
226
// if (rs.wasNull()) {
227
// _0 = null;
228
// }
229
String JavaDoc rsGet = (String JavaDoc) typeToResultSetGetField.get(prim);
230                     buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = new " + fieldType.getName() + "(rs." + rsGet + "(i + 1));\n");
231                     buf.append("\t\t\t\t\t\t\tif (rs.wasNull()) {\n");
232                     buf.append("\t\t\t\t\t\t\t\t_" + fieldNo + " = null;\n");
233                     buf.append("\t\t\t\t\t\t\t}\n");
234                 } else if (fieldType.equals(java.math.BigInteger JavaDoc.class)) { // special case for BigInteger
235
// BigDecimal decimal4 = rs.getBigDecimal(firstCol++);
236
// if (decimal4 != null) {
237
// _4 = decimal4.toBigInteger();
238
// } else {
239
// _4 = null;
240
// }
241
buf.append("\t\t\t\t\t\t\tjava.math.BigDecimal decimal" + fieldNo + " = rs.getBigDecimal(i + 1);\n");
242                     buf.append("\t\t\t\t\t\t\tif (decimal" + fieldNo + " != null) {\n");
243                     buf.append("\t\t\t\t\t\t\t\t_" + fieldNo + " = decimal" + fieldNo + ".toBigInteger();\n");
244                     buf.append("\t\t\t\t\t\t\t} else {\n");
245                     buf.append("\t\t\t\t\t\t\t\t_" + fieldNo + " = null;\n");
246                     buf.append("\t\t\t\t\t\t\t}\n");
247                 } else {
248                     if (isExternalized(fmd)) {
249                         buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = rs.getObject(i + 1);\n");
250                     } else if ((String JavaDoc) typeToResultSetGetField.get(fieldType) == null) {
251                         buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = (" + fieldType.getName() + ")rs.getObject(i + 1);\n");
252                     } else {
253                         String JavaDoc getField = (String JavaDoc) typeToResultSetGetField.get(fieldType);
254                         buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = rs." + getField + "(i + 1);\n");
255                     }
256                 }
257
258             }
259         } else if (f instanceof JdbcPolyRefField) {
260             buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = getPolyRefOID(cmd.stateFields[" + fieldNo + "], rs, (i + 1));\n");
261         } else if (f instanceof JdbcRefField) {
262             JdbcRefField rf = (JdbcRefField) f;
263             String JavaDoc oidName = rf.targetClass.oidClassName;
264             boolean isInHier = rf.targetClass.isInHeirachy();
265             if (isInHier) {
266                 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = new " + oidName + "(cmd.jmd.classes[" + rf.targetClass.index + "], false);\n");
267             } else {
268                 buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = new " + oidName + "();\n");
269             }
270             buf.append("\t\t\t\t\t\t\tif (!((JdbcOID)_" + fieldNo + ").copyKeyFields(rs, (i + 1))) {\n");
271             buf.append("\t\t\t\t\t\t\t\t_" + fieldNo + " = null;\n");
272             buf.append("\t\t\t\t\t\t\t}\n");
273         }
274         buf.append("\t\t\t\t\t\t\t" + getFilledFieldName(fieldNo) + " |= " + getFieldIndex(fieldNo) + ";\n");
275         buf.append("\t\t\t\t\t\t\tbreak;\n\n");
276     }
277
278     private String JavaDoc getConverterSet(JdbcSimpleField field) {
279         int fieldNo = field.stateFieldNo;
280         JdbcColumn c = field.col;
281         boolean isPrim = c.javaType.isPrimitive();
282         String JavaDoc wrapper = null;
283         if (isPrim) {
284             wrapper = (String JavaDoc) primativeTypesToWrapper.get(c.javaType);
285         }
286         String JavaDoc con = "jdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", " +
287                 (isPrim ? "new " + wrapper + "(_" + fieldNo + ")" : "_" + fieldNo + "") + ");";
288         return con;
289     }
290
291
292
293     private void getCopyPass1FieldfromSQL(FieldMetaData fmd, int fieldNo,
294             StringBuffer JavaDoc buf) {
295         JdbcField f = (JdbcField)fmd.storeField;
296         if (f instanceof JdbcSimpleField) {
297             JdbcColumn c = ((JdbcSimpleField) f).col;
298             Class JavaDoc fieldType = c.javaType;
299             boolean isPrim = c.javaType.isPrimitive();
300             if (c.converter != null) {// converter
301
// _2 = (Date)jdbcConverter_2.get(rs, firstCol++, jdbcCol_2);
302
if (isExternalized(fmd) || isPrimitiveArray(fmd)) {
303                     buf.append("\t\t\t\t\t_"+ fieldNo +" = jdbcConverter_"+ fieldNo +".get(rs, firstCol++, jdbcCol_"+ fieldNo +");\n");
304                 } else if (isPrim) {
305                     String JavaDoc wrapper = (String JavaDoc) primativeTypesToWrapper.get(fieldType);
306                     String JavaDoc toValue = (String JavaDoc) wrapperStringToValue.get(wrapper);
307                     buf.append("\t\t\t\t\t_" + fieldNo + " = (("+ wrapper +")jdbcConverter_" + fieldNo + ".get(rs, firstCol++, jdbcCol_" + fieldNo + "))."+ toValue +"();\n");
308                 } else {
309                     buf.append("\t\t\t\t\t_" + fieldNo + " = ("+ fieldType.getName() +")jdbcConverter_" + fieldNo + ".get(rs, firstCol++, jdbcCol_" + fieldNo + ");\n");
310                 }
311
312             } else { // no converter
313
Class JavaDoc prim = (Class JavaDoc)wrapperTypesToPrimative.get(fieldType);
314                 if (prim != null) { // its a wrapper class
315
// _0 = new Integer(rs.getInt(firstCol++));
316
// if (rs.wasNull()) {
317
// _0 = null;
318
// }
319
String JavaDoc rsGet = (String JavaDoc) typeToResultSetGetField.get(prim);
320                     buf.append("\t\t\t\t\t_"+ fieldNo +" = new "+ fieldType.getName() +"(rs."+ rsGet +"(firstCol++));\n");
321                     buf.append("\t\t\t\t\tif (rs.wasNull()) {\n");
322                     buf.append("\t\t\t\t\t\t_"+ fieldNo +" = null;\n");
323                     buf.append("\t\t\t\t\t}\n");
324                 } else if (fieldType.equals(java.math.BigInteger JavaDoc.class)) { // special case for BigInteger
325
// BigDecimal decimal4 = rs.getBigDecimal(firstCol++);
326
// if (decimal4 != null) {
327
// _4 = decimal4.toBigInteger();
328
// } else {
329
// _4 = null;
330
// }
331
buf.append("\t\t\t\t\tjava.math.BigDecimal decimal"+ fieldNo +" = rs.getBigDecimal(firstCol++);\n");
332                     buf.append("\t\t\t\t\tif (decimal"+ fieldNo +" != null) {\n");
333                     buf.append("\t\t\t\t\t\t_"+ fieldNo +" = decimal"+ fieldNo +".toBigInteger();\n");
334                     buf.append("\t\t\t\t\t} else {\n");
335                     buf.append("\t\t\t\t\t\t_"+ fieldNo +" = null;\n");
336                     buf.append("\t\t\t\t\t}\n");
337                 } else {
338                     if (isExternalized(fmd)) {
339                         buf.append("\t\t\t\t\t_"+fieldNo+" = rs.getObject(firstCol++);\n");
340                     } else if ((String JavaDoc) typeToResultSetGetField.get(fieldType) == null) {
341                         buf.append("\t\t\t\t\t_" + fieldNo + " = ("+ fieldType.getName() +")rs.getObject(firstCol++);\n");
342                     } else {
343                         String JavaDoc getField = (String JavaDoc) typeToResultSetGetField.get(fieldType);
344                         buf.append("\t\t\t\t\t_" + fieldNo + " = rs." + getField + "(firstCol++);\n");
345                     }
346                 }
347
348             }
349         } else if (f instanceof JdbcPolyRefField) {
350 // _3 = State.getPolyRefOID(cmd.stateFields[3], rs, firstCol);
351
// firstCol += 2;
352
buf.append("\t\t\t\t\t_"+fieldNo+" = getPolyRefOID(cmd.stateFields["+fieldNo+"], rs, firstCol);\n");
353             int inc = ((JdbcPolyRefField) f).cols.length;
354             if (inc == 1){
355                 buf.append("\t\t\t\t\tfirstCol++;\n");
356             } else {
357                 buf.append("\t\t\t\t\tfirstCol += " + inc + ";\n");
358             }
359
360
361         } else if (f instanceof JdbcRefField) {
362             JdbcRefField rf = (JdbcRefField) f;
363             String JavaDoc oidName = rf.targetClass.oidClassName;
364             boolean isInHier = rf.targetClass.isInHeirachy();
365             if (isInHier) {
366                 buf.append("\t\t\t\t\t_"+fieldNo+" = new "+ oidName +"(cmd.jmd.classes["+ rf.targetClass.index +"], false);\n");
367             } else {
368                 buf.append("\t\t\t\t\t_" + fieldNo + " = new " + oidName + "();\n");
369             }
370             buf.append("\t\t\t\t\tif (!((JdbcOID)_" + fieldNo + ").copyKeyFields(rs, firstCol)) {\n");
371             buf.append("\t\t\t\t\t\t_" + fieldNo + " = null;\n");
372             buf.append("\t\t\t\t\t}\n");
373             int inc = rf.cols.length;
374             if (inc == 1) {
375                 buf.append("\t\t\t\t\tfirstCol++;\n");
376             } else {
377                 buf.append("\t\t\t\t\tfirstCol += " + inc + ";\n");
378             }
379         }
380         buf.append("\t\t\t\t\t"+ getFilledFieldName(fieldNo) +" |= "+ getFieldIndex(fieldNo) +";\n");
381         buf.append("\t\t\t\t\tbreak;\n\n");
382     }
383
384     private List JavaDoc getOracleStyleLOBFieldsMetaData() {
385         ArrayList JavaDoc list = new ArrayList JavaDoc();
386         FieldMetaData[] fields = cmd.stateFields;
387         list.ensureCapacity(fields.length);
388         for (int i = 0; i < fields.length; i++) {
389             FieldMetaData field = fields[i];
390             if (field.storeField != null && ((JdbcField)field.storeField).isOracleStyleLOB()) {
391                 list.add(field);
392             }
393         }
394         return list;
395     }
396
397     private void addSetOptimisticLockingParams() {
398         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
399 // public final int setOptimisticLockingParams(PreparedStatement ps, int firstParam) throws SQLException {
400
// ps.setShort(firstParam++, _23);
401
// ps.setInt(firstParam++, _0.intValue());
402
// return firstParam;
403
// jdbcConverter_5.set(ps, firstParam++, jdbcCol_5, _5);
404
// }
405

406         buf.append("\n\tpublic final int setOptimisticLockingParams(PreparedStatement ps, int firstParam) throws SQLException {\n");
407         JdbcSimpleField f = ((JdbcClass)cmd.storeClass).optimisticLockingField;
408         if (f != null) {
409             int fieldNo = f.stateFieldNo;
410             JdbcColumn c = f.col;
411             Class JavaDoc classType = c.javaType;
412             if (c.converter != null) {// converter
413
buf.append("\t\t"+getConverterSet(f)+"\n");
414             } else { // no converter
415
if (wrapperTypesToPrimative.containsKey(classType)) { // its a wrapper class
416
Class JavaDoc primType = (Class JavaDoc) wrapperTypesToPrimative.get(classType);
417                     String JavaDoc psSet = (String JavaDoc)typeToPreparedStatementSetField.get(primType);
418                     String JavaDoc toValue = (String JavaDoc)wrapperTypesToValue.get(classType);
419                     buf.append("\t\tps."+ psSet +"(firstParam++, _"+ fieldNo +"."+ toValue +"());\n");
420                 } else if (classType.equals(BigInteger JavaDoc.class)) { // special case for BigInteger
421
buf.append("\t\tps.setBigDecimal(firstParam++, new BigDecimal(_"+ fieldNo +"));\n");
422                 } else {
423                     String JavaDoc psSet = (String JavaDoc)typeToPreparedStatementSetField.get(classType);
424                     if (psSet != null) {
425                        buf.append("\t\tps."+ psSet +"(firstParam++, _"+ fieldNo +");\n");
426                     } else {
427                         buf.append("\t\tps.setObject(firstParam++, _"+ fieldNo +", "+ c.jdbcType +");\n");
428                     }
429                 }
430             }
431
432         }
433         buf.append("\t\treturn firstParam;\n");
434         buf.append("\t}\n");
435         spec.addMethod(buf.toString());
436     }
437
438     protected void addCompareToPass1() {
439         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
440         buf.append("\n\tpublic final int compareToPass1(com.versant.core.common.State state) {\n");
441         buf.append("\t\t" + className + " s = ("+className+") state;\n");
442         int[] masks = new int[getNumOfControlFields()];
443         int num = cmd.stateFields.length;
444         for (int i = 0; i < num; i++) {
445             if (cmd.stateFields[i].primaryField) {
446                 int fieldNum = cmd.stateFields[i].stateFieldNo;
447                 masks[getLocalVarIndex(0, fieldNum)] += getFieldIndex(fieldNum);
448             }
449         }
450         int maskLenght = masks.length;
451         for (int i = 0; i < maskLenght; i++) {
452             if (i == 0) {
453                 buf.append("\t\tint ans = ((filled0 & "+ masks[i] +") - (s.filled0 & " + masks[i] + "))");
454             } else {
455                 buf.append(" + ((filled"+i+" & "+ masks[i] +") - (s.filled"+i+" & "+ masks[i] +"))");
456             }
457
458         }
459         buf.append(";\n");
460         // do test for CLOBS here.
461
List JavaDoc list = getOracleStyleLOBFieldsMetaData();
462         if (!list.isEmpty()) {
463             // do if stuff
464
Iterator JavaDoc iter = list.iterator();
465             while (iter.hasNext()) {
466                 FieldMetaData metaData = (FieldMetaData) iter.next();
467                 int fieldNo = metaData.stateFieldNo;
468 // if (ans == 0 && (filled0 & 1) != 0) {
469
// if (_0 == null && s._0 != null) {
470
// return -1;
471
// }
472
// if (_0 != null && s._0 == null) {
473
// return 1;
474
// }
475
// }
476
buf.append("\t\tif (ans == 0 && ("+ getFilledFieldName(fieldNo) +" & "+ getFieldIndex(fieldNo) +") != 0) {\n");
477                 buf.append("\t\t\tif (_"+ fieldNo +" == null && s._"+ fieldNo +" != null) {\n");
478                 buf.append("\t\t\t\treturn -1;\n");
479                 buf.append("\t\t\t}\n");
480                 buf.append("\t\t\tif (_"+ fieldNo +" != null && s._"+ fieldNo +" == null) {\n");
481                 buf.append("\t\t\t\treturn 1;\n");
482                 buf.append("\t\t\t}\n");
483                 buf.append("\t\t}\n");
484             }
485         }
486         buf.append("\t\treturn ans;\n");
487         buf.append("\t}\n");
488         spec.addMethod(buf.toString());
489     }
490
491     protected void addSetParams() {
492         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
493         buf.append("\n\tpublic final int setParams(PreparedStatement ps, int fieldNos[], int firstFieldNo, int lastFieldNo, int firstParam, com.versant.core.server.PersistGraph pGraph, int tableNo) throws java.sql.SQLException {\n");
494         buf.append("\t\tfor (; firstFieldNo < lastFieldNo; firstFieldNo++) {\n");
495         buf.append("\t\t\tswitch (fieldNos[firstFieldNo]) {\n");
496         List JavaDoc pass1List = getPass1FieldsMetaData();
497         for (Iterator JavaDoc iter = pass1List.iterator();iter.hasNext();) {
498             FieldMetaData fmd = (FieldMetaData) iter.next();
499             buf.append("\t\t\t\tcase "+ fmd.stateFieldNo +":\n");
500             getSetFieldsToSQL_IL_CheckNull(fmd.stateFieldNo, buf);
501         }
502         buf.append("\t\t\t\tdefault:\n");
503         buf.append("\t\t\t\t\tbreak;\n\n");
504         buf.append("\t\t\t}\n");
505         buf.append("\t\t}\n");
506         buf.append("\t\treturn firstParam;\n");
507         buf.append("\t}\n");
508         spec.addMethod(buf.toString());
509     }
510
511     protected void addSetParamsChangedAndNotNull() {
512         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
513         buf.append("\n\tpublic final int setParamsChangedAndNotNull(PreparedStatement ps, int fieldNos[], int firstFieldNo, int lastFieldNo, int firstParam, com.versant.core.server.PersistGraph pGraph, int tableNo) throws java.sql.SQLException {\n");
514         List JavaDoc pass1List = getPass1FieldsMetaDataWithChangedLocking();
515         if (!pass1List.isEmpty()){
516             buf.append("\t\tfor (; firstFieldNo < lastFieldNo; firstFieldNo++) {\n");
517             buf.append("\t\t\tif (!isNull(fieldNos[firstFieldNo])) {\n");
518             buf.append("\t\t\t\tswitch (fieldNos[firstFieldNo]) {\n");
519             for (Iterator JavaDoc iter = pass1List.iterator();iter.hasNext();) {
520                 FieldMetaData fmd = (FieldMetaData) iter.next();
521                 buf.append("\t\t\t\t\tcase "+fmd.stateFieldNo+":\n");
522                 getSetFieldsToSQL_IL(fmd.stateFieldNo, buf);
523             }
524             buf.append("\t\t\t\t\tdefault:\n");
525             buf.append("\t\t\t\t\t\tbreak;\n\n");
526             buf.append("\t\t\t\t}\n");
527             buf.append("\t\t\t}\n");
528             buf.append("\t\t}\n");
529         }
530         buf.append("\t\treturn firstParam;\n");
531         buf.append("\t}\n");
532         spec.addMethod(buf.toString());
533     }
534
535     protected void getSetFieldsToSQL_IL_CheckNull(int fieldNo, StringBuffer JavaDoc buf) {
536         FieldMetaData fmd = cmd.stateFields[fieldNo];
537         JdbcField field = (JdbcField)fmd.storeField;
538         JdbcTable fieldTable = field.mainTable;
539         JdbcTable[] tables = ((JdbcClass)cmd.storeClass).allTables;
540         boolean isMultiTable = false;
541         int tableNo = -1;
542         if (tables.length > 1) {
543             isMultiTable = true;
544             for (int i = 0; i < tables.length; i++) {
545                 if (tables[i] == fieldTable) {
546                     tableNo = i;
547                 }
548             }
549         }
550         if (field instanceof JdbcSimpleField) {
551             JdbcColumn c = ((JdbcSimpleField) field).col;
552             if (c.isForUpdate()) {
553                 Class JavaDoc fieldType = c.javaType;
554                 boolean isPrimative = c.javaType.isPrimitive();
555                 if (isPrimative) {
556                     if (isExternalized(fmd)) {
557                         isPrimative = false;
558                     }
559                 }
560                 if (c.converter != null) {// converter
561
boolean isPrim = c.javaType.isPrimitive();
562                     if (isMultiTable) { // we have a multi table
563
buf.append("\t\t\t\t\tif (tableNo == "+ tableNo +") {\n");
564                         if (isPrim) {
565                             buf.append("\t\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", new "+ primativeTypesToWrapper.get(fieldType) +"(_" + fieldNo + "));\n");
566                         } else {
567                             buf.append("\t\t\t\t\t\tjdbcConverter_"+ fieldNo +".set(ps, firstParam++, jdbcCol_"+ fieldNo +", _"+ fieldNo +");\n");
568                         }
569                         buf.append("\t\t\t\t\t}\n");
570                     } else {
571                         if (isPrim) {
572                             buf.append("\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", new " + primativeTypesToWrapper.get(fieldType) + "(_" + fieldNo + "));\n");
573                         } else {
574                             buf.append("\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", _" + fieldNo + ");\n");
575                         }
576                     }
577                 } else {
578                     if (isPrimative) {
579                         // this is a temp var
580
if (isMultiTable) { // we have a multi table
581
buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
582                             buf.append("\t\t\t\t\t\tps."+ typeToPreparedStatementSetField.get(fieldType) +"(firstParam++, _"+ fieldNo +");\n");
583                             buf.append("\t\t\t\t\t}\n");
584                         } else {
585                             buf.append("\t\t\t\t\tps." + typeToPreparedStatementSetField.get(fieldType) + "(firstParam++, _" + fieldNo + ");\n");
586                         }
587                     } else if (wrapperTypesToPrimative.containsKey(fieldType)) { // its a wrapper class
588
String JavaDoc toVal = (String JavaDoc) wrapperTypesToValue.get(fieldType);
589                         Class JavaDoc primType = (Class JavaDoc) wrapperTypesToPrimative.get(fieldType);
590                         String JavaDoc psSet = (String JavaDoc) typeToPreparedStatementSetField.get(primType);
591                         if (isMultiTable) { // we have a multi table
592
// if (_8 != null) {
593
// ps.setInt(firstParam++, _8.intValue());
594
// } else {
595
// ps.setNull(firstParam++, 4);
596
// }
597

598                             buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
599                             buf.append("\t\t\t\t\t\tif (_" + fieldNo + " != null) {\n");
600                             buf.append("\t\t\t\t\t\t\tps."+ psSet +"(firstParam++, _"+ fieldNo +"."+ toVal +"());\n");
601                             buf.append("\t\t\t\t\t\t} else {\n");
602                             buf.append("\t\t\t\t\t\t\tps.setNull(firstParam++, "+ c.jdbcType +");\n");
603                             buf.append("\t\t\t\t\t\t}\n");
604                             buf.append("\t\t\t\t\t}\n");
605                         } else {
606                             buf.append("\t\t\t\t\tif (_" + fieldNo + " != null) {\n");
607                             buf.append("\t\t\t\t\t\tps." + psSet + "(firstParam++, _" + fieldNo + "." + toVal + "());\n");
608                             buf.append("\t\t\t\t\t} else {\n");
609                             buf.append("\t\t\t\t\t\tps.setNull(firstParam++, " + c.jdbcType + ");\n");
610                             buf.append("\t\t\t\t\t}\n");
611                         }
612                     } else if (fieldType.equals(java.math.BigInteger JavaDoc.class)) { // special case for BigInteger
613
// if (_4 != null) {
614
// ps.setBigDecimal(firstParam++, new BigDecimal(_4));
615
// } else {
616
// ps.setNull(firstParam++, 2);
617
// }
618
if (isMultiTable) { // we have a multi table
619
buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
620                             buf.append("\t\t\t\t\t\tif (_" + fieldNo + " != null) {\n");
621                             buf.append("\t\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_"+ fieldNo +"));\n");
622                             buf.append("\t\t\t\t\t\t} else {\n");
623                             buf.append("\t\t\t\t\t\t\tps.setNull(firstParam++, " + c.jdbcType + ");\n");
624                             buf.append("\t\t\t\t\t\t}\n");
625                             buf.append("\t\t\t\t\t}\n");
626                         } else {
627                             buf.append("\t\t\t\t\tif (_" + fieldNo + " != null) {\n");
628                             buf.append("\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_" + fieldNo + "));\n");
629                             buf.append("\t\t\t\t\t} else {\n");
630                             buf.append("\t\t\t\t\t\tps.setNull(firstParam++, " + c.jdbcType + ");\n");
631                             buf.append("\t\t\t\t\t}\n");
632                         }
633                     } else {
634 // if (_3 != null) {
635
// ps.setBigDecimal(firstParam++, _3);
636
// } else {
637
// ps.setNull(firstParam++, 2);
638
// }
639
String JavaDoc psSet = (String JavaDoc) typeToPreparedStatementSetField.get(fieldType);
640                         if (isExternalized(fmd)){
641                             psSet = null;
642                         }
643                         if (isMultiTable) { // we have a multi table
644
buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
645                             buf.append("\t\t\t\t\t\tif (_" + fieldNo + " != null) {\n");
646                             buf.append("\t\t\t\t\t\t\tps."+ (psSet != null ? psSet : "setObject")
647                                     +"(firstParam++, "+ ((isExternalized(fmd) && psSet != null) ? "(" + fieldType.getName() + ")" : "")
648                                     +"_" + fieldNo + (psSet != null ? "" : (", " + c.jdbcType))+");\n");
649                             buf.append("\t\t\t\t\t\t} else {\n");
650                             buf.append("\t\t\t\t\t\t\tps.setNull(firstParam++, " + c.jdbcType + ");\n");
651                             buf.append("\t\t\t\t\t\t}\n");
652                             buf.append("\t\t\t\t\t}\n");
653                         } else {
654                             buf.append("\t\t\t\t\tif (_" + fieldNo + " != null) {\n");
655                             buf.append("\t\t\t\t\t\tps." + (psSet != null ? psSet : "setObject")
656                                     + "(firstParam++, " + ((isExternalized(fmd) && psSet != null) ? "(" + fieldType.getName() + ")" : "")
657                                     + "_" + fieldNo + (psSet != null ? "" : (", " + c.jdbcType)) + ");\n");
658                             buf.append("\t\t\t\t\t} else {\n");
659                             buf.append("\t\t\t\t\t\tps.setNull(firstParam++, " + c.jdbcType + ");\n");
660                             buf.append("\t\t\t\t\t}\n");
661                         }
662                     }
663                 }
664             }
665         } else if (field instanceof JdbcPolyRefField) {
666 // firstParam = State.setPolyRefData(cmd.stateFields[3], (OID) _3, cmd, ps, firstParam);
667
if (isMultiTable) { // we have a multi table
668
buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
669                 buf.append("\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields["+ fieldNo +"], (OID) _"+ fieldNo +", cmd, ps, firstParam);\n");
670                 buf.append("\t\t\t\t\t}\n");
671             } else {
672                 buf.append("\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields[" + fieldNo + "], (OID) _" + fieldNo + ", cmd, ps, firstParam);\n");
673             }
674         } else if (field instanceof JdbcRefField) {
675             if (isMultiTable) { // we have a multi table
676
buf.append("\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
677             } else {
678                 buf.append("\t\t\t\t\t{\n");
679             }
680             buf.append("\t\t\t\t\t\tOID oid = (OID)_" + fieldNo + ";\n");
681             buf.append("\t\t\t\t\t\tif (oid == null || (oid = oid.getRealOID()) == null) {\n");
682             JdbcColumn[] cols = field.mainTableCols;
683             int nc = cols.length;
684             for (int j = 0; j < nc; j++) {
685                 JdbcColumn col = cols[j];
686                 if (col.isForUpdate()) {
687                     buf.append("\t\t\t\t\t\t\tps.setNull(firstParam++, " + col.jdbcType + ");\n");
688                 }
689             }
690             buf.append("\t\t\t\t\t\t} else {\n");
691             buf.append("\t\t\t\t\t\t\tfirstParam = ((JdbcOID)oid).setParams(ps, firstParam, ((JdbcField)cmd.stateFields["+ fieldNo +"].storeField).mainTableCols);\n");
692             buf.append("\t\t\t\t\t\t}\n");
693             buf.append("\t\t\t\t\t}\n");
694         }
695         buf.append("\t\t\t\t\tbreak;\n\n");
696     }
697
698     protected void getSetFieldsToSQL_IL(int fieldNo, StringBuffer JavaDoc buf) {
699         FieldMetaData fmd = cmd.stateFields[fieldNo];
700         JdbcField field = (JdbcField)fmd.storeField;
701         JdbcTable fieldTable = field.mainTable;
702         JdbcTable[] tables = ((JdbcClass)cmd.storeClass).allTables;
703         boolean isMultiTable = false;
704         int tableNo = -1;
705         if (tables.length > 1) {
706             isMultiTable = true;
707             for (int i = 0; i < tables.length; i++) {
708                 if (tables[i] == fieldTable) {
709                     tableNo = i;
710                 }
711             }
712         }
713         if (field instanceof JdbcSimpleField) {
714             JdbcColumn c = ((JdbcSimpleField) field).col;
715             if (c.isForUpdate()) {
716                 Class JavaDoc fieldType = c.javaType;
717                 boolean isPrimative = c.javaType.isPrimitive();
718                 if (isPrimative){
719                     if (isExternalized(fmd)){
720                         isPrimative = false;
721                     }
722                 }
723                 if (c.converter != null) {// converter
724
boolean isPrim = c.javaType.isPrimitive();
725                     if (isMultiTable) { // we have a multi table
726
buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
727                         if (isPrim) {
728                             buf.append("\t\t\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", new " + primativeTypesToWrapper.get(fieldType) + "(_" + fieldNo + "));\n");
729                         } else {
730                             buf.append("\t\t\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", _" + fieldNo + ");\n");
731                         }
732                         buf.append("\t\t\t\t\t\t}\n");
733                     } else {
734                         if (isPrim) {
735                             buf.append("\t\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", new " + primativeTypesToWrapper.get(fieldType) + "(_" + fieldNo + "));\n");
736                         } else {
737                             buf.append("\t\t\t\t\t\tjdbcConverter_" + fieldNo + ".set(ps, firstParam++, jdbcCol_" + fieldNo + ", _" + fieldNo + ");\n");
738                         }
739                     }
740                 } else {
741                     if (isPrimative) {
742                         if (isMultiTable) { // we have a multi table
743
buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
744                             buf.append("\t\t\t\t\t\t\tps." + typeToPreparedStatementSetField.get(fieldType) + "(firstParam++, _" + fieldNo + ");\n");
745                             buf.append("\t\t\t\t\t\t}\n");
746                         } else {
747                             buf.append("\t\t\t\t\t\tps." + typeToPreparedStatementSetField.get(fieldType) + "(firstParam++, _" + fieldNo + ");\n");
748                         }
749                     } else if (wrapperTypesToPrimative.containsKey(fieldType)) { // its a wrapper class
750
String JavaDoc toVal = (String JavaDoc) wrapperTypesToValue.get(fieldType);
751                         Class JavaDoc primType = (Class JavaDoc) wrapperTypesToPrimative.get(fieldType);
752                         String JavaDoc psSet = (String JavaDoc) typeToPreparedStatementSetField.get(primType);
753                         if (isMultiTable) { // we have a multi table
754
// ps.setInt(firstParam++, _8.intValue());
755
buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
756                             buf.append("\t\t\t\t\t\t\tps." + psSet + "(firstParam++, _" + fieldNo + "." + toVal + "());\n");
757                             buf.append("\t\t\t\t\t\t}\n");
758                         } else {
759                             buf.append("\t\t\t\t\t\tps." + psSet + "(firstParam++, _" + fieldNo + "." + toVal + "());\n");
760                         }
761                     } else if (fieldType.equals(java.math.BigInteger JavaDoc.class)) { // special case for BigInteger
762
// ps.setBigDecimal(firstParam++, new BigDecimal(_4));
763
if (isMultiTable) { // we have a multi table
764
buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
765                             buf.append("\t\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_" + fieldNo + "));\n");
766                             buf.append("\t\t\t\t\t\t}\n");
767                         } else {
768                             buf.append("\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_" + fieldNo + "));\n");
769                         }
770                     } else {
771 // ps.setBigDecimal(firstParam++, _3);
772
String JavaDoc psSet = (String JavaDoc) typeToPreparedStatementSetField.get(fieldType);
773                         if (isExternalized(fmd)){
774                             psSet = null;
775                         }
776                         if (isMultiTable) { // we have a multi table
777
buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
778                             buf.append("\t\t\t\t\t\t\tps." + (psSet != null ? psSet : "setObject")
779                                     + "(firstParam++, " + ((isExternalized(fmd) && psSet != null) ? "(" + fieldType.getName() + ")" : "")
780                                     + "_" + fieldNo + (psSet != null ? "" : (", " + c.jdbcType)) + ");\n");
781                             buf.append("\t\t\t\t\t\t}\n");
782                         } else {
783                             buf.append("\t\t\t\t\t\tps." + (psSet != null ? psSet : "setObject")
784                                     + "(firstParam++, " + ((isExternalized(fmd) && psSet != null) ? "(" + fieldType.getName() + ")" : "")
785                                     + "_" + fieldNo + (psSet != null ? "" : (", " + c.jdbcType)) + ");\n");
786
787                         }
788                     }
789                 }
790             }
791         } else if (field instanceof JdbcPolyRefField) {
792 // firstParam = State.setPolyRefData(cmd.stateFields[3], (OID) _3, cmd, ps, firstParam);
793
if (isMultiTable) { // we have a multi table
794
buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
795                 buf.append("\t\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields[" + fieldNo + "], (OID) _" + fieldNo + ", cmd, ps, firstParam);\n");
796                 buf.append("\t\t\t\t\t\t}\n");
797             } else {
798                 buf.append("\t\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields[" + fieldNo + "], (OID) _" + fieldNo + ", cmd, ps, firstParam);\n");
799             }
800         } else if (field instanceof JdbcRefField) {
801 // firstParam = ((OID)_1).setParams(ps, firstParam, cmd.stateFields[1].storeField.mainTableCols);
802
if (isMultiTable) { // we have a multi table
803
buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo + ") {\n");
804                 buf.append("\t\t\t\t\t\t\tfirstParam = ((JdbcOID)_" + fieldNo + ").setParams(ps, firstParam, ((JdbcField)cmd.stateFields[" + fieldNo + "].storeField).mainTableCols);\n");
805                 buf.append("\t\t\t\t\t\t}\n");
806             } else {
807                 buf.append("\t\t\t\t\t\tfirstParam = ((JdbcOID)_" + fieldNo + ").setParams(ps, firstParam, ((JdbcField)cmd.stateFields[" + fieldNo + "].storeField).mainTableCols);\n");
808             }
809         }
810         buf.append("\t\t\t\t\t\tbreak;\n\n");
811     }
812
813     protected void addContainsValidAppIdFields() {
814         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
815         buf.append("\n\tpublic final boolean containsValidAppIdFields() {\n");
816         if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
817             FieldMetaData[] pkf = cmd.pcHeirachy[0].pkFields;
818             for (int i = 0; i < pkf.length; i++) {
819                 FieldMetaData fmd = pkf[i];
820                 int pkFieldNum = fmd.stateFieldNo;
821                 Class JavaDoc fieldType = fmd.type;
822                 boolean isPrimative = fmd.type.isPrimitive();
823
824                 if (isPrimative) {
825                     if (fieldType.equals(int.class)) {
826                         Integer JavaDoc intt = (Integer JavaDoc) fmd.getPKDefaultValue();
827                         buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == "+ intt.intValue() +") {\n");
828                     } else if (fieldType.equals(long.class)) {
829                         Long JavaDoc longg = (Long JavaDoc) fmd.getPKDefaultValue();
830                         buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + longg.longValue() + ") {\n");
831                     } else if (fieldType.equals(boolean.class)) {
832                         Boolean JavaDoc bool = (Boolean JavaDoc) fmd.getPKDefaultValue();
833                         if (bool.booleanValue()) {
834                             buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + ") {\n");
835                         } else {
836                             buf.append("\t\tif (("+ getFilledFieldName(pkFieldNum) +" & "+ getFieldIndex(pkFieldNum) +") == 0 || !_"+ pkFieldNum +") {\n");
837                         }
838                     } else if (fieldType.equals(double.class)) {
839                         Double JavaDoc doubl = (Double JavaDoc) fmd.getPKDefaultValue();
840                         buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + doubl.doubleValue() + ") {\n");
841                     } else if (fieldType.equals(float.class)) {
842                         Float JavaDoc floatt = (Float JavaDoc) fmd.getPKDefaultValue();
843                         buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + floatt.doubleValue() + ") {\n");
844                     } else if (fieldType.equals(short.class)) {
845                         Short JavaDoc shortt = (Short JavaDoc) fmd.getPKDefaultValue();
846                         buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + shortt.shortValue() + ") {\n");
847                     } else if (fieldType.equals(char.class)) {
848                         Character JavaDoc charr = (Character JavaDoc) fmd.getPKDefaultValue();
849                         buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + charr.charValue() + ") {\n");
850                     } else if (fieldType.equals(byte.class)) {
851                         Byte JavaDoc bytee = (Byte JavaDoc) fmd.getPKDefaultValue();
852                         buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum) + " & " + getFieldIndex(pkFieldNum) + ") == 0 || _" + pkFieldNum + " == " + bytee.byteValue() + ") {\n");
853                     } else {
854                         throw BindingSupportImpl.getInstance().invalidOperation("Unsupported type " + fieldType);
855                     }
856                     buf.append("\t\t\treturn false;\n");
857                     buf.append("\t\t}\n");
858                 } else {
859                     buf.append("\t\tif (("+ getFilledFieldName(pkFieldNum) +" & "+ getFieldIndex(pkFieldNum) +") == 0 || _"+ pkFieldNum +" == null || _" + pkFieldNum + ".equals(cmd.pcHeirachy[0].pkFields["+ i +"].getPKDefaultValue())) {\n");
860                     buf.append("\t\t\treturn false;\n");
861                     buf.append("\t\t}\n");
862                 }
863             }
864             buf.append("\t\treturn true;\n");
865         } else if (cmd.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) {
866             buf.append("\t\treturn false;\n");
867         }
868         buf.append("\t}\n");
869         spec.addMethod(buf.toString());
870     }
871
872     /**
873      * Add all PC fields in the hier to state and
874      * private boolean isDirty;
875      * private boolean[] dirtyFields = new boolean[20];
876      * private boolean[] filled = new boolean[20];
877      */

878     protected void addFields() {
879         super.addFields();
880         FieldMetaData[] fields = cmd.stateFields;
881         for (int i = 0; i < fields.length; i++) {
882             JdbcField f = (JdbcField)fields[i].storeField;
883             if (f instanceof JdbcSimpleField) {
884                 JdbcSimpleField sf = (JdbcSimpleField) f;
885                 if (sf.col.converter != null) {
886                     spec.addField("public static " + sf.col.converter.getClass().getName()
887                             +" "+JDBC_CONVERTER_FIELD_PREFIX + i);
888                     spec.addField("public static " + sf.col.getClass().getName()
889                             +" "+ "jdbcCol_" + i);
890                 }
891             }
892         }
893     }
894
895     protected void addCopyFields() {
896         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
897         buf.append("\n\tpublic final void copyFields(OID oid) {\n");
898         ClassMetaData currentCMD = null;
899         if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
900             currentCMD = getTopPCSuperClassMetaData();
901             buf.append("\t\t" + currentCMD.oidClassName + " id = (" + currentCMD.oidClassName + ")oid;\n");
902             FieldMetaData[] fmds = currentCMD.pkFields;
903             for (int i = 0; i < fmds.length; i++) {
904                 FieldMetaData fmd = fmds[i];
905                 int stateFieldNum = fmd.stateFieldNo;
906                 buf.append("\t\t_"+ stateFieldNum +" = id._"+ i +";\n");
907                 buf.append("\t\t"+getFilledFieldName(stateFieldNum) +" |= "+ getFieldIndex(stateFieldNum) +";\n");
908             }
909         }
910         buf.append("\t}\n");
911         spec.addMethod(buf.toString());
912     }
913
914     protected void addHasSameNullFields() {
915         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
916         buf.append("\n\tpublic final boolean hasSameNullFields(State state, State mask) {\n");
917         JdbcClass jc = (JdbcClass)cmd.storeClass;
918         if (jc.optimisticLocking != JdbcClass.OPTIMISTIC_LOCKING_CHANGED) {
919             buf.append("\t\treturn true;\n");
920         } else {
921             buf.append("\t\t"+className+" s = ("+ className +") state;\n");
922             buf.append("\t\t"+className+" ms = ("+ className +") mask;\n");
923             int num = getNumOfControlFields();
924             for (int i = 0; i < num; i++) {
925                 if (i == 0) {
926                     buf.append("\t\tint filledMask = ms.filled0;\n");
927                 } else {
928                     buf.append("\t\tfilledMask = ms.filled" + i + ";\n");
929                 }
930                 List JavaDoc fields = getRealObjectFields(i);
931                 for (Iterator JavaDoc iter = fields.iterator(); iter.hasNext();) {
932                     FieldMetaData fmd = (FieldMetaData) iter.next();
933                     int fieldNum = getFieldNo(fmd);
934                     int index = getFieldIndex(fieldNum);
935                     buf.append("\t\tif ((filledMask & "+ index +") != 0 && (_"+ fieldNum +" == null) != (s._"+ fieldNum +" == null)) {return false;}\n");
936                 }
937             }
938             buf.append("\t\treturn true;\n");
939         }
940         buf.append("\t}\n");
941         spec.addMethod(buf.toString());
942     }
943
944     protected void addCopyKeyFieldsUpdate() {
945         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
946         buf.append("\n\tpublic final void copyKeyFieldsUpdate(OID oid) {\n");
947         if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
948             buf.append("\t\t"+ cmd.oidClassName +" other = ("+ cmd.oidClassName +") oid;\n");
949             FieldMetaData[] pkFields = cmd.pkFields;
950             JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
951             for (int i = 0; i < pkc.length; i++) {
952                 int stateFieldNo = pkFields[i].stateFieldNo;
953                 buf.append("\t\tif (containsField("+ stateFieldNo +")) {\n");
954                 buf.append("\t\t\tother._"+i+" = _"+ stateFieldNo +";\n");
955                 buf.append("\t\t}\n");
956             }
957         }
958         buf.append("\t}\n");
959         spec.addMethod(buf.toString());
960     }
961
962     protected void addCopyKeyFieldsFromOID() {
963         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
964         buf.append("\n\tpublic final void copyKeyFields(OID oid) {\n");
965         if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
966             buf.append("\t\t"+ cmd.oidClassName +" other = ("+ cmd.oidClassName +") oid;\n");
967             FieldMetaData[] pkFields = cmd.pkFields;
968             JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
969             for (int i = 0; i < pkc.length; i++) {
970                 buf.append("\t\tother._"+i+" = _"+ pkFields[i].stateFieldNo +";\n");
971             }
972         }
973         buf.append("\t}\n");
974         spec.addMethod(buf.toString());
975     }
976
977     protected void addCheckKeyFields() {
978         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
979         buf.append("\n\tpublic final boolean checkKeyFields(OID oid) {\n");
980         if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
981             buf.append("\t\t" + cmd.oidClassName + " other = (" + cmd.oidClassName + ") oid;\n");
982             FieldMetaData[] pkFields = cmd.pkFields;
983             JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
984             for (int i = 0; i < pkc.length; i++) {
985                 Class JavaDoc classType = pkc[i].javaType;
986                 boolean isPrimitive = classType.isPrimitive();
987                 if (isPrimitive) {
988                     buf.append("\t\tif (other._"+i+" != _"+ pkFields[i].stateFieldNo +") {\n");
989                     buf.append("\t\t\treturn false;\n");
990                     buf.append("\t\t}\n");
991                 } else {
992                     buf.append("\t\tif (!other._"+i+".equals(_"+ pkFields[i].stateFieldNo +")) {\n");
993                     buf.append("\t\t\treturn false;\n");
994                     buf.append("\t\t}\n");
995                 }
996             }
997         }
998         buf.append("\t\treturn true;\n");
999         buf.append("\t}\n");
1000        spec.addMethod(buf.toString());
1001    }
1002
1003    protected void addClearApplicationIdentityFields() {
1004        StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
1005        buf.append("\n\tpublic final void clearApplicationIdentityFields() {\n");
1006        if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
1007            int[] masks = new int[getNumOfControlFields()];
1008            FieldMetaData[] pkf = cmd.pcHeirachy[0].pkFields;
1009            for (int i = pkf.length - 1; i >= 0; i--) {
1010                int fieldNum = pkf[i].fieldNo;
1011                masks[getLocalVarIndex(0, fieldNum)] += getFieldIndex(fieldNum);
1012            }
1013            int maskLength = masks.length;
1014            for (int i = 0; i < maskLength; i++) {
1015                if (masks[i] != 0) {
1016                    buf.append("\t\tfilled"+i+" = filled"+i+" & "+ (masks[i] ^ 0xFFFFFFFF) +";\n");
1017                }
1018            }
1019        }
1020        buf.append("\t}\n");
1021        spec.addMethod(buf.toString());
1022    }
1023
1024    protected void addContainsApplicationIdentityFields() {
1025        StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
1026        buf.append("\n\tpublic final boolean containsApplicationIdentityFields() {\n");
1027        if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
1028            FieldMetaData[] pkf = cmd.pcHeirachy[0].pkFields;
1029            for (int i = 0; i < pkf.length; i++) {
1030                int pkFieldNum = pkf[i].stateFieldNo;
1031                buf.append("\t\tif (("+ getFilledFieldName(pkFieldNum) +" & "+ getFieldIndex(pkFieldNum) +") == 0) {\n");
1032                buf.append("\t\t\treturn false;\n");
1033                buf.append("\t\t}\n");
1034            }
1035            buf.append("\t\treturn true;\n");
1036        } else {
1037            buf.append("\t\treturn false;\n");
1038        }
1039        buf.append("\t}\n");
1040        spec.addMethod(buf.toString());
1041    }
1042
1043    protected List JavaDoc getPass1FieldsMetaDataWithChangedLocking() {
1044        ArrayList JavaDoc list = new ArrayList JavaDoc();
1045        FieldMetaData[] fields = cmd.stateFields;
1046        list.ensureCapacity(fields.length);
1047        for (int i = 0; i < fields.length; i++) {
1048            FieldMetaData field = fields[i];
1049            if (field.primaryField) {
1050                if (((JdbcField)field.storeField).includeForChangedLocking) {
1051                    list.add(field);
1052                }
1053            }
1054        }
1055        return list;
1056    }
1057
1058}
1059
1060
Popular Tags