aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/sql/shared/abstract-videos-model-query-builder.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-10 16:57:13 +0200
committerChocobozzz <me@florianbigard.com>2021-06-11 09:31:59 +0200
commit1d43c3a613c72d69f7360fee9e5bfe6f662d62f7 (patch)
treed4ba891ffdb1182e39620c06feff1503365d66b5 /server/models/video/sql/shared/abstract-videos-model-query-builder.ts
parentd9bf974f5df787bbeaab5b04949ca91a2b3ca2a3 (diff)
downloadPeerTube-1d43c3a613c72d69f7360fee9e5bfe6f662d62f7.tar.gz
PeerTube-1d43c3a613c72d69f7360fee9e5bfe6f662d62f7.tar.zst
PeerTube-1d43c3a613c72d69f7360fee9e5bfe6f662d62f7.zip
Use separate queries for video files
Diffstat (limited to 'server/models/video/sql/shared/abstract-videos-model-query-builder.ts')
-rw-r--r--server/models/video/sql/shared/abstract-videos-model-query-builder.ts65
1 files changed, 49 insertions, 16 deletions
diff --git a/server/models/video/sql/shared/abstract-videos-model-query-builder.ts b/server/models/video/sql/shared/abstract-videos-model-query-builder.ts
index bdf926cbe..8ed207eea 100644
--- a/server/models/video/sql/shared/abstract-videos-model-query-builder.ts
+++ b/server/models/video/sql/shared/abstract-videos-model-query-builder.ts
@@ -1,19 +1,24 @@
1import validator from 'validator'
1import { AbstractVideosQueryBuilder } from './abstract-videos-query-builder' 2import { AbstractVideosQueryBuilder } from './abstract-videos-query-builder'
2import { VideoAttributes } from './video-attributes' 3import { VideoAttributes } from './video-attributes'
3import { VideoModelBuilder } from './video-model-builder' 4
5/**
6 *
7 * Abstract builder to create SQL query and fetch video models
8 *
9 */
4 10
5export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder { 11export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder {
6 protected attributes: { [key: string]: string } = {} 12 protected attributes: { [key: string]: string } = {}
7 protected joins: string[] = [] 13 protected joins: string[] = []
14 protected where: string
8 15
9 protected videoAttributes: VideoAttributes 16 protected videoAttributes: VideoAttributes
10 protected videoModelBuilder: VideoModelBuilder
11 17
12 constructor (private readonly mode: 'list' | 'get') { 18 constructor (protected readonly mode: 'list' | 'get') {
13 super() 19 super()
14 20
15 this.videoAttributes = new VideoAttributes(this.mode) 21 this.videoAttributes = new VideoAttributes(this.mode)
16 this.videoModelBuilder = new VideoModelBuilder(this.mode, this.videoAttributes)
17 } 22 }
18 23
19 protected buildSelect () { 24 protected buildSelect () {
@@ -78,21 +83,30 @@ export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder
78 } 83 }
79 } 84 }
80 85
81 protected includeFiles () { 86 protected includeWebtorrentFiles (required: boolean) {
82 this.joins.push( 87 const joinType = required ? 'INNER' : 'LEFT'
83 'LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"', 88 this.joins.push(joinType + ' JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"')
89
90 this.attributes = {
91 ...this.attributes,
92
93 ...this.buildAttributesObject('VideoFiles', this.videoAttributes.getFileAttributes())
94 }
95 }
96
97 protected includeStreamingPlaylistFiles (required: boolean) {
98 const joinType = required ? 'INNER' : 'LEFT'
84 99
85 'LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"', 100 this.joins.push(
101 joinType + ' JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"',
86 102
87 'LEFT JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' + 103 joinType + ' JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' +
88 'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"' 104 'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"'
89 ) 105 )
90 106
91 this.attributes = { 107 this.attributes = {
92 ...this.attributes, 108 ...this.attributes,
93 109
94 ...this.buildAttributesObject('VideoFiles', this.videoAttributes.getFileAttributes()),
95
96 ...this.buildAttributesObject('VideoStreamingPlaylists', this.videoAttributes.getStreamingPlaylistAttributes()), 110 ...this.buildAttributesObject('VideoStreamingPlaylists', this.videoAttributes.getStreamingPlaylistAttributes()),
97 ...this.buildAttributesObject('VideoStreamingPlaylists->VideoFiles', this.videoAttributes.getFileAttributes()) 111 ...this.buildAttributesObject('VideoStreamingPlaylists->VideoFiles', this.videoAttributes.getFileAttributes())
98 } 112 }
@@ -196,11 +210,8 @@ export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder
196 } 210 }
197 } 211 }
198 212
199 protected includeRedundancies () { 213 protected includeWebTorrentRedundancies () {
200 this.joins.push( 214 this.joins.push(
201 'LEFT OUTER JOIN "videoRedundancy" AS "VideoStreamingPlaylists->RedundancyVideos" ' +
202 'ON "VideoStreamingPlaylists"."id" = "VideoStreamingPlaylists->RedundancyVideos"."videoStreamingPlaylistId"',
203
204 'LEFT OUTER JOIN "videoRedundancy" AS "VideoFiles->RedundancyVideos" ON ' + 215 'LEFT OUTER JOIN "videoRedundancy" AS "VideoFiles->RedundancyVideos" ON ' +
205 '"VideoFiles"."id" = "VideoFiles->RedundancyVideos"."videoFileId"' 216 '"VideoFiles"."id" = "VideoFiles->RedundancyVideos"."videoFileId"'
206 ) 217 )
@@ -208,7 +219,19 @@ export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder
208 this.attributes = { 219 this.attributes = {
209 ...this.attributes, 220 ...this.attributes,
210 221
211 ...this.buildAttributesObject('VideoFiles->RedundancyVideos', this.videoAttributes.getRedundancyAttributes()), 222 ...this.buildAttributesObject('VideoFiles->RedundancyVideos', this.videoAttributes.getRedundancyAttributes())
223 }
224 }
225
226 protected includeStreamingPlaylistRedundancies () {
227 this.joins.push(
228 'LEFT OUTER JOIN "videoRedundancy" AS "VideoStreamingPlaylists->RedundancyVideos" ' +
229 'ON "VideoStreamingPlaylists"."id" = "VideoStreamingPlaylists->RedundancyVideos"."videoStreamingPlaylistId"'
230 )
231
232 this.attributes = {
233 ...this.attributes,
234
212 ...this.buildAttributesObject('VideoStreamingPlaylists->RedundancyVideos', this.videoAttributes.getRedundancyAttributes()) 235 ...this.buildAttributesObject('VideoStreamingPlaylists->RedundancyVideos', this.videoAttributes.getRedundancyAttributes())
213 } 236 }
214 } 237 }
@@ -236,4 +259,14 @@ export class AbstractVideosModelQueryBuilder extends AbstractVideosQueryBuilder
236 259
237 return result 260 return result
238 } 261 }
262
263 protected whereId (id: string | number) {
264 if (validator.isInt('' + id)) {
265 this.where = 'WHERE "video".id = :videoId'
266 } else {
267 this.where = 'WHERE uuid = :videoId'
268 }
269
270 this.replacements.videoId = id
271 }
239} 272}