File: Query.pm

package info (click to toggle)
libtest-dbic-expectedqueries-perl 2.002-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 148 kB
  • sloc: perl: 347; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 1,775 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package Test::DBIC::ExpectedQueries::Query;
$Test::DBIC::ExpectedQueries::Query::VERSION = '2.002';
use Moo;



has sql                     => ( is => "ro", required => 1 );
has stack_trace             => ( is => "ro", required => 1 ); # Just a string
has report_subselect_tables => (is => "ro", required => 1);

has table       => ( is => "rw" );
has operation   => ( is => "rw" );

sub BUILD { shift->analyze_sql() }

sub analyze_sql {
    my $self = shift;
    my $sql = $self->sql;

    my $table = qr/
        [^\w.]*     # optional quote
        ([\w.]+)  # capture table
    /x;
    my $select_table = qr/^ \s* select\s+ .+? \s? from \s+ $table (.*) /ixsm;

    if($sql =~ /^ \s* insert\s+ into \s+ $table /ixsm) {
        $self->table($1);
        $self->operation("insert");
    }
    elsif($sql =~ /^ \s* update\s+ $table /ixsm) {
        $self->table($1);
        $self->operation("update");
    }
    elsif($sql =~ /^ \s* delete\s+ from \s+ $table /ixsm) {
        $self->table($1);
        $self->operation("delete");
    }
    elsif($sql =~ $select_table) {
        my ($table, $rest_sql) = ($1, $2);

        if ($self->report_subselect_tables) {
            while ( uc($table) eq "SELECT" && ("select $rest_sql" =~ $select_table) ) {
                ($table, $rest_sql) = ($1, $2);
            }
            if ($table && uc($table) ne "SELECT") {
                $self->table($table);
                $self->operation("select");
            }
        }
        else {
            $self->table($table);
            $self->operation("select");
        }
    }

    return $self;
}

sub display_sql {
    my $self = shift;
    return "SQL: (" . $self->sql . ")";
}

sub display_stack_trace {
    my $self = shift;
    return "     " . $self->stack_trace;
}

1;