7885ed5c4390c8e3277a36b29375fcc2b836ad04
[dbsrgits/DBIx-Class.git] / t / cdbi / 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 our $dbh;
10
11 my $err;
12 if (! $ENV{DBICTEST_MYSQL_DSN} ) {
13   $err = 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test';
14 }
15 elsif ( ! DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mysql') ) {
16   $err = 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mysql')
17 }
18
19 if ($err) {
20   my $t = eval { Test::Builder->new };
21   if ($t and ! $t->current_test) {
22     $t->skip_all ($err);
23   }
24   else {
25     die "$err\n";
26   }
27 }
28
29 my @connect = (@ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}, { PrintError => 0});
30 $dbh = DBI->connect(@connect) or die DBI->errstr;
31 my @table;
32
33 END { $dbh->do("DROP TABLE $_") foreach @table }
34
35 __PACKAGE__->connection(@connect);
36
37 sub set_table {
38   my $class = shift;
39   $class->table($class->create_test_table);
40 }
41
42 sub create_test_table {
43   my $self   = shift;
44   my $table  = $self->next_available_table;
45   my $create = sprintf "CREATE TABLE $table ( %s )", $self->create_sql;
46   push @table, $table;
47   $dbh->do($create);
48   return $table;
49 }
50
51 sub next_available_table {
52   my $self   = shift;
53   my @tables = sort @{
54     $dbh->selectcol_arrayref(
55       qq{
56     SHOW TABLES
57   }
58     )
59     };
60   my $table = $tables[-1] || "aaa";
61   return "z$table";
62 }
63
64 1;