release 2.1.6
[m6w6/ext-pq] / scripts / php_pq_type-pg11.php
1 #!/usr/bin/env php
2 <?php
3
4 /*
5 * Generate php_pq_type.h from
6 * postgresql.git/src/include/catalog/pg_type.dat
7 *
8 * Since PgSQL-11
9 */
10
11 $dir = $argv[1] ?? __DIR__."/../../postgresql.git";
12 $dat = file_get_contents($dir . "/src/include/catalog/pg_type.dat");
13 $typ = [];
14 $arr = [];
15
16 if (!$dat) {
17 exit(1);
18 }
19 if (!preg_match_all('/{(.*?)}/s', $dat, $matches)) {
20 fprintf(STDERR, "Failed to find entries in pg_type.dat\n");
21 exit(1);
22 }
23
24 foreach ($matches[1] as $set_data) {
25 if (!preg_match_all('/(?P<key>\w+) => \'(?P<val>(?:[^\']|(?<=\\\\)\')+)\'/', $set_data, $set_matches)) {
26 fprintf(STDERR, "Failed matching key value pairs in set: '%s'\n", $set_data);
27 continue;
28 }
29 $set = array_combine($set_matches["key"], $set_matches["val"]);
30 $ucn = strtoupper($set["typname"]);
31 $typ[$set["oid"]] = $ucn;
32
33 if (isset($set["array_type_oid"])) {
34 $arr[$set["array_type_oid"]] = $set["oid"];
35 $typ[$set["array_type_oid"]] = $ucn . "ARRAY";
36 }
37 if (isset($set["typdelim"])) {
38 $delims[$set["oid"]] = $delims[$set["array_type_oid"]] = $set["typdelim"];
39 }
40 }
41
42 ksort($typ, SORT_NUMERIC);
43 ksort($arr, SORT_NUMERIC);
44 ?>
45
46 /* Generated file. See scripts/gen_pq_type-pq11.php */
47
48 #ifndef PHP_PQ_TYPE
49 # define PHP_PQ_TYPE(t,o)
50 #endif
51
52 <?php foreach ($typ as $oid => $ucn) : ?>
53 #ifndef PHP_PQ_OID_<?=$ucn?>
54
55 # define PHP_PQ_OID_<?=$ucn?> <?=$oid?>
56
57 #endif
58 PHP_PQ_TYPE("<?=$ucn?>", <?=$oid?>)
59 <?php endforeach; ?>
60
61 #ifndef PHP_PQ_TYPE_IS_ARRAY
62 # define PHP_PQ_TYPE_IS_ARRAY(oid) ( \
63 0 \
64 <?php foreach ($arr as $oid => $type) : ?>
65 || ((oid) == <?=$oid?>) \
66 <?php endforeach; ?>
67 )
68 #endif
69
70 #ifndef PHP_PQ_TYPE_OF_ARRAY
71 # define PHP_PQ_TYPE_OF_ARRAY(oid) ( \
72 <?php foreach ($arr as $oid => $type) : ?>
73 (oid) == <?=$oid?> ? <?=$type?> : \
74 <?php endforeach; ?>
75 0 \
76 )
77 #endif
78
79 #ifndef PHP_PQ_DELIM_OF_ARRAY
80 # define PHP_PQ_DELIM_OF_ARRAY(oid) ((char) ( \
81 <?php foreach ($delims as $oid => $delim) : ?>
82 (oid) == <?=$oid?> ? '<?=$delim?>' : \
83 <?php endforeach; ?>
84 ',' \
85 ))
86 #endif