| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # client::mysql Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Client::Mysql; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::Client::Sqlite Metabrik::System::Package); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 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 | my $self = shift; | ||
| 73 | my ($string) = @_; | ||
| 74 | |||
| 75 | my $sp = Metabrik::String::Password->new_from_brik_init($self) or return; | ||
| 76 | return $sp->prompt($string); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub open { | ||
| 80 | my $self = shift; | ||
| 81 | my ($db, $host, $port, $username, $password) = @_; | ||
| 82 | |||
| 83 | $db ||= $self->db; | ||
| 84 | $host ||= $self->host; | ||
| 85 | $port ||= $self->port; | ||
| 86 | $username ||= $self->username; | ||
| 87 | $password ||= $self->password || $self->password_prompt("Enter $username password: ") or return; | ||
| 88 | $self->brik_help_run_undef_arg('open', $db) or return; | ||
| 89 | $self->brik_help_run_undef_arg('open', $host) or return; | ||
| 90 | $self->brik_help_run_undef_arg('open', $port) or return; | ||
| 91 | $self->brik_help_run_undef_arg('open', $username) or return; | ||
| 92 | |||
| 93 | 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 | if (! $dbh) { | ||
| 104 | return $self->log->error("open: DBI: $DBI::errstr"); | ||
| 105 | } | ||
| 106 | |||
| 107 | $self->dbh($dbh); | ||
| 108 | |||
| 109 | return 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub show_tables { | ||
| 113 | my $self = shift; | ||
| 114 | |||
| 115 | my $dbh = $self->dbh; | ||
| 116 | $self->brik_help_run_undef_arg('open', $dbh) or return; | ||
| 117 | |||
| 118 | my @tables = map { s/.*\.//; s/`//g; $_ } $dbh->tables; | ||
| 119 | |||
| 120 | return \@tables; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub createdb { | ||
| 124 | my $self = shift; | ||
| 125 | my ($db) = @_; | ||
| 126 | |||
| 127 | $self->brik_help_run_undef_arg('createdb', $db) or return; | ||
| 128 | |||
| 129 | my $host = $self->host; | ||
| 130 | my $port = $self->port; | ||
| 131 | my $username = $self->username; | ||
| 132 | my $password = $self->password || $self->password_prompt("Enter $username password: ") or return; | ||
| 133 | |||
| 134 | my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; | ||
| 135 | |||
| 136 | # mysqladmin create database | ||
| 137 | return $sc->system("mysqladmin -h $host --port=$port -u $username --password=$password create $db"); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub dropdb { | ||
| 141 | my $self = shift; | ||
| 142 | my ($db) = @_; | ||
| 143 | |||
| 144 | $self->brik_help_run_undef_arg('dropdb', $db) or return; | ||
| 145 | |||
| 146 | my $host = $self->host; | ||
| 147 | my $port = $self->port; | ||
| 148 | my $username = $self->username; | ||
| 149 | my $password = $self->password || $self->password_prompt("Enter $username password: ") or return; | ||
| 150 | |||
| 151 | my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; | ||
| 152 | |||
| 153 | # mysqladmin drop database | ||
| 154 | return $sc->system("mysqladmin -h $host --port=$port -u $username --password=$password drop $db"); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub save { | ||
| 158 | my $self = shift; | ||
| 159 | my ($db, $filename) = @_; | ||
| 160 | |||
| 161 | $self->brik_help_run_undef_arg('save', $db) or return; | ||
| 162 | $self->brik_help_run_undef_arg('save', $filename) or return; | ||
| 163 | |||
| 164 | my $host = $self->host; | ||
| 165 | my $port = $self->port; | ||
| 166 | my $username = $self->username; | ||
| 167 | my $password = $self->password || $self->password_prompt("Enter $username password: ") or return; | ||
| 168 | |||
| 169 | my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; | ||
| 170 | |||
| 171 | # mysqldump -h hostname -u user --password=password databasename > filename | ||
| 172 | return $sc->system("mysqldump -h $host --port=$port -u $username --password=$password $db > $filename"); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub load { | ||
| 176 | my $self = shift; | ||
| 177 | my ($filename, $db) = @_; | ||
| 178 | |||
| 179 | $self->brik_help_run_undef_arg('load', $filename) or return; | ||
| 180 | $self->brik_help_run_file_not_found('load', $filename) or return; | ||
| 181 | $self->brik_help_run_undef_arg('load', $db) or return; | ||
| 182 | |||
| 183 | my $host = $self->host; | ||
| 184 | my $port = $self->port; | ||
| 185 | my $username = $self->username; | ||
| 186 | my $password = $self->password || $self->password_prompt("Enter $username password: ") or return; | ||
| 187 | |||
| 188 | my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; | ||
| 189 | |||
| 190 | # mysql -h hostname -u user --password=password databasename < filename | ||
| 191 | return $sc->system("mysql -h $host --port=$port -u $username --password=$password $db < $filename"); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub create_user { | ||
| 195 | my $self = shift; | ||
| 196 | my ($username, $password, $ip) = @_; | ||
| 197 | |||
| 198 | $ip ||= '%'; | ||
| 199 | $self->brik_help_run_undef_arg('create_user', $username) or return; | ||
| 200 | |||
| 201 | my $mysql_host = $self->host; | ||
| 202 | my $mysql_port = $self->port; | ||
| 203 | my $mysql_username = $self->username; | ||
| 204 | my $mysql_password = $self->password || $self->password_prompt("Enter $mysql_username password: ") or return; | ||
| 205 | |||
| 206 | $password ||= $self->password_prompt("Enter $username password: ") or return; | ||
| 207 | |||
| 208 | my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; | ||
| 209 | |||
| 210 | 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 | $self->log->debug("create_user: cmd [$cmd]"); | ||
| 213 | |||
| 214 | return $sc->system($cmd); | ||
| 215 | } | ||
| 216 | |||
| 217 | sub grant_all_privileges { | ||
| 218 | my $self = shift; | ||
| 219 | my ($db, $username, $password, $ip) = @_; | ||
| 220 | |||
| 221 | $ip ||= '%'; | ||
| 222 | $self->brik_help_run_undef_arg('grant_all_privileges', $db) or return; | ||
| 223 | $self->brik_help_run_undef_arg('grant_all_privileges', $username) or return; | ||
| 224 | |||
| 225 | my $mysql_host = $self->host; | ||
| 226 | my $mysql_port = $self->port; | ||
| 227 | my $mysql_username = $self->username; | ||
| 228 | my $mysql_password = $self->password || $self->password_prompt("Enter $mysql_username password: ") or return; | ||
| 229 | |||
| 230 | $password ||= $self->password_prompt("Enter $username password: ") or return; | ||
| 231 | |||
| 232 | my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; | ||
| 233 | |||
| 234 | 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 | $self->log->debug("grant_all_privileges: cmd [$cmd]"); | ||
| 237 | |||
| 238 | return $sc->system($cmd); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub enter_shell { | ||
| 242 | my $self = shift; | ||
| 243 | my ($mysql_db, $mysql_host, $mysql_port, $mysql_username, $mysql_password) = @_; | ||
| 244 | |||
| 245 | $mysql_db ||= $self->db; | ||
| 246 | $mysql_host ||= $self->host; | ||
| 247 | $mysql_port ||= $self->port; | ||
| 248 | $mysql_username ||= $self->username; | ||
| 249 | $mysql_password ||= $self->password || $self->password_prompt("Enter $mysql_username password: ") or return; | ||
| 250 | |||
| 251 | my $sc = Metabrik::Shell::Command->new_from_brik_init($self) or return; | ||
| 252 | |||
| 253 | my $cmd = "mysql -h $mysql_host --port=$mysql_port -u $mysql_username --password=$mysql_password mysql"; | ||
| 254 | |||
| 255 | $self->log->debug("shell: cmd [$cmd]"); | ||
| 256 | |||
| 257 | return $sc->system($cmd); | ||
| 258 | } | ||
| 259 | |||
| 260 | 1; | ||
| 261 | |||
| 262 | __END__ | ||