Skip to content

Commit

Permalink
reverting back some instanceof checks to prevent compiler errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pleerock committed Feb 10, 2022
1 parent 7588ac1 commit 7bf12a3
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 16 deletions.
9 changes: 6 additions & 3 deletions src/query-builder/InsertQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {AbstractSqliteDriver} from "../driver/sqlite-abstract/AbstractSqliteDriv
import {BroadcasterResult} from "../subscriber/BroadcasterResult";
import {EntitySchema} from "../entity-schema/EntitySchema";
import {TypeORMError} from "../error";
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver";
import {MysqlDriver} from "../driver/mysql/MysqlDriver";
import {AuroraDataApiDriver} from "../driver/aurora-data-api/AuroraDataApiDriver";

/**
* Allows to build complex sql queries in a fashion way and execute those queries.
Expand Down Expand Up @@ -100,7 +103,7 @@ export class InsertQueryBuilder<Entity> extends QueryBuilder<Entity> {
));
}

if (returningColumns.length > 0 && this.connection.driver.options.type === "mssql") {
if (returningColumns.length > 0 && this.connection.driver instanceof SqlServerDriver) {
declareSql = this.connection.driver.buildTableVariableDeclaration("@OutputTable", returningColumns);
selectOutputSql = `SELECT * FROM @OutputTable`;
}
Expand Down Expand Up @@ -555,7 +558,7 @@ export class InsertQueryBuilder<Entity> extends QueryBuilder<Entity> {

// just any other regular value
} else {
if (this.connection.driver.options.type === "mssql")
if (this.connection.driver instanceof SqlServerDriver)
value = this.connection.driver.parametrizeValue(column, value);

// we need to store array values in a special class to make sure parameter replacement will work correctly
Expand All @@ -565,7 +568,7 @@ export class InsertQueryBuilder<Entity> extends QueryBuilder<Entity> {

const paramName = this.createParameter(value);

if ((this.connection.driver.options.type === "mysql" || this.connection.driver.options.type === "aurora-data-api") && this.connection.driver.spatialTypes.indexOf(column.type) !== -1) {
if ((this.connection.driver instanceof MysqlDriver || this.connection.driver instanceof AuroraDataApiDriver) && this.connection.driver.spatialTypes.indexOf(column.type) !== -1) {
const useLegacy = this.connection.driver.options.legacySpatialSupport;
const geomFromText = useLegacy ? "GeomFromText" : "ST_GeomFromText";
if (column.srid != null) {
Expand Down
3 changes: 2 additions & 1 deletion src/query-builder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {TypeORMError} from "../error";
import {WhereClause, WhereClauseCondition} from "./WhereClause";
import {NotBrackets} from "./NotBrackets";
import {EntityPropertyNotFoundError} from "../error/EntityPropertyNotFoundError";
import {OracleDriver} from "../driver/oracle/OracleDriver";

// todo: completely cover query builder with tests
// todo: entityOrProperty can be target name. implement proper behaviour if it is.
Expand Down Expand Up @@ -761,7 +762,7 @@ export abstract class QueryBuilder<Entity> {
}
}).join(", ");

if (driver.options.type === "oracle") {
if (driver instanceof OracleDriver) {
columnsExpression += " INTO " + columns.map(column => {
return this.createParameter({ type: driver.columnTypeToNativeParameter(column.type), dir: driver.oracle.BIND_OUT });
}).join(", ");
Expand Down
4 changes: 3 additions & 1 deletion src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {FindOperator} from "../find-options/FindOperator";
import {OrmUtils} from "../util/OrmUtils";
import {EntityPropertyNotFoundError} from "../error/EntityPropertyNotFoundError";
import {EqualOperator} from "../find-options/EqualOperator";
import {MysqlDriver} from "../driver/mysql/MysqlDriver";
import {AuroraDataApiDriver} from "../driver/aurora-data-api/AuroraDataApiDriver";

/**
* Allows to build complex sql queries in a fashion way and execute those queries.
Expand Down Expand Up @@ -1841,7 +1843,7 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements
allColumns.forEach(column => {
let selectionPath = this.escape(aliasName) + "." + this.escape(column.databaseName);
if (this.connection.driver.spatialTypes.indexOf(column.type) !== -1) {
if (this.connection.driver.options.type === "mysql" || this.connection.driver.options.type === "aurora-data-api") {
if (this.connection.driver instanceof MysqlDriver || this.connection.driver instanceof AuroraDataApiDriver) {
const useLegacy = this.connection.driver.options.legacySpatialSupport;
const asText = useLegacy ? "AsText" : "ST_AsText";
selectionPath = `${asText}(${selectionPath})`;
Expand Down
9 changes: 6 additions & 3 deletions src/query-builder/UpdateQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {UpdateValuesMissingError} from "../error/UpdateValuesMissingError";
import {QueryDeepPartialEntity} from "./QueryPartialEntity";
import {TypeORMError} from "../error";
import {EntityPropertyNotFoundError} from "../error/EntityPropertyNotFoundError";
import {MysqlDriver} from "../driver/mysql/MysqlDriver";
import {AuroraDataApiDriver} from "../driver/aurora-data-api/AuroraDataApiDriver";
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver";

/**
* Allows to build complex sql queries in a fashion way and execute those queries.
Expand Down Expand Up @@ -90,7 +93,7 @@ export class UpdateQueryBuilder<Entity> extends QueryBuilder<Entity> implements
));
}

if (returningColumns.length > 0 && this.connection.driver.options.type === "mssql") {
if (returningColumns.length > 0 && this.connection.driver instanceof SqlServerDriver) {
declareSql = this.connection.driver.buildTableVariableDeclaration("@OutputTable", returningColumns);
selectOutputSql = `SELECT * FROM @OutputTable`;
}
Expand Down Expand Up @@ -405,14 +408,14 @@ export class UpdateQueryBuilder<Entity> extends QueryBuilder<Entity> implements
} else if (this.connection.driver.options.type === "sap" && value === null) {
updateColumnAndValues.push(this.escape(column.databaseName) + " = NULL");
} else {
if (this.connection.driver.options.type === "mssql") {
if (this.connection.driver instanceof SqlServerDriver) {
value = this.connection.driver.parametrizeValue(column, value);
}

const paramName = this.createParameter(value);

let expression = null;
if ((this.connection.driver.options.type === "mysql" || this.connection.driver.options.type === "aurora-data-api") && this.connection.driver.spatialTypes.indexOf(column.type) !== -1) {
if ((this.connection.driver instanceof MysqlDriver || this.connection.driver instanceof AuroraDataApiDriver) && this.connection.driver.spatialTypes.indexOf(column.type) !== -1) {
const useLegacy = this.connection.driver.options.legacySpatialSupport;
const geomFromText = useLegacy ? "GeomFromText" : "ST_GeomFromText";
if (column.srid != null) {
Expand Down
3 changes: 2 additions & 1 deletion src/schema-builder/RdbmsSchemaBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {TableCheck} from "./table/TableCheck";
import {TableExclusion} from "./table/TableExclusion";
import {View} from "./view/View";
import {ViewUtils} from "./util/ViewUtils";
import {PostgresDriver} from "../driver/postgres/PostgresDriver";

/**
* Creates complete tables schemas in the database based on the entity metadatas.
Expand Down Expand Up @@ -108,7 +109,7 @@ export class RdbmsSchemaBuilder implements SchemaBuilder {
* If the schema contains views, create the typeorm_metadata table if it doesn't exist yet
*/
async createMetadataTableIfNecessary(queryRunner: QueryRunner): Promise<void> {
if (this.viewEntityToSyncMetadatas.length > 0 || (this.connection.driver.options.type === "postgres" && this.connection.driver.isGeneratedColumnsSupported)) {
if (this.viewEntityToSyncMetadatas.length > 0 || (this.connection.driver instanceof PostgresDriver && this.connection.driver.isGeneratedColumnsSupported)) {
await this.createTypeormMetadataTable(queryRunner);
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/functional/query-runner/add-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Connection} from "../../../src/connection/Connection";
import {AbstractSqliteDriver} from "../../../src/driver/sqlite-abstract/AbstractSqliteDriver";
import {TableColumn} from "../../../src/schema-builder/table/TableColumn";
import {closeTestingConnections, createTestingConnections} from "../../utils/test-utils";
import {PostgresDriver} from "../../../src/driver/postgres/PostgresDriver";

describe("query runner > add column", () => {

Expand Down Expand Up @@ -90,7 +91,7 @@ describe("query runner > add column", () => {
const isMySQL = connection.driver.options.type === "mysql" && connection.options.type === "mysql";
let postgresSupported = false;

if (connection.driver.options.type === "postgres") {
if (connection.driver instanceof PostgresDriver) {
postgresSupported = connection.driver.isGeneratedColumnsSupported;
}

Expand Down
3 changes: 2 additions & 1 deletion test/functional/query-runner/change-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Connection} from "../../../src/connection/Connection";
import {closeTestingConnections, createTestingConnections} from "../../utils/test-utils";
import {AbstractSqliteDriver} from "../../../src/driver/sqlite-abstract/AbstractSqliteDriver";
import {TableColumn} from "../../../src";
import {PostgresDriver} from "../../../src/driver/postgres/PostgresDriver";

describe("query runner > change column", () => {

Expand Down Expand Up @@ -138,7 +139,7 @@ describe("query runner > change column", () => {
it("should correctly change generated as expression", () => Promise.all(connections.map(async connection => {

// Only works on postgres
if (!(connection.driver.options.type === "postgres")) return;
if (!(connection.driver instanceof PostgresDriver)) return;

const queryRunner = connection.createQueryRunner();

Expand Down
9 changes: 6 additions & 3 deletions test/functional/schema-builder/change-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {AbstractSqliteDriver} from "../../../src/driver/sqlite-abstract/Abstract
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Post} from "./entity/Post";
import {PostVersion} from "./entity/PostVersion";
import {PostgresDriver} from "../../../src/driver/postgres/PostgresDriver";
import {CockroachDriver} from "../../../src/driver/cockroachdb/CockroachDriver";
import {SqlServerDriver} from "../../../src/driver/sqlserver/SqlServerDriver";

describe("schema builder > change column", () => {

Expand Down Expand Up @@ -180,7 +183,7 @@ describe("schema builder > change column", () => {
idColumn.generationStrategy = "uuid";

// depending on driver, we must change column and referenced column types
if (connection.driver.options.type === "postgres" || connection.driver.options.type === "cockroachdb") {
if (connection.driver instanceof PostgresDriver || connection.driver instanceof CockroachDriver) {
idColumn.type = "uuid";
} else if (connection.driver.options.type === "mssql") {
idColumn.type = "uniqueidentifier";
Expand All @@ -193,7 +196,7 @@ describe("schema builder > change column", () => {
const postTable = await queryRunner.getTable("post");
await queryRunner.release();

if (connection.driver.options.type === "postgres" || connection.driver.options.type === "mssql" || connection.driver.options.type === "cockroachdb") {
if (connection.driver instanceof PostgresDriver || connection.driver instanceof CockroachDriver || connection.driver instanceof SqlServerDriver) {
postTable!.findColumnByName("id")!.isGenerated.should.be.true;
postTable!.findColumnByName("id")!.generationStrategy!.should.be.equal("uuid");

Expand Down Expand Up @@ -224,7 +227,7 @@ describe("schema builder > change column", () => {
idColumn.generationStrategy = "uuid";

// depending on driver, we must change column and referenced column types
if (connection.driver.options.type === "postgres" || connection.driver.options.type === "cockroachdb") {
if (connection.driver instanceof PostgresDriver || connection.driver instanceof CockroachDriver) {
idColumn.type = "uuid";
teacherColumn.type = "uuid";
} else if (connection.driver.options.type === "mssql") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {expect} from "chai";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
import {Connection} from "../../../../src";
import {Post} from "./entity/Post";
import {MysqlDriver} from "../../../../src/driver/mysql/MysqlDriver";
import {PostgresDriver} from "../../../../src/driver/postgres/PostgresDriver";

describe("transaction > transaction with load many", () => {

Expand All @@ -19,10 +21,10 @@ describe("transaction > transaction with load many", () => {

const driver = connection.driver;

if (driver.options.type === "mysql") {
if (driver instanceof MysqlDriver) {
const pool = driver.pool;
pool.on("acquire", () => acquireCount++);
} else if (driver.options.type === "postgres") {
} else if (driver instanceof PostgresDriver) {
const pool = driver.master;
pool.on("acquire", () => acquireCount++);
}
Expand Down

0 comments on commit 7bf12a3

Please sign in to comment.