Merge 'trunk' into 'replication_dedux'
[dbsrgits/DBIx-Class.git] / t / testlib / MyBase.pm
1 package # hide from PAUSE
2     MyBase;
3
4 use strict;
5 use base qw(DBIx::Class::CDBICompat);
6
7 use DBI;
8
9 use vars qw/$dbh/;
10
11 my @connect = ("dbi:mysql:test", "", "");
12
13 $dbh = DBI->connect(@connect) or die DBI->errstr;
14 my @table;
15
16 END { $dbh->do("DROP TABLE $_") foreach @table }
17
18 __PACKAGE__->connection(@connect);
19
20 sub set_table {
21         my $class = shift;
22         $class->table($class->create_test_table);
23 }
24
25 sub create_test_table {
26         my $self   = shift;
27         my $table  = $self->next_available_table;
28         my $create = sprintf "CREATE TABLE $table ( %s )", $self->create_sql;
29         push @table, $table;
30         $dbh->do($create);
31         return $table;
32 }
33
34 sub next_available_table {
35         my $self   = shift;
36         my @tables = sort @{
37                 $dbh->selectcol_arrayref(
38                         qq{
39     SHOW TABLES
40   }
41                 )
42                 };
43         my $table = $tables[-1] || "aaa";
44         return "z$table";
45 }
46
47 1;