KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > jdbc > typing > MappingInfo


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2003 XQuark Group.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
19  * You can also get it at http://www.gnu.org/licenses/lgpl.html
20  *
21  * For more information on this software, see http://www.xquark.org.
22  */

23
24 package org.xquark.jdbc.typing;
25
26 import java.math.BigDecimal JavaDoc;
27 import java.sql.*;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Types JavaDoc;
31
32 import org.xquark.schema.SchemaException;
33 import org.xquark.schema.SimpleType;
34 import org.xquark.schema.datatypes.*;
35 import org.xquark.schema.datatypes.PrimitiveType;
36 import org.xquark.schema.datatypes.QName;
37 import org.xquark.schema.datatypes.URI;
38
39 /** This class describes features of the database corresponding type
40  * for a datum to map, basing on the XML schema type.
41  *
42  * <p>Performs checks between XML type and SQL type.</p>
43  *
44  * <p>The type info build is the natural mapping between schema types
45  * and JDBC. It is not built from the database metadata but will be
46  * checked against it.</p>
47  */

48 public class MappingInfo extends JavaTypeInfo implements DBMSConstants
49 {
50     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
51     private static final String JavaDoc RCSName = "$Name: $";
52     
53     /** mapping between schema type and JDBC base (abstract) types */
54     public static int ABSTRACT_SCHEMA_JDBC_MAPPING[] = new int[33];
55     
56     static
57     {
58         // mapping between schema type and base JDBC base types
59
ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.STRING] = Types.VARCHAR;
60         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.BOOLEAN] = Types.DECIMAL;
61         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.DECIMAL] = Types.DECIMAL;
62         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.FLOAT] = Types.FLOAT;
63         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.DOUBLE] = Types.DOUBLE;
64         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.QNAME] = Types.VARCHAR;
65         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.NOTATION] = Types.VARCHAR;
66         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.DURATION] = Types.VARCHAR;
67         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.DATE_TIME] = Types.TIMESTAMP;
68         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.TIME] = Types.TIMESTAMP;
69         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.DATE] = Types.TIMESTAMP;
70         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.GYEAR_MONTH] = Types.TIMESTAMP;
71         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.GYEAR] = Types.TIMESTAMP;
72         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.GMONTH_DAY] = Types.TIMESTAMP;
73         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.GDAY] = Types.TIMESTAMP;
74         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.GMONTH] = Types.TIMESTAMP;
75         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.HEX_BINARY] = Types.VARBINARY;
76         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.BASE64_BINARY] = Types.VARBINARY;
77         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.ANY_URI] = Types.VARCHAR;
78         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.UNION] = Types.VARCHAR; // For generation, a VARCHAR column, may accept almost anything...
79
ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.LIST] = Types.VARCHAR;
80         ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.ANYSIMPLETYPE] = Types.VARCHAR;
81     }
82     
83     /* setString() and getString() can be used */
84     protected boolean isJDBCStringIOAllowed = true; // for default mapping
85

86     /* setCharacterStream() and set() must be used */
87     protected boolean useStreams = false; // for default mapping
88

89     /**
90      * No conversion to performs since the schema output fits direcly the
91      * column JDBC type.
92      */

93     protected static final short NONE = 0;
94     /** JDBC performed */
95     protected static final short IMPLICIT = 1;
96     /**
97      * Performed by mapper specific code. See
98      * {@link #storeConvertedToSQL(PreparedStatement, int, Object)}.
99      */

100     protected static final short AUTOMATIC = 2;
101     
102     /** User-defined (NYI) */
103     protected static final short EXPLICIT = 3;
104     
105     /** Forbidden */
106     protected static final short IMPOSSIBLE = 4;
107     
108     /** Flag used to determine if a conversion is needed between the schema
109      * output and JDBC
110      */

111     protected short conversion = IMPOSSIBLE; // for default mapping
112

113     protected DbType dbType = null;
114     protected boolean targetIsString;
115     /* use string delimitor if target is String */
116     protected boolean useStringDelimitor = false;
117     
118     //////////////////////////////////////////////////////////////////////////
119
// CONSTRUCTORS & ACCESSORS
120
//////////////////////////////////////////////////////////////////////////
121
protected MappingInfo() {}
122
123     /** Constructor used for user generators
124      */

125     public MappingInfo(String JavaDoc XMLType)
126     {
127         super(XMLType);
128     }
129     
130     /* Used for value generators */
131     public MappingInfo(SimpleType XMLType)
132     {
133         super(XMLType);
134     }
135     
136     public MappingInfo(SimpleType XMLType, DbType cmeta)
137     {
138         super(XMLType);
139         setDBType(cmeta);
140     }
141     
142     protected void setDBType(DbType cmeta) {
143         // keep columnMetadata
144
this.dbType = cmeta;
145         if (cmeta.isLongType())
146             useStreams = true;
147         
148         int targetJDBCType = cmeta.getJDBCType();
149         targetIsString = (targetJDBCType == Types.CHAR)
150                         || (targetJDBCType == Types.VARCHAR)
151                         || (targetJDBCType == Types.LONGVARCHAR);
152         
153         
154         // set the isJDBCStringIOAllowed flag
155
isJDBCStringIOAllowed = targetIsString || (javaType == JAVA_STRING);
156     }
157     
158     public DbType getDBType()
159     {
160         return dbType;
161     }
162
163     /**
164      * true if target is a SQL String Type or the schema convert output is a
165      * Java String. In other words, generator returns a value that is of type
166      * String because the actual value is a String or because the normalized or
167      * canonical value is used.
168      */

169     public boolean useJDBCStringMethods()
170     {
171         return isJDBCStringIOAllowed;
172     }
173     
174     /**
175      * true if target is a SQL String Type and the schema convert output is not
176      * a Java String.
177      */

178     public boolean storeCanonicalString()
179     {
180         return isJDBCStringIOAllowed && (javaType != JAVA_STRING);
181     }
182     
183     public void setParameter(PreparedStatement JavaDoc pStmt, int i, String JavaDoc value)
184     throws SQLException JavaDoc, SchemaException
185     {
186         Object JavaDoc oValue = sType.convert(value);
187         switch(javaType) {
188             case JAVA_STRING :
189                 pStmt.setString(i, (String JavaDoc)oValue);
190                 break;
191             case SCHEMA_QNAME :
192                 pStmt.setString(i, ((QName)oValue).toString());
193                 break;
194             case SCHEMA_URI :
195                 pStmt.setString(i, ((URI)oValue).toString());
196                 break;
197             case SCHEMA_DATETIME :
198                 pStmt.setTimestamp(i, new Timestamp(((DateTime)oValue).getTime())); // TODO: consider TZ
199
break;
200             case SCHEMA_DURATION :
201                 pStmt.setString(i, ((Duration)oValue).toString());
202                 break;
203             case JAVA_LONG :
204                 pStmt.setLong(i, ((Long JavaDoc)oValue).longValue());
205                 break;
206             case JAVA_BIG_DECIMAL :
207                 if (oValue instanceof Long JavaDoc)
208                     pStmt.setLong(i, ((Long JavaDoc)oValue).longValue());
209                 else
210                     pStmt.setBigDecimal(i, (BigDecimal JavaDoc)oValue);
211                 break;
212             case JAVA_FLOAT :
213                 pStmt.setFloat(i, ((Float JavaDoc)oValue).floatValue());
214                 break;
215             case JAVA_DOUBLE :
216                 pStmt.setDouble(i, ((Double JavaDoc)oValue).doubleValue());
217                 break;
218             case JAVA_BOOLEAN :
219                 pStmt.setBoolean(i, ((Boolean JavaDoc)oValue).booleanValue());
220                 break;
221             case SCHEMA_BYTE_ARRAY :
222                 pStmt.setBytes(i, ((ByteArray) oValue).getData());
223                 break;
224             default:
225         }
226     }
227
228     public String JavaDoc toString()
229     {
230         return "[javaType=" + javaType + ",string=" + isJDBCStringIOAllowed + ",targetJDBC=" + dbType.getJDBCType() + "]";
231     }
232 }
233
Popular Tags