1 import { QueryTypes, Sequelize } from 'sequelize'
2 import { AbstractCommand } from '../shared'
4 export class SQLCommand extends AbstractCommand {
5 private sequelize: Sequelize
7 deleteAll (table: string) {
8 const seq = this.getSequelize()
10 const options = { type: QueryTypes.DELETE }
12 return seq.query(`DELETE FROM "${table}"`, options)
15 async getCount (table: string) {
16 const seq = this.getSequelize()
18 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
20 const [ { total } ] = await seq.query<{ total: string }>(`SELECT COUNT(*) as total FROM "${table}"`, options)
21 if (total === null) return 0
23 return parseInt(total, 10)
26 async getInternalFileUrl (fileId: number) {
27 return this.selectQuery(`SELECT "fileUrl" FROM "videoFile" WHERE id = ${fileId}`)
28 .then(rows => rows[0].fileUrl as string)
31 setActorField (to: string, field: string, value: string) {
32 const seq = this.getSequelize()
34 const options = { type: QueryTypes.UPDATE }
36 return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
39 setVideoField (uuid: string, field: string, value: string) {
40 const seq = this.getSequelize()
42 const options = { type: QueryTypes.UPDATE }
44 return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
47 setPlaylistField (uuid: string, field: string, value: string) {
48 const seq = this.getSequelize()
50 const options = { type: QueryTypes.UPDATE }
52 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
55 async countVideoViewsOf (uuid: string) {
56 const seq = this.getSequelize()
58 const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
59 `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
61 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
62 const [ { total } ] = await seq.query<{ total: number }>(query, options)
66 return parseInt(total + '', 10)
69 getActorImage (filename: string) {
70 return this.selectQuery(`SELECT * FROM "actorImage" WHERE filename = '${filename}'`)
71 .then(rows => rows[0])
74 selectQuery (query: string) {
75 const seq = this.getSequelize()
76 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
78 return seq.query<any>(query, options)
81 updateQuery (query: string) {
82 const seq = this.getSequelize()
83 const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE }
85 return seq.query(query, options)
88 setPluginField (pluginName: string, field: string, value: string) {
89 const seq = this.getSequelize()
91 const options = { type: QueryTypes.UPDATE }
93 return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options)
96 setPluginVersion (pluginName: string, newVersion: string) {
97 return this.setPluginField(pluginName, 'version', newVersion)
100 setPluginLatestVersion (pluginName: string, newVersion: string) {
101 return this.setPluginField(pluginName, 'latestVersion', newVersion)
104 setActorFollowScores (newScore: number) {
105 const seq = this.getSequelize()
107 const options = { type: QueryTypes.UPDATE }
109 return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)
112 setTokenField (accessToken: string, field: string, value: string) {
113 const seq = this.getSequelize()
115 const options = { type: QueryTypes.UPDATE }
117 return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options)
121 if (!this.sequelize) return
123 await this.sequelize.close()
124 this.sequelize = undefined
127 private getSequelize () {
128 if (this.sequelize) return this.sequelize
130 const dbname = 'peertube_test' + this.server.internalServerNumber
131 const username = 'peertube'
132 const password = 'peertube'
133 const host = 'localhost'
136 this.sequelize = new Sequelize(dbname, username, password, {
143 return this.sequelize