| 1 | package DBIO::PostgreSQL::Introspect::Parse; | ||||||||||||||||||||||
| 2 | # ABSTRACT: Pure parsers for raw PostgreSQL metadata strings | ||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | 172 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 172 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 172 | our $VERSION = '0.900007'; | |||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 4 | ▶ | sub include_columns { | ||||||||||||||||||||
|
Subroutine
include_columns: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 12 | 4 | my ($class, $definition) = @_; | |||||||||||||||||||||
| 13 | 4 | ▶ | return [] unless defined $definition; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 14 | 3 | ▶ | if ($definition =~ /\bINCLUDE\s*\(([^)]+)\)/i) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 15 | 2 | my $cols = $1; | |||||||||||||||||||||
| 16 | 2 | return [ map { s/^\s+|\s+$//g; s/^"|"$//g; $_ } split /,/, $cols ]; | |||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||
| 18 | 1 | return []; | |||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | 5 | ▶ | sub storage_params { | ||||||||||||||||||||
|
Subroutine
storage_params: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 23 | 5 | my ($class, $reloptions) = @_; | |||||||||||||||||||||
| 24 | 5 | ▶ | return {} unless defined $reloptions; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 25 | 4 | my $raw = $reloptions; | |||||||||||||||||||||
| 26 | 4 | ▶ | $raw =~ s/^\{|\}$//g if !ref $raw; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 27 | 4 | ▶ | my @items = ref $raw ? @$raw : split /,/, $raw; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 28 | 4 | my %params; | |||||||||||||||||||||
| 29 | 4 | for my $item (@items) { | |||||||||||||||||||||
| 30 | 5 | $item =~ s/^\s+|\s+$//g; | |||||||||||||||||||||
| 31 | 5 | ▶ | if ($item =~ /^(.+?)=(.+)$/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 32 | 5 | $params{$1} = $2; | |||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||
| 35 | 4 | return \%params; | |||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | 11 | ▶ | sub column_opclasses { | ||||||||||||||||||||
|
Subroutine
column_opclasses: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||
| 40 | 11 | my ($class, $definition) = @_; | |||||||||||||||||||||
| 41 | 11 | ▶ | return {} unless defined $definition; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | 10 | my $list = _key_column_list($definition); | |||||||||||||||||||||
| 44 | 10 | ▶ | return {} unless defined $list; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | 9 | my %opclasses; | |||||||||||||||||||||
| 47 | 9 | for my $item (_split_top_level($list)) { | |||||||||||||||||||||
| 48 | 11 | $item =~ s/^\s+|\s+$//g; | |||||||||||||||||||||
| 49 | 11 | ▶ | next unless length $item; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 50 | 11 | ▶ | next if $item =~ /^\(/; # expression entry, not a plain column | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | 11 | my ($column, $rest); | |||||||||||||||||||||
| 53 | 11 | ▶ | if ($item =~ /^"((?:[^"]|"")*)"\s*(.*)$/s) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 54 | 1 | ($column, $rest) = ($1, $2); | |||||||||||||||||||||
| 55 | 1 | $column =~ s/""/"/g; | |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||
| 57 | elsif ($item =~ /^([^\W\d][\w\$]*)\s*(.*)$/s) { | ||||||||||||||||||||||
| 58 | 10 | ($column, $rest) = ($1, $2); | |||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||
| 61 | 0 | next; | |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | # A COLLATE clause sits between the column and its operator class. | ||||||||||||||||||||||
| 65 | 11 | $rest =~ s/^COLLATE\s+(?:"(?:[^"]|"")*"|[^\W\d][\w\$.]*)\s*//i; | |||||||||||||||||||||
| 66 | |||||||||||||||||||||||
| 67 | 11 | ▶ | next unless $rest =~ /^([^\W\d][\w\$]*(?:\.[^\W\d][\w\$]*)?)\s*(\([^)]*\))?/; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 68 | 8 | my ($name, $params) = ($1, $2); | |||||||||||||||||||||
| 69 | 8 | ▶ | next if $name =~ /^(?:ASC|DESC|NULLS)\z/i; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | 7 | ▶ | $opclasses{$column} = $name . (defined $params ? $params : ''); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | 9 | return \%opclasses; | |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | # The key-column list of an index definition: everything between the '(' | ||||||||||||||||||||||
| 78 | # that follows "USING <access_method>" and its matching ')'. Returns undef | ||||||||||||||||||||||
| 79 | # when there is no such balanced list. Scanned character-wise rather than | ||||||||||||||||||||||
| 80 | # with a regex because expression entries nest parentheses and may contain | ||||||||||||||||||||||
| 81 | # commas -- "USING btree (lower((a || ','::text)))". | ||||||||||||||||||||||
| 82 | 10 | ▶ | sub _key_column_list { | ||||||||||||||||||||
|
Subroutine
_key_column_list: called CC=10 SCAR=23.0
| |||||||||||||||||||||||
| 83 | 10 | my ($definition) = @_; | |||||||||||||||||||||
| 84 | return undef | ||||||||||||||||||||||
| 85 | 10 | ▶ | unless $definition =~ /\bUSING\s+(?:"(?:[^"]|"")*"|[^\W\d][\w\$]*)\s*\(/gi; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 86 | |||||||||||||||||||||||
| 87 | 9 | my $depth = 1; | |||||||||||||||||||||
| 88 | 9 | my $in_quote = 0; | |||||||||||||||||||||
| 89 | 9 | my $out = ''; | |||||||||||||||||||||
| 90 | 9 | for my $char (split //, substr($definition, pos $definition)) { | |||||||||||||||||||||
| 91 | 251 | ▶ | if ($in_quote) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 92 | 13 | $out .= $char; | |||||||||||||||||||||
| 93 | 13 | ▶ | $in_quote = 0 if $char eq '"'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 94 | 13 | next; | |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||
| 96 | 238 | ▶ | if ($char eq '"') { $in_quote = 1 } | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 97 | 3 | elsif ($char eq '(') { $depth++ } | |||||||||||||||||||||
| 98 | elsif ($char eq ')') { | ||||||||||||||||||||||
| 99 | 12 | $depth--; | |||||||||||||||||||||
| 100 | 12 | ▶ | last if $depth == 0; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||
| 102 | 229 | $out .= $char; | |||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | 9 | ▶ | return $depth == 0 ? $out : undef; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | # Split on the commas that separate index key entries, i.e. those outside | ||||||||||||||||||||||
| 109 | # any nested parentheses and outside a quoted identifier. | ||||||||||||||||||||||
| 110 | 9 | ▶ | sub _split_top_level { | ||||||||||||||||||||
|
Subroutine
_split_top_level: called CC=10 SCAR=23.0
| |||||||||||||||||||||||
| 111 | 9 | my ($text) = @_; | |||||||||||||||||||||
| 112 | 9 | my @parts; | |||||||||||||||||||||
| 113 | 9 | my $depth = 0; | |||||||||||||||||||||
| 114 | 9 | my $in_quote = 0; | |||||||||||||||||||||
| 115 | 9 | my $current = ''; | |||||||||||||||||||||
| 116 | 9 | for my $char (split //, $text) { | |||||||||||||||||||||
| 117 | 242 | ▶ | if ($in_quote) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 118 | 13 | $current .= $char; | |||||||||||||||||||||
| 119 | 13 | ▶ | $in_quote = 0 if $char eq '"'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 120 | 13 | next; | |||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||
| 122 | 229 | ▶ | if ($char eq '"') { $in_quote = 1 } | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 123 | 3 | elsif ($char eq '(') { $depth++ } | |||||||||||||||||||||
| 124 | 3 | elsif ($char eq ')') { $depth-- } | |||||||||||||||||||||
| 125 | ▶ | elsif ($char eq ',' && $depth == 0) { | |||||||||||||||||||||
|
Condition100%3/3
$char eq "," and $depth == 0
MC/DC100%2/2
$char eq "," and $depth == 0
$char eq ","
$depth == 0
Truth table100%3/3
A and B
A= $char eq ","B= $depth == 0
| |||||||||||||||||||||||
| 126 | 2 | push @parts, $current; | |||||||||||||||||||||
| 127 | 2 | $current = ''; | |||||||||||||||||||||
| 128 | 2 | next; | |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||
| 130 | 227 | $current .= $char; | |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||
| 132 | 9 | ▶ | push @parts, $current if length $current; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 133 | 9 | return @parts; | |||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | 172 | 1; | |||||||||||||||||||||
| 137 | |||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||