001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.BitConstantNode
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.compile;
023:
024: import org.apache.derby.iapi.types.BitDataValue;
025: import org.apache.derby.iapi.types.TypeId;
026: import org.apache.derby.iapi.types.DataValueFactory;
027:
028: import org.apache.derby.iapi.error.StandardException;
029:
030: import org.apache.derby.iapi.services.compiler.MethodBuilder;
031:
032: import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
033:
034: import org.apache.derby.iapi.services.io.FormatableBitSet;
035: import org.apache.derby.iapi.util.ReuseFactory;
036: import org.apache.derby.iapi.reference.ClassName;
037: import org.apache.derby.iapi.services.classfile.VMOpcode;
038:
039: import java.sql.Types;
040:
041: public class BitConstantNode extends ConstantNode {
042:
043: private int bitLength;
044:
045: /**
046: * Initializer for a BitConstantNode.
047: *
048: * @param arg1 A Bit containing the value of the constant OR The TypeId for the type of the node
049: *
050: * @exception StandardException
051: */
052:
053: public void init(Object arg1) throws StandardException {
054: super .init(arg1, Boolean.TRUE, ReuseFactory.getInteger(0));
055: }
056:
057: public void init(Object arg1, Object arg2) throws StandardException {
058: String a1 = (String) arg1;
059:
060: byte[] nv = org.apache.derby.iapi.util.StringUtil
061: .fromHexString(a1, 0, a1.length());
062:
063: Integer bitLengthO = (Integer) arg2;
064: bitLength = bitLengthO.intValue();
065:
066: init(TypeId.getBuiltInTypeId(Types.BINARY), Boolean.FALSE,
067: bitLengthO);
068:
069: org.apache.derby.iapi.types.BitDataValue dvd = getDataValueFactory()
070: .getBitDataValue(nv);
071:
072: dvd.setWidth(bitLength, 0, false);
073:
074: setValue(dvd);
075: }
076:
077: /**
078: * Initializer for non-numeric types. Needed for our subclasses
079: *
080: * @param typeId The Type ID of the datatype
081: * @param nullable True means the constant is nullable
082: * @param maximumWidth The maximum number of bytes in the data value
083: *
084: * @exception StandardException
085: */
086: public void init(Object typeId, Object nullable, Object maximumWidth)
087: throws StandardException {
088: init(typeId, ReuseFactory.getInteger(0), ReuseFactory
089: .getInteger(0), nullable, maximumWidth);
090: }
091:
092: /**
093: * Return an Object representing the bind time value of this
094: * expression tree. If the expression tree does not evaluate to
095: * a constant at bind time then we return null.
096: * This is useful for bind time resolution of VTIs.
097: * RESOLVE: What do we do for primitives?
098: *
099: * @return An Object representing the bind time value of this expression tree.
100: * (null if not a bind time constant.)
101: *
102: * @exception StandardException Thrown on error
103: */
104: Object getConstantValueAsObject() throws StandardException {
105: return value.getBytes();
106: }
107:
108: /**
109: * This generates the proper constant. It is implemented
110: * by every specific constant node (e.g. IntConstantNode).
111: *
112: * @param acb The ExpressionClassBuilder for the class being built
113: * @param mb The method the code to place the code
114: * @exception StandardException Thrown on error
115: */
116: void generateConstant(ExpressionClassBuilder acb, MethodBuilder mb)
117: throws StandardException {
118: byte[] bytes = value.getBytes();
119:
120: String hexLiteral = org.apache.derby.iapi.util.StringUtil
121: .toHexString(bytes, 0, bytes.length);
122:
123: mb.push(hexLiteral);
124: mb.push(0);
125: mb.push(hexLiteral.length());
126:
127: mb.callMethod(VMOpcode.INVOKESTATIC,
128: "org.apache.derby.iapi.util.StringUtil",
129: "fromHexString", "byte[]", 3);
130: }
131: }
|