-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathRoutineDrop.php
More file actions
48 lines (43 loc) · 1.81 KB
/
Copy pathRoutineDrop.php
File metadata and controls
48 lines (43 loc) · 1.81 KB
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
<?php namespace DBDiff\SQLGen\DiffToSQL;
use DBDiff\SQLGen\Dialect\SQLDialectInterface;
/**
* Builds the DROP statement that precedes a routine replacement.
*
* Shared by DropRoutineSQL and AlterRoutineSQL, which previously carried
* identical private copies — so the argument-list handling below would
* otherwise have had to be written twice.
*/
class RoutineDrop {
/**
* @param string $definition Routine DDL, used only to tell FUNCTION from PROCEDURE.
* @param string $name Bare name, or a Postgres signature like `fn(integer,text)`.
*/
public static function build(string $definition, string $name, SQLDialectInterface $dialect): string {
$type = preg_match('/\bPROCEDURE\b/i', $definition) ? 'PROCEDURE' : 'FUNCTION';
[$bare, $args] = self::splitSignature($name);
return "DROP $type IF EXISTS " . $dialect->quote($bare) . $args . ';';
}
/**
* Split `fn(integer,text)` into the name and its argument list.
*
* Postgres keys routines by signature so overloads stay distinct (issue
* #187), and an unqualified DROP is ambiguous once overloads exist:
*
* ERROR: function name "cosine_distance" is not unique
*
* The argument list is appended outside the quoted identifier — quoting
* the whole signature would produce `"fn(integer,text)"`, a single odd
* identifier rather than a call signature.
*
* MySQL has no overloads and passes a bare name, which returns unchanged.
*
* @return array{string, string} [bare name, argument list including parens]
*/
private static function splitSignature(string $name): array {
$pos = strpos($name, '(');
if ($pos === false) {
return [$name, ''];
}
return [substr($name, 0, $pos), substr($name, $pos)];
}
}