save point working environment variables
This commit is contained in:
parent
3ddd822c15
commit
c835810c0e
4 changed files with 105 additions and 5 deletions
79
lib/Environment/SQLite.pm
Normal file
79
lib/Environment/SQLite.pm
Normal file
|
@ -0,0 +1,79 @@
|
|||
package Environment::SQLite;
|
||||
|
||||
use DBI;
|
||||
use Cwd;
|
||||
|
||||
sub new {
|
||||
my ($class, %args) = @_;
|
||||
|
||||
my $self = bless {
|
||||
db_path => $args{db_path} || 'pshell_env.db'
|
||||
}, $class;
|
||||
|
||||
# Convert to absolute path
|
||||
$self->{db_path} = getcwd() . '/' . $self->{db_path} unless $self->{db_path} =~ m|^/|;
|
||||
|
||||
$self->_init_db;
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub _init_db {
|
||||
my ($self) = @_;
|
||||
|
||||
my $dbh = DBI->connect("dbi:SQLite:dbname=$self->{db_path}", "", "",
|
||||
{ RaiseError => 1, PrintError => 0 });
|
||||
|
||||
$dbh->do(<<"SQL");
|
||||
CREATE TABLE IF NOT EXISTS environment (
|
||||
name TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
)
|
||||
SQL
|
||||
|
||||
$dbh->disconnect;
|
||||
}
|
||||
|
||||
sub set {
|
||||
my ($self, $name, $value) = @_;
|
||||
|
||||
my $dbh = DBI->connect("dbi:SQLite:dbname=$self->{db_path}", "", "",
|
||||
{ RaiseError => 1, PrintError => 0 });
|
||||
|
||||
$dbh->do("REPLACE INTO environment (name, value) VALUES (?, ?)", undef, $name, $value);
|
||||
|
||||
$dbh->disconnect;
|
||||
}
|
||||
|
||||
sub get {
|
||||
my ($self, $name) = @_;
|
||||
|
||||
my $dbh = DBI->connect("dbi:SQLite:dbname=$self->{db_path}", "", "",
|
||||
{ RaiseError => 1, PrintError => 0 });
|
||||
|
||||
my ($value) = $dbh->selectrow_array(
|
||||
"SELECT value FROM environment WHERE name = ?", undef, $name);
|
||||
|
||||
$dbh->disconnect;
|
||||
return $value;
|
||||
}
|
||||
|
||||
sub get_all {
|
||||
my ($self) = @_;
|
||||
|
||||
my $dbh = DBI->connect("dbi:SQLite:dbname=$self->{db_path}", "", "",
|
||||
{ RaiseError => 1, PrintError => 0 });
|
||||
|
||||
my $sth = $dbh->prepare("SELECT name, value FROM environment");
|
||||
$sth->execute;
|
||||
|
||||
my %env;
|
||||
while (my $row = $sth->fetchrow_arrayref) {
|
||||
$env{$row->[0]} = $row->[1];
|
||||
}
|
||||
|
||||
$dbh->disconnect;
|
||||
return %env;
|
||||
}
|
||||
|
||||
1;
|
Loading…
Add table
Add a link
Reference in a new issue