]>
Commit | Line | Data |
---|---|---|
9293139f | 1 | import { QueryTypes, Sequelize } from 'sequelize' |
4638cd71 | 2 | import { forceNumber } from '@shared/core-utils' |
c55e3d72 | 3 | import { AbstractCommand } from '../shared' |
9293139f C |
4 | |
5 | export class SQLCommand extends AbstractCommand { | |
6 | private sequelize: Sequelize | |
7 | ||
8 | deleteAll (table: string) { | |
9 | const seq = this.getSequelize() | |
10 | ||
11 | const options = { type: QueryTypes.DELETE } | |
12 | ||
13 | return seq.query(`DELETE FROM "${table}"`, options) | |
14 | } | |
15 | ||
16 | async getCount (table: string) { | |
17 | const seq = this.getSequelize() | |
18 | ||
19 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT } | |
20 | ||
21 | const [ { total } ] = await seq.query<{ total: string }>(`SELECT COUNT(*) as total FROM "${table}"`, options) | |
22 | if (total === null) return 0 | |
23 | ||
24 | return parseInt(total, 10) | |
25 | } | |
26 | ||
9ab330b9 C |
27 | async getInternalFileUrl (fileId: number) { |
28 | return this.selectQuery(`SELECT "fileUrl" FROM "videoFile" WHERE id = ${fileId}`) | |
29 | .then(rows => rows[0].fileUrl as string) | |
30 | } | |
31 | ||
9293139f C |
32 | setActorField (to: string, field: string, value: string) { |
33 | const seq = this.getSequelize() | |
34 | ||
35 | const options = { type: QueryTypes.UPDATE } | |
36 | ||
37 | return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options) | |
38 | } | |
39 | ||
40 | setVideoField (uuid: string, field: string, value: string) { | |
41 | const seq = this.getSequelize() | |
42 | ||
43 | const options = { type: QueryTypes.UPDATE } | |
44 | ||
45 | return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) | |
46 | } | |
47 | ||
48 | setPlaylistField (uuid: string, field: string, value: string) { | |
49 | const seq = this.getSequelize() | |
50 | ||
51 | const options = { type: QueryTypes.UPDATE } | |
52 | ||
53 | return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) | |
54 | } | |
55 | ||
56 | async countVideoViewsOf (uuid: string) { | |
57 | const seq = this.getSequelize() | |
58 | ||
9293139f C |
59 | const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' + |
60 | `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'` | |
61 | ||
62 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT } | |
63 | const [ { total } ] = await seq.query<{ total: number }>(query, options) | |
64 | ||
65 | if (!total) return 0 | |
66 | ||
4638cd71 | 67 | return forceNumber(total) |
9293139f C |
68 | } |
69 | ||
70 | getActorImage (filename: string) { | |
71 | return this.selectQuery(`SELECT * FROM "actorImage" WHERE filename = '${filename}'`) | |
72 | .then(rows => rows[0]) | |
73 | } | |
74 | ||
75 | selectQuery (query: string) { | |
76 | const seq = this.getSequelize() | |
77 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT } | |
78 | ||
79 | return seq.query<any>(query, options) | |
80 | } | |
81 | ||
82 | updateQuery (query: string) { | |
83 | const seq = this.getSequelize() | |
84 | const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE } | |
85 | ||
86 | return seq.query(query, options) | |
87 | } | |
88 | ||
89 | setPluginField (pluginName: string, field: string, value: string) { | |
90 | const seq = this.getSequelize() | |
91 | ||
92 | const options = { type: QueryTypes.UPDATE } | |
93 | ||
94 | return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options) | |
95 | } | |
96 | ||
97 | setPluginVersion (pluginName: string, newVersion: string) { | |
98 | return this.setPluginField(pluginName, 'version', newVersion) | |
99 | } | |
100 | ||
101 | setPluginLatestVersion (pluginName: string, newVersion: string) { | |
102 | return this.setPluginField(pluginName, 'latestVersion', newVersion) | |
103 | } | |
104 | ||
105 | setActorFollowScores (newScore: number) { | |
106 | const seq = this.getSequelize() | |
107 | ||
108 | const options = { type: QueryTypes.UPDATE } | |
109 | ||
110 | return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options) | |
111 | } | |
112 | ||
113 | setTokenField (accessToken: string, field: string, value: string) { | |
114 | const seq = this.getSequelize() | |
115 | ||
116 | const options = { type: QueryTypes.UPDATE } | |
117 | ||
118 | return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options) | |
119 | } | |
120 | ||
121 | async cleanup () { | |
122 | if (!this.sequelize) return | |
123 | ||
124 | await this.sequelize.close() | |
125 | this.sequelize = undefined | |
126 | } | |
127 | ||
128 | private getSequelize () { | |
129 | if (this.sequelize) return this.sequelize | |
130 | ||
131 | const dbname = 'peertube_test' + this.server.internalServerNumber | |
132 | const username = 'peertube' | |
133 | const password = 'peertube' | |
134 | const host = 'localhost' | |
135 | const port = 5432 | |
136 | ||
137 | this.sequelize = new Sequelize(dbname, username, password, { | |
138 | dialect: 'postgres', | |
139 | host, | |
140 | port, | |
141 | logging: false | |
142 | }) | |
143 | ||
144 | return this.sequelize | |
145 | } | |
146 | ||
147 | } |