Package org.apache.derby.iapi.types
Class XML
- java.lang.Object
-
- org.apache.derby.iapi.types.DataType
-
- org.apache.derby.iapi.types.XML
-
- All Implemented Interfaces:
java.io.Externalizable
,java.io.Serializable
,java.lang.Comparable
,Formatable
,Storable
,StreamStorable
,TypedFormat
,DataValueDescriptor
,Orderable
,XMLDataValue
public class XML extends DataType implements XMLDataValue, StreamStorable
This type implements the XMLDataValue interface and thus is the type on which all XML related operations are executed. The first and simplest XML store implementation is a UTF-8 based one--all XML data is stored on disk as a UTF-8 string, just like the other Derby string types. In order to make it possible for smarter XML implementations to exist in the future, this class always writes an "XML implementation id" to disk before writing the rest of its data. When reading the data, the impl id is read first and serves as an indicator of how the rest of the data should be read. So long as there's only one implementation (UTF-8) the impl id can be ignored; but when smarter implementations are written, the impl id will be the key to figuring out how an XML value should be read, written, and processed.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static int
BASE_MEMORY_USAGE
private boolean
containsTopLevelAttr
private SqlXmlUtil
tmpUtil
protected static short
UTF8_IMPL_ID
static int
XML_DOC_ANY
static int
XML_SEQUENCE
private static java.lang.String
xmlReqCheck
private SQLChar
xmlStringValue
static short
XQ_EMPTY_ON_EMPTY
static short
XQ_NULL_ON_EMPTY
static short
XQ_PASS_BY_REF
static short
XQ_PASS_BY_VALUE
static short
XQ_RETURN_CONTENT
static short
XQ_RETURN_SEQUENCE
private int
xType
-
Fields inherited from interface org.apache.derby.iapi.types.DataValueDescriptor
UNKNOWN_LOGICAL_LENGTH
-
Fields inherited from interface org.apache.derby.iapi.types.Orderable
ORDER_OP_EQUALS, ORDER_OP_GREATEROREQUALS, ORDER_OP_GREATERTHAN, ORDER_OP_LESSOREQUALS, ORDER_OP_LESSTHAN
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static boolean
checkJAXPRequirement()
Check if we have a JAXP implementation installed.static void
checkXMLRequirements()
See if the required JAXP and Xalan classes are in the user's classpath.private static boolean
checkXPathRequirement()
Check if XPath is supported on this platform.DataValueDescriptor
cloneValue(boolean forceMaterialization)
Clone this DataValueDescriptor.int
compare(DataValueDescriptor other)
Compare two XML DataValueDescriptors.int
estimateMemoryUsage()
Estimate the memory usage in bytes of the data value and the overhead of the class.int
getLength()
Gets the length of the data value.DataValueDescriptor
getNewNull()
Get a new null value of the same type as this data value.java.lang.String
getString()
Gets the value in the data value descriptor as a String.int
getTypeFormatId()
Get a universally unique identifier for the type of this object.java.lang.String
getTypeName()
Get the SQL name of the datatypeint
getXType()
Retrieve this XML value's qualified type.boolean
hasTopLevelAttr()
Return whether or not this XML value represents a sequence that has one or more top-level attribute nodes.boolean
isNull()
Return whether the value is null or not.void
loadStream()
Set the value by reading the stream and converting it to an object form.void
markAsHavingTopLevelAttr()
Take note of the fact this XML value represents an XML sequence that has one or more top-level attribute nodes.void
normalize(DataTypeDescriptor desiredType, DataValueDescriptor source)
Normalization method - this method will always be called when storing an XML value into an XML column, for example, when inserting/updating.void
readExternal(java.io.ObjectInput in)
Read an XML value from an input stream.void
readExternalFromArray(ArrayInputStream in)
Read the DataValueDescriptor from the stream.void
restoreToNull()
Restore this object to its (SQL)null value.java.io.InputStream
returnStream()
Return the on-disk stream state of the object.protected void
setFrom(DataValueDescriptor theValue)
Set the value of this DataValueDescriptor based on the value of the specified DataValueDescriptor.void
setStream(java.io.InputStream newStream)
sets the on-disk stream state for the object.void
setValueFromResultSet(java.sql.ResultSet resultSet, int colNumber, boolean isNullable)
Set the value based on the value for the specified DataValueDescriptor from the specified ResultSet.void
setXType(int xtype)
Set this XML value's qualified type.int
typePrecedence()
Each built-in type in JSQL has a precedence.void
writeExternal(java.io.ObjectOutput out)
Write an XML value.BooleanDataValue
XMLExists(SqlXmlUtil sqlxUtil)
The SQL/XML XMLExists operator.XMLDataValue
XMLParse(StringDataValue stringValue, boolean preserveWS, SqlXmlUtil sqlxUtil)
Method to parse an XML string and, if it's valid, store the _serialized_ version locally and then return this XMLDataValue.XMLDataValue
XMLQuery(SqlXmlUtil sqlxUtil, XMLDataValue result)
Evaluate the XML query expression contained within the received util object against this XML value and store the results into the received XMLDataValue "result" param (assuming "result" is non-null; else create a new XMLDataValue).StringDataValue
XMLSerialize(StringDataValue result, int targetType, int targetWidth, int targetCollationType)
The SQL/XML XMLSerialize operator.-
Methods inherited from class org.apache.derby.iapi.types.DataType
checkHostVariable, cloneHolder, coalesce, compare, compare, compare, compareTo, dataTypeConversion, equals, equals, flip, genericSetObject, getBoolean, getByte, getBytes, getDate, getDouble, getFloat, getInt, getLong, getObject, getShort, getStream, getTime, getTimestamp, getTraceString, getTypeName, greaterOrEquals, greaterThan, hasStream, in, invalidFormat, isNotNull, isNullOp, lessOrEquals, lessThan, notEquals, outOfRange, recycle, setBigDecimal, setInto, setInto, setObject, setObjectForCast, setToNull, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, throwLangSetMismatch, throwLangSetMismatch, typeToBigDecimal
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.derby.iapi.types.DataValueDescriptor
checkHostVariable, cloneHolder, coalesce, compare, compare, compare, equals, getBoolean, getByte, getBytes, getDate, getDouble, getFloat, getInt, getLong, getObject, getShort, getStream, getTime, getTimestamp, getTraceString, greaterOrEquals, greaterThan, hasStream, in, isNotNull, isNullOp, lessOrEquals, lessThan, notEquals, recycle, setBigDecimal, setInto, setInto, setObjectForCast, setToNull, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, typeToBigDecimal
-
-
-
-
Field Detail
-
UTF8_IMPL_ID
protected static final short UTF8_IMPL_ID
- See Also:
- Constant Field Values
-
BASE_MEMORY_USAGE
private static final int BASE_MEMORY_USAGE
-
XQ_PASS_BY_REF
public static final short XQ_PASS_BY_REF
- See Also:
- Constant Field Values
-
XQ_PASS_BY_VALUE
public static final short XQ_PASS_BY_VALUE
- See Also:
- Constant Field Values
-
XQ_RETURN_SEQUENCE
public static final short XQ_RETURN_SEQUENCE
- See Also:
- Constant Field Values
-
XQ_RETURN_CONTENT
public static final short XQ_RETURN_CONTENT
- See Also:
- Constant Field Values
-
XQ_EMPTY_ON_EMPTY
public static final short XQ_EMPTY_ON_EMPTY
- See Also:
- Constant Field Values
-
XQ_NULL_ON_EMPTY
public static final short XQ_NULL_ON_EMPTY
- See Also:
- Constant Field Values
-
XML_DOC_ANY
public static final int XML_DOC_ANY
- See Also:
- Constant Field Values
-
XML_SEQUENCE
public static final int XML_SEQUENCE
- See Also:
- Constant Field Values
-
xType
private int xType
-
xmlStringValue
private SQLChar xmlStringValue
-
xmlReqCheck
private static java.lang.String xmlReqCheck
-
containsTopLevelAttr
private boolean containsTopLevelAttr
-
tmpUtil
private SqlXmlUtil tmpUtil
-
-
Constructor Detail
-
XML
public XML()
Default constructor.
-
XML
private XML(SQLChar val, int xmlType, boolean seqWithAttr, boolean materialize)
Private constructor used for thecloneValue
method. Returns a new instance of XML whose fields are clones of the values received.- Parameters:
val
- A SQLChar instance to clone and use for this XML value.xmlType
- Qualified XML type for "val"seqWithAttr
- Whether or not "val" corresponds to sequence with one or more top-level attribute nodes.materialize
- whether or not to force materialization of the underlying source data
-
-
Method Detail
-
cloneValue
public DataValueDescriptor cloneValue(boolean forceMaterialization)
Description copied from interface:DataValueDescriptor
Clone this DataValueDescriptor. Results in a new object that has the same value as this but can be modified independently.Even though the objects can be modified independently regardless of the value of
forceMaterialization
, both the clone and the original may be dependent on the store state ifforceMaterialization
is set tofalse
. An example is if you need to access the value you just read usingcloneValue
after the current transaction has ended, or after the source result set has been closed.- Specified by:
cloneValue
in interfaceDataValueDescriptor
- Parameters:
forceMaterialization
- any streams representing the data value will be materialized iftrue
, the data value will be kept as a stream if possible iffalse
- Returns:
- A clone of the
DataValueDescriptor
with the same initial value as this. - See Also:
DataValueDescriptor.cloneValue(boolean)
-
getNewNull
public DataValueDescriptor getNewNull()
Description copied from interface:DataValueDescriptor
Get a new null value of the same type as this data value.- Specified by:
getNewNull
in interfaceDataValueDescriptor
- See Also:
DataValueDescriptor.getNewNull()
-
getTypeName
public java.lang.String getTypeName()
Description copied from interface:DataValueDescriptor
Get the SQL name of the datatype- Specified by:
getTypeName
in interfaceDataValueDescriptor
- Returns:
- The SQL name of the datatype
- See Also:
DataValueDescriptor.getTypeName()
-
typePrecedence
public int typePrecedence()
Description copied from class:DataType
Each built-in type in JSQL has a precedence. This precedence determines how to do type promotion when using binary operators. For example, float has a higher precedence than int, so when adding an int to a float, the result type is float. The precedence for some types is arbitrary. For example, it doesn't matter what the precedence of the boolean type is, since it can't be mixed with other types. But the precedence for the number types is critical. The SQL standard requires that exact numeric types be promoted to approximate numeric when one operator uses both. Also, the precedence is arranged so that one will not lose precision when promoting a type.- Specified by:
typePrecedence
in interfaceDataValueDescriptor
- Overrides:
typePrecedence
in classDataType
- Returns:
- The precedence of this type.
- See Also:
DataValueDescriptor.typePrecedence()
-
getString
public java.lang.String getString() throws StandardException
Description copied from interface:DataValueDescriptor
Gets the value in the data value descriptor as a String. Throws an exception if the data value is not a string.- Specified by:
getString
in interfaceDataValueDescriptor
- Returns:
- The data value as a String.
- Throws:
StandardException
- Thrown on error- See Also:
DataValueDescriptor.getString()
-
getLength
public int getLength() throws StandardException
Description copied from interface:DataValueDescriptor
Gets the length of the data value. The meaning of this is implementation-dependent. For string types, it is the number of characters in the string. For numeric types, it is the number of bytes used to store the number. This is the actual length of this value, not the length of the type it was defined as. For example, a VARCHAR value may be shorter than the declared VARCHAR (maximum) length.- Specified by:
getLength
in interfaceDataValueDescriptor
- Returns:
- The length of the data value
- Throws:
StandardException
- On error- See Also:
DataValueDescriptor.getLength()
-
estimateMemoryUsage
public int estimateMemoryUsage()
Description copied from interface:DataValueDescriptor
Estimate the memory usage in bytes of the data value and the overhead of the class.- Specified by:
estimateMemoryUsage
in interfaceDataValueDescriptor
- Returns:
- the estimated memory usage
- See Also:
DataValueDescriptor.estimateMemoryUsage()
-
readExternalFromArray
public void readExternalFromArray(ArrayInputStream in) throws java.io.IOException
Description copied from class:DataType
Read the DataValueDescriptor from the stream. The default implementation callsreadExternal()
, which accesses theArrayInputStream
as a generic stream. If sub-classes can implement it more efficiently by accessing the array, they should override this method.- Specified by:
readExternalFromArray
in interfaceDataValueDescriptor
- Overrides:
readExternalFromArray
in classDataType
- Parameters:
in
- The array stream positioned at the beginning of the byte stream to read from.- Throws:
java.io.IOException
- Usual error is if you try to read past limit on the stream.- See Also:
DataValueDescriptor.readExternalFromArray(org.apache.derby.iapi.services.io.ArrayInputStream)
-
setFrom
protected void setFrom(DataValueDescriptor theValue) throws StandardException
Description copied from class:DataType
Set the value of this DataValueDescriptor based on the value of the specified DataValueDescriptor.- Overrides:
setFrom
in classDataType
- Parameters:
theValue
- The DataValueDescriptor that holds the value to which we want to set this DataValueDescriptor's value.- Throws:
StandardException
- See Also:
DataType.setFrom(org.apache.derby.iapi.types.DataValueDescriptor)
-
setValueFromResultSet
public final void setValueFromResultSet(java.sql.ResultSet resultSet, int colNumber, boolean isNullable) throws java.sql.SQLException
Description copied from interface:DataValueDescriptor
Set the value based on the value for the specified DataValueDescriptor from the specified ResultSet.- Specified by:
setValueFromResultSet
in interfaceDataValueDescriptor
- Parameters:
resultSet
- The specified ResultSet.colNumber
- The 1-based column # into the resultSet.isNullable
- Whether or not the column is nullable (No need to call wasNull() if not)- Throws:
java.sql.SQLException
- Error accessing the result set- See Also:
DataValueDescriptor.setValueFromResultSet(java.sql.ResultSet, int, boolean)
-
compare
public int compare(DataValueDescriptor other) throws StandardException
Compare two XML DataValueDescriptors. NOTE: This method should only be used by the database store for the purpose of index positioning--comparisons of XML type are not allowed from the language side of things. That said, all store wants to do is order the NULLs, so we don't actually have to do a full comparison. Just return an order value based on whether or not this XML value and the other XML value are null. As mentioned in the "compare" method of DataValueDescriptor, nulls are considered equal to other nulls and less than all other values. An example of when this method might be used is if the user executed a query like: select i from x_table where x_col is not null- Specified by:
compare
in interfaceDataValueDescriptor
- Parameters:
other
- The Orderable to compare this one to.- Returns:
- <0 - this Orderable is less than other. 0 - this Orderable equals other. >0 - this Orderable is greater than other. The code should not explicitly look for -1, or 1.
- Throws:
StandardException
- Thrown on error- See Also:
DataValueDescriptor.compare(org.apache.derby.iapi.types.DataValueDescriptor)
-
normalize
public void normalize(DataTypeDescriptor desiredType, DataValueDescriptor source) throws StandardException
Normalization method - this method will always be called when storing an XML value into an XML column, for example, when inserting/updating. We always force normalization in this case because we need to make sure the qualified type of the value we're trying to store is XML_DOC_ANY--we don't allow anything else.- Specified by:
normalize
in interfaceDataValueDescriptor
- Overrides:
normalize
in classDataType
- Parameters:
desiredType
- The type to normalize the source column tosource
- The value to normalize- Throws:
StandardException
- Thrown if source is not XML_DOC_ANY.
-
getTypeFormatId
public int getTypeFormatId()
Description copied from interface:TypedFormat
Get a universally unique identifier for the type of this object.- Specified by:
getTypeFormatId
in interfaceTypedFormat
- Returns:
- The identifier. (A UUID stuffed in an array of 16 bytes).
- See Also:
From the engine's perspective, all XML implementations share the same format id.
-
isNull
public boolean isNull()
Description copied from interface:Storable
Return whether the value is null or not.- Specified by:
isNull
in interfaceStorable
- Returns:
- true if the value is null and false otherwise.
- See Also:
Storable.isNull()
-
restoreToNull
public void restoreToNull()
Description copied from interface:Storable
Restore this object to its (SQL)null value.- Specified by:
restoreToNull
in interfaceStorable
- See Also:
Storable.restoreToNull()
-
readExternal
public void readExternal(java.io.ObjectInput in) throws java.io.IOException
Read an XML value from an input stream.- Specified by:
readExternal
in interfacejava.io.Externalizable
- Parameters:
in
- The stream from which we're reading.- Throws:
java.io.IOException
-
writeExternal
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException
Write an XML value.- Specified by:
writeExternal
in interfacejava.io.Externalizable
- Parameters:
out
- The stream to which we're writing.- Throws:
java.io.IOException
-
returnStream
public java.io.InputStream returnStream()
Description copied from interface:StreamStorable
Return the on-disk stream state of the object.- Specified by:
returnStream
in interfaceStreamStorable
- See Also:
StreamStorable.returnStream()
-
setStream
public void setStream(java.io.InputStream newStream)
Description copied from interface:StreamStorable
sets the on-disk stream state for the object.- Specified by:
setStream
in interfaceStreamStorable
- See Also:
StreamStorable.setStream(java.io.InputStream)
-
loadStream
public void loadStream() throws StandardException
Description copied from interface:StreamStorable
Set the value by reading the stream and converting it to an object form.- Specified by:
loadStream
in interfaceStreamStorable
- Throws:
StandardException
- on error- See Also:
StreamStorable.loadStream()
-
XMLParse
public XMLDataValue XMLParse(StringDataValue stringValue, boolean preserveWS, SqlXmlUtil sqlxUtil) throws StandardException
Method to parse an XML string and, if it's valid, store the _serialized_ version locally and then return this XMLDataValue.- Specified by:
XMLParse
in interfaceXMLDataValue
- Parameters:
stringValue
- The string value to check.preserveWS
- Whether or not to preserve ignorable whitespace.sqlxUtil
- Contains SQL/XML objects and util methods that facilitate execution of XML-related operations- Returns:
- If 'text' constitutes a valid XML document, it has been stored in this XML value and this XML value is returned; otherwise, an exception is thrown.
- Throws:
StandardException
- Thrown on error.
-
XMLSerialize
public StringDataValue XMLSerialize(StringDataValue result, int targetType, int targetWidth, int targetCollationType) throws StandardException
The SQL/XML XMLSerialize operator. Serializes this XML value into a string with a user-specified character type, and returns that string via the received StringDataValue (if the received StringDataValue is non-null and of the correct type; else, a new StringDataValue is returned).- Specified by:
XMLSerialize
in interfaceXMLDataValue
- Parameters:
result
- The result of a previous call to this method, null if not called yet.targetType
- The string type to which we want to serialize.targetWidth
- The width of the target type.targetCollationType
- The collation type of the target type.- Returns:
- A serialized (to string) version of this XML object, in the form of a StringDataValue object.
- Throws:
StandardException
- Thrown on error
-
XMLExists
public BooleanDataValue XMLExists(SqlXmlUtil sqlxUtil) throws StandardException
The SQL/XML XMLExists operator. Checks to see if evaluation of the query expression contained within the received util object against this XML value returns at least one item. NOTE: For now, the query expression must be XPath only (XQuery not supported) because that's what Xalan supports.- Specified by:
XMLExists
in interfaceXMLDataValue
- Parameters:
sqlxUtil
- Contains SQL/XML objects and util methods that facilitate execution of XML-related operations- Returns:
- True if evaluation of the query expression stored in sqlxUtil returns at least one node for this XML value; unknown if the xml value is NULL; false otherwise.
- Throws:
StandardException
- Thrown on error
-
XMLQuery
public XMLDataValue XMLQuery(SqlXmlUtil sqlxUtil, XMLDataValue result) throws StandardException
Evaluate the XML query expression contained within the received util object against this XML value and store the results into the received XMLDataValue "result" param (assuming "result" is non-null; else create a new XMLDataValue).- Specified by:
XMLQuery
in interfaceXMLDataValue
- Parameters:
sqlxUtil
- Contains SQL/XML objects and util methods that facilitate execution of XML-related operationsresult
- The result of a previous call to this method; null if not called yet.- Returns:
- An XMLDataValue whose content corresponds to the serialized version of the results from evaluation of the query expression. Note: this XMLDataValue may not be storable into Derby XML columns.
- Throws:
StandardException
- thrown on error
-
setXType
public void setXType(int xtype)
Set this XML value's qualified type.- Specified by:
setXType
in interfaceXMLDataValue
-
getXType
public int getXType()
Retrieve this XML value's qualified type.- Specified by:
getXType
in interfaceXMLDataValue
-
markAsHavingTopLevelAttr
public void markAsHavingTopLevelAttr()
Take note of the fact this XML value represents an XML sequence that has one or more top-level attribute nodes.- Specified by:
markAsHavingTopLevelAttr
in interfaceXMLDataValue
-
hasTopLevelAttr
public boolean hasTopLevelAttr()
Return whether or not this XML value represents a sequence that has one or more top-level attribute nodes.- Specified by:
hasTopLevelAttr
in interfaceXMLDataValue
-
checkXMLRequirements
public static void checkXMLRequirements() throws StandardException
See if the required JAXP and Xalan classes are in the user's classpath. Assumption is that we will always call this method before instantiating an instance of SqlXmlUtil, and thus we will never get a ClassNotFound exception caused by missing JAXP/Xalan classes. Instead, if either is missing we should throw an informative error indicating what the problem is. NOTE: This method only does the checks necessary to allow successful instantiation of the SqlXmlUtil class. Further checks (esp. the presence of a JAXP _implementation_ in addition to the JAXP _interfaces_) are performed in the SqlXmlUtil constructor.- Throws:
StandardException
- thrown if the required classes cannot be located in the classpath.
-
checkJAXPRequirement
private static boolean checkJAXPRequirement()
Check if we have a JAXP implementation installed.- Returns:
true
if JAXP is installed, orfalse
otherwise
-
checkXPathRequirement
private static boolean checkXPathRequirement()
Check if XPath is supported on this platform.- Returns:
true
if XPath is supported, orfalse
otherwise
-
-