-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimized version of
range
and page
for postgresql using window f…
…unction. closes #62
- Loading branch information
Showing
14 changed files
with
174 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import QueryBuilderOperation from '../QueryBuilderOperation'; | ||
|
||
export default class RangeOperation extends QueryBuilderOperation { | ||
|
||
constructor(name, opt) { | ||
super(name, opt); | ||
this.resultSizePromise = null; | ||
} | ||
|
||
call(builder, args) { | ||
if (args.length === 2) { | ||
const start = args[0]; | ||
const end = args[1]; | ||
|
||
// Need to set these here instead of `onBuild` so that they | ||
// don't end up in the resultSize query. | ||
builder.limit(end - start + 1).offset(start); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
onBefore(builder) { | ||
// Don't return the promise so that it is executed | ||
// in parallel with the actual query. | ||
this.resultSizePromise = builder.resultSize(); | ||
return null; | ||
} | ||
|
||
onAfterInternal(builder, result) { | ||
return this.resultSizePromise.then(count => { | ||
return { | ||
results: result, | ||
total: parseInt(count, 10) | ||
}; | ||
}); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/queryBuilder/operations/range/RangePostgresOperation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import RangeOperation from './RangeOperation' | ||
|
||
const totalCountCol = 'objectiontmptotalcount'; | ||
|
||
export default class RangePostgresOperation extends RangeOperation { | ||
|
||
constructor(name, opt) { | ||
super(name, opt); | ||
this.count = 0; | ||
} | ||
|
||
onBefore(knexBuilder, builder) { | ||
// Do nothing. | ||
} | ||
|
||
onBuild(knexBuilder, builder) { | ||
const knex = builder.knex(); | ||
knexBuilder.select(knex.raw(`count(*) over () as ${totalCountCol}`)); | ||
} | ||
|
||
onRawResult(builder, rows) { | ||
if (Array.isArray(rows) && rows.length && typeof rows[0] === 'object') { | ||
this.count = rows[0][totalCountCol]; | ||
|
||
for (let i = 0, l = rows.length; i < l; ++i) { | ||
delete rows[i][totalCountCol]; | ||
} | ||
} else if (rows && typeof rows === 'object') { | ||
this.count = rows[totalCountCol]; | ||
delete rows[totalCountCol]; | ||
} | ||
|
||
return rows; | ||
} | ||
|
||
onAfterInternal(builder, result) { | ||
return { | ||
results: result, | ||
total: parseInt(this.count, 10) | ||
}; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...r/operations/WhereInCompositeOperation.js → ...eInComposite/WhereInCompositeOperation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ations/WhereInCompositeSqliteOperation.js → ...posite/WhereInCompositeSqliteOperation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters