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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
<?php
/**
* PHPPgAdmin v6.0.0-RC2
*/
namespace PHPPgAdmin\Database\Traits;
/**
* Common trait for full text search manipulation.
*/
trait FunctionTrait
{
/**
* Returns a list of all functions in the database.
*
* @param bool $all If true, will find all available functions, if false just those in search path
* @param mixed $type If truthy, will return functions of type trigger
*
* @return \PHPPgAdmin\ADORecordSet All functions
*/
public function getFunctions($all = false, $type = null)
{
if ($all) {
$where = 'pg_catalog.pg_function_is_visible(p.oid)';
$distinct = 'DISTINCT ON (p.proname)';
if ($type) {
$where .= " AND p.prorettype = (select oid from pg_catalog.pg_type p where p.typname = 'trigger') ";
}
} else {
$c_schema = $this->_schema;
$this->clean($c_schema);
$where = "n.nspname = '{$c_schema}'";
$distinct = '';
}
$sql = "
SELECT
{$distinct}
p.oid AS prooid,
p.proname,
p.proretset,
pg_catalog.format_type(p.prorettype, NULL) AS proresult,
pg_catalog.oidvectortypes(p.proargtypes) AS proarguments,
pl.lanname AS prolanguage,
pg_catalog.obj_description(p.oid, 'pg_proc') AS procomment,
p.proname || ' (' || pg_catalog.oidvectortypes(p.proargtypes) || ')' AS proproto,
CASE WHEN p.proretset THEN 'setof ' ELSE '' END || pg_catalog.format_type(p.prorettype, NULL) AS proreturns,
coalesce(u.usename::text,p.proowner::text) AS proowner
FROM pg_catalog.pg_proc p
INNER JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
INNER JOIN pg_catalog.pg_language pl ON pl.oid = p.prolang
LEFT JOIN pg_catalog.pg_user u ON u.usesysid = p.proowner
WHERE NOT p.proisagg
AND {$where}
ORDER BY p.proname, proresult
";
return $this->selectSet($sql);
}
/**
* Returns a list of all functions that can be used in triggers.
*
* @return \PHPPgAdmin\ADORecordSet Functions that can be used in a trigger
*/
public function getTriggerFunctions()
{
return $this->getFunctions(true, 'trigger');
}
/**
* Returns an array containing a function's properties.
*
* @param array $f The array of data for the function
*
* @return array|int An array containing the properties, or -1 in case of error
*/
public function getFunctionProperties($f)
{
$temp = [];
// Volatility
if ($f['provolatile'] == 'v') {
$temp[] = 'VOLATILE';
} elseif ($f['provolatile'] == 'i') {
$temp[] = 'IMMUTABLE';
} elseif ($f['provolatile'] == 's') {
$temp[] = 'STABLE';
} else {
return -1;
}
// Null handling
$f['proisstrict'] = $this->phpBool($f['proisstrict']);
if ($f['proisstrict']) {
$temp[] = 'RETURNS NULL ON NULL INPUT';
} else {
$temp[] = 'CALLED ON NULL INPUT';
}
// Security
$f['prosecdef'] = $this->phpBool($f['prosecdef']);
if ($f['prosecdef']) {
$temp[] = 'SECURITY DEFINER';
} else {
$temp[] = 'SECURITY INVOKER';
}
return $temp;
}
/**
* Updates (replaces) a function.
*
* @param string $funcname The name of the function to create
* @param string $newname The new name for the function
* @param string $args imploded array of argument types
* @param string $returns The return type
* @param string $definition The definition for the new function
* @param string $language The language the function is written for
* @param array $flags An array of optional flags
* @param bool $setof True if returns a set, false otherwise
* @param string $funcown
* @param string $newown
* @param string $funcschema
* @param string $newschema
* @param float $cost
* @param int $rows
* @param string $comment The comment on the function
*
* @return bool|int 0 success
*/
public function setFunction(
$funcname,
$newname,
$args,
$returns,
$definition,
$language,
$flags,
$setof,
$funcown,
$newown,
$funcschema,
$newschema,
$cost,
$rows,
$comment
) {
// Begin a transaction
$status = $this->beginTransaction();
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
// Replace the existing function
$status = $this->createFunction($funcname, $args, $returns, $definition, $language, $flags, $setof, $cost, $rows, $comment, true);
if ($status != 0) {
$this->rollbackTransaction();
return $status;
}
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
// Rename the function, if necessary
$this->fieldClean($newname);
/* $funcname is escaped in createFunction */
if ($funcname != $newname) {
$sql = "ALTER FUNCTION \"{$f_schema}\".\"{$funcname}\"({$args}) RENAME TO \"{$newname}\"";
$status = $this->execute($sql);
if ($status != 0) {
$this->rollbackTransaction();
return -5;
}
$funcname = $newname;
}
// Alter the owner, if necessary
if ($this->hasFunctionAlterOwner()) {
$this->fieldClean($newown);
if ($funcown != $newown) {
$sql = "ALTER FUNCTION \"{$f_schema}\".\"{$funcname}\"({$args}) OWNER TO \"{$newown}\"";
$status = $this->execute($sql);
if ($status != 0) {
$this->rollbackTransaction();
return -6;
}
}
}
// Alter the schema, if necessary
if ($this->hasFunctionAlterSchema()) {
$this->fieldClean($newschema);
/* $funcschema is escaped in createFunction */
if ($funcschema != $newschema) {
$sql = "ALTER FUNCTION \"{$f_schema}\".\"{$funcname}\"({$args}) SET SCHEMA \"{$newschema}\"";
$status = $this->execute($sql);
if ($status != 0) {
$this->rollbackTransaction();
return -7;
}
}
}
return $this->endTransaction();
}
/**
* Creates a new function.
*
* @param string $funcname The name of the function to create
* @param string $args A comma separated string of types
* @param string $returns The return type
* @param string $definition The definition for the new function
* @param string $language The language the function is written for
* @param array $flags An array of optional flags
* @param bool $setof True if it returns a set, false otherwise
* @param string $cost cost the planner should use in the function execution step
* @param int $rows number of rows planner should estimate will be returned
* @param string $comment Comment for the function
* @param bool $replace (optional) True if OR REPLACE, false for
* normal
*
* @return bool|int 0 success
*/
public function createFunction($funcname, $args, $returns, $definition, $language, $flags, $setof, $cost, $rows, $comment, $replace = false)
{
// Begin a transaction
$status = $this->beginTransaction();
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
$this->fieldClean($funcname);
$this->clean($args);
$this->fieldClean($language);
$this->arrayClean($flags);
$this->clean($cost);
$this->clean($rows);
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$sql = 'CREATE';
if ($replace) {
$sql .= ' OR REPLACE';
}
$sql .= " FUNCTION \"{$f_schema}\".\"{$funcname}\" (";
if ($args != '') {
$sql .= $args;
}
// For some reason, the returns field cannot have quotes...
$sql .= ') RETURNS ';
if ($setof) {
$sql .= 'SETOF ';
}
$sql .= "{$returns} AS ";
if (is_array($definition)) {
$this->arrayClean($definition);
$sql .= "'".$definition[0]."'";
if ($definition[1]) {
$sql .= ",'".$definition[1]."'";
}
} else {
$this->clean($definition);
$sql .= "'".$definition."'";
}
$sql .= " LANGUAGE \"{$language}\"";
// Add costs
if (!empty($cost)) {
$sql .= " COST {$cost}";
}
if ($rows != 0) {
$sql .= " ROWS {$rows}";
}
// Add flags
foreach ($flags as $v) {
// Skip default flags
if ($v == '') {
continue;
}
$sql .= "\n{$v}";
}
$status = $this->execute($sql);
if ($status != 0) {
$this->rollbackTransaction();
return -3;
}
/* set the comment */
$status = $this->setComment('FUNCTION', "\"{$funcname}\"({$args})", null, $comment);
if ($status != 0) {
$this->rollbackTransaction();
return -4;
}
return $this->endTransaction();
}
/**
* Drops a function.
*
* @param int $function_oid The OID of the function to drop
* @param bool $cascade True to cascade drop, false to restrict
*
* @return int 0 if operation was successful
*/
public function dropFunction($function_oid, $cascade)
{
// Function comes in with $object as function OID
$fn = $this->getFunction($function_oid);
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($fn->fields['proname']);
$sql = "DROP FUNCTION \"{$f_schema}\".\"{$fn->fields['proname']}\"({$fn->fields['proarguments']})";
if ($cascade) {
$sql .= ' CASCADE';
}
return $this->execute($sql);
}
/**
* Returns all details for a particular function.
*
* @param int $function_oid
*
* @return \PHPPgAdmin\ADORecordSet Function info
*
* @internal param string The $func name of the function to retrieve
*/
public function getFunction($function_oid)
{
$this->clean($function_oid);
$sql = "
SELECT
pc.oid AS prooid, proname,
pg_catalog.pg_get_userbyid(proowner) AS proowner,
nspname as proschema, lanname as prolanguage, procost, prorows,
pg_catalog.format_type(prorettype, NULL) as proresult, prosrc,
probin, proretset, proisstrict, provolatile, prosecdef,
pg_catalog.oidvectortypes(pc.proargtypes) AS proarguments,
proargnames AS proargnames,
pg_catalog.obj_description(pc.oid, 'pg_proc') AS procomment,
proconfig,
(select array_agg( (select typname from pg_type pt
where pt.oid = p.oid) ) from unnest(proallargtypes) p)
AS proallarguments,
proargmodes
FROM
pg_catalog.pg_proc pc, pg_catalog.pg_language pl,
pg_catalog.pg_namespace pn
WHERE
pc.oid = '{$function_oid}'::oid AND pc.prolang = pl.oid
AND pc.pronamespace = pn.oid
";
return $this->selectSet($sql);
}
/**
* Returns plain definition for a particular function.
*
* @param int $function_oid
*
* @return \PHPPgAdmin\ADORecordSet Function definition
*/
public function getFunctionDef($function_oid)
{
$this->clean($function_oid);
$sql = "
SELECT
f.proname as relname,
n.nspname,
u.usename AS relowner,
pg_catalog.obj_description(f.oid, 'pg_proc') as relcomment,
(SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=f.pronamespace) AS tablespace,
pg_get_functiondef(f.oid),
pl.lanname AS prolanguage
FROM pg_catalog.pg_proc f
JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid)
JOIN pg_catalog.pg_language pl ON pl.oid = f.prolang
LEFT JOIN pg_catalog.pg_user u ON u.usesysid=f.proowner
WHERE f.oid='{$function_oid}'
";
return $this->selectSet($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);
abstract public function phpBool($parameter);
abstract public function hasFunctionAlterOwner();
abstract public function hasFunctionAlterSchema();
abstract public function arrayClean(&$arr);
}
|