Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to set DateFirst #503

Merged
merged 9 commits into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const DEFAULT_CLIENT_REQUEST_TIMEOUT = 15 * 1000;
const DEFAULT_CANCEL_TIMEOUT = 5 * 1000;
const DEFAULT_PACKET_SIZE = 4 * 1024;
const DEFAULT_TEXTSIZE = '2147483647';
const DEFAULT_DATEFIRST = 7;
const DEFAULT_PORT = 1433;
const DEFAULT_TDS_VERSION = '7_4';

Expand Down Expand Up @@ -61,6 +62,7 @@ class Connection extends EventEmitter {
connectionIsolationLevel: ISOLATION_LEVEL.READ_COMMITTED,
cryptoCredentialsDetails: {},
database: undefined,
datefirst: DEFAULT_DATEFIRST,
enableArithAbort: false,
enableAnsiNullDefault: true,
encrypt: false,
Expand Down Expand Up @@ -127,6 +129,14 @@ class Connection extends EventEmitter {
this.config.options.database = config.options.database;
}

if (config.options.datefirst) {
if (config.options.datefirst < 1 || config.options.port > 7) {
throw new RangeError('DateFirst should be >= 1 and <= 7');
}

this.config.options.datefirst = config.options.datefirst;
}

if (config.options.enableAnsiNullDefault != undefined) {
this.config.options.enableAnsiNullDefault = config.options.enableAnsiNullDefault;
}
Expand Down Expand Up @@ -711,7 +721,7 @@ class Connection extends EventEmitter {
const xact_abort = this.config.options.abortTransactionOnError ? 'on' : 'off';
const enableAnsiNullDefault = this.config.options.enableAnsiNullDefault ? 'on' : 'off';
const enableArithAbort = this.config.options.enableArithAbort ? 'on' : 'off';
return 'set textsize ' + this.config.options.textsize + '\nset quoted_identifier on\nset arithabort ' + enableArithAbort + '\nset numeric_roundabort off\nset ansi_warnings on\nset ansi_padding on\nset ansi_nulls on\nset ansi_null_dflt_on ' + enableAnsiNullDefault + '\nset concat_null_yields_null on\nset cursor_close_on_commit off\nset implicit_transactions off\nset language us_english\nset dateformat mdy\nset datefirst 7\nset transaction isolation level ' + (this.getIsolationLevelText(this.config.options.connectionIsolationLevel)) + '\nset xact_abort ' + xact_abort;
return 'set textsize ' + this.config.options.textsize + '\nset quoted_identifier on\nset arithabort ' + enableArithAbort + '\nset numeric_roundabort off\nset ansi_warnings on\nset ansi_padding on\nset ansi_nulls on\nset ansi_null_dflt_on ' + enableAnsiNullDefault + '\nset concat_null_yields_null on\nset cursor_close_on_commit off\nset implicit_transactions off\nset language us_english\nset dateformat mdy\nset datefirst ' + this.config.options.datefirst + '\nset transaction isolation level ' + (this.getIsolationLevelText(this.config.options.connectionIsolationLevel)) + '\nset xact_abort ' + xact_abort;
}

processedInitialSql() {
Expand Down
49 changes: 47 additions & 2 deletions test/integration/connection-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ runNtlmTest = (test, domainCase) ->
)

exports.ntlm = (test) ->
runNtlmTest test, DomainCaseEnum.AsIs
runNtlmTest test, DomainCaseEnum.AsIs

exports.ntlmLower = (test) ->
runNtlmTest test, DomainCaseEnum.Lower
Expand Down Expand Up @@ -1056,7 +1056,6 @@ exports.disableAnsiNullDefault = (test) ->

testArithAbort = (test, setting) ->
test.expect(5)

config = getConfig()
config.options.enableArithAbort = setting if typeof setting is 'boolean'

Expand Down Expand Up @@ -1106,3 +1105,49 @@ exports.badArithAbort = (test) ->
connection = new Connection(config)

test.done()

testDateFirstImpl = (test, datefirst) =>
datefirst = datefirst || 7
test.expect(3)
config = getConfig()
config.options.datefirst = datefirst

connection = new Connection(config)

request = new Request('select @@datefirst', (err) ->
test.ifError(err)
connection.close()
)

request.on('row', (columns) ->
dateFirstActual = columns[0].value
test.strictEqual(dateFirstActual, datefirst)
)

connection.on 'connect', (err) ->
test.ifError(err)
connection.execSql(request)

connection.on 'end', (info) ->
test.done()

# Test that the default setting for DATEFIRST is 7
exports.testDatefirstDefault = (test) ->
testDateFirstImpl(test, undefined)

# Test that the DATEFIRST setting can be changed via an optional configuration
exports.testDatefirstCustom = (test) ->
testDateFirstImpl(test, 3)

# Test that an invalid DATEFIRST setting throws
exports.badDatefirst = (test) ->
test.expect(1)
config = getConfig()
config.options.datefirst = -1

connection = null

test.throws ->
connection = new Connection(config)

test.done()