File: MyBase.pm

package info (click to toggle)
libclass-dbi-perl 3.0.17-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 548 kB
  • ctags: 251
  • sloc: perl: 2,118; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 762 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package MyBase;

use strict;
use base qw(Class::DBI);

use vars qw/$dbh/;

my @connect = ("dbi:mysql:test", "", "");

$dbh = DBI->connect(@connect) or die DBI->errstr;
my @table;

END { $dbh->do("DROP TABLE $_") foreach @table }

__PACKAGE__->connection(@connect);

sub set_table {
	my $class = shift;
	$class->table($class->create_test_table);
}

sub create_test_table {
	my $self   = shift;
	my $table  = $self->next_available_table;
	my $create = sprintf "CREATE TABLE $table ( %s )", $self->create_sql;
	push @table, $table;
	$dbh->do($create);
	return $table;
}

sub next_available_table {
	my $self   = shift;
	my @tables = sort @{
		$dbh->selectcol_arrayref(
			qq{
    SHOW TABLES
  }
		)
		};
	my $table = $tables[-1] || "aaa";
	return "z$table";
}

1;