Skip to content

Commit

Permalink
Merge pull request #1520 from dhensby/pulls/named-instance-port
Browse files Browse the repository at this point in the history
Support named instances with ports
  • Loading branch information
dhensby committed Aug 1, 2023
2 parents f384e0f + 011f4c0 commit 73d6861
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 20 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
v9.1.1 (2023-01-??)
v9.1.2 (2023-08-01)
-------------------
[fix] Support more named instance formats ([#1520](https://github.com/tediousjs/node-mssql/pull/1520))
[refactor] Stop using deprecated regex symbols ([#1520](https://github.com/tediousjs/node-mssql/pull/1520))

v9.1.1 (2023-01-19)
-------------------
[revert] Add support for AAD authentication via connection string ((#1436)[https://github.com/tediousjs/node-mssql/pull/1436])

Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Microsoft SQL Server client for Node.js

[![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Travis CI][travis-image]][travis-url] [![Appveyor CI][appveyor-image]][appveyor-url] [![Join the chat at https://gitter.im/patriksimek/node-mssql](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/patriksimek/node-mssql?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Appveyor CI][appveyor-image]][appveyor-url] [![Join the chat at https://gitter.im/patriksimek/node-mssql](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/patriksimek/node-mssql?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Supported TDS drivers:
- [Tedious][tedious-url] (pure JavaScript - Windows/macOS/Linux, default)
Expand Down Expand Up @@ -2093,8 +2093,6 @@ to create new connections or not
[downloads-url]: https://www.npmjs.com/package/mssql
[david-image]: https://img.shields.io/david/tediousjs/node-mssql.svg?style=flat-square
[david-url]: https://david-dm.org/tediousjs/node-mssql
[travis-image]: https://img.shields.io/travis/tediousjs/node-mssql/master.svg?style=flat-square&label=unit
[travis-url]: https://travis-ci.org/tediousjs/node-mssql
[appveyor-image]: https://ci.appveyor.com/api/projects/status/e5gq1a0ujwams9t7/branch/master?svg=true
[appveyor-url]: https://ci.appveyor.com/project/tediousjs/node-mssql

Expand Down
34 changes: 22 additions & 12 deletions lib/base/connection-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ class ConnectionPool extends EventEmitter {
this.config.arrayRowMode = this.config.arrayRowMode || false
this.config.validateConnection = 'validateConnection' in this.config ? this.config.validateConnection : true

if (/^(.*)\\(.*)$/.exec(this.config.server)) {
this.config.server = RegExp.$1
this.config.options.instanceName = RegExp.$2
const namedServer = /^(.*)\\(.*)$/.exec(this.config.server)
if (namedServer) {
this.config.server = namedServer[1]
this.config.options.instanceName = namedServer[2]
}

if (typeof this.config.options.useColumnNames !== 'undefined' && this.config.options.useColumnNames !== true) {
Expand Down Expand Up @@ -149,13 +150,21 @@ class ConnectionPool extends EventEmitter {
if (/^tcp:/i.test(server)) {
server = server.substr(4)
}
if (/^(.*)\\(.*)$/.exec(server)) {
server = RegExp.$1
instanceName = RegExp.$2
const namedServerParts = /^(.*)\\(.*)$/.exec(server)
if (namedServerParts) {
server = namedServerParts[1].trim()
instanceName = namedServerParts[2].trim()
}
if (/^(.*),(.*)$/.exec(server)) {
server = RegExp.$1.trim()
port = parseInt(RegExp.$2.trim(), 10)
const serverParts = /^(.*),(.*)$/.exec(server)
if (serverParts) {
server = serverParts[1].trim()
port = parseInt(serverParts[2].trim(), 10)
} else {
const instanceParts = /^(.*),(.*)$/.exec(instanceName)
if (instanceParts) {
instanceName = instanceParts[1].trim()
port = parseInt(instanceParts[2].trim(), 10)
}
}
if (server === '.' || server === '(.)' || server.toLowerCase() === '(localdb)' || server.toLowerCase() === '(local)') {
server = 'localhost'
Expand Down Expand Up @@ -239,9 +248,10 @@ class ConnectionPool extends EventEmitter {
case 'user id': {
let user = value
let domain
if (/^(.*)\\(.*)$/.exec(user)) {
domain = RegExp.$1
user = RegExp.$2
const domainUser = /^(.*)\\(.*)$/.exec(user)
if (domainUser) {
domain = domainUser[1]
user = domainUser[2]
}
Object.assign(config, {
domain,
Expand Down
5 changes: 3 additions & 2 deletions lib/error/request-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ class RequestError extends MSSQLError {
}

this.name = 'RequestError'
if ((/^\[Microsoft\]\[SQL Server Native Client 11\.0\](?:\[SQL Server\])?([\s\S]*)$/).exec(this.message)) {
this.message = RegExp.$1
const parsedMessage = (/^\[Microsoft\]\[SQL Server Native Client 11\.0\](?:\[SQL Server\])?([\s\S]*)$/).exec(this.message)
if (parsedMessage) {
this.message = parsedMessage[1]
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/msnodesqlv8/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,9 @@ class Request extends BaseRequest {
})

req.on('info', msg => {
if ((/^\[Microsoft\]\[SQL Server Native Client 11\.0\](?:\[SQL Server\])?([\s\S]*)$/).exec(msg.message)) {
msg.message = RegExp.$1
const parsedMessage = (/^\[Microsoft\]\[SQL Server Native Client 11\.0\](?:\[SQL Server\])?([\s\S]*)$/).exec(msg.message)
if (parsedMessage) {
msg.message = parsedMessage[1]
}

this.emit('info', {
Expand Down
36 changes: 36 additions & 0 deletions test/common/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,39 @@ describe('value handlers', () => {
assert.strictEqual(sql.valueHandler.size, 0)
})
})

describe('connection string parser', () => {
it('parses named instance and port', () => {
const config = BasePool.parseConnectionString('Data source=instance\\database,1234')
assert.deepStrictEqual(config, {
options: {
instanceName: 'database'
},
pool: {},
port: 1234,
server: 'instance'
})
})
it('parses named instance and port (with instance port)', () => {
const config = BasePool.parseConnectionString('Data source=instance,1234\\database')
assert.deepStrictEqual(config, {
options: {
instanceName: 'database'
},
pool: {},
port: 1234,
server: 'instance'
})
})
it('parses named instance', () => {
const config = BasePool.parseConnectionString('Data source=instance\\database')
assert.deepStrictEqual(config, {
options: {
instanceName: 'database'
},
pool: {},
port: 1433,
server: 'instance'
})
})
})

0 comments on commit 73d6861

Please sign in to comment.