line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::Skinny::ProxyTable; |
2
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
38
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
42
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
5
|
1
|
|
|
1
|
|
520
|
use DBIx::Skinny::ProxyTable::Rule; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
6
|
1
|
|
|
1
|
|
18
|
use Carp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
74
|
|
7
|
1
|
|
|
1
|
|
1064
|
use DBIx::Skinny::Util qw(); |
|
1
|
|
|
|
|
37358
|
|
|
1
|
|
|
|
|
427
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
0
|
|
|
0
|
0
|
|
my ($class, $skinny) = @_; |
11
|
0
|
|
|
|
|
|
my $self = { skinny => $skinny }; |
12
|
0
|
|
|
|
|
|
bless $self, $class; |
13
|
0
|
|
|
|
|
|
return $self; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub skinny { |
17
|
0
|
|
|
0
|
0
|
|
my $self = $_[0]; |
18
|
0
|
|
|
|
|
|
$self->{skinny}; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub set { |
22
|
0
|
|
|
0
|
1
|
|
my ($self, $from, $to) = @_; |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
$self->_validate($from); |
25
|
0
|
|
|
|
|
|
$self->_validate($to); |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my $skinny = $self->skinny; |
28
|
0
|
|
|
|
|
|
my $schema = $skinny->schema; |
29
|
0
|
|
|
|
|
|
my $_schema_info = $schema->schema_info; |
30
|
0
|
|
|
|
|
|
$_schema_info->{$to} = $_schema_info->{$from}; |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
$self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# This method is safety net for creating wrong table name or executing SQL injection. |
36
|
|
|
|
|
|
|
sub _validate { |
37
|
0
|
|
|
0
|
|
|
my ($self, $str) = @_; |
38
|
0
|
0
|
|
|
|
|
if ( $str !~ /^[a-zA-Z0-9_]+$/ ) { |
39
|
0
|
|
|
|
|
|
Carp::croak("$str should be normal character"); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub copy_table { |
44
|
0
|
|
|
0
|
1
|
|
my ($self, $from, $to) = @_; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
$self->_validate($from); |
47
|
0
|
|
|
|
|
|
$self->_validate($to); |
48
|
0
|
|
0
|
|
|
|
my $dbd = $self->skinny->dbd && ref $self->skinny->dbd; |
49
|
0
|
0
|
0
|
|
|
|
if ( $dbd && $dbd =~ /^DBIx::Skinny::DBD::(.+)$/ ) { |
50
|
0
|
|
|
|
|
|
$dbd = $1; |
51
|
0
|
0
|
|
|
|
|
if ( $dbd eq 'mysql' ) { |
|
|
0
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
$self->skinny->dbh->do(sprintf(q{ CREATE TABLE IF NOT EXISTS %s LIKE %s }, $to, $from)); |
53
|
|
|
|
|
|
|
} elsif ( $dbd eq 'SQLite' ) { |
54
|
0
|
0
|
|
|
|
|
my $record = $self->skinny->dbh->selectcol_arrayref(q{ |
55
|
|
|
|
|
|
|
SELECT sql FROM |
56
|
|
|
|
|
|
|
( SELECT * FROM sqlite_master UNION ALL |
57
|
|
|
|
|
|
|
SELECT * FROM sqlite_temp_master) |
58
|
|
|
|
|
|
|
WHERE type != 'meta' and tbl_name = ? |
59
|
|
|
|
|
|
|
}, {}, $from)->[0] |
60
|
|
|
|
|
|
|
or Carp::croak("Can't find table $from in sqlite_master or sqlite_temp_master"); |
61
|
0
|
|
|
|
|
|
my $sql = $record; |
62
|
0
|
|
|
|
|
|
$sql =~ s/TABLE $from \(/TABLE IF NOT EXISTS $to (/; |
63
|
0
|
|
|
|
|
|
$self->skinny->dbh->do($sql); |
64
|
|
|
|
|
|
|
} else { |
65
|
0
|
|
|
|
|
|
die "DBIx::Skinny::DBD::$dbd is not supported"; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub rule { |
71
|
0
|
|
|
0
|
1
|
|
my ($self, $base, @args) = @_; |
72
|
0
|
|
|
|
|
|
return DBIx::Skinny::ProxyTable::Rule->new($self, $base, @args); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
1; |
76
|
|
|
|
|
|
|
__END__ |