line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Locale::Framework::SQL; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
32473
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
42
|
|
4
|
1
|
|
|
1
|
|
2306
|
use DBI; |
|
1
|
|
|
|
|
26308
|
|
|
1
|
|
|
|
|
109
|
|
5
|
1
|
|
|
1
|
|
3499
|
use DBI::Const::GetInfoType; |
|
1
|
|
|
|
|
12502
|
|
|
1
|
|
|
|
|
1387
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION='0.07'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my %cache; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
0
|
|
|
0
|
1
|
|
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
|
0
|
|
|
|
|
my $dsn=$args->{"DSN"} or die "You need to specify a valid DSN for DBI (DSN => ...)"; |
23
|
0
|
0
|
|
|
|
|
my $user=$args->{"DBUSER"} or die "You need to specify a database user (DBUSER => ...)"; |
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
|
0
|
|
|
|
|
$self->{"table"}=$args->{"TABLE"} or die "You need to specify a valid table name (TABLE => ...)"; |
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"); |
|
0
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my $dbh=$self->{"dbh"}; |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
if (not $sth->execute()) { |
44
|
0
|
|
|
|
|
|
$sth->finish(); |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my $driver=lc($dbh->{Driver}->{Name}); |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
if ($driver eq "pg") { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub DESTROY { |
74
|
0
|
|
|
0
|
|
|
my $self=shift; |
75
|
0
|
|
|
|
|
|
$self->{"dbh"}->disconnect(); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub translate { |
79
|
0
|
|
|
0
|
1
|
|
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
|
0
|
|
|
|
|
if ($lang eq "") { return $text; } |
|
0
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
else { |
88
|
0
|
0
|
|
|
|
|
if (exists $cache{"$lang && $text"}) { |
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
|
0
|
|
|
|
|
if ($sth->rows() gt 0) { |
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
|
|
|
|
|
|
|
sub clear_cache { |
111
|
0
|
|
|
0
|
1
|
|
%cache = (); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub set_translation { |
115
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
116
|
0
|
|
|
|
|
|
my $lang=shift; |
117
|
0
|
|
|
|
|
|
my $text=shift; |
118
|
0
|
|
|
|
|
|
my $translation=shift; |
119
|
|
|
|
|
|
|
|
120
|
0
|
0
|
|
|
|
|
if ($lang eq "") { |
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
|
0
|
|
|
|
|
if ($sth->rows() gt 0) { |
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; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
__END__ |