KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > metadata > JdbcSimpleField


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.metadata;
13
14 import com.versant.core.jdbc.sql.JdbcNameGenerator;
15 import com.versant.core.jdbc.sql.SqlDriver;
16 import com.versant.core.jdbc.sql.exp.ColumnExp;
17 import com.versant.core.jdbc.sql.exp.SelectExp;
18 import com.versant.core.jdbc.JdbcUtils;
19 import com.versant.core.jdbc.JdbcConverter;
20 import com.versant.core.util.CharBuf;
21 import com.versant.core.common.State;
22
23 import java.util.ArrayList JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27 import com.versant.core.common.BindingSupportImpl;
28
29 /**
30  * A field that fits into a single logical column. Note that this includes
31  * embedded fields like primitive arrays[] (byte[], int[], Integer[] etc.).
32  */

33 public class JdbcSimpleField extends JdbcField {
34
35     /**
36      * The column used to store this field.
37      */

38     public JdbcColumn col;
39
40     private boolean oracleStyleLOB;
41     private String JavaDoc oracleStyleLOBNotNullString;
42
43     /**
44      * Set the table field on all our main table columns.
45      */

46     public void setMainTable(JdbcTable table) {
47         super.setMainTable(table);
48         col.setTable(table);
49     }
50
51     /**
52      * Make sure all of this fields main table columns have names.
53      */

54     public void nameColumns(String JavaDoc tableName, JdbcNameGenerator nameGen) {
55         if (col.name == null) {
56             col.name = nameGen.generateFieldColumnName(tableName, fmd.name, false);
57         } else if (!nameGen.isColumnInTable(tableName, col.name)) {
58             try {
59                 nameGen.addColumnName(tableName, col.name);
60             } catch (IllegalArgumentException JavaDoc e) {
61                 throw BindingSupportImpl.getInstance().runtime(
62                     "Invalid jdbc-column-name for field " + fmd.name + ": " +
63                     e.getMessage() + "\n" + getContext());
64             }
65         }
66     }
67
68     /**
69      * Init the mainTableCols field to all our main table columns.
70      */

71     public void initMainTableCols() {
72         mainTableCols = new JdbcColumn[]{col};
73         JdbcConverter converter = col.converter;
74         if (converter != null) {
75             oracleStyleLOB = converter.isOracleStyleLOB();
76             oracleStyleLOBNotNullString = converter.getOracleStyleLOBNotNullString();
77         } else {
78             oracleStyleLOB = false;
79         }
80         super.initMainTableCols();
81     }
82
83     /**
84      * Flatten all of this fields main table columns to a.
85      */

86     public void addMainTableCols(ArrayList JavaDoc a) {
87         a.add(col);
88     }
89
90     /**
91      * Append part of an update statement for us to s (e.g col = ?).
92      */

93     public boolean appendUpdate(CharBuf s, State state) {
94         s.append(col.name);
95         s.append('=');
96         if (oracleStyleLOB) {
97             if (state.isNull(stateFieldNo)) {
98                 s.append("null");
99             } else {
100                 s.append(oracleStyleLOBNotNullString);
101             }
102             return true;
103         } else {
104             s.append('?');
105             return false;
106         }
107     }
108
109     /**
110      * Append part of a where clause for us to s (e.g cola = ? and colb = ?).
111      * This is used for generating the where clause for changed locking.
112      */

113     public void appendWhere(CharBuf s, SqlDriver sqlDriver) {
114         s.append(col.name);
115         s.append("=");
116         sqlDriver.appendWhereParam(s, col);
117     }
118
119     /**
120      * Append part of a is null where clause for us to s (e.g cola is null
121      * and colb is null).
122      * This is used for generating the where clause for changed locking.
123      */

124     public void appendWhereIsNull(CharBuf s, SqlDriver sqlDriver) {
125         s.append(col.name);
126         s.append(" is null");
127     }
128
129     /**
130      * Append part of the insert list for us to s (e.g. cola, colb)).
131      */

132     public void appendInsertColumnList(CharBuf s) {
133         s.append(col.name);
134     }
135
136     /**
137      * Append part of the insert value list for us to s (e.g. ?, ?)). This
138      * must return true if a replacable parameter was <b>not</b> added (e.g.
139      * columns using Oracle LOBs which put in empty_clob() or whatever).
140      */

141     public boolean appendInsertValueList(CharBuf s, State state) {
142         if (oracleStyleLOB) {
143             if (state.isNull(stateFieldNo)) s.append("null");
144             else s.append(oracleStyleLOBNotNullString);
145             return true;
146         } else {
147             s.append('?');
148             return false;
149         }
150     }
151
152     /**
153      * Convert this field into a list of ColumnExp's or null if this is
154      * not possible.
155      */

156     public ColumnExp toColumnExp(SelectExp se, boolean joinToSuper) {
157         if (joinToSuper) return new ColumnExp(col, SelectExp.createJoinToSuperTable(se, this), this);
158         else return new ColumnExp(col, se, this);
159     }
160
161     public ColumnExp createOwningTableColumnExpList(SelectExp se) {
162         return new ColumnExp(col, se, this);
163     }
164
165     /**
166      * Set this field on a PreparedStatement. This is used to set parameters
167      * for queries.
168      * @return Index of the parameter after the last one we set in ps
169      */

170     public int setQueryParam(PreparedStatement JavaDoc ps, int firstParam, Object JavaDoc value)
171             throws SQLException JavaDoc {
172         if (col.converter != null) {
173             col.converter.set(ps, firstParam++, col, value);
174         } else {
175             JdbcUtils.set(ps, firstParam++, value, col.javaTypeCode, col.jdbcType);
176         }
177         return firstParam;
178     }
179
180     /**
181      * Does this field require the sucky Oracle LOB support on insert/update?
182      */

183     public boolean isOracleStyleLOB() {
184         return oracleStyleLOB;
185     }
186
187 }
188
189
190
Popular Tags