line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::DB::Mysql; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
18094
|
use 5.014; |
|
1
|
|
|
|
|
3
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
19
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use Venus::Class; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
with 'Venus::Role::Optional'; |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
1765
|
use DBI; |
|
1
|
|
|
|
|
16961
|
|
|
1
|
|
|
|
|
909
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# ATTRIBUTES |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
attr 'dbh'; |
21
|
|
|
|
|
|
|
attr 'dsn'; |
22
|
|
|
|
|
|
|
attr 'hostname'; |
23
|
|
|
|
|
|
|
attr 'hostport'; |
24
|
|
|
|
|
|
|
attr 'initial'; |
25
|
|
|
|
|
|
|
attr 'uri'; |
26
|
|
|
|
|
|
|
attr 'username'; |
27
|
|
|
|
|
|
|
attr 'password'; |
28
|
|
|
|
|
|
|
attr 'database'; |
29
|
|
|
|
|
|
|
attr 'template'; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# OPTIONS |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub lazy_build_dbh { |
34
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
0
|
|
|
|
$data ||= DBI->connect($self->dsn, $self->username, $self->password, { |
37
|
|
|
|
|
|
|
RaiseError => 1, |
38
|
|
|
|
|
|
|
AutoCommit => 1 |
39
|
|
|
|
|
|
|
}); |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
return $data; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub lazy_build_dsn { |
45
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
return $self->dsngen($self->database); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub lazy_build_hostname { |
51
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
52
|
|
|
|
|
|
|
|
53
|
0
|
0
|
|
|
|
|
return $data ? $data : $ENV{TESTDB_HOSTNAME}; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub lazy_build_hostport { |
57
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
58
|
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
|
return $data ? $data : $ENV{TESTDB_HOSTPORT}; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub lazy_build_initial { |
63
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
0
|
|
|
|
return $data ? $data : $ENV{TESTDB_INITIAL} || 'mysql'; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub lazy_build_uri { |
69
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
return $self->urigen($self->database); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub lazy_build_username { |
75
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
0
|
|
|
|
return $data ? $data : $ENV{TESTDB_USERNAME} || 'root'; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub lazy_build_password { |
81
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
0
|
|
|
|
return $data ? $data : $ENV{TESTDB_PASSWORD} || ''; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub lazy_build_database { |
87
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
return $data ? $data : join '_', 'testing_db', time, $$, sprintf "%04d", rand 999; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub lazy_build_template { |
93
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
return $data ? $data : $ENV{TESTDB_TEMPLATE}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# METHODS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub create { |
101
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
my $dbh = DBI->connect($self->dsngen($self->initial), |
104
|
|
|
|
|
|
|
$self->username, |
105
|
|
|
|
|
|
|
$self->password, |
106
|
|
|
|
|
|
|
{ |
107
|
|
|
|
|
|
|
RaiseError => 1, |
108
|
|
|
|
|
|
|
AutoCommit => 1 |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare(qq(CREATE DATABASE `@{[$self->database]}`)); |
|
0
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
$sth->execute; |
115
|
0
|
|
|
|
|
|
$dbh->disconnect; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
$self->dbh; |
118
|
0
|
|
|
|
|
|
$self->uri; |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
return $self; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub destroy { |
124
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
125
|
|
|
|
|
|
|
|
126
|
0
|
0
|
|
|
|
|
$self->dbh->disconnect if $self->{dbh}; |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
my $dbh = DBI->connect($self->dsngen($self->initial), |
129
|
|
|
|
|
|
|
$self->username, |
130
|
|
|
|
|
|
|
$self->password, |
131
|
|
|
|
|
|
|
{ |
132
|
|
|
|
|
|
|
RaiseError => 1, |
133
|
|
|
|
|
|
|
AutoCommit => 1 |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
); |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare(qq(DROP DATABASE `@{[$self->database]}`)); |
|
0
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
$sth->execute; |
140
|
0
|
|
|
|
|
|
$dbh->disconnect; |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
return $self; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub dsngen { |
146
|
0
|
|
|
0
|
0
|
|
my ($self, $name) = @_; |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
my $hostname = $self->hostname; |
149
|
0
|
|
|
|
|
|
my $hostport = $self->hostport; |
150
|
|
|
|
|
|
|
|
151
|
0
|
0
|
|
|
|
|
return join ';', "dbi:mysql:database=$name", join ';', |
|
|
0
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
($hostname ? ("host=@{[$hostname]}") : ()), |
153
|
0
|
|
|
|
|
|
($hostport ? ("port=@{[$hostport]}") : ()) |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub urigen { |
157
|
0
|
|
|
0
|
0
|
|
my ($self, $name) = @_; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
my $username = $self->username; |
160
|
0
|
|
|
|
|
|
my $password = $self->password; |
161
|
0
|
|
|
|
|
|
my $hostname = $self->hostname; |
162
|
0
|
|
|
|
|
|
my $hostport = $self->hostport; |
163
|
|
|
|
|
|
|
|
164
|
0
|
0
|
|
|
|
|
return join( |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
165
|
|
|
|
|
|
|
'/', 'mysql', |
166
|
|
|
|
|
|
|
($username ? '' : ()), |
167
|
|
|
|
|
|
|
( |
168
|
|
|
|
|
|
|
$username |
169
|
|
|
|
|
|
|
? join('@', |
170
|
|
|
|
|
|
|
join(':', $username ? ($username, ($password ? $password : ())) : ()), |
171
|
|
|
|
|
|
|
$hostname |
172
|
|
|
|
|
|
|
? ($hostport ? (join(':', $hostname, $hostport)) : $hostname) |
173
|
|
|
|
|
|
|
: '') |
174
|
|
|
|
|
|
|
: () |
175
|
|
|
|
|
|
|
), |
176
|
|
|
|
|
|
|
$name |
177
|
|
|
|
|
|
|
) |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
1; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 NAME |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Test::DB::Mysql - Temporary Testing Databases for Mysql |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=cut |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 ABSTRACT |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Temporary Mysql Database for Testing |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=cut |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 VERSION |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
0.10 |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=cut |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head1 SYNOPSIS |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
package main; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
use Test::DB::Mysql; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
my $tdbo = Test::DB::Mysql->new; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
# my $dbh = $tdbo->create->dbh; |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=cut |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head1 DESCRIPTION |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
This package provides methods for generating and destroying Mysql databases for |
217
|
|
|
|
|
|
|
testing purposes. The attributes can be set using their respective environment |
218
|
|
|
|
|
|
|
variables: C, C, C, |
219
|
|
|
|
|
|
|
C, and C. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=cut |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
This package has the following attributes: |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 dbh |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
dbh(Object) |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Object)> values, and is optional. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=cut |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head2 dsn |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
dsn(Str) |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=cut |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 database |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
database(Str) |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=cut |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=head2 hostname |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
hostname(Str) |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=cut |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head2 hostport |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
hostport(Str) |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=cut |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head2 uri |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
uri(Str) |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=cut |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head2 username |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
username(Str) |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=cut |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=head2 password |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
password(Str) |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
This attribute is read-only, accepts C<(Str)> values, and is optional. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=cut |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head1 METHODS |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
This package provides the following methods: |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=cut |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=head2 create |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
create() : Object |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
The create method creates a temporary database and returns the invocant. |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=over 4 |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=item create example 1 |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
# given: synopsis |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
$tdbo->create; |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
# |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=back |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=cut |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=head2 destroy |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
destroy() : Object |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
The destroy method destroys (drops) the database and returns the invocant. |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
=over 4 |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=item destroy example 1 |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
# given: synopsis |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
$tdbo->create; |
332
|
|
|
|
|
|
|
$tdbo->destroy; |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
# |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
=back |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
=cut |