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 setActorField (to: string, field: string, value: string) {
27 const seq = this.getSequelize()
29 const options = { type: QueryTypes.UPDATE }
31 return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
34 setVideoField (uuid: string, field: string, value: string) {
35 const seq = this.getSequelize()
37 const options = { type: QueryTypes.UPDATE }
39 return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
42 setPlaylistField (uuid: string, field: string, value: string) {
43 const seq = this.getSequelize()
45 const options = { type: QueryTypes.UPDATE }
47 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
50 async countVideoViewsOf (uuid: string) {
51 const seq = this.getSequelize()
53 const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
54 `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
56 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
57 const [ { total } ] = await seq.query<{ total: number }>(query, options)
61 return parseInt(total + '', 10)
64 getActorImage (filename: string) {
65 return this.selectQuery(`SELECT * FROM "actorImage" WHERE filename = '${filename}'`)
66 .then(rows => rows[0])
69 selectQuery (query: string) {
70 const seq = this.getSequelize()
71 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
73 return seq.query<any>(query, options)
76 updateQuery (query: string) {
77 const seq = this.getSequelize()
78 const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE }
80 return seq.query(query, options)
83 setPluginField (pluginName: string, field: string, value: string) {
84 const seq = this.getSequelize()
86 const options = { type: QueryTypes.UPDATE }
88 return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options)
91 setPluginVersion (pluginName: string, newVersion: string) {
92 return this.setPluginField(pluginName, 'version', newVersion)
95 setPluginLatestVersion (pluginName: string, newVersion: string) {
96 return this.setPluginField(pluginName, 'latestVersion', newVersion)
99 setActorFollowScores (newScore: number) {
100 const seq = this.getSequelize()
102 const options = { type: QueryTypes.UPDATE }
104 return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)
107 setTokenField (accessToken: string, field: string, value: string) {
108 const seq = this.getSequelize()
110 const options = { type: QueryTypes.UPDATE }
112 return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options)
116 if (!this.sequelize) return
118 await this.sequelize.close()
119 this.sequelize = undefined
122 private getSequelize () {
123 if (this.sequelize) return this.sequelize
125 const dbname = 'peertube_test' + this.server.internalServerNumber
126 const username = 'peertube'
127 const password = 'peertube'
128 const host = 'localhost'
131 this.sequelize = new Sequelize(dbname, username, password, {
138 return this.sequelize