Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AggregateTrait.php « databasetraits « database « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91363f98eaaea0d2b94b815c7834fcf79d33e27a (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php

/**
 * PHPPgAdmin 6.1.3
 */

namespace PHPPgAdmin\Database\Traits;

use PHPPgAdmin\ADORecordSet;

/**
 * Common trait for aggregates manipulation.
 */
trait AggregateTrait
{
    /**
     * Creates a new aggregate in the database.
     *
     * @param string $name     The name of the aggregate
     * @param string $basetype The input data type of the aggregate
     * @param string $sfunc    The name of the state transition function for the aggregate
     * @param string $stype    The data type for the aggregate's state value
     * @param string $ffunc    The name of the final function for the aggregate
     * @param string $initcond The initial setting for the state value
     * @param string $sortop   The sort operator for the aggregate
     * @param string $comment  Aggregate comment
     *
     * @return bool|int 0 success
     */
    public function createAggregate($name, $basetype, $sfunc, $stype, $ffunc, $initcond, $sortop, $comment)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($name);
        $this->fieldClean($basetype);
        $this->fieldClean($sfunc);
        $this->fieldClean($stype);
        $this->fieldClean($ffunc);
        $this->fieldClean($initcond);
        $this->fieldClean($sortop);

        $this->beginTransaction();

        $sql = \sprintf(
            'CREATE AGGREGATE "%s"."%s" (BASETYPE = "%s", SFUNC = "%s", STYPE = "%s"',
            $f_schema,
            $name,
            $basetype,
            $sfunc,
            $stype
        );

        if ('' !== \trim($ffunc)) {
            $sql .= \sprintf(
                ', FINALFUNC = "%s"',
                $ffunc
            );
        }

        if ('' !== \trim($initcond)) {
            $sql .= \sprintf(
                ', INITCOND = "%s"',
                $initcond
            );
        }

        if ('' !== \trim($sortop)) {
            $sql .= \sprintf(
                ', SORTOP = "%s"',
                $sortop
            );
        }

        $sql .= ')';

        $status = $this->execute($sql);

        if ($status) {
            $this->rollbackTransaction();

            return -1;
        }

        if ('' !== \trim($comment)) {
            $status = $this->setComment('AGGREGATE', $name, '', $comment, $basetype);

            if ($status) {
                $this->rollbackTransaction();

                return -1;
            }
        }

        return $this->endTransaction();
    }

    /**
     * Removes an aggregate function from the database.
     *
     * @param string $aggrname The name of the aggregate
     * @param string $aggrtype The input data type of the aggregate
     * @param bool   $cascade  True to cascade drop, false to restrict
     *
     * @return ADORecordSet|int
     */
    public function dropAggregate($aggrname, $aggrtype, $cascade)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($aggrname);
        $this->fieldClean($aggrtype);

        $sql = \sprintf(
            'DROP AGGREGATE "%s"."%s" ("%s")',
            $f_schema,
            $aggrname,
            $aggrtype
        );

        if ($cascade) {
            $sql .= ' CASCADE';
        }

        return $this->execute($sql);
    }

    /**
     * Gets all information for an aggregate.
     *
     * @param string $name     The name of the aggregate
     * @param string $basetype The input data type of the aggregate
     *
     * @return ADORecordSet|int
     */
    public function getAggregate($name, $basetype)
    {
        $c_schema = $this->_schema;
        $this->clean($c_schema);
        $this->fieldClean($name);
        $this->fieldClean($basetype);

        $sql = \sprintf(
            '
            SELECT p.proname, CASE p.proargtypes[0]
                WHEN \'pg_catalog."any"\'::pg_catalog.regtype THEN NULL
                ELSE pg_catalog.format_type(p.proargtypes[0], NULL) END AS proargtypes,
                a.aggtransfn, format_type(a.aggtranstype, NULL) AS aggstype, a.aggfinalfn,
                a.agginitval, a.aggsortop, u.usename, pg_catalog.obj_description(p.oid, \'pg_proc\') AS aggrcomment
            FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n, pg_catalog.pg_user u, pg_catalog.pg_aggregate a
            WHERE n.oid = p.pronamespace AND p.proowner=u.usesysid AND p.oid=a.aggfnoid
                AND p.proisagg AND n.nspname=\'%s\'
                AND p.proname=\'',
            $c_schema
        ) . $name . "'
                AND CASE p.proargtypes[0]
                    WHEN 'pg_catalog.\"any\"'::pg_catalog.regtype THEN ''
                    ELSE pg_catalog.format_type(p.proargtypes[0], NULL)
                END ='" . $basetype . "'";

        return $this->selectSet($sql);
    }

    /**
     * Gets all aggregates.
     *
     * @return ADORecordSet|int
     */
    public function getAggregates()
    {
        $c_schema = $this->_schema;
        $this->clean($c_schema);
        $sql = \sprintf(
            'SELECT p.proname, CASE p.proargtypes[0] WHEN \'pg_catalog."any"\'::pg_catalog.regtype THEN NULL ELSE
               pg_catalog.format_type(p.proargtypes[0], NULL) END AS proargtypes, a.aggtransfn, u.usename,
               pg_catalog.obj_description(p.oid, \'pg_proc\') AS aggrcomment
               FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n, pg_catalog.pg_user u, pg_catalog.pg_aggregate a
               WHERE n.oid = p.pronamespace AND p.proowner=u.usesysid AND p.oid=a.aggfnoid
               AND p.proisagg AND n.nspname=\'%s\' ORDER BY 1, 2',
            $c_schema
        );

        return $this->selectSet($sql);
    }

    /**
     * Alters an aggregate.
     *
     * @param string $aggrname       The actual name of the aggregate
     * @param string $aggrtype       The actual input data type of the aggregate
     * @param string $aggrowner      The actual owner of the aggregate
     * @param string $aggrschema     The actual schema the aggregate belongs to
     * @param string $aggrcomment    The actual comment for the aggregate
     * @param string $newaggrname    The new name of the aggregate
     * @param string $newaggrowner   The new owner of the aggregate
     * @param string $newaggrschema  The new schema where the aggregate will belong to
     * @param string $newaggrcomment The new comment for the aggregate
     *
     * @return bool|int 0 success
     */
    public function alterAggregate(
        $aggrname,
        $aggrtype,
        $aggrowner,
        $aggrschema,
        $aggrcomment,
        $newaggrname,
        $newaggrowner,
        $newaggrschema,
        $newaggrcomment
    ) {
        // Clean fields
        $this->fieldClean($aggrname);
        $this->fieldClean($aggrtype);
        $this->fieldClean($aggrowner);
        $this->fieldClean($aggrschema);
        $this->fieldClean($newaggrname);
        $this->fieldClean($newaggrowner);
        $this->fieldClean($newaggrschema);

        $this->beginTransaction();

        // Change the owner, if it has changed
        if ($aggrowner !== $newaggrowner) {
            $status = $this->changeAggregateOwner($aggrname, $aggrtype, $newaggrowner);

            if (0 !== $status) {
                $this->rollbackTransaction();

                return -1;
            }
        }

        // Set the comment, if it has changed
        if ($aggrcomment !== $newaggrcomment) {
            $status = $this->setComment('AGGREGATE', $aggrname, '', $newaggrcomment, $aggrtype);

            if ($status) {
                $this->rollbackTransaction();

                return -2;
            }
        }

        // Change the schema, if it has changed
        if ($aggrschema !== $newaggrschema) {
            $status = $this->changeAggregateSchema($aggrname, $aggrtype, $newaggrschema);

            if (0 !== $status) {
                $this->rollbackTransaction();

                return -3;
            }
        }

        // Rename the aggregate, if it has changed
        if ($aggrname !== $newaggrname) {
            $status = $this->renameAggregate($newaggrschema, $aggrname, $aggrtype, $newaggrname);

            if (0 !== $status) {
                $this->rollbackTransaction();

                return -4;
            }
        }

        return $this->endTransaction();
    }

    /**
     * Changes the owner of an aggregate function.
     *
     * @param string $aggrname     The name of the aggregate
     * @param string $aggrtype     The input data type of the aggregate
     * @param string $newaggrowner The new owner of the aggregate
     *
     * @return ADORecordSet|int
     */
    public function changeAggregateOwner($aggrname, $aggrtype, $newaggrowner)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($aggrname);
        $this->fieldClean($newaggrowner);
        $sql = \sprintf(
            'ALTER AGGREGATE "%s"."%s" ("%s") OWNER TO "%s"',
            $f_schema,
            $aggrname,
            $aggrtype,
            $newaggrowner
        );

        return $this->execute($sql);
    }

    /**
     * Changes the schema of an aggregate function.
     *
     * @param string $aggrname      The name of the aggregate
     * @param string $aggrtype      The input data type of the aggregate
     * @param string $newaggrschema The new schema for the aggregate
     *
     * @return ADORecordSet|int
     */
    public function changeAggregateSchema($aggrname, $aggrtype, $newaggrschema)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($aggrname);
        $this->fieldClean($newaggrschema);
        $sql = \sprintf(
            'ALTER AGGREGATE "%s"."%s" ("%s") SET SCHEMA  "%s"',
            $f_schema,
            $aggrname,
            $aggrtype,
            $newaggrschema
        );

        return $this->execute($sql);
    }

    /**
     * Renames an aggregate function.
     *
     * @param string $aggrschema  The schema of the aggregate
     * @param string $aggrname    The actual name of the aggregate
     * @param string $aggrtype    The actual input data type of the aggregate
     * @param string $newaggrname The new name of the aggregate
     *
     * @return ADORecordSet|int
     */
    public function renameAggregate($aggrschema, $aggrname, $aggrtype, $newaggrname)
    {
        /* this function is called from alterAggregate where params are cleaned */
        $sql = \sprintf(
            'ALTER AGGREGATE "%s"',
            $aggrschema
        ) . '.' . \sprintf(
            '"%s" ("%s") RENAME TO "%s"',
            $aggrname,
            $aggrtype,
            $newaggrname
        );

        return $this->execute($sql);
    }

    abstract public function fieldClean(&$str);

    abstract public function beginTransaction();

    abstract public function rollbackTransaction();

    abstract public function endTransaction();

    abstract public function execute($sql);

    abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null);

    abstract public function selectSet($sql);

    abstract public function clean(&$str);
}