App-AltSQL
view release on metacpan or search on metacpan
lib/App/AltSQL/Model/MySQL.pm view on Meta::CPAN
},
password => {
help => '-p | --password=PASSWORD | -pPASSWORD',
},
database => {
cli => 'database|d=s',
help => '-d DATABASE | --database DATABASE',
},
port => {
cli => 'port=i',
help => '--port PORT',
},
no_auto_rehash => {
cli => 'no-auto-rehash|A',
help => "-A --no-auto-rehash -- Don't scan the information schema for tab autocomplete data",
},
);
}
sub setup {
my $self = shift;
$self->find_and_read_configs();
# If the user has configured a custom prompt in .my.cnf and not one in the config, use that in the Term instance
if ($self->prompt && ! $self->app->config->{prompt}) {
$self->app->term->prompt( $self->parse_prompt() );
}
}
sub find_and_read_configs {
my $self = shift;
my @config_paths = (
"$ENV{HOME}/.my.cnf",
);
foreach my $path (@config_paths) {
(-e $path) or next;
$self->read_my_dot_cnf($path);
}
}
sub read_my_dot_cnf {
my $self = shift;
my $path = shift;
my @valid_keys = qw( user password host port database prompt safe_update select_limit no_auto_rehash ); # keys we'll read
my @valid_sections = qw( client mysql ); # valid [section] names
my @boolean_keys = qw( safe_update no_auto_rehash );
open MYCNF, "<$path";
# ignore lines in file until we hit a valid [section]
# then read key=value pairs
my $in_valid_section = 0;
while(<MYCNF>) {
# ignore commented lines:
/^\s*#/ && next;
if (/^\s*\[(.*?)\]\s*$/) { # we've hit a section
# verify that we're inside a valid section,
# and if so, set $in_valid_section
if ( grep $_ eq $1, @valid_sections ) {
$in_valid_section = 1;
} else {
$in_valid_section = 0;
}
} elsif ($in_valid_section) {
# read a key/value pair
#/^\s*(.+?)\s*=\s*(.+?)\s*$/;
#my ($key, $val) = ($1, $2);
my ($key, $val) = split /\s*=\s*/, $_, 2;
# value cleanup
$key =~ s/^\s*(.+?)\s*$/$1/;
$key || next;
$key =~ s/-/_/g;
$val || ( $val = '' );
$val && $val =~ s/\s*$//;
# special case for no_auto_rehash, which is 'skip-auto-rehash' in my.cnf
if ($key eq 'skip_auto_rehash') {
$key = 'no_auto_rehash';
}
# verify that the field is one of the supported ones
unless ( grep $_ eq $key, @valid_keys ) { next; }
# if this key is expected to be a boolean, fix the value
if ( grep $_ eq $key, @boolean_keys ) {
if ($val eq '0' || $val eq 'false') {
$val = 0;
} else {
# this includes empty values
$val = 1;
}
}
# override anything that was set on the commandline with the stuff read from the config.
unless (defined $self->{$key}) { $self->{$key} = $val };
}
}
close MYCNF;
}
sub db_connect {
my $self = shift;
my $dsn = 'DBI:mysql:' . join (';',
map { "$_=" . $self->$_ }
grep { defined $self->$_ }
qw(database host port)
);
my $dbh = DBI->connect($dsn, $self->user, $self->password, {
PrintError => 0,
mysql_auto_reconnect => 1,
mysql_enable_utf8 => 1,
}) or die $DBI::errstr . "\nDSN used: '$dsn'\n";
$self->dbh($dbh);
## Update autocomplete entries
if ($self->database) {
$self->current_database($self->database);
$self->update_autocomplete_entries($self->database);
}
$self->update_db_types();
}
sub update_autocomplete_entries {
my ($self, $database) = @_;
return if $self->no_auto_rehash;
my $cache_key = 'autocomplete_' . $database;
if (! $self->{_cache}{$cache_key}) {
$self->log_debug("Reading table information for completion of table and column names\nYou can turn off this feature to get a quicker startup with -A\n");
my %autocomplete;
my $rows = $self->dbh->selectall_arrayref("select TABLE_NAME, COLUMN_NAME from information_schema.COLUMNS where TABLE_SCHEMA = ?", {}, $database);
foreach my $row (@$rows) {
$autocomplete{$row->[0]} = 1; # Table
$autocomplete{$row->[1]} = 1; # Column
$autocomplete{$row->[0] . '.' . $row->[1]} = 1; # Table.Column
}
$self->{_cache}{$cache_key} = \%autocomplete;
( run in 1.644 second using v1.01-cache-2.11-cpan-99c4e6809bf )