KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > fetch > FopGetOID


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

12 package com.versant.core.jdbc.fetch;
13
14 import com.versant.core.metadata.ClassMetaData;
15 import com.versant.core.metadata.MDStatics;
16 import com.versant.core.jdbc.sql.exp.SelectExp;
17 import com.versant.core.jdbc.sql.exp.SqlExp;
18 import com.versant.core.jdbc.metadata.JdbcClass;
19 import com.versant.core.jdbc.metadata.JdbcColumn;
20 import com.versant.core.jdbc.metadata.JdbcTable;
21 import com.versant.core.jdbc.JdbcOID;
22 import com.versant.core.common.OID;
23
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27 /**
28  * A fetch of the OID (primary key) for a class. This when there is an object
29  * in the ResultSet whose primary key is not available elsewhere or otherwise
30  * read as part of a State. Example 'SELECT o FROM Order AS o' will use one
31  * of these to get the OID for each Order returned.
32  */

33 public class FopGetOID extends FetchOp {
34
35     private final FetchOpData src;
36     private final ClassMetaData cmd;
37     private final Data data;
38
39     private int firstColIndex; // the index of our first select list col
40

41     /**
42      * This gets our OID from fetchData and delegates to our src for
43      * the OID and ResultSet.
44      */

45     public class Data extends FetchOpDataProxy {
46
47         public Data(FetchOpData src) {
48             super(src);
49         }
50
51         public void setOID(FetchResultImp fetchResult, OID oid) {
52             fetchResult.setData(FopGetOID.this, oid);
53         }
54
55         public OID getOID(FetchResultImp fetchResult) {
56             return (OID)fetchResult.getData(FopGetOID.this);
57         }
58
59         public String JavaDoc getDescription() {
60             return " [" + getIndex() + "]";
61         }
62     }
63
64     public FetchOpData getOutputData() {
65         return data;
66     }
67
68     /**
69      * Fetch an OID for the heirachy based on cmd.
70      */

71     public FopGetOID(FetchSpec spec, FetchOpData src, ClassMetaData cmd) {
72         super(spec);
73         this.src = src;
74         this.cmd = cmd.top;
75         data = new Data(src);
76     }
77
78     /**
79      * Init this FetchOp and return the expressions that we need to be added
80      * to the select list of the query or null if none. The FetchOp may
81      * add additional FetchOp's to the spec.
82      */

83     public SqlExp init(SelectExp root, int firstColIndex) {
84         this.firstColIndex = firstColIndex;
85         JdbcClass jdbcClass = (JdbcClass)cmd.storeClass;
86         JdbcTable table = jdbcClass.table;
87         return JdbcColumn.toSqlExp(table.pk, root);
88     }
89
90     public void fetch(FetchResultImp fetchResult) throws SQLException JavaDoc {
91         ResultSet JavaDoc rs = src.getResultSet(fetchResult);
92         if (rs == null) {
93             return; // nothing to fetch
94
}
95         JdbcOID oid = (JdbcOID)cmd.createOID(!cmd.isInHeirachy());
96         if (oid.copyKeyFields(rs, firstColIndex)) {
97             data.setOID(fetchResult, oid);
98         }
99     }
100
101     public String JavaDoc getDescription() {
102         return cmd.qname + src.getDescription();
103     }
104
105     public int getResultType() {
106         return MDStatics.OID;
107     }
108
109     public Object JavaDoc getResult(FetchResultImp fetchResult) {
110         return data.getOID(fetchResult);
111     }
112
113 }
114
115
Popular Tags