mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
Merge pull request #516 from hajekj14/fix-oracle-limit-offset
fix oracle offset limit
This commit is contained in:
commit
d761f3c795
@ -25,6 +25,7 @@ import {RelationIdMetadataToAttributeTransformer} from "./relation-id/RelationId
|
||||
import {RelationCountLoadResult} from "./relation-count/RelationCountLoadResult";
|
||||
import {RelationCountLoader} from "./relation-count/RelationCountLoader";
|
||||
import {RelationCountMetadataToAttributeTransformer} from "./relation-count/RelationCountMetadataToAttributeTransformer";
|
||||
import {OracleDriver} from "../driver/oracle/OracleDriver";
|
||||
import {Broadcaster} from "../subscriber/Broadcaster";
|
||||
|
||||
|
||||
@ -832,6 +833,7 @@ export class QueryBuilder<Entity> {
|
||||
sql += this.createLimitExpression();
|
||||
sql += this.createOffsetExpression();
|
||||
sql += this.createLockExpression();
|
||||
sql = this.createLimitOffsetSpecificExpression(sql);
|
||||
[sql] = this.connection.driver.escapeQueryWithParameters(sql, this.expressionMap.parameters);
|
||||
return sql.trim();
|
||||
}
|
||||
@ -849,6 +851,7 @@ export class QueryBuilder<Entity> {
|
||||
sql += this.createLimitExpression();
|
||||
sql += this.createOffsetExpression();
|
||||
sql += this.createLockExpression();
|
||||
sql = this.createLimitOffsetSpecificExpression(sql);
|
||||
return sql.trim();
|
||||
}
|
||||
|
||||
@ -866,6 +869,7 @@ export class QueryBuilder<Entity> {
|
||||
sql += this.createLimitExpression();
|
||||
sql += this.createOffsetExpression();
|
||||
sql += this.createLockExpression();
|
||||
sql = this.createLimitOffsetSpecificExpression(sql);
|
||||
return this.connection.driver.escapeQueryWithParameters(sql, this.getParameters());
|
||||
}
|
||||
|
||||
@ -923,9 +927,9 @@ export class QueryBuilder<Entity> {
|
||||
idsQuery += metadata.primaryColumns.map((primaryColumn, index) => {
|
||||
const propertyName = this.escapeAlias(mainAliasName + "_" + primaryColumn.databaseName);
|
||||
if (index === 0) {
|
||||
return `DISTINCT(${distinctAlias}.${propertyName}) as ids_${primaryColumn.databaseName}`;
|
||||
return `DISTINCT(${distinctAlias}.${propertyName}) as "ids_${primaryColumn.databaseName}"`;
|
||||
} else {
|
||||
return `${distinctAlias}.${propertyName}) as ids_${primaryColumn.databaseName}`;
|
||||
return `${distinctAlias}.${propertyName}) as "ids_${primaryColumn.databaseName}"`;
|
||||
}
|
||||
}).join(", ");
|
||||
if (selects.length > 0)
|
||||
@ -1388,6 +1392,9 @@ export class QueryBuilder<Entity> {
|
||||
switch (this.expressionMap.queryType) {
|
||||
case "select":
|
||||
const selection = allSelects.map(select => select.selection + (select.aliasName ? " AS " + ea(select.aliasName) : "")).join(", ");
|
||||
if ((this.expressionMap.limit || this.expressionMap.offset) && this.connection.driver instanceof OracleDriver) {
|
||||
return "SELECT ROWNUM " + this.escapeAlias("RN") + "," + selection + " FROM " + this.escapeTable(tableName) + " " + ea(aliasName) + lock;
|
||||
}
|
||||
return "SELECT " + selection + " FROM " + this.escapeTable(tableName) + " " + ea(aliasName) + lock;
|
||||
case "delete":
|
||||
return "DELETE FROM " + et(tableName);
|
||||
@ -1630,13 +1637,26 @@ export class QueryBuilder<Entity> {
|
||||
return "";
|
||||
}
|
||||
|
||||
protected createLimitOffsetSpecificExpression(sql: string): string {
|
||||
if ((this.expressionMap.offset || this.expressionMap.limit) && this.connection.driver instanceof OracleDriver) {
|
||||
sql = "SELECT * FROM (" + sql + ") WHERE ";
|
||||
if (this.expressionMap.offset) {
|
||||
sql += this.escapeAlias("RN") + " >= " + this.expressionMap.offset;
|
||||
}
|
||||
if (this.expressionMap.limit) {
|
||||
sql += (this.expressionMap.offset ? " AND " : "") + this.escapeAlias("RN") + " <= " + ((this.expressionMap.offset || 0) + this.expressionMap.limit);
|
||||
}
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
|
||||
protected createLimitExpression(): string {
|
||||
if (!this.expressionMap.limit) return "";
|
||||
if (!this.expressionMap.limit || this.connection.driver instanceof OracleDriver) return "";
|
||||
return " LIMIT " + this.expressionMap.limit;
|
||||
}
|
||||
|
||||
protected createOffsetExpression(): string {
|
||||
if (!this.expressionMap.offset) return "";
|
||||
if (!this.expressionMap.offset || this.connection.driver instanceof OracleDriver) return "";
|
||||
return " OFFSET " + this.expressionMap.offset;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user