1 import { QueryTypes, Sequelize } from 'sequelize'
2 import { AbstractCommand } from '../shared/abstract-command'
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()
54 const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
55 `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
57 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
58 const [ { total } ] = await seq.query<{ total: number }>(query, options)
62 return parseInt(total + '', 10)
65 getActorImage (filename: string) {
66 return this.selectQuery(`SELECT * FROM "actorImage" WHERE filename = '${filename}'`)
67 .then(rows => rows[0])
70 selectQuery (query: string) {
71 const seq = this.getSequelize()
72 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
74 return seq.query<any>(query, options)
77 updateQuery (query: string) {
78 const seq = this.getSequelize()
79 const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE }
81 return seq.query(query, options)
84 setPluginField (pluginName: string, field: string, value: string) {
85 const seq = this.getSequelize()
87 const options = { type: QueryTypes.UPDATE }
89 return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options)
92 setPluginVersion (pluginName: string, newVersion: string) {
93 return this.setPluginField(pluginName, 'version', newVersion)
96 setPluginLatestVersion (pluginName: string, newVersion: string) {
97 return this.setPluginField(pluginName, 'latestVersion', newVersion)
100 setActorFollowScores (newScore: number) {
101 const seq = this.getSequelize()
103 const options = { type: QueryTypes.UPDATE }
105 return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)
108 setTokenField (accessToken: string, field: string, value: string) {
109 const seq = this.getSequelize()
111 const options = { type: QueryTypes.UPDATE }
113 return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options)
117 if (!this.sequelize) return
119 await this.sequelize.close()
120 this.sequelize = undefined
123 private getSequelize () {
124 if (this.sequelize) return this.sequelize
126 const dbname = 'peertube_test' + this.server.internalServerNumber
127 const username = 'peertube'
128 const password = 'peertube'
129 const host = 'localhost'
132 this.sequelize = new Sequelize(dbname, username, password, {
139 return this.sequelize