KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > lib > sql > QueryParameter


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: QueryParameter.java,v 1.11 2004/02/11 17:56:36 minchau Exp $
18  */

19
20 /* This class holds a parameter definition for a JDBC PreparedStatement or CallableStatement. */
21
22 package org.apache.xalan.lib.sql;
23
24 import java.util.Hashtable JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.CallableStatement JavaDoc;
27 import java.sql.Statement JavaDoc;
28
29 public class QueryParameter
30 {
31   private int m_type;
32   private String JavaDoc m_name;
33   private String JavaDoc m_value;
34   private boolean m_output;
35   private String JavaDoc m_typeName;
36   private static Hashtable JavaDoc m_Typetable = null;
37
38   public QueryParameter()
39   {
40     m_type = -1;
41     m_name = null;
42     m_value = null;
43     m_output = false;
44     m_typeName = null;
45   }
46
47   /**
48    * @param v The parameter value.
49    * @param t The type of the parameter.
50    */

51   public QueryParameter( String JavaDoc v, String JavaDoc t )
52   {
53     m_name = null;
54     m_value = v;
55     m_output = false;
56     setTypeName(t);
57   }
58
59   public QueryParameter( String JavaDoc name, String JavaDoc value, String JavaDoc type, boolean out_flag )
60   {
61     m_name = name;
62     m_value = value;
63     m_output = out_flag;
64     setTypeName(type);
65   }
66
67   /**
68    *
69    */

70   public String JavaDoc getValue( ) {
71     return m_value;
72   }
73
74   /**
75    * @param newValue
76    *
77    */

78   public void setValue( String JavaDoc newValue ) {
79     m_value = newValue;
80   }
81
82   /** Used to set the parameter type when the type information is provided in the query.
83    * @param newType The parameter type.
84    *
85    */

86   public void setTypeName( String JavaDoc newType )
87   {
88     m_type = map_type(newType);
89     m_typeName = newType;
90   }
91
92   /**
93    *
94    */

95   public String JavaDoc getTypeName( )
96   {
97     return m_typeName;
98   }
99
100   /**
101    *
102    */

103   public int getType( )
104   {
105     return m_type;
106   }
107
108   /**
109    *
110    */

111   public String JavaDoc getName()
112   {
113     return m_name;
114   }
115
116   /**
117    * Set Name, this should really be covered in the constructor but the
118    * QueryParser has a State issue where the name is discoverd after the
119    * Parameter object needs to be created
120    */

121   public void setName(String JavaDoc n)
122   {
123     m_name = n;
124   }
125
126   /**
127   *
128   */

129   public boolean isOutput()
130   {
131     return m_output;
132   }
133
134   /**
135    * Set Name, this should really be covered in the constructor but the
136    * QueryParser has a State issue where the name is discoverd after the
137    * Parameter object needs to be created
138    */

139   public void setIsOutput(boolean flag)
140   {
141     m_output = flag;
142   }
143
144   private static int map_type(String JavaDoc typename)
145   {
146     if ( m_Typetable == null )
147     {
148       // Load up the type mapping table.
149
m_Typetable = new Hashtable JavaDoc();
150       m_Typetable.put("BIGINT", new Integer JavaDoc(java.sql.Types.BIGINT));
151       m_Typetable.put("BINARY", new Integer JavaDoc(java.sql.Types.BINARY));
152       m_Typetable.put("BIT", new Integer JavaDoc(java.sql.Types.BIT));
153       m_Typetable.put("CHAR", new Integer JavaDoc(java.sql.Types.CHAR));
154       m_Typetable.put("DATE", new Integer JavaDoc(java.sql.Types.DATE));
155       m_Typetable.put("DECIMAL", new Integer JavaDoc(java.sql.Types.DECIMAL));
156       m_Typetable.put("DOUBLE", new Integer JavaDoc(java.sql.Types.DOUBLE));
157       m_Typetable.put("FLOAT", new Integer JavaDoc(java.sql.Types.FLOAT));
158       m_Typetable.put("INTEGER", new Integer JavaDoc(java.sql.Types.INTEGER));
159       m_Typetable.put("LONGVARBINARY", new Integer JavaDoc(java.sql.Types.LONGVARBINARY));
160       m_Typetable.put("LONGVARCHAR", new Integer JavaDoc(java.sql.Types.LONGVARCHAR));
161       m_Typetable.put("NULL", new Integer JavaDoc(java.sql.Types.NULL));
162       m_Typetable.put("NUMERIC", new Integer JavaDoc(java.sql.Types.NUMERIC));
163       m_Typetable.put("OTHER", new Integer JavaDoc(java.sql.Types.OTHER));
164       m_Typetable.put("REAL", new Integer JavaDoc(java.sql.Types.REAL));
165       m_Typetable.put("SMALLINT", new Integer JavaDoc(java.sql.Types.SMALLINT));
166       m_Typetable.put("TIME", new Integer JavaDoc(java.sql.Types.TIME));
167       m_Typetable.put("TIMESTAMP", new Integer JavaDoc(java.sql.Types.TIMESTAMP));
168       m_Typetable.put("TINYINT", new Integer JavaDoc(java.sql.Types.TINYINT));
169       m_Typetable.put("VARBINARY", new Integer JavaDoc(java.sql.Types.VARBINARY));
170       m_Typetable.put("VARCHAR", new Integer JavaDoc(java.sql.Types.VARCHAR));
171
172       // Aliases from Xalan SQL extension.
173
m_Typetable.put("STRING", new Integer JavaDoc(java.sql.Types.VARCHAR));
174       m_Typetable.put("BIGDECIMAL", new Integer JavaDoc(java.sql.Types.NUMERIC));
175       m_Typetable.put("BOOLEAN", new Integer JavaDoc(java.sql.Types.BIT));
176       m_Typetable.put("BYTES", new Integer JavaDoc(java.sql.Types.LONGVARBINARY));
177       m_Typetable.put("LONG", new Integer JavaDoc(java.sql.Types.BIGINT));
178       m_Typetable.put("SHORT", new Integer JavaDoc(java.sql.Types.SMALLINT));
179     }
180
181     Integer JavaDoc type = (Integer JavaDoc) m_Typetable.get(typename.toUpperCase());
182     int rtype;
183     if ( type == null )
184       rtype = java.sql.Types.OTHER;
185     else
186       rtype = type.intValue();
187
188     return(rtype);
189   }
190
191   /**
192    * This code was in the XConnection, it is included for reference but it
193    * should not be used.
194    *
195    * @TODO Remove this code as soon as it is determined that its Use Case is
196    * resolved elsewhere.
197    */

198   /**
199    * Set the parameter for a Prepared Statement
200    * @param pos
201    * @param stmt
202    * @param p
203    *
204    * @throws SQLException
205    */

206   /*
207   private void setParameter( int pos, PreparedStatement stmt, QueryParameter p )throws SQLException
208   {
209     String type = p.getType();
210     if (type.equalsIgnoreCase("string"))
211     {
212       stmt.setString(pos, p.getValue());
213     }
214
215     if (type.equalsIgnoreCase("bigdecimal"))
216     {
217       stmt.setBigDecimal(pos, new BigDecimal(p.getValue()));
218     }
219
220     if (type.equalsIgnoreCase("boolean"))
221     {
222       Integer i = new Integer( p.getValue() );
223       boolean b = ((i.intValue() != 0) ? false : true);
224       stmt.setBoolean(pos, b);
225     }
226
227     if (type.equalsIgnoreCase("bytes"))
228     {
229       stmt.setBytes(pos, p.getValue().getBytes());
230     }
231
232     if (type.equalsIgnoreCase("date"))
233     {
234       stmt.setDate(pos, Date.valueOf(p.getValue()));
235     }
236
237     if (type.equalsIgnoreCase("double"))
238     {
239       Double d = new Double(p.getValue());
240       stmt.setDouble(pos, d.doubleValue() );
241     }
242
243     if (type.equalsIgnoreCase("float"))
244     {
245       Float f = new Float(p.getValue());
246       stmt.setFloat(pos, f.floatValue());
247     }
248
249     if (type.equalsIgnoreCase("long"))
250     {
251       Long l = new Long(p.getValue());
252       stmt.setLong(pos, l.longValue());
253     }
254
255     if (type.equalsIgnoreCase("short"))
256     {
257       Short s = new Short(p.getValue());
258       stmt.setShort(pos, s.shortValue());
259     }
260
261     if (type.equalsIgnoreCase("time"))
262     {
263       stmt.setTime(pos, Time.valueOf(p.getValue()) );
264     }
265
266     if (type.equalsIgnoreCase("timestamp"))
267     {
268
269       stmt.setTimestamp(pos, Timestamp.valueOf(p.getValue()) );
270     }
271
272   }
273   */

274
275 }
276
277
278
Popular Tags