line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MojoX::Mysql::DB; |
2
|
5
|
|
|
5
|
|
22
|
use Mojo::Base -base; |
|
5
|
|
|
|
|
6
|
|
|
5
|
|
|
|
|
44
|
|
3
|
5
|
|
|
5
|
|
708
|
use Mojo::Util qw(dumper); |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
231
|
|
4
|
5
|
|
|
5
|
|
23
|
use Carp qw(croak); |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
5121
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
has [qw(id)] => '_default'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub DESTROY { |
9
|
3
|
|
|
3
|
|
3073
|
my $self = shift; |
10
|
3
|
|
|
|
|
13
|
$self->rollback; |
11
|
3
|
|
|
|
|
8
|
$self->disconnect; |
12
|
3
|
|
|
|
|
6
|
$self->flush; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# Master connect DB |
16
|
|
|
|
|
|
|
sub connect_master { |
17
|
4
|
|
|
4
|
1
|
147
|
my $self = shift; |
18
|
4
|
|
|
|
|
70
|
my $id = $self->id; |
19
|
4
|
|
|
|
|
38
|
$self->flush; |
20
|
|
|
|
|
|
|
|
21
|
4
|
|
|
|
|
25
|
my $config = {}; |
22
|
4
|
50
|
|
|
|
20
|
if(exists $self->{'config'}->{$id}->{'master'}){ |
23
|
4
|
|
|
|
|
12
|
$config = $self->{'config'}->{$id}->{'master'}; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
else{ |
26
|
0
|
|
|
|
|
0
|
croak 'Not found id server'; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
4
|
50
|
33
|
|
|
44
|
if(defined $config && ref($config) eq 'HASH' && %{$config}){ |
|
4
|
|
33
|
|
|
42
|
|
30
|
4
|
50
|
|
|
|
21
|
if(ref($self->{'dbh'}{'master'}{$id}) eq 'DBI::db'){ |
31
|
0
|
|
|
|
|
0
|
my $dbh = $self->{'dbh'}{'master'}{$id}; |
32
|
0
|
0
|
|
|
|
0
|
return $dbh if($dbh->ping); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
4
|
|
|
|
|
72
|
my $dbh = DBI->connect("DBI:mysql:".$config->{'dsn'}, $config->{'user'}, $config->{'password'}, { |
36
|
|
|
|
|
|
|
AutoCommit=>0, |
37
|
|
|
|
|
|
|
RaiseError=>0, |
38
|
|
|
|
|
|
|
PrintError=>0, |
39
|
|
|
|
|
|
|
mysql_enable_utf8=>1, |
40
|
|
|
|
|
|
|
mysql_auto_reconnect=>1, |
41
|
|
|
|
|
|
|
mysql_write_timeout=>$config->{'write_timeout'}, |
42
|
|
|
|
|
|
|
mysql_read_timeout=>$config->{'read_timeout'}, |
43
|
|
|
|
|
|
|
}); |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
|
|
|
0
|
if($DBI::errstr){ |
46
|
0
|
|
|
|
|
0
|
$self->{'dbh'}{'master'}{$id} = undef; |
47
|
0
|
|
|
|
|
0
|
return; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
else{ |
50
|
0
|
|
|
|
|
0
|
$self->{'dbh'}{'master'}{$id} = $dbh; |
51
|
0
|
|
|
|
|
0
|
$dbh->{'RaiseError'} = 1; |
52
|
0
|
|
|
|
|
0
|
return $dbh; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
0
|
|
|
|
|
0
|
return; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Slave connect DB |
59
|
|
|
|
|
|
|
sub connect_slave { |
60
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
61
|
0
|
|
|
|
|
0
|
my $id = $self->id; |
62
|
0
|
|
|
|
|
0
|
$self->flush; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
my $config = {}; |
65
|
0
|
0
|
|
|
|
0
|
if(exists $self->{'config'}{$id}->{'slave'}){ |
66
|
0
|
|
|
|
|
0
|
$config = $self->{'config'}->{$id}->{'slave'}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else{ |
69
|
0
|
|
|
|
|
0
|
croak 'Not found id server'; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
0
|
|
|
0
|
if(defined $config && ref($config) eq 'ARRAY' && @{$config}){ |
|
0
|
|
0
|
|
|
0
|
|
73
|
0
|
0
|
|
|
|
0
|
if(ref($self->{'dbh'}{'slave'}{$id}) eq 'DBI::db'){ |
74
|
0
|
|
|
|
|
0
|
my $dbh = $self->{'dbh'}{'slave'}{$id}; |
75
|
0
|
0
|
|
|
|
0
|
return $dbh if($dbh->ping); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
0
|
for my $conf (@{$config}){ |
|
0
|
|
|
|
|
0
|
|
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
0
|
my $dbh = DBI->connect("DBI:mysql:".$conf->{'dsn'}, $conf->{'user'}, $conf->{'password'}, { |
81
|
|
|
|
|
|
|
AutoCommit=>0, |
82
|
|
|
|
|
|
|
RaiseError=>0, |
83
|
|
|
|
|
|
|
PrintError=>0, |
84
|
|
|
|
|
|
|
mysql_enable_utf8=>1, |
85
|
|
|
|
|
|
|
mysql_auto_reconnect=>1, |
86
|
|
|
|
|
|
|
mysql_write_timeout=>$conf->{'write_timeout'}, |
87
|
|
|
|
|
|
|
mysql_read_timeout=>$conf->{'read_timeout'}, |
88
|
|
|
|
|
|
|
}); |
89
|
|
|
|
|
|
|
|
90
|
0
|
0
|
|
|
|
0
|
if($DBI::errstr){ |
91
|
0
|
|
|
|
|
0
|
$self->{'dbh'}{'slave'}{$id} = undef; |
92
|
0
|
|
|
|
|
0
|
next; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
else{ |
95
|
0
|
|
|
|
|
0
|
$self->{'dbh'}{'slave'}{$id} = $dbh; |
96
|
0
|
|
|
|
|
0
|
$dbh->{'RaiseError'} = 1; |
97
|
0
|
|
|
|
|
0
|
return $dbh; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
0
|
|
|
|
|
0
|
return; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub commit { |
105
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
106
|
0
|
|
|
|
|
0
|
while(my($id,$types) = each %{$self->{'dbh'}}){ |
|
0
|
|
|
|
|
0
|
|
107
|
0
|
0
|
|
|
|
0
|
if(ref $types eq 'HASH'){ |
108
|
0
|
|
|
|
|
0
|
for my $type (keys %{$types}){ |
|
0
|
|
|
|
|
0
|
|
109
|
0
|
|
|
|
|
0
|
my $dbh = $self->{'dbh'}->{$id}->{$type}; |
110
|
0
|
0
|
|
|
|
0
|
$dbh->commit if(ref $dbh eq 'DBI::db'); |
111
|
0
|
0
|
|
|
|
0
|
warn "commit:$id,$type" if(defined $ENV{'MOJO_MYSQL_DEBUG'}); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub rollback { |
118
|
4
|
|
|
4
|
1
|
15
|
my ($self) = @_; |
119
|
4
|
|
|
|
|
8
|
while(my($id,$types) = each %{$self->{'dbh'}}){ |
|
8
|
|
|
|
|
40
|
|
120
|
4
|
50
|
|
|
|
16
|
if(ref $types eq 'HASH'){ |
121
|
4
|
|
|
|
|
9
|
for my $type (keys %{$types}){ |
|
4
|
|
|
|
|
18
|
|
122
|
0
|
|
|
|
|
0
|
my $dbh = $self->{'dbh'}->{$id}->{$type}; |
123
|
0
|
0
|
|
|
|
0
|
$dbh->rollback if(ref $dbh eq 'DBI::db'); |
124
|
0
|
0
|
|
|
|
0
|
warn "rollback:$id,$type" if(defined $ENV{'MOJO_MYSQL_DEBUG'}); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub disconnect { |
131
|
3
|
|
|
3
|
1
|
5
|
my ($self) = @_; |
132
|
3
|
|
|
|
|
4
|
while(my($id,$types) = each %{$self->{'dbh'}}){ |
|
6
|
|
|
|
|
22
|
|
133
|
3
|
50
|
|
|
|
10
|
if(ref $types eq 'HASH'){ |
134
|
3
|
|
|
|
|
4
|
for my $type (keys %{$types}){ |
|
3
|
|
|
|
|
9
|
|
135
|
0
|
|
|
|
|
0
|
my $dbh = $self->{'dbh'}->{$id}->{$type}; |
136
|
0
|
0
|
|
|
|
0
|
$dbh->disconnect if(ref $dbh eq 'DBI::db'); |
137
|
0
|
0
|
|
|
|
0
|
warn "disconnect:$id,$type" if(defined $ENV{'MOJO_MYSQL_DEBUG'}); |
138
|
0
|
|
|
|
|
0
|
delete $self->{'dbh'}; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub flush { |
145
|
7
|
|
|
7
|
0
|
13
|
my $self = shift; |
146
|
7
|
|
|
|
|
142
|
$self->id('_default'); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=encoding utf8 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 NAME |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
MojoX::Mysql::DB - Connect DB |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 SYNOPSIS |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
use MojoX::Mysql; |
161
|
|
|
|
|
|
|
use Mojo::Util qw(dumper); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my %config = ( |
164
|
|
|
|
|
|
|
user=>'root', |
165
|
|
|
|
|
|
|
password=>undef, |
166
|
|
|
|
|
|
|
server=>[ |
167
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', type=>'master'}, |
168
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', type=>'slave'}, |
169
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>1, type=>'master'}, |
170
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>1, type=>'slave'}, |
171
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>2, type=>'master'}, |
172
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>2, type=>'slave'}, |
173
|
|
|
|
|
|
|
] |
174
|
|
|
|
|
|
|
); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
my $mysql = MojoX::Mysql->new(%config); |
177
|
|
|
|
|
|
|
my $dbh = $mysql->db->connect_master; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head2 id |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
$mysql->id(1); # choice id server |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 METHODS |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head2 connect_master |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
$mysql->db->connect_master; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Return connect object. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 connect_slave |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
$mysql->db->connect_slave; |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Return connect object. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 commit |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
$mysql->db->commit; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Commit on all servers |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head2 rollback |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
$mysql->db->rollback; |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Rollback on all servers |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head2 disconnect |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
$mysql->db->disconnect; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Disconnect on all servers |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=cut |