File Coverage

blib/lib/Metabrik/Client/Mysql.pm
Criterion Covered Total %
statement 9 115 7.8
branch 0 68 0.0
condition 0 64 0.0
subroutine 3 14 21.4
pod 1 11 9.0
total 13 272 4.7


line stmt bran cond sub pod time code
1             #
2             # $Id$
3             #
4             # client::mysql Brik
5             #
6             package Metabrik::Client::Mysql;
7 1     1   809 use strict;
  1         6  
  1         30  
8 1     1   6 use warnings;
  1         2  
  1         30  
9              
10 1     1   6 use base qw(Metabrik::Client::Sqlite Metabrik::System::Package);
  1         2  
  1         560  
11              
12             sub brik_properties {
13             return {
14 0     0 1   revision => '$Revision$',
15             tags => [ qw(unstable) ],
16             author => 'GomoR ',
17             license => 'http://opensource.org/licenses/BSD-3-Clause',
18             attributes => {
19             db => [ qw(db_name) ],
20             autocommit => [ qw(0|1) ],
21             host => [ qw(host) ],
22             port => [ qw(port) ],
23             username => [ qw(username) ],
24             password => [ qw(password) ],
25             dbh => [ qw(INTERNAL) ],
26             },
27             attributes_default => {
28             autocommit => 1,
29             db => 'mysql',
30             host => 'localhost',
31             port => 3306,
32             username => 'root',
33             },
34             commands => {
35             install => [ ], # Inherited
36             open => [ qw(db_name|OPTIONAL host|OPTIONAL port|OPTIONAL username|OPTIONAL password|OPTIONAL) ],
37             exec => [ qw(sql_query) ],
38             create => [ qw(table_name fields_array key|OPTIONAL) ],
39             insert => [ qw(table_name data_hash) ],
40             select => [ qw(table_name fields_array|OPTIONAL key|OPTIONAL) ],
41             commit => [ ],
42             show_tables => [ ],
43             list_types => [ ],
44             close => [ ],
45             save => [ qw(db_name output_file) ],
46             load => [ qw(input_file db_name) ],
47             createdb => [ qw(db_name) ],
48             dropdb => [ qw(db_name) ],
49             create_user => [ qw(username password|OPTIONAL) ],
50             grant_all_privileges => [ qw(database username password|OPTIONAL ip_address|OPTIONAL) ],
51             password_prompt => [ qw(string|OPTIONAL) ],
52             enter_shell => [ qw(db_name|OPTIONAL host|OPTIONAL port|OPTIONAL username|OPTIONAL password|OPTIONAL) ],
53             },
54             require_modules => {
55             'DBI' => [ ],
56             'DBD::mysql' => [ ],
57             'Metabrik::String::Password' => [ ],
58             },
59             require_binaries => {
60             'mysql' => [ ],
61             'mysqladmin' => [ ],
62             },
63             need_packages => {
64             ubuntu => [ qw(libmysqlclient-dev mysql-client) ],
65             debian => [ qw(libmysqlclient-dev mysql-client) ],
66             kali => [ qw(libmysqlclient-dev mysql-client) ],
67             },
68             };
69             }
70              
71             sub password_prompt {
72 0     0 0   my $self = shift;
73 0           my ($string) = @_;
74              
75 0 0         my $sp = Metabrik::String::Password->new_from_brik_init($self) or return;
76 0           return $sp->prompt($string);
77             }
78              
79             sub open {
80 0     0 0   my $self = shift;
81 0           my ($db, $host, $port, $username, $password) = @_;
82              
83 0   0       $db ||= $self->db;
84 0   0       $host ||= $self->host;
85 0   0       $port ||= $self->port;
86 0   0       $username ||= $self->username;
87 0 0 0       $password ||= $self->password || $self->password_prompt("Enter $username password: ") or return;
      0        
88 0 0         $self->brik_help_run_undef_arg('open', $db) or return;
89 0 0         $self->brik_help_run_undef_arg('open', $host) or return;
90 0 0         $self->brik_help_run_undef_arg('open', $port) or return;
91 0 0         $self->brik_help_run_undef_arg('open', $username) or return;
92              
93 0           my $dbh = DBI->connect("DBI:mysql:database=$db;host=$host;port=$port", $username, $password, {
94             AutoCommit => $self->autocommit,
95             RaiseError => 1,
96             PrintError => 0,
97             PrintWarn => 0,
98             #HandleError => sub {
99             #my ($errstr, $dbh, $arg) = @_;
100             #die("DBI: $errstr\n");
101             #},
102             });
103 0 0         if (! $dbh) {
104 0           return $self->log->error("open: DBI: $DBI::errstr");
105             }
106              
107 0           $self->dbh($dbh);
108              
109 0           return 1;
110             }
111              
112             sub show_tables {
113 0     0 0   my $self = shift;
114              
115 0           my $dbh = $self->dbh;
116 0 0         $self->brik_help_run_undef_arg('open', $dbh) or return;
117              
118 0           my @tables = map { s/.*\.//; s/`//g; $_ } $dbh->tables;
  0            
  0            
  0            
119              
120 0           return \@tables;
121             }
122              
123             sub createdb {
124 0     0 0   my $self = shift;
125 0           my ($db) = @_;
126              
127 0 0         $self->brik_help_run_undef_arg('createdb', $db) or return;
128              
129 0           my $host = $self->host;
130 0           my $port = $self->port;
131 0           my $username = $self->username;
132 0 0 0       my $password = $self->password || $self->password_prompt("Enter $username password: ") or return;
133              
134 0 0         my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return;
135              
136             # mysqladmin create database
137 0           return $sc->system("mysqladmin -h $host --port=$port -u $username --password=$password create $db");
138             }
139              
140             sub dropdb {
141 0     0 0   my $self = shift;
142 0           my ($db) = @_;
143              
144 0 0         $self->brik_help_run_undef_arg('dropdb', $db) or return;
145              
146 0           my $host = $self->host;
147 0           my $port = $self->port;
148 0           my $username = $self->username;
149 0 0 0       my $password = $self->password || $self->password_prompt("Enter $username password: ") or return;
150              
151 0 0         my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return;
152              
153             # mysqladmin drop database
154 0           return $sc->system("mysqladmin -h $host --port=$port -u $username --password=$password drop $db");
155             }
156              
157             sub save {
158 0     0 0   my $self = shift;
159 0           my ($db, $filename) = @_;
160              
161 0 0         $self->brik_help_run_undef_arg('save', $db) or return;
162 0 0         $self->brik_help_run_undef_arg('save', $filename) or return;
163              
164 0           my $host = $self->host;
165 0           my $port = $self->port;
166 0           my $username = $self->username;
167 0 0 0       my $password = $self->password || $self->password_prompt("Enter $username password: ") or return;
168              
169 0 0         my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return;
170              
171             # mysqldump -h hostname -u user --password=password databasename > filename
172 0           return $sc->system("mysqldump -h $host --port=$port -u $username --password=$password $db > $filename");
173             }
174              
175             sub load {
176 0     0 0   my $self = shift;
177 0           my ($filename, $db) = @_;
178              
179 0 0         $self->brik_help_run_undef_arg('load', $filename) or return;
180 0 0         $self->brik_help_run_file_not_found('load', $filename) or return;
181 0 0         $self->brik_help_run_undef_arg('load', $db) or return;
182              
183 0           my $host = $self->host;
184 0           my $port = $self->port;
185 0           my $username = $self->username;
186 0 0 0       my $password = $self->password || $self->password_prompt("Enter $username password: ") or return;
187              
188 0 0         my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return;
189              
190             # mysql -h hostname -u user --password=password databasename < filename
191 0           return $sc->system("mysql -h $host --port=$port -u $username --password=$password $db < $filename");
192             }
193              
194             sub create_user {
195 0     0 0   my $self = shift;
196 0           my ($username, $password, $ip) = @_;
197              
198 0   0       $ip ||= '%';
199 0 0         $self->brik_help_run_undef_arg('create_user', $username) or return;
200              
201 0           my $mysql_host = $self->host;
202 0           my $mysql_port = $self->port;
203 0           my $mysql_username = $self->username;
204 0 0 0       my $mysql_password = $self->password || $self->password_prompt("Enter $mysql_username password: ") or return;
205              
206 0 0 0       $password ||= $self->password_prompt("Enter $username password: ") or return;
207              
208 0 0         my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return;
209              
210 0           my $cmd = "mysql -h $mysql_host --port=$mysql_port -u $mysql_username --password=$mysql_password --execute=\"create user $username\@'$ip' identified by '$password'\" mysql";
211              
212 0           $self->log->debug("create_user: cmd [$cmd]");
213              
214 0           return $sc->system($cmd);
215             }
216              
217             sub grant_all_privileges {
218 0     0 0   my $self = shift;
219 0           my ($db, $username, $password, $ip) = @_;
220              
221 0   0       $ip ||= '%';
222 0 0         $self->brik_help_run_undef_arg('grant_all_privileges', $db) or return;
223 0 0         $self->brik_help_run_undef_arg('grant_all_privileges', $username) or return;
224              
225 0           my $mysql_host = $self->host;
226 0           my $mysql_port = $self->port;
227 0           my $mysql_username = $self->username;
228 0 0 0       my $mysql_password = $self->password || $self->password_prompt("Enter $mysql_username password: ") or return;
229              
230 0 0 0       $password ||= $self->password_prompt("Enter $username password: ") or return;
231              
232 0 0         my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return;
233              
234 0           my $cmd = "mysql -h $mysql_host --port=$mysql_port -u $mysql_username --password=$mysql_password --execute=\"grant all privileges on $db.* to $username\@'$ip' identified by '$password'\" mysql";
235              
236 0           $self->log->debug("grant_all_privileges: cmd [$cmd]");
237              
238 0           return $sc->system($cmd);
239             }
240              
241             sub enter_shell {
242 0     0 0   my $self = shift;
243 0           my ($mysql_db, $mysql_host, $mysql_port, $mysql_username, $mysql_password) = @_;
244              
245 0   0       $mysql_db ||= $self->db;
246 0   0       $mysql_host ||= $self->host;
247 0   0       $mysql_port ||= $self->port;
248 0   0       $mysql_username ||= $self->username;
249 0 0 0       $mysql_password ||= $self->password || $self->password_prompt("Enter $mysql_username password: ") or return;
      0        
250              
251 0 0         my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return;
252              
253 0           my $cmd = "mysql -h $mysql_host --port=$mysql_port -u $mysql_username --password=$mysql_password mysql";
254              
255 0           $self->log->debug("shell: cmd [$cmd]");
256              
257 0           return $sc->system($cmd);
258             }
259              
260             1;
261              
262             __END__