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
|
<?php
/**
* PHPPgAdmin v6.0.0-RC7
*/
namespace PHPPgAdmin\Database\Traits;
/**
* 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 = "CREATE AGGREGATE \"{$f_schema}\".\"{$name}\" (BASETYPE = \"{$basetype}\", SFUNC = \"{$sfunc}\", STYPE = \"{$stype}\"";
if (trim($ffunc) != '') {
$sql .= ", FINALFUNC = \"{$ffunc}\"";
}
if (trim($initcond) != '') {
$sql .= ", INITCOND = \"{$initcond}\"";
}
if (trim($sortop) != '') {
$sql .= ", SORTOP = \"{$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 int 0 if operation was successful
*/
public function dropAggregate($aggrname, $aggrtype, $cascade)
{
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($aggrname);
$this->fieldClean($aggrtype);
$sql = "DROP AGGREGATE \"{$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 \PHPPgAdmin\ADORecordSet A recordset
*/
public function getAggregate($name, $basetype)
{
$c_schema = $this->_schema;
$this->clean($c_schema);
$this->fieldClean($name);
$this->fieldClean($basetype);
$sql = "
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='{$c_schema}'
AND p.proname='".$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 \PHPPgAdmin\ADORecordSet A recordset
*/
public function getAggregates()
{
$c_schema = $this->_schema;
$this->clean($c_schema);
$sql = "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='{$c_schema}' ORDER BY 1, 2";
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 ($status != 0) {
$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 ($status != 0) {
$this->rollbackTransaction();
return -3;
}
}
// Rename the aggregate, if it has changed
if ($aggrname != $newaggrname) {
$status = $this->renameAggregate($newaggrschema, $aggrname, $aggrtype, $newaggrname);
if ($status != 0) {
$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 int 0 if operation was successful
*/
public function changeAggregateOwner($aggrname, $aggrtype, $newaggrowner)
{
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($aggrname);
$this->fieldClean($newaggrowner);
$sql = "ALTER AGGREGATE \"{$f_schema}\".\"{$aggrname}\" (\"{$aggrtype}\") OWNER TO \"{$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 int 0 if operation was successful
*/
public function changeAggregateSchema($aggrname, $aggrtype, $newaggrschema)
{
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($aggrname);
$this->fieldClean($newaggrschema);
$sql = "ALTER AGGREGATE \"{$f_schema}\".\"{$aggrname}\" (\"{$aggrtype}\") SET SCHEMA \"{$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 int 0 if operation was successful
*/
public function renameAggregate($aggrschema, $aggrname, $aggrtype, $newaggrname)
{
/* this function is called from alterAggregate where params are cleaned */
$sql = "ALTER AGGREGATE \"{$aggrschema}\"".'.'."\"{$aggrname}\" (\"{$aggrtype}\") RENAME TO \"{$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);
}
|