~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/xalan-j_2_7_1/src/org/apache/xalan/lib/sql/QueryParameter.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
9
 *
 
10
 *     http://www.apache.org/licenses/LICENSE-2.0
 
11
 *
 
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.
 
17
 */
 
18
/*
 
19
 * $Id: QueryParameter.java,v 1.2 2009/12/10 03:18:45 matthewoliver Exp $
 
20
 */
 
21
 
 
22
/* This class holds a parameter definition for a JDBC PreparedStatement or CallableStatement. */
 
23
 
 
24
package org.apache.xalan.lib.sql;
 
25
 
 
26
import java.util.Hashtable;
 
27
import java.sql.PreparedStatement;
 
28
import java.sql.CallableStatement;
 
29
import java.sql.Statement;
 
30
 
 
31
public class QueryParameter
 
32
{
 
33
  private int     m_type;
 
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;
 
39
 
 
40
  public QueryParameter()
 
41
  {
 
42
    m_type = -1;
 
43
    m_name = null;
 
44
    m_value = null;
 
45
    m_output = false;
 
46
    m_typeName = null;
 
47
  }
 
48
 
 
49
  /**
 
50
   * @param v The parameter value.
 
51
   * @param t The type of the parameter.
 
52
   */
 
53
  public QueryParameter( String v, String t )
 
54
  {
 
55
    m_name = null;
 
56
    m_value = v;
 
57
    m_output = false;
 
58
    setTypeName(t);
 
59
  }
 
60
 
 
61
  public QueryParameter( String name, String value, String type, boolean out_flag )
 
62
  {
 
63
    m_name = name;
 
64
    m_value = value;
 
65
    m_output = out_flag;
 
66
    setTypeName(type);
 
67
  }
 
68
 
 
69
  /**
 
70
   *
 
71
   */
 
72
  public String getValue( ) {
 
73
    return m_value;
 
74
  }
 
75
 
 
76
  /**
 
77
   * @param newValue
 
78
   *
 
79
   */
 
80
  public void setValue( String newValue ) {
 
81
    m_value = newValue;
 
82
  }
 
83
 
 
84
  /** Used to set the parameter type when the type information is provided in the query.
 
85
   * @param newType The parameter type.
 
86
   *
 
87
   */
 
88
  public void setTypeName( String newType )
 
89
  {
 
90
    m_type = map_type(newType);
 
91
    m_typeName = newType;
 
92
  }
 
93
 
 
94
  /**
 
95
   *
 
96
   */
 
97
  public String getTypeName( )
 
98
  {
 
99
    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