| 1 | package DBIx::Class::ParseError::Parser; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 4 | ▶ | use Moo::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use DBIx::Class::ParseError::Error; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 4 | ▶ | use Regexp::Common qw(list); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 4 | requires 'type_regex'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 4 | has _schema => ( | |||||||||||||||||||
| 12 | is => 'ro', required => 1, init_arg => 'schema', | ||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 4 | has _source_table_map => ( | |||||||||||||||||||
| 16 | is => 'lazy', builder => '_build_source_table_map', | ||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | sub _build_source_table_map { | ||||||||||||||||||||
| 20 | 4 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_build_source_table_map: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 4 | my $schema = $self->_schema; | |||||||||||||||||||
| 22 | return { | ||||||||||||||||||||
| 23 | map { | ||||||||||||||||||||
| 24 | 4 | my $source = $schema->source($_); | |||||||||||||||||||
| 25 | 12 | ( $schema->class($_) => $source, $source->from => $source ) | |||||||||||||||||||
| 26 | } $schema->sources | ||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | sub parse_type { | ||||||||||||||||||||
| 31 | 17 | ▶ | my ($self, $error) = @_; | ||||||||||||||||||
|
Subroutine
parse_type: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||
| 32 | 17 | my $type_regex = $self->type_regex; | |||||||||||||||||||
| 33 | 17 | foreach (sort keys %$type_regex) { | |||||||||||||||||||
| 34 | 63 | ▶ | if ( my @data = $error =~ $type_regex->{$_} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | return { | ||||||||||||||||||||
| 36 | name => $_, | ||||||||||||||||||||
| 37 | 17 | ▶ | data => [ grep { defined && length } @data ], | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | 0 | return { name => 'unknown' }; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | sub _add_info_from_type { | ||||||||||||||||||||
| 45 | 17 | ▶ | my ($self, $error_info, $error_type) = @_; | ||||||||||||||||||
|
Subroutine
_add_info_from_type: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 46 | 17 | my $table = $error_info->{'table'}; | |||||||||||||||||||
| 47 | 17 | ▶ | my $replace_dots = sub { $_[0] =~ s{\.}{_}; $_[0] }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | 17 | ▶ | my $remove_table = sub { $_[0] =~ s{^$table\.}{}i; $_[0] }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 49 | 17 | my $source = $self->_source_table_map->{$table}; | |||||||||||||||||||
| 50 | my $action_type_map = { | ||||||||||||||||||||
| 51 | unique_key => sub { | ||||||||||||||||||||
| 52 | 6 | ▶ | my $unique_keys = { $source->unique_constraints }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 53 | my $unique_data = [ | ||||||||||||||||||||
| 54 | 6 | map { $replace_dots->($_) } @{ $error_type->{'data'} } | |||||||||||||||||||
| 55 | ]; | ||||||||||||||||||||
| 56 | 6 | ▶ | if ( my $unique_cols = $unique_keys->{ $unique_data->[0] } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | $error_info->{'columns'} = [ | ||||||||||||||||||||
| 58 | 2 | map { $remove_table->($_) } @$unique_cols | |||||||||||||||||||
| 59 | ]; | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||
| 62 | 4 | $error_info->{'type'} = 'primary_key'; | |||||||||||||||||||
| 63 | $error_info->{'columns'} = [ | ||||||||||||||||||||
| 64 | 4 | map { $remove_table->($_) } @{ $unique_keys->{'primary'} } | |||||||||||||||||||
| 65 | ]; | ||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||
| 68 | primary_key => sub { | ||||||||||||||||||||
| 69 | $error_info->{'columns'} = [ | ||||||||||||||||||||
| 70 | 0 | ▶ | map { $remove_table->($_) } $source->primary_columns | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 71 | ]; | ||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||
| 73 | default => sub { | ||||||||||||||||||||
| 74 | 11 | ▶ | if ( @{ $error_type->{'data'} } ) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||
| 75 | $error_info->{'columns'} = [ | ||||||||||||||||||||
| 76 | 10 | map { $remove_table->($_) } @{ $error_type->{'data'} } | |||||||||||||||||||
| 77 | ]; | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||
| 80 | 17 | }; | |||||||||||||||||||
| 81 | ( $action_type_map->{ $error_type->{'name'} } | ||||||||||||||||||||
| 82 | 17 | ▶ | || $action_type_map->{'default'} )->(); | ||||||||||||||||||
|
Condition66%2/3
$$action_type_map{$error_type->{'name'}} || $action_type_map->{'default'}
MC/DC0%0/2
$$action_type_map{$error_type->{'name'}} || $action_type_map->{'default'}
$$action_type_map{$error_type->{'name'}}
$action_type_map->{'default'}
Truth table66%2/3
A || B
A= $$action_type_map{$error_type->{'name'}}B= $action_type_map->{'default'}
| |||||||||||||||||||||
| 83 | 17 | return $error_info; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | sub _build_column_data { | ||||||||||||||||||||
| 87 | 12 | ▶ | my ($self, $column_keys, $column_values) = @_; | ||||||||||||||||||
|
Subroutine
_build_column_data: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 88 | 12 | $column_keys =~ s{\s*=\s*\?}{}g; | |||||||||||||||||||
| 89 | 12 | $column_keys = [split(/\,\s+/, $column_keys)]; | |||||||||||||||||||
| 90 | 12 | ▶ | if ($column_values) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 11 | $column_values =~ s{\'}{}g; | |||||||||||||||||||
| 92 | $column_values = [ | ||||||||||||||||||||
| 93 | 11 | map { (split(/=/))[1] } | |||||||||||||||||||
| 94 | split(/\,\s+/, $column_values) | ||||||||||||||||||||
| 95 | ]; | ||||||||||||||||||||
| 96 | return { | ||||||||||||||||||||
| 97 | map { | ||||||||||||||||||||
| 98 | 11 | my $value = shift(@$column_values); | |||||||||||||||||||
| 99 | 28 | ▶ | $_ => ($value =~ m/undef/ ? undef : $value) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | } @$column_keys | ||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||
| 104 | 1 | return { map { $_ => undef } @$column_keys }; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | sub parse_general_info { | ||||||||||||||||||||
| 109 | 17 | ▶ | my ($self, $error, $error_type) = @_; | ||||||||||||||||||
|
Subroutine
parse_general_info: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 17 | my $insert_re = qr{ | |||||||||||||||||||
| 112 | INSERT\s+INTO\s+ | ||||||||||||||||||||
| 113 | (\w+)\s+ | ||||||||||||||||||||
| 114 | \( \s* ($RE{list}{-pat => '\w+'}|\w+)\s* \)\s+ | ||||||||||||||||||||
| 115 | VALUES\s+ | ||||||||||||||||||||
| 116 | \( \s* (?:$RE{list}{-pat => '\?'}|\?)\s* \)\s*\" | ||||||||||||||||||||
| 117 | \s*\w*\s*\w*:?\s* | ||||||||||||||||||||
| 118 | ($RE{list}{-pat => '\d=\'?[\w\s]+\'?'})? | ||||||||||||||||||||
| 119 | }ix; | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 17 | my $update_re = qr{ | |||||||||||||||||||
| 122 | UPDATE\s+ | ||||||||||||||||||||
| 123 | (\w+)\s+ | ||||||||||||||||||||
| 124 | SET\s+ | ||||||||||||||||||||
| 125 | ($RE{list}{-pat => '\w+\s*\=\s*\?'}|\w+\s*\=\s*\?)\s* | ||||||||||||||||||||
| 126 | (?:WHERE)?.*\" | ||||||||||||||||||||
| 127 | \s*\w*\s*\w*:?\s* | ||||||||||||||||||||
| 128 | ($RE{list}{-pat => '\d=\'?[\w\s]+\'?'})? | ||||||||||||||||||||
| 129 | }ix; | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 17 | my $missing_column_re = qr{ | |||||||||||||||||||
| 132 | (store_column|get_column)\(\)\:\s+ | ||||||||||||||||||||
| 133 | no\s+such\s+column\s+['"](\w+)['"]\s+ | ||||||||||||||||||||
| 134 | on\s+($RE{list}{-pat => '\w+'}{-sep => '::'}) | ||||||||||||||||||||
| 135 | }ix; | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 17 | my $source_table_map = $self->_source_table_map; | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 17 | my $error_info; | |||||||||||||||||||
| 140 | 17 | ▶ | if ( $error =~ $insert_re ) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 141 | 9 | my ($table, $column_keys, $column_values) = ($1, $2, $3); | |||||||||||||||||||
| 142 | 9 | $error_info = { | |||||||||||||||||||
| 143 | operation => 'insert', | ||||||||||||||||||||
| 144 | table => $table, | ||||||||||||||||||||
| 145 | column_data => $self->_build_column_data( | ||||||||||||||||||||
| 146 | $column_keys, $column_values | ||||||||||||||||||||
| 147 | ), | ||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | elsif ( $error =~ $update_re ) { | ||||||||||||||||||||
| 151 | 3 | my ($table, $column_keys, $column_values) = ($1, $2, $3); | |||||||||||||||||||
| 152 | 3 | $error_info = { | |||||||||||||||||||
| 153 | operation => 'update', | ||||||||||||||||||||
| 154 | table => $table, | ||||||||||||||||||||
| 155 | column_data => $self->_build_column_data( | ||||||||||||||||||||
| 156 | $column_keys, $column_values | ||||||||||||||||||||
| 157 | ), | ||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | elsif ( $error =~ $missing_column_re ) { | ||||||||||||||||||||
| 161 | 5 | my ($op_key, $column, $source_name) = ($1, $2, $3); | |||||||||||||||||||
| 162 | 5 | my $op_mapping = { | |||||||||||||||||||
| 163 | 'store_column' => 'insert', | ||||||||||||||||||||
| 164 | 'get_column' => 'update', | ||||||||||||||||||||
| 165 | }; | ||||||||||||||||||||
| 166 | 5 | my $source = $source_table_map->{ $source_name }; | |||||||||||||||||||
| 167 | $error_info = { | ||||||||||||||||||||
| 168 | 5 | ▶ | operation => $op_mapping->{ lc $op_key }, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 169 | $source ? ( table => $source->name ) : (), | ||||||||||||||||||||
| 170 | columns => [$column], | ||||||||||||||||||||
| 171 | }; | ||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | else { | ||||||||||||||||||||
| 174 | 0 | die 'Parsing error string failed'; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 17 | ▶ | if (my $source = $source_table_map->{ $error_info->{'table'} }) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | 17 | $error_info->{'source_name'} = $source->source_name; | |||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 17 | return $self->_add_info_from_type($error_info, $error_type); | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | sub process { | ||||||||||||||||||||
| 185 | 17 | ▶ | my ($self, $error) = @_; | ||||||||||||||||||
|
Subroutine
process: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 186 | 17 | my $error_type = $self->parse_type($error); | |||||||||||||||||||
| 187 | my $err_info = { | ||||||||||||||||||||
| 188 | type => $error_type->{'name'}, | ||||||||||||||||||||
| 189 | 17 | %{ $self->parse_general_info($error, $error_type) }, | |||||||||||||||||||
| 190 | }; | ||||||||||||||||||||
| 191 | 17 | return DBIx::Class::ParseError::Error->new( | |||||||||||||||||||
| 192 | message => "$error", | ||||||||||||||||||||
| 193 | %$err_info, | ||||||||||||||||||||
| 194 | ); | ||||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 4 | 1; | |||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | __END__ | ||||||||||||||||||||