line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MySQL::Partition; |
2
|
3
|
|
|
3
|
|
159569
|
use 5.008001; |
|
3
|
|
|
|
|
26
|
|
3
|
3
|
|
|
3
|
|
12
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
52
|
|
4
|
3
|
|
|
3
|
|
12
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
110
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "1.0001"; |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
1003
|
use MySQL::Partition::Handle; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
76
|
|
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
1157
|
use Module::Load (); |
|
3
|
|
|
|
|
2680
|
|
|
3
|
|
|
|
|
88
|
|
11
|
|
|
|
|
|
|
use Class::Accessor::Lite ( |
12
|
3
|
|
|
|
|
20
|
rw => [qw/dry_run verbose/], |
13
|
|
|
|
|
|
|
ro => [qw/type dbh table expression/], |
14
|
3
|
|
|
3
|
|
17
|
); |
|
3
|
|
|
|
|
6
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub dbname { |
17
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
18
|
0
|
0
|
0
|
|
|
0
|
exists $self->{dbname} ? $self->{dbname} : $self->{dbname} ||= _get_dbname($self->dbh->{Name}); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
5
|
|
|
5
|
1
|
8439
|
my $class = shift; |
23
|
5
|
50
|
|
|
|
13
|
die q[can't call new method directory in sub class] if $class ne __PACKAGE__; |
24
|
5
|
50
|
|
|
|
29
|
my %args = @_ == 1 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
25
|
|
|
|
|
|
|
|
26
|
5
|
|
|
|
|
12
|
$args{type} = uc $args{type}; |
27
|
|
|
|
|
|
|
|
28
|
5
|
|
|
|
|
17
|
my ($type) = split /\s+/, $args{type}; |
29
|
5
|
|
|
|
|
15
|
my $sub_class = __PACKAGE__ . '::Type::' . ucfirst( lc $type ); |
30
|
5
|
|
|
|
|
14
|
Module::Load::load($sub_class); |
31
|
5
|
|
|
|
|
165
|
bless \%args, $sub_class; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
__PACKAGE__->_grow_methods(qw/create_partitions add_partitions drop_partitions truncate_partitions/); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub retrieve_partitions { |
37
|
0
|
|
|
0
|
1
|
0
|
my ($self, $table) = @_; |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
0
|
|
|
0
|
my $parts = $self->{partitions} ||= do { |
40
|
0
|
|
|
|
|
0
|
my @parts; |
41
|
0
|
|
|
|
|
0
|
my $sth = $self->dbh->prepare(' |
42
|
|
|
|
|
|
|
SELECT |
43
|
|
|
|
|
|
|
partition_name |
44
|
|
|
|
|
|
|
FROM |
45
|
|
|
|
|
|
|
information_schema.PARTITIONS |
46
|
|
|
|
|
|
|
WHERE |
47
|
|
|
|
|
|
|
table_name = ? AND |
48
|
|
|
|
|
|
|
table_schema = ? AND |
49
|
|
|
|
|
|
|
partition_method = ? |
50
|
|
|
|
|
|
|
ORDER BY |
51
|
|
|
|
|
|
|
partition_name |
52
|
|
|
|
|
|
|
'); |
53
|
0
|
|
|
|
|
0
|
$sth->execute($self->table, $self->dbname, $self->type); |
54
|
0
|
|
|
|
|
0
|
while (my $row = $sth->fetchrow_arrayref) { |
55
|
0
|
0
|
|
|
|
0
|
push @parts, $row->[0] if defined $row->[0]; |
56
|
|
|
|
|
|
|
} |
57
|
0
|
|
|
|
|
0
|
\@parts; |
58
|
|
|
|
|
|
|
}; |
59
|
0
|
|
|
|
|
0
|
@$parts; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub is_partitioned { |
63
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
64
|
0
|
0
|
|
|
|
0
|
$self->retrieve_partitions ? 1 : (); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub has_partition { |
68
|
0
|
|
|
0
|
0
|
0
|
my ($self, $partition_name) = @_; |
69
|
0
|
|
|
|
|
0
|
grep {$_ eq $partition_name} $self->retrieve_partitions; |
|
0
|
|
|
|
|
0
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _build_create_partitions_sql { |
73
|
3
|
|
|
3
|
|
733
|
my ($self, @args) = @_; |
74
|
|
|
|
|
|
|
|
75
|
3
|
100
|
100
|
|
|
15
|
if ($self->isa('MySQL::Partition::Type::Range') && $self->catch_all_partition_name) { |
76
|
1
|
|
|
|
|
8
|
push @args, $self->catch_all_partition_name, 'MAXVALUE'; |
77
|
|
|
|
|
|
|
} |
78
|
3
|
|
|
|
|
22
|
sprintf 'ALTER TABLE %s PARTITION BY %s (%s) (%s)', |
79
|
|
|
|
|
|
|
$self->table, $self->type, $self->expression, $self->_build_partition_parts(@args); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _build_add_partitions_sql { |
83
|
2
|
|
|
2
|
|
7
|
my ($self, @args) = @_; |
84
|
|
|
|
|
|
|
|
85
|
2
|
|
|
|
|
5
|
sprintf 'ALTER TABLE %s ADD PARTITION (%s)', $self->table, $self->_build_partition_parts(@args); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub _build_partition_parts { |
89
|
6
|
|
|
6
|
|
65
|
my ($self, @args) = @_; |
90
|
|
|
|
|
|
|
|
91
|
6
|
|
|
|
|
9
|
my @parts; |
92
|
6
|
|
|
|
|
14
|
while (my ($partition_name, $partition_description) = splice @args, 0, 2) { |
93
|
8
|
|
|
|
|
15
|
push @parts, $self->_build_partition_part($partition_name, $partition_description); |
94
|
|
|
|
|
|
|
} |
95
|
6
|
|
|
|
|
34
|
join ', ', @parts; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub _build_partition_part { |
99
|
0
|
|
|
0
|
|
0
|
die 'this is abstruct method'; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _build_drop_partitions_sql { |
103
|
1
|
|
|
1
|
|
3
|
my ($self, @partition_names) = @_; |
104
|
|
|
|
|
|
|
|
105
|
1
|
|
|
|
|
3
|
sprintf 'ALTER TABLE %s DROP PARTITION %s', $self->table, join(', ', @partition_names); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub _build_truncate_partitions_sql { |
109
|
1
|
|
|
1
|
|
472
|
my ($self, @partition_names) = @_; |
110
|
|
|
|
|
|
|
|
111
|
1
|
|
|
|
|
4
|
sprintf 'ALTER TABLE %s TRUNCATE PARTITION %s', $self->table, join(', ', @partition_names); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub _grow_methods { |
115
|
4
|
|
|
4
|
|
15
|
my ($class, @methods) = @_; |
116
|
|
|
|
|
|
|
|
117
|
4
|
|
|
|
|
12
|
for my $method (@methods) { |
118
|
14
|
|
|
|
|
33
|
my $prepare_method = "prepare_$method"; |
119
|
14
|
|
|
|
|
29
|
my $sql_builder_method = "_build_${method}_sql"; |
120
|
|
|
|
|
|
|
|
121
|
3
|
|
|
3
|
|
2280
|
no strict 'refs'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
135
|
|
122
|
14
|
|
|
|
|
61
|
*{$class . '::' . $prepare_method} = sub { |
123
|
3
|
|
|
3
|
|
17
|
use strict 'refs'; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
301
|
|
124
|
0
|
|
|
0
|
|
0
|
my ($self, @args) = @_; |
125
|
0
|
|
|
|
|
0
|
my $sql = $self->$sql_builder_method(@args); |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
0
|
return MySQL::Partition::Handle->new( |
128
|
|
|
|
|
|
|
statement => $sql, |
129
|
|
|
|
|
|
|
mysql_partition => $self, |
130
|
|
|
|
|
|
|
); |
131
|
14
|
|
|
|
|
52
|
}; |
132
|
14
|
|
|
|
|
105
|
*{$class . '::' . $method} = sub { |
133
|
3
|
|
|
3
|
|
19
|
use strict 'refs'; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
463
|
|
134
|
0
|
|
|
0
|
|
|
my ($self, @args) = @_; |
135
|
0
|
|
|
|
|
|
$self->$prepare_method(@args)->execute; |
136
|
14
|
|
|
|
|
41
|
}; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub _get_dbname { |
141
|
0
|
|
|
0
|
|
|
my $connected_db = shift; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# XXX can't parse 'host=hoge;database=fuga' |
144
|
0
|
|
|
|
|
|
my ($dbname) = $connected_db =~ m!^(?:(?:database|dbname)=)?([^;]*)!i; |
145
|
0
|
|
|
|
|
|
$dbname; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
__END__ |