File Coverage

blib/lib/DBIx/POS.pm
Criterion Covered Total %
statement 13 53 24.5
branch 0 18 0.0
condition 0 5 0.0
subroutine 5 15 33.3
pod n/a
total 18 91 19.7


line stmt bran cond sub pod time code
1             package DBIx::POS::Statement;
2              
3 1     1   2262 use overload '""' => sub { shift->{sql} };
  1     0   1192  
  1         8  
  0         0  
4              
5             sub new {
6 0     0     my $proto = shift;
7 0   0       my $class = ref $proto || $proto;
8 0           my $self = shift;
9 0           bless ($self, $class);
10 0           return $self;
11             }
12              
13             sub desc {
14 0     0     my $self = shift;
15 0 0         $self->{desc} = shift if (@_);
16 0           return $self->{desc};
17             }
18              
19             sub name {
20 0     0     my $self = shift;
21 0 0         $self->{name} = shift if (@_);
22 0           return $self->{name};
23             }
24              
25             sub noreturn {
26 0     0     my $self = shift;
27 0 0         $self->{noreturn} = shift if (@_);
28 0           return $self->{noreturn};
29             }
30              
31             sub param {
32 0     0     my $self = shift;
33 0 0         $self->{param} = shift if (@_);
34 0           return $self->{param};
35             }
36              
37             sub short {
38 0     0     my $self = shift;
39 0 0         $self->{short} = shift if (@_);
40 0           return $self->{short};
41             }
42              
43             sub sql {
44 0     0     my $self = shift;
45 0 0         $self->{sql} = shift if (@_);
46 0           return $self->{sql};
47             }
48              
49             package DBIx::POS;
50             # arch-tag: 2f764256-6fc0-415e-865d-767d9f202f02
51              
52 1     1   328 use strict;
  1         1  
  1         34  
53 1     1   4 use warnings;
  1         6  
  1         67  
54 1     1   6 use base qw{Pod::Parser};
  1         1  
  1         450  
55              
56             # Set our version
57             our $VERSION = '0.03';
58              
59             # Hold data for our pending statement
60             my $info = {};
61              
62             # Hold our SQL
63             my %sql;
64              
65             # What command we're looking at
66             my $state;
67              
68             # Does the work of creating a new instance
69             sub _new_instance {
70 0     0     my $class = shift;
71 0           my $file = shift;
72 0           $class->new->parse_from_file ($file);
73 0           bless \%sql, $class;
74             }
75              
76             # Handle =whatever commands
77             sub command {
78 0     0     my ($self, $command, $paragraph, $line) = @_;
79              
80             # Get rid of all trailing whitespace
81 0           $paragraph =~ s/\s+$//ms;
82              
83             # There may be a short description right after the command
84 0 0         if ($command eq 'desc') {
85 0   0       $info->{short} = $paragraph || "";
86             }
87              
88             # The name comes right after the command
89 0 0         if ($command eq 'name') {
90 0           $self->end_input;
91 0           $info->{name} = $paragraph;
92             }
93              
94             # The noreturn comes right after the command
95 0 0         if ($command eq 'noreturn') {
96 0           $info->{noreturn} = 1;
97             }
98              
99             # Remember what command we're in
100 0           $state = $command;
101             }
102              
103             sub end_input {
104             my ($self) = @_;
105              
106             # If there's stuff to try and construct from
107             if (%{$info}) {
108              
109             # If we have the necessary bits
110             if (scalar (grep {m/^(?:name|short|desc|sql)$/} keys %{$info}) == 3) {
111              
112             # Grab the entire content for the %sql hash
113             $sql{$info->{name}} = DBIx::POS::Statement->new ($info);
114              
115             # Start with a new empty hashref
116             $info = {};
117             }
118              
119             # Something's missing
120             else {
121              
122             # A nice format for dumping
123 1     1   1627 use YAML qw{Dump};
  0            
  0            
124              
125             die "Malformed entry\n" . Dump (\%sql, $info);
126             }
127             }
128             }
129              
130             # Taken directly from Class::Singleton---we were already overriding
131             # _new_instance, and it seemed silly to have an additional dependency
132             # for four statements.
133              
134             sub instance {
135             my $class = shift;
136              
137             # get a reference to the _instance variable in the $class package
138             no strict 'refs';
139             my $instance = \${ "$class\::_instance" };
140              
141             defined $$instance
142             ? $$instance
143             : ($$instance = $class->_new_instance(@_));
144             }
145              
146             # Handle the blocks of text between commands
147             sub textblock {
148             my ($parser, $paragraph, $line) = @_;
149              
150             # Collapse trailing whitespace to a \n
151             $paragraph =~ s/\s+$/\n/ms;
152              
153             if ($state eq 'desc') {
154             $info->{desc} .= $paragraph;
155             }
156              
157             elsif ($state eq 'param') {
158             $info->{param} .= $paragraph;
159             }
160              
161             elsif ($state eq 'sql') {
162             $info->{sql} .= $paragraph;
163             }
164             }
165              
166             # We handle verbatim sections the same way
167             sub verbatim {
168             my ($parser, $paragraph, $line) = @_;
169              
170             # Collapse trailing whitespace to a \n
171             $paragraph =~ s/\s+$/\n/ms;
172              
173             if ($state eq 'desc') {
174             $info->{desc} .= $paragraph;
175             }
176              
177             elsif ($state eq 'param') {
178             $info->{param} .= $paragraph;
179             }
180              
181             elsif ($state eq 'sql') {
182             $info->{sql} .= $paragraph;
183             }
184             }
185              
186             1;
187              
188             __END__