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