|
| 1 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { ParameterType } from "../../../parameter-type"; |
| 4 | +import { DB2Platform } from "../../../platforms/db2-platform"; |
| 5 | +import { NotSupported } from "../../../platforms/exception/not-supported"; |
| 6 | +import { MariaDBPlatform } from "../../../platforms/mariadb-platform"; |
| 7 | +import { MariaDB1060Platform } from "../../../platforms/mariadb1060-platform"; |
| 8 | +import { MySQLPlatform } from "../../../platforms/mysql-platform"; |
| 9 | +import { MySQL80Platform } from "../../../platforms/mysql80-platform"; |
| 10 | +import { OraclePlatform } from "../../../platforms/oracle-platform"; |
| 11 | +import { SQLitePlatform } from "../../../platforms/sqlite-platform"; |
| 12 | +import { ConflictResolutionMode } from "../../../query/for-update/conflict-resolution-mode"; |
| 13 | +import { UnionType } from "../../../query/union-type"; |
| 14 | +import { Column } from "../../../schema/column"; |
| 15 | +import { PrimaryKeyConstraint } from "../../../schema/primary-key-constraint"; |
| 16 | +import { Table } from "../../../schema/table"; |
| 17 | +import { Types } from "../../../types/types"; |
| 18 | +import { useFunctionalTestCase } from "../_helpers/functional-test-case"; |
| 19 | + |
| 20 | +describe("Functional/Query/QueryBuilderTest", () => { |
| 21 | + const functional = useFunctionalTestCase(); |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + const table = Table.editor() |
| 25 | + .setUnquotedName("for_update") |
| 26 | + .setColumns(Column.editor().setUnquotedName("id").setTypeName(Types.INTEGER).create()) |
| 27 | + .setPrimaryKeyConstraint(PrimaryKeyConstraint.editor().setUnquotedColumnNames("id").create()) |
| 28 | + .create(); |
| 29 | + |
| 30 | + await functional.dropAndCreateTable(table); |
| 31 | + await functional.connection().insert("for_update", { id: 1 }); |
| 32 | + await functional.connection().insert("for_update", { id: 2 }); |
| 33 | + }); |
| 34 | + |
| 35 | + it("for update ordinary", async ({ skip }) => { |
| 36 | + const connection = functional.connection(); |
| 37 | + if (connection.getDatabasePlatform() instanceof SQLitePlatform) { |
| 38 | + skip(); |
| 39 | + } |
| 40 | + |
| 41 | + const qb = connection.createQueryBuilder(); |
| 42 | + qb.select("id").from("for_update").forUpdate(); |
| 43 | + |
| 44 | + expect(await qb.fetchFirstColumn()).toEqual([1, 2]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("for update skip locked when supported", async ({ skip }) => { |
| 48 | + const connection = functional.connection(); |
| 49 | + if (!platformSupportsSkipLocked(connection.getDatabasePlatform())) { |
| 50 | + skip(); |
| 51 | + } |
| 52 | + |
| 53 | + const qb1 = connection.createQueryBuilder(); |
| 54 | + qb1.select("id").from("for_update").where("id = 1").forUpdate(); |
| 55 | + |
| 56 | + await connection.beginTransaction(); |
| 57 | + expect(await qb1.fetchFirstColumn()).toEqual([1]); |
| 58 | + |
| 59 | + const connection2 = await functional.createConnection(); |
| 60 | + |
| 61 | + try { |
| 62 | + const qb2 = connection2.createQueryBuilder(); |
| 63 | + qb2 |
| 64 | + .select("id") |
| 65 | + .from("for_update") |
| 66 | + .orderBy("id") |
| 67 | + .forUpdate(ConflictResolutionMode.SKIP_LOCKED); |
| 68 | + |
| 69 | + expect(await qb2.fetchFirstColumn()).toEqual([2]); |
| 70 | + } finally { |
| 71 | + await connection2.close(); |
| 72 | + if (connection.isTransactionActive()) { |
| 73 | + await connection.rollBack(); |
| 74 | + } |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it("for update skip locked when not supported", async ({ skip }) => { |
| 79 | + const connection = functional.connection(); |
| 80 | + if (platformSupportsSkipLocked(connection.getDatabasePlatform())) { |
| 81 | + skip(); |
| 82 | + } |
| 83 | + |
| 84 | + const qb = connection.createQueryBuilder(); |
| 85 | + qb.select("id").from("for_update").forUpdate(ConflictResolutionMode.SKIP_LOCKED); |
| 86 | + |
| 87 | + await expect(qb.executeQuery()).rejects.toThrow(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("union all and distinct return expected results", async () => { |
| 91 | + const connection = functional.connection(); |
| 92 | + const platform = connection.getDatabasePlatform(); |
| 93 | + |
| 94 | + const qbAll = connection.createQueryBuilder(); |
| 95 | + qbAll |
| 96 | + .union(platform.getDummySelectSQL("2 as field_one")) |
| 97 | + .addUnion(platform.getDummySelectSQL("1 as field_one"), UnionType.ALL) |
| 98 | + .addUnion(platform.getDummySelectSQL("1 as field_one"), UnionType.ALL) |
| 99 | + .orderBy("field_one", "ASC"); |
| 100 | + |
| 101 | + const qbDistinct = connection.createQueryBuilder(); |
| 102 | + qbDistinct |
| 103 | + .union(platform.getDummySelectSQL("2 as field_one")) |
| 104 | + .addUnion(platform.getDummySelectSQL("1 as field_one"), UnionType.DISTINCT) |
| 105 | + .addUnion(platform.getDummySelectSQL("1 as field_one"), UnionType.DISTINCT) |
| 106 | + .orderBy("field_one", "ASC"); |
| 107 | + |
| 108 | + const allRows = normalizeNumericRows( |
| 109 | + await qbAll.executeQuery().then((result) => result.fetchAllAssociative()), |
| 110 | + ); |
| 111 | + const distinctRows = normalizeNumericRows( |
| 112 | + await qbDistinct.executeQuery().then((result) => result.fetchAllAssociative()), |
| 113 | + ); |
| 114 | + |
| 115 | + expect(allRows).toEqual([{ field_one: 1 }, { field_one: 1 }, { field_one: 2 }]); |
| 116 | + expect(distinctRows).toEqual([{ field_one: 1 }, { field_one: 2 }]); |
| 117 | + }); |
| 118 | + |
| 119 | + it("union and addUnion work with query builder parts and named parameters", async () => { |
| 120 | + const connection = functional.connection(); |
| 121 | + const qb = connection.createQueryBuilder(); |
| 122 | + |
| 123 | + const sub1 = qb |
| 124 | + .sub() |
| 125 | + .select("id") |
| 126 | + .from("for_update") |
| 127 | + .where(qb.expr().eq("id", qb.createNamedParameter(1, ParameterType.INTEGER))); |
| 128 | + const sub2 = qb |
| 129 | + .sub() |
| 130 | + .select("id") |
| 131 | + .from("for_update") |
| 132 | + .where(qb.expr().eq("id", qb.createNamedParameter(2, ParameterType.INTEGER))); |
| 133 | + const sub3 = qb |
| 134 | + .sub() |
| 135 | + .select("id") |
| 136 | + .from("for_update") |
| 137 | + .where(qb.expr().eq("id", qb.createNamedParameter(1, ParameterType.INTEGER))); |
| 138 | + |
| 139 | + qb.union(sub1) |
| 140 | + .addUnion(sub2, UnionType.DISTINCT) |
| 141 | + .addUnion(sub3, UnionType.DISTINCT) |
| 142 | + .orderBy("id", "DESC"); |
| 143 | + |
| 144 | + const rows = normalizeNumericRows( |
| 145 | + await qb.executeQuery().then((result) => result.fetchAllAssociative()), |
| 146 | + ); |
| 147 | + |
| 148 | + expect(rows).toEqual([{ id: 2 }, { id: 1 }]); |
| 149 | + }); |
| 150 | + |
| 151 | + it("select with CTE named parameter", async ({ skip }) => { |
| 152 | + const connection = functional.connection(); |
| 153 | + const platform = connection.getDatabasePlatform(); |
| 154 | + if (!platformSupportsCTEs(platform) || !platformSupportsCTEColumnsDefinition(platform)) { |
| 155 | + skip(); |
| 156 | + } |
| 157 | + |
| 158 | + const qb = connection.createQueryBuilder(); |
| 159 | + const cteQueryBuilder = qb |
| 160 | + .sub() |
| 161 | + .select("id AS virtual_id") |
| 162 | + .from("for_update") |
| 163 | + .where("id = :id"); |
| 164 | + |
| 165 | + qb.with("cte_a", cteQueryBuilder, ["virtual_id"]) |
| 166 | + .select("virtual_id") |
| 167 | + .from("cte_a") |
| 168 | + .setParameter("id", 1); |
| 169 | + |
| 170 | + const rows = normalizeNumericRows( |
| 171 | + await qb.executeQuery().then((result) => result.fetchAllAssociative()), |
| 172 | + ); |
| 173 | + |
| 174 | + expect(rows).toEqual([{ virtual_id: 1 }]); |
| 175 | + }); |
| 176 | + |
| 177 | + it("platform does not support CTE", async ({ skip }) => { |
| 178 | + const connection = functional.connection(); |
| 179 | + if (platformSupportsCTEs(connection.getDatabasePlatform())) { |
| 180 | + skip(); |
| 181 | + } |
| 182 | + |
| 183 | + const qb = connection.createQueryBuilder(); |
| 184 | + const cteQueryBuilder = qb.sub().select("id").from("for_update"); |
| 185 | + qb.with("cte_a", cteQueryBuilder).select("id").from("cte_a"); |
| 186 | + |
| 187 | + await expect(qb.executeQuery()).rejects.toThrow(NotSupported); |
| 188 | + }); |
| 189 | +}); |
| 190 | + |
| 191 | +function normalizeNumericRows( |
| 192 | + rows: Array<Record<string, unknown>>, |
| 193 | +): Array<Record<string, number | string | null>> { |
| 194 | + return rows.map((row) => |
| 195 | + Object.fromEntries( |
| 196 | + Object.entries(row).map(([key, value]) => [ |
| 197 | + key.toLowerCase(), |
| 198 | + typeof value === "number" |
| 199 | + ? value |
| 200 | + : typeof value === "bigint" |
| 201 | + ? Number(value) |
| 202 | + : typeof value === "string" && /^-?\d+$/.test(value) |
| 203 | + ? Number(value) |
| 204 | + : (value as string | null), |
| 205 | + ]), |
| 206 | + ), |
| 207 | + ); |
| 208 | +} |
| 209 | + |
| 210 | +function platformSupportsSkipLocked(platform: unknown): boolean { |
| 211 | + if (platform instanceof DB2Platform) { |
| 212 | + return false; |
| 213 | + } |
| 214 | + |
| 215 | + if (platform instanceof MySQLPlatform) { |
| 216 | + return platform instanceof MySQL80Platform; |
| 217 | + } |
| 218 | + |
| 219 | + if (platform instanceof MariaDBPlatform) { |
| 220 | + return platform instanceof MariaDB1060Platform; |
| 221 | + } |
| 222 | + |
| 223 | + return !(platform instanceof SQLitePlatform); |
| 224 | +} |
| 225 | + |
| 226 | +function platformSupportsCTEs(platform: unknown): boolean { |
| 227 | + return !(platform instanceof MySQLPlatform) || platform instanceof MySQL80Platform; |
| 228 | +} |
| 229 | + |
| 230 | +function platformSupportsCTEColumnsDefinition(platform: unknown): boolean { |
| 231 | + if (platform instanceof DB2Platform || platform instanceof OraclePlatform) { |
| 232 | + return false; |
| 233 | + } |
| 234 | + |
| 235 | + return !(platform instanceof MySQLPlatform) || platform instanceof MySQL80Platform; |
| 236 | +} |
0 commit comments