diff --git a/src/connection.coffee b/src/connection.coffee index fa2dcb729..8fb82dafd 100644 --- a/src/connection.coffee +++ b/src/connection.coffee @@ -277,6 +277,7 @@ class Connection extends EventEmitter @config.options.useColumnNames ?= false @config.options.connectionIsolationLevel ||= ISOLATION_LEVEL.READ_COMMITTED @config.options.readOnlyIntent ?= false + @config.options.enableAnsiNullDefault ?= true if !@config.options.port && !@config.options.instanceName @config.options.port = DEFAULT_PORT @@ -675,6 +676,7 @@ class Connection extends EventEmitter getInitialSql: -> xact_abort = if @config.options.abortTransactionOnError then 'on' else 'off' + enableAnsiNullDefault = if @config.options.enableAnsiNullDefault then 'on' else 'off' """set textsize #{@config.options.textsize} set quoted_identifier on set arithabort off @@ -682,6 +684,7 @@ set numeric_roundabort off set ansi_warnings on set ansi_padding on set ansi_nulls on +set ansi_null_dflt_on #{enableAnsiNullDefault} set concat_null_yields_null on set cursor_close_on_commit off set implicit_transactions off diff --git a/test/integration/connection-test.coffee b/test/integration/connection-test.coffee index 4bfbbd79a..a16dde592 100644 --- a/test/integration/connection-test.coffee +++ b/test/integration/connection-test.coffee @@ -915,3 +915,50 @@ exports.requestTimeout = (test) -> connection.on('debug', (text) -> #console.log(text) ) + +runSqlBatch = (test, config, sql, requestCallback) -> + connection = new Connection(config) + + request = new Request sql, -> + requestCallback.apply this, arguments + connection.close() + + connection.on 'connect', (err) -> + test.ifError(err) + connection.execSqlBatch(request) + + connection.on 'end', (info) -> + test.done() + +# Test that the default behavior allows adding null values to a +# temporary table where the nullability is not explicitly declared. +exports.testAnsiNullDefault = (test) -> + test.expect(2) + + sql = """ + create table #testAnsiNullDefault (id int); + insert #testAnsiNullDefault values (null); + drop table #testAnsiNullDefault; + """ + + runSqlBatch test, getConfig(), sql, (err) -> + test.ifError(err) + +# Test that the default behavior can be overridden (so that temporary +# table columns are non-nullable by default). +exports.disableAnsiNullDefault = (test) -> + test.expect(3) + + sql = """ + create table #testAnsiNullDefaults (id int); + insert #testAnsiNullDefaults values (null); + drop table #testAnsiNullDefaults; + """ + + config = getConfig() + config.options.enableAnsiNullDefault = false + + runSqlBatch test, config, sql, (err) -> + test.ok(err instanceof Error) + test.strictEqual err?.number, 515 # Cannot insert the value NULL + diff --git a/test/integration/errors-test.coffee b/test/integration/errors-test.coffee index f1a38d087..8bdb8ee71 100644 --- a/test/integration/errors-test.coffee +++ b/test/integration/errors-test.coffee @@ -31,11 +31,11 @@ exports.uniqueConstraint = (test) -> test.ok(err instanceof Error) test.strictEqual(err.number, 2627) -exports.ansiNullDefaults = (test) -> +exports.nullable = (test) -> sql = """ - create table #testAnsiNullDefault (id int); - insert #testAnsiNullDefault values (null); - drop table #testAnsiNullDefault; + create table #testNullable (id int not null); + insert #testNullable values (null); + drop table #testNullable; """ test.expect(3)