From: Allen Day Date: Thu, 17 Apr 2003 23:16:29 +0000 (+0000) Subject: adding callbacks to Translator.pm to allow mangling of PK/FK/table names/package... X-Git-Tag: v0.02~187 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7d5bcab8b6c8f5edb64633f092245d97510a1a06;p=dbsrgits%2FSQL-Translator.git adding callbacks to Translator.pm to allow mangling of PK/FK/table names/package names (CDBI specific) for producer classes. --- diff --git a/lib/SQL/Translator.pm b/lib/SQL/Translator.pm index 4fa0e78..04d4596 100644 --- a/lib/SQL/Translator.pm +++ b/lib/SQL/Translator.pm @@ -1,7 +1,7 @@ package SQL::Translator; # ---------------------------------------------------------------------- -# $Id: Translator.pm,v 1.21 2003-04-17 13:40:47 dlc Exp $ +# $Id: Translator.pm,v 1.22 2003-04-17 23:16:28 allenday Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # darren chamberlain , @@ -27,7 +27,7 @@ use vars qw( $VERSION $REVISION $DEFAULT_SUB $DEBUG $ERROR ); use base 'Class::Base'; $VERSION = '0.01'; -$REVISION = sprintf "%d.%02d", q$Revision: 1.21 $ =~ /(\d+)\.(\d+)/; +$REVISION = sprintf "%d.%02d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/; $DEBUG = 0 unless defined $DEBUG; $ERROR = ""; @@ -70,6 +70,14 @@ sub init { $self->parser ($config->{'parser'} || $config->{'from'} || $DEFAULT_SUB); $self->producer($config->{'producer'} || $config->{'to'} || $DEFAULT_SUB); + # + # Set up callbacks for formatting of pk,fk,table,package names in producer + # + $self->format_table_name($config->{'format_table_name'}); + $self->format_package_name($config->{'format_package_name'}); + $self->format_fk_name($config->{'format_fk_name'}); + $self->format_pk_name($config->{'format_pk_name'}); + # # Set the parser_args and producer_args # @@ -648,6 +656,38 @@ sub load { return 1; } +sub format_table_name { + my $self = shift; + my $sub = shift; + $self->{_format_table_name} = $sub if ref($sub) eq 'CODE'; + return $self->{_format_table_name}->($sub,@_) if defined($self->{_format_table_name}); + return($sub); +} + +sub format_package_name { + my $self = shift; + my $sub = shift; + $self->{_format_package_name} = $sub if ref($sub) eq 'CODE'; + return $self->{_format_package_name}->($sub,@_) if defined($self->{_format_package_name}); + return($sub); +} + +sub format_fk_name { + my $self = shift; + my $sub = shift; + $self->{_format_fk_name} = $sub if ref($sub) eq 'CODE'; + return $self->{_format_fk_name}->($sub,@_) if defined($self->{_format_fk_name}); + return($sub); +} + +sub format_pk_name { + my $self = shift; + my $sub = shift; + $self->{_format_pk_name} = $sub if ref($sub) eq 'CODE'; + return $self->{_format_pk_name}->($sub,@_) if defined($self->{_format_pk_name}); + return($sub); +} + # ---------------------------------------------------------------------- # isa($ref, $type) # @@ -681,6 +721,13 @@ SQL::Translator - convert schema from one database to another no_comments => 0, # Don't include comments in output show_warnings => 0, # Print name mutations, conflicts add_drop_table => 1, # Add "drop table" statements + + #make all table names CAPS in producers which support this option + format_table_name => sub {my $tablename = shift; return uc($tablename)}, + #null-op formatting, only here for documentation's sake + format_package_name => sub {return shift}, + format_fk_name => sub {return shift}, + format_pk_name => sub {return shift}, ); my $output = $translator->translate( diff --git a/lib/SQL/Translator/Parser/PostgreSQL.pm b/lib/SQL/Translator/Parser/PostgreSQL.pm index bd9601b..6df524f 100644 --- a/lib/SQL/Translator/Parser/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/PostgreSQL.pm @@ -1,7 +1,7 @@ package SQL::Translator::Parser::PostgreSQL; # ------------------------------------------------------------------- -# $Id: PostgreSQL.pm,v 1.11 2003-04-17 19:42:33 allenday Exp $ +# $Id: PostgreSQL.pm,v 1.12 2003-04-17 23:16:29 allenday Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # Allen Day , @@ -107,7 +107,7 @@ Alter table: use strict; use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/; $DEBUG = 0 unless defined $DEBUG; use Data::Dumper; @@ -202,7 +202,7 @@ create : create_table table_name '(' create_definition(s /,/) ')' table_option(s for my $constraint ( @{ $definition->{'constraints'} || [] } ) { $constraint->{'fields'} = [ $field_name ]; - push @{ $tables{ $table_name }{'constraints'} }, + push @{ $tables{ $table_name }{'constraints'} }, $constraint; } }