-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support query comments in the query builder (#6892)
add a `comment` method to the QueryBuilder so we can include an arbitrary comment in our queries for a variety of purposes - from query plan stability to SQL reverse proxy routing, to debugging. the comment builder is supported in selects, inserts, deletes, and updates this is directly inspired by the functionality supported by hibernate to handle SQL Query comments it uses C-style queries which are ANSI SQL 2003 & supported in all of the dialects of SQL that we support as drivers fixes #3643
- Loading branch information
1 parent
4475d80
commit 84c18a9
Showing
8 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {Entity} from "../../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
|
||
@Entity() | ||
export class Test { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
test/functional/query-builder/comment/query-builder-comment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import "reflect-metadata"; | ||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils"; | ||
import {Connection} from "../../../../src/connection/Connection"; | ||
import {Test} from "./entity/Test"; | ||
import {expect} from "chai"; | ||
|
||
describe("query builder > comment", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [Test], | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should scrub end comment pattern from string", () => Promise.all(connections.map(async connection => { | ||
const sql = connection.manager.createQueryBuilder(Test, "test") | ||
.comment("Hello World */") | ||
.getSql(); | ||
|
||
expect(sql).to.match(/^\/\* Hello World \*\/ SELECT/); | ||
}))); | ||
|
||
it("should not allow an empty comment", () => Promise.all(connections.map(async connection => { | ||
const sql = connection.manager.createQueryBuilder(Test, "test") | ||
.comment("") | ||
.getSql(); | ||
|
||
expect(sql).to.not.match(/^\/\* Hello World \*\/ SELECT/); | ||
}))); | ||
|
||
it("should allow a comment with just whitespaces", () => Promise.all(connections.map(async connection => { | ||
const sql = connection.manager.createQueryBuilder(Test, "test") | ||
.comment(" ") | ||
.getSql(); | ||
|
||
expect(sql).to.match(/^\/\* \*\/ SELECT/); | ||
}))); | ||
|
||
it("should allow a multi-line comment", () => Promise.all(connections.map(async connection => { | ||
const sql = connection.manager.createQueryBuilder(Test, "test") | ||
.comment("Hello World\nIt's a beautiful day!") | ||
.getSql(); | ||
|
||
expect(sql).to.match(/^\/\* Hello World\nIt's a beautiful day! \*\/ SELECT/); | ||
}))); | ||
|
||
it("should include comment in select", () => Promise.all(connections.map(async connection => { | ||
const sql = connection.manager.createQueryBuilder(Test, "test") | ||
.comment("Hello World") | ||
.getSql(); | ||
|
||
expect(sql).to.match(/^\/\* Hello World \*\/ SELECT/); | ||
}))); | ||
|
||
it("should include comment in update", () => Promise.all(connections.map(async connection => { | ||
const sql = connection.manager.createQueryBuilder(Test, "test") | ||
.update() | ||
.set({ id: 2 }) | ||
.comment("Hello World") | ||
.getSql(); | ||
|
||
expect(sql).to.match(/^\/\* Hello World \*\/ UPDATE/); | ||
}))); | ||
|
||
it("should include comment in insert", () => Promise.all(connections.map(async connection => { | ||
const sql = connection.manager.createQueryBuilder(Test, "test") | ||
.insert() | ||
.values({ id: 1 }) | ||
.comment("Hello World") | ||
.getSql(); | ||
|
||
expect(sql).to.match(/^\/\* Hello World \*\/ INSERT/); | ||
}))); | ||
|
||
it("should include comment in delete", () => Promise.all(connections.map(async connection => { | ||
const sql = connection.manager.createQueryBuilder(Test, "test") | ||
.delete() | ||
.comment("Hello World") | ||
.getSql(); | ||
|
||
expect(sql).to.match(/^\/\* Hello World \*\/ DELETE/); | ||
}))); | ||
|
||
}); |