Skip to content
Permalink
Browse files

fix(QueryBuilder): Fix for inserting null values directly

  • Loading branch information
elpete committed Jan 30, 2020
1 parent 4e493d6 commit 1de27a697f65bfdeed63442ad66be47cd0d30344
@@ -1793,7 +1793,7 @@ component displayname="QueryBuilder" accessors="true" {
columns.sort( "textnocase" );
var newBindings = values.map( function( value ) {
return columns.map( function( column ) {
return getUtils().extractBinding( value[ column ] );
return getUtils().extractBinding( value.keyExists( column ) ? value[ column ] : javacast( "null", "" ) );
} );
} );

@@ -10,7 +10,11 @@ component displayname="QueryUtils" singleton {
*
* @return any
*/
public any function extractBinding( required any value ) {
public any function extractBinding( any value ) {
if ( isNull( arguments.value ) ) {
return { "cfsqltype": "CF_SQL_VARCHAR", "value": "", "null": true };
}

if ( isBuilder( arguments.value ) ) {
return arguments.value.getBindings();
}
@@ -1693,6 +1693,17 @@ component extends="testbox.system.BaseSpec" {
);
}, insertWithRaw() );
} );

it( "can insert with null values", function() {
testCase( function( builder ) {
return builder
.from( "users" )
.insert(
values = { "email": "john@example.com", "optional_field": javacast( "null", "" ) },
toSql = true
);
}, insertWithNull() );
} );
} );

describe( "update statements", function() {
@@ -1822,7 +1833,11 @@ component extends="testbox.system.BaseSpec" {
if ( builder.getUtils().isExpression( binding ) ) {
return binding.getSQL();
} else {
return binding.value;
if ( binding.null ) {
return "NULL";
} else {
return binding.value;
}
}
} );
}
@@ -539,6 +539,13 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
};
}

function insertWithNull() {
return {
sql: "INSERT INTO `users` (`email`, `optional_field`) VALUES (?, ?)",
bindings: [ "john@example.com", "NULL" ]
};
}

function returning() {
return { exception: "UnsupportedOperation" };
}
@@ -551,6 +551,13 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
};
}

function insertWithNull() {
return {
sql: "INSERT ALL INTO ""USERS"" (""EMAIL"", ""OPTIONAL_FIELD"") VALUES (?, ?) SELECT 1 FROM dual",
bindings: [ "john@example.com", "NULL" ]
};
}

function returning() {
return { exception: "UnsupportedOperation" };
}
@@ -548,6 +548,13 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
};
}

function insertWithNull() {
return {
sql: "INSERT INTO ""users"" (""email"", ""optional_field"") VALUES (?, ?)",
bindings: [ "john@example.com", "NULL" ]
};
}

function returning() {
return {
sql: "INSERT INTO ""users"" (""email"", ""name"") VALUES (?, ?) RETURNING ""id""",
@@ -539,6 +539,13 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
};
}

function insertWithNull() {
return {
sql: "INSERT INTO [users] ([email], [optional_field]) VALUES (?, ?)",
bindings: [ "john@example.com", "NULL" ]
};
}

function returning() {
return {
sql: "INSERT INTO [users] ([email], [name]) OUTPUT INSERTED.[id] VALUES (?, ?)",

0 comments on commit 1de27a6

Please sign in to comment.
You can’t perform that action at this time.