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.OIDSrcGenerator;
0014: import com.versant.core.metadata.ModelMetaData;
0015: import com.versant.core.metadata.ClassMetaData;
0016: import com.versant.core.metadata.MDStatics;
0017: import com.versant.core.metadata.FieldMetaData;
0018: import com.versant.core.common.BindingSupportImpl;
0019: import com.versant.core.compiler.ClassSpec;
0020: import com.versant.core.jdbc.metadata.JdbcColumn;
0021: import com.versant.core.jdbc.metadata.JdbcField;
0022: import com.versant.core.jdbc.metadata.JdbcClass;
0023:
0024: import java.sql.ResultSet;
0025: import java.sql.PreparedStatement;
0026: import java.sql.SQLException;
0027:
0028: /**
0029: * Adds JDBC specific stuff to OID.
0030: */
0031: public class JdbcOIDGenerator extends OIDSrcGenerator {
0032:
0033: public JdbcOIDGenerator(ModelMetaData jmd) {
0034: super (jmd);
0035: }
0036:
0037: public ClassSpec generateOID(ClassMetaData cmd) {
0038: ClassSpec spec = super .generateOID(cmd);
0039:
0040: spec.addImport(JdbcOID.class.getName());
0041: spec.addImport(ResultSet.class.getName());
0042: spec.addImport(PreparedStatement.class.getName());
0043: spec.addImport(SQLException.class.getName());
0044: spec.addImport(JdbcColumn.class.getName());
0045: spec.addImport(JdbcUtils.class.getName());
0046: spec.addImport(JdbcField.class.getName());
0047: spec.addImport(JdbcClass.class.getName());
0048:
0049: spec.addInterface("JdbcOID");
0050:
0051: addCopyKeyFields();
0052: addSetParams();
0053: addSetParams2();
0054: addValidateKeyFields();
0055: addCopyKeyFields2();
0056:
0057: return spec;
0058: }
0059:
0060: protected void addInitStaticsBody(StringBuffer buf) {
0061: super .addInitStaticsBody(buf);
0062: // early exit from method if storeClass is null (i.e. remote PMF)
0063: if (cmd.isInHeirachy()) {
0064: buf
0065: .append("\t\tif (top.storeClass == null) return true;\n");
0066: } else {
0067: buf
0068: .append("\t\tif (cmd.storeClass == null) return true;\n");
0069: }
0070: JdbcColumn[] pkc = ((JdbcClass) cmd.top.storeClass).table.pk;
0071: boolean first = true;
0072: for (int j = 0; j < pkc.length; j++) {
0073: if (pkc[j].converter != null) {
0074: if (first) {
0075: buf.append("\t\tClassMetaData t = jmd.classes["
0076: + currentCMD.top.index + "];\n");
0077: buf
0078: .append("\t\tJdbcColumn[] pkc = ((JdbcClass)t.storeClass).table.pk;\n");
0079: first = false;
0080: }
0081: buf.append("\t\t" + JDBC_CONVERTER_FIELD_PREFIX + j
0082: + " = ("
0083: + pkc[j].converter.getClass().getName()
0084: + ")pkc[" + j + "].converter;\n");
0085: buf.append("\t\tjdbcCol_" + j + " = pkc[" + j + "];\n");
0086: }
0087: }
0088: }
0089:
0090: protected void addPopulateObjectIdClassInstance() {
0091: StringBuffer buf = new StringBuffer();
0092: buf
0093: .append("\n\tpublic void populateObjectIdClassInstance(Object o) {\n");
0094: if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
0095: if (cmd.top.objectIdClass != null) {
0096: String oidClassName = cmd.top.objectIdClass.getName()
0097: .replace('$', '.');
0098: buf.append("\t\t" + oidClassName + " pk = ("
0099: + oidClassName + ")o;\n");
0100: FieldMetaData[] fields = cmd.pkFields;
0101: for (int i = 0; i < fields.length; i++) {
0102: FieldMetaData field = fields[i];
0103: buf.append("\t\tpk.");
0104: buf.append(field.getPkFieldName());
0105: buf.append(" = ");
0106: buf.append(getFieldName(i));
0107: buf.append(";\n");
0108: }
0109: }
0110: }
0111: buf.append("\t}\n");
0112: spec.addMethod(buf.toString());
0113: }
0114:
0115: private void addValidateKeyFields() {
0116: StringBuffer buf = new StringBuffer();
0117: /*
0118: public boolean validateKeyFields(ResultSet rs, int firstCol) throws SQLException {
0119: rs.getInt(firstCol++);
0120: return !rs.wasNull();
0121: }
0122: */
0123: buf
0124: .append("\n\tpublic boolean validateKeyFields(ResultSet rs, int firstCol) throws SQLException {\n");
0125: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
0126: for (int i = 0; i < pkc.length; i++) {
0127: Class fieldType = pkc[i].javaType;
0128: if (pkc[i].converter != null) { // converter
0129: /*
0130: jdbcConverter_2.get(rs, firstCol++, jdbcCol_2);
0131: if (rs.wasNull())return false;
0132: */
0133: buf.append("\t\t");
0134: buf.append("jdbcConverter_" + i);
0135: buf.append(".get(rs,firstCol++, jdbcCol_" + i);
0136: buf.append(");\n\t\t");
0137: buf.append("if (rs.wasNull())return false;\n");
0138:
0139: } else { // no converter
0140: boolean isWrapper = wrapperTypesToPrimitive.keySet()
0141: .contains(fieldType);
0142: if (isWrapper) {
0143: /*
0144: rs.getShort(firstCol++);
0145: if (rs.wasNull()) return false;
0146: */
0147:
0148: buf.append("\t\trs.");
0149: buf
0150: .append((String) typeToResultSetGetField
0151: .get(wrapperTypesToPrimitive
0152: .get(fieldType)));
0153: buf.append("(firstCol++);\n");
0154: } else {
0155: /*
0156: rs.getString(firstCol++);
0157: if (rs.wasNull()) return false;
0158: */
0159: buf.append("\t\trs.");
0160: buf.append((String) typeToResultSetGetField
0161: .get(fieldType));
0162: buf.append("(firstCol++);\n");
0163:
0164: }
0165: buf.append("\t\tif (rs.wasNull())return false;\n");
0166: }
0167: }
0168: buf.append("\t\treturn true;\n\t}\n");
0169: spec.addMethod(buf.toString());
0170: }
0171:
0172: private void addCopyKeyFields() {
0173: StringBuffer buf = new StringBuffer();
0174: /*
0175: public boolean copyKeyFields(ResultSet rs, int firstCol) throws SQLException {
0176: _0 = rs.getInt(firstCol++);
0177: if (rs.wasNull()) {
0178: return false;
0179: }
0180: return true;
0181: }
0182:
0183:
0184: public boolean copyKeyFields(ResultSet rs, int firstCol) throws SQLException {
0185:
0186:
0187: Boolean prim_0 = (Boolean)jdbcConverter_0.get(rs, firstCol, jdbcCol_0);
0188: firstCol++;
0189: if (rs.wasNull()) {
0190: return false;
0191: }
0192: _0 = prim_0.booleanValue();
0193: Boolean prim_1 = (Boolean)jdbcConverter_1.get(rs, firstCol, jdbcCol_1);
0194: firstCol++;
0195: if (rs.wasNull()) {
0196: return false;
0197: }
0198: _1 = prim_1.booleanValue();
0199: _2 = (Locale)jdbcConverter_2.get(rs, firstCol, jdbcCol_2);
0200: firstCol++;
0201: if (rs.wasNull()) {
0202: return false;
0203: }
0204: _3 = rs.getString(firstCol++);
0205: return !rs.wasNull();
0206: }
0207:
0208: */
0209:
0210: buf
0211: .append("\n\tpublic boolean copyKeyFields(ResultSet rs,int firstCol) throws SQLException{\n");
0212:
0213: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
0214: for (int i = 0; i < pkc.length; i++) {
0215: Class fieldType = pkc[i].javaType;
0216: boolean isPrim = pkc[i].javaType.isPrimitive();
0217: String fieldName = "_" + i;
0218: if (pkc[i].converter != null) { // converter
0219: if (isPrim) {
0220: /*
0221: Boolean prim_1 = (Boolean)jdbcConverter_1.get(rs, firstCol++, jdbcCol_1);
0222: if (rs.wasNull()) {
0223: return false;
0224: }
0225: _0 = prim_0.booleanValue();
0226: */
0227: buf.append("\t\t");
0228: buf.append((String) primitiveToWrapperTypes
0229: .get(fieldType));
0230: buf.append(" prim_" + i);
0231: buf.append(" = (");
0232: buf.append((String) primitiveToWrapperTypes
0233: .get(fieldType));
0234: buf.append(")jdbcConverter_" + i);
0235: buf.append(".get(rs,firstCol++, jdbcCol_" + i);
0236: buf.append(");\n");
0237: buf.append("\t\tif (rs.wasNull()) return false;\n");
0238: buf.append("\t\t");
0239: buf.append(fieldName);
0240: buf.append(" = ");
0241: buf.append(" prim_" + i);
0242: buf.append(".");
0243: buf.append((String) wrapperTypesToValue
0244: .get(fieldType));
0245: buf.append("();\n");
0246: } else {
0247: /*
0248: _2 = (Locale)jdbcConverter_2.get(rs, firstCol++, jdbcCol_2);
0249: if (rs.wasNull())return false;
0250: */
0251: buf.append("\t\t");
0252: buf.append(fieldName);
0253: buf.append(" = (");
0254: buf.append(fieldType.getName());
0255: buf.append(")jdbcConverter_" + i);
0256: buf.append(".get(rs,firstCol++, jdbcCol_" + i);
0257: buf.append(");\n");
0258: buf.append("\t\tif (rs.wasNull()) return false;\n");
0259: }
0260: } else { // no converter
0261: boolean isWrapper = wrapperTypesToPrimitive.keySet()
0262: .contains(fieldType);
0263: if (isWrapper) {
0264: /*
0265: _3 = new Short(rs.getShort(firstCol++));
0266: if (rs.wasNull()) return false;
0267: */
0268: buf.append("\t\t");
0269: buf.append(fieldName);
0270: buf.append(" = new ");
0271: buf.append(fieldType.getName());
0272: buf.append("(rs.");
0273: buf
0274: .append((String) typeToResultSetGetField
0275: .get(wrapperTypesToPrimitive
0276: .get(fieldType)));
0277: buf.append("(firstCol++));\n");
0278: } else {
0279: /*
0280: _3 = rs.getString(firstCol++);
0281: if (rs.wasNull()) return false;
0282: */
0283: buf.append("\t\t");
0284: buf.append(fieldName);
0285: buf.append(" = rs.");
0286: buf.append((String) typeToResultSetGetField
0287: .get(fieldType));
0288: buf.append("(firstCol++);\n");
0289:
0290: }
0291: buf.append("\t\tif (rs.wasNull()) return false;\n");
0292: }
0293: }
0294: buf.append("\t\treturn true;\n\t}\n");
0295: spec.addMethod(buf.toString());
0296: }
0297:
0298: private void addSetParams() {
0299: StringBuffer buf = new StringBuffer();
0300: /*
0301:
0302: public int setParams(PreparedStatement ps, int firstParam) throws SQLException {
0303: ps.setInt(firstParam++, _0);
0304: jdbcConverter_1.set(ps, firstParam++, jdbcCol_1, new Boolean(_1));
0305: jdbcConverter_2.set(ps, firstParam++, jdbcCol_2, _2);
0306: if (_3 == null) {
0307: ps.setNull(firstParam++, 12);
0308: } else {
0309: ps.setString(firstParam++, _3);
0310: }
0311: return firstParam;
0312: }
0313: */
0314:
0315: buf
0316: .append("\n\tpublic int setParams(PreparedStatement ps, int firstParam) throws SQLException {\n");
0317: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
0318: for (int i = 0; i < pkc.length; i++) {
0319: Class fieldType = pkc[i].javaType;
0320: String fieldName = "_" + i;
0321: boolean isPrimitive = pkc[i].javaType.isPrimitive();
0322: if (pkc[i].converter != null) { // converter
0323: String converterName = JDBC_CONVERTER_FIELD_PREFIX + i;
0324: String colName = "jdbcCol_" + i;
0325:
0326: if (isPrimitive) {
0327: /*
0328: jdbcConverter_1.set(ps, firstParam++, jdbcCol_1, new Boolean(_1));
0329: */
0330: buf.append("\t\t");
0331: buf.append(converterName);
0332: buf.append(".set(ps, firstParam++, ");
0333: buf.append(colName);
0334: buf.append(", new ");
0335: buf.append((String) primitiveToWrapperTypes
0336: .get(fieldType));
0337: buf.append("(");
0338: buf.append(fieldName);
0339: buf.append("));\n");
0340: } else {
0341: /*
0342: jdbcConverter_2.set(ps, firstParam++, jdbcCol_2, _2);
0343: */
0344: buf.append("\t\t");
0345: buf.append(converterName);
0346: buf.append(".set(ps, firstParam++, ");
0347: buf.append(colName);
0348: buf.append(", ");
0349: buf.append(fieldName);
0350: buf.append(");\n");
0351: }
0352: } else if (isPrimitive) { //primitive no converter
0353: /*
0354: ps.setInt(firstParam++, _0);
0355: */
0356: buf.append("\t\t");
0357: buf.append("ps.");
0358: buf.append((String) typeToPreparedStatementSetField
0359: .get(fieldType));
0360: buf.append("(firstParam++, ");
0361: buf.append(fieldName);
0362: buf.append(");\n");
0363: } else { // Object no converter
0364: /*
0365: if (_3 == null) ps.setNull(firstParam++, 12);
0366: else ps.setString(firstParam++, _3);
0367: */
0368: buf.append("\t\t");
0369: buf.append("if (");
0370: buf.append(fieldName);
0371: buf.append(" == null) ps.setNull(firstParam++,");
0372: buf.append(" " + (pkc[i]).jdbcType);
0373: buf.append(");\n");
0374: if (wrapperTypesToPrimitive.containsKey(fieldType)) {// we have a wrapper type
0375:
0376: /*
0377: else ps.setShort(firstParam++, _3.shortValue());
0378: */
0379:
0380: buf.append("\t\t");
0381: buf.append("else ps.");
0382: buf
0383: .append((String) typeToPreparedStatementSetField
0384: .get(wrapperTypesToPrimitive
0385: .get(fieldType)));
0386: buf.append("(firstParam++,");
0387: buf.append(fieldName);
0388: buf.append(".");
0389: buf
0390: .append((String) wrapperTypesToValue
0391: .get(wrapperTypesToPrimitive
0392: .get(fieldType)));
0393: buf.append("());\n");
0394: } else {
0395: /*
0396: else ps.setString(firstParam++, _3);
0397: */
0398: buf.append("\t\t");
0399: buf.append("else ps.");
0400: buf.append((String) typeToPreparedStatementSetField
0401: .get(fieldType));
0402: buf.append("(firstParam++,");
0403: buf.append(fieldName);
0404: buf.append(");\n");
0405: }
0406: }
0407: }
0408: buf.append("\t\treturn firstParam;\n\t}\n");
0409: spec.addMethod(buf.toString());
0410: }
0411:
0412: private void addSetParams2() {
0413: StringBuffer buf = new StringBuffer();
0414: /*
0415: public int setParams(PreparedStatement ps, int firstParam, JdbcColumn pkc[]) throws SQLException {
0416: JdbcColumn c = null;
0417: c = pkc[0];
0418: if (c.isForUpdate()) {
0419: if (c.converter != null) {
0420: c.converter.set(ps, firstParam++, c, new Boolean(_0));
0421: } else {
0422: JdbcUtils.set(ps, firstParam++, new Boolean(_0), c.javaTypeCode, c.jdbcType);
0423: }
0424: }
0425: c = pkc[2];
0426: if (c.isForUpdate()) {
0427: if (c.converter != null) {
0428: c.converter.set(ps, firstParam++, c, _2);
0429: } else {
0430: JdbcUtils.set(ps, firstParam++, _2, c.javaTypeCode, c.jdbcType);
0431: }
0432: }
0433: return firstParam;
0434: }
0435: */
0436: buf
0437: .append("\n\tpublic int setParams(PreparedStatement ps, int firstParam, JdbcColumn[] pkc) throws SQLException {\n");
0438:
0439: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
0440: /*
0441: JdbcColumn c = null;
0442: */
0443: buf.append("\t\tJdbcColumn c = null;\n");
0444: for (int i = 0; i < pkc.length; i++) {
0445: Class fieldType = pkc[i].javaType;
0446: String fieldName = "_" + i;
0447: boolean isPrimitive = pkc[i].javaType.isPrimitive();
0448: /*
0449: c = pkc[0];
0450: if (c.isForUpdate()) {
0451: if (c.converter != null) c.converter.set(ps, firstParam++, c, new Boolean(_0));
0452: else JdbcUtils.set(ps, firstParam++, new Boolean(_0), c.javaTypeCode, c.jdbcType);
0453: }
0454: */
0455: buf.append("\t\tc = pkc[" + i);
0456: buf.append("];\n");
0457:
0458: buf.append("\t\tif (c.isForUpdate()) {\n");
0459: if (isPrimitive) {
0460: /*
0461: if (c.converter != null) c.converter.set(ps, firstParam++, c, new Boolean(_0));
0462: else JdbcUtils.set(ps, firstParam++, new Boolean(_0), c.javaTypeCode, c.jdbcType);
0463: */
0464:
0465: buf
0466: .append("\t\t\tif (c.converter != null) c.converter.set(ps, firstParam++, c, new ");
0467: buf.append((String) primitiveToWrapperTypes
0468: .get(fieldType));
0469: buf.append("(");
0470: buf.append(fieldName);
0471: buf.append("));\n");
0472:
0473: buf
0474: .append("\t\t\telse JdbcUtils.set(ps, firstParam++, new ");
0475: buf.append((String) primitiveToWrapperTypes
0476: .get(fieldType));
0477: buf.append("(");
0478: buf.append(fieldName);
0479: buf.append("), c.javaTypeCode, c.jdbcType);\n");
0480:
0481: } else {
0482: /*
0483: if (c.converter != null) c.converter.set(ps, firstParam++, c, _2);
0484: else JdbcUtils.set(ps, firstParam++, _2, c.javaTypeCode, c.jdbcType);
0485: */
0486: buf
0487: .append("\t\t\tif (c.converter != null) c.converter.set(ps, firstParam++, c, ");
0488: buf.append(fieldName);
0489: buf.append(");\n");
0490:
0491: buf
0492: .append("\t\t\telse JdbcUtils.set(ps, firstParam++, ");
0493: buf.append(fieldName);
0494: buf.append(", c.javaTypeCode, c.jdbcType);\n");
0495:
0496: }
0497: buf.append("\t\t}\n");
0498:
0499: }
0500: buf.append("\t\treturn firstParam;\n\t}\n");
0501: spec.addMethod(buf.toString());
0502: }
0503:
0504: protected void addCopyKeyFields2() {
0505: StringBuffer buf = new StringBuffer();
0506: buf
0507: .append("\n\tpublic boolean copyKeyFields(ResultSet rs, JdbcField[] pks, int[] pkFieldIndexs) throws SQLException {\n");
0508: if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
0509: buf
0510: .append("\t\tfor (int j = 0; j < pkFieldIndexs.length; j++) {\n");
0511: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
0512: buf.append("\t\t\tswitch(j){\n");
0513: for (int i = 0; i < pkc.length; i++) {
0514: Class fieldType = pkc[i].javaType;
0515: boolean isPrim = pkc[i].javaType.isPrimitive();
0516: String fieldName = "_" + i;
0517: buf.append("\t\t\t\tcase " + i + " :\n");
0518: if (pkc[i].converter != null) { // converter
0519: if (isPrim) {
0520: /*
0521: Boolean prim_1 = (Boolean)jdbcConverter_1.get(rs, pkFieldIndexs[j] + 1, jdbcCol_1);
0522: if (rs.wasNull()) {
0523: return false;
0524: }
0525: _0 = prim_0.booleanValue();
0526: */
0527:
0528: buf.append("\t\t\t\t\t"
0529: + (String) primitiveToWrapperTypes
0530: .get(fieldType));
0531: buf.append(" prim_" + i);
0532: buf.append(" = (");
0533: buf.append((String) primitiveToWrapperTypes
0534: .get(fieldType));
0535: buf.append(")jdbcConverter_" + i);
0536: buf
0537: .append(".get(rs, pkFieldIndexs[j] + 1, jdbcCol_"
0538: + i);
0539: buf.append(");\n");
0540: buf.append("\t\t\t\t\t" + fieldName);
0541: buf.append(" = ");
0542: buf.append(" prim_" + i);
0543: buf.append(".");
0544: buf.append((String) wrapperTypesToValue
0545: .get(fieldType));
0546: buf.append("();\n");
0547: } else {
0548: /*
0549: _2 = (Locale)jdbcConverter_2.get(rs, firstCol++, jdbcCol_2);
0550: if (rs.wasNull())return false;
0551: */
0552: buf.append("\t\t\t\t\t" + fieldName);
0553: buf.append(" = (");
0554: buf.append(fieldType.getName());
0555: buf.append(")jdbcConverter_" + i);
0556: buf
0557: .append(".get(rs,pkFieldIndexs[j] + 1, jdbcCol_"
0558: + i);
0559: buf.append(");\n");
0560: }
0561: } else { // no converter
0562: boolean isWrapper = wrapperTypesToPrimitive
0563: .keySet().contains(fieldType);
0564: if (isWrapper) {
0565: /*
0566: _3 = new Short(rs.getShort(firstCol++));
0567: if (rs.wasNull()) return false;
0568: */
0569: buf.append("\t\t\t\t\t" + fieldName);
0570: buf.append(" = new ");
0571: buf.append(fieldType.getName());
0572: buf.append("(rs.");
0573: buf.append((String) typeToResultSetGetField
0574: .get(wrapperTypesToPrimitive
0575: .get(fieldType)));
0576: buf.append("(pkFieldIndexs[j] + 1));\n");
0577: } else {
0578: /*
0579: _3 = rs.getString(firstCol++);
0580: if (rs.wasNull()) return false;
0581: */
0582: buf.append("\t\t\t\t\t" + fieldName);
0583: buf.append(" = rs.");
0584: buf.append((String) typeToResultSetGetField
0585: .get(fieldType));
0586: buf.append("(pkFieldIndexs[j] + 1);\n");
0587:
0588: }
0589: }
0590: buf.append("\t\t\t\t\tbreak;\n");
0591: }
0592: buf.append("\t\t\t}\n");
0593: buf.append("\t\t\tif (rs.wasNull()) return false;\n");
0594: buf.append("\t\t}\n");
0595: }
0596: buf.append("\t\treturn true;\n");
0597: buf.append("\t}\n");
0598: spec.addMethod(buf.toString());
0599: }
0600:
0601: protected void addGetLongPrimaryKey() {
0602: StringBuffer buf = new StringBuffer();
0603: buf.append("\n\tpublic long getLongPrimaryKey() {\n");
0604: if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) {
0605: JdbcColumn c = ((JdbcClass) currentCMD.storeClass).table.pk[0];
0606: switch (c.javaTypeCode) {
0607: case MDStatics.BYTE:
0608: case MDStatics.SHORT:
0609: case MDStatics.INT:
0610: buf.append("\t\treturn (long)_0;\n");
0611: break;
0612:
0613: case MDStatics.LONG:
0614: buf.append("\t\treturn _0;\n");
0615: break;
0616: }
0617: } else {
0618: buf.append("\t\treturn (long) -1;\n");
0619: }
0620: buf.append("\t}\n");
0621: spec.addMethod(buf.toString());
0622: }
0623:
0624: protected void addSetLongPrimaryKey() {
0625: StringBuffer buf = new StringBuffer();
0626: buf.append("\n\tpublic void setLongPrimaryKey(long newPK) {\n");
0627: if (currentCMD.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) {
0628: JdbcColumn c = ((JdbcClass) currentCMD.storeClass).table.pk[0];
0629: buf.append("\t\t_0 = ");
0630: switch (c.javaTypeCode) {
0631: case MDStatics.BYTE:
0632: buf.append("(byte)");
0633: break;
0634: case MDStatics.SHORT:
0635: buf.append("(short)");
0636: break;
0637: case MDStatics.INT:
0638: buf.append("(int)");
0639: break;
0640: }
0641: buf.append("newPK;\n");
0642: }
0643: buf.append("\t}\n");
0644: spec.addMethod(buf.toString());
0645: }
0646:
0647: protected void addfillFromPK() {
0648: StringBuffer buf = new StringBuffer();
0649: buf.append("\n\tpublic void fillFromPK(Object pk) {\n");
0650: if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
0651: if (cmd.top.objectIdClass != null) {
0652: String oidClassName = cmd.top.objectIdClass.getName()
0653: .replace('$', '.');
0654: /*
0655: AppIdConcrete1Key appPK = (AppIdConcrete1Key)pk;
0656: */
0657: buf.append("\t\t" + oidClassName);
0658: buf.append(" appPK = (");
0659: buf.append(oidClassName);
0660: buf.append(")pk;\n");
0661:
0662: FieldMetaData[] fields = cmd.pkFields;
0663: for (int i = 0; i < fields.length; i++) {
0664: FieldMetaData field = fields[i];
0665: /*
0666: _0 = appPK.appIdConcKey;
0667: */
0668: buf.append("\t\t" + getFieldName(i));
0669: buf.append(" = appPK.");
0670: buf.append(field.getPkFieldName());
0671: buf.append(";\n");
0672: }
0673: }
0674: }
0675: buf.append("\t}\n");
0676: spec.addMethod(buf.toString());
0677: }
0678:
0679: protected void addCompareTo() {
0680: StringBuffer buf = new StringBuffer();
0681: buf.append("\n\tpublic int compareTo(Object o) {\n");
0682: buf.append("\t\tOID other = (OID)o;\n");
0683: buf.append("\t\tint diff = " + cmd.top.index
0684: + " - other.getBaseClassMetaData().index;\n");
0685: buf.append("\t\tif (diff != 0) return diff;\n");
0686: buf.append("\t\tif (other.isNew()) return 1;\n");
0687: buf.append("\t\telse {\n");
0688: buf.append("\t\t\t" + className + " original = (" + className
0689: + ")other;\n");
0690:
0691: ClassMetaData currentCMD = getCurrentCMD();
0692:
0693: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
0694: int count = pkc.length - 1;
0695: boolean isLast;
0696: for (int i = 0; i < pkc.length; i++) {
0697: JdbcColumn c = pkc[i];
0698: String fieldName = "_" + i;
0699: isLast = i == count;
0700:
0701: switch (c.javaTypeCode) {
0702:
0703: case MDStatics.CHAR:
0704: case MDStatics.BYTE:
0705: case MDStatics.SHORT:
0706: case MDStatics.INT:
0707:
0708: if (isLast) {
0709: /*
0710: return _0 - original._0;
0711: */
0712: buf.append("\t\t\treturn " + fieldName
0713: + " - original." + fieldName + ";\n");
0714: } else {
0715: /*
0716: if (_0 != original._0) return _0 - original._0;
0717: */
0718: buf.append("\t\t\tif (" + fieldName
0719: + " != original." + fieldName + ") return "
0720: + fieldName + " - original." + fieldName
0721: + ";\n");
0722: }
0723: break;
0724: case MDStatics.LOCALE:
0725: /*
0726: diff = _2.toString().compareTo(original._2.toString());
0727: if (diff != 0) return diff;
0728: */
0729: buf.append("\t\t\tdiff = " + fieldName
0730: + ".toString().compareTo(original." + fieldName
0731: + ".toString());\n");
0732:
0733: if (isLast) {
0734: buf.append("\t\t\treturn diff;\n");
0735: } else {
0736: buf.append("\t\t\tif (diff != 0) return diff;\n");
0737: }
0738: break;
0739:
0740: case MDStatics.CHARW:
0741: case MDStatics.BYTEW:
0742: case MDStatics.SHORTW:
0743: case MDStatics.INTW:
0744: case MDStatics.LONGW:
0745: case MDStatics.FLOATW:
0746: case MDStatics.DOUBLEW:
0747: case MDStatics.STRING:
0748: case MDStatics.BIGDECIMAL:
0749: case MDStatics.BIGINTEGER:
0750: case MDStatics.DATE:
0751:
0752: if (isLast) {
0753: buf.append("\t\t\treturn " + fieldName
0754: + ".compareTo(original." + fieldName
0755: + ");\n");
0756: } else {
0757: buf.append("\t\t\tdiff = " + fieldName
0758: + ".compareTo(original." + fieldName
0759: + ");\n");
0760: buf.append("\t\t\tif (diff != 0) return diff;\n");
0761: }
0762: break;
0763:
0764: case MDStatics.DOUBLE:
0765: case MDStatics.FLOAT:
0766: case MDStatics.LONG:
0767:
0768: if (isLast) {
0769: /*
0770: return _0 - original._0;
0771: */
0772: buf.append("\t\t\treturn (int)(" + fieldName
0773: + " - original." + fieldName + ");\n");
0774: } else {
0775: /*
0776: if (_0 != original._0) return _0 - original._0;
0777: */
0778: buf.append("\t\t\tif (" + fieldName
0779: + " != original." + fieldName
0780: + ") return (int)(" + fieldName
0781: + " - original." + fieldName + ");\n");
0782: }
0783: break;
0784:
0785: case MDStatics.BOOLEANW:
0786:
0787: if (isLast) {
0788: buf.append("\t\t\treturn !" + fieldName
0789: + ".booleanValue() ? -1 : 1;\n");
0790: } else {
0791: /*
0792: if (_0 != original._0) return !_0 ? -1 : 1;
0793: */
0794: buf.append("\t\t\tif (!" + fieldName
0795: + ".equals(original." + fieldName
0796: + ")) return !" + fieldName
0797: + ".booleanValue() ? -1 : 1;\n");
0798: }
0799:
0800: break;
0801: case MDStatics.BOOLEAN:
0802: if (isLast) {
0803: buf.append("\t\t\treturn !" + fieldName
0804: + " ? -1 : 1;\n");
0805: } else {
0806: /*
0807: if (_0 != original._0) return !_0 ? -1 : 1;
0808: */
0809: buf
0810: .append("\t\t\tif (" + fieldName
0811: + " != original." + fieldName
0812: + ") return !" + fieldName
0813: + " ? -1 : 1;\n");
0814: }
0815: break;
0816:
0817: default:
0818: throw BindingSupportImpl.getInstance().runtime(
0819: "Unable to create a ObjectId for a Type '"
0820: + c.javaType.getName() + "'");
0821: }
0822:
0823: }
0824: buf.append("\t\t}\n\t}\n");
0825: spec.addMethod(buf.toString());
0826: }
0827:
0828: protected void addToPKString() {
0829: StringBuffer buf = new StringBuffer();
0830: buf.append("\n\tpublic String toPkString() {\n");
0831: buf.append("\t\tStringBuffer s = new StringBuffer();\n");
0832: JdbcColumn[] pkc = ((JdbcClass) cmd.storeClass).table.pk;
0833: for (int i = 0; i < pkc.length; i++) {
0834: String fieldName = "_" + i;
0835: buf.append("\t\ts.append(");
0836: buf.append(fieldName);
0837: buf.append(");\n");
0838: if (i != (pkc.length - 1)) {
0839: buf.append("\t\ts.append(\", \");\n");
0840: }
0841: }
0842: buf.append("\t\treturn s.toString();\n\t}\n");
0843: spec.addMethod(buf.toString());
0844: }
0845:
0846: protected void addToSString() {
0847: StringBuffer buf = new StringBuffer();
0848: buf.append("\n\tpublic String toSString() {\n");
0849: JdbcColumn[] pkc = ((JdbcClass) cmd.storeClass).table.pk;
0850: /*
0851: StringBuffer s = new StringBuffer("OID:za.AppIDAbstract1 (Table:app_i_d_abstract1");
0852: */
0853: buf.append("\t\tStringBuffer s = new StringBuffer(\"");
0854: buf.append("OID:" + currentCMD.qname + " (Table:"
0855: + ((JdbcClass) currentCMD.storeClass).tableName);
0856: buf.append("\");\n");
0857:
0858: for (int i = 0; i < pkc.length; i++) {
0859: JdbcColumn c = pkc[i];
0860: String fieldName = "_" + i;
0861:
0862: buf.append("\t\ts.append(\"");
0863: buf.append(" Column:" + c.name + " = \"");
0864: buf.append(");\n");
0865:
0866: buf.append("\t\ts.append(");
0867: buf.append(fieldName);
0868: buf.append(");\n");
0869: }
0870: buf.append("\t\ts.append(\")\");\n");
0871: buf.append("\t\treturn s.toString();\n");
0872: buf.append("\t}\n");
0873: spec.addMethod(buf.toString());
0874: }
0875:
0876: protected void addGetCopy() {
0877: StringBuffer buf = new StringBuffer();
0878: buf.append("\n\tpublic OID copy() {\n");
0879: buf.append("\t\t" + className);
0880: buf.append(" copy = new ");
0881: buf.append(className);
0882: buf.append("();\n");
0883: if (cmd.isInHeirachy()) {
0884: buf.append("\t\tcopy.cmd = cmd;\n");
0885: buf.append("\t\tcopy.resolved = resolved;\n");
0886: }
0887: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
0888: int num = pkc.length;
0889: for (int i = 0; i < num; i++) {
0890: Class fieldType = pkc[i].javaType;
0891: String fieldName = getFieldName(i);
0892: boolean isPrimitive = pkc[i].javaType.isPrimitive();
0893: boolean isArray = pkc[i].javaType.isArray();
0894: boolean isCloneable = false;
0895:
0896: if (!isPrimitive) {
0897: Class[] interfaces = pkc[i].javaType.getInterfaces();
0898: for (int j = 0; j < interfaces.length; j++) {
0899: Class aClass = interfaces[j];
0900: if (aClass.getName().equals("Cloneable")) {
0901: isCloneable = true;
0902: }
0903: }
0904: }
0905: if (isPrimitive) {
0906: /*
0907: copy._0 = _0;
0908: */
0909: buf.append("\t\tcopy.");
0910: buf.append(fieldName);
0911: buf.append(" = ");
0912: buf.append(fieldName);
0913: buf.append(";\n");
0914: } else if (isArray) {
0915: /*
0916: System.arraycopy((Object)_0,0, (Object) copy._0, 0, _0.length);
0917: */
0918: buf.append("\t\tSystem.arraycopy((Object)");
0919: buf.append(fieldName);
0920: buf.append(", 0, (Object)copy.");
0921: buf.append(fieldName);
0922: buf.append(", 0, ");
0923: buf.append(fieldName);
0924: buf.append(".length);\n");
0925: } else if (isCloneable) {
0926: /*
0927: copy._1 = (Locale)_1.clone();
0928: */
0929: buf.append("\t\tcopy.");
0930: buf.append(fieldName);
0931: buf.append(" = (");
0932: buf.append(fieldType.getName());
0933: buf.append(")");
0934: buf.append(fieldName);
0935: buf.append(".clone();\n");
0936: } else {// normal object ????
0937: /*
0938: copy._0 = _0;
0939: */
0940: buf.append("\t\tcopy.");
0941: buf.append(fieldName);
0942: buf.append(" = ");
0943: buf.append(fieldName);
0944: buf.append(";\n");
0945: }
0946: }
0947: buf.append("\t\treturn copy;\n\t}\n");
0948: spec.addMethod(buf.toString());
0949: }
0950:
0951: protected void addCopyKeyFieldsUpdate() {
0952: StringBuffer buf = new StringBuffer();
0953: buf
0954: .append("\n\tpublic void copyKeyFieldsUpdate(State state) {\n");
0955: if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
0956: ClassMetaData currentCMD = getTopPCSuperClassMetaData();
0957: buf.append("\t\t");
0958: buf.append(stateClassName);
0959: buf.append(" other = (");
0960: buf.append(stateClassName);
0961: buf.append(")state;\n");
0962: FieldMetaData[] pkFields = currentCMD.pkFields;
0963: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
0964: for (int i = 0; i < pkc.length; i++) {
0965: int stateFieldNum = pkFields[i].fieldNo;
0966: String stateFieldName = "_" + stateFieldNum;
0967: String fieldName = "_" + i;
0968: /*
0969: if (other.containsField(1)) _0 = other._1;
0970: */
0971: buf.append("\t\tif (other.containsField("
0972: + stateFieldNum);
0973: buf.append(")) ");
0974: buf.append(fieldName);
0975: buf.append(" = other.");
0976: buf.append(stateFieldName);
0977: buf.append(";\n");
0978: }
0979: }
0980: buf.append("\t}\n");
0981: spec.addMethod(buf.toString());
0982: }
0983:
0984: protected void addCopyKeyFieldsFromState() {
0985: StringBuffer buf = new StringBuffer();
0986: buf.append("\n\tpublic void copyKeyFields(State state) {\n");
0987: if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) {
0988: ClassMetaData currentCMD = getTopPCSuperClassMetaData();
0989: buf.append("\t\t");
0990: buf.append(stateClassName);
0991: buf.append(" other = (");
0992: buf.append(stateClassName);
0993: buf.append(")state;\n");
0994: FieldMetaData[] pkFields = currentCMD.pkFields;
0995: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
0996: for (int i = 0; i < pkc.length; i++) {
0997: String stateFieldName = "_" + pkFields[i].fieldNo;
0998: String fieldName = "_" + i;
0999: buf.append("\t\t");
1000: buf.append(fieldName);
1001: buf.append(" = other.");
1002: buf.append(stateFieldName);
1003: buf.append(";\n");
1004: }
1005: }
1006: buf.append("\t}\n");
1007: spec.addMethod(buf.toString());
1008: }
1009:
1010: protected void addEqualsObject() {
1011: StringBuffer buf = new StringBuffer();
1012: buf.append("\n\tpublic boolean equals(Object object) {\n");
1013: buf.append("\t\t");
1014: buf.append("if (object instanceof ");
1015: buf.append(className);
1016: buf.append(") {\n");
1017: buf.append("\t\t\t");
1018: buf.append(className);
1019: buf.append(" other = (");
1020: buf.append(className);
1021: buf.append(")object;\n");
1022: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
1023: for (int i = 0; i < pkc.length; i++) {
1024: String fieldName = "_" + i;
1025: boolean isPrimitive = pkc[i].javaType.isPrimitive();
1026:
1027: if (isPrimitive) {
1028: /*
1029: if (_0 != other._0) return false;
1030: */
1031: buf.append("\t\t\t");
1032: buf.append("if (");
1033: buf.append(fieldName);
1034: buf.append(" != other.");
1035: buf.append(fieldName);
1036: buf.append(") return false;\n");
1037:
1038: } else { // this is a object and it can be null
1039: /*
1040: if (_3 != null) {
1041: if (!_3.equals(other._3)) return false;
1042: } else if (other._3 != null) return false;
1043:
1044: */
1045: buf.append("\t\t\t");
1046: buf.append("if (");
1047: buf.append(fieldName);
1048: buf.append(" != null){\n");
1049: buf.append("\t\t\t\t");
1050: buf.append("if (!");
1051: buf.append(fieldName);
1052: buf.append(".equals(other.");
1053: buf.append(fieldName);
1054: buf.append(")) return false;\n");
1055: buf.append("\t\t\t");
1056: buf.append("} else if (other.");
1057: buf.append(fieldName);
1058: buf.append(" != null) return false;\n");
1059: }
1060: }
1061: buf.append("\t\t\treturn true;\n");
1062: buf.append("\t\t} else {\n\t\t\treturn false;\n\t\t}\n\t}\n");
1063: spec.addMethod(buf.toString());
1064: }
1065:
1066: protected void addHashCode() {
1067: StringBuffer buf = new StringBuffer();
1068: buf.append("\n\tpublic int hashCode() {\n");
1069: int noOfVars = 0;
1070: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
1071: noOfVars = pkc.length;
1072: for (int i = 0; i < noOfVars; i++) {
1073: Class fieldType = pkc[i].javaType;
1074: String fieldName = "_" + i;
1075: boolean isPrimitive = pkc[i].javaType.isPrimitive();
1076:
1077: /* If there is only one pk field we speed things up */
1078: if (noOfVars == 1) {
1079: if (isPrimitive) {
1080: if (fieldType.equals(long.class)
1081: || fieldType.equals(float.class)
1082: || fieldType.equals(double.class)) {
1083: /*
1084: return (int)(877146213 * _0);
1085: */
1086: buf.append("\t\treturn (int)(" + cmd.classId);
1087: buf.append(" * ");
1088: buf.append(fieldName);
1089: buf.append(");\n");
1090: } else if (fieldType.equals(boolean.class)) {
1091: /*
1092: return _0 ? 1 : -1;
1093: */
1094: buf.append("\t\treturn ");
1095: buf.append(fieldName);
1096: buf.append(" ? 1 : -1;\n");
1097:
1098: } else {
1099: /*
1100: return 877146213 * _0;
1101: */
1102: buf.append("\t\treturn " + cmd.classId);
1103: buf.append(" * ");
1104: buf.append(fieldName);
1105: buf.append(";\n");
1106: }
1107: } else { // this is a object and it can be null
1108: /*
1109: return 877146213 * _0.hashCode(); ???????
1110: */
1111: buf.append("\t\treturn ");
1112: buf.append(fieldName);
1113: buf.append(".hashCode();\n");
1114: }
1115: } else {
1116: /** We have multiple fields */
1117: if (i == 0) {
1118: buf.append("\t\tint hashCode = 0;\n");
1119: }
1120: if (isPrimitive) {
1121: if (fieldType.equals(long.class)
1122: || fieldType.equals(float.class)
1123: || fieldType.equals(double.class)) {
1124: /*
1125: hashCode += _4;
1126: */
1127: buf.append("\t\thashCode += ");
1128: buf.append(fieldName);
1129: buf.append(";\n");
1130: } else if (fieldType.equals(boolean.class)) {
1131: /*
1132: hashCode += _1 ? 1 : -1;
1133: */
1134: buf.append("\t\thashCode += ");
1135: buf.append(fieldName);
1136: buf.append(" ? 1 : -1;\n");
1137:
1138: } else {
1139: /*
1140: hashCode += _4;
1141: */
1142: buf.append("\t\thashCode += ");
1143: buf.append(fieldName);
1144: buf.append(";\n");
1145: }
1146: } else { // this is a object and it can be null
1147: /*
1148: hashCode += _4;
1149: */
1150: buf.append("\t\thashCode += ");
1151: buf.append(fieldName);
1152: buf.append(".hashCode();\n");
1153: }
1154: }
1155: }
1156: if (noOfVars > 1) {
1157: buf.append("\t\treturn hashCode;\n\t}\n");
1158: } else {
1159: buf.append("\t}\n");
1160: }
1161: spec.addMethod(buf.toString());
1162: }
1163:
1164: /**
1165: * Add all PK field(s).
1166: */
1167: protected void addFields() {
1168: super .addFields();
1169: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
1170: for (int i = 0; i < pkc.length; i++) {
1171: spec.addField("public " + pkc[i].javaType.getName() + " _"
1172: + i);
1173: if (pkc[i].converter != null) {
1174: spec.addField("public static "
1175: + pkc[i].converter.getClass().getName() + " "
1176: + JDBC_CONVERTER_FIELD_PREFIX + i);
1177: spec
1178: .addField("public static "
1179: + pkc[i].getClass().getName()
1180: + " jdbcCol_" + i);
1181: }
1182: }
1183: }
1184:
1185: protected void addCopyKeyFieldsObjects() {
1186: StringBuffer buf = new StringBuffer();
1187: buf.append("\n\tpublic void copyKeyFields(Object[] data) {\n");
1188: JdbcColumn[] pkc = ((JdbcClass) currentCMD.storeClass).table.pk;
1189: for (int i = 0; i < pkc.length; i++) {
1190: Class fieldType = pkc[i].javaType;
1191: String fieldName = "_" + i;
1192: boolean isPrimitive = pkc[i].javaType.isPrimitive();
1193:
1194: if (isPrimitive) {
1195: /*
1196: _0 = ((Integer)data[0]).intValue();
1197: */
1198: buf.append("\t\t");
1199: buf.append(fieldName);
1200: buf.append(" = ((");
1201: buf.append((String) primitiveToWrapperTypes
1202: .get(fieldType));
1203: buf.append(")data[" + i);
1204: buf.append("]).");
1205: buf.append((String) primitiveTypesToValue
1206: .get(fieldType));
1207: buf.append("();\n");
1208: } else {
1209: /*
1210: _3 = (String)data[3];
1211: */
1212: buf.append("\t\t");
1213: buf.append(fieldName);
1214: buf.append(" = (");
1215: buf.append(fieldType.getName());
1216: buf.append(")data[" + i);
1217: buf.append("];\n");
1218: }
1219: }
1220: buf.append("\t}\n");
1221: spec.addMethod(buf.toString());
1222: }
1223:
1224: protected void addToString() {
1225: StringBuffer buf = new StringBuffer();
1226: buf.append("\n\tpublic String toString() {\n");
1227: if (cmd.isInHeirachy()) {
1228: buf
1229: .append("\t\tif (!resolved) {\n"
1230: + "\t\t\tthrow BindingSupportImpl.getInstance().internal(\n"
1231: + "\t\t\t\t\"Called 'toString()' on unresolved oid\");\n"
1232: + "\t\t}\n");
1233: }
1234: buf.append("\t\tStringBuffer s = new StringBuffer();\n");
1235: if (currentCMD.isInHeirachy()) {
1236: buf.append("\t\ts.append(cmd.classId);\n");
1237: } else {
1238: buf.append("\t\ts.append(\"" + cmd.classId);
1239: buf.append("\");\n");
1240: }
1241: JdbcColumn[] pkc = ((JdbcClass) cmd.storeClass).table.pk;
1242: for (int i = 0; i < pkc.length; i++) {
1243: String fieldName = "_" + i;
1244: buf.append("\t\ts.append(\""
1245: + MDStatics.OID_STRING_SEPERATOR + "\");\n");
1246: buf.append("\t\ts.append(" + fieldName + ");\n");
1247: }
1248: buf.append("\t\treturn s.toString();\n\t}\n");
1249: spec.addMethod(buf.toString());
1250: }
1251:
1252: }
|