Search in sources :

Example 6 with StringDataValue

use of org.apache.derby.iapi.types.StringDataValue in project derby by apache.

the class UTF8ReaderTest method testRepositioningWithinBufferRealText.

/**
 * Tests repositioning withing buffer with a "real text" to make sure the
 * correct values are returned.
 */
public void testRepositioningWithinBufferRealText() throws IOException, SQLException, StandardException {
    setAutoCommit(false);
    Statement stmt = createStatement();
    ResultSet rs = stmt.executeQuery(// See insertTestData
    "select * from Utf8ReaderTest where id = 1");
    rs.next();
    StringDataValue dvd = (StringDataValue) ((EmbedResultSet) rs).getColumn(3);
    StoreStreamClob ssClob = new StoreStreamClob(dvd.getStreamWithDescriptor(), (EmbedResultSet) rs);
    Reader reader = ssClob.getInternalReader(1);
    assertEquals('B', reader.read());
    reader = ssClob.getInternalReader(24);
    assertEquals('\'', reader.read());
    reader = ssClob.getInternalReader(42);
    assertEquals('H', reader.read());
    reader = ssClob.getInternalReader(70);
    assertEquals('M', reader.read());
    reader = ssClob.getInternalReader(102);
    assertEquals('M', reader.read());
    reader = ssClob.getInternalReader(128);
    assertEquals('B', reader.read());
    reader = ssClob.getInternalReader(155);
    assertEquals('A', reader.read());
    reader = ssClob.getInternalReader(184);
    assertEquals('S', reader.read());
    reader = ssClob.getInternalReader(207);
    assertEquals('H', reader.read());
    reader = ssClob.getInternalReader(224);
    assertEquals('O', reader.read());
    reader = ssClob.getInternalReader(128);
    char[] buf = new char[4];
    assertEquals(4, reader.read(buf));
    assertEquals("But ", new String(buf));
    reader = ssClob.getInternalReader(70);
    buf = new char[32];
    assertEquals(32, reader.read(buf));
    assertEquals("Men the grocer and butcher sent\n", new String(buf));
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) StringDataValue(org.apache.derby.iapi.types.StringDataValue)

Example 7 with StringDataValue

use of org.apache.derby.iapi.types.StringDataValue in project derby by apache.

the class UTF8ReaderTest method testRepositioningWithinBuffer.

/**
 * Tests repositioning withing the buffer.
 */
public void testRepositioningWithinBuffer() throws IOException, SQLException, StandardException {
    setAutoCommit(false);
    Statement stmt = createStatement();
    ResultSet rs = stmt.executeQuery("select * from Utf8ReaderTest where id = 100");
    rs.next();
    StringDataValue dvd = (StringDataValue) ((EmbedResultSet) rs).getColumn(3);
    StoreStreamClob ssClob = new StoreStreamClob(dvd.getStreamWithDescriptor(), (EmbedResultSet) rs);
    Reader reader = ssClob.getInternalReader(1);
    assertEquals('a', reader.read());
    int bufSize = 26000;
    char[] buf = new char[bufSize];
    int count = 0;
    while (count < bufSize) {
        count += reader.read(buf, count, bufSize - count);
    }
    // We have now read 26001 chars. Next char should be 'b'.
    // Internal buffer size after the singel read below should be:
    // 26002 % 8192 = 1426
    assertEquals('b', reader.read());
    reader.close();
    // Get internal readers and do stuff.
    checkInternalStream(26002, ssClob);
    checkInternalStream(26001, ssClob);
    // First char in buffer
    checkInternalStream(26002 - 1426 + 1, ssClob);
    // Last char in buffer
    checkInternalStream(26001 + (8192 - 1426 + 1), ssClob);
    // Requires reset
    checkInternalStream(26002 - 1426, ssClob);
    // Requires refilling buffer
    checkInternalStream(26002 - 1426 + 1, ssClob);
    checkInternalStream(26002, ssClob);
    checkInternalStream(1, ssClob);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) StringDataValue(org.apache.derby.iapi.types.StringDataValue)

Example 8 with StringDataValue

use of org.apache.derby.iapi.types.StringDataValue in project derby by apache.

the class ResultColumn method columnTypeAndLengthMatch.

boolean columnTypeAndLengthMatch(ResultColumn otherColumn) throws StandardException {
    ValueNode otherExpression = otherColumn.getExpression();
    DataTypeDescriptor resultColumnType = getTypeServices();
    DataTypeDescriptor otherResultColumnType = otherColumn.getTypeServices();
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(resultColumnType != null, "Type is null for column " + this);
        SanityManager.ASSERT(otherResultColumnType != null, "Type is null for column " + otherColumn);
    }
    /*
		** We can never make any assumptions about
		** parameters.  So don't even bother in this
		** case.
		*/
    if ((otherExpression != null) && (otherExpression.requiresTypeFromContext()) || (_expression.requiresTypeFromContext())) {
        return false;
    }
    // method in org.apache.derby.iapi.types.XML for more.
    if (resultColumnType.getTypeId().isXMLTypeId())
        return false;
    /* Are they the same type? */
    if (!resultColumnType.getTypeId().equals(otherResultColumnType.getTypeId())) {
        /* If the source is a constant of a different type then
			 * we try to convert that constant to a constant of our
			 * type. (The initial implementation only does the conversion
			 * to string types because the most common problem is a char
			 * constant with a varchar column.)  
			 * NOTE: We do not attempt any conversion here if the source
			 * is a string type and the target is not or vice versa in 
			 * order to avoid problems with implicit varchar conversions.
			 * Anyway, we will check if the "converted" constant has the
			 * same type as the original constant.  If not, then the conversion
			 * happened.  In that case, we will reuse the ConstantNode, for simplicity,
			 * and reset the type to match the desired type.
			 */
        if (otherExpression instanceof ConstantNode) {
            ConstantNode constant = (ConstantNode) otherColumn.getExpression();
            DataValueDescriptor oldValue = constant.getValue();
            DataValueDescriptor newValue = convertConstant(resultColumnType.getTypeId(), resultColumnType.getMaximumWidth(), oldValue);
            if ((oldValue != newValue) && (oldValue instanceof StringDataValue == newValue instanceof StringDataValue)) {
                constant.setValue(newValue);
                constant.setType(getTypeServices());
                otherColumn.bindResultColumnToExpression();
                otherResultColumnType = otherColumn.getType();
            }
            // depending on the collation type.
            if (newValue instanceof StringDataValue) {
                constant.setCollationInfo(resultColumnType);
                DataValueFactory dvf = getDataValueFactory();
                newValue = ((StringDataValue) newValue).getValue(dvf.getCharacterCollator(constant.getTypeServices().getCollationType()));
                constant.setValue(newValue);
            }
        }
        if (!resultColumnType.getTypeId().equals(otherResultColumnType.getTypeId())) {
            return false;
        }
    }
    /* Are they the same precision? */
    if (resultColumnType.getPrecision() != otherResultColumnType.getPrecision()) {
        return false;
    }
    /* Are they the same scale? */
    if (resultColumnType.getScale() != otherResultColumnType.getScale()) {
        return false;
    }
    /* Are they the same width? */
    if (resultColumnType.getMaximumWidth() != otherResultColumnType.getMaximumWidth()) {
        return false;
    }
    /* Is the source nullable and the target non-nullable? 
		 * The source is nullable if it is nullable or if the target is generated
		 * for an unmatched column in an insert with a column list.
		 * This additional check is needed because when we generate any additional
		 * source RCs for an insert with a column list the generated RCs for any 
		 * non-specified columns get the type info from the column.  Thus, 
		 * for t1(non_nullable, nullable)
		 *	insert into t2 (nullable) values 1;
		 * RCType.isNullable() returns false for the generated source RC for 
		 * non_nullable.  In this case, we want to see it as
		 */
    if ((!resultColumnType.isNullable()) && (otherResultColumnType.isNullable() || otherColumn.isGeneratedForUnmatchedColumnInInsert())) {
        return false;
    }
    return true;
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) DataValueFactory(org.apache.derby.iapi.types.DataValueFactory) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) StringDataValue(org.apache.derby.iapi.types.StringDataValue)

Example 9 with StringDataValue

use of org.apache.derby.iapi.types.StringDataValue in project derby by apache.

the class ConcatenationOperatorNode method evaluateConstantExpressions.

/**
 * Check if this node always evaluates to the same value. If so, return
 * a constant node representing the known result.
 *
 * @return a constant node representing the result of this concatenation
 * operation, or {@code this} if the result is not known up front
 */
@Override
ValueNode evaluateConstantExpressions() throws StandardException {
    if (leftOperand instanceof CharConstantNode && rightOperand instanceof CharConstantNode) {
        CharConstantNode leftOp = (CharConstantNode) leftOperand;
        CharConstantNode rightOp = (CharConstantNode) rightOperand;
        StringDataValue leftValue = (StringDataValue) leftOp.getValue();
        StringDataValue rightValue = (StringDataValue) rightOp.getValue();
        StringDataValue resultValue = (StringDataValue) getTypeServices().getNull();
        resultValue.concatenate(leftValue, rightValue, resultValue);
        return new CharConstantNode(resultValue.getString(), getContextManager());
    }
    return this;
}
Also used : StringDataValue(org.apache.derby.iapi.types.StringDataValue)

Example 10 with StringDataValue

use of org.apache.derby.iapi.types.StringDataValue in project derby by apache.

the class EmbedResultSet method getClob.

/**
 * JDBC 2.0
 *
 * Get a CLOB column.
 *
 * @param columnIndex the first column is 1, the second is 2, ...
 * @return an object representing a CLOB
 */
public final Clob getClob(int columnIndex) throws SQLException {
    // closing currentStream does not depend on the
    closeCurrentStream();
    // underlying connection. Do this outside of
    // the connection synchronization.
    // checking result set closure does not depend
    checkIfClosed("getClob");
    // on the underlying connection. Do this
    // outside of the connection synchronization.
    useStreamOrLOB(columnIndex);
    synchronized (getConnectionSynchronization()) {
        int colType = getColumnType(columnIndex);
        // DB2:, only allow getClob on a CLOB column.
        if (colType != Types.CLOB)
            throw dataTypeConversion("java.sql.Clob", columnIndex);
        boolean pushStack = false;
        EmbedConnection ec = getEmbedConnection();
        try {
            StringDataValue dvd = (StringDataValue) getColumn(columnIndex);
            LanguageConnectionContext lcc = getLanguageConnectionContext(ec);
            if (wasNull = dvd.isNull()) {
                InterruptStatus.restoreIntrFlagIfSeen();
                return null;
            }
            // column in the database.
            if (dvd.hasStream()) {
                pushStack = true;
                setupContextStack();
            }
            EmbedClob result = new EmbedClob(ec, dvd);
            restoreIntrFlagIfSeen(pushStack, ec);
            return result;
        } catch (Throwable t) {
            throw handleException(t);
        } finally {
            if (pushStack)
                restoreContextStack();
        }
    }
}
Also used : LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) StringDataValue(org.apache.derby.iapi.types.StringDataValue)

Aggregations

StringDataValue (org.apache.derby.iapi.types.StringDataValue)10 Reader (java.io.Reader)5 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 LoopingAlphabetReader (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)3 InputStream (java.io.InputStream)2 CharacterStreamDescriptor (org.apache.derby.iapi.jdbc.CharacterStreamDescriptor)2 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)2 ReaderToUTF8Stream (org.apache.derby.iapi.types.ReaderToUTF8Stream)2 StandardException (org.apache.derby.shared.common.error.StandardException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 EOFException (java.io.EOFException)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SQLException (java.sql.SQLException)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)1 DataValueFactory (org.apache.derby.iapi.types.DataValueFactory)1