PM/Database.pm

67 lines
1.2 KiB
Perl
Raw Normal View History

2014-04-27 23:11:52 +04:00
package Database;
use DBI;
2014-04-28 12:04:17 +04:00
use GPG;
2014-04-27 23:11:52 +04:00
sub new {
my $class = shift;
2014-04-28 00:04:16 +04:00
# Get home dir
2014-04-28 12:04:17 +04:00
my $home = $ENV->{HOME};
2014-04-28 00:04:16 +04:00
2014-04-27 23:11:52 +04:00
my $self = {
2014-04-28 12:04:17 +04:00
_home => $home,
2014-04-27 23:11:52 +04:00
};
bless $self, $class;
return $self;
}
2014-04-27 23:35:58 +04:00
2014-04-28 00:04:16 +04:00
sub connect {
my ($self) = @_;
my $home = $self->{_home};
my $db_file = $home . "/.PM/db.sqlite";
my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file","","");
return $dbh;
}
sub mdo {
2014-04-28 12:04:17 +04:00
my ($self, $query, $type) = @_;
2014-04-28 00:04:16 +04:00
my $dbh = $self->{_dbh};
}
sub create_base {
my ($self) = @_;
my $home = $self->{_home};
my $pm_dir = $home."/.PM/";
# Check dir
2014-04-28 12:04:17 +04:00
if (!(-d $pm_dir)) {
2014-04-28 00:04:16 +04:00
# Create dirrectory
@mkdir_cmd = ("mkdir", "$pm_dir");
system(@mkdir_cmd) == 0 or die "Cannot create dir $pm_dir: $!\n";
# Create DB file
@createdb_cmd = ("touch", "$pm_dir/db.sqlite");
system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n";
# Create table. TODO: write this
my $dbh = DBI->connect("dbi:SQLite:dbname=$pm_dir/db.sqlite","","");
print "Create database schema\n";
my $q_table = "create table passwords(name VARCHAR(32), resource TEXT, password TEXT)";
2014-04-28 12:04:17 +04:00
$dbh->do($q_table);
# Encrypt db
# TODO: write this
2014-04-28 00:04:16 +04:00
return 0;
}
else {
print "Dirrectory is exist!\n";
return 0;
}
}
2014-04-27 23:35:58 +04:00
1;