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import {QueryResultCacheOptions} from \"./QueryResultCacheOptions\";\nimport {Table} from \"../schema-builder/table/Table\";\nimport {QueryRunner} from \"../query-runner/QueryRunner\";\nimport {Connection} from \"../connection/Connection\";\nimport {SqlServerDriver} from \"../driver/sqlserver/SqlServerDriver\";\nimport {MssqlParameter} from \"../driver/sqlserver/MssqlParameter\";\nimport {ObjectLiteral} from \"../common/ObjectLiteral\";\nimport {OracleDriver} from \"../driver/oracle/OracleDriver\";\nimport {PostgresConnectionOptions} from \"../driver/postgres/PostgresConnectionOptions\";\nimport {SqlServerConnectionOptions} from \"../driver/sqlserver/SqlServerConnectionOptions\";\n\n/**\n * Caches query result into current database, into separate table called \"query-result-cache\".\n */\nexport class DbQueryResultCache implements QueryResultCache {\n\n    // -------------------------------------------------------------------------\n    // Private properties\n    // -------------------------------------------------------------------------\n\n    private queryResultCacheTable: string;\n\n    // -------------------------------------------------------------------------\n    // Constructor\n    // -------------------------------------------------------------------------\n\n    constructor(protected connection: Connection) {\n\n        const options = <SqlServerConnectionOptions|PostgresConnectionOptions>this.connection.driver.options;\n        const cacheOptions = typeof this.connection.options.cache === \"object\" ? this.connection.options.cache : {};    \n        const cacheTableName = cacheOptions.tableName || \"query-result-cache\";\n\n        this.queryResultCacheTable = this.connection.driver.buildTableName(cacheTableName, options.schema, options.database);\n    }\n\n    // -------------------------------------------------------------------------\n    // Public Methods\n    // -------------------------------------------------------------------------\n\n    /**\n     * Creates a connection with given cache provider.\n     */\n    async connect(): Promise<void> {\n    }\n\n    /**\n     * Disconnects with given cache provider.\n     */\n    async disconnect(): Promise<void> {\n    }\n\n    /**\n     * Creates table for storing cache if it does not exist yet.\n     */\n    async synchronize(queryRunner?: QueryRunner): Promise<void> {\n        queryRunner = this.getQueryRunner(queryRunner);\n        const driver = this.connection.driver;\n        const tableExist = await queryRunner.hasTable(this.queryResultCacheTable); // todo: table name should be configurable\n        if (tableExist)\n            return;\n\n        await queryRunner.createTable(new Table(\n            {\n                name: this.queryResultCacheTable,\n                columns: [\n                    {\n                        name: \"id\",\n                        isPrimary: true,\n                        isNullable: false,\n                        type: driver.normalizeType({type: driver.mappedDataTypes.cacheId}),\n                        generationStrategy: \"increment\",\n                        isGenerated: true\n                    },\n                    {\n                        name: \"identifier\",\n                        type: driver.normalizeType({type: driver.mappedDataTypes.cacheIdentifier}),\n                        isNullable: true\n                    },\n                    {\n                        name: \"time\",\n                        type: driver.normalizeType({type: driver.mappedDataTypes.cacheTime}),\n                        isPrimary: false,\n                        isNullable: false\n                    },\n                    {\n                        name: \"duration\",\n                        type: driver.normalizeType({type: driver.mappedDataTypes.cacheDuration}),\n                        isPrimary: false,\n                        isNullable: false\n                    },\n                    {\n                        name: \"query\",\n                        type: driver.normalizeType({type: driver.mappedDataTypes.cacheQuery}),\n                        isPrimary: false,\n                        isNullable: false\n                    },\n                    {\n                        name: \"result\",\n                        type: driver.normalizeType({type: driver.mappedDataTypes.cacheResult}),\n                        isNullable: false\n                    },\n                ]\n            },\n        ));\n    }\n\n    /**\n     * Caches given query result.\n     * Returns cache result if found.\n     * Returns undefined if result is not cached.\n     */\n    getFromCache(options: QueryResultCacheOptions, queryRunner?: QueryRunner): Promise<QueryResultCacheOptions|undefined> {\n        queryRunner = this.getQueryRunner(queryRunner);\n        const qb = this.connection\n            .createQueryBuilder(queryRunner)\n            .select()\n            .from(this.queryResultCacheTable, \"cache\");\n\n        if (options.identifier) {\n            return qb\n                .where(`${qb.escape(\"cache\")}.${qb.escape(\"identifier\")} = :identifier`)\n                .setParameters({ identifier: this.connection.driver instanceof SqlServerDriver ? new MssqlParameter(options.identifier, \"nvarchar\") : options.identifier })\n                .getRawOne();\n\n        } else if (options.query) {\n            if (this.connection.driver instanceof OracleDriver) {\n                return qb\n                    .where(`dbms_lob.compare(${qb.escape(\"cache\")}.${qb.escape(\"query\")}, :query) = 0`, { query: options.query })\n                    .getRawOne();\n            }\n\n            return qb\n                .where(`${qb.escape(\"cache\")}.${qb.escape(\"query\")} = :query`)\n                .setParameters({ query: this.connection.driver instanceof SqlServerDriver ? new MssqlParameter(options.query, \"nvarchar\") : options.query })\n                .getRawOne();\n        }\n\n        return Promise.resolve(undefined);\n    }\n\n    /**\n     * Checks if cache is expired or not.\n     */\n    isExpired(savedCache: QueryResultCacheOptions): boolean {\n        const duration = typeof savedCache.duration === \"string\" ? parseInt(savedCache.duration) : savedCache.duration;\n        return ((typeof savedCache.time === \"string\" ? parseInt(savedCache.time as any) : savedCache.time)! + duration) < new Date().getTime();\n    }\n\n    /**\n     * Stores given query result in the cache.\n     */\n    async storeInCache(options: QueryResultCacheOptions, savedCache: QueryResultCacheOptions|undefined, queryRunner?: QueryRunner): Promise<void> {\n        queryRunner = this.getQueryRunner(queryRunner);\n\n        let insertedValues: ObjectLiteral = options;\n        if (this.connection.driver instanceof SqlServerDriver) { // todo: bad abstraction, re-implement this part, probably better if we create an entity metadata for cache table\n            insertedValues = {\n                identifier: new MssqlParameter(options.identifier, \"nvarchar\"),\n                time: new MssqlParameter(options.time, \"bigint\"),\n                duration: new MssqlParameter(options.duration, \"int\"),\n                query: new MssqlParameter(options.query, \"nvarchar\"),\n                result: new MssqlParameter(options.result, \"nvarchar\"),\n            };\n        }\n\n        if (savedCache && savedCache.identifier) { // if exist then update\n            const qb = queryRunner.manager\n                .createQueryBuilder()\n                .update(this.queryResultCacheTable)\n                .set(insertedValues);\n\n            qb.where(`${qb.escape(\"identifier\")} = :condition`, { condition: insertedValues.identifier });\n            await qb.execute();\n\n        } else if (savedCache && savedCache.query) { // if exist then update\n            const qb = queryRunner.manager\n                .createQueryBuilder()\n                .update(this.queryResultCacheTable)\n                .set(insertedValues);\n\n            if (this.connection.driver instanceof OracleDriver) {\n                qb.where(`dbms_lob.compare(\"query\", :condition) = 0`, { condition: insertedValues.query });\n\n            } else {\n                qb.where(`${qb.escape(\"query\")} = :condition`, { condition: insertedValues.query });\n            }\n\n            await qb.execute();\n\n        } else { // otherwise insert\n            await queryRunner.manager\n                .createQueryBuilder()\n                .insert()\n                .into(this.queryResultCacheTable)\n                .values(insertedValues)\n                .execute();\n        }\n    }\n\n    /**\n     * Clears everything stored in the cache.\n     */\n    async clear(queryRunner: QueryRunner): Promise<void> {\n        return this.getQueryRunner(queryRunner).clearTable(this.queryResultCacheTable);\n    }\n\n    /**\n     * Removes all cached results by given identifiers from cache.\n     */\n    async remove(identifiers: string[], queryRunner?: QueryRunner): Promise<void> {\n        await Promise.all(identifiers.map(identifier => {\n            const qb = this.getQueryRunner(queryRunner).manager.createQueryBuilder();\n            return qb.delete()\n                .from(this.queryResultCacheTable)\n                .where(`${qb.escape(\"identifier\")} = :identifier`, {identifier})\n                .execute();\n        }));\n    }\n\n    // -------------------------------------------------------------------------\n    // Protected Methods\n    // -------------------------------------------------------------------------\n\n    /**\n     * Gets a query runner to work with.\n     */\n    protected getQueryRunner(queryRunner: QueryRunner|undefined): QueryRunner {\n        if (queryRunner)\n            return queryRunner;\n\n        return this.connection.createQueryRunner(\"master\");\n    }\n\n}\n"],"sourceRoot":".."}