KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > query > ArgExpr


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.query;
30
31 import java.sql.PreparedStatement;
32 import java.sql.SQLException;
33
34 import com.caucho.util.CharBuffer;
35
36 import com.caucho.amber.type.Type;
37
38 /**
39  * Parameter argument expression.
40  */

41 class ArgExpr extends AbstractAmberExpr {
42   private QueryParser _parser;
43   
44   // argument index
45
private int _index;
46   
47   private int _sqlIndex;
48
49
50   /**
51    * Creates a new argument expression.
52    *
53    * @param index the argument index
54    */

55   ArgExpr(QueryParser parser, int index)
56   {
57     _parser = parser;
58     
59     _index = index;
60   }
61
62   /**
63    * Returns the index value
64    */

65   int getIndex()
66   {
67     return _index;
68   }
69
70   /**
71    * Binds the expression as a select item.
72    */

73   public AmberExpr bindSelect(QueryParser parser)
74   {
75     parser.addArg(this);
76           
77     return this;
78   }
79
80   /**
81    * Generates the literal.
82    */

83   public void generateWhere(CharBuffer cb)
84   {
85     _sqlIndex = _parser.generateSQLArg();
86     
87     cb.append("?");
88   }
89
90   /**
91    * Sets the parameter.
92    */

93   public void setParameter(PreparedStatement pstmt, int i,
94                Type []argTypes, Object []argValues)
95     throws SQLException
96   {
97     if (argTypes[_index - 1] != null)
98       argTypes[_index - 1].setParameter(pstmt, _sqlIndex + 1,
99                     argValues[_index - 1]);
100     else
101       pstmt.setString(_sqlIndex + 1, null);
102   }
103
104   public String toString()
105   {
106     return "?" + _index;
107   }
108 }
109
Popular Tags