Profile of SQL/Translator/Parser/DBIx/Class.pm

← Index
NYTProf Performance Profile «block view•line view•sub view»
For xt/tapper-mcp-scheduler-with-db-longrun.t
Run on Tue May 22 17:18:39 2012
Reported on Tue May 22 17:23:18 2012

Filename | /2home/ss5/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/SQL/Translator/Parser/DBIx/Class.pm
Statements | Executed 4696 statements in 12.4ms

Subroutines
Calls | P | F | Exclusive Time | Inclusive Time | Subroutine
--- | --- | --- | --- | --- | ---
4 | 1 | 1 | 9.90ms | 118ms | SQL::Translator::Parser::DBIx::Class::::parseSQL::Translator::Parser::DBIx::Class::parse
85 | 3 | 1 | 1.04ms | 1.13ms | SQL::Translator::Parser::DBIx::Class::::_resolve_depsSQL::Translator::Parser::DBIx::Class::_resolve_deps (recurses: max depth 2, inclusive time 715µs)
155 | 3 | 1 | 236µs | 236µs | SQL::Translator::Parser::DBIx::Class::::CORE:matchSQL::Translator::Parser::DBIx::Class::CORE:match (opcode)
175 | 8 | 1 | 230µs | 274µs | SQL::Translator::Parser::DBIx::Class::::CORE:sortSQL::Translator::Parser::DBIx::Class::CORE:sort (opcode)
60 | 1 | 1 | 204µs | 3.92ms | SQL::Translator::Parser::DBIx::Class::::__ANON__[:154]SQL::Translator::Parser::DBIx::Class::__ANON__[:154]
1 | 1 | 1 | 19µs | 24µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@9SQL::Translator::Parser::DBIx::Class::BEGIN@9
1 | 1 | 1 | 12µs | 112µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@17SQL::Translator::Parser::DBIx::Class::BEGIN@17
1 | 1 | 1 | 12µs | 34µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@385SQL::Translator::Parser::DBIx::Class::BEGIN@385
1 | 1 | 1 | 11µs | 28µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@10SQL::Translator::Parser::DBIx::Class::BEGIN@10
1 | 1 | 1 | 11µs | 42µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@20SQL::Translator::Parser::DBIx::Class::BEGIN@20
1 | 1 | 1 | 11µs | 90µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@19SQL::Translator::Parser::DBIx::Class::BEGIN@19
1 | 1 | 1 | 10µs | 39µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@15SQL::Translator::Parser::DBIx::Class::BEGIN@15
1 | 1 | 1 | 10µs | 43µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@16SQL::Translator::Parser::DBIx::Class::BEGIN@16
1 | 1 | 1 | 9µs | 86µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@23SQL::Translator::Parser::DBIx::Class::BEGIN@23
1 | 1 | 1 | 8µs | 8µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@18SQL::Translator::Parser::DBIx::Class::BEGIN@18
1 | 1 | 1 | 8µs | 253µs | SQL::Translator::Parser::DBIx::Class::::BEGIN@21SQL::Translator::Parser::DBIx::Class::BEGIN@21

Call graph for these subroutines as a Graphviz dot language file.

Line | State ments | Time on line | Calls | Time in subs | Code
--- | --- | --- | --- | --- | ---
1 |  |  |  |  | package SQL::Translator::Parser::DBIx::Class;
2 |  |  |  |  |
3 |  |  |  |  | # AUTHOR: Jess Robinson
4 |  |  |  |  |
5 |  |  |  |  | # Some mistakes the fault of Matt S Trout
6 |  |  |  |  |
7 |  |  |  |  | # Others the fault of Ash Berlin
8 |  |  |  |  |
9 | 3 | 24µs | 2 | 30µs | # spent 24µs (19+6) within SQL::Translator::Parser::DBIx::Class::BEGIN@9 which was called: # once (19µs+6µs) by SQL::Translator::load at line 9 use strict; # spent 24µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@9 # spent 6µs making 1 call to strict::import
10 | 3 | 61µs | 2 | 46µs | # spent 28µs (11+17) within SQL::Translator::Parser::DBIx::Class::BEGIN@10 which was called: # once (11µs+17µs) by SQL::Translator::load at line 10 use warnings; # spent 28µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@10 # spent 17µs making 1 call to warnings::import
11 | 1 | 600ns |  |  | our ($DEBUG, $VERSION, @EXPORT_OK);
12 | 1 | 500ns |  |  | $VERSION = '1.10';
13 | 1 | 300ns |  |  | $DEBUG = 0 unless defined $DEBUG;
14 |  |  |  |  |
15 | 3 | 25µs | 2 | 67µs | # spent 39µs (10+29) within SQL::Translator::Parser::DBIx::Class::BEGIN@15 which was called: # once (10µs+29µs) by SQL::Translator::load at line 15 use Exporter; # spent 39µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@15 # spent 29µs making 1 call to Exporter::import
16 | 3 | 23µs | 2 | 76µs | # spent 43µs (10+33) within SQL::Translator::Parser::DBIx::Class::BEGIN@16 which was called: # once (10µs+33µs) by SQL::Translator::load at line 16 use SQL::Translator::Utils qw(debug normalize_name); # spent 43µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@16 # spent 33µs making 1 call to Exporter::import
17 | 3 | 26µs | 2 | 211µs | # spent 112µs (12+99) within SQL::Translator::Parser::DBIx::Class::BEGIN@17 which was called: # once (12µs+99µs) by SQL::Translator::load at line 17 use DBIx::Class::Carp qw/^SQL::Translator|^DBIx::Class|^Try::Tiny/; # spent 112µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@17 # spent 99µs making 1 call to DBIx::Class::Carp::import
18 | 3 | 22µs | 1 | 8µs | # spent 8µs within SQL::Translator::Parser::DBIx::Class::BEGIN@18 which was called: # once (8µs+0s) by SQL::Translator::load at line 18 use DBIx::Class::Exception; # spent 8µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@18
19 | 3 | 27µs | 2 | 169µs | # spent 90µs (11+79) within SQL::Translator::Parser::DBIx::Class::BEGIN@19 which was called: # once (11µs+79µs) by SQL::Translator::load at line 19 use Scalar::Util qw/weaken blessed/; # spent 90µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@19 # spent 79µs making 1 call to Exporter::import
20 | 3 | 22µs | 2 | 74µs | # spent 42µs (11+31) within SQL::Translator::Parser::DBIx::Class::BEGIN@20 which was called: # once (11µs+31µs) by SQL::Translator::load at line 20 use Try::Tiny; # spent 42µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@20 # spent 31µs making 1 call to Exporter::import
21 | 3 | 20µs | 2 | 499µs | # spent 253µs (8+246) within SQL::Translator::Parser::DBIx::Class::BEGIN@21 which was called: # once (8µs+246µs) by SQL::Translator::load at line 21 use namespace::clean; # spent 253µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@21 # spent 246µs making 1 call to namespace::clean::import
22 |  |  |  |  |
23 | 3 | 1.44ms | 2 | 163µs | # spent 86µs (9+77) within SQL::Translator::Parser::DBIx::Class::BEGIN@23 which was called: # once (9µs+77µs) by SQL::Translator::load at line 23 use base qw(Exporter); # spent 86µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@23 # spent 77µs making 1 call to base::import
24 |  |  |  |  |
25 | 1 | 800ns |  |  | @EXPORT_OK = qw(parse);
26 |  |  |  |  |
27 |  |  |  |  | # -------------------------------------------------------------------
28 |  |  |  |  | # parse($tr, $data)
29 |  |  |  |  | #
30 |  |  |  |  | # setting parser_args => { add_fk_index => 0 } will prevent
31 |  |  |  |  | # the auto-generation of an index for each FK.
32 |  |  |  |  | #
33 |  |  |  |  | # Note that $data, in the case of this parser, is not useful.
34 |  |  |  |  | # We're working with DBIx::Class Schemas, not data streams.
35 |  |  |  |  | # -------------------------------------------------------------------
36 |  |  |  |  | # spent 118ms (9.90+108) within SQL::Translator::Parser::DBIx::Class::parse which was called 4 times, avg 29.5ms/call: # 4 times (9.90ms+108ms) by SQL::Translator::translate at line 509 of SQL/Translator.pm, avg 29.5ms/call sub parse {
37 |  |  |  |  | # this is a hack to prevent schema leaks due to a retarded SQLT implementation
38 |  |  |  |  | # DO NOT REMOVE (until SQLT2 is out, the all of this will be rewritten anyway)
39 | 105 | 492µs | 4 | 9µs | weaken $_[1] if ref ($_[1]); # spent 9µs making 4 calls to Scalar::Util::weaken, avg 2µs/call
40 |  |  |  |  |
41 |  |  |  |  | my ($tr, $data) = @_;
42 |  |  | 4 | 30µs | my $args = $tr->parser_args; # spent 30µs making 4 calls to SQL::Translator::parser_args, avg 8µs/call
43 |  |  |  |  | my $dbicschema = $args->{'DBIx::Class::Schema'} || $args->{"DBIx::Schema"} ||$data;
44 |  |  |  |  | $dbicschema ||= $args->{'package'};
45 |  |  |  |  | my $limit_sources = $args->{'sources'};
46 |  |  |  |  |
47 |  |  |  |  | DBIx::Class::Exception->throw('No DBIx::Class::Schema') unless ($dbicschema);
48 |  |  |  |  | if (!ref $dbicschema) {
49 |  |  |  |  | eval "require $dbicschema"
50 |  |  |  |  | or DBIx::Class::Exception->throw("Can't load $dbicschema: $@");
51 |  |  |  |  | }
52 |  |  |  |  |
53 |  |  | 4 | 305µs | my $schema = $tr->schema; # spent 305µs making 4 calls to SQL::Translator::schema, avg 76µs/call
54 |  |  |  |  | my $table_no = 0;
55 |  |  |  |  |
56 |  |  | 12 | 40µs | $schema->name( ref($dbicschema) . " v" . ($dbicschema->schema_version || '1.x')) # spent 22µs making 4 calls to DBIx::Class::Schema::schema_version, avg 5µs/call # spent 18µs making 8 calls to SQL::Translator::Schema::name, avg 2µs/call
57 |  |  |  |  | unless ($schema->name);
58 |  |  |  |  |
59 |  |  | 8 | 134µs | my @monikers = sort $dbicschema->sources; # spent 116µs making 4 calls to DBIx::Class::Schema::sources, avg 29µs/call # spent 17µs making 4 calls to SQL::Translator::Parser::DBIx::Class::CORE:sort, avg 4µs/call
60 |  |  |  |  | if ($limit_sources) {
61 |  |  |  |  | my $ref = ref $limit_sources || '';
62 |  |  |  |  | $dbicschema->throw_exception ("'sources' parameter must be an array or hash ref")
63 |  |  |  |  | unless( $ref eq 'ARRAY' || ref eq 'HASH' );
64 |  |  |  |  |
65 |  |  |  |  | # limit monikers to those specified in
66 |  |  |  |  | my $sources;
67 |  |  |  |  | if ($ref eq 'ARRAY') {
68 |  |  |  |  | $sources->{$_} = 1 for (@$limit_sources);
69 |  |  |  |  | } else {
70 |  |  |  |  | $sources = $limit_sources;
71 |  |  |  |  | }
72 |  |  |  |  | @monikers = grep { $sources->{$_} } @monikers;
73 |  |  |  |  | }
74 |  |  |  |  |
75 |  |  |  |  |
76 |  |  |  |  | my(%table_monikers, %view_monikers);
77 |  |  |  |  | for my $moniker (@monikers){
78 | 74 | 202µs | 37 | 436µs | my $source = $dbicschema->source($moniker); # spent 436µs making 37 calls to DBIx::Class::Schema::source, avg 12µs/call
79 | 4 | 10µs | 39 | 40µs | if ( $source->isa('DBIx::Class::ResultSource::Table') ) { # spent 40µs making 39 calls to UNIVERSAL::isa, avg 1µs/call
80 |  |  |  |  | $table_monikers{$moniker}++;
81 |  |  |  |  | } elsif( $source->isa('DBIx::Class::ResultSource::View') ){
82 |  |  | 1 | 2µs | next if $source->is_virtual; # spent 2µs making 1 call to DBIx::Class::ResultSource::View::is_virtual
83 |  |  |  |  | $view_monikers{$moniker}++;
84 |  |  |  |  | }
85 |  |  |  |  | }
86 |  |  |  |  |
87 |  |  |  |  | my %tables;
88 |  |  | 4 | 7µs | foreach my $moniker (sort keys %table_monikers) # spent 7µs making 4 calls to SQL::Translator::Parser::DBIx::Class::CORE:sort, avg 2µs/call
89 |  |  |  |  | {
90 | 525 | 1.23ms | 35 | 518µs | my $source = $dbicschema->source($moniker); # spent 518µs making 35 calls to DBIx::Class::Schema::source, avg 15µs/call
91 |  |  | 1 | 3µs | my $table_name = $source->name; # spent 3µs making 1 call to DBIx::Class::ResultSource::Table::name
92 |  |  |  |  |
93 |  |  |  |  | # FIXME - this isn't the right way to do it, but sqlt does not
94 |  |  |  |  | # support quoting properly to be signaled about this
95 |  |  |  |  | $table_name = $$table_name if ref $table_name eq 'SCALAR';
96 |  |  |  |  |
97 |  |  |  |  | # It's possible to have multiple DBIC sources using the same table
98 |  |  |  |  | next if $tables{$table_name};
99 |  |  |  |  |
100 |  |  |  |  | $tables{$table_name}{source} = $source;
101 |  |  | 35 | 2.39ms | my $table = $tables{$table_name}{object} = SQL::Translator::Schema::Table->new( # spent 2.39ms making 35 calls to SQL::Translator::Schema::Table::new, avg 68µs/call
102 |  |  |  |  | name => $table_name,
103 |  |  |  |  | type => 'TABLE',
104 |  |  |  |  | );
105 |  |  | 35 | 137µs | foreach my $col ($source->columns) # spent 137µs making 35 calls to DBIx::Class::ResultSource::columns, avg 4µs/call
106 |  |  |  |  | {
107 |  |  |  |  | # assuming column_info in dbic is the same as DBI (?)
108 |  |  |  |  | # data_type is a number, column_type is text?
109 |  |  |  |  | my %colinfo = (
110 |  |  |  |  | name => $col,
111 |  |  |  |  | size => 0,
112 |  |  |  |  | is_auto_increment => 0,
113 |  |  |  |  | is_foreign_key => 0,
114 |  |  |  |  | is_nullable => 0,
115 | 720 | 2.55ms | 240 | 2.26ms | %{$source->column_info($col)} # spent 2.26ms making 240 calls to DBIx::Class::ResultSource::column_info, avg 9µs/call
116 |  |  |  |  | );
117 |  |  |  |  | if ($colinfo{is_nullable}) {
118 |  |  |  |  | $colinfo{default} = '' unless exists $colinfo{default};
119 |  |  |  |  | }
120 | 1 | 395µs | 480 | 46.2ms | my $f = $table->add_field(%colinfo) # spent 45.2ms making 240 calls to SQL::Translator::Schema::Table::add_field, avg 188µs/call # spent 968µs making 240 calls to SQL::Translator::Schema::Field::__ANON__[SQL/Translator/Schema/Field.pm:58], avg 4µs/call
121 |  |  |  |  | || $dbicschema->throw_exception ($table->error);
122 |  |  |  |  | }
123 |  |  |  |  |
124 |  |  | 35 | 137µs | my @primary = $source->primary_columns; # spent 137µs making 35 calls to DBIx::Class::ResultSource::primary_columns, avg 4µs/call
125 |  |  |  |  |
126 |  |  | 35 | 8.85ms | $table->primary_key(@primary) if @primary; # spent 8.85ms making 35 calls to SQL::Translator::Schema::Table::primary_key, avg 253µs/call
127 |  |  |  |  |
128 |  |  | 35 | 165µs | my %unique_constraints = $source->unique_constraints; # spent 165µs making 35 calls to DBIx::Class::ResultSource::unique_constraints, avg 5µs/call
129 |  |  | 35 | 40µs | foreach my $uniq (sort keys %unique_constraints) { # spent 40µs making 35 calls to SQL::Translator::Parser::DBIx::Class::CORE:sort, avg 1µs/call
130 | 38 | 120µs | 41 | 623µs | if (!$source->_compare_relationship_keys($unique_constraints{$uniq}, \@primary)) { # spent 383µs making 3 calls to SQL::Translator::Schema::Table::add_constraint, avg 128µs/call # spent 240µs making 38 calls to DBIx::Class::ResultSource::_compare_relationship_keys, avg 6µs/call
131 |  |  |  |  | $table->add_constraint(
132 |  |  |  |  | type => 'unique',
133 |  |  |  |  | name => $uniq,
134 |  |  |  |  | fields => $unique_constraints{$uniq}
135 |  |  |  |  | );
136 |  |  |  |  | }
137 |  |  |  |  | }
138 |  |  |  |  |
139 |  |  | 35 | 122µs | my @rels = $source->relationships(); # spent 122µs making 35 calls to DBIx::Class::ResultSource::relationships, avg 3µs/call
140 |  |  |  |  |
141 |  |  |  |  | my %created_FK_rels;
142 |  |  |  |  |
143 |  |  |  |  | # global add_fk_index set in parser_args
144 |  |  |  |  | my $add_fk_index = (exists $args->{add_fk_index} && ! $args->{add_fk_index}) ? 0 : 1;
145 |  |  |  |  |
146 |  |  | 35 | 30µs | foreach my $rel (sort @rels) # spent 30µs making 35 calls to SQL::Translator::Parser::DBIx::Class::CORE:sort, avg 866ns/call
147 |  |  |  |  | {
148 |  |  |  |  |
149 | 1140 | 2.44ms | 60 | 147µs | my $rel_info = $source->relationship_info($rel); # spent 147µs making 60 calls to DBIx::Class::ResultSource::relationship_info, avg 2µs/call
150 |  |  |  |  |
151 |  |  |  |  | # Ignore any rel cond that isn't a straight hash
152 |  |  |  |  | next unless ref $rel_info->{cond} eq 'HASH';
153 |  |  |  |  |
154 | 60 | 186µs | 120 | 8.32ms | # spent 3.92ms (204µs+3.71) within SQL::Translator::Parser::DBIx::Class::__ANON__[/2home/ss5/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/SQL/Translator/Parser/DBIx/Class.pm:154] which was called 60 times, avg 65µs/call: # 60 times (204µs+3.71ms) by Try::Tiny::try at line 71 of Try/Tiny.pm, avg 65µs/call my $relsource = try { $source->related_source($rel) }; # spent 4.61ms making 60 calls to Try::Tiny::try, avg 77µs/call # spent 3.71ms making 60 calls to DBIx::Class::ResultSource::related_source, avg 62µs/call
155 |  |  |  |  | unless ($relsource) {
156 |  |  |  |  | warn "Ignoring relationship '$rel' - related resultsource '$rel_info->{class}' is not registered with this schema\n";
157 |  |  |  |  | next;
158 |  |  |  |  | };
159 |  |  |  |  |
160 |  |  |  |  | # related sources might be excluded via a {sources} filter or might be views
161 |  |  | 1 | 2µs | next unless exists $table_monikers{$relsource->source_name}; # spent 2µs making 1 call to DBIx::Class::ResultSource::Table::source_name
162 |  |  |  |  |
163 |  |  | 1 | 900ns | my $rel_table = $relsource->name; # spent 900ns making 1 call to DBIx::Class::ResultSource::Table::name
164 |  |  |  |  |
165 |  |  |  |  | # FIXME - this isn't the right way to do it, but sqlt does not
166 |  |  |  |  | # support quoting properly to be signaled about this
167 |  |  |  |  | $rel_table = $$rel_table if ref $rel_table eq 'SCALAR';
168 |  |  |  |  |
169 |  |  | 60 | 25.0ms | my $reverse_rels = $source->reverse_relationship_info($rel); # spent 25.0ms making 60 calls to DBIx::Class::ResultSource::reverse_relationship_info, avg 416µs/call
170 |  |  |  |  | my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
171 |  |  |  |  |
172 |  |  |  |  | # Force the order of @cond to match the order of ->add_columns
173 |  |  |  |  | my $idx;
174 |  |  | 60 | 276µs | my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $relsource->columns; # spent 276µs making 60 calls to DBIx::Class::ResultSource::columns, avg 5µs/call
175 |  |  | 60 | 25µs | my @cond = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } keys(%{$rel_info->{cond}}); # spent 25µs making 60 calls to SQL::Translator::Parser::DBIx::Class::CORE:sort, avg 413ns/call
176 |  |  |  |  |
177 |  |  |  |  | # Get the key information, mapping off the foreign/self markers
178 | 60 | 242µs | 60 | 128µs | my @refkeys = map {/^\w+\.(\w+)$/} @cond; # spent 128µs making 60 calls to SQL::Translator::Parser::DBIx::Class::CORE:match, avg 2µs/call
179 | 60 | 207µs | 60 | 83µs | my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond; # spent 83µs making 60 calls to SQL::Translator::Parser::DBIx::Class::CORE:match, avg 1µs/call
180 |  |  |  |  |
181 |  |  |  |  | # determine if this relationship is a self.fk => foreign.pk (i.e. belongs_to)
182 |  |  |  |  | my $fk_constraint;
183 |  |  |  |  |
184 |  |  |  |  | #first it can be specified explicitly
185 | 6 | 12µs |  |  | if ( exists $rel_info->{attrs}{is_foreign_key_constraint} ) {
186 |  |  |  |  | $fk_constraint = $rel_info->{attrs}{is_foreign_key_constraint};
187 |  |  |  |  | }
188 |  |  |  |  | # it can not be multi
189 |  |  |  |  | elsif ( $rel_info->{attrs}{accessor}
190 |  |  |  |  | && $rel_info->{attrs}{accessor} eq 'multi' ) {
191 |  |  |  |  | $fk_constraint = 0;
192 |  |  |  |  | }
193 |  |  |  |  | # if indeed single, check if all self.columns are our primary keys.
194 |  |  |  |  | # this is supposed to indicate a has_one/might_have...
195 |  |  |  |  | # where's the introspection!!?? :)
196 |  |  |  |  | else {
197 |  |  | 6 | 27µs | $fk_constraint = not $source->_compare_relationship_keys(\@keys, \@primary); # spent 27µs making 6 calls to DBIx::Class::ResultSource::_compare_relationship_keys, avg 5µs/call
198 |  |  |  |  | }
199 |  |  |  |  |
200 |  |  |  |  | my $cascade;
201 |  |  |  |  | for my $c (qw/delete update/) {
202 | 120 | 244µs |  |  | if (exists $rel_info->{attrs}{"on_$c"}) {
203 |  |  |  |  | if ($fk_constraint) {
204 |  |  |  |  | $cascade->{$c} = $rel_info->{attrs}{"on_$c"};
205 |  |  |  |  | }
206 |  |  |  |  | elsif ( $rel_info->{attrs}{"on_$c"} ) {
207 |  |  |  |  | carp "SQLT attribute 'on_$c' was supplied for relationship '$moniker/$rel', which does not appear to be a foreign constraint. "
208 |  |  |  |  | . "If you are sure that SQLT must generate a constraint for this relationship, add 'is_foreign_key_constraint => 1' to the attributes.\n";
209 |  |  |  |  | }
210 |  |  |  |  | }
211 |  |  |  |  | elsif (defined $otherrelationship and $otherrelationship->{attrs}{$c eq 'update' ? 'cascade_copy' : 'cascade_delete'}) {
212 |  |  |  |  | $cascade->{$c} = 'CASCADE';
213 |  |  |  |  | }
214 |  |  |  |  | }
215 |  |  |  |  |
216 | 147 | 212µs |  |  | if($rel_table) {
217 |  |  |  |  | # Constraints are added only if applicable
218 |  |  |  |  | next unless $fk_constraint;
219 |  |  |  |  |
220 |  |  |  |  | # Make sure we dont create the same foreign key constraint twice
221 |  |  | 29 | 8µs | my $key_test = join("\x00", sort @keys); # spent 8µs making 29 calls to SQL::Translator::Parser::DBIx::Class::CORE:sort, avg 279ns/call
222 |  |  |  |  | next if $created_FK_rels{$rel_table}->{$key_test};
223 |  |  |  |  |
224 | 203 | 399µs |  |  | if (scalar(@keys)) {
225 |  |  |  |  | $created_FK_rels{$rel_table}->{$key_test} = 1;
226 |  |  |  |  |
227 |  |  |  |  | my $is_deferrable = $rel_info->{attrs}{is_deferrable};
228 |  |  |  |  |
229 |  |  |  |  | # calculate dependencies: do not consider deferrable constraints and
230 |  |  |  |  | # self-references for dependency calculations
231 |  |  |  |  | if (! $is_deferrable and $rel_table ne $table_name) {
232 |  |  |  |  | $tables{$table_name}{foreign_table_deps}{$rel_table}++;
233 |  |  |  |  | }
234 |  |  |  |  |
235 |  |  |  |  | $table->add_constraint(
236 |  |  | 29 | 5.64ms | type => 'foreign_key', # spent 5.64ms making 29 calls to SQL::Translator::Schema::Table::add_constraint, avg 195µs/call
237 |  |  |  |  | name => join('_', $table_name, 'fk', @keys),
238 |  |  |  |  | fields => \@keys,
239 |  |  |  |  | reference_fields => \@refkeys,
240 |  |  |  |  | reference_table => $rel_table,
241 |  |  |  |  | on_delete => uc ($cascade->{delete} || ''),
242 |  |  |  |  | on_update => uc ($cascade->{update} || ''),
243 |  |  |  |  | (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()),
244 |  |  |  |  | );
245 |  |  |  |  |
246 |  |  |  |  | # global parser_args add_fk_index param can be overridden on the rel def
247 |  |  |  |  | my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index;
248 |  |  |  |  |
249 |  |  |  |  | # Check that we do not create an index identical to the PK index
250 |  |  |  |  | # (some RDBMS croak on this, and it generally doesn't make much sense)
251 |  |  |  |  | # NOTE: we do not sort the key columns because the order of
252 |  |  |  |  | # columns is important for indexes and two indexes with the
253 |  |  |  |  | # same cols but different order are allowed and sometimes
254 |  |  |  |  | # needed
255 |  |  |  |  | next if join("\x00", @keys) eq join("\x00", @primary);
256 |  |  |  |  |
257 |  |  | 29 | 3.74ms | if ($add_fk_index_rel) { # spent 3.74ms making 29 calls to SQL::Translator::Schema::Table::add_index, avg 129µs/call
258 |  |  |  |  | my $index = $table->add_index(
259 |  |  |  |  | name => join('_', $table_name, 'idx', @keys),
260 |  |  |  |  | fields => \@keys,
261 |  |  |  |  | type => 'NORMAL',
262 |  |  |  |  | );
263 |  |  |  |  | }
264 |  |  |  |  | }
265 |  |  |  |  | }
266 |  |  |  |  | }
267 |  |  |  |  |
268 |  |  |  |  | }
269 |  |  |  |  |
270 |  |  |  |  | # attach the tables to the schema in dependency order
271 |  |  | 35 | 1.07ms | my $dependencies = { # spent 1.07ms making 35 calls to SQL::Translator::Parser::DBIx::Class::_resolve_deps, avg 31µs/call
272 | 35 | 54µs |  |  | map { $_ => _resolve_deps ($_, \%tables) } (keys %tables)
273 |  |  |  |  | };
274 |  |  |  |  |
275 |  |  | 4 | 67µs | for my $table (sort # spent 67µs making 4 calls to SQL::Translator::Parser::DBIx::Class::CORE:sort, avg 17µs/call
276 |  |  |  |  | {
277 |  |  |  |  | keys %{$dependencies->{$a} || {} } <=> keys %{ $dependencies->{$b} || {} }
278 |  |  |  |  | ||
279 |  |  |  |  | $a cmp $b
280 |  |  |  |  | }
281 |  |  |  |  | (keys %tables)
282 |  |  |  |  | ) {
283 | 105 | 233µs | 35 | 900µs | $schema->add_table ($tables{$table}{object}); # spent 900µs making 35 calls to SQL::Translator::Schema::add_table, avg 26µs/call
284 |  |  | 35 | 3.08ms | $tables{$table}{source} -> _invoke_sqlt_deploy_hook( $tables{$table}{object} ); # spent 3.08ms making 35 calls to DBIx::Class::ResultSource::_invoke_sqlt_deploy_hook, avg 88µs/call
285 |  |  |  |  |
286 |  |  |  |  | # the hook might have already removed the table
287 | 1 | 93µs | 105 | 291µs | if ($schema->get_table($table) && $table =~ /^ \s* \( \s* SELECT \s+/ix) { # spent 165µs making 35 calls to SQL::Translator::Schema::Table::__ANON__[SQL/Translator/Schema/Table.pm:59], avg 5µs/call # spent 101µs making 35 calls to SQL::Translator::Schema::get_table, avg 3µs/call # spent 25µs making 35 calls to SQL::Translator::Parser::DBIx::Class::CORE:match, avg 717ns/call
288 |  |  |  |  | carp <<'EOW';
289 |  |  |  |  |
290 |  |  |  |  | Custom SQL through ->name(\'( SELECT ...') is DEPRECATED, for more details see
291 |  |  |  |  | "Arbitrary SQL through a custom ResultSource" in DBIx::Class::Manual::Cookbook
292 |  |  |  |  | or http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod
293 |  |  |  |  |
294 |  |  |  |  | EOW
295 |  |  |  |  |
296 |  |  |  |  | # remove the table as there is no way someone might want to
297 |  |  |  |  | # actually deploy this
298 |  |  |  |  | $schema->drop_table ($table);
299 |  |  |  |  | }
300 |  |  |  |  | }
301 |  |  |  |  |
302 |  |  |  |  | my %views;
303 | 2 | 4µs | 2 | 32µs | my @views = map { $dbicschema->source($_) } keys %view_monikers; # spent 32µs making 2 calls to DBIx::Class::Schema::source, avg 16µs/call
304 |  |  |  |  |
305 |  |  | 4 | 84µs | my $view_dependencies = { # spent 61µs making 2 calls to SQL::Translator::Parser::DBIx::Class::_resolve_deps, avg 30µs/call # spent 23µs making 2 calls to DBIx::Class::Schema::source, avg 12µs/call
306 |  |  |  |  | map {
307 | 2 | 6µs |  |  | $_ => _resolve_deps( $dbicschema->source($_), \%view_monikers )
308 |  |  |  |  | } ( keys %view_monikers )
309 |  |  |  |  | };
310 |  |  |  |  |
311 |  |  | 1 | 19µs | my @view_sources = # spent 19µs making 1 call to DBIx::Class::ResultSource::source_name
312 |  |  |  |  | sort {
313 |  |  | 3 | 32µs | keys %{ $view_dependencies->{ $a->source_name } || {} } <=> # spent 24µs making 2 calls to DBIx::Class::Schema::source, avg 12µs/call # spent 8µs making 1 call to DBIx::Class::ResultSource::source_name
314 | 2 | 4µs | 2 | 14µs | keys %{ $view_dependencies->{ $b->source_name } || {} } # spent 14µs making 2 calls to DBIx::Class::ResultSource::source_name, avg 7µs/call
315 |  |  |  |  | || $a->source_name cmp $b->source_name
316 |  |  |  |  | }
317 | 2 | 3µs | 4 | 80µs | map { $dbicschema->source($_) } # spent 80µs making 4 calls to SQL::Translator::Parser::DBIx::Class::CORE:sort, avg 20µs/call
318 |  |  |  |  | keys %view_monikers;
319 |  |  |  |  |
320 |  |  |  |  | foreach my $source (@view_sources)
321 |  |  |  |  | {
322 | 14 | 44µs | 2 | 18µs | my $view_name = $source->name; # spent 18µs making 2 calls to DBIx::Class::ResultSource::name, avg 9µs/call
323 |  |  |  |  |
324 |  |  |  |  | # FIXME - this isn't the right way to do it, but sqlt does not
325 |  |  |  |  | # support quoting properly to be signaled about this
326 |  |  |  |  | $view_name = $$view_name if ref $view_name eq 'SCALAR';
327 |  |  |  |  |
328 |  |  |  |  | # Skip custom query sources
329 |  |  |  |  | next if ref $view_name;
330 |  |  |  |  |
331 |  |  |  |  | # Its possible to have multiple DBIC source using same table
332 |  |  |  |  | next if $views{$view_name}++;
333 |  |  |  |  |
334 |  |  | 1 | 1µs | $dbicschema->throw_exception ("view $view_name is missing a view_definition") # spent 1µs making 1 call to DBIx::Class::ResultSource::View::view_definition
335 |  |  |  |  | unless $source->view_definition;
336 |  |  |  |  |
337 |  |  | 6 | 301µs | my $view = $schema->add_view ( # spent 291µs making 2 calls to SQL::Translator::Schema::add_view, avg 146µs/call # spent 9µs making 2 calls to DBIx::Class::ResultSource::columns, avg 4µs/call # spent 1µs making 2 calls to DBIx::Class::ResultSource::View::view_definition, avg 550ns/call
338 |  |  |  |  | name => $view_name,
339 |  |  |  |  | fields => [ $source->columns ],
340 |  |  |  |  | $source->view_definition ? ( 'sql' => $source->view_definition ) : ()
341 |  |  |  |  | ) || $dbicschema->throw_exception ($schema->error);
342 |  |  |  |  |
343 |  |  | 2 | 184µs | $source->_invoke_sqlt_deploy_hook($view); # spent 184µs making 2 calls to DBIx::Class::ResultSource::_invoke_sqlt_deploy_hook, avg 92µs/call
344 |  |  |  |  | }
345 |  |  |  |  |
346 |  |  |  |  |
347 |  |  | 4 | 19µs | if ($dbicschema->can('sqlt_deploy_hook')) { # spent 19µs making 4 calls to UNIVERSAL::can, avg 5µs/call
348 |  |  |  |  | $dbicschema->sqlt_deploy_hook($schema);
349 |  |  |  |  | }
350 |  |  |  |  |
351 |  |  |  |  | return 1;
352 |  |  |  |  | }
353 |  |  |  |  |
354 |  |  |  |  | #
355 |  |  |  |  | # Quick and dirty dependency graph calculator
356 |  |  |  |  | #
357 |  |  |  |  | # spent 1.13ms (1.04+93µs) within SQL::Translator::Parser::DBIx::Class::_resolve_deps which was called 85 times, avg 13µs/call: # 48 times (511µs+-511µs) by SQL::Translator::Parser::DBIx::Class::_resolve_deps at line 398, avg 0s/call # 35 times (500µs+573µs) by SQL::Translator::Parser::DBIx::Class::parse at line 271, avg 31µs/call # 2 times (30µs+30µs) by SQL::Translator::Parser::DBIx::Class::parse at line 305, avg 30µs/call sub _resolve_deps {
358 | 680 | 670µs |  |  | my ( $question, $answers, $seen ) = @_;
359 |  |  |  |  | my $ret = {};
360 |  |  |  |  | $seen ||= {};
361 |  |  |  |  | my @deps;
362 |  |  |  |  |
363 |  |  |  |  | # copy and bump all deps by one (so we can reconstruct the chain)
364 |  |  |  |  | my %seen = map { $_ => $seen->{$_} + 1 } ( keys %$seen );
365 | 170 | 154µs | 87 | 44µs | if ( blessed($question) # spent 42µs making 85 calls to Scalar::Util::blessed, avg 498ns/call # spent 2µs making 2 calls to UNIVERSAL::isa, avg 1µs/call
366 |  |  |  |  | && $question->isa('DBIx::Class::ResultSource::View') )
367 |  |  |  |  | {
368 |  |  | 2 | 26µs | $seen{ $question->result_class } = 1; # spent 26µs making 2 calls to DBIx::Class::ResultSource::result_class, avg 13µs/call
369 |  |  |  |  | @deps = keys %{ $question->{deploy_depends_on} };
370 |  |  |  |  | }
371 |  |  |  |  | else {
372 |  |  |  |  | $seen{$question} = 1;
373 |  |  |  |  | @deps = keys %{ $answers->{$question}{foreign_table_deps} };
374 |  |  |  |  | }
375 |  |  |  |  |
376 |  |  |  |  | for my $dep (@deps) {
377 | 336 | 298µs |  |  | if ( $seen->{$dep} ) {
378 |  |  |  |  | return {};
379 |  |  |  |  | }
380 |  |  |  |  | my $next_dep;
381 |  |  |  |  |
382 | 48 | 15µs | 48 | 22µs | if ( blessed($question) # spent 22µs making 48 calls to Scalar::Util::blessed, avg 462ns/call
383 |  |  |  |  | && $question->isa('DBIx::Class::ResultSource::View') )
384 |  |  |  |  | {
385 | 3 | 149µs | 2 | 56µs | # spent 34µs (12+22) within SQL::Translator::Parser::DBIx::Class::BEGIN@385 which was called: # once (12µs+22µs) by SQL::Translator::load at line 385 no warnings 'uninitialized'; # spent 34µs making 1 call to SQL::Translator::Parser::DBIx::Class::BEGIN@385 # spent 22µs making 1 call to warnings::unimport
386 |  |  |  |  | my ($next_dep_source_name) =
387 |  |  |  |  | grep {
388 |  |  |  |  | $question->schema->source($_)->result_class eq $dep
389 |  |  |  |  | && !( $question->schema->source($_)
390 |  |  |  |  | ->isa('DBIx::Class::ResultSource::Table') )
391 |  |  |  |  | } @{ [ $question->schema->sources ] };
392 |  |  |  |  | return {} unless $next_dep_source_name;
393 |  |  |  |  | $next_dep = $question->schema->source($next_dep_source_name);
394 |  |  |  |  | }
395 |  |  |  |  | else {
396 |  |  |  |  | $next_dep = $dep;
397 |  |  |  |  | }
398 |  |  | 48 | 0s | my $subdeps = _resolve_deps( $next_dep, $answers, \%seen ); # spent 715µs making 48 calls to SQL::Translator::Parser::DBIx::Class::_resolve_deps, avg 15µs/call, recursion: max depth 2, sum of overlapping time 715µs
399 |  |  |  |  | $ret->{$_} += $subdeps->{$_} for ( keys %$subdeps );
400 |  |  |  |  | ++$ret->{$dep};
401 |  |  |  |  | }
402 |  |  |  |  | return $ret;
403 |  |  |  |  | }
404 |  |  |  |  |
405 | 1 | 6µs |  |  | 1;
406 |  |  |  |  |
407 |  |  |  |  | =head1 NAME
408 |  |  |  |  |
409 |  |  |  |  | SQL::Translator::Parser::DBIx::Class - Create a SQL::Translator schema
410 |  |  |  |  | from a DBIx::Class::Schema instance
411 |  |  |  |  |
412 |  |  |  |  | =head1 SYNOPSIS
413 |  |  |  |  |
414 |  |  |  |  | ## Via DBIx::Class
415 |  |  |  |  | use MyApp::Schema;
416 |  |  |  |  | my $schema = MyApp::Schema->connect("dbi:SQLite:something.db");
417 |  |  |  |  | $schema->create_ddl_dir();
418 |  |  |  |  | ## or
419 |  |  |  |  | $schema->deploy();
420 |  |  |  |  |
421 |  |  |  |  | ## Standalone
422 |  |  |  |  | use MyApp::Schema;
423 |  |  |  |  | use SQL::Translator;
424 |  |  |  |  |
425 |  |  |  |  | my $schema = MyApp::Schema->connect;
426 |  |  |  |  | my $trans = SQL::Translator->new (
427 |  |  |  |  | parser => 'SQL::Translator::Parser::DBIx::Class',
428 |  |  |  |  | parser_args => {
429 |  |  |  |  | package => $schema,
430 |  |  |  |  | add_fk_index => 0,
431 |  |  |  |  | sources => [qw/
432 |  |  |  |  | Artist
433 |  |  |  |  | CD
434 |  |  |  |  | /],
435 |  |  |  |  | },
436 |  |  |  |  | producer => 'SQLite',
437 |  |  |  |  | ) or die SQL::Translator->error;
438 |  |  |  |  | my $out = $trans->translate() or die $trans->error;
439 |  |  |  |  |
440 |  |  |  |  | =head1 DESCRIPTION
441 |  |  |  |  |
442 |  |  |  |  | This class requires L<SQL::Translator> installed to work.
443 |  |  |  |  |
444 |  |  |  |  | C<SQL::Translator::Parser::DBIx::Class> reads a DBIx::Class schema,
445 |  |  |  |  | interrogates the columns, and stuffs it all in an $sqlt_schema object.
446 |  |  |  |  |
447 |  |  |  |  | Its primary use is in deploying database layouts described as a set
448 |  |  |  |  | of L<DBIx::Class> classes, to a database. To do this, see
449 |  |  |  |  | L<DBIx::Class::Schema/deploy>.
450 |  |  |  |  |
451 |  |  |  |  | This can also be achieved by having DBIx::Class export the schema as a
452 |  |  |  |  | set of SQL files ready for import into your database, or passed to
453 |  |  |  |  | other machines that need to have your application installed but don't
454 |  |  |  |  | have SQL::Translator installed. To do this see
455 |  |  |  |  | L<DBIx::Class::Schema/create_ddl_dir>.
456 |  |  |  |  |
457 |  |  |  |  | =head1 PARSER OPTIONS
458 |  |  |  |  |
459 |  |  |  |  | =head2 add_fk_index
460 |  |  |  |  |
461 |  |  |  |  | Create an index for each foreign key.
462 |  |  |  |  | Enabled by default, as having indexed foreign key columns is normally the
463 |  |  |  |  | sensible thing to do.
464 |  |  |  |  |
465 |  |  |  |  | =head2 sources
466 |  |  |  |  |
467 |  |  |  |  | =over 4
468 |  |  |  |  |
469 |  |  |  |  | =item Arguments: \@class_names
470 |  |  |  |  |
471 |  |  |  |  | =back
472 |  |  |  |  |
473 |  |  |  |  | Limit the amount of parsed sources by supplying an explicit list of source names.
474 |  |  |  |  |
475 |  |  |  |  | =head1 SEE ALSO
476 |  |  |  |  |
477 |  |  |  |  | L<SQL::Translator>, L<DBIx::Class::Schema>
478 |  |  |  |  |
479 |  |  |  |  | =head1 AUTHORS
480 |  |  |  |  |
481 |  |  |  |  | See L<DBIx::Class/CONTRIBUTORS>.
482 |  |  |  |  |
483 |  |  |  |  | =head1 LICENSE
484 |  |  |  |  |
485 |  |  |  |  | You may distribute this code under the same terms as Perl itself.
486 |  |  |  |  |
487 | 1 | 11µs | 1 | 620µs | =cut # spent 620µs making 1 call to B::Hooks::EndOfScope::__ANON__[B/Hooks/EndOfScope.pm:26]
|  |  |  |  | # spent 236µs within SQL::Translator::Parser::DBIx::Class::CORE:match which was called 155 times, avg 2µs/call: # 60 times (128µs+0s) by SQL::Translator::Parser::DBIx::Class::parse at line 178, avg 2µs/call # 60 times (83µs+0s) by SQL::Translator::Parser::DBIx::Class::parse at line 179, avg 1µs/call # 35 times (25µs+0s) by SQL::Translator::Parser::DBIx::Class::parse at line 287, avg 717ns/call sub SQL::Translator::Parser::DBIx::Class::CORE:match; # opcode
|  |  |  |  | # spent 274µs (230+44) within SQL::Translator::Parser::DBIx::Class::CORE:sort which was called 175 times, avg 2µs/call: # 60 times (25µs+0s) by SQL::Translator::Parser::DBIx::Class::parse at line 175, avg 413ns/call # 35 times (40µs+0s) by SQL::Translator::Parser::DBIx::Class::parse at line 129, avg 1µs/call # 35 times (30µs+0s) by SQL::Translator::Parser::DBIx::Class::parse at line 146, avg 866ns/call # 29 times (8µs+0s) by SQL::Translator::Parser::DBIx::Class::parse at line 221, avg 279ns/call # 4 times (36µs+44µs) by SQL::Translator::Parser::DBIx::Class::parse at line 317, avg 20µs/call # 4 times (67µs+0s) by SQL::Translator::Parser::DBIx::Class::parse at line 275, avg 17µs/call # 4 times (17µs+0s) by SQL::Translator::Parser::DBIx::Class::parse at line 59, avg 4µs/call # 4 times (7µs+0s) by SQL::Translator::Parser::DBIx::Class::parse at line 88, avg 2µs/call sub SQL::Translator::Parser::DBIx::Class::CORE:sort; # opcode