10 use Path::Class qw/file/;
12 BEGIN { delete @ENV{qw(DBIC_TRACE DBIC_TRACE_PROFILE DBICTEST_SQLITE_USE_FILE)} }
14 my $schema = DBICTest->init_schema();
16 my $lfn = file("t/var/sql-$$.log");
20 # make sure we are testing the vanilla debugger and not ::PrettyPrint
21 require DBIx::Class::Storage::Statistics;
22 $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new);
24 ok ( $schema->storage->debug(1), 'debug' );
25 $schema->storage->debugfh($lfn->openw);
26 $schema->resultset('CD')->count;
28 my @loglines = $lfn->slurp;
29 is (@loglines, 1, 'one line of log');
30 like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success');
32 $schema->storage->debugfh(undef);
35 local $ENV{DBIC_TRACE} = "=$lfn";
38 $schema->resultset('CD')->count;
40 my $schema2 = DBICTest->init_schema(no_deploy => 1);
41 $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms
43 my @loglines = $lfn->slurp;
44 is(@loglines, 2, '2 lines of log');
45 like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success');
46 like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success');
48 $schema->storage->debugobj->debugfh(undef)
55 open(STDERRCOPY, '>&STDERR');
57 # STDERR will be closed, no T::B diag in blocks
60 $schema->resultset('CD')->search({})->count;
65 open(STDERR, '>&STDERRCOPY');
68 like $exception, qr/\QDuplication of STDERR for debug output failed (perhaps your STDERR is closed?)/;
72 # test debugcb and debugobj protocol
74 my $rs = $schema->resultset('CD')->search( {
76 cdid => { -between => [ 1, 3 ] },
77 title => { '!=' => \[ '?', undef ] }
80 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 != ? ) )';
81 my @bind_trace = qw( '1' '1' '3' NULL ); # quotes are in fact part of the trace </facepalm>
85 $schema->storage->debugcb(sub { push @args, @_ } );
91 sprintf( "%s: %s\n", $sql_trace, join ', ', @bind_trace ),
95 package DBICTest::DebugObj;
96 our @ISA = 'DBIx::Class::Storage::Statistics';
100 ( $self->{_traced_sql}, @{$self->{_traced_bind}} ) = @_;
104 my $do = $schema->storage->debugobj(DBICTest::DebugObj->new);
108 is( $do->{_traced_sql}, $sql_trace );
110 is_deeply ( $do->{_traced_bind}, \@bind_trace );