pekim/tedious — pull request #127 by justcfx2u (2014-02-05 21:38:57+00:00)
Title: Feature: Auto-queue SQL if busy, replay as able.

This is a simple (naïve?) FIFO queue implementation. It must be enabled using the `useQueue: true` configuration option which is passed to the new Connection object.

Several of my production projects try to use the SQL connection often, which occasionally results in `execSql()` being called while the Connection is busy. In the majority of cases I do not need nor have a desire to maintain multiple connections to the SQL server; it is much more useful to me to automatically store the SQL request and execute it as the connection is available, rater than trying to handle the error and re-try outside of the SQL library (tedious).

This patch provides queue support capability for Tedious.
It is only lightly tested so far, but it appears to work in development.

A simple test file is included below:
```coffee
tedious = require('tedious')
util = require('util')

sqlconfig =
userName: 'USER',
password: 'PASSWORD',
server: 'SERVER GOES HERE'
options:
useQueue: true
debug: true

tds = {}
tds.conn = new tedious.Connection(sqlconfig);
tds.conn.on 'debug', (thing) ->
console.log util.inspect(thing, {depth: null})

tds.conn.on 'connect', (err) ->
if err?
console.error "tedious.Connection: ERROR: #{err}"
process.exit(1)
return

console.info "Connected to database at #{sqlconfig.server} as '#{sqlconfig.userName}'."
setImmediate(fuzz)
setImmediate(find)

tds.conn.on 'errorMessage', (err) ->
if err?
console.error "tedious.Connection: ERROR: #{util.inspect(err, {depth: null})}"
process.exit(1)
return

tds.conn.on 'end', ->
console.error "tedious.Connection: ERROR: Connection closed."
process.exit(1)
return

tds.req = tedious.Request
tds.TYPES = tedious.TYPES

tally = 0

fuzz = ->
query = 'INSERT INTO test_table VALUES (@q)'
sql = new tds.req query, (err, rowCount) ->
if err?
console.error("ERROR: #{err}")
#return

console.log "- fin fuzz #{tally}"
tally++
setImmediate(fuzz)

sql.addParameter('q', tds.TYPES.VarChar, '1.#{tally}')

console.log "- start fuzz #{tally}"
tds.conn.execSql(sql)

find = ->
query = 'SELECT * FROM test_table'
sql = new tds.req query, (err, rowCount) ->
if err?
console.error("ERROR: #{err}")
#return

console.log "- fin find #{tally}"
tally++
setImmediate(find)

console.log "- start find #{tally}"
tds.conn.execSql(sql)

```