KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > CharConstantNode


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.CharConstantNode
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.types.StringDataValue;
25 import org.apache.derby.iapi.types.TypeId;
26
27
28 import org.apache.derby.iapi.error.StandardException;
29
30 import org.apache.derby.iapi.services.compiler.MethodBuilder;
31
32 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
33 import org.apache.derby.iapi.reference.SQLState;
34
35 import org.apache.derby.iapi.util.ReuseFactory;
36
37 import java.sql.Types JavaDoc;
38
39 public final class CharConstantNode extends ConstantNode
40 {
41     /**
42      * Initializer for a CharConstantNode.
43      *
44      * @param arg1 A String containing the value of the constant OR The TypeId for the type of the node
45      *
46      * @exception StandardException
47      */

48     public void init(
49                     Object JavaDoc arg1)
50         throws StandardException
51     {
52         if (arg1 instanceof TypeId)
53         {
54             super.init(
55                         arg1,
56                         Boolean.TRUE,
57                         ReuseFactory.getInteger(0));
58         }
59         else
60         {
61             String JavaDoc val = (String JavaDoc) arg1;
62
63             super.init(
64                 TypeId.CHAR_ID,
65                 (val == null) ? Boolean.TRUE : Boolean.FALSE,
66                 (val != null) ?
67                     ReuseFactory.getInteger(val.length()) :
68                     ReuseFactory.getInteger(0));
69
70             setValue(getDataValueFactory().getCharDataValue(val));
71         }
72     }
73
74     /**
75      * Initializer for a CharConstantNode of a specific length.
76      *
77      * @param newValue A String containing the value of the constant
78      * @param newLength The length of the new value of the constant
79      *
80      * @exception StandardException
81      */

82     public void init(
83                     Object JavaDoc newValue,
84                     Object JavaDoc newLength)
85         throws StandardException
86     {
87         String JavaDoc val = (String JavaDoc) newValue;
88         int newLen = ((Integer JavaDoc) newLength).intValue();
89
90         super.init(
91              TypeId.CHAR_ID,
92              (val == null) ? Boolean.TRUE : Boolean.FALSE,
93              newLength);
94
95         if (val.length() > newLen)
96         {
97             throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, "CHAR", val, String.valueOf(newLen));
98         }
99
100         // Blank pad the string if necessesary
101
while (val.length() < newLen)
102         {
103             val = val + ' ';
104         }
105
106         setValue(getDataValueFactory().getCharDataValue(val));
107     }
108
109     /**
110      * Return the value from this CharConstantNode
111      *
112      * @return The value of this CharConstantNode.
113      *
114      * @exception StandardException Thrown on error
115      */

116
117     public String JavaDoc getString() throws StandardException
118     {
119         return value.getString();
120     }
121
122     /**
123      * Return the length
124      *
125      * @return The length of the value this node represents
126      *
127      * @exception StandardException Thrown on error
128      */

129
130     //public int getLength() throws StandardException
131
//{
132
// return value.getLength();
133
//}
134

135     /**
136      * Return an Object representing the bind time value of this
137      * expression tree. If the expression tree does not evaluate to
138      * a constant at bind time then we return null.
139      * This is useful for bind time resolution of VTIs.
140      * RESOLVE: What do we do for primitives?
141      *
142      * @return An Object representing the bind time value of this expression tree.
143      * (null if not a bind time constant.)
144      *
145      * @exception StandardException Thrown on error
146      */

147     Object JavaDoc getConstantValueAsObject() throws StandardException
148     {
149         return value.getString();
150     }
151
152     /**
153      * This generates the proper constant. It is implemented
154      * by every specific constant node (e.g. IntConstantNode).
155      *
156      * @param acb The ExpressionClassBuilder for the class being built
157      * @param mb The method the code to place the code
158      *
159      * @exception StandardException Thrown on error
160      */

161     void generateConstant(ExpressionClassBuilder acb, MethodBuilder mb) throws StandardException
162     {
163         // The generated java is the expression:
164
// "#getString()"
165
mb.push(getString());
166     }
167 }
168
Popular Tags