Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for parsing XML data type in result sets.

fixes #67
  • Loading branch information...
commit eb54b46c7024c2ab467e873bd6fc7c011c4ca32b 1 parent d6a61ed
@pekim authored
View
9 src/data-type.coffee
@@ -104,7 +104,7 @@ TYPE =
'real'
writeParameterData: (buffer, parameter) ->
# ParamMetaData (TYPE_INFO)
-
+
# Some issues with rounding
buffer.writeUInt8(typeByName.FloatN.id)
@@ -186,7 +186,7 @@ TYPE =
buffer.writeInt32LE(Math.floor(parseInt(parameter.value) / 0x100000000))
else
buffer.writeUInt8(0)
-
+
# Variable-length types
0x22:
type: 'IMAGE'
@@ -373,6 +373,10 @@ TYPE =
name: 'NChar'
hasCollation: true
dataLengthLength: 2
+ 0xF1:
+ type: 'XML'
+ name: 'Xml'
+ hasSchemaPresent: true
# Types not (yet) supported
###
@@ -387,7 +391,6 @@ TYPE =
BINARYTYPE: 0x2D # Binary (legacy support)
VARBINARYTYPE: 0x25 # VarBinary (legacy support)
- XMLTYPE: 0xF1 # XML (introduced in TDS 7.2)
UDTTYPE: 0xF0 # CLR-UDT (introduced in TDS 7.2)
SSVARIANTTYPE: 0x62 # Sql_Variant (introduced in TDS 7.2)
View
11 src/metadata-parser.coffee
@@ -64,6 +64,16 @@ parse = (buffer, tdsVersion) ->
else
collation = undefined
+ schema = undefined
+ if type.hasSchemaPresent
+ schemaPresent = buffer.readUInt8()
+
+ if schemaPresent == 0x01
+ schema =
+ dbname: buffer.readBVarchar()
+ owningSchema: buffer.readBVarchar()
+ xmlSchemaCollection: buffer.readUsVarchar()
+
metadata =
userType: userType
flags: flags
@@ -72,5 +82,6 @@ parse = (buffer, tdsVersion) ->
precision: precision
scale: scale
dataLength: dataLength
+ schema: schema
module.exports = parse
View
4 src/value-parser.coffee
@@ -9,7 +9,7 @@ THREE_AND_A_THIRD = 3 + (1 / 3)
MONEY_DIVISOR = 10000
PLP_NULL = new Buffer([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
-UNKNOWN_PLP_LEN = new Buffer([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE])
+UNKNOWN_PLP_LEN = new Buffer([0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
parse = (buffer, metaData) ->
value = undefined
@@ -139,6 +139,8 @@ parse = (buffer, metaData) ->
value = null
else
value = readBinary(buffer, dataLength)
+ when 'Xml'
+ value = readMaxNChars(buffer)
when 'SmallDateTime'
value = readSmallDateTime(buffer)
when 'DateTime'
View
37 test/integration/datatypes-in-results-test.coffee
@@ -1,3 +1,4 @@
+async = require('async')
Connection = require('../../src/connection')
Request = require('../../src/request')
fs = require('fs')
@@ -214,10 +215,44 @@ exports.imageNull = (test) ->
exports.guid = (test) ->
execSql(test, "select cast('01234567-89AB-CDEF-0123-456789ABCDEF' as uniqueidentifier)", '01234567-89AB-CDEF-0123-456789ABCDEF')
-
exports.guidNull = (test) ->
execSql(test, "select cast(null as uniqueidentifier)", null)
+exports.xml = (test) ->
+ xml = '<root><child attr="attr-value"/></root>'
+ execSql(test, "select cast('#{xml}' as xml)", xml)
+
+exports.xmlNull = (test) ->
+ execSql(test, "select cast(null as xml)", null)
+
+exports.xmlWithSchema = (test) ->
+ # Cannot use temp tables, as schema collections as not available to them.
+
+ xml = '<root/>'
+
+ schemaName = 'test_tedious_schema'
+ schema = '''
+ <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+ <xsd:element name="root">
+ </xsd:element>
+ </xsd:schema>'''
+
+ dropSchema = """
+ begin try
+ drop xml schema collection #{schemaName}
+ end try begin catch end catch;
+ """
+
+ createSchema = "create xml schema collection #{schemaName} as N'#{schema}';"
+
+ tableName = 'test_tedious_table'
+ dropTable = "drop table #{tableName};"
+ createTable = "create table #{tableName} (xml XML (#{schemaName}));"
+ insert = "insert into #{tableName} (xml) values('#{xml}');"
+ select = "select xml from #{tableName};"
+
+ execSql(test, "#{dropTable} #{dropSchema} #{createSchema} #{createTable} #{insert} #{select}", xml)
+
execSql = (test, sql, expectedValue) ->
test.expect(2)
View
4 test/unit/token/row-token-parser-test.coffee
@@ -242,7 +242,7 @@ module.exports.varCharMaxUnknownLength = (test) ->
value = 'abcdef'
buffer = new WritableTrackingBuffer(0, 'ascii')
- buffer.writeBuffer(new Buffer([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]))
+ buffer.writeBuffer(new Buffer([0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]))
buffer.writeUInt32LE(3)
buffer.writeString(value.slice(0, 3))
buffer.writeUInt32LE(3)
@@ -362,7 +362,7 @@ module.exports.varBinaryMaxUnknownLength = (test) ->
value = [0x12, 0x34, 0x56, 0x78]
buffer = new WritableTrackingBuffer(0, 'ucs2')
- buffer.writeBuffer(new Buffer([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]))
+ buffer.writeBuffer(new Buffer([0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]))
buffer.writeUInt32LE(2)
buffer.writeBuffer(new Buffer(value.slice(0, 2)))
buffer.writeUInt32LE(2)
Please sign in to comment.
Something went wrong with that request. Please try again.