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