2
* Licensed to the Apache Software Foundation (ASF) under one
3
* or more contributor license agreements. See the NOTICE file
4
* distributed with this work for additional information
5
* regarding copyright ownership. The ASF licenses this file
6
* to you under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
* $Id: QueryParameter.java,v 1.2 2009/12/10 03:18:45 matthewoliver Exp $
22
/* This class holds a parameter definition for a JDBC PreparedStatement or CallableStatement. */
24
package org.apache.xalan.lib.sql;
26
import java.util.Hashtable;
27
import java.sql.PreparedStatement;
28
import java.sql.CallableStatement;
29
import java.sql.Statement;
31
public class QueryParameter
34
private String m_name;
35
private String m_value;
36
private boolean m_output;
37
private String m_typeName;
38
private static Hashtable m_Typetable = null;
40
public QueryParameter()
50
* @param v The parameter value.
51
* @param t The type of the parameter.
53
public QueryParameter( String v, String t )
61
public QueryParameter( String name, String value, String type, boolean out_flag )
72
public String getValue( ) {
80
public void setValue( String newValue ) {
84
/** Used to set the parameter type when the type information is provided in the query.
85
* @param newType The parameter type.
88
public void setTypeName( String newType )
90
m_type = map_type(newType);
97
public String getTypeName( )
105
public int getType( )
113
public String getName()
119
* Set Name, this should really be covered in the constructor but the
120
* QueryParser has a State issue where the name is discoverd after the
121
* Parameter object needs to be created
123
public void setName(String n)
131
public boolean isOutput()
137
* Set Name, this should really be covered in the constructor but the
138
* QueryParser has a State issue where the name is discoverd after the
139
* Parameter object needs to be created
141
public void setIsOutput(boolean flag)
146
private static int map_type(String typename)
148
if ( m_Typetable == null )
150
// Load up the type mapping table.
151
m_Typetable = new Hashtable();
152
m_Typetable.put("BIGINT", new Integer(java.sql.Types.BIGINT));
153
m_Typetable.put("BINARY", new Integer(java.sql.Types.BINARY));
154
m_Typetable.put("BIT", new Integer(java.sql.Types.BIT));
155
m_Typetable.put("CHAR", new Integer(java.sql.Types.CHAR));
156
m_Typetable.put("DATE", new Integer(java.sql.Types.DATE));
157
m_Typetable.put("DECIMAL", new Integer(java.sql.Types.DECIMAL));
158
m_Typetable.put("DOUBLE", new Integer(java.sql.Types.DOUBLE));
159
m_Typetable.put("FLOAT", new Integer(java.sql.Types.FLOAT));
160
m_Typetable.put("INTEGER", new Integer(java.sql.Types.INTEGER));
161
m_Typetable.put("LONGVARBINARY", new Integer(java.sql.Types.LONGVARBINARY));
162
m_Typetable.put("LONGVARCHAR", new Integer(java.sql.Types.LONGVARCHAR));
163
m_Typetable.put("NULL", new Integer(java.sql.Types.NULL));
164
m_Typetable.put("NUMERIC", new Integer(java.sql.Types.NUMERIC));
165
m_Typetable.put("OTHER", new Integer(java.sql.Types.OTHER));
166
m_Typetable.put("REAL", new Integer(java.sql.Types.REAL));
167
m_Typetable.put("SMALLINT", new Integer(java.sql.Types.SMALLINT));
168
m_Typetable.put("TIME", new Integer(java.sql.Types.TIME));
169
m_Typetable.put("TIMESTAMP", new Integer(java.sql.Types.TIMESTAMP));
170
m_Typetable.put("TINYINT", new Integer(java.sql.Types.TINYINT));
171
m_Typetable.put("VARBINARY", new Integer(java.sql.Types.VARBINARY));
172
m_Typetable.put("VARCHAR", new Integer(java.sql.Types.VARCHAR));
174
// Aliases from Xalan SQL extension.
175
m_Typetable.put("STRING", new Integer(java.sql.Types.VARCHAR));
176
m_Typetable.put("BIGDECIMAL", new Integer(java.sql.Types.NUMERIC));
177
m_Typetable.put("BOOLEAN", new Integer(java.sql.Types.BIT));
178
m_Typetable.put("BYTES", new Integer(java.sql.Types.LONGVARBINARY));
179
m_Typetable.put("LONG", new Integer(java.sql.Types.BIGINT));
180
m_Typetable.put("SHORT", new Integer(java.sql.Types.SMALLINT));
183
Integer type = (Integer) m_Typetable.get(typename.toUpperCase());
186
rtype = java.sql.Types.OTHER;
188
rtype = type.intValue();
194
* This code was in the XConnection, it is included for reference but it
195
* should not be used.
197
* @TODO Remove this code as soon as it is determined that its Use Case is
198
* resolved elsewhere.
201
* Set the parameter for a Prepared Statement
206
* @throws SQLException
209
private void setParameter( int pos, PreparedStatement stmt, QueryParameter p )throws SQLException
211
String type = p.getType();
212
if (type.equalsIgnoreCase("string"))
214
stmt.setString(pos, p.getValue());
217
if (type.equalsIgnoreCase("bigdecimal"))
219
stmt.setBigDecimal(pos, new BigDecimal(p.getValue()));
222
if (type.equalsIgnoreCase("boolean"))
224
Integer i = new Integer( p.getValue() );
225
boolean b = ((i.intValue() != 0) ? false : true);
226
stmt.setBoolean(pos, b);
229
if (type.equalsIgnoreCase("bytes"))
231
stmt.setBytes(pos, p.getValue().getBytes());
234
if (type.equalsIgnoreCase("date"))
236
stmt.setDate(pos, Date.valueOf(p.getValue()));
239
if (type.equalsIgnoreCase("double"))
241
Double d = new Double(p.getValue());
242
stmt.setDouble(pos, d.doubleValue() );
245
if (type.equalsIgnoreCase("float"))
247
Float f = new Float(p.getValue());
248
stmt.setFloat(pos, f.floatValue());
251
if (type.equalsIgnoreCase("long"))
253
Long l = new Long(p.getValue());
254
stmt.setLong(pos, l.longValue());
257
if (type.equalsIgnoreCase("short"))
259
Short s = new Short(p.getValue());
260
stmt.setShort(pos, s.shortValue());
263
if (type.equalsIgnoreCase("time"))
265
stmt.setTime(pos, Time.valueOf(p.getValue()) );
268
if (type.equalsIgnoreCase("timestamp"))
271
stmt.setTimestamp(pos, Timestamp.valueOf(p.getValue()) );