Skip to content

Commit 4234c68

Browse files
committed
Add error code generator
1 parent 4483756 commit 4234c68

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
/.travis.yml export-ignore
55
/phpunit.xml export-ignore
66
/tests export-ignore
7+
/tools export-ignore
8+
/Makefile export-ignore

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
SOURCE := https://raw.githubusercontent.com/postgres/postgres/master/src/backend/utils/errcodes.txt
2+
TARGET := $(CURDIR)/src/PostgresError.php
3+
GENERATOR := php $(CURDIR)/tools/generator.php
4+
5+
all: clean $(TARGET)
6+
7+
$(TARGET):
8+
curl -s $(SOURCE) | $(GENERATOR) > $@
9+
10+
clean:
11+
-$(RM) $(TARGET)
12+
13+
.PHONY: all clean

tools/generator.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
$codes = array();
4+
$const = array();
5+
6+
while ($row = fgets(STDIN)) {
7+
if (preg_match('/^([0-9A-Z]{5}) +[SWE] +[^ ]+? +([a-z_]+)$/', $row, $matches)) {
8+
$key = strtoupper($matches[2]);
9+
if (isset($codes[$key])) {
10+
$key .= '_EXCEPTION';
11+
}
12+
$codes[$key] = '';
13+
$const[] = " const {$key} = '{$matches[1]}';";
14+
}
15+
}
16+
17+
$const = implode(PHP_EOL, $const);
18+
19+
echo <<<EOD
20+
<?php
21+
22+
namespace Sunaoka\PostgresError;
23+
24+
class PostgresError
25+
{
26+
$const
27+
}
28+
29+
EOD;

0 commit comments

Comments
 (0)