From: Rafael Kitover Date: Tue, 2 Aug 2011 10:07:38 +0000 (-0400) Subject: minor changes to table/column comment code X-Git-Tag: 0.07011~59 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ea998e8ea908a2f10462740b568831737224a75e;p=dbsrgits%2FDBIx-Class-Schema-Loader.git minor changes to table/column comment code Updates Changes. Mentions Oracle support in generate_pod POD in ::Loader::Base. Adds checking for generic comments via ->next::method in ::DBI::Oracle as well. Wraps table/column comment queries for MySQL on information_schema in try blocks, as not all versions of MySQL have information_schema (5.0+ do.) Changes MySQL comment tests to use the new slurp_file util. Makes the test messages in t/30_*.t more clear. --- diff --git a/Changes b/Changes index fc30cf2..13536ac 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Perl extension DBIx::Class::Schema::Loader + - generic table and column comments support + - MySQL table and column comments support - support DOS line endings on *nix and *nix line ending on Win32 - add quiet option - $schema->loader is now a public method diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index a576c71..49c0359 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -264,7 +264,7 @@ L.) By default POD will be generated for columns and relationships, using database metadata for the text if available and supported. -Metadata can be stored in two ways. +Comment metadata can be stored in two ways. The first is that you can create two tables named C and C respectively. They both need to have columns named @@ -275,12 +275,13 @@ source of metadata about tables and comments. (If you wish you can change the name of these tables with the parameters L and L.) -As a fallback you can use built-in commenting mechanisms. Currently this -is only supported for PostgreSQL and MySQL. To create comments in -PostgreSQL you add statements of the form C. -To create comments in MySQL you add C to the end of the -column or table definition. Note that MySQL restricts the length of comments, -and also does not handle complex Unicode characters properly. +As a fallback you can use built-in commenting mechanisms. Currently this is +only supported for PostgreSQL, Oracle and MySQL. To create comments in +PostgreSQL you add statements of the form C, the same syntax is used in Oracle. To create comments in MySQL you add +C to the end of the column or table definition. Note that MySQL +restricts the length of comments, and also does not handle complex Unicode +characters properly. Set this to C<0> to turn off all POD generation. diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm index f26ad53..693122d 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm @@ -122,8 +122,14 @@ sub _table_uniq_info { } sub _table_comment { - my ( $self, $table ) = @_; - my ($table_comment) = $self->schema->storage->dbh->selectrow_array( + my $self = shift; + my ($table) = @_; + + my $table_comment = $self->next::method(@_); + + return $table_comment if $table_comment; + + ($table_comment) = $self->schema->storage->dbh->selectrow_array( q{ SELECT comments FROM all_tab_comments WHERE owner = ? @@ -136,8 +142,14 @@ sub _table_comment { } sub _column_comment { - my ( $self, $table, $column_number, $column_name ) = @_; - my ($column_comment) = $self->schema->storage->dbh->selectrow_array( + my $self = shift; + my ($table, $column_number, $column_name) = @_; + + my $column_comment = $self->next::method(@_); + + return $column_comment if $column_comment; + + ($column_comment) = $self->schema->storage->dbh->selectrow_array( q{ SELECT comments FROM all_col_comments WHERE owner = ? diff --git a/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm b/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm index ee886ae..50925aa 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/mysql.pm @@ -5,6 +5,7 @@ use warnings; use base 'DBIx::Class::Schema::Loader::DBI'; use mro 'c3'; use List::Util 'first'; +use Try::Tiny; use namespace::clean; our $VERSION = '0.07010'; @@ -245,12 +246,13 @@ sub _table_comment { my ( $self, $table ) = @_; my $comment = $self->next::method($table); if (not $comment) { - ($comment) = $self->schema->storage->dbh->selectrow_array( + ($comment) = try { $self->schema->storage->dbh->selectrow_array( qq{SELECT table_comment FROM information_schema.tables WHERE table_schema = schema() AND table_name = ? }, undef, $table); + }; # InnoDB likes to auto-append crap. if (not $comment) { # Do nothing. @@ -262,20 +264,21 @@ sub _table_comment { $comment =~ s/; InnoDB.*//; } } - return $comment || "Gotcha $table?"; + return $comment; } sub _column_comment { my ( $self, $table, $column_number, $column_name ) = @_; my $comment = $self->next::method($table, $column_number, $column_name); if (not $comment) { - ($comment) = $self->schema->storage->dbh->selectrow_array( + ($comment) = try { $self->schema->storage->dbh->selectrow_array( qq{SELECT column_comment FROM information_schema.columns WHERE table_schema = schema() AND table_name = ? AND column_name = ? }, undef, $table, $column_name); + }; } return $comment; } diff --git a/t/10_02mysql_common.t b/t/10_02mysql_common.t index bf6eb4a..9a538ea 100644 --- a/t/10_02mysql_common.t +++ b/t/10_02mysql_common.t @@ -1,10 +1,8 @@ use strict; -use File::Slurp qw(slurp); +use DBIx::Class::Schema::Loader::Utils 'slurp_file'; use Test::More; use lib qw(t/lib); use dbixcsl_common_tests; -use utf8; -use Encode 'decode'; my $dsn = $ENV{DBICTEST_MYSQL_DSN} || ''; my $user = $ENV{DBICTEST_MYSQL_USER} || ''; @@ -191,9 +189,9 @@ my $tester = dbixcsl_common_tests->new( 'hairy enum introspected correctly'; my $class = $classes->{'mysql_loader-test1'}; - my $filename = $schema->_loader->get_dump_filename($class); + my $filename = $schema->loader->get_dump_filename($class); - my $code = decode('UTF-8', scalar slurp $filename); + my $code = slurp_file $filename; like $code, qr/^=head1 NAME\n\n^$class - The\nTable\n\n^=cut\n/m, 'table comment'; diff --git a/t/30_02bad_comment_table.t b/t/30_02bad_comment_table.t index da90e5c..94c1432 100644 --- a/t/30_02bad_comment_table.t +++ b/t/30_02bad_comment_table.t @@ -7,6 +7,7 @@ use File::Slurp qw(slurp); use File::Path; use make_dbictest_db_bad_comment_tables; use dbixcsl_test_dir qw/$tdir/; +use Try::Tiny; my $dump_path = "$tdir/dump"; @@ -18,13 +19,15 @@ my $dump_path = "$tdir/dump"; ); } -DBICTest::Schema::1->connect($make_dbictest_db_bad_comment_tables::dsn); +try { + DBICTest::Schema::1->connect($make_dbictest_db_bad_comment_tables::dsn); +}; plan tests => 1; -my $foo = slurp("$dump_path/DBICTest/Schema/1/Result/Foo.pm"); -my $bar = slurp("$dump_path/DBICTest/Schema/1/Result/Bar.pm"); +my $foo = try { slurp("$dump_path/DBICTest/Schema/1/Result/Foo.pm") }; +my $bar = try { slurp("$dump_path/DBICTest/Schema/1/Result/Bar.pm") }; -like($foo, qr/Result::Foo\n/, 'No error from no comment table'); +like($foo, qr/Result::Foo\n/, 'No error from invalid comment tables'); END { rmtree($dump_path, 1, 1); } diff --git a/t/30_03no_comment_table.t b/t/30_03no_comment_table.t index 8ef3473..f7dad9e 100644 --- a/t/30_03no_comment_table.t +++ b/t/30_03no_comment_table.t @@ -25,6 +25,6 @@ plan tests => 1; my $foo = slurp("$dump_path/DBICTest/Schema/1/Result/Foo.pm"); my $bar = slurp("$dump_path/DBICTest/Schema/1/Result/Bar.pm"); -like($foo, qr/Result::Foo\n/, 'No error from no comments'); +like($foo, qr/Result::Foo\n/, 'No error from lack of comment tables'); END { rmtree($dump_path, 1, 1); }