KickJava   Java API By Example, From Geeks To Geeks.

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


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

12 package com.versant.core.jdbc;
13
14 import com.versant.core.common.GenericOID;
15 import com.versant.core.common.OID;
16 import com.versant.core.metadata.ClassMetaData;
17 import com.versant.core.jdbc.metadata.JdbcColumn;
18 import com.versant.core.jdbc.metadata.JdbcField;
19 import com.versant.core.jdbc.metadata.JdbcClass;
20
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24
25 /**
26  * Adds JDBC specific methods to GenericOID.
27  */

28 public class JdbcGenericOID extends GenericOID implements JdbcOID {
29
30     public JdbcGenericOID() {
31     }
32
33     public JdbcGenericOID(ClassMetaData cmd, boolean resolved) {
34         super(cmd, resolved);
35     }
36
37     /**
38      * Populate this OID from the given ResultSet. The firstCol parameter
39      * specifies the column index of the first column to read from rs. If
40      * the primary key consists of multiple columns then they will be
41      * present in the same order as defined in the meta data.
42      *
43      * @return False if OID is 'null', true otherwise
44      */

45     public boolean copyKeyFields(ResultSet JavaDoc rs, int firstCol)
46             throws SQLException JavaDoc {
47         JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
48         for (int i = 0; i < pkc.length; i++) {
49             JdbcColumn c = pkc[i];
50             if (c.converter != null) {
51                 pk[i] = c.converter.get(rs, firstCol++, c);
52             } else {
53                 pk[i] = JdbcUtils.get(rs, firstCol++, c.javaTypeCode, c.scale);
54             }
55             if (rs.wasNull()) return false;
56         }
57         return true;
58     }
59
60     /**
61      * This is for appId
62      */

63     public boolean copyKeyFields(ResultSet JavaDoc rs, JdbcField[] pks, int[] pkFieldIndexs)
64             throws SQLException JavaDoc {
65         for (int j = 0; j < pkFieldIndexs.length; j++) {
66             JdbcColumn c = pks[pkFieldIndexs[j]].mainTableCols[0];
67             if (c.converter != null) {
68                 pk[j] = c.converter.get(rs, pkFieldIndexs[j] + 1, c);
69             } else {
70                 pk[j] = JdbcUtils.get(rs, pkFieldIndexs[j] + 1, c.javaTypeCode, c.scale);
71             }
72             if (rs.wasNull()) return false;
73         }
74         return true;
75     }
76
77     /**
78      * Return false if oid is null.
79      */

80     public boolean validateKeyFields(ResultSet JavaDoc rs, int firstCol) throws SQLException JavaDoc {
81         JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
82         for (int i = 0; i < pkc.length; i++) {
83             JdbcColumn c = pkc[i];
84             if (c.converter != null) {
85                 c.converter.get(rs, firstCol++, c);
86             } else {
87                 JdbcUtils.get(rs, firstCol++, c.javaTypeCode, c.scale);
88             }
89             if (rs.wasNull()) return false;
90         }
91         return true;
92     }
93
94     /**
95      * Set parameters on a PrepareStatement from this OID. The firstParam
96      * parameter specifies the column index of the first parameter to set.
97      * If the primary key consists of multiple parameters then they must
98      * all be set in the same order as defined in the meta data. The new
99      * firstParam value must be returned i.e. if firstParam started as 3 and
100      * our pk consists of 2 columns then 5 must be returned.
101      */

102     public int setParams(PreparedStatement JavaDoc ps, int firstParam)
103             throws SQLException JavaDoc {
104         JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
105         for (int i = 0; i < pkc.length; i++) {
106             JdbcColumn c = pkc[i];
107             if (c.converter != null) {
108                 c.converter.set(ps, firstParam++, c, pk[i]);
109             } else {
110                 JdbcUtils.set(ps, firstParam++, pk[i], c.javaTypeCode,
111                         c.jdbcType);
112             }
113         }
114         return firstParam;
115     }
116
117     /**
118      * Set parameters on a PrepareStatement from this OID. Columns in pkc
119      * that return false from isForUpdate() should be ignored.
120      */

121     public int setParams(PreparedStatement JavaDoc ps, int firstParam,
122             JdbcColumn[] pkc)
123             throws SQLException JavaDoc {
124         for (int i = 0; i < pkc.length; i++) {
125             JdbcColumn c = pkc[i];
126             if (c.isForUpdate()) {
127                 if (c.converter != null) {
128                     c.converter.set(ps, firstParam++, c, pk[i]);
129                 } else {
130                     JdbcUtils.set(ps, firstParam++, pk[i], c.javaTypeCode,
131                             c.jdbcType);
132                 }
133             }
134         }
135         return firstParam;
136     }
137
138     /**
139      * Util method to 'setNull' for oid param.
140      */

141     public static int setNullParams(PreparedStatement JavaDoc ps,
142             int firstParam, ClassMetaData cmd) throws SQLException JavaDoc {
143         JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
144         for (int i = 0; i < pkc.length; i++) {
145             ps.setNull(firstParam++, pkc[i].jdbcType);
146         }
147         return firstParam;
148     }
149
150     protected GenericOID newInstance() {
151         return new JdbcGenericOID();
152     }
153
154     public String JavaDoc toSString() {
155         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
156         s.append("GenericOID@");
157         s.append(Integer.toHexString(System.identityHashCode(this)));
158         s.append(' ');
159         if (cmd == null) {
160             s.append("classIndex ");
161             s.append(cmd.index);
162         } else {
163             String JavaDoc n = cmd.qname;
164             int i = n.lastIndexOf('.');
165             if (i >= 0) n = n.substring(i + 1);
166             s.append(n);
167             s.append(' ');
168             JdbcColumn[] pkc = ((JdbcClass)cmd.storeClass).table.pk;
169             for (i = 0; i < pkc.length; i++) {
170                 JdbcColumn c = pkc[i];
171                 s.append(c.name);
172                 s.append('=');
173                 s.append(pk[i]);
174             }
175         }
176         if (!isResolved()) s.append(" NOTRES ");
177         return s.toString();
178     }
179
180 }
181
182
Popular Tags