|
| 1 | +import { expect } from "chai" |
| 2 | +import "reflect-metadata" |
| 3 | +import { DataSource } from "../../../../src/data-source/DataSource" |
| 4 | +import { |
| 5 | + closeTestingConnections, |
| 6 | + createTestingConnections, |
| 7 | + reloadTestingDatabases, |
| 8 | +} from "../../../utils/test-utils" |
| 9 | +import { Person } from "./entity/Person" |
| 10 | +import { DriverUtils } from "../../../../src/driver/DriverUtils" |
| 11 | + |
| 12 | +describe("query builder > parameters > reused parameters", () => { |
| 13 | + let dataSources: DataSource[] |
| 14 | + before( |
| 15 | + async () => |
| 16 | + (dataSources = await createTestingConnections({ |
| 17 | + entities: [Person], |
| 18 | + schemaCreate: true, |
| 19 | + dropSchema: true, |
| 20 | + })), |
| 21 | + ) |
| 22 | + |
| 23 | + beforeEach(() => reloadTestingDatabases(dataSources)) |
| 24 | + after(() => closeTestingConnections(dataSources)) |
| 25 | + |
| 26 | + it("should generate a valid query", () => |
| 27 | + Promise.all( |
| 28 | + dataSources.map(async (dataSource) => { |
| 29 | + const personRepository = dataSource.getRepository(Person) |
| 30 | + await personRepository.save([ |
| 31 | + { firstName: "Jane", lastName: "Smith" }, |
| 32 | + { firstName: "Johanna", lastName: "Schmidt" }, |
| 33 | + { firstName: "Ioana", lastName: "Fieraru" }, |
| 34 | + { firstName: "Giovanna", lastName: "Ferrari" }, |
| 35 | + ]) |
| 36 | + |
| 37 | + const sqlSubstring = |
| 38 | + dataSource.driver.options.type === "oracle" |
| 39 | + ? "SUBSTR" |
| 40 | + : "SUBSTRING" |
| 41 | + const firstNameTrimmed = `${sqlSubstring}(${dataSource.driver.escape( |
| 42 | + "firstName", |
| 43 | + )}, 1, :charCount)` |
| 44 | + const lastNameTrimmed = `${sqlSubstring}(${dataSource.driver.escape( |
| 45 | + "lastName", |
| 46 | + )}, 1, :charCount)` |
| 47 | + |
| 48 | + const queryBuilder = personRepository |
| 49 | + .createQueryBuilder() |
| 50 | + .select(firstNameTrimmed, "firstName") |
| 51 | + .addSelect(lastNameTrimmed, "lastName") |
| 52 | + .setParameters({ charCount: 5 }) |
| 53 | + |
| 54 | + const [query, parameters] = queryBuilder.getQueryAndParameters() |
| 55 | + |
| 56 | + if (DriverUtils.isPostgresFamily(dataSource.driver)) { |
| 57 | + expect(query).to.equal( |
| 58 | + 'SELECT SUBSTRING("firstName", 1, $1) AS "firstName", SUBSTRING("lastName", 1, $1) AS "lastName" FROM "person" "Person"', |
| 59 | + ) |
| 60 | + expect(parameters).to.have.length(1) |
| 61 | + } else if (DriverUtils.isMySQLFamily(dataSource.driver)) { |
| 62 | + expect(query).to.equal( |
| 63 | + "SELECT SUBSTRING(`firstName`, 1, ?) AS `firstName`, SUBSTRING(`lastName`, 1, ?) AS `lastName` FROM `person` `Person`", |
| 64 | + ) |
| 65 | + expect(parameters).to.have.length(2) |
| 66 | + } else if (DriverUtils.isSQLiteFamily(dataSource.driver)) { |
| 67 | + expect(query).to.equal( |
| 68 | + 'SELECT SUBSTRING("firstName", 1, 5) AS "firstName", SUBSTRING("lastName", 1, 5) AS "lastName" FROM "person" "Person"', |
| 69 | + ) |
| 70 | + expect(parameters).to.have.length(0) |
| 71 | + } else if (dataSource.driver.options.type === "spanner") { |
| 72 | + expect(query).to.equal( |
| 73 | + 'SELECT SUBSTRING("firstName", 1, @param0) AS "firstName", SUBSTRING("lastName", 1, @param0) AS "lastName" FROM "person" "Person"', |
| 74 | + ) |
| 75 | + expect(parameters).to.have.length(1) |
| 76 | + } else if (dataSource.driver.options.type === "oracle") { |
| 77 | + expect(query).to.equal( |
| 78 | + 'SELECT SUBSTR("firstName", 1, :1) AS "firstName", SUBSTR("lastName", 1, :2) AS "lastName" FROM "person" "Person"', |
| 79 | + ) |
| 80 | + expect(parameters).to.have.length(2) |
| 81 | + } else if (dataSource.driver.options.type === "mssql") { |
| 82 | + expect(query).to.equal( |
| 83 | + 'SELECT SUBSTRING("firstName", 1, @0) AS "firstName", SUBSTRING("lastName", 1, @0) AS "lastName" FROM "person" "Person"', |
| 84 | + ) |
| 85 | + expect(parameters).to.have.length(1) |
| 86 | + } else { |
| 87 | + // e.g.: SAP |
| 88 | + expect(query).to.equal( |
| 89 | + 'SELECT SUBSTRING("firstName", 1, ?) AS "firstName", SUBSTRING("lastName", 1, ?) AS "lastName" FROM "person" "Person"', |
| 90 | + ) |
| 91 | + expect(parameters).to.have.length(2) |
| 92 | + } |
| 93 | + |
| 94 | + const statistics = await queryBuilder.getRawMany<unknown>() |
| 95 | + |
| 96 | + expect(statistics).to.deep.equal([ |
| 97 | + { |
| 98 | + firstName: "Jane", |
| 99 | + lastName: "Smith", |
| 100 | + }, |
| 101 | + { |
| 102 | + firstName: "Johan", |
| 103 | + lastName: "Schmi", |
| 104 | + }, |
| 105 | + { |
| 106 | + firstName: "Ioana", |
| 107 | + lastName: "Fiera", |
| 108 | + }, |
| 109 | + { |
| 110 | + firstName: "Giova", |
| 111 | + lastName: "Ferra", |
| 112 | + }, |
| 113 | + ]) |
| 114 | + }), |
| 115 | + )) |
| 116 | +}) |
0 commit comments