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,YAA+B;QAE/B,MAAM,CAAC,QAAQ,EAAE,SAAS,CAAC,GAAG,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,CAAA;QAC5D,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,KAAK,CACxB,UACI,QAAQ,CAAC,CAAC,CAAC,IAAI,QAAQ,GAAG,CAAC,CAAC,CAAC,IACjC,wBAAwB,IAAI,CAAC,UAAU,CACnC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,GAAG,CAAC,CAAC,CAAC,EAAE,eAAe,CACnD,oBAAoB,YAAY,UAC7B,YAAY,KAAK,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,UACxC,UAAU,SAAS,IAAI,CAC1B,CAAA;QACD,OAAO,GAAG,CAAA;IACd,CAAC;IACS,KAAK,CAAC,iBAAiB,CAAC,SAAiB,EAAE,MAAc;QAC/D,MAAM,CAAC,QAAQ,EAAE,SAAS,CAAC,GAAG,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,CAAA;QAC5D,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,KAAK,CACxB,UACI,QAAQ,CAAC,CAAC,CAAC,IAAI,QAAQ,IAAI,CAAC,CAAC,CAAC,EAClC,GAAG,MAAM,KAAK,SAAS,IAAI,CAC9B,CAAA;QACD,OAAO,GAAG,CAAA;IACd,CAAC;CACJ;AAtJD,4DAsJC","file":"BetterSqlite3QueryRunner.js","sourcesContent":["import { QueryRunnerAlreadyReleasedError } from \"../../error/QueryRunnerAlreadyReleasedError\"\nimport { QueryFailedError } from \"../../error/QueryFailedError\"\nimport { AbstractSqliteQueryRunner } from \"../sqlite-abstract/AbstractSqliteQueryRunner\"\nimport { Broadcaster } from \"../../subscriber/Broadcaster\"\nimport { BetterSqlite3Driver } from \"./BetterSqlite3Driver\"\nimport { QueryResult } from \"../../query-runner/QueryResult\"\n\n/**\n * Runs queries on a single sqlite database connection.\n *\n * Does not support compose primary keys with autoincrement field.\n * todo: need to throw exception for this case.\n */\nexport class BetterSqlite3QueryRunner extends AbstractSqliteQueryRunner {\n /**\n * Database driver used by connection.\n */\n driver: BetterSqlite3Driver\n\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(driver: BetterSqlite3Driver) {\n super()\n this.driver = driver\n this.connection = driver.connection\n this.broadcaster = new Broadcaster(this)\n if (typeof this.driver.options.statementCacheSize === \"number\") {\n this.cacheSize = this.driver.options.statementCacheSize\n } else {\n this.cacheSize = 100\n }\n }\n\n private cacheSize: number\n private stmtCache = new Map()\n\n private async getStmt(query: string) {\n if (this.cacheSize > 0) {\n let stmt = this.stmtCache.get(query)\n if (!stmt) {\n const databaseConnection = await this.connect()\n stmt = databaseConnection.prepare(query)\n this.stmtCache.set(query, stmt)\n while (this.stmtCache.size > this.cacheSize) {\n // since es6 map keeps the insertion order,\n // it comes to be FIFO cache\n const key = this.stmtCache.keys().next().value\n this.stmtCache.delete(key)\n }\n }\n return stmt\n } else {\n const databaseConnection = await this.connect()\n return databaseConnection.prepare(query)\n }\n }\n\n /**\n * Called before migrations are run.\n */\n async beforeMigration(): Promise {\n await this.query(`PRAGMA foreign_keys = OFF`)\n }\n\n /**\n * Called after migrations are run.\n */\n async afterMigration(): Promise {\n await this.query(`PRAGMA foreign_keys = ON`)\n }\n\n /**\n * Executes a given SQL query.\n */\n async query(\n query: string,\n parameters?: any[],\n useStructuredResult = false,\n ): Promise {\n if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()\n\n const connection = this.driver.connection\n\n this.driver.connection.logger.logQuery(query, parameters, this)\n const queryStartTime = +new Date()\n\n const stmt = await this.getStmt(query)\n\n try {\n const result = new QueryResult()\n\n if (stmt.reader) {\n const raw = stmt.all.apply(stmt, parameters)\n\n result.raw = raw\n\n if (Array.isArray(raw)) {\n result.records = raw\n }\n } else {\n const raw = stmt.run.apply(stmt, parameters)\n result.affected = raw.changes\n result.raw = raw.lastInsertRowid\n }\n\n // log slow queries if maxQueryExecution time is set\n const maxQueryExecutionTime =\n this.driver.options.maxQueryExecutionTime\n const queryEndTime = +new Date()\n const queryExecutionTime = queryEndTime - queryStartTime\n if (\n maxQueryExecutionTime &&\n queryExecutionTime > maxQueryExecutionTime\n )\n connection.logger.logQuerySlow(\n queryExecutionTime,\n query,\n parameters,\n this,\n )\n\n if (!useStructuredResult) {\n return result.raw\n }\n\n return result\n } catch (err) {\n connection.logger.logQueryError(err, query, parameters, this)\n throw new QueryFailedError(query, parameters, err)\n }\n }\n\n // -------------------------------------------------------------------------\n // Protected Methods\n // -------------------------------------------------------------------------\n\n protected async loadTableRecords(\n tablePath: string,\n tableOrIndex: \"table\" | \"index\",\n ) {\n const [database, tableName] = this.splitTablePath(tablePath)\n const res = await this.query(\n `SELECT ${\n database ? `'${database}'` : null\n } as database, * FROM ${this.escapePath(\n `${database ? `${database}.` : \"\"}sqlite_master`,\n )} WHERE \"type\" = '${tableOrIndex}' AND \"${\n tableOrIndex === \"table\" ? \"name\" : \"tbl_name\"\n }\" IN ('${tableName}')`,\n )\n return res\n }\n protected async loadPragmaRecords(tablePath: string, pragma: string) {\n const [database, tableName] = this.splitTablePath(tablePath)\n const res = await this.query(\n `PRAGMA ${\n database ? `\"${database}\".` : \"\"\n }${pragma}(\"${tableName}\")`,\n )\n return res\n }\n}\n"],"sourceRoot":"../.."}