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 ( ? , ? , ? ); |);
    $sth->execute( $lang, $msgid, $msgstr );
}

sub find {
    my ( $self, $lang , $msgid ) = @_;
    my $sth = $self->dbh->prepare(qq| SELECT * FROM po_string WHERE lang = ? AND msgid = ? LIMIT 1;|);
    $sth->execute( $lang, $msgid );
    my $data = $sth->fetchrow_hashref();
    $sth->finish;


    return $data;
}

sub get_unset_entry_list {
    my ($self, $lang ) = @_;
    my $sth;
    if( $lang ) {
        $sth = $self->dbh->prepare(qq| SELECT * FROM po_string where lang = ? and msgstr = '' or msgstr is null; |);
        $sth->execute( $lang );
    }
    else {
        $sth = $self->dbh->prepare(qq| SELECT * FROM po_string where msgstr = '' or msgstr is null; | );
        $sth->execute();
    }
    return $self->_entry_sth_to_list( $sth );
}



( run in 2.414 seconds using v1.01-cache-2.11-cpan-0d23b851a93 )