Skip to content

Commit

Permalink
fix(QueryBuilder): Correctly format RETURNING clauses
Browse files Browse the repository at this point in the history
Specifically when using Quick, column formatters could return
a fully qualified column.  When using a column in a RETURNING clause,
the column should never be fully qualified.  It is always the result
of a query inserting into or updating a specific table.  This fix
uses only the last item in a dot-delimited list as the column name
after formatting.
  • Loading branch information
elpete committed Jun 19, 2020
1 parent 8211900 commit 977edcf
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 36 deletions.
76 changes: 41 additions & 35 deletions box.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,62 @@
{
"name":"qb",
"version":"7.9.1",
"author":"Eric Peterson",
"homepage":"https://github.com/coldbox-modules/qb",
"documentation":"https://github.com/coldbox-modules/qb",
"location":"forgeboxStorage",
"scripts":{
"generateAPIDocs":"rm .tmp --recurse --force && docbox generate mapping=qb excludes=test|ModuleConfig strategy-outputDir=.tmp/apidocs strategy-projectTitle=qb",
"commitAPIDocs":"run-script generateAPIDocs && !git add docs/apidocs/* && !git commit -m 'Updated API Docs'",
"format":"cfformat run models/**/*.cfc,tests/resources/**/*.cfc,tests/specs/**/*.cfc --overwrite",
"format:check":"cfformat check models/**/*.cfc,tests/resources/**/*.cfc,tests/specs/**/*.cfc"
"name": "qb",
"version": "7.9.1",
"author": "Eric Peterson",
"homepage": "https://github.com/coldbox-modules/qb",
"documentation": "https://github.com/coldbox-modules/qb",
"location": "forgeboxStorage",
"scripts": {
"generateAPIDocs": "rm .tmp --recurse --force && docbox generate mapping=qb excludes=test|ModuleConfig strategy-outputDir=.tmp/apidocs strategy-projectTitle=qb",
"commitAPIDocs": "run-script generateAPIDocs && !git add docs/apidocs/* && !git commit -m 'Updated API Docs'",
"format": "cfformat run models/**/*.cfc,tests/resources/**/*.cfc,tests/specs/**/*.cfc --overwrite",
"format:check": "cfformat check models/**/*.cfc,tests/resources/**/*.cfc,tests/specs/**/*.cfc"
},
"repository":{
"type":"git",
"URL":"https://github.com/coldbox-modules/qb"
"repository": {
"type": "git",
"URL": "https://github.com/coldbox-modules/qb"
},
"bugs":"https://github.com/coldbox-modules/qb/issues",
"slug":"qb",
"shortDescription":"A query builder for the rest of us",
"type":"modules",
"keywords":[
"bugs": "https://github.com/coldbox-modules/qb/issues",
"slug": "qb",
"shortDescription": "A query builder for the rest of us",
"type": "modules",
"keywords": [
"ORM",
"query",
"SQL"
],
"private":false,
"projectURL":"https://github.com/coldbox-modules/qb",
"license":[
"private": false,
"projectURL": "https://github.com/coldbox-modules/qb",
"license": [
{
"type":"MIT",
"URL":"https://github.com/coldbox-modules/qb/LICENSE"
"type": "MIT",
"URL": "https://github.com/coldbox-modules/qb/LICENSE"
}
],
"dependencies":{
"cbpaginator":"^2.0.0"
"dependencies": {
"cbpaginator": "^2.0.0"
},
"devDependencies":{
"testbox":"^3.0.0"
"devDependencies": {
"testbox": "^3.0.0"
},
"installPaths":{
"testbox":"testbox/",
"cbpaginator":"modules/cbpaginator/"
"installPaths": {
"testbox": "testbox/",
"cbpaginator": "modules/cbpaginator/"
},
"ignore":[
"ignore": [
"**/.*",
"test",
"tests",
"docs/**/*.*",
"server.json"
],
"testbox":{
"runner":"http://localhost:7777/tests/runner.cfm",
"verbose":false
"testbox": {
"runner": "http://localhost:7777/tests/runner.cfm",
"verbose": false
},
"githooks": {
"preCommit": [
"cfformat run `!git diff --name-only --staged` --overwrite",
"!git add `git diff --name-only --staged`"
]
}
}
4 changes: 3 additions & 1 deletion models/Query/QueryBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,9 @@ component displayname="QueryBuilder" accessors="true" {

function returning( required any columns ) {
variables.returning = isArray( arguments.columns ) ? arguments.columns : listToArray( arguments.columns );
variables.returning = variables.returning.map( applyColumnFormatter );
variables.returning = variables.returning.map( function( column ) {
return listLast( applyColumnFormatter( column ), "." );
} );
return this;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/resources/AbstractQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,18 @@ component extends="testbox.system.BaseSpec" {
}, returning() );
} );

it( "returning ignores table qualifiers", function() {
testCase( function( builder ) {
return builder
.setColumnFormatter( function( column ) {
return "tablePrefix." & column;
} )
.from( "users" )
.returning( "id" )
.insert( values = { "email": "foo", "name": "bar" }, toSql = true );
}, returningIgnoresTableQualifiers() );
} );

it( "can insert with raw values", function() {
testCase( function( builder ) {
return builder
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/Query/MySQLQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { exception: "UnsupportedOperation" };
}

function returningIgnoresTableQualifiers() {
return { exception: "UnsupportedOperation" };
}

function updateAllRecords() {
return { sql: "UPDATE `users` SET `email` = ?, `name` = ?", bindings: [ "foo", "bar" ] };
}
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/Query/OracleQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,10 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
return { exception: "UnsupportedOperation" };
}

function returningIgnoresTableQualifiers() {
return { exception: "UnsupportedOperation" };
}

function updateAllRecords() {
return { sql: "UPDATE ""USERS"" SET ""EMAIL"" = ?, ""NAME"" = ?", bindings: [ "foo", "bar" ] };
}
Expand Down
7 changes: 7 additions & 0 deletions tests/specs/Query/PostgresQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,13 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
};
}

function returningIgnoresTableQualifiers() {
return {
sql: "INSERT INTO ""users"" (""email"", ""name"") VALUES (?, ?) RETURNING ""id""",
bindings: [ "foo", "bar" ]
};
}

function updateAllRecords() {
return { sql: "UPDATE ""users"" SET ""email"" = ?, ""name"" = ?", bindings: [ "foo", "bar" ] };
}
Expand Down
7 changes: 7 additions & 0 deletions tests/specs/Query/SqlServerQueryBuilderSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,13 @@ component extends="tests.resources.AbstractQueryBuilderSpec" {
};
}

function returningIgnoresTableQualifiers() {
return {
sql: "INSERT INTO [users] ([email], [name]) OUTPUT INSERTED.[id] VALUES (?, ?)",
bindings: [ "foo", "bar" ]
};
}

function updateAllRecords() {
return { sql: "UPDATE [users] SET [email] = ?, [name] = ?", bindings: [ "foo", "bar" ] };
}
Expand Down

0 comments on commit 977edcf

Please sign in to comment.