App-DBCritic

 view release on metacpan or  search on metacpan

lib/App/DBCritic/Policy/NullableTextColumn.pm  view on Meta::CPAN

package App::DBCritic::Policy::NullableTextColumn;

# ABSTRACT: Check for ResultSources with nullable text columns

#pod =head1 SYNOPSIS
#pod
#pod     use App::DBCritic;
#pod
#pod     my $critic = App::DBCritic->new(
#pod         dsn => 'dbi:Oracle:HR', username => 'scott', password => 'tiger');
#pod     $critic->critique();
#pod
#pod =head1 DESCRIPTION
#pod
#pod This policy returns a violation if a
#pod L<DBIx::Class::ResultSource|DBIx::Class::ResultSource> has nullable text
#pod columns.
#pod
#pod =cut

use strict;
use utf8;
use Modern::Perl '2011';    ## no critic (Modules::ProhibitUseQuotedVersion)

our $VERSION = '0.023';     # VERSION
use DBI ':sql_types';
use English '-no_match_vars';
use List::Util 1.33 'any';
use Moo;
use Sub::Quote;
use namespace::autoclean -also => qr{\A _}xms;

has description => (
    is      => 'ro',
    default => quote_sub q{'Nullable text column'},
);

#pod =attr description
#pod
#pod "Nullable text column"
#pod
#pod =cut

has explanation => (
    is      => 'ro',
    default => quote_sub
        q{'Text columns should not be nullable. Default to empty string instead.'},
);

#pod =attr explanation
#pod
#pod "Text columns should not be nullable. Default to empty string instead."
#pod
#pod =cut

sub violates {
    my $source = shift->element;

    ## no critic (ProhibitAccessOfPrivateData,ProhibitCallsToUndeclaredSubs)
    my @text_types = (
        qw(TEXT NTEXT CLOB NCLOB CHARACTER CHAR NCHAR VARCHAR VARCHAR2 NVARCHAR2),
        'CHARACTER VARYING',
        map     { uc $_->{TYPE_NAME} }
            map { $source->storage->dbh->type_info($_) } (
            SQL_CHAR,        SQL_CLOB,
            SQL_VARCHAR,     SQL_WVARCHAR,
            SQL_LONGVARCHAR, SQL_WLONGVARCHAR,
            ),
    );

    my %column = %{ $source->columns_info };
    return join "\n", map {"$_ is a nullable text column."} grep {
        my $col = $_;
        any { uc( $column{$col}{data_type} // q{} ) eq $_ } @text_types
            and $column{$col}{is_nullable};
    } keys %column;
}

#pod =method violates
#pod
#pod Returns details of each column from the
#pod L<"current element"|App::DBCritic::Policy> that maps to
#pod following data types and

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 2.646 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-f5108d614456 )