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 setPluginField (pluginName: string, field: string, value: string) {
90 const seq = this.getSequelize()
92 const options = { type: QueryTypes.UPDATE }
94 return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options)
97 setPluginVersion (pluginName: string, newVersion: string) {
98 return this.setPluginField(pluginName, 'version', newVersion)
101 setPluginLatestVersion (pluginName: string, newVersion: string) {
102 return this.setPluginField(pluginName, 'latestVersion', newVersion)
105 setActorFollowScores (newScore: number) {
106 const seq = this.getSequelize()
108 const options = { type: QueryTypes.UPDATE }
110 return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)
113 setTokenField (accessToken: string, field: string, value: string) {
114 const seq = this.getSequelize()
116 const options = { type: QueryTypes.UPDATE }
118 return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options)
122 if (!this.sequelize) return
124 await this.sequelize.close()
125 this.sequelize = undefined
128 private getSequelize () {
129 if (this.sequelize) return this.sequelize
131 const dbname = 'peertube_test' + this.server.internalServerNumber
132 const username = 'peertube'
133 const password = 'peertube'
134 const host = 'localhost'
137 this.sequelize = new Sequelize(dbname, username, password, {
144 return this.sequelize