line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Config::Backend::SQL; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
26616
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
5
|
1
|
|
|
1
|
|
2290
|
use DBI; |
|
1
|
|
|
|
|
16394
|
|
|
1
|
|
|
|
|
896
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION='0.10'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
0
|
|
|
0
|
1
|
|
my $class=shift; |
11
|
0
|
|
|
|
|
|
my $args={ |
12
|
|
|
|
|
|
|
DSN => undef, |
13
|
|
|
|
|
|
|
DBUSER => undef, |
14
|
|
|
|
|
|
|
DBPASS => undef, |
15
|
|
|
|
|
|
|
TABLE => "conf_table", |
16
|
|
|
|
|
|
|
@_ |
17
|
|
|
|
|
|
|
}; |
18
|
|
|
|
|
|
|
|
19
|
0
|
0
|
|
|
|
|
my $dsn=$args->{"DSN"} or die "You need to specify a valid DSN for Conf::SQL"; |
20
|
0
|
0
|
|
|
|
|
my $user=$args->{"DBUSER"} or die "You need to specify a valid DB User for Conf::SQL"; |
21
|
0
|
|
|
|
|
|
my $pass=$args->{"DBPASS"}; |
22
|
0
|
0
|
|
|
|
|
my $table=$args->{"TABLE"} or die "You need to specify a valid tablename for Conf::SQL"; |
23
|
0
|
|
|
|
|
|
my $self; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Read in config |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
$self->{"dbh"}=DBI->connect($dsn,$user,$pass); |
28
|
0
|
|
|
|
|
|
$self->{"TABLE"}=$table; |
29
|
0
|
|
|
|
|
|
$self->{"dbh"}->{"PrintError"}=0; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Table exists? |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
my $sth=$self->{"dbh"}->prepare("SELECT COUNT(var) FROM $table"); |
34
|
0
|
|
|
|
|
|
$sth->execute(); |
35
|
0
|
0
|
|
|
|
|
if (not $sth->execute()) { |
36
|
0
|
|
|
|
|
|
$sth->finish(); |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
my $dbh=$self->{"dbh"}; |
39
|
0
|
|
|
|
|
|
my $driver=lc($dbh->{Driver}->{Name}); |
40
|
|
|
|
|
|
|
|
41
|
0
|
0
|
|
|
|
|
if ($driver eq "pg") { # PostgreSQL |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
$dbh->do("CREATE TABLE $table(uid varchar,var varchar,value varchar)"); |
43
|
0
|
|
|
|
|
|
$dbh->do("CREATE INDEX $table"."_idx ON $table(uid, var)"); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
elsif ($driver eq "mysql") { # mysql |
46
|
0
|
|
|
|
|
|
$dbh->do("CREATE TABLE $table(uid varchar(250),var text,value mediumtext)"); |
47
|
0
|
|
|
|
|
|
$dbh->do("CREATE INDEX $table"."_idx ON $table(uid, var(200))"); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
elsif ($driver eq "sqlite") { # sqlite |
50
|
0
|
|
|
|
|
|
$dbh->do("CREATE TABLE $table(uid varchar(250),var varchar(1024),value text)"); |
51
|
0
|
|
|
|
|
|
$dbh->do("CREATE INDEX $table"."_idx ON $table(uid, var)"); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
0
|
|
|
|
|
|
die "Cannot create table CREATE TABLE $table(uid varchar(250),var varchar(1024),value text)\n". |
55
|
|
|
|
|
|
|
"and index CREATE INDEX $table"."_idx ON $table(uid, var)\n". |
56
|
|
|
|
|
|
|
"I don't know this database system '$driver'"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
0
|
|
|
|
|
|
$sth->finish(); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Get USER ID |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$self->{"user"}=getlogin() || getpwuid( $< ) || |
66
|
|
|
|
|
|
|
$ENV{ LOGNAME } || $ENV{ USER } || |
67
|
0
|
|
0
|
|
|
|
$ENV{ USERNAME } || 'unknown'; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# bless |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
bless $self,$class; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
return $self; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub DESTROY { |
77
|
0
|
|
|
0
|
|
|
my $self=shift; |
78
|
0
|
|
|
|
|
|
$self->{"dbh"}->disconnect(); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub set { |
82
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
83
|
0
|
|
|
|
|
|
my $var=shift; |
84
|
0
|
|
|
|
|
|
my $val=shift; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my $user=$self->{"user"}; |
87
|
0
|
|
|
|
|
|
my $dbh=$self->{"dbh"}; |
88
|
0
|
|
|
|
|
|
my $table=$self->{"TABLE"}; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Update or insert? |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my $sth=$dbh->prepare("SELECT COUNT(var) FROM $table WHERE uid='$user' AND var='$var'"); |
93
|
0
|
|
|
|
|
|
$sth->execute(); |
94
|
0
|
|
|
|
|
|
my ($count)=$sth->fetchrow_array(); |
95
|
0
|
|
|
|
|
|
$sth->finish(); |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
if ($count==0) { |
98
|
0
|
|
|
|
|
|
$dbh->do("INSERT INTO $table (var,uid,value) VALUES (".$dbh->quote($var).",".$dbh->quote($user).",".$dbh->quote($val).")"); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
else { |
101
|
0
|
|
|
|
|
|
$dbh->do("UPDATE $table SET value=".$dbh->quote($val)." WHERE uid=".$dbh->quote($user)." AND var=".$dbh->quote($var)); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub get { |
106
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
107
|
0
|
|
|
|
|
|
my $var=shift; |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
my $user=$self->{"user"}; |
110
|
0
|
|
|
|
|
|
my $dbh=$self->{"dbh"}; |
111
|
0
|
|
|
|
|
|
my $table=$self->{"TABLE"}; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# get |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
my $val=undef; |
116
|
0
|
|
|
|
|
|
my $sth=$dbh->prepare("SELECT value FROM $table WHERE uid=".$dbh->quote($user)." AND var=".$dbh->quote($var)); |
117
|
0
|
|
|
|
|
|
$sth->execute(); |
118
|
0
|
0
|
|
|
|
|
if ($sth->rows()!=0) { |
119
|
0
|
|
|
|
|
|
($val)=$sth->fetchrow_array(); |
120
|
|
|
|
|
|
|
} |
121
|
0
|
|
|
|
|
|
$sth->finish(); |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return $val; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub del { |
127
|
0
|
|
|
0
|
1
|
|
my ($self,$var)=@_; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
my $user=$self->{"user"}; |
130
|
0
|
|
|
|
|
|
my $dbh=$self->{"dbh"}; |
131
|
0
|
|
|
|
|
|
my $table=$self->{"TABLE"}; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$dbh->do("DELETE FROM $table WHERE uid=".$dbh->quote($user)." AND var=".$dbh->quote($var)); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub variables { |
137
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
my $user=$self->{"user"}; |
140
|
0
|
|
|
|
|
|
my $dbh=$self->{"dbh"}; |
141
|
0
|
|
|
|
|
|
my $table=$self->{"TABLE"}; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# get variables |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
my @vars; |
146
|
0
|
|
|
|
|
|
my $sth=$dbh->prepare("SELECT var FROM $table WHERE uid='$user'"); |
147
|
0
|
|
|
|
|
|
$sth->execute(); |
148
|
0
|
|
|
|
|
|
for (1..$sth->rows()) { |
149
|
0
|
|
|
|
|
|
my ($var)=$sth->fetchrow_array(); |
150
|
0
|
|
|
|
|
|
push @vars,$var; |
151
|
|
|
|
|
|
|
} |
152
|
0
|
|
|
|
|
|
$sth->finish(); |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
return @vars; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
1; |
158
|
|
|
|
|
|
|
__END__ |