File Coverage

blib/lib/App/DBCritic/Policy/NullableTextColumn.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 36 36 100.0


line stmt bran cond sub pod time code
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 has nullable text
17             #pod columns.
18             #pod
19             #pod =cut
20              
21 5     5   4272 use strict;
  5         13  
  5         163  
22 5     5   25 use utf8;
  5         11  
  5         35  
23 5     5   131 use Modern::Perl '2011'; ## no critic (Modules::ProhibitUseQuotedVersion)
  5         12  
  5         35  
24              
25             our $VERSION = '0.023'; # VERSION
26 5     5   855 use DBI ':sql_types';
  5         10  
  5         1962  
27 5     5   38 use English '-no_match_vars';
  5         10  
  5         45  
28 5     5   1793 use List::Util 1.33 'any';
  5         175  
  5         318  
29 5     5   32 use Moo;
  5         12  
  5         31  
30 5     5   2751 use Sub::Quote;
  5         10  
  5         514  
31 5     5   39 use namespace::autoclean -also => qr{\A _}xms;
  5         20  
  5         60  
32              
33             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             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             sub violates {
57             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             map { uc $_->{TYPE_NAME} }
64             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             my %column = %{ $source->columns_info };
72             return join "\n", map {"$_ is a nullable text column."} grep {
73             my $col = $_;
74             any { uc( $column{$col}{data_type} // q{} ) eq $_ } @text_types
75             and $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
89             #pod
90             #pod =item C
91             #pod
92             #pod =item C
93             #pod
94             #pod =item C
95             #pod
96             #pod =item C
97             #pod
98             #pod =item C
99             #pod
100             #pod =item C
101             #pod
102             #pod =item C
103             #pod
104             #pod =item C
105             #pod
106             #pod =item C
107             #pod
108             #pod =item C
109             #pod
110             #pod =item C
111             #pod
112             #pod =item C
113             #pod
114             #pod =item C
115             #pod
116             #pod =item C
117             #pod
118             #pod =item C
119             #pod
120             #pod =item C
121             #pod
122             #pod =back
123             #pod
124             #pod =cut
125              
126             with 'App::DBCritic::PolicyType::ResultSource';
127              
128             #pod =attr applies_to
129             #pod
130             #pod This policy applies to Ls.
131             #pod
132             #pod =cut
133              
134             1;
135              
136             __END__