1 import { QueryTypes, Sequelize } from 'sequelize'
2 import { forceNumber } from '@shared/core-utils'
3 import { AbstractCommand } from '../shared'
5 export class SQLCommand extends AbstractCommand {
6 private sequelize: Sequelize
8 deleteAll (table: string) {
9 const seq = this.getSequelize()
11 const options = { type: QueryTypes.DELETE }
13 return seq.query(`DELETE FROM "${table}"`, options)
16 async getCount (table: string) {
17 const seq = this.getSequelize()
19 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
21 const [ { total } ] = await seq.query<{ total: string }>(`SELECT COUNT(*) as total FROM "${table}"`, options)
22 if (total === null) return 0
24 return parseInt(total, 10)
27 async getInternalFileUrl (fileId: number) {
28 return this.selectQuery(`SELECT "fileUrl" FROM "videoFile" WHERE id = ${fileId}`)
29 .then(rows => rows[0].fileUrl as string)
32 setActorField (to: string, field: string, value: string) {
33 const seq = this.getSequelize()
35 const options = { type: QueryTypes.UPDATE }
37 return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
40 setVideoField (uuid: string, field: string, value: string) {
41 const seq = this.getSequelize()
43 const options = { type: QueryTypes.UPDATE }
45 return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
48 setPlaylistField (uuid: string, field: string, value: string) {
49 const seq = this.getSequelize()
51 const options = { type: QueryTypes.UPDATE }
53 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
56 async countVideoViewsOf (uuid: string) {
57 const seq = this.getSequelize()
59 const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
60 `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
62 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
63 const [ { total } ] = await seq.query<{ total: number }>(query, options)
67 return forceNumber(total)
70 getActorImage (filename: string) {
71 return this.selectQuery(`SELECT * FROM "actorImage" WHERE filename = '${filename}'`)
72 .then(rows => rows[0])
75 selectQuery (query: string) {
76 const seq = this.getSequelize()
77 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
79 return seq.query<any>(query, options)
82 updateQuery (query: string) {
83 const seq = this.getSequelize()
84 const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE }
86 return seq.query(query, options)
89 // ---------------------------------------------------------------------------
91 setPluginField (pluginName: string, field: string, value: string) {
92 const seq = this.getSequelize()
94 const options = { type: QueryTypes.UPDATE }
96 return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options)
99 setPluginVersion (pluginName: string, newVersion: string) {
100 return this.setPluginField(pluginName, 'version', newVersion)
103 setPluginLatestVersion (pluginName: string, newVersion: string) {
104 return this.setPluginField(pluginName, 'latestVersion', newVersion)
107 // ---------------------------------------------------------------------------
109 async getPlaylistInfohash (playlistId: number) {
110 const result = await this.selectQuery('SELECT "p2pMediaLoaderInfohashes" FROM "videoStreamingPlaylist" WHERE id = ' + playlistId)
111 if (!result || result.length === 0) return []
113 return result[0].p2pMediaLoaderInfohashes
116 // ---------------------------------------------------------------------------
118 setActorFollowScores (newScore: number) {
119 const seq = this.getSequelize()
121 const options = { type: QueryTypes.UPDATE }
123 return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)
126 setTokenField (accessToken: string, field: string, value: string) {
127 const seq = this.getSequelize()
129 const options = { type: QueryTypes.UPDATE }
131 return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options)
135 if (!this.sequelize) return
137 await this.sequelize.close()
138 this.sequelize = undefined
141 private getSequelize () {
142 if (this.sequelize) return this.sequelize
144 const dbname = 'peertube_test' + this.server.internalServerNumber
145 const username = 'peertube'
146 const password = 'peertube'
147 const host = '127.0.0.1'
150 this.sequelize = new Sequelize(dbname, username, password, {
157 return this.sequelize