Skip to content

Commit

Permalink
Add a rowCount parameter to Request's callback, with the count of all…
Browse files Browse the repository at this point in the history
… the rows returned for the request.
  • Loading branch information
pekim committed Apr 14, 2012
1 parent 7a43119 commit 464d9a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/connection.coffee
Expand Up @@ -94,7 +94,7 @@ class Connection extends EventEmitter

sqlRequest = @request
@request = undefined
sqlRequest.callback(sqlRequest.error)
sqlRequest.callback(sqlRequest.error, sqlRequest.rowCount)
FINAL:
name: 'Final'
enter: ->
Expand Down Expand Up @@ -185,6 +185,7 @@ class Connection extends EventEmitter
)
@tokenStreamParser.on('row', (token) =>
if @request
@request.rowCount++
@request.emit('row', token.columns)
else
throw new Error("Received 'row' when no sqlRequest is in progress")
Expand Down Expand Up @@ -357,6 +358,7 @@ class Connection extends EventEmitter
request.callback(message)
else
@request = request
@request.rowCount = 0

@messageIo.sendMessage(packetType, payload.data)
@debug.payload(->
Expand Down
26 changes: 16 additions & 10 deletions test/integration/connection-test.coffee
Expand Up @@ -153,12 +153,13 @@ exports.connectByInstanceName = (test) ->
)

exports.execSql = (test) ->
test.expect(7)
test.expect(8)

config = getConfig()

request = new Request('select 8 as C1', (err) ->
request = new Request('select 8 as C1', (err, rowCount) ->
test.ok(!err)
test.strictEqual(rowCount, 1)

connection.close()
)
Expand Down Expand Up @@ -197,12 +198,14 @@ exports.execSql = (test) ->
)

exports.execSqlWithOrder = (test) ->
test.expect(9)
test.expect(10)

config = getConfig()

request = new Request("select top 2 object_id, name, column_id, system_type_id from sys.columns order by name, system_type_id", (err) ->
sql = "select top 2 object_id, name, column_id, system_type_id from sys.columns order by name, system_type_id"
request = new Request(sql, (err, rowCount) ->
test.ok(!err)
test.strictEqual(rowCount, 2)

connection.close()
)
Expand Down Expand Up @@ -249,7 +252,7 @@ exports.execSqlWithOrder = (test) ->
)

exports.execSqlMultipleTimes = (test) ->
test.expect(15)
test.expect(20)

requestsToMake = 5;
config = getConfig()
Expand All @@ -259,8 +262,9 @@ exports.execSqlMultipleTimes = (test) ->
connection.close()
return

request = new Request('select 8 as C1', (err) ->
request = new Request('select 8 as C1', (err, rowCount) ->
test.ok(!err)
test.strictEqual(rowCount, 1)

requestsToMake--
makeRequest()
Expand Down Expand Up @@ -326,13 +330,14 @@ exports.execBadSql = (test) ->
)

exports.sqlWithMultipleResultSets = (test) ->
test.expect(7)
test.expect(8)

config = getConfig()
row = 0

request = new Request('select 1; select 2;', (err) ->
request = new Request('select 1; select 2;', (err, rowCount) ->
test.ok(!err)
test.strictEqual(rowCount, 2)

connection.close()
)
Expand Down Expand Up @@ -368,12 +373,13 @@ exports.sqlWithMultipleResultSets = (test) ->
)

exports.execProcAsSql = (test) ->
test.expect(6)
test.expect(7)

config = getConfig()

request = new Request('exec sp_help int', (err) ->
request = new Request('exec sp_help int', (err, rowCount) ->
test.ok(!err)
test.strictEqual(rowCount, 1)

connection.close()
)
Expand Down

0 comments on commit 464d9a6

Please sign in to comment.