c06f17921b83a1a066409ffb5deb647d4c107008
[dbsrgits/DBIx-Class.git] / t / cdbi / testlib / MyBase.pm
1 package # hide from PAUSE
2     MyBase;
3
4 use strict;
5 use DBI;
6
7 use lib 't/lib';
8 use DBICTest;
9
10 use base qw(DBIx::Class::CDBICompat);
11
12 our $dbh;
13
14 my $err;
15 if (! $ENV{DBICTEST_MYSQL_DSN} ) {
16   $err = 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test';
17 }
18 elsif ( ! DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mysql') ) {
19   $err = 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mysql')
20 }
21
22 if ($err) {
23   my $t = eval { Test::Builder->new };
24   if ($t and ! $t->current_test) {
25     $t->skip_all ($err);
26   }
27   else {
28     die "$err\n";
29   }
30 }
31
32 my @connect = (@ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}, { PrintError => 0});
33 # this is only so we grab a lock on mysql
34 {
35   my $x = DBICTest::Schema->connect(@connect);
36 }
37
38 $dbh = DBI->connect(@connect) or die DBI->errstr;
39 my @table;
40
41 END { $dbh->do("DROP TABLE $_") foreach @table }
42
43 __PACKAGE__->connection(@connect);
44
45 sub set_table {
46   my $class = shift;
47   $class->table($class->create_test_table);
48 }
49
50 sub create_test_table {
51   my $self   = shift;
52   my $table  = $self->next_available_table;
53   my $create = sprintf "CREATE TABLE $table ( %s )", $self->create_sql;
54   push @table, $table;
55   $dbh->do($create);
56   return $table;
57 }
58
59 sub next_available_table {
60   my $self   = shift;
61   my @tables = sort @{
62     $dbh->selectcol_arrayref(
63       qq{
64     SHOW TABLES
65   }
66     )
67     };
68   my $table = $tables[-1] || "aaa";
69   return "z$table";
70 }
71
72 1;