001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc.metadata;
012:
013: import com.versant.core.metadata.StoreFetchGroup;
014: import com.versant.core.metadata.FetchGroup;
015: import com.versant.core.metadata.FieldMetaData;
016: import com.versant.core.metadata.FetchGroupField;
017: import com.versant.core.jdbc.FgDs;
018: import com.versant.core.common.BindingSupportImpl;
019:
020: /**
021: * Extra JDBC specific info attached to a FetchGroup.
022: */
023: public class JdbcFetchGroup implements StoreFetchGroup {
024:
025: public static int OPT_INC_SUBS = 1;
026: public static int OPT_START_OUTER = 2;
027:
028: private FetchGroup fg;
029: private FgDs[] fgDses = new FgDs[4];
030:
031: public JdbcFetchGroup() {
032: }
033:
034: public void setFetchGroup(FetchGroup fg) {
035: this .fg = fg;
036: }
037:
038: public FgDs getFgDs(boolean incSubClasses, boolean outer) {
039: int opts = createOpts(incSubClasses, outer);
040: FgDs fgDs = fgDses[opts];
041: if (fgDs == null) {
042: fgDs = new FgDs(null, fg, fg.name, opts);
043: }
044: return fgDs;
045: }
046:
047: public FgDs getExistingFgDs(boolean incSubClasses, boolean outer) {
048: int opts = createOpts(incSubClasses, outer);
049: FgDs fgDs = fgDses[opts];
050: if (fgDs == null) {
051: StringBuffer msg = new StringBuffer();
052: msg.append("FgDs does not exist: " + fg.classMetaData.qname
053: + "." + fg.name + " incSubClasses " + incSubClasses
054: + " outer " + outer);
055: msg.append("\nincSubClasses outer FgDs");
056: msg.append("\nfalse false "
057: + fgDses[createOpts(false, false)]);
058: msg.append("\nfalse true "
059: + fgDses[createOpts(false, true)]);
060: msg.append("\ntrue false "
061: + fgDses[createOpts(true, false)]);
062: msg.append("\ntrue true "
063: + fgDses[createOpts(true, true)] + "\n");
064: throw BindingSupportImpl.getInstance().internal(
065: msg.toString());
066: }
067: return fgDs;
068: }
069:
070: public static int createOpts(boolean incSubClasses, boolean outer) {
071: int opt = 0;
072: if (incSubClasses)
073: opt += OPT_INC_SUBS;
074: if (outer)
075: opt += OPT_START_OUTER;
076: return opt;
077: }
078:
079: public void setFgDs(FgDs fgDs) {
080: if (!fgDs.isFinished()) {
081: throw BindingSupportImpl.getInstance().internal(
082: "The FetchPlan has not been finished");
083: }
084: fgDses[fgDs.getOpts()] = fgDs;
085: }
086:
087: public void fieldAdded(FieldMetaData fmd) {
088: JdbcField jdbcField = ((JdbcField) fmd.storeField);
089: if (jdbcField.mainTableCols != null) {
090: fg.jdbcTotalCols += jdbcField.mainTableCols.length;
091: }
092: }
093:
094: public void finish() {
095: FetchGroupField[] fields = fg.fields;
096: int nf = fields.length;
097: for (int i = nf - 1; i >= 0; i--) {
098: //select a default crossjoinField
099: if (fg.crossJoinedCollectionField == null
100: && fields[i].fmd.storeField instanceof JdbcCollectionField
101: && !(fields[i].fmd.storeField instanceof JdbcMapField)) {
102: fg.crossJoinedCollectionField = fields[i];
103: }
104: }
105: }
106:
107: }
|