| 1 | package Locale::Framework::SQL; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 1 | ▶ | use DBI; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 1 | ▶ | use DBI::Const::GetInfoType; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 1 | our $VERSION='0.07'; | |||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 1 | my %cache; | |||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | 0 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||
| 12 | 0 | my $class=shift; | |||||||||||||||||
| 13 | 0 | my $args={ | |||||||||||||||||
| 14 | DSN => undef, | ||||||||||||||||||
| 15 | DBUSER => undef, | ||||||||||||||||||
| 16 | DBPASS => "", | ||||||||||||||||||
| 17 | TABLE => "lang_translations", | ||||||||||||||||||
| 18 | @_ | ||||||||||||||||||
| 19 | }; | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | 0 | ▶ | my $dsn=$args->{"DSN"} or die "You need to specify a valid DSN for DBI (DSN => ...)"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 23 | 0 | ▶ | my $user=$args->{"DBUSER"} or die "You need to specify a database user (DBUSER => ...)"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 24 | 0 | my $pass=$args->{"DBPASS"}; | |||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 0 | my $self; | |||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | 0 | $self->{"dsn"}=$dsn; | |||||||||||||||||
| 29 | 0 | $self->{"dbh"}=DBI->connect($dsn,$user,$pass); | |||||||||||||||||
| 30 | 0 | $self->{"dbh"}->{"PrintError"}=0; | |||||||||||||||||
| 31 | 0 | ▶ | $self->{"table"}=$args->{"TABLE"} or die "You need to specify a valid table name (TABLE => ...)"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 32 | 0 | $self->{"status"}="none"; | |||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | 0 | my $table=$self->{"table"}; | |||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 0 | bless $self,$class; | |||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | { # Check existence | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 0 | my $sth=$self->{"dbh"}->prepare("SELECT COUNT(txt) FROM $table"); | |||||||||||||||||
| 41 | 0 | my $dbh=$self->{"dbh"}; | |||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 0 | ▶ | if (not $sth->execute()) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 44 | 0 | $sth->finish(); | |||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | 0 | my $driver=lc($dbh->{Driver}->{Name}); | |||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | 0 | ▶ | if ($driver eq "pg") { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 49 | 0 | $self->{"dbh"}->do("CREATE TABLE $table (txt varchar, lang varchar(32), translation varchar, translated numeric(1))"); | |||||||||||||||||
| 50 | 0 | $self->{"dbh"}->do("CREATE INDEX $table"."_idx ON $table(lang,txt)"); | |||||||||||||||||
| 51 | } | ||||||||||||||||||
| 52 | elsif ($driver eq "mysql") { | ||||||||||||||||||
| 53 | 0 | $self->{"dbh"}->do("CREATE TABLE $table (txt text, lang varchar(32), translation text, translated numeric(1))"); | |||||||||||||||||
| 54 | 0 | $self->{"dbh"}->do("CREATE INDEX $table"."_idx ON $table(lang,txt(200))"); | |||||||||||||||||
| 55 | } | ||||||||||||||||||
| 56 | elsif ($driver eq "sqlite") { | ||||||||||||||||||
| 57 | 0 | $self->{"dbh"}->do("CREATE TABLE $table (txt text, lang varchar(32), translation text, translated numeric(1))"); | |||||||||||||||||
| 58 | 0 | $self->{"dbh"}->do("CREATE INDEX $table"."_idx ON $table(lang,txt)"); | |||||||||||||||||
| 59 | } | ||||||||||||||||||
| 60 | else { | ||||||||||||||||||
| 61 | 0 | die "Cannot create table $table (txt varchar(BIG), lang varchar(32), translation varchar(big),translated numeric(1))\n". | |||||||||||||||||
| 62 | "I don't know this database system '$driver'"; | ||||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | } | ||||||||||||||||||
| 65 | else { | ||||||||||||||||||
| 66 | 0 | $sth->finish(); | |||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | } | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 0 | return $self; | |||||||||||||||||
| 71 | } | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | 0 | ▶ | sub DESTROY { | ||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 74 | 0 | my $self=shift; | |||||||||||||||||
| 75 | 0 | $self->{"dbh"}->disconnect(); | |||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 0 | ▶ | sub translate { | ||||||||||||||||
|
Subroutine
translate: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||
| 79 | 0 | my $self=shift; | |||||||||||||||||
| 80 | 0 | my $lang=shift; | |||||||||||||||||
| 81 | 0 | my $text=shift; | |||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | 0 | my $dbh=$self->{"dbh"}; | |||||||||||||||||
| 84 | 0 | my $table=$self->{"table"}; | |||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | 0 | ▶ | if ($lang eq "") { return $text; } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 87 | else { | ||||||||||||||||||
| 88 | 0 | ▶ | if (exists $cache{"$lang && $text"}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 89 | 0 | return $cache{"$lang && $text"}; | |||||||||||||||||
| 90 | } | ||||||||||||||||||
| 91 | else { | ||||||||||||||||||
| 92 | 0 | my $sth=$dbh->prepare("SELECT translation FROM $table WHERE txt='$text' AND lang='$lang'"); | |||||||||||||||||
| 93 | 0 | $sth->execute(); | |||||||||||||||||
| 94 | 0 | ▶ | if ($sth->rows() gt 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 95 | 0 | my @r=$sth->fetchrow_array(); | |||||||||||||||||
| 96 | 0 | $cache{"$lang && $text"}=shift @r; | |||||||||||||||||
| 97 | 0 | $sth->finish(); | |||||||||||||||||
| 98 | 0 | return $self->translate($lang,$text); | |||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | else { | ||||||||||||||||||
| 101 | 0 | $cache{"$lang && $text"}=$text; | |||||||||||||||||
| 102 | 0 | $sth->finish(); | |||||||||||||||||
| 103 | 0 | $dbh->do("INSERT INTO $table (translation, lang, txt, translated) VALUES ('$text','$lang','$text',0)"); | |||||||||||||||||
| 104 | 0 | return $self->translate($lang,$text); | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | } | ||||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | } | ||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | 0 | ▶ | sub clear_cache { | ||||||||||||||||
|
Subroutine
clear_cache: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 111 | 0 | %cache = (); | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 0 | ▶ | sub set_translation { | ||||||||||||||||
|
Subroutine
set_translation: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||
| 115 | 0 | my $self=shift; | |||||||||||||||||
| 116 | 0 | my $lang=shift; | |||||||||||||||||
| 117 | 0 | my $text=shift; | |||||||||||||||||
| 118 | 0 | my $translation=shift; | |||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 0 | ▶ | if ($lang eq "") { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 121 | 0 | die "Cannot set a translation for an empty language"; | |||||||||||||||||
| 122 | } | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | 0 | my $dbh=$self->{"dbh"}; | |||||||||||||||||
| 125 | 0 | my $table=$self->{"table"}; | |||||||||||||||||
| 126 | |||||||||||||||||||
| 127 | 0 | my $sth=$dbh->prepare("SELECT translation FROM $table WHERE txt='$text' AND lang='$lang'"); | |||||||||||||||||
| 128 | 0 | $sth->execute(); | |||||||||||||||||
| 129 | 0 | ▶ | if ($sth->rows() gt 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 130 | 0 | $sth->finish(); | |||||||||||||||||
| 131 | 0 | $dbh->do("UPDATE $table SET translation='$translation', translated=1 WHERE txt='$text' AND lang='$lang'"); | |||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | else { | ||||||||||||||||||
| 134 | 0 | $sth->finish(); | |||||||||||||||||
| 135 | 0 | $dbh->do("INSERT INTO $table (translation, lang, txt, translated) VALUES ('$text','$lang','$text',0)"); | |||||||||||||||||
| 136 | } | ||||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | 0 | $cache{"$lang && $text"}=$translation; | |||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | 0 | return 1; | |||||||||||||||||
| 141 | } | ||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | 1 | 1; | |||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||