initial add
This commit is contained in:
commit
1e414f35c9
6 changed files with 146 additions and 0 deletions
63
lib/History/SQLite.pm
Normal file
63
lib/History/SQLite.pm
Normal file
|
@ -0,0 +1,63 @@
|
|||
package History::SQLite;
|
||||
|
||||
use DBI;
|
||||
|
||||
sub new {
|
||||
my ($class, %args) = @_;
|
||||
|
||||
my $self = bless {
|
||||
db_path => $args{db_path} || 'command_history.db'
|
||||
}, $class;
|
||||
|
||||
$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 command_history (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
command TEXT NOT NULL,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
SQL
|
||||
|
||||
$dbh->disconnect;
|
||||
}
|
||||
|
||||
sub add {
|
||||
my ($self, $command) = @_;
|
||||
|
||||
my $dbh = DBI->connect("dbi:SQLite:dbname=$self->{db_path}", "", "",
|
||||
{ RaiseError => 1, PrintError => 0 });
|
||||
|
||||
$dbh->do("INSERT INTO command_history (command) VALUES (?)", undef, $command);
|
||||
|
||||
$dbh->disconnect;
|
||||
}
|
||||
|
||||
sub get_all {
|
||||
my ($self) = @_;
|
||||
|
||||
my $dbh = DBI->connect("dbi:SQLite:dbname=$self->{db_path}", "", "",
|
||||
{ RaiseError => 1, PrintError => 0 });
|
||||
|
||||
my $sth = $dbh->prepare("SELECT command FROM command_history ORDER BY timestamp");
|
||||
$sth->execute;
|
||||
|
||||
my @commands;
|
||||
while (my $row = $sth->fetchrow_arrayref) {
|
||||
push @commands, $row->[0];
|
||||
}
|
||||
|
||||
$dbh->disconnect;
|
||||
return @commands;
|
||||
}
|
||||
|
||||
1;
|
Loading…
Add table
Add a link
Reference in a new issue