App-I18N
view release on metacpan or search on metacpan
lib/App/I18N/DB.pm view on Meta::CPAN
package App::I18N::DB;
use warnings;
use strict;
use Any::Moose;
use Encode;
use DBI;
use DBD::SQLite;
has dbh =>
( is => 'rw' );
sub BUILD {
my ($self,$args) = @_;
if( $args->{path} ) {
$self->connect( $args->{path} );
}
elsif( $args->{name} ) {
my $dbname = $args->{name};
my $dbpath = File::Spec->join( $ENV{HOME} , $dbname );
$self->connect( $dbpath );
}
elsif( ! $args->{dbh} ) {
print "Importing database schema\n";
my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:","","",
{ RaiseError => 1, sqlite_unicode => 1, });
$self->dbh( $dbh );
$self->init_schema();
}
}
sub connect {
my ($self,$dbpath) = @_;
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbpath","","");
$self->dbh( $dbh );
}
sub close {
my $self = shift;
$self->dbh->disconnect();
}
sub init_schema {
my ($self) = shift;
$self->dbh->do( qq|
create table po_string (
id INTEGER PRIMARY KEY AUTOINCREMENT,
lang TEXT,
msgid TEXT,
msgstr TEXT,
updated_on timestamp,
updated_by varchar(120));
|);
}
# by {id}
sub get_entry {
my ( $self, $id ) = @_;
my $sth = $self->dbh->prepare(qq{ select * from po_string where id = ? });
$sth->execute($id);
my $data = $sth->fetchrow_hashref();
$sth->finish;
return $data;
}
sub set_entry {
my ($self,$id,$msgstr) = @_;
die unless $id && $msgstr;
my $sth = $self->dbh->prepare(qq{ update po_string set msgstr = ? where id = ? });
my $ret = $sth->execute( $msgstr, $id );
$sth->finish;
return $ret;
}
sub last_id {
my $self = shift;
}
sub insert {
my ( $self , $lang , $msgid, $msgstr ) = @_;
$msgstr = decode_utf8( $msgstr );
my $sth = $self->dbh->prepare(
qq| INSERT INTO po_string ( lang , msgid , msgstr ) VALUES ( ? , ? , ? ); |);
( run in 1.050 second using v1.01-cache-2.11-cpan-5511b514fd6 )