From: Rafael Kitover Date: Sat, 24 Apr 2010 00:56:51 +0000 (-0400) Subject: handle CamelCase columns for making relnames X-Git-Tag: 0.07000~55 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=19b7d71c67672e15f11ca6772963170fd6c6b588;p=dbsrgits%2FDBIx-Class-Schema-Loader.git handle CamelCase columns for making relnames --- diff --git a/Changes b/Changes index 8f58d78..6db1296 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for Perl extension DBIx::Class::Schema::Loader - - support CamelCase table names + - support CamelCase table names and column names (in case-sensitive + mode) - rewrite datetime default functions as \'CURRENT_TIMESTAMP' where possible (except for Sybase ASE) to ease cross-deployment - use column_info instead of select to get Oracle column list (RT#42281) diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index b067c76..83947a8 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -165,8 +165,9 @@ the v5 RelBuilder. =item v6 -All monikers and relationships inflected using L, -more aggressive C<_id> stripping from relationships. +All monikers and relationships are inflected using +L, and there is more aggressive C<_id> stripping +from relationship names. In general, there is very little difference between v5 and v6 schemas. @@ -175,8 +176,15 @@ In general, there is very little difference between v5 and v6 schemas. This mode is identical to C mode, except that monikerization of CamelCase table names is also done correctly. -If you don't have any CamelCase table names, you can upgrade without breaking -any of your code. +CamelCase column names in case-sensitive mode will also be handled correctly +for relationship name inflection. + +Currently, only Sybase ASE, MSSQL with CS/BIN collation and Firebird without +the L +option are in case-sensitive mode. + +If you don't have any CamelCase table or column names, you can upgrade without +breaking any of your code. =back @@ -250,9 +258,6 @@ For example: will set the C option to 0 for all generated relationships, except for C, which will have cascade_delete as 1. -NOTE: this option is not supported if v4 backward-compatible naming is -set either globally (naming => 'v4') or just for relationships. - =head2 debug If set to true, each constructive L statement the loader @@ -927,7 +932,10 @@ sub _relbuilder { require DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040; return $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040->new( - $self->schema, $self->inflect_plural, $self->inflect_singular + $self->schema, + $self->inflect_plural, + $self->inflect_singular, + $self->relationship_attrs, ); } elsif ($self->naming->{relationships} eq 'v5') { @@ -939,6 +947,15 @@ sub _relbuilder { $self->relationship_attrs, ); } + elsif ($self->naming->{relationships} eq 'v6') { + require DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_06; + return $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_06->new ( + $self->schema, + $self->inflect_plural, + $self->inflect_singular, + $self->relationship_attrs, + ); + } return $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder->new ( $self->schema, diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm index e6a2c92..63a284c 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm @@ -14,7 +14,7 @@ DBIx::Class::Schema::Loader::RelBuilder - Builds relationships for DBIx::Class:: =head1 SYNOPSIS -See L +See L and L. =head1 DESCRIPTION @@ -212,6 +212,14 @@ sub _remote_attrs { return $attrs; } +sub _normalize_name { + my ($self, $name) = @_; + + my @words = split /(?<=[[:lower:]])[\W_]*(?=[[:upper:]])|[\W_]+/, $name; + + return join '_', map lc, @words; +} + sub _remote_relname { my ($self, $remote_table, $cond) = @_; @@ -220,12 +228,12 @@ sub _remote_relname { # name, to make filter accessors work, but strip trailing _id if(scalar keys %{$cond} == 1) { my ($col) = values %{$cond}; - $col = lc $col; + $col = $self->_normalize_name($col); $col =~ s/_id$//; $remote_relname = $self->_inflect_singular($col); } else { - $remote_relname = $self->_inflect_singular(lc $remote_table); + $remote_relname = $self->_inflect_singular($self->_normalize_name($remote_table)); } return $remote_relname; @@ -312,17 +320,17 @@ sub _relnames_and_method { # col names to distinguish my ($local_relname, $local_relname_uninflected); if ( $counters->{$remote_moniker} > 1) { - my $colnames = lc(q{_} . join(q{_}, map lc($_), @$local_cols)); + my $colnames = q{_} . $self->_normalize_name(join '_', @$local_cols); $remote_relname .= $colnames if keys %$cond > 1; - $local_relname = lc($local_table) . $colnames; + $local_relname = $self->_normalize_name($local_table . $colnames); $local_relname =~ s/_id$//; $local_relname_uninflected = $local_relname; - $local_relname = $self->_inflect_plural( $local_relname ); + $local_relname = $self->_inflect_plural($local_relname); } else { - $local_relname_uninflected = lc $local_table; - $local_relname = $self->_inflect_plural(lc $local_table); + $local_relname_uninflected = $self->_normalize_name($local_table); + $local_relname = $self->_inflect_plural($self->_normalize_name($local_table)); } my $remote_method = 'has_many'; @@ -350,3 +358,4 @@ the same terms as Perl itself. =cut 1; +# vim:et sts=4 sw=4 tw=0: diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm index 1d40f4c..e415c57 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_040.pm @@ -3,26 +3,12 @@ package DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040; use strict; use warnings; use Class::C3; -use base 'DBIx::Class::Schema::Loader::RelBuilder'; +use base 'DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_05'; use Carp::Clan qw/^DBIx::Class/; use Lingua::EN::Inflect::Number (); our $VERSION = '0.07000'; -sub _default_relationship_attrs { +{} } - -sub _to_PL { - my ($self, $name) = @_; - - return Lingua::EN::Inflect::Number::to_PL($name); -} - -sub _to_S { - my ($self, $name) = @_; - - return Lingua::EN::Inflect::Number::to_S($name); -} - sub _relnames_and_method { my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_; diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_05.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_05.pm index f461c5a..5621117 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_05.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_05.pm @@ -3,7 +3,7 @@ package DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_05; use strict; use warnings; use Class::C3; -use base 'DBIx::Class::Schema::Loader::RelBuilder'; +use base 'DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_06'; use Carp::Clan qw/^DBIx::Class/; use Lingua::EN::Inflect::Number (); @@ -66,7 +66,7 @@ sub _relnames_and_method { =head1 NAME DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_05 - RelBuilder for -compatibility with DBIx::Class::Schema::Loader version 0.05000 +compatibility with DBIx::Class::Schema::Loader version 0.05003 =head1 DESCRIPTION diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_06.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_06.pm new file mode 100644 index 0000000..8a01674 --- /dev/null +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder/Compat/v0_06.pm @@ -0,0 +1,40 @@ +package DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_06; + +use strict; +use warnings; +use Class::C3; +use base 'DBIx::Class::Schema::Loader::RelBuilder'; +use Carp::Clan qw/^DBIx::Class/; +use Lingua::EN::Inflect::Phrase (); + +our $VERSION = '0.07000'; + +sub _normalize_name { + my ($self, $name) = @_; + + return lc $name; +} + +=head1 NAME + +DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_06 - RelBuilder for +compatibility with DBIx::Class::Schema::Loader version 0.06001 + +=head1 DESCRIPTION + +See L and +L. + +=head1 AUTHOR + +See L and L. + +=head1 LICENSE + +This library is free software; you can redistribute it and/or modify it under +the same terms as Perl itself. + +=cut + +1; +# vim:et sts=4 sw=4 tw=0: