11 use Path::Class qw/file/;
13 BEGIN { delete @ENV{qw(DBIC_TRACE DBIC_TRACE_PROFILE DBICTEST_SQLITE_USE_FILE)} }
15 my $schema = DBICTest->init_schema();
17 my $lfn = file("t/var/sql-$$.log");
21 # make sure we are testing the vanilla debugger and not ::PrettyPrint
22 require DBIx::Class::Storage::Statistics;
23 $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
25 ok ( $schema->storage->debug(1), 'debug' );
26 $schema->storage->debugfh($lfn->openw);
27 $schema->resultset('CD')->count;
29 my @loglines = $lfn->slurp;
30 is (@loglines, 1, 'one line of log');
31 like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success');
33 $schema->storage->debugfh(undef);
36 local $ENV{DBIC_TRACE} = "=$lfn";
39 $schema->resultset('CD')->count;
41 my $schema2 = DBICTest->init_schema(no_deploy => 1);
42 $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms
44 my @loglines = $lfn->slurp;
45 is(@loglines, 2, '2 lines of log');
46 like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success');
47 like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success');
49 $schema->storage->debugobj->debugfh(undef)
56 open(STDERRCOPY, '>&STDERR');
58 my $exception_line_number;
59 # STDERR will be closed, no T::B diag in blocks
62 $exception_line_number = __LINE__ + 1; # important for test, do not reformat
63 $schema->resultset('CD')->search({})->count;
68 open(STDERR, '>&STDERRCOPY');
72 \QDuplication of STDERR for debug output failed (perhaps your STDERR is closed?)\E
74 \Qat @{[__FILE__]} line $exception_line_number\E$
79 local $SIG{__WARN__} = sub { push @warnings, @_ if $_[0] =~ /character/i };
81 open(STDERR, '>', File::Spec->devnull) or die $!;
82 $schema->resultset('CD')->search({ title => "\x{1f4a9}" })->count;
89 open(STDERR, '>&STDERRCOPY');
92 die "How did that fail... $exception"
95 is_deeply(\@warnings, [], 'No warnings with unicode on STDERR');
98 # test debugcb and debugobj protocol
100 my $rs = $schema->resultset('CD')->search( {
102 cdid => { -between => [ 1, 3 ] },
103 title => { '!=' => \[ '?', undef ] }
106 my $sql_trace = 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( ( artist = ? AND ( cdid BETWEEN ? AND ? ) AND title != ? ) )';
107 my @bind_trace = qw( '1' '1' '3' NULL ); # quotes are in fact part of the trace </facepalm>
111 $schema->storage->debugcb(sub { push @args, @_ } );
117 sprintf( "%s: %s\n", $sql_trace, join ', ', @bind_trace ),
121 package DBICTest::DebugObj;
122 our @ISA = 'DBIx::Class::Storage::Statistics';
126 ( $self->{_traced_sql}, @{$self->{_traced_bind}} ) = @_;
130 my $do = $schema->storage->debugobj(DBICTest::DebugObj->new);
134 is( $do->{_traced_sql}, $sql_trace );
136 is_deeply ( $do->{_traced_bind}, \@bind_trace );