KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.BitConstantNode
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.BitDataValue;
25 import org.apache.derby.iapi.types.TypeId;
26 import org.apache.derby.iapi.types.DataValueFactory;
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
34 import org.apache.derby.iapi.services.io.FormatableBitSet;
35 import org.apache.derby.iapi.util.ReuseFactory;
36 import org.apache.derby.iapi.reference.ClassName;
37 import org.apache.derby.iapi.services.classfile.VMOpcode;
38
39 import java.sql.Types JavaDoc;
40
41 public class BitConstantNode extends ConstantNode
42 {
43
44     private int bitLength;
45
46
47     /**
48      * Initializer for a BitConstantNode.
49      *
50      * @param arg1 A Bit containing the value of the constant OR The TypeId for the type of the node
51      *
52      * @exception StandardException
53      */

54
55     public void init(
56                     Object JavaDoc arg1)
57         throws StandardException
58     {
59         super.init(
60                     arg1,
61                     Boolean.TRUE,
62                     ReuseFactory.getInteger(0));
63     }
64
65     public void init(
66                     Object JavaDoc arg1, Object JavaDoc arg2)
67         throws StandardException
68     {
69         String JavaDoc a1 = (String JavaDoc) arg1;
70
71         byte[] nv = org.apache.derby.iapi.util.StringUtil.fromHexString(a1, 0, a1.length());
72
73         Integer JavaDoc bitLengthO = (Integer JavaDoc) arg2;
74         bitLength = bitLengthO.intValue();
75
76         init(
77             TypeId.getBuiltInTypeId(Types.BINARY),
78             Boolean.FALSE,
79             bitLengthO);
80
81         org.apache.derby.iapi.types.BitDataValue dvd = getDataValueFactory().getBitDataValue(nv);
82
83         dvd.setWidth(bitLength, 0, false);
84
85         setValue(dvd);
86     }
87
88     /**
89      * Initializer for non-numeric types. Needed for our subclasses
90      *
91      * @param typeId The Type ID of the datatype
92      * @param nullable True means the constant is nullable
93      * @param maximumWidth The maximum number of bytes in the data value
94      *
95      * @exception StandardException
96      */

97     public void init(
98             Object JavaDoc typeId,
99             Object JavaDoc nullable,
100             Object JavaDoc maximumWidth)
101         throws StandardException
102     {
103         init(
104                     typeId,
105                     ReuseFactory.getInteger(0),
106                     ReuseFactory.getInteger(0),
107                     nullable,
108                     maximumWidth);
109     }
110
111
112     /**
113      * Return an Object representing the bind time value of this
114      * expression tree. If the expression tree does not evaluate to
115      * a constant at bind time then we return null.
116      * This is useful for bind time resolution of VTIs.
117      * RESOLVE: What do we do for primitives?
118      *
119      * @return An Object representing the bind time value of this expression tree.
120      * (null if not a bind time constant.)
121      *
122      * @exception StandardException Thrown on error
123      */

124     Object JavaDoc getConstantValueAsObject()
125         throws StandardException
126     {
127         return value.getBytes();
128     }
129
130     /**
131      * This generates the proper constant. It is implemented
132      * by every specific constant node (e.g. IntConstantNode).
133      *
134      * @param acb The ExpressionClassBuilder for the class being built
135      * @param mb The method the code to place the code
136      * @exception StandardException Thrown on error
137      */

138     void generateConstant(ExpressionClassBuilder acb, MethodBuilder mb)
139         throws StandardException
140     {
141         byte[] bytes = value.getBytes();
142
143         String JavaDoc hexLiteral = org.apache.derby.iapi.util.StringUtil.toHexString(bytes, 0, bytes.length);
144
145         mb.push(hexLiteral);
146         mb.push(0);
147         mb.push(hexLiteral.length());
148
149         mb.callMethod(VMOpcode.INVOKESTATIC, "org.apache.derby.iapi.util.StringUtil", "fromHexString",
150                         "byte[]", 3);
151     }
152 }
153
Popular Tags