aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/miscs/sql-command.ts
blob: 2a3e9e607a47cf81688a9a0377dd403dee2139c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { QueryTypes, Sequelize } from 'sequelize'
import { AbstractCommand } from '../shared'

export class SQLCommand extends AbstractCommand {
  private sequelize: Sequelize

  deleteAll (table: string) {
    const seq = this.getSequelize()

    const options = { type: QueryTypes.DELETE }

    return seq.query(`DELETE FROM "${table}"`, options)
  }

  async getCount (table: string) {
    const seq = this.getSequelize()

    const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }

    const [ { total } ] = await seq.query<{ total: string }>(`SELECT COUNT(*) as total FROM "${table}"`, options)
    if (total === null) return 0

    return parseInt(total, 10)
  }

  setActorField (to: string, field: string, value: string) {
    const seq = this.getSequelize()

    const options = { type: QueryTypes.UPDATE }

    return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
  }

  setVideoField (uuid: string, field: string, value: string) {
    const seq = this.getSequelize()

    const options = { type: QueryTypes.UPDATE }

    return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
  }

  setPlaylistField (uuid: string, field: string, value: string) {
    const seq = this.getSequelize()

    const options = { type: QueryTypes.UPDATE }

    return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
  }

  async countVideoViewsOf (uuid: string) {
    const seq = this.getSequelize()

    // tslint:disable
    const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
      `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`

    const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
    const [ { total } ] = await seq.query<{ total: number }>(query, options)

    if (!total) return 0

    return parseInt(total + '', 10)
  }

  getActorImage (filename: string) {
    return this.selectQuery(`SELECT * FROM "actorImage" WHERE filename = '${filename}'`)
      .then(rows => rows[0])
  }

  selectQuery (query: string) {
    const seq = this.getSequelize()
    const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }

    return seq.query<any>(query, options)
  }

  updateQuery (query: string) {
    const seq = this.getSequelize()
    const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE }

    return seq.query(query, options)
  }

  setPluginField (pluginName: string, field: string, value: string) {
    const seq = this.getSequelize()

    const options = { type: QueryTypes.UPDATE }

    return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options)
  }

  setPluginVersion (pluginName: string, newVersion: string) {
    return this.setPluginField(pluginName, 'version', newVersion)
  }

  setPluginLatestVersion (pluginName: string, newVersion: string) {
    return this.setPluginField(pluginName, 'latestVersion', newVersion)
  }

  setActorFollowScores (newScore: number) {
    const seq = this.getSequelize()

    const options = { type: QueryTypes.UPDATE }

    return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)
  }

  setTokenField (accessToken: string, field: string, value: string) {
    const seq = this.getSequelize()

    const options = { type: QueryTypes.UPDATE }

    return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options)
  }

  async cleanup () {
    if (!this.sequelize) return

    await this.sequelize.close()
    this.sequelize = undefined
  }

  private getSequelize () {
    if (this.sequelize) return this.sequelize

    const dbname = 'peertube_test' + this.server.internalServerNumber
    const username = 'peertube'
    const password = 'peertube'
    const host = 'localhost'
    const port = 5432

    this.sequelize = new Sequelize(dbname, username, password, {
      dialect: 'postgres',
      host,
      port,
      logging: false
    })

    return this.sequelize
  }

}