]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/sql/videos-model-list-query-builder.ts
Optimize join build
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / videos-model-list-query-builder.ts
CommitLineData
e5dbd508 1import { Sequelize } from 'sequelize'
d9bf974f 2import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
1d43c3a6 3import { VideoModelBuilder } from './shared/video-model-builder'
e5dbd508
C
4import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder'
5
1d43c3a6
C
6/**
7 *
8 * Build videos list SQL query and create video models
9 *
10 */
11
d9bf974f
C
12export class VideosModelListQueryBuilder extends AbstractVideosModelQueryBuilder {
13 protected attributes: { [key: string]: string }
e5dbd508
C
14
15 private innerQuery: string
16 private innerSort: string
17
1d43c3a6
C
18 private readonly videoModelBuilder: VideoModelBuilder
19
e5dbd508 20 constructor (protected readonly sequelize: Sequelize) {
d9bf974f 21 super('list')
1d43c3a6 22
17bb4538 23 this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
e5dbd508
C
24 }
25
26 queryVideos (options: BuildVideosListQueryOptions) {
27 this.buildInnerQuery(options)
28 this.buildListQueryFromIdsQuery(options)
29
17bb4538 30 return this.runQuery(undefined).then(rows => this.videoModelBuilder.buildVideosFromRows(rows))
e5dbd508
C
31 }
32
33 private buildInnerQuery (options: BuildVideosListQueryOptions) {
34 const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize)
35 const { query, sort, replacements } = idsQueryBuilder.getIdsListQueryAndSort(options)
36
37 this.replacements = replacements
38 this.innerQuery = query
39 this.innerSort = sort
40 }
41
42 private buildListQueryFromIdsQuery (options: BuildVideosListQueryOptions) {
43 this.attributes = {
44 '"video".*': ''
45 }
46
3c79c2ce 47 this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"')
e5dbd508
C
48
49 this.includeChannels()
50 this.includeAccounts()
51 this.includeThumbnails()
52
53 if (options.withFiles) {
1d43c3a6
C
54 this.includeWebtorrentFiles(false)
55 this.includeStreamingPlaylistFiles(false)
e5dbd508
C
56 }
57
58 if (options.user) {
d9bf974f 59 this.includeUserHistory(options.user.id)
e5dbd508
C
60 }
61
62 if (options.videoPlaylistId) {
63 this.includePlaylist(options.videoPlaylistId)
64 }
65
66 const select = this.buildSelect()
67
3c79c2ce 68 this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`
e5dbd508 69 }
e5dbd508 70}