← Index
NYTProf Performance Profile   « block view • line view • sub view »
For ddd2.pl
  Run on Tue May 25 16:52:24 2010
Reported on Tue May 25 16:57:00 2010

File /project/perl/lib/Class/DBI/SQL/Transformer.pm
Statements Executed 67
Statement Execution Time 2.77ms
Subroutines — ordered by exclusive time
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
211562µs1.55msClass::DBI::SQL::Transformer::::_do_transformationClass::DBI::SQL::Transformer::_do_transformation
211170µs329µsClass::DBI::SQL::Transformer::::_expand_tableClass::DBI::SQL::Transformer::_expand_table
21193µs93µsClass::DBI::SQL::Transformer::::newClass::DBI::SQL::Transformer::new
21180µs1.64msClass::DBI::SQL::Transformer::::sqlClass::DBI::SQL::Transformer::sql
95280µs80µsClass::DBI::SQL::Transformer::::CORE:substClass::DBI::SQL::Transformer::CORE:subst (opcode)
62249µs49µsClass::DBI::SQL::Transformer::::CORE:substcontClass::DBI::SQL::Transformer::CORE:substcont (opcode)
21145µs45µsClass::DBI::SQL::Transformer::::argsClass::DBI::SQL::Transformer::args
21212µs12µsClass::DBI::SQL::Transformer::::CORE:matchClass::DBI::SQL::Transformer::CORE:match (opcode)
0000s0sClass::DBI::SQL::Transformer::::BEGINClass::DBI::SQL::Transformer::BEGIN
0000s0sClass::DBI::SQL::Transformer::::__ANON__[:94]Class::DBI::SQL::Transformer::__ANON__[:94]
0000s0sClass::DBI::SQL::Transformer::::_expand_joinClass::DBI::SQL::Transformer::_expand_join
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Class::DBI::SQL::Transformer;
2
3390µs126µsuse strict;
# spent 26µs making 1 call to strict::import
431.60ms1118µsuse warnings;
# spent 118µs making 1 call to warnings::import
5
6=head1 NAME
7
8Class::DBI::SQL::Transformer - Transform SQL
9
10=head1 SYNOPSIS
11
12 my $trans = $tclass->new($self, $sql, @args);
13 return $self->SUPER::transform_sql($trans->sql => $trans->args);
14
15=head1 DESCRIPTION
16
17Class::DBI hooks into the transform_sql() method in Ima::DBI to provide
18its own SQL extensions. Class::DBI::SQL::Transformer does the heavy
19lifting of these transformations.
20
21=head1 CONSTRUCTOR
22
23=head2 new
24
25 my $trans = $tclass->new($self, $sql, @args);
26
27Create a new transformer for the SQL and arguments that will be used
28with the given object (or class).
29
30=cut
31
32
# spent 93µs within Class::DBI::SQL::Transformer::new which was called 2 times, avg 46µs/call: # 2 times (93µs+0s) by Class::DBI::transform_sql at line 153 of Class/DBI.pm, avg 46µs/call
sub new {
33217µs my ($me, $caller, $sql, @args) = @_;
34283µs bless {
35 _caller => $caller,
36 _sql => $sql,
37 _args => [@args],
38 _transformed => 0,
39 } => $me;
40}
41
42=head2 sql / args
43
44 my $sql = $trans->sql;
45 my @args = $trans->args;
46
47The transformed SQL and args.
48
49=cut
50
51# TODO Document what the different transformations are
52# and factor out how they're called so that people can pick and mix the
53# ones they want and add new ones.
54
55
# spent 1.64ms (80µs+1.55) within Class::DBI::SQL::Transformer::sql which was called 2 times, avg 818µs/call: # 2 times (80µs+1.55ms) by Class::DBI::transform_sql at line 154 of Class/DBI.pm, avg 818µs/call
sub sql {
56213µs my $self = shift;
57233µs21.55ms $self->_do_transformation if !$self->{_transformed};
# spent 1.55ms making 2 calls to Class::DBI::SQL::Transformer::_do_transformation, avg 778µs/call
58232µs return $self->{_transformed_sql};
59}
60
61
# spent 45µs within Class::DBI::SQL::Transformer::args which was called 2 times, avg 22µs/call: # 2 times (45µs+0s) by Class::DBI::transform_sql at line 154 of Class/DBI.pm, avg 22µs/call
sub args {
62212µs my $self = shift;
63210µs $self->_do_transformation if !$self->{_transformed};
64235µs return @{ $self->{_transformed_args} };
65}
66
67
# spent 329µs (170+159) within Class::DBI::SQL::Transformer::_expand_table which was called 2 times, avg 164µs/call: # 2 times (170µs+159µs) by Class::DBI::SQL::Transformer::_do_transformation at line 114, avg 164µs/call
sub _expand_table {
6829µs my $self = shift;
69222µs my ($class, $alias) = split /=/, shift, 2;
70212µs my $caller = $self->{_caller};
71262µs2159µs my $table = $class ? $class->table : $caller->table;
# spent 159µs making 2 calls to Class::DBI::table, avg 80µs/call
72222µs $self->{cmap}{ $alias || $table } = $class || ref $caller || $caller;
73210µs ($alias ||= "") &&= " $alias";
74237µs return $table . $alias;
75}
76
77sub _expand_join {
78 my $self = shift;
79 my $joins = shift;
80 my @table = split /\s+/, $joins;
81
82 my $caller = $self->{_caller};
83 my %tojoin = map { $table[$_] => $table[ $_ + 1 ] } 0 .. $#table - 1;
84 my @sql;
85 while (my ($t1, $t2) = each %tojoin) {
86 my ($c1, $c2) = map $self->{cmap}{$_}
87 || $caller->_croak("Don't understand table '$_' in JOIN"), ($t1, $t2);
88
89 my $join_col = sub {
90 my ($c1, $c2) = @_;
91 my $meta = $c1->meta_info('has_a');
92 my ($col) = grep $meta->{$_}->foreign_class eq $c2, keys %$meta;
93 $col;
94 };
95
96 my $col = $join_col->($c1 => $c2) || do {
97 ($c1, $c2) = ($c2, $c1);
98 ($t1, $t2) = ($t2, $t1);
99 $join_col->($c1 => $c2);
100 };
101
102 $caller->_croak("Don't know how to join $c1 to $c2") unless $col;
103 push @sql, sprintf " %s.%s = %s.%s ", $t1, $col, $t2, $c2->primary_column;
104 }
105 return join " AND ", @sql;
106}
107
108
# spent 1.55ms (562µs+993µs) within Class::DBI::SQL::Transformer::_do_transformation which was called 2 times, avg 778µs/call: # 2 times (562µs+993µs) by Class::DBI::SQL::Transformer::sql at line 57, avg 778µs/call
sub _do_transformation {
109210µs my $me = shift;
110214µs my $sql = $me->{_sql};
111212µs my @args = @{ $me->{_args} };
112211µs my $caller = $me->{_caller};
113
1144181µs8396µs $sql =~ s/__TABLE\(?(.*?)\)?__/$me->_expand_table($1)/eg;
# spent 329µs making 2 calls to Class::DBI::SQL::Transformer::_expand_table, avg 164µs/call # spent 35µs making 4 calls to Class::DBI::SQL::Transformer::CORE:substcont, avg 9µs/call # spent 32µs making 2 calls to Class::DBI::SQL::Transformer::CORE:subst, avg 16µs/call
115253µs212µs $sql =~ s/__JOIN\((.*?)\)__/$me->_expand_join($1)/eg;
# spent 12µs making 2 calls to Class::DBI::SQL::Transformer::CORE:subst, avg 6µs/call
1162109µs5314µs $sql =~ s/__ESSENTIAL__/join ", ", $caller->_essential/eg;
# spent 287µs making 1 call to Class::DBI::_essential # spent 14µs making 2 calls to Class::DBI::SQL::Transformer::CORE:substcont, avg 7µs/call # spent 13µs making 2 calls to Class::DBI::SQL::Transformer::CORE:subst, avg 7µs/call
117 $sql =~
118238µs212µs s/__ESSENTIAL\((.*?)\)__/join ", ", map "$1.$_", $caller->_essential/eg;
# spent 12µs making 2 calls to Class::DBI::SQL::Transformer::CORE:subst, avg 6µs/call
119268µs212µs if ($sql =~ /__IDENTIFIER__/) {
# spent 12µs making 2 calls to Class::DBI::SQL::Transformer::CORE:match, avg 6µs/call
120147µs1144µs my $key_sql = join " AND ", map "$_=?", $caller->primary_columns;
# spent 144µs making 1 call to Class::DBI::primary_column
121130µs111µs $sql =~ s/__IDENTIFIER__/$key_sql/g;
# spent 11µs making 1 call to Class::DBI::SQL::Transformer::CORE:subst
122 }
123
124215µs $me->{_transformed_sql} = $sql;
125218µs $me->{_transformed_args} = [@args];
126210µs $me->{_transformed} = 1;
127235µs return 1;
128}
129
130114µs1;
131
# spent 12µs within Class::DBI::SQL::Transformer::CORE:match which was called 2 times, avg 6µs/call: # 2 times (12µs+0s) by Class::DBI::SQL::Transformer::_do_transformation at line 119 of Class/DBI/SQL/Transformer.pm, avg 6µs/call
sub Class::DBI::SQL::Transformer::CORE:match; # xsub
# spent 80µs within Class::DBI::SQL::Transformer::CORE:subst which was called 9 times, avg 9µs/call: # 2 times (32µs+0s) by Class::DBI::SQL::Transformer::_do_transformation at line 114 of Class/DBI/SQL/Transformer.pm, avg 16µs/call # 2 times (13µs+0s) by Class::DBI::SQL::Transformer::_do_transformation at line 116 of Class/DBI/SQL/Transformer.pm, avg 7µs/call # 2 times (12µs+0s) by Class::DBI::SQL::Transformer::_do_transformation at line 118 of Class/DBI/SQL/Transformer.pm, avg 6µs/call # 2 times (12µs+0s) by Class::DBI::SQL::Transformer::_do_transformation at line 115 of Class/DBI/SQL/Transformer.pm, avg 6µs/call # once (11µs+0s) by Class::DBI::SQL::Transformer::_do_transformation at line 121 of Class/DBI/SQL/Transformer.pm
sub Class::DBI::SQL::Transformer::CORE:subst; # xsub
# spent 49µs within Class::DBI::SQL::Transformer::CORE:substcont which was called 6 times, avg 8µs/call: # 4 times (35µs+0s) by Class::DBI::SQL::Transformer::_do_transformation at line 114 of Class/DBI/SQL/Transformer.pm, avg 9µs/call # 2 times (14µs+0s) by Class::DBI::SQL::Transformer::_do_transformation at line 116 of Class/DBI/SQL/Transformer.pm, avg 7µs/call
sub Class::DBI::SQL::Transformer::CORE:substcont; # xsub