001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 /* 019 * $Id: QueryParameter.java 468638 2006-10-28 06:52:06Z minchau $ 020 */ 021 022 /* This class holds a parameter definition for a JDBC PreparedStatement or CallableStatement. */ 023 024 package org.apache.xalan.lib.sql; 025 026 import java.util.Hashtable; 027 import java.sql.PreparedStatement; 028 import java.sql.CallableStatement; 029 import java.sql.Statement; 030 031 public class QueryParameter 032 { 033 private int m_type; 034 private String m_name; 035 private String m_value; 036 private boolean m_output; 037 private String m_typeName; 038 private static Hashtable m_Typetable = null; 039 040 public QueryParameter() 041 { 042 m_type = -1; 043 m_name = null; 044 m_value = null; 045 m_output = false; 046 m_typeName = null; 047 } 048 049 /** 050 * @param v The parameter value. 051 * @param t The type of the parameter. 052 */ 053 public QueryParameter( String v, String t ) 054 { 055 m_name = null; 056 m_value = v; 057 m_output = false; 058 setTypeName(t); 059 } 060 061 public QueryParameter( String name, String value, String type, boolean out_flag ) 062 { 063 m_name = name; 064 m_value = value; 065 m_output = out_flag; 066 setTypeName(type); 067 } 068 069 /** 070 * 071 */ 072 public String getValue( ) { 073 return m_value; 074 } 075 076 /** 077 * @param newValue 078 * 079 */ 080 public void setValue( String newValue ) { 081 m_value = newValue; 082 } 083 084 /** Used to set the parameter type when the type information is provided in the query. 085 * @param newType The parameter type. 086 * 087 */ 088 public void setTypeName( String newType ) 089 { 090 m_type = map_type(newType); 091 m_typeName = newType; 092 } 093 094 /** 095 * 096 */ 097 public String getTypeName( ) 098 { 099 return m_typeName; 100 } 101 102 /** 103 * 104 */ 105 public int getType( ) 106 { 107 return m_type; 108 } 109 110 /** 111 * 112 */ 113 public String getName() 114 { 115 return m_name; 116 } 117 118 /** 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 122 */ 123 public void setName(String n) 124 { 125 m_name = n; 126 } 127 128 /** 129 * 130 */ 131 public boolean isOutput() 132 { 133 return m_output; 134 } 135 136 /** 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 140 */ 141 public void setIsOutput(boolean flag) 142 { 143 m_output = flag; 144 } 145 146 private static int map_type(String typename) 147 { 148 if ( m_Typetable == null ) 149 { 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)); 173 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)); 181 } 182 183 Integer type = (Integer) m_Typetable.get(typename.toUpperCase()); 184 int rtype; 185 if ( type == null ) 186 rtype = java.sql.Types.OTHER; 187 else 188 rtype = type.intValue(); 189 190 return(rtype); 191 } 192 193 /** 194 * This code was in the XConnection, it is included for reference but it 195 * should not be used. 196 * 197 * @TODO Remove this code as soon as it is determined that its Use Case is 198 * resolved elsewhere. 199 */ 200 /** 201 * Set the parameter for a Prepared Statement 202 * @param pos 203 * @param stmt 204 * @param p 205 * 206 * @throws SQLException 207 */ 208 /* 209 private void setParameter( int pos, PreparedStatement stmt, QueryParameter p )throws SQLException 210 { 211 String type = p.getType(); 212 if (type.equalsIgnoreCase("string")) 213 { 214 stmt.setString(pos, p.getValue()); 215 } 216 217 if (type.equalsIgnoreCase("bigdecimal")) 218 { 219 stmt.setBigDecimal(pos, new BigDecimal(p.getValue())); 220 } 221 222 if (type.equalsIgnoreCase("boolean")) 223 { 224 Integer i = new Integer( p.getValue() ); 225 boolean b = ((i.intValue() != 0) ? false : true); 226 stmt.setBoolean(pos, b); 227 } 228 229 if (type.equalsIgnoreCase("bytes")) 230 { 231 stmt.setBytes(pos, p.getValue().getBytes()); 232 } 233 234 if (type.equalsIgnoreCase("date")) 235 { 236 stmt.setDate(pos, Date.valueOf(p.getValue())); 237 } 238 239 if (type.equalsIgnoreCase("double")) 240 { 241 Double d = new Double(p.getValue()); 242 stmt.setDouble(pos, d.doubleValue() ); 243 } 244 245 if (type.equalsIgnoreCase("float")) 246 { 247 Float f = new Float(p.getValue()); 248 stmt.setFloat(pos, f.floatValue()); 249 } 250 251 if (type.equalsIgnoreCase("long")) 252 { 253 Long l = new Long(p.getValue()); 254 stmt.setLong(pos, l.longValue()); 255 } 256 257 if (type.equalsIgnoreCase("short")) 258 { 259 Short s = new Short(p.getValue()); 260 stmt.setShort(pos, s.shortValue()); 261 } 262 263 if (type.equalsIgnoreCase("time")) 264 { 265 stmt.setTime(pos, Time.valueOf(p.getValue()) ); 266 } 267 268 if (type.equalsIgnoreCase("timestamp")) 269 { 270 271 stmt.setTimestamp(pos, Timestamp.valueOf(p.getValue()) ); 272 } 273 274 } 275 */ 276 277 } 278 279