Permalink
Browse files

Add connection.reset function, to reset connection to initial state.

fixes #85
  • Loading branch information...
1 parent f9a4296 commit e4da4b00e9c81b99fb77a9aa0c398ad73fa0579d @pekim pekim committed Mar 24, 2013
View
@@ -1,5 +1,3 @@
-#process.env.NODE_DEBUG = 'tls'
-
require('./buffertools')
Debug = require('./debug')
EventEmitter = require('events').EventEmitter
@@ -280,6 +278,9 @@ class Connection extends EventEmitter
if @config.options.rowCollectionOnDone
@request.rows = []
)
+ @tokenStreamParser.on('resetConnection', (token) =>
+ @emit('resetConnection')
+ )
connect: ->
if (@config.options.port)
@@ -420,7 +421,11 @@ class Connection extends EventEmitter
@tokenStreamParser.addBuffer(data)
sendInitialSql: ->
- initialSql = 'set textsize ' + @config.options.textsize + '''
+ payload = new SqlBatchPayload(@getInitialSql(), @currentTransactionDescriptor())
+ @messageIo.sendMessage(TYPE.SQL_BATCH, payload.data)
+
+ getInitialSql: ->
+ 'set textsize ' + @config.options.textsize + '''
set quoted_identifier on
set arithabort off
set numeric_roundabort off
@@ -435,9 +440,6 @@ set dateformat mdy
set datefirst 7
set transaction isolation level read committed'''
- payload = new SqlBatchPayload(initialSql, @currentTransactionDescriptor())
- @messageIo.sendMessage(TYPE.SQL_BATCH, payload.data)
-
processedInitialSql: ->
@clearConnectTimer()
@emit('connect')
@@ -513,13 +515,22 @@ set transaction isolation level read committed'''
@request.rowCount = 0
@request.rows = []
- @messageIo.sendMessage(packetType, payload.data)
+ @messageIo.sendMessage(packetType, payload.data, @resetConnectionOnNextRequest)
+ @resetConnectionOnNextRequest = false
@debug.payload(->
payload.toString(' ')
)
@transitionTo(@STATE.SENT_CLIENT_REQUEST)
+ reset: (callback) =>
+ request = new Request(@getInitialSql(), (err, rowCount, rows) ->
+ callback(err)
+ )
+
+ @resetConnectionOnNextRequest = true
+ @execSqlBatch(request)
+
currentTransactionDescriptor: ->
@transactionDescriptors[@transactionDescriptors.length - 1]
View
@@ -58,7 +58,7 @@ class MessageIO extends EventEmitter
@securePair.cleartext.addListener('data', @eventData)
# TODO listen for 'drain' event when socket.write returns false.
- sendMessage: (packetType, data) ->
+ sendMessage: (packetType, data, resetConnection) ->
numberOfPackets = (Math.floor((data.length - 1) / @packetDataSize)) + 1
for packetNumber in [0..numberOfPackets - 1]
@@ -71,6 +71,7 @@ class MessageIO extends EventEmitter
packet = new Packet(packetType)
packet.last(packetNumber == numberOfPackets - 1)
+ packet.resetConnection(resetConnection)
packet.packetId(packetNumber + 1)
packet.addData(packetPayload)
View
@@ -59,6 +59,16 @@ class Packet
length: ->
@buffer.readUInt16BE(OFFSET.Length)
+ resetConnection: (reset) ->
+ status = @buffer.readUInt8(OFFSET.Status)
+
+ if reset
+ status |= STATUS.RESETCONNECTION
+ else
+ status &= 0xFF - STATUS.RESETCONNECTION
+
+ @buffer.writeUInt8(status, OFFSET.Status)
+
last: (last) ->
status = @buffer.readUInt8(OFFSET.Status)
@@ -32,6 +32,9 @@ types =
event: 'partnerNode'
17:
name: 'TXN_ENDED'
+ 18:
+ name: 'RESET_CONNECTION'
+ event: 'resetConnection'
module.exports = (buffer) ->
length = buffer.readUInt16LE()
@@ -43,7 +46,7 @@ module.exports = (buffer) ->
when 'DATABASE', 'LANGUAGE', 'CHARSET', 'PACKET_SIZE', 'DATABASE_MIRRORING_PARTNER'
newValue = buffer.readBVarchar()
oldValue = buffer.readBVarchar()
- when 'SQL_COLLATION', 'BEGIN_TXN', 'COMMIT_TXN', 'ROLLBACK_TXN'
+ when 'SQL_COLLATION', 'BEGIN_TXN', 'COMMIT_TXN', 'ROLLBACK_TXN', 'RESET_CONNECTION'
valueLength = buffer.readUInt8()
newValue = buffer.readBuffer(valueLength)
@@ -1,3 +1,4 @@
+async = require('async')
Connection = require('../../src/connection')
Request = require('../../src/request')
fs = require('fs')
@@ -669,3 +670,64 @@ exports.execProcAsSql = (test) ->
connection.on('debug', (text) ->
#console.log(text)
)
+
+exports.resetConnection = (test) ->
+ test.expect(4)
+
+ config = getConfig()
+
+ testAnsiNullsOptionOn = (callback) ->
+ testAnsiNullsOption(true, callback)
+
+ testAnsiNullsOptionOff = (callback) ->
+ testAnsiNullsOption(false, callback)
+
+ testAnsiNullsOption = (expectedOptionOn, callback) ->
+ request = new Request('select @@options & 32', (err, rowCount) ->
+ callback(err)
+ )
+
+ request.on('row', (columns) ->
+ optionOn = columns[0].value == 32
+ test.strictEqual(optionOn, expectedOptionOn)
+ )
+
+ connection.execSql(request)
+
+ setAnsiNullsOptionOff = (callback) ->
+ request = new Request('set ansi_nulls off', (err, rowCount) ->
+ callback(err)
+ )
+
+ connection.execSqlBatch(request)
+
+ connection = new Connection(config)
+
+ connection.on('resetConnection', ->
+ test.ok(true)
+ )
+
+ connection.on('connect', (err) ->
+ async.series([
+ testAnsiNullsOptionOn,
+ setAnsiNullsOptionOff,
+ testAnsiNullsOptionOff,
+ connection.reset,
+ testAnsiNullsOptionOn,
+ (callback) ->
+ connection.close()
+ callback()
+ ])
+ )
+
+ connection.on('end', (info) ->
+ test.done()
+ )
+
+ connection.on('infoMessage', (info) ->
+ #console.log("#{info.number} : #{info.message}")
+ )
+
+ connection.on('debug', (text) ->
+ #console.log(text)
+ )

0 comments on commit e4da4b0

Please sign in to comment.