« blib/lib/App/DBCritic/Policy.pm
↑ Summary
blib/lib/App/DBCritic/Policy/DuplicateRelationships.pm »
| 1 | package App::DBCritic::Policy::NullableTextColumn; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # ABSTRACT: Check for ResultSources with nullable text columns | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||||||||||||
| 6 | #pod | ||||||||||||||||||||
| 7 | #pod use App::DBCritic; | ||||||||||||||||||||
| 8 | #pod | ||||||||||||||||||||
| 9 | #pod my $critic = App::DBCritic->new( | ||||||||||||||||||||
| 10 | #pod dsn => 'dbi:Oracle:HR', username => 'scott', password => 'tiger'); | ||||||||||||||||||||
| 11 | #pod $critic->critique(); | ||||||||||||||||||||
| 12 | #pod | ||||||||||||||||||||
| 13 | #pod =head1 DESCRIPTION | ||||||||||||||||||||
| 14 | #pod | ||||||||||||||||||||
| 15 | #pod This policy returns a violation if a | ||||||||||||||||||||
| 16 | #pod L<DBIx::Class::ResultSource|DBIx::Class::ResultSource> has nullable text | ||||||||||||||||||||
| 17 | #pod columns. | ||||||||||||||||||||
| 18 | #pod | ||||||||||||||||||||
| 19 | #pod =cut | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 6 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 6 | ▶ | use Modern::Perl '2011'; ## no critic (Modules::ProhibitUseQuotedVersion) | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 2 | our $VERSION = '0.023'; # VERSION | |||||||||||||||||||
| 26 | 6 | ▶ | use DBI ':sql_types'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 6 | ▶ | use English '-no_match_vars'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 6 | ▶ | use List::Util 1.33 'any'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 6 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 6 | ▶ | use Sub::Quote; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 6 | ▶ | use namespace::autoclean -also => qr{\A _}xms; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 2 | has description => ( | |||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||
| 35 | default => quote_sub q{'Nullable text column'}, | ||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | #pod =attr description | ||||||||||||||||||||
| 39 | #pod | ||||||||||||||||||||
| 40 | #pod "Nullable text column" | ||||||||||||||||||||
| 41 | #pod | ||||||||||||||||||||
| 42 | #pod =cut | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 2 | has explanation => ( | |||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||
| 46 | default => quote_sub | ||||||||||||||||||||
| 47 | q{'Text columns should not be nullable. Default to empty string instead.'}, | ||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | #pod =attr explanation | ||||||||||||||||||||
| 51 | #pod | ||||||||||||||||||||
| 52 | #pod "Text columns should not be nullable. Default to empty string instead." | ||||||||||||||||||||
| 53 | #pod | ||||||||||||||||||||
| 54 | #pod =cut | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 7 | ▶ | sub violates { | ||||||||||||||||||
|
Subroutine
violates: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 57 | 7 | my $source = shift->element; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | ## no critic (ProhibitAccessOfPrivateData,ProhibitCallsToUndeclaredSubs) | ||||||||||||||||||||
| 60 | my @text_types = ( | ||||||||||||||||||||
| 61 | qw(TEXT NTEXT CLOB NCLOB CHARACTER CHAR NCHAR VARCHAR VARCHAR2 NVARCHAR2), | ||||||||||||||||||||
| 62 | 'CHARACTER VARYING', | ||||||||||||||||||||
| 63 | 7 | map { uc $_->{TYPE_NAME} } | |||||||||||||||||||
| 64 | 7 | map { $source->storage->dbh->type_info($_) } ( | |||||||||||||||||||
| 65 | SQL_CHAR, SQL_CLOB, | ||||||||||||||||||||
| 66 | SQL_VARCHAR, SQL_WVARCHAR, | ||||||||||||||||||||
| 67 | SQL_LONGVARCHAR, SQL_WLONGVARCHAR, | ||||||||||||||||||||
| 68 | ), | ||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 7 | my %column = %{ $source->columns_info }; | |||||||||||||||||||
| 72 | 1 | return join "\n", map {"$_ is a nullable text column."} grep { | |||||||||||||||||||
| 73 | 7 | my $col = $_; | |||||||||||||||||||
| 74 | any { uc( $column{$col}{data_type} // q{} ) eq $_ } @text_types | ||||||||||||||||||||
| 75 | 14 | ▶ | and $column{$col}{is_nullable}; | ||||||||||||||||||
|
Condition66%2/3
&any(sub {
uc($column{$col}{'data_type'} // '') eq $_;
}
, @text_types) && $column{$col}{'is_nullable'}
MC/DC50%1/2
&any(sub {
uc($column{$col}{'data_type'} // '') eq $_;
}
, @text_types) && $column{$col}{'is_nullable'}
&any(sub {
uc($column{$col}{'data_type'} // '') eq $_;
}
, @text_types)
$column{$col}{'is_nullable'}
Truth table66%2/3
A && B
A= &any(sub {
uc($column{$col}{'data_type'} // '') eq $_;
}
, @text_types)B= $column{$col}{'is_nullable'}
| |||||||||||||||||||||
| 76 | } keys %column; | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | #pod =method violates | ||||||||||||||||||||
| 80 | #pod | ||||||||||||||||||||
| 81 | #pod Returns details of each column from the | ||||||||||||||||||||
| 82 | #pod L<"current element"|App::DBCritic::Policy> that maps to | ||||||||||||||||||||
| 83 | #pod following data types and | ||||||||||||||||||||
| 84 | #pod L<"is nullable"|DBIx::Class::ResultSource/is_nullable>: | ||||||||||||||||||||
| 85 | #pod | ||||||||||||||||||||
| 86 | #pod =over | ||||||||||||||||||||
| 87 | #pod | ||||||||||||||||||||
| 88 | #pod =item C<TEXT> | ||||||||||||||||||||
| 89 | #pod | ||||||||||||||||||||
| 90 | #pod =item C<NTEXT> | ||||||||||||||||||||
| 91 | #pod | ||||||||||||||||||||
| 92 | #pod =item C<CLOB> | ||||||||||||||||||||
| 93 | #pod | ||||||||||||||||||||
| 94 | #pod =item C<NCLOB> | ||||||||||||||||||||
| 95 | #pod | ||||||||||||||||||||
| 96 | #pod =item C<CHARACTER> | ||||||||||||||||||||
| 97 | #pod | ||||||||||||||||||||
| 98 | #pod =item C<CHAR> | ||||||||||||||||||||
| 99 | #pod | ||||||||||||||||||||
| 100 | #pod =item C<NCHAR> | ||||||||||||||||||||
| 101 | #pod | ||||||||||||||||||||
| 102 | #pod =item C<VARCHAR> | ||||||||||||||||||||
| 103 | #pod | ||||||||||||||||||||
| 104 | #pod =item C<VARCHAR2> | ||||||||||||||||||||
| 105 | #pod | ||||||||||||||||||||
| 106 | #pod =item C<NVARCHAR2> | ||||||||||||||||||||
| 107 | #pod | ||||||||||||||||||||
| 108 | #pod =item C<CHARACTER VARYING> | ||||||||||||||||||||
| 109 | #pod | ||||||||||||||||||||
| 110 | #pod =item C<SQL_CHAR> | ||||||||||||||||||||
| 111 | #pod | ||||||||||||||||||||
| 112 | #pod =item C<SQL_CLOB> | ||||||||||||||||||||
| 113 | #pod | ||||||||||||||||||||
| 114 | #pod =item C<SQL_VARCHAR> | ||||||||||||||||||||
| 115 | #pod | ||||||||||||||||||||
| 116 | #pod =item C<SQL_WVARCHAR> | ||||||||||||||||||||
| 117 | #pod | ||||||||||||||||||||
| 118 | #pod =item C<SQL_LONGVARCHAR> | ||||||||||||||||||||
| 119 | #pod | ||||||||||||||||||||
| 120 | #pod =item C<SQL_WLONGVARCHAR> | ||||||||||||||||||||
| 121 | #pod | ||||||||||||||||||||
| 122 | #pod =back | ||||||||||||||||||||
| 123 | #pod | ||||||||||||||||||||
| 124 | #pod =cut | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 2 | with 'App::DBCritic::PolicyType::ResultSource'; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | #pod =attr applies_to | ||||||||||||||||||||
| 129 | #pod | ||||||||||||||||||||
| 130 | #pod This policy applies to L<ResultSource|DBIx::Class::ResultSource>s. | ||||||||||||||||||||
| 131 | #pod | ||||||||||||||||||||
| 132 | #pod =cut | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 2 | 1; | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||