-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdbtestdata.pl
198 lines (160 loc) · 4.31 KB
/
dbtestdata.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#! perl --
{
package dbtestdata;
use strict;
use warnings;
use data::VariableDataGenerator;
use sql::VariableSQLGenerator;
use utf8;
use DBI;
use Getopt::Long;
use IO::Handle;
##
# DBテストデータ作成ツール。
#
# usage:
# perl dbtestdata.pl insert|update|delete OPTIONS
##
my $PULSE_COMMIT = 10000;
our %options = (
'conf' => []
);
exit main();
##
# エントリポイント
##
sub main {
GetOptions(
\%options,
"username:s",
"password",
"database:s",
"hostname:s",
"conf:s@"
);
my $mode = $ARGV[0];
my @confs = @{ $options{'conf'} || [] };
if ($mode !~ m/^(insert|update|delete)$/) {
die "unknown mode";
}
map{ -f($_) || die "conf not found: $_"; }@confs;
my $db = getConnection();
foreach my $conffile (@confs) {
my $config = require($conffile);
my $proc = "main_${mode}";
STDOUT->print("<", $config->{'name'}, ">\n");
no strict 'refs';
$proc->($db, $config);
}
STDOUT->print("\n");
STDOUT->print("ended.\n");
return 0;
}
##
# DBIを返す
##
sub getConnection() {
my $password = undef;
if ($options{'password'}) {
while (1) {
STDERR->print("password: ");
STDERR->flush();
$password = STDIN->getline();
chomp($password);
$password && last;
}
}
my $db = DBI->connect(
sprintf("dbi:mysql:%s:%s", $options{'database'} || '', $options{'hostname'} || ''),
$options{'username'},
$password
) || die $DBI::error;
# ここがMySQL5依存(^^;
$db->do("SET NAMES utf8");
$db->do("SET SESSION wait_timeout = 1000000");
return $db;
}
##
# テストデータのINSERTをします
##
sub main_insert {
my($db, $config) = @_;
local $| = 1;
$db->{AutoCommit} = 0;
my $confInsert = $config->{'insert'};
while (my($table, $conf) = each(%$confInsert)) {
STDOUT->print("INSERT $table\n");
my $count = 0;
while ($count < $conf->{'count'}) {
$count++;
$db->do(
INSERT_SQL(
$table,
(ref($conf->{'clazz'}) eq 'CODE') ? $conf->{'clazz'}->($count) : $conf->{'clazz'}
)
) || die $DBI::error;
if (! ($count % $PULSE_COMMIT)) {
my $bar = $count % ($PULSE_COMMIT*2) ? '|' : '-';
$db->commit() || die $DBI::error;
STDOUT->print("\r$bar $count commited.");
}
}
$db->commit() || die $DBI::error;
STDOUT->print("\r+ $count commited.\n");
STDOUT->print("finished.\n");
}
}
##
# テストデータのUPDATEをします
##
sub main_update {
my($db, $config) = @_;
local $| = 1;
$db->{AutoCommit} = 0;
my $confUpdate = $config->{'update'};
while (my($table, $conf) = each(%$confUpdate)) {
STDOUT->print("UPDATE $table\n");
my $sql = sprintf("SELECT %s FROM %s", $conf->{'primary'}, $table);
my $sth = $db->prepare($sql) || die $DBI::error;
$sth->execute() || die $DBI::error;
my @primaryKeys;
while (my $row = $sth->fetchrow_arrayref()) {
push(@primaryKeys, $row->[0]);
}
my $count = 0;
foreach my $pk (@primaryKeys) {
$count++;
$db->do(
UPDATE_SQL(
$table,
(ref($conf->{'clazz'}) eq 'CODE') ? $conf->{'clazz'}->($pk) : $conf->{'clazz'},
[ WHERE($conf->{'primary'}, $pk) ]
)
) || die $DBI::error;
if (! ($count % $PULSE_COMMIT)) {
my $bar = $count % ($PULSE_COMMIT*2) ? '|' : '-';
$db->commit() || die $DBI::error;
STDOUT->print("\r$bar $count commited.");
}
}
$db->commit() || die $DBI::error;
STDOUT->print("\r+ $count commited.\n");
STDOUT->print("finished.\n");
}
}
##
# レコードを全DELETEします
##
sub main_delete {
my($db, $config) = @_;
$db->{AutoCommit} = 1;
my $confDelete = $config->{'delete'};
foreach my $t (@$confDelete) {
STDOUT->print("DELETE $t\n");
$db->do("DELETE FROM $t") || die $DBI::error;
STDOUT->print("finished.\n");
}
}
1;
}
__END__