From: Ken Youens-Clark Date: Thu, 13 Aug 2009 16:38:31 +0000 (+0000) Subject: Just a cleaner version of "next_unused_name." X-Git-Tag: v0.11008~103 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f5191f69ad874a5bea0f232862528347fcdf1f77;p=dbsrgits%2FSQL-Translator.git Just a cleaner version of "next_unused_name." --- diff --git a/lib/SQL/Translator/Producer/PostgreSQL.pm b/lib/SQL/Translator/Producer/PostgreSQL.pm index c5e54b7..ae37a83 100644 --- a/lib/SQL/Translator/Producer/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/PostgreSQL.pm @@ -299,22 +299,22 @@ sub unreserve { # ------------------------------------------------------------------- sub next_unused_name { - my $name = shift || ''; - if ( !defined( $used_names{$name} ) ) { - $used_names{$name} = $name; - return $name; - } + my $orig_name = shift or return; + my $name = $orig_name; + + my $suffix_gen = sub { + my $suffix = 0; + return ++$suffix ? '' : $suffix; + }; - my $i = 2; - while ( defined( $used_names{ $name . $i } ) ) { - ++$i; + for (;;) { + $name = $orig_name . $suffix_gen->(); + last if $used_names{ $name }++; } - $name .= $i; - $used_names{$name} = $name; + return $name; } - sub create_table { my ($table, $options) = @_;