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

Set ansi_null_dflt_on on by default #230

Merged
merged 1 commit into from
Mar 15, 2015
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
3 changes: 3 additions & 0 deletions src/connection.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -675,13 +676,15 @@ 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
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
Expand Down
47 changes: 47 additions & 0 deletions test/integration/connection-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

8 changes: 4 additions & 4 deletions test/integration/errors-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down