Skip to content

Commit 49c784e

Browse files
committed
Remove hard-coded schema knowledge about pg_attribute from genbki.pl
Add the ability to label a column's default value in the catalog header, and implement this for pg_attribute. A new function in Catalog.pm is used to fill in a tuple with defaults. The build process will complain loudly if a catalog entry is incomplete, Commit 8137f2c labeled variable length columns for the C preprocessor. Expose that label to genbki.pl so we can exclude those columns from schema macros in a general fashion. Also, format schema macro entries according to their types. This means slightly less code maintenance, but more importantly it's a proving ground for mechanisms intended to be used in later commits. While at it, I (Álvaro) couldn't resist making some changes in genbki.pl: rename some functions to actually indicate their purpose instead of actively misleading onlookers; and don't iterate on the whole of pg_type to find the entry for each catalog row, using a hash instead of an array. Author: John Naylor, some changes by Álvaro Herrera Discussion: https://postgr.es/m/CAJVSVGVJHwD8sfDfZW9TbCHWKf=C1YDRM-rF=2JenRU_y+VcFg@mail.gmail.com
1 parent bdb70c1 commit 49c784e

File tree

4 files changed

+187
-136
lines changed

4 files changed

+187
-136
lines changed

src/backend/catalog/Catalog.pm

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ sub Catalogs
3737
foreach my $input_file (@_)
3838
{
3939
my %catalog;
40+
my $is_varlen = 0;
41+
4042
$catalog{columns} = [];
4143
$catalog{data} = [];
4244

@@ -164,30 +166,39 @@ sub Catalogs
164166
elsif ($declaring_attributes)
165167
{
166168
next if (/^{|^$/);
167-
next if (/^#/);
169+
if (/^#/)
170+
{
171+
$is_varlen = 1 if /^#ifdef\s+CATALOG_VARLEN/;
172+
next;
173+
}
168174
if (/^}/)
169175
{
170176
undef $declaring_attributes;
171177
}
172178
else
173179
{
174180
my %column;
175-
my ($atttype, $attname, $attopt) = split /\s+/, $_;
176-
die "parse error ($input_file)" unless $attname;
181+
my @attopts = split /\s+/, $_;
182+
my $atttype = shift @attopts;
183+
my $attname = shift @attopts;
184+
die "parse error ($input_file)"
185+
unless ($attname and $atttype);
186+
177187
if (exists $RENAME_ATTTYPE{$atttype})
178188
{
179189
$atttype = $RENAME_ATTTYPE{$atttype};
180190
}
181191
if ($attname =~ /(.*)\[.*\]/) # array attribute
182192
{
183193
$attname = $1;
184-
$atttype .= '[]'; # variable-length only
194+
$atttype .= '[]';
185195
}
186196

187197
$column{type} = $atttype;
188198
$column{name} = $attname;
199+
$column{is_varlen} = 1 if $is_varlen;
189200

190-
if (defined $attopt)
201+
foreach my $attopt (@attopts)
191202
{
192203
if ($attopt eq 'BKI_FORCE_NULL')
193204
{
@@ -197,11 +208,20 @@ sub Catalogs
197208
{
198209
$column{forcenotnull} = 1;
199210
}
211+
elsif ($attopt =~ /BKI_DEFAULT\((\S+)\)/)
212+
{
213+
$column{default} = $1;
214+
}
200215
else
201216
{
202217
die
203218
"unknown column option $attopt on column $attname";
204219
}
220+
221+
if ($column{forcenull} and $column{forcenotnull})
222+
{
223+
die "$attname is forced both null and not null";
224+
}
205225
}
206226
push @{ $catalog{columns} }, \%column;
207227
}
@@ -235,6 +255,46 @@ sub SplitDataLine
235255
return @result;
236256
}
237257

258+
# Fill in default values of a record using the given schema. It's the
259+
# caller's responsibility to specify other values beforehand.
260+
sub AddDefaultValues
261+
{
262+
my ($row, $schema) = @_;
263+
my @missing_fields;
264+
my $msg;
265+
266+
foreach my $column (@$schema)
267+
{
268+
my $attname = $column->{name};
269+
my $atttype = $column->{type};
270+
271+
if (defined $row->{$attname})
272+
{
273+
;
274+
}
275+
elsif (defined $column->{default})
276+
{
277+
$row->{$attname} = $column->{default};
278+
}
279+
else
280+
{
281+
# Failed to find a value.
282+
push @missing_fields, $attname;
283+
}
284+
}
285+
286+
if (@missing_fields)
287+
{
288+
$msg = "Missing values for: " . join(', ', @missing_fields);
289+
$msg .= "\nShowing other values for context:\n";
290+
while (my($key, $value) = each %$row)
291+
{
292+
$msg .= "$key => $value, ";
293+
}
294+
}
295+
return $msg;
296+
}
297+
238298
# Rename temporary files to final names.
239299
# Call this function with the final file name and the .tmp extension
240300
# Note: recommended extension is ".tmp$$", so that parallel make steps

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy