0001: /*
0002: * Copyright (c) 1998 - 2005 Versant Corporation
0003: * All rights reserved. This program and the accompanying materials
0004: * are made available under the terms of the Eclipse Public License v1.0
0005: * which accompanies this distribution, and is available at
0006: * http://www.eclipse.org/legal/epl-v10.html
0007: *
0008: * Contributors:
0009: * Versant Corporation - initial API and implementation
0010: */
0011: package com.versant.core.jdbc;
0012:
0013: import com.versant.core.metadata.generator.StateSrcGenerator;
0014: import com.versant.core.metadata.ClassMetaData;
0015: import com.versant.core.metadata.FieldMetaData;
0016: import com.versant.core.metadata.MDStatics;
0017: import com.versant.core.common.BindingSupportImpl;
0018: import com.versant.core.compiler.ClassSpec;
0019: import com.versant.core.jdbc.metadata.*;
0020:
0021: import java.util.List;
0022: import java.util.Iterator;
0023: import java.util.ArrayList;
0024: import java.math.BigInteger;
0025: import java.sql.ResultSet;
0026: import java.sql.PreparedStatement;
0027: import java.sql.SQLException;
0028:
0029: /**
0030: * Adds JDBC specific stuff to the standard generated State class.
0031: */
0032: public class JdbcStateGenerator extends StateSrcGenerator {
0033:
0034: public JdbcStateGenerator() {
0035: super ();
0036: }
0037:
0038: public ClassSpec generateState(ClassMetaData cmd) {
0039: ClassSpec spec = super .generateState(cmd);
0040:
0041: spec.addImport(JdbcState.class.getName());
0042: spec.addImport(JdbcOID.class.getName());
0043: spec.addImport(ResultSet.class.getName());
0044: spec.addImport(PreparedStatement.class.getName());
0045: spec.addImport(SQLException.class.getName());
0046: spec.addImport(JdbcColumn.class.getName());
0047: spec.addImport(JdbcUtils.class.getName());
0048: spec.addImport(JdbcField.class.getName());
0049: spec.addImport(JdbcPolyRefField.class.getName());
0050: spec.addImport(JdbcSimpleField.class.getName());
0051:
0052: spec.addInterface("JdbcState");
0053:
0054: addJdbcHelpers();
0055: addCopyPass1Fields(); //cool
0056: addSetOracleStyleLOBs();
0057: addCopyPass1Fields2();
0058: addSetOptimisticLockingParams();//don't know
0059: addSetParams(); //cool
0060: addSetParamsChangedAndNotNull(); //cool
0061:
0062: return spec;
0063: }
0064:
0065: protected void addInitStaticsBody(StringBuffer buf) {
0066: super .addInitStaticsBody(buf);
0067: // early exit from method if storeClass is null (i.e. remote PMF)
0068: buf.append("\t\tif (cmd.storeClass == null) return true;\n");
0069: FieldMetaData[] fields = cmd.stateFields;
0070: boolean first = true;
0071: for (int j = 0; j < fields.length; j++) {
0072: JdbcField f = (JdbcField) fields[j].storeField;
0073: if (f instanceof JdbcSimpleField) {
0074: JdbcSimpleField sf = (JdbcSimpleField) f;
0075: if (sf.col.converter != null) {
0076: if (first) {
0077: buf
0078: .append("\t\tFieldMetaData[] fields = cmd.stateFields;\n");
0079: buf.append("\t\tJdbcSimpleField sf;\n");
0080: first = false;
0081: }
0082: buf.append("\t\tsf = (JdbcSimpleField)fields[" + j
0083: + "].storeField;\n");
0084: buf
0085: .append("\t\t"
0086: + StateSrcGenerator.JDBC_CONVERTER_FIELD_PREFIX
0087: + j
0088: + " = ("
0089: + sf.col.converter.getClass()
0090: .getName()
0091: + ")sf.col.converter;\n");
0092: buf.append("\t\tjdbcCol_" + j + " = sf.col;\n");
0093: }
0094: }
0095: }
0096: }
0097:
0098: private void addJdbcHelpers() {
0099: spec
0100: .addMethod("private OID getPolyRefOID(\n"
0101: + " FieldMetaData fmd,\n"
0102: + " ResultSet rs,\n"
0103: + " int firstCol) throws SQLException {\n"
0104: + " return getPolyRefOID((JdbcField)fmd.storeField, rs, firstCol);\n"
0105: + "}");
0106: spec.addMethod("private OID getPolyRefOID(\n"
0107: + " JdbcField f,\n" + " ResultSet rs,\n"
0108: + " int firstCol)\n"
0109: + " throws SQLException {\n"
0110: + " JdbcPolyRefField pf =\n"
0111: + " (JdbcPolyRefField)f;\n"
0112: + " return pf.getData(rs, firstCol);\n" + "}");
0113: spec
0114: .addMethod("private int setPolyRefData(\n"
0115: + " FieldMetaData fmd,\n"
0116: + " OID oid,\n"
0117: + " ClassMetaData cmd,\n"
0118: + " PreparedStatement ps,\n"
0119: + " int firstParam) throws SQLException {\n"
0120: + " return setPolyRefData((JdbcField)fmd.storeField, oid, cmd, ps, firstParam);\n"
0121: + "}");
0122: spec
0123: .addMethod("public static int setPolyRefData(\n"
0124: + " JdbcField f,\n"
0125: + " OID oid,\n"
0126: + " ClassMetaData cmd,\n"
0127: + " PreparedStatement ps,\n"
0128: + " int firstParam) throws SQLException {\n"
0129: + " JdbcPolyRefField pf =\n"
0130: + " (JdbcPolyRefField)f;\n"
0131: + " return pf.setData(ps, firstParam, oid);\n"
0132: + "}");
0133: }
0134:
0135: private void addSetOracleStyleLOBs() {
0136: StringBuffer buf = new StringBuffer();
0137: buf
0138: .append("\n\tpublic final void setOracleStyleLOBs(ResultSet rs, int stateFieldNos[], int numFieldNos, int firstCol) throws SQLException {\n");
0139: List oracleStyleList = getOracleStyleLOBFieldsMetaData();
0140: if (!oracleStyleList.isEmpty()) {
0141: buf.append("\t\tfor (int i = 0; i < numFieldNos; i++) {\n");
0142: buf.append("\t\t\tswitch (stateFieldNos[i]) {\n");
0143: for (Iterator iter = oracleStyleList.iterator(); iter
0144: .hasNext();) {
0145: FieldMetaData fmd = (FieldMetaData) iter.next();
0146: int fieldNo = fmd.stateFieldNo;
0147: buf.append("\t\t\t\tcase " + fieldNo + ":\n");
0148: buf.append("\t\t\t\t\tjdbcConverter_" + fieldNo
0149: + ".set(rs, firstCol++, jdbcCol_" + fieldNo
0150: + ", _" + fieldNo + ");\n");
0151: buf.append("\t\t\t\t\tbreak;\n\n");
0152: }
0153: buf.append("\t\t\t}\n");
0154: buf.append("\t\t}\n");
0155: }
0156: buf.append("\t}\n");
0157: spec.addMethod(buf.toString());
0158: }
0159:
0160: private void addCopyPass1Fields() {
0161: StringBuffer buf = new StringBuffer();
0162: buf
0163: .append("\n\tpublic final void copyPass1Fields(ResultSet rs, FetchGroup fetchGroup, int firstCol) throws SQLException {\n");
0164: buf.append("\t\tint fgn[] = fetchGroup.stateFieldNos;\n");
0165: buf.append("\t\tfor (int i = 0; i < fgn.length; i++) {\n");
0166: buf.append("\t\t\tswitch (fgn[i]) {\n");
0167: List pass1List = getPass1FieldsMetaData();
0168: for (Iterator iterator = pass1List.iterator(); iterator
0169: .hasNext();) {
0170: FieldMetaData fmd = (FieldMetaData) iterator.next();
0171: int fieldNo = fmd.stateFieldNo;
0172: buf.append("\t\t\t\tcase " + fieldNo + ":\n");
0173: getCopyPass1FieldfromSQL(fmd, fieldNo, buf);
0174: }
0175: buf.append("\t\t\t\tdefault:\n");
0176: buf.append("\t\t\t\t\tbreak;\n\n");
0177: buf.append("\t\t\t}\n");
0178: buf.append("\t\t}\n");
0179: buf.append("\t}\n");
0180: spec.addMethod(buf.toString());
0181: }
0182:
0183: private void addCopyPass1Fields2() {
0184: StringBuffer buf = new StringBuffer();
0185: buf
0186: .append("\n\tpublic final void copyPass1Fields(ResultSet rs, JdbcField fields[]) {\n");
0187: List pass1List = getPass1FieldsMetaData();
0188: if (!pass1List.isEmpty()) {
0189: buf.append("\t\tJdbcField field = null;\n");
0190: buf.append("\t\ttry {\n");
0191: buf
0192: .append("\t\t\tfor (int i = 0; i < fields.length; i++) {\n");
0193: buf.append("\t\t\t\tfield = fields[i];\n");
0194: buf
0195: .append("\t\t\t\tif (field != null && cmd.stateFields[field.stateFieldNo] == field.fmd) {\n");
0196: buf.append("\t\t\t\t\tswitch (field.stateFieldNo) {\n");
0197: for (Iterator iter = pass1List.iterator(); iter.hasNext();) {
0198: FieldMetaData fmd = (FieldMetaData) iter.next();
0199: int fieldNo = fmd.stateFieldNo;
0200: buf.append("\t\t\t\t\t\tcase " + fieldNo + ":\n");
0201: getCopyPass1FieldfromSQL2(fmd, fieldNo, buf);
0202: }
0203: buf.append("\t\t\t\t\t}\n");
0204: buf.append("\t\t\t\t}\n");
0205: buf.append("\t\t\t}\n");
0206: buf.append("\t\t} catch (SQLException e) {\n");
0207: buf
0208: .append("\t\t\tthrow com.versant.core.common.BindingSupportImpl.getInstance().datastore(\"Error reading field \" + field.fmd.getQName() + \" from ResultSet: \" + e, e);\n");
0209: buf.append("\t\t}\n");
0210: }
0211: buf.append("\t}\n");
0212: spec.addMethod(buf.toString());
0213: }
0214:
0215: private void getCopyPass1FieldfromSQL2(FieldMetaData fmd,
0216: int fieldNo, StringBuffer buf) {
0217: JdbcField f = (JdbcField) fmd.storeField;
0218: if (f instanceof JdbcSimpleField) {
0219: JdbcColumn c = ((JdbcSimpleField) f).col;
0220: Class fieldType = c.javaType;
0221: boolean isPrim = c.javaType.isPrimitive();
0222: if (c.converter != null) {// converter
0223: // _2 = (Date)jdbcConverter_2.get(rs, firstCol++, jdbcCol_2);
0224: if (isExternalized(fmd) || isPrimitiveArray(fmd)) {
0225: buf.append("\t\t\t\t\t\t\t_" + fieldNo
0226: + " = jdbcConverter_" + fieldNo
0227: + ".get(rs, (i + 1), jdbcCol_" + fieldNo
0228: + ");\n");
0229: } else if (isPrim) {
0230: String wrapper = (String) primativeTypesToWrapper
0231: .get(fieldType);
0232: String toValue = (String) wrapperStringToValue
0233: .get(wrapper);
0234: buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = (("
0235: + wrapper + ")jdbcConverter_" + fieldNo
0236: + ".get(rs, (i + 1), jdbcCol_" + fieldNo
0237: + "))." + toValue + "();\n");
0238: } else {
0239: buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = ("
0240: + fieldType.getName() + ")jdbcConverter_"
0241: + fieldNo + ".get(rs, (i + 1), jdbcCol_"
0242: + fieldNo + ");\n");
0243: }
0244:
0245: } else { // no converter
0246: Class prim = (Class) wrapperTypesToPrimative
0247: .get(fieldType);
0248: if (prim != null) { // its a wrapper class
0249: // _0 = new Integer(rs.getInt(firstCol++));
0250: // if (rs.wasNull()) {
0251: // _0 = null;
0252: // }
0253: String rsGet = (String) typeToResultSetGetField
0254: .get(prim);
0255: buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = new "
0256: + fieldType.getName() + "(rs." + rsGet
0257: + "(i + 1));\n");
0258: buf.append("\t\t\t\t\t\t\tif (rs.wasNull()) {\n");
0259: buf.append("\t\t\t\t\t\t\t\t_" + fieldNo
0260: + " = null;\n");
0261: buf.append("\t\t\t\t\t\t\t}\n");
0262: } else if (fieldType.equals(java.math.BigInteger.class)) { // special case for BigInteger
0263: // BigDecimal decimal4 = rs.getBigDecimal(firstCol++);
0264: // if (decimal4 != null) {
0265: // _4 = decimal4.toBigInteger();
0266: // } else {
0267: // _4 = null;
0268: // }
0269: buf
0270: .append("\t\t\t\t\t\t\tjava.math.BigDecimal decimal"
0271: + fieldNo
0272: + " = rs.getBigDecimal(i + 1);\n");
0273: buf.append("\t\t\t\t\t\t\tif (decimal" + fieldNo
0274: + " != null) {\n");
0275: buf.append("\t\t\t\t\t\t\t\t_" + fieldNo
0276: + " = decimal" + fieldNo
0277: + ".toBigInteger();\n");
0278: buf.append("\t\t\t\t\t\t\t} else {\n");
0279: buf.append("\t\t\t\t\t\t\t\t_" + fieldNo
0280: + " = null;\n");
0281: buf.append("\t\t\t\t\t\t\t}\n");
0282: } else {
0283: if (isExternalized(fmd)) {
0284: buf.append("\t\t\t\t\t\t\t_" + fieldNo
0285: + " = rs.getObject(i + 1);\n");
0286: } else if ((String) typeToResultSetGetField
0287: .get(fieldType) == null) {
0288: buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = ("
0289: + fieldType.getName()
0290: + ")rs.getObject(i + 1);\n");
0291: } else {
0292: String getField = (String) typeToResultSetGetField
0293: .get(fieldType);
0294: buf.append("\t\t\t\t\t\t\t_" + fieldNo
0295: + " = rs." + getField + "(i + 1);\n");
0296: }
0297: }
0298:
0299: }
0300: } else if (f instanceof JdbcPolyRefField) {
0301: buf.append("\t\t\t\t\t\t\t_" + fieldNo
0302: + " = getPolyRefOID(cmd.stateFields[" + fieldNo
0303: + "], rs, (i + 1));\n");
0304: } else if (f instanceof JdbcRefField) {
0305: JdbcRefField rf = (JdbcRefField) f;
0306: String oidName = rf.targetClass.oidClassName;
0307: boolean isInHier = rf.targetClass.isInHeirachy();
0308: if (isInHier) {
0309: buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = new "
0310: + oidName + "(cmd.jmd.classes["
0311: + rf.targetClass.index + "], false);\n");
0312: } else {
0313: buf.append("\t\t\t\t\t\t\t_" + fieldNo + " = new "
0314: + oidName + "();\n");
0315: }
0316: buf.append("\t\t\t\t\t\t\tif (!((JdbcOID)_" + fieldNo
0317: + ").copyKeyFields(rs, (i + 1))) {\n");
0318: buf.append("\t\t\t\t\t\t\t\t_" + fieldNo + " = null;\n");
0319: buf.append("\t\t\t\t\t\t\t}\n");
0320: }
0321: buf.append("\t\t\t\t\t\t\t" + getFilledFieldName(fieldNo)
0322: + " |= " + getFieldIndex(fieldNo) + ";\n");
0323: buf.append("\t\t\t\t\t\t\tbreak;\n\n");
0324: }
0325:
0326: private String getConverterSet(JdbcSimpleField field) {
0327: int fieldNo = field.stateFieldNo;
0328: JdbcColumn c = field.col;
0329: boolean isPrim = c.javaType.isPrimitive();
0330: String wrapper = null;
0331: if (isPrim) {
0332: wrapper = (String) primativeTypesToWrapper.get(c.javaType);
0333: }
0334: String con = "jdbcConverter_"
0335: + fieldNo
0336: + ".set(ps, firstParam++, jdbcCol_"
0337: + fieldNo
0338: + ", "
0339: + (isPrim ? "new " + wrapper + "(_" + fieldNo + ")"
0340: : "_" + fieldNo + "") + ");";
0341: return con;
0342: }
0343:
0344: private void getCopyPass1FieldfromSQL(FieldMetaData fmd,
0345: int fieldNo, StringBuffer buf) {
0346: JdbcField f = (JdbcField) fmd.storeField;
0347: if (f instanceof JdbcSimpleField) {
0348: JdbcColumn c = ((JdbcSimpleField) f).col;
0349: Class fieldType = c.javaType;
0350: boolean isPrim = c.javaType.isPrimitive();
0351: if (c.converter != null) {// converter
0352: // _2 = (Date)jdbcConverter_2.get(rs, firstCol++, jdbcCol_2);
0353: if (isExternalized(fmd) || isPrimitiveArray(fmd)) {
0354: buf.append("\t\t\t\t\t_" + fieldNo
0355: + " = jdbcConverter_" + fieldNo
0356: + ".get(rs, firstCol++, jdbcCol_" + fieldNo
0357: + ");\n");
0358: } else if (isPrim) {
0359: String wrapper = (String) primativeTypesToWrapper
0360: .get(fieldType);
0361: String toValue = (String) wrapperStringToValue
0362: .get(wrapper);
0363: buf.append("\t\t\t\t\t_" + fieldNo + " = (("
0364: + wrapper + ")jdbcConverter_" + fieldNo
0365: + ".get(rs, firstCol++, jdbcCol_" + fieldNo
0366: + "))." + toValue + "();\n");
0367: } else {
0368: buf.append("\t\t\t\t\t_" + fieldNo + " = ("
0369: + fieldType.getName() + ")jdbcConverter_"
0370: + fieldNo + ".get(rs, firstCol++, jdbcCol_"
0371: + fieldNo + ");\n");
0372: }
0373:
0374: } else { // no converter
0375: Class prim = (Class) wrapperTypesToPrimative
0376: .get(fieldType);
0377: if (prim != null) { // its a wrapper class
0378: // _0 = new Integer(rs.getInt(firstCol++));
0379: // if (rs.wasNull()) {
0380: // _0 = null;
0381: // }
0382: String rsGet = (String) typeToResultSetGetField
0383: .get(prim);
0384: buf.append("\t\t\t\t\t_" + fieldNo + " = new "
0385: + fieldType.getName() + "(rs." + rsGet
0386: + "(firstCol++));\n");
0387: buf.append("\t\t\t\t\tif (rs.wasNull()) {\n");
0388: buf
0389: .append("\t\t\t\t\t\t_" + fieldNo
0390: + " = null;\n");
0391: buf.append("\t\t\t\t\t}\n");
0392: } else if (fieldType.equals(java.math.BigInteger.class)) { // special case for BigInteger
0393: // BigDecimal decimal4 = rs.getBigDecimal(firstCol++);
0394: // if (decimal4 != null) {
0395: // _4 = decimal4.toBigInteger();
0396: // } else {
0397: // _4 = null;
0398: // }
0399: buf.append("\t\t\t\t\tjava.math.BigDecimal decimal"
0400: + fieldNo
0401: + " = rs.getBigDecimal(firstCol++);\n");
0402: buf.append("\t\t\t\t\tif (decimal" + fieldNo
0403: + " != null) {\n");
0404: buf.append("\t\t\t\t\t\t_" + fieldNo + " = decimal"
0405: + fieldNo + ".toBigInteger();\n");
0406: buf.append("\t\t\t\t\t} else {\n");
0407: buf
0408: .append("\t\t\t\t\t\t_" + fieldNo
0409: + " = null;\n");
0410: buf.append("\t\t\t\t\t}\n");
0411: } else {
0412: if (isExternalized(fmd)) {
0413: buf.append("\t\t\t\t\t_" + fieldNo
0414: + " = rs.getObject(firstCol++);\n");
0415: } else if ((String) typeToResultSetGetField
0416: .get(fieldType) == null) {
0417: buf.append("\t\t\t\t\t_" + fieldNo + " = ("
0418: + fieldType.getName()
0419: + ")rs.getObject(firstCol++);\n");
0420: } else {
0421: String getField = (String) typeToResultSetGetField
0422: .get(fieldType);
0423: buf.append("\t\t\t\t\t_" + fieldNo + " = rs."
0424: + getField + "(firstCol++);\n");
0425: }
0426: }
0427:
0428: }
0429: } else if (f instanceof JdbcPolyRefField) {
0430: // _3 = State.getPolyRefOID(cmd.stateFields[3], rs, firstCol);
0431: // firstCol += 2;
0432: buf.append("\t\t\t\t\t_" + fieldNo
0433: + " = getPolyRefOID(cmd.stateFields[" + fieldNo
0434: + "], rs, firstCol);\n");
0435: int inc = ((JdbcPolyRefField) f).cols.length;
0436: if (inc == 1) {
0437: buf.append("\t\t\t\t\tfirstCol++;\n");
0438: } else {
0439: buf.append("\t\t\t\t\tfirstCol += " + inc + ";\n");
0440: }
0441:
0442: } else if (f instanceof JdbcRefField) {
0443: JdbcRefField rf = (JdbcRefField) f;
0444: String oidName = rf.targetClass.oidClassName;
0445: boolean isInHier = rf.targetClass.isInHeirachy();
0446: if (isInHier) {
0447: buf.append("\t\t\t\t\t_" + fieldNo + " = new "
0448: + oidName + "(cmd.jmd.classes["
0449: + rf.targetClass.index + "], false);\n");
0450: } else {
0451: buf.append("\t\t\t\t\t_" + fieldNo + " = new "
0452: + oidName + "();\n");
0453: }
0454: buf.append("\t\t\t\t\tif (!((JdbcOID)_" + fieldNo
0455: + ").copyKeyFields(rs, firstCol)) {\n");
0456: buf.append("\t\t\t\t\t\t_" + fieldNo + " = null;\n");
0457: buf.append("\t\t\t\t\t}\n");
0458: int inc = rf.cols.length;
0459: if (inc == 1) {
0460: buf.append("\t\t\t\t\tfirstCol++;\n");
0461: } else {
0462: buf.append("\t\t\t\t\tfirstCol += " + inc + ";\n");
0463: }
0464: }
0465: buf.append("\t\t\t\t\t" + getFilledFieldName(fieldNo) + " |= "
0466: + getFieldIndex(fieldNo) + ";\n");
0467: buf.append("\t\t\t\t\tbreak;\n\n");
0468: }
0469:
0470: private List getOracleStyleLOBFieldsMetaData() {
0471: ArrayList list = new ArrayList();
0472: FieldMetaData[] fields = cmd.stateFields;
0473: list.ensureCapacity(fields.length);
0474: for (int i = 0; i < fields.length; i++) {
0475: FieldMetaData field = fields[i];
0476: if (field.storeField != null
0477: && ((JdbcField) field.storeField)
0478: .isOracleStyleLOB()) {
0479: list.add(field);
0480: }
0481: }
0482: return list;
0483: }
0484:
0485: private void addSetOptimisticLockingParams() {
0486: StringBuffer buf = new StringBuffer();
0487: // public final int setOptimisticLockingParams(PreparedStatement ps, int firstParam) throws SQLException {
0488: // ps.setShort(firstParam++, _23);
0489: // ps.setInt(firstParam++, _0.intValue());
0490: // return firstParam;
0491: // jdbcConverter_5.set(ps, firstParam++, jdbcCol_5, _5);
0492: // }
0493:
0494: buf
0495: .append("\n\tpublic final int setOptimisticLockingParams(PreparedStatement ps, int firstParam) throws SQLException {\n");
0496: JdbcSimpleField f = ((JdbcClass) cmd.storeClass).optimisticLockingField;
0497: if (f != null) {
0498: int fieldNo = f.stateFieldNo;
0499: JdbcColumn c = f.col;
0500: Class classType = c.javaType;
0501: if (c.converter != null) {// converter
0502: buf.append("\t\t" + getConverterSet(f) + "\n");
0503: } else { // no converter
0504: if (wrapperTypesToPrimative.containsKey(classType)) { // its a wrapper class
0505: Class primType = (Class) wrapperTypesToPrimative
0506: .get(classType);
0507: String psSet = (String) typeToPreparedStatementSetField
0508: .get(primType);
0509: String toValue = (String) wrapperTypesToValue
0510: .get(classType);
0511: buf.append("\t\tps." + psSet + "(firstParam++, _"
0512: + fieldNo + "." + toValue + "());\n");
0513: } else if (classType.equals(BigInteger.class)) { // special case for BigInteger
0514: buf
0515: .append("\t\tps.setBigDecimal(firstParam++, new BigDecimal(_"
0516: + fieldNo + "));\n");
0517: } else {
0518: String psSet = (String) typeToPreparedStatementSetField
0519: .get(classType);
0520: if (psSet != null) {
0521: buf
0522: .append("\t\tps." + psSet
0523: + "(firstParam++, _" + fieldNo
0524: + ");\n");
0525: } else {
0526: buf.append("\t\tps.setObject(firstParam++, _"
0527: + fieldNo + ", " + c.jdbcType + ");\n");
0528: }
0529: }
0530: }
0531:
0532: }
0533: buf.append("\t\treturn firstParam;\n");
0534: buf.append("\t}\n");
0535: spec.addMethod(buf.toString());
0536: }
0537:
0538: protected void addCompareToPass1() {
0539: StringBuffer buf = new StringBuffer();
0540: buf
0541: .append("\n\tpublic final int compareToPass1(com.versant.core.common.State state) {\n");
0542: buf.append("\t\t" + className + " s = (" + className
0543: + ") state;\n");
0544: int[] masks = new int[getNumOfControlFields()];
0545: int num = cmd.stateFields.length;
0546: for (int i = 0; i < num; i++) {
0547: if (cmd.stateFields[i].primaryField) {
0548: int fieldNum = cmd.stateFields[i].stateFieldNo;
0549: masks[getLocalVarIndex(0, fieldNum)] += getFieldIndex(fieldNum);
0550: }
0551: }
0552: int maskLenght = masks.length;
0553: for (int i = 0; i < maskLenght; i++) {
0554: if (i == 0) {
0555: buf.append("\t\tint ans = ((filled0 & " + masks[i]
0556: + ") - (s.filled0 & " + masks[i] + "))");
0557: } else {
0558: buf
0559: .append(" + ((filled" + i + " & " + masks[i]
0560: + ") - (s.filled" + i + " & "
0561: + masks[i] + "))");
0562: }
0563:
0564: }
0565: buf.append(";\n");
0566: // do test for CLOBS here.
0567: List list = getOracleStyleLOBFieldsMetaData();
0568: if (!list.isEmpty()) {
0569: // do if stuff
0570: Iterator iter = list.iterator();
0571: while (iter.hasNext()) {
0572: FieldMetaData metaData = (FieldMetaData) iter.next();
0573: int fieldNo = metaData.stateFieldNo;
0574: // if (ans == 0 && (filled0 & 1) != 0) {
0575: // if (_0 == null && s._0 != null) {
0576: // return -1;
0577: // }
0578: // if (_0 != null && s._0 == null) {
0579: // return 1;
0580: // }
0581: // }
0582: buf.append("\t\tif (ans == 0 && ("
0583: + getFilledFieldName(fieldNo) + " & "
0584: + getFieldIndex(fieldNo) + ") != 0) {\n");
0585: buf.append("\t\t\tif (_" + fieldNo + " == null && s._"
0586: + fieldNo + " != null) {\n");
0587: buf.append("\t\t\t\treturn -1;\n");
0588: buf.append("\t\t\t}\n");
0589: buf.append("\t\t\tif (_" + fieldNo + " != null && s._"
0590: + fieldNo + " == null) {\n");
0591: buf.append("\t\t\t\treturn 1;\n");
0592: buf.append("\t\t\t}\n");
0593: buf.append("\t\t}\n");
0594: }
0595: }
0596: buf.append("\t\treturn ans;\n");
0597: buf.append("\t}\n");
0598: spec.addMethod(buf.toString());
0599: }
0600:
0601: protected void addSetParams() {
0602: StringBuffer buf = new StringBuffer();
0603: buf
0604: .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");
0605: buf
0606: .append("\t\tfor (; firstFieldNo < lastFieldNo; firstFieldNo++) {\n");
0607: buf.append("\t\t\tswitch (fieldNos[firstFieldNo]) {\n");
0608: List pass1List = getPass1FieldsMetaData();
0609: for (Iterator iter = pass1List.iterator(); iter.hasNext();) {
0610: FieldMetaData fmd = (FieldMetaData) iter.next();
0611: buf.append("\t\t\t\tcase " + fmd.stateFieldNo + ":\n");
0612: getSetFieldsToSQL_IL_CheckNull(fmd.stateFieldNo, buf);
0613: }
0614: buf.append("\t\t\t\tdefault:\n");
0615: buf.append("\t\t\t\t\tbreak;\n\n");
0616: buf.append("\t\t\t}\n");
0617: buf.append("\t\t}\n");
0618: buf.append("\t\treturn firstParam;\n");
0619: buf.append("\t}\n");
0620: spec.addMethod(buf.toString());
0621: }
0622:
0623: protected void addSetParamsChangedAndNotNull() {
0624: StringBuffer buf = new StringBuffer();
0625: buf
0626: .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");
0627: List pass1List = getPass1FieldsMetaDataWithChangedLocking();
0628: if (!pass1List.isEmpty()) {
0629: buf
0630: .append("\t\tfor (; firstFieldNo < lastFieldNo; firstFieldNo++) {\n");
0631: buf
0632: .append("\t\t\tif (!isNull(fieldNos[firstFieldNo])) {\n");
0633: buf.append("\t\t\t\tswitch (fieldNos[firstFieldNo]) {\n");
0634: for (Iterator iter = pass1List.iterator(); iter.hasNext();) {
0635: FieldMetaData fmd = (FieldMetaData) iter.next();
0636: buf
0637: .append("\t\t\t\t\tcase " + fmd.stateFieldNo
0638: + ":\n");
0639: getSetFieldsToSQL_IL(fmd.stateFieldNo, buf);
0640: }
0641: buf.append("\t\t\t\t\tdefault:\n");
0642: buf.append("\t\t\t\t\t\tbreak;\n\n");
0643: buf.append("\t\t\t\t}\n");
0644: buf.append("\t\t\t}\n");
0645: buf.append("\t\t}\n");
0646: }
0647: buf.append("\t\treturn firstParam;\n");
0648: buf.append("\t}\n");
0649: spec.addMethod(buf.toString());
0650: }
0651:
0652: protected void getSetFieldsToSQL_IL_CheckNull(int fieldNo,
0653: StringBuffer buf) {
0654: FieldMetaData fmd = cmd.stateFields[fieldNo];
0655: JdbcField field = (JdbcField) fmd.storeField;
0656: JdbcTable fieldTable = field.mainTable;
0657: JdbcTable[] tables = ((JdbcClass) cmd.storeClass).allTables;
0658: boolean isMultiTable = false;
0659: int tableNo = -1;
0660: if (tables.length > 1) {
0661: isMultiTable = true;
0662: for (int i = 0; i < tables.length; i++) {
0663: if (tables[i] == fieldTable) {
0664: tableNo = i;
0665: }
0666: }
0667: }
0668: if (field instanceof JdbcSimpleField) {
0669: JdbcColumn c = ((JdbcSimpleField) field).col;
0670: if (c.isForUpdate()) {
0671: Class fieldType = c.javaType;
0672: boolean isPrimative = c.javaType.isPrimitive();
0673: if (isPrimative) {
0674: if (isExternalized(fmd)) {
0675: isPrimative = false;
0676: }
0677: }
0678: if (c.converter != null) {// converter
0679: boolean isPrim = c.javaType.isPrimitive();
0680: if (isMultiTable) { // we have a multi table
0681: buf.append("\t\t\t\t\tif (tableNo == "
0682: + tableNo + ") {\n");
0683: if (isPrim) {
0684: buf.append("\t\t\t\t\t\tjdbcConverter_"
0685: + fieldNo
0686: + ".set(ps, firstParam++, jdbcCol_"
0687: + fieldNo
0688: + ", new "
0689: + primativeTypesToWrapper
0690: .get(fieldType) + "(_"
0691: + fieldNo + "));\n");
0692: } else {
0693: buf.append("\t\t\t\t\t\tjdbcConverter_"
0694: + fieldNo
0695: + ".set(ps, firstParam++, jdbcCol_"
0696: + fieldNo + ", _" + fieldNo
0697: + ");\n");
0698: }
0699: buf.append("\t\t\t\t\t}\n");
0700: } else {
0701: if (isPrim) {
0702: buf.append("\t\t\t\t\tjdbcConverter_"
0703: + fieldNo
0704: + ".set(ps, firstParam++, jdbcCol_"
0705: + fieldNo
0706: + ", new "
0707: + primativeTypesToWrapper
0708: .get(fieldType) + "(_"
0709: + fieldNo + "));\n");
0710: } else {
0711: buf.append("\t\t\t\t\tjdbcConverter_"
0712: + fieldNo
0713: + ".set(ps, firstParam++, jdbcCol_"
0714: + fieldNo + ", _" + fieldNo
0715: + ");\n");
0716: }
0717: }
0718: } else {
0719: if (isPrimative) {
0720: // this is a temp var
0721: if (isMultiTable) { // we have a multi table
0722: buf.append("\t\t\t\t\tif (tableNo == "
0723: + tableNo + ") {\n");
0724: buf.append("\t\t\t\t\t\tps."
0725: + typeToPreparedStatementSetField
0726: .get(fieldType)
0727: + "(firstParam++, _" + fieldNo
0728: + ");\n");
0729: buf.append("\t\t\t\t\t}\n");
0730: } else {
0731: buf.append("\t\t\t\t\tps."
0732: + typeToPreparedStatementSetField
0733: .get(fieldType)
0734: + "(firstParam++, _" + fieldNo
0735: + ");\n");
0736: }
0737: } else if (wrapperTypesToPrimative
0738: .containsKey(fieldType)) { // its a wrapper class
0739: String toVal = (String) wrapperTypesToValue
0740: .get(fieldType);
0741: Class primType = (Class) wrapperTypesToPrimative
0742: .get(fieldType);
0743: String psSet = (String) typeToPreparedStatementSetField
0744: .get(primType);
0745: if (isMultiTable) { // we have a multi table
0746: // if (_8 != null) {
0747: // ps.setInt(firstParam++, _8.intValue());
0748: // } else {
0749: // ps.setNull(firstParam++, 4);
0750: // }
0751:
0752: buf.append("\t\t\t\t\tif (tableNo == "
0753: + tableNo + ") {\n");
0754: buf.append("\t\t\t\t\t\tif (_" + fieldNo
0755: + " != null) {\n");
0756: buf.append("\t\t\t\t\t\t\tps." + psSet
0757: + "(firstParam++, _" + fieldNo
0758: + "." + toVal + "());\n");
0759: buf.append("\t\t\t\t\t\t} else {\n");
0760: buf
0761: .append("\t\t\t\t\t\t\tps.setNull(firstParam++, "
0762: + c.jdbcType + ");\n");
0763: buf.append("\t\t\t\t\t\t}\n");
0764: buf.append("\t\t\t\t\t}\n");
0765: } else {
0766: buf.append("\t\t\t\t\tif (_" + fieldNo
0767: + " != null) {\n");
0768: buf.append("\t\t\t\t\t\tps." + psSet
0769: + "(firstParam++, _" + fieldNo
0770: + "." + toVal + "());\n");
0771: buf.append("\t\t\t\t\t} else {\n");
0772: buf
0773: .append("\t\t\t\t\t\tps.setNull(firstParam++, "
0774: + c.jdbcType + ");\n");
0775: buf.append("\t\t\t\t\t}\n");
0776: }
0777: } else if (fieldType
0778: .equals(java.math.BigInteger.class)) { // special case for BigInteger
0779: // if (_4 != null) {
0780: // ps.setBigDecimal(firstParam++, new BigDecimal(_4));
0781: // } else {
0782: // ps.setNull(firstParam++, 2);
0783: // }
0784: if (isMultiTable) { // we have a multi table
0785: buf.append("\t\t\t\t\tif (tableNo == "
0786: + tableNo + ") {\n");
0787: buf.append("\t\t\t\t\t\tif (_" + fieldNo
0788: + " != null) {\n");
0789: buf
0790: .append("\t\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_"
0791: + fieldNo + "));\n");
0792: buf.append("\t\t\t\t\t\t} else {\n");
0793: buf
0794: .append("\t\t\t\t\t\t\tps.setNull(firstParam++, "
0795: + c.jdbcType + ");\n");
0796: buf.append("\t\t\t\t\t\t}\n");
0797: buf.append("\t\t\t\t\t}\n");
0798: } else {
0799: buf.append("\t\t\t\t\tif (_" + fieldNo
0800: + " != null) {\n");
0801: buf
0802: .append("\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_"
0803: + fieldNo + "));\n");
0804: buf.append("\t\t\t\t\t} else {\n");
0805: buf
0806: .append("\t\t\t\t\t\tps.setNull(firstParam++, "
0807: + c.jdbcType + ");\n");
0808: buf.append("\t\t\t\t\t}\n");
0809: }
0810: } else {
0811: // if (_3 != null) {
0812: // ps.setBigDecimal(firstParam++, _3);
0813: // } else {
0814: // ps.setNull(firstParam++, 2);
0815: // }
0816: String psSet = (String) typeToPreparedStatementSetField
0817: .get(fieldType);
0818: if (isExternalized(fmd)) {
0819: psSet = null;
0820: }
0821: if (isMultiTable) { // we have a multi table
0822: buf.append("\t\t\t\t\tif (tableNo == "
0823: + tableNo + ") {\n");
0824: buf.append("\t\t\t\t\t\tif (_" + fieldNo
0825: + " != null) {\n");
0826: buf
0827: .append("\t\t\t\t\t\t\tps."
0828: + (psSet != null ? psSet
0829: : "setObject")
0830: + "(firstParam++, "
0831: + ((isExternalized(fmd) && psSet != null) ? "("
0832: + fieldType
0833: .getName()
0834: + ")"
0835: : "")
0836: + "_"
0837: + fieldNo
0838: + (psSet != null ? ""
0839: : (", " + c.jdbcType))
0840: + ");\n");
0841: buf.append("\t\t\t\t\t\t} else {\n");
0842: buf
0843: .append("\t\t\t\t\t\t\tps.setNull(firstParam++, "
0844: + c.jdbcType + ");\n");
0845: buf.append("\t\t\t\t\t\t}\n");
0846: buf.append("\t\t\t\t\t}\n");
0847: } else {
0848: buf.append("\t\t\t\t\tif (_" + fieldNo
0849: + " != null) {\n");
0850: buf
0851: .append("\t\t\t\t\t\tps."
0852: + (psSet != null ? psSet
0853: : "setObject")
0854: + "(firstParam++, "
0855: + ((isExternalized(fmd) && psSet != null) ? "("
0856: + fieldType
0857: .getName()
0858: + ")"
0859: : "")
0860: + "_"
0861: + fieldNo
0862: + (psSet != null ? ""
0863: : (", " + c.jdbcType))
0864: + ");\n");
0865: buf.append("\t\t\t\t\t} else {\n");
0866: buf
0867: .append("\t\t\t\t\t\tps.setNull(firstParam++, "
0868: + c.jdbcType + ");\n");
0869: buf.append("\t\t\t\t\t}\n");
0870: }
0871: }
0872: }
0873: }
0874: } else if (field instanceof JdbcPolyRefField) {
0875: // firstParam = State.setPolyRefData(cmd.stateFields[3], (OID) _3, cmd, ps, firstParam);
0876: if (isMultiTable) { // we have a multi table
0877: buf.append("\t\t\t\t\tif (tableNo == " + tableNo
0878: + ") {\n");
0879: buf
0880: .append("\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields["
0881: + fieldNo
0882: + "], (OID) _"
0883: + fieldNo
0884: + ", cmd, ps, firstParam);\n");
0885: buf.append("\t\t\t\t\t}\n");
0886: } else {
0887: buf
0888: .append("\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields["
0889: + fieldNo
0890: + "], (OID) _"
0891: + fieldNo
0892: + ", cmd, ps, firstParam);\n");
0893: }
0894: } else if (field instanceof JdbcRefField) {
0895: if (isMultiTable) { // we have a multi table
0896: buf.append("\t\t\t\t\tif (tableNo == " + tableNo
0897: + ") {\n");
0898: } else {
0899: buf.append("\t\t\t\t\t{\n");
0900: }
0901: buf
0902: .append("\t\t\t\t\t\tOID oid = (OID)_" + fieldNo
0903: + ";\n");
0904: buf
0905: .append("\t\t\t\t\t\tif (oid == null || (oid = oid.getRealOID()) == null) {\n");
0906: JdbcColumn[] cols = field.mainTableCols;
0907: int nc = cols.length;
0908: for (int j = 0; j < nc; j++) {
0909: JdbcColumn col = cols[j];
0910: if (col.isForUpdate()) {
0911: buf
0912: .append("\t\t\t\t\t\t\tps.setNull(firstParam++, "
0913: + col.jdbcType + ");\n");
0914: }
0915: }
0916: buf.append("\t\t\t\t\t\t} else {\n");
0917: buf
0918: .append("\t\t\t\t\t\t\tfirstParam = ((JdbcOID)oid).setParams(ps, firstParam, ((JdbcField)cmd.stateFields["
0919: + fieldNo
0920: + "].storeField).mainTableCols);\n");
0921: buf.append("\t\t\t\t\t\t}\n");
0922: buf.append("\t\t\t\t\t}\n");
0923: }
0924: buf.append("\t\t\t\t\tbreak;\n\n");
0925: }
0926:
0927: protected void getSetFieldsToSQL_IL(int fieldNo, StringBuffer buf) {
0928: FieldMetaData fmd = cmd.stateFields[fieldNo];
0929: JdbcField field = (JdbcField) fmd.storeField;
0930: JdbcTable fieldTable = field.mainTable;
0931: JdbcTable[] tables = ((JdbcClass) cmd.storeClass).allTables;
0932: boolean isMultiTable = false;
0933: int tableNo = -1;
0934: if (tables.length > 1) {
0935: isMultiTable = true;
0936: for (int i = 0; i < tables.length; i++) {
0937: if (tables[i] == fieldTable) {
0938: tableNo = i;
0939: }
0940: }
0941: }
0942: if (field instanceof JdbcSimpleField) {
0943: JdbcColumn c = ((JdbcSimpleField) field).col;
0944: if (c.isForUpdate()) {
0945: Class fieldType = c.javaType;
0946: boolean isPrimative = c.javaType.isPrimitive();
0947: if (isPrimative) {
0948: if (isExternalized(fmd)) {
0949: isPrimative = false;
0950: }
0951: }
0952: if (c.converter != null) {// converter
0953: boolean isPrim = c.javaType.isPrimitive();
0954: if (isMultiTable) { // we have a multi table
0955: buf.append("\t\t\t\t\t\tif (tableNo == "
0956: + tableNo + ") {\n");
0957: if (isPrim) {
0958: buf.append("\t\t\t\t\t\t\tjdbcConverter_"
0959: + fieldNo
0960: + ".set(ps, firstParam++, jdbcCol_"
0961: + fieldNo
0962: + ", new "
0963: + primativeTypesToWrapper
0964: .get(fieldType) + "(_"
0965: + fieldNo + "));\n");
0966: } else {
0967: buf.append("\t\t\t\t\t\t\tjdbcConverter_"
0968: + fieldNo
0969: + ".set(ps, firstParam++, jdbcCol_"
0970: + fieldNo + ", _" + fieldNo
0971: + ");\n");
0972: }
0973: buf.append("\t\t\t\t\t\t}\n");
0974: } else {
0975: if (isPrim) {
0976: buf.append("\t\t\t\t\t\tjdbcConverter_"
0977: + fieldNo
0978: + ".set(ps, firstParam++, jdbcCol_"
0979: + fieldNo
0980: + ", new "
0981: + primativeTypesToWrapper
0982: .get(fieldType) + "(_"
0983: + fieldNo + "));\n");
0984: } else {
0985: buf.append("\t\t\t\t\t\tjdbcConverter_"
0986: + fieldNo
0987: + ".set(ps, firstParam++, jdbcCol_"
0988: + fieldNo + ", _" + fieldNo
0989: + ");\n");
0990: }
0991: }
0992: } else {
0993: if (isPrimative) {
0994: if (isMultiTable) { // we have a multi table
0995: buf.append("\t\t\t\t\t\tif (tableNo == "
0996: + tableNo + ") {\n");
0997: buf.append("\t\t\t\t\t\t\tps."
0998: + typeToPreparedStatementSetField
0999: .get(fieldType)
1000: + "(firstParam++, _" + fieldNo
1001: + ");\n");
1002: buf.append("\t\t\t\t\t\t}\n");
1003: } else {
1004: buf.append("\t\t\t\t\t\tps."
1005: + typeToPreparedStatementSetField
1006: .get(fieldType)
1007: + "(firstParam++, _" + fieldNo
1008: + ");\n");
1009: }
1010: } else if (wrapperTypesToPrimative
1011: .containsKey(fieldType)) { // its a wrapper class
1012: String toVal = (String) wrapperTypesToValue
1013: .get(fieldType);
1014: Class primType = (Class) wrapperTypesToPrimative
1015: .get(fieldType);
1016: String psSet = (String) typeToPreparedStatementSetField
1017: .get(primType);
1018: if (isMultiTable) { // we have a multi table
1019: // ps.setInt(firstParam++, _8.intValue());
1020: buf.append("\t\t\t\t\t\tif (tableNo == "
1021: + tableNo + ") {\n");
1022: buf.append("\t\t\t\t\t\t\tps." + psSet
1023: + "(firstParam++, _" + fieldNo
1024: + "." + toVal + "());\n");
1025: buf.append("\t\t\t\t\t\t}\n");
1026: } else {
1027: buf.append("\t\t\t\t\t\tps." + psSet
1028: + "(firstParam++, _" + fieldNo
1029: + "." + toVal + "());\n");
1030: }
1031: } else if (fieldType
1032: .equals(java.math.BigInteger.class)) { // special case for BigInteger
1033: // ps.setBigDecimal(firstParam++, new BigDecimal(_4));
1034: if (isMultiTable) { // we have a multi table
1035: buf.append("\t\t\t\t\t\tif (tableNo == "
1036: + tableNo + ") {\n");
1037: buf
1038: .append("\t\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_"
1039: + fieldNo + "));\n");
1040: buf.append("\t\t\t\t\t\t}\n");
1041: } else {
1042: buf
1043: .append("\t\t\t\t\t\tps.setBigDecimal(firstParam++, new java.math.BigDecimal(_"
1044: + fieldNo + "));\n");
1045: }
1046: } else {
1047: // ps.setBigDecimal(firstParam++, _3);
1048: String psSet = (String) typeToPreparedStatementSetField
1049: .get(fieldType);
1050: if (isExternalized(fmd)) {
1051: psSet = null;
1052: }
1053: if (isMultiTable) { // we have a multi table
1054: buf.append("\t\t\t\t\t\tif (tableNo == "
1055: + tableNo + ") {\n");
1056: buf
1057: .append("\t\t\t\t\t\t\tps."
1058: + (psSet != null ? psSet
1059: : "setObject")
1060: + "(firstParam++, "
1061: + ((isExternalized(fmd) && psSet != null) ? "("
1062: + fieldType
1063: .getName()
1064: + ")"
1065: : "")
1066: + "_"
1067: + fieldNo
1068: + (psSet != null ? ""
1069: : (", " + c.jdbcType))
1070: + ");\n");
1071: buf.append("\t\t\t\t\t\t}\n");
1072: } else {
1073: buf
1074: .append("\t\t\t\t\t\tps."
1075: + (psSet != null ? psSet
1076: : "setObject")
1077: + "(firstParam++, "
1078: + ((isExternalized(fmd) && psSet != null) ? "("
1079: + fieldType
1080: .getName()
1081: + ")"
1082: : "")
1083: + "_"
1084: + fieldNo
1085: + (psSet != null ? ""
1086: : (", " + c.jdbcType))
1087: + ");\n");
1088:
1089: }
1090: }
1091: }
1092: }
1093: } else if (field instanceof JdbcPolyRefField) {
1094: // firstParam = State.setPolyRefData(cmd.stateFields[3], (OID) _3, cmd, ps, firstParam);
1095: if (isMultiTable) { // we have a multi table
1096: buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo
1097: + ") {\n");
1098: buf
1099: .append("\t\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields["
1100: + fieldNo
1101: + "], (OID) _"
1102: + fieldNo
1103: + ", cmd, ps, firstParam);\n");
1104: buf.append("\t\t\t\t\t\t}\n");
1105: } else {
1106: buf
1107: .append("\t\t\t\t\t\t\tfirstParam = setPolyRefData(cmd.stateFields["
1108: + fieldNo
1109: + "], (OID) _"
1110: + fieldNo
1111: + ", cmd, ps, firstParam);\n");
1112: }
1113: } else if (field instanceof JdbcRefField) {
1114: // firstParam = ((OID)_1).setParams(ps, firstParam, cmd.stateFields[1].storeField.mainTableCols);
1115: if (isMultiTable) { // we have a multi table
1116: buf.append("\t\t\t\t\t\tif (tableNo == " + tableNo
1117: + ") {\n");
1118: buf
1119: .append("\t\t\t\t\t\t\tfirstParam = ((JdbcOID)_"
1120: + fieldNo
1121: + ").setParams(ps, firstParam, ((JdbcField)cmd.stateFields["
1122: + fieldNo
1123: + "].storeField).mainTableCols);\n");
1124: buf.append("\t\t\t\t\t\t}\n");
1125: } else {
1126: buf
1127: .append("\t\t\t\t\t\tfirstParam = ((JdbcOID)_"
1128: + fieldNo
1129: + ").setParams(ps, firstParam, ((JdbcField)cmd.stateFields["
1130: + fieldNo
1131: + "].storeField).mainTableCols);\n");
1132: }
1133: }
1134: buf.append("\t\t\t\t\t\tbreak;\n\n");
1135: }
1136:
1137: protected void addContainsValidAppIdFields() {
1138: StringBuffer buf = new StringBuffer();
1139: buf
1140: .append("\n\tpublic final boolean containsValidAppIdFields() {\n");
1141: if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
1142: FieldMetaData[] pkf = cmd.pcHeirachy[0].pkFields;
1143: for (int i = 0; i < pkf.length; i++) {
1144: FieldMetaData fmd = pkf[i];
1145: int pkFieldNum = fmd.stateFieldNo;
1146: Class fieldType = fmd.type;
1147: boolean isPrimative = fmd.type.isPrimitive();
1148:
1149: if (isPrimative) {
1150: if (fieldType.equals(int.class)) {
1151: Integer intt = (Integer) fmd
1152: .getPKDefaultValue();
1153: buf.append("\t\tif (("
1154: + getFilledFieldName(pkFieldNum)
1155: + " & " + getFieldIndex(pkFieldNum)
1156: + ") == 0 || _" + pkFieldNum + " == "
1157: + intt.intValue() + ") {\n");
1158: } else if (fieldType.equals(long.class)) {
1159: Long longg = (Long) fmd.getPKDefaultValue();
1160: buf.append("\t\tif (("
1161: + getFilledFieldName(pkFieldNum)
1162: + " & " + getFieldIndex(pkFieldNum)
1163: + ") == 0 || _" + pkFieldNum + " == "
1164: + longg.longValue() + ") {\n");
1165: } else if (fieldType.equals(boolean.class)) {
1166: Boolean bool = (Boolean) fmd
1167: .getPKDefaultValue();
1168: if (bool.booleanValue()) {
1169: buf.append("\t\tif (("
1170: + getFilledFieldName(pkFieldNum)
1171: + " & " + getFieldIndex(pkFieldNum)
1172: + ") == 0 || _" + pkFieldNum
1173: + ") {\n");
1174: } else {
1175: buf.append("\t\tif (("
1176: + getFilledFieldName(pkFieldNum)
1177: + " & " + getFieldIndex(pkFieldNum)
1178: + ") == 0 || !_" + pkFieldNum
1179: + ") {\n");
1180: }
1181: } else if (fieldType.equals(double.class)) {
1182: Double doubl = (Double) fmd.getPKDefaultValue();
1183: buf.append("\t\tif (("
1184: + getFilledFieldName(pkFieldNum)
1185: + " & " + getFieldIndex(pkFieldNum)
1186: + ") == 0 || _" + pkFieldNum + " == "
1187: + doubl.doubleValue() + ") {\n");
1188: } else if (fieldType.equals(float.class)) {
1189: Float floatt = (Float) fmd.getPKDefaultValue();
1190: buf.append("\t\tif (("
1191: + getFilledFieldName(pkFieldNum)
1192: + " & " + getFieldIndex(pkFieldNum)
1193: + ") == 0 || _" + pkFieldNum + " == "
1194: + floatt.doubleValue() + ") {\n");
1195: } else if (fieldType.equals(short.class)) {
1196: Short shortt = (Short) fmd.getPKDefaultValue();
1197: buf.append("\t\tif (("
1198: + getFilledFieldName(pkFieldNum)
1199: + " & " + getFieldIndex(pkFieldNum)
1200: + ") == 0 || _" + pkFieldNum + " == "
1201: + shortt.shortValue() + ") {\n");
1202: } else if (fieldType.equals(char.class)) {
1203: Character charr = (Character) fmd
1204: .getPKDefaultValue();
1205: buf.append("\t\tif (("
1206: + getFilledFieldName(pkFieldNum)
1207: + " & " + getFieldIndex(pkFieldNum)
1208: + ") == 0 || _" + pkFieldNum + " == "
1209: + charr.charValue() + ") {\n");
1210: } else if (fieldType.equals(byte.class)) {
1211: Byte bytee = (Byte) fmd.getPKDefaultValue();
1212: buf.append("\t\tif (("
1213: + getFilledFieldName(pkFieldNum)
1214: + " & " + getFieldIndex(pkFieldNum)
1215: + ") == 0 || _" + pkFieldNum + " == "
1216: + bytee.byteValue() + ") {\n");
1217: } else {
1218: throw BindingSupportImpl
1219: .getInstance()
1220: .invalidOperation(
1221: "Unsupported type " + fieldType);
1222: }
1223: buf.append("\t\t\treturn false;\n");
1224: buf.append("\t\t}\n");
1225: } else {
1226: buf.append("\t\tif (("
1227: + getFilledFieldName(pkFieldNum) + " & "
1228: + getFieldIndex(pkFieldNum) + ") == 0 || _"
1229: + pkFieldNum + " == null || _" + pkFieldNum
1230: + ".equals(cmd.pcHeirachy[0].pkFields[" + i
1231: + "].getPKDefaultValue())) {\n");
1232: buf.append("\t\t\treturn false;\n");
1233: buf.append("\t\t}\n");
1234: }
1235: }
1236: buf.append("\t\treturn true;\n");
1237: } else if (cmd.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) {
1238: buf.append("\t\treturn false;\n");
1239: }
1240: buf.append("\t}\n");
1241: spec.addMethod(buf.toString());
1242: }
1243:
1244: /**
1245: * Add all PC fields in the hier to state and
1246: * private boolean isDirty;
1247: * private boolean[] dirtyFields = new boolean[20];
1248: * private boolean[] filled = new boolean[20];
1249: */
1250: protected void addFields() {
1251: super .addFields();
1252: FieldMetaData[] fields = cmd.stateFields;
1253: for (int i = 0; i < fields.length; i++) {
1254: JdbcField f = (JdbcField) fields[i].storeField;
1255: if (f instanceof JdbcSimpleField) {
1256: JdbcSimpleField sf = (JdbcSimpleField) f;
1257: if (sf.col.converter != null) {
1258: spec.addField("public static "
1259: + sf.col.converter.getClass().getName()
1260: + " " + JDBC_CONVERTER_FIELD_PREFIX + i);
1261: spec.addField("public static "
1262: + sf.col.getClass().getName() + " "
1263: + "jdbcCol_" + i);
1264: }
1265: }
1266: }
1267: }
1268:
1269: protected void addCopyFields() {
1270: StringBuffer buf = new StringBuffer();
1271: buf.append("\n\tpublic final void copyFields(OID oid) {\n");
1272: ClassMetaData currentCMD = null;
1273: if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
1274: currentCMD = getTopPCSuperClassMetaData();
1275: buf.append("\t\t" + currentCMD.oidClassName + " id = ("
1276: + currentCMD.oidClassName + ")oid;\n");
1277: FieldMetaData[] fmds = currentCMD.pkFields;
1278: for (int i = 0; i < fmds.length; i++) {
1279: FieldMetaData fmd = fmds[i];
1280: int stateFieldNum = fmd.stateFieldNo;
1281: buf.append("\t\t_" + stateFieldNum + " = id._" + i
1282: + ";\n");
1283: buf
1284: .append("\t\t"
1285: + getFilledFieldName(stateFieldNum)
1286: + " |= " + getFieldIndex(stateFieldNum)
1287: + ";\n");
1288: }
1289: }
1290: buf.append("\t}\n");
1291: spec.addMethod(buf.toString());
1292: }
1293:
1294: protected void addHasSameNullFields() {
1295: StringBuffer buf = new StringBuffer();
1296: buf
1297: .append("\n\tpublic final boolean hasSameNullFields(State state, State mask) {\n");
1298: JdbcClass jc = (JdbcClass) cmd.storeClass;
1299: if (jc.optimisticLocking != JdbcClass.OPTIMISTIC_LOCKING_CHANGED) {
1300: buf.append("\t\treturn true;\n");
1301: } else {
1302: buf.append("\t\t" + className + " s = (" + className
1303: + ") state;\n");
1304: buf.append("\t\t" + className + " ms = (" + className
1305: + ") mask;\n");
1306: int num = getNumOfControlFields();
1307: for (int i = 0; i < num; i++) {
1308: if (i == 0) {
1309: buf.append("\t\tint filledMask = ms.filled0;\n");
1310: } else {
1311: buf
1312: .append("\t\tfilledMask = ms.filled" + i
1313: + ";\n");
1314: }
1315: List fields = getRealObjectFields(i);
1316: for (Iterator iter = fields.iterator(); iter.hasNext();) {
1317: FieldMetaData fmd = (FieldMetaData) iter.next();
1318: int fieldNum = getFieldNo(fmd);
1319: int index = getFieldIndex(fieldNum);
1320: buf.append("\t\tif ((filledMask & " + index
1321: + ") != 0 && (_" + fieldNum
1322: + " == null) != (s._" + fieldNum
1323: + " == null)) {return false;}\n");
1324: }
1325: }
1326: buf.append("\t\treturn true;\n");
1327: }
1328: buf.append("\t}\n");
1329: spec.addMethod(buf.toString());
1330: }
1331:
1332: protected void addCopyKeyFieldsUpdate() {
1333: StringBuffer buf = new StringBuffer();
1334: buf
1335: .append("\n\tpublic final void copyKeyFieldsUpdate(OID oid) {\n");
1336: if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
1337: buf.append("\t\t" + cmd.oidClassName + " other = ("
1338: + cmd.oidClassName + ") oid;\n");
1339: FieldMetaData[] pkFields = cmd.pkFields;
1340: JdbcColumn[] pkc = ((JdbcClass) cmd.storeClass).table.pk;
1341: for (int i = 0; i < pkc.length; i++) {
1342: int stateFieldNo = pkFields[i].stateFieldNo;
1343: buf.append("\t\tif (containsField(" + stateFieldNo
1344: + ")) {\n");
1345: buf.append("\t\t\tother._" + i + " = _" + stateFieldNo
1346: + ";\n");
1347: buf.append("\t\t}\n");
1348: }
1349: }
1350: buf.append("\t}\n");
1351: spec.addMethod(buf.toString());
1352: }
1353:
1354: protected void addCopyKeyFieldsFromOID() {
1355: StringBuffer buf = new StringBuffer();
1356: buf.append("\n\tpublic final void copyKeyFields(OID oid) {\n");
1357: if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
1358: buf.append("\t\t" + cmd.oidClassName + " other = ("
1359: + cmd.oidClassName + ") oid;\n");
1360: FieldMetaData[] pkFields = cmd.pkFields;
1361: JdbcColumn[] pkc = ((JdbcClass) cmd.storeClass).table.pk;
1362: for (int i = 0; i < pkc.length; i++) {
1363: buf.append("\t\tother._" + i + " = _"
1364: + pkFields[i].stateFieldNo + ";\n");
1365: }
1366: }
1367: buf.append("\t}\n");
1368: spec.addMethod(buf.toString());
1369: }
1370:
1371: protected void addCheckKeyFields() {
1372: StringBuffer buf = new StringBuffer();
1373: buf
1374: .append("\n\tpublic final boolean checkKeyFields(OID oid) {\n");
1375: if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
1376: buf.append("\t\t" + cmd.oidClassName + " other = ("
1377: + cmd.oidClassName + ") oid;\n");
1378: FieldMetaData[] pkFields = cmd.pkFields;
1379: JdbcColumn[] pkc = ((JdbcClass) cmd.storeClass).table.pk;
1380: for (int i = 0; i < pkc.length; i++) {
1381: Class classType = pkc[i].javaType;
1382: boolean isPrimitive = classType.isPrimitive();
1383: if (isPrimitive) {
1384: buf.append("\t\tif (other._" + i + " != _"
1385: + pkFields[i].stateFieldNo + ") {\n");
1386: buf.append("\t\t\treturn false;\n");
1387: buf.append("\t\t}\n");
1388: } else {
1389: buf.append("\t\tif (!other._" + i + ".equals(_"
1390: + pkFields[i].stateFieldNo + ")) {\n");
1391: buf.append("\t\t\treturn false;\n");
1392: buf.append("\t\t}\n");
1393: }
1394: }
1395: }
1396: buf.append("\t\treturn true;\n");
1397: buf.append("\t}\n");
1398: spec.addMethod(buf.toString());
1399: }
1400:
1401: protected void addClearApplicationIdentityFields() {
1402: StringBuffer buf = new StringBuffer();
1403: buf
1404: .append("\n\tpublic final void clearApplicationIdentityFields() {\n");
1405: if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
1406: int[] masks = new int[getNumOfControlFields()];
1407: FieldMetaData[] pkf = cmd.pcHeirachy[0].pkFields;
1408: for (int i = pkf.length - 1; i >= 0; i--) {
1409: int fieldNum = pkf[i].fieldNo;
1410: masks[getLocalVarIndex(0, fieldNum)] += getFieldIndex(fieldNum);
1411: }
1412: int maskLength = masks.length;
1413: for (int i = 0; i < maskLength; i++) {
1414: if (masks[i] != 0) {
1415: buf.append("\t\tfilled" + i + " = filled" + i
1416: + " & " + (masks[i] ^ 0xFFFFFFFF) + ";\n");
1417: }
1418: }
1419: }
1420: buf.append("\t}\n");
1421: spec.addMethod(buf.toString());
1422: }
1423:
1424: protected void addContainsApplicationIdentityFields() {
1425: StringBuffer buf = new StringBuffer();
1426: buf
1427: .append("\n\tpublic final boolean containsApplicationIdentityFields() {\n");
1428: if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
1429: FieldMetaData[] pkf = cmd.pcHeirachy[0].pkFields;
1430: for (int i = 0; i < pkf.length; i++) {
1431: int pkFieldNum = pkf[i].stateFieldNo;
1432: buf.append("\t\tif ((" + getFilledFieldName(pkFieldNum)
1433: + " & " + getFieldIndex(pkFieldNum)
1434: + ") == 0) {\n");
1435: buf.append("\t\t\treturn false;\n");
1436: buf.append("\t\t}\n");
1437: }
1438: buf.append("\t\treturn true;\n");
1439: } else {
1440: buf.append("\t\treturn false;\n");
1441: }
1442: buf.append("\t}\n");
1443: spec.addMethod(buf.toString());
1444: }
1445:
1446: protected List getPass1FieldsMetaDataWithChangedLocking() {
1447: ArrayList list = new ArrayList();
1448: FieldMetaData[] fields = cmd.stateFields;
1449: list.ensureCapacity(fields.length);
1450: for (int i = 0; i < fields.length; i++) {
1451: FieldMetaData field = fields[i];
1452: if (field.primaryField) {
1453: if (((JdbcField) field.storeField).includeForChangedLocking) {
1454: list.add(field);
1455: }
1456: }
1457: }
1458: return list;
1459: }
1460:
1461: }
|