line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MojoX::Mysql::DB; |
2
|
7
|
|
|
7
|
|
32
|
use Mojo::Base -base; |
|
7
|
|
|
|
|
9
|
|
|
7
|
|
|
|
|
68
|
|
3
|
7
|
|
|
7
|
|
1199
|
use Mojo::Util qw(dumper); |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
327
|
|
4
|
7
|
|
|
7
|
|
27
|
use Carp qw(croak); |
|
7
|
|
|
|
|
9
|
|
|
7
|
|
|
|
|
7040
|
|
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
|
0
|
|
|
|
|
|
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
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}); |
90
|
|
|
|
|
|
|
|
91
|
0
|
0
|
|
|
|
|
if($DBI::errstr){ |
92
|
0
|
|
|
|
|
|
$self->{'dbh'}{'slave'}{$id} = undef; |
93
|
0
|
|
|
|
|
|
next; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
else{ |
96
|
0
|
|
|
|
|
|
$self->{'dbh'}{'slave'}{$id} = $dbh; |
97
|
0
|
|
|
|
|
|
$dbh->{'RaiseError'} = 1; |
98
|
0
|
|
|
|
|
|
return $dbh; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
0
|
|
|
|
|
|
return; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub commit { |
106
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
107
|
0
|
|
|
|
|
|
while(my($id,$types) = each %{$self->{'dbh'}}){ |
|
0
|
|
|
|
|
|
|
108
|
0
|
0
|
|
|
|
|
if(ref $types eq 'HASH'){ |
109
|
0
|
|
|
|
|
|
for my $type (keys %{$types}){ |
|
0
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
my $dbh = $self->{'dbh'}->{$id}->{$type}; |
111
|
0
|
0
|
|
|
|
|
$dbh->commit if(ref $dbh eq 'DBI::db'); |
112
|
0
|
0
|
|
|
|
|
warn "commit:$id,$type" if(defined $ENV{'MOJO_MYSQL_DEBUG'}); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub rollback { |
119
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
120
|
0
|
|
|
|
|
|
while(my($id,$types) = each %{$self->{'dbh'}}){ |
|
0
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
|
if(ref $types eq 'HASH'){ |
122
|
0
|
|
|
|
|
|
for my $type (keys %{$types}){ |
|
0
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
my $dbh = $self->{'dbh'}->{$id}->{$type}; |
124
|
0
|
0
|
|
|
|
|
$dbh->rollback if(ref $dbh eq 'DBI::db'); |
125
|
0
|
0
|
|
|
|
|
warn "rollback:$id,$type" if(defined $ENV{'MOJO_MYSQL_DEBUG'}); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub disconnect { |
132
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
133
|
0
|
|
|
|
|
|
while(my($id,$types) = each %{$self->{'dbh'}}){ |
|
0
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
if(ref $types eq 'HASH'){ |
135
|
0
|
|
|
|
|
|
for my $type (keys %{$types}){ |
|
0
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
my $dbh = $self->{'dbh'}->{$id}->{$type}; |
137
|
0
|
0
|
|
|
|
|
$dbh->disconnect if(ref $dbh eq 'DBI::db'); |
138
|
0
|
0
|
|
|
|
|
warn "disconnect:$id,$type" if(defined $ENV{'MOJO_MYSQL_DEBUG'}); |
139
|
0
|
|
|
|
|
|
delete $self->{'dbh'}; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub flush { |
146
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
147
|
0
|
|
|
|
|
|
$self->id('_default'); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
1; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=encoding utf8 |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 NAME |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
MojoX::Mysql::DB - Connect DB |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 SYNOPSIS |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
use MojoX::Mysql; |
162
|
|
|
|
|
|
|
use Mojo::Util qw(dumper); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
my %config = ( |
165
|
|
|
|
|
|
|
user=>'root', |
166
|
|
|
|
|
|
|
password=>undef, |
167
|
|
|
|
|
|
|
server=>[ |
168
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', type=>'master'}, |
169
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', type=>'slave'}, |
170
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>1, type=>'master'}, |
171
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>1, type=>'slave'}, |
172
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>2, type=>'master'}, |
173
|
|
|
|
|
|
|
{dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>2, type=>'slave'}, |
174
|
|
|
|
|
|
|
] |
175
|
|
|
|
|
|
|
); |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
my $mysql = MojoX::Mysql->new(%config); |
178
|
|
|
|
|
|
|
my $dbh = $mysql->db->connect_master; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 id |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
$mysql->id(1); # choice id server |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 METHODS |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 connect_master |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
$mysql->db->connect_master; |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Return connect object. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head2 connect_slave |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
$mysql->db->connect_slave; |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Return connect object. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head2 commit |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
$mysql->db->commit; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Commit on all servers |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head2 rollback |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
$mysql->db->rollback; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Rollback on all servers |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head2 disconnect |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
$mysql->db->disconnect; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Disconnect on all servers |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=cut |