File Coverage

blib/lib/Register/Generic.pm
Criterion Covered Total %
statement 12 72 16.6
branch 0 10 0.0
condition n/a
subroutine 2 9 22.2
pod 0 8 0.0
total 14 99 14.1


line stmt bran cond sub pod time code
1             package Register::Generic;
2 1     1   6 use DBI;
  1         2  
  1         1100  
3             require Exporter;
4              
5             sub new {
6 1     1 0 44 my $type=shift;
7 1         7 my %params=@_;
8 1         4 my $self={};
9 1         4 my $name="${type}::new";
10              
11 1         3 $self->{'Type'}=$type;
12 1         7 my $regpath = Register::checkReq ($name, "regpath", $params{'regpath'});
13 1         5 my $regname = Register::checkReq ($name, "regname", $params{'regname'});
14 1         5 ($self->{'REGISTER_NAME'}=$regname)=~s/(\W)//g;
15 1         13 $self->{'DBH'}=DBI->connect("DBI:CSV:f_dir=".$regpath,"","");
16 0 0         if (!(-d $regpath)) {
17 0           printf "ERROR:\n";
18 0           printf "LOCATION: \<".$self->{'Type'}."\>\n";
19 0           printf "CAUSE: directory \<".$regpath."\> not found !!!\n";
20 0           exit(1);
21             };
22 0 0         if (!(-f $regpath."/".$self->{'REGISTER_NAME'})) {
23 0           my($sql)=qq {
24             CREATE TABLE
25             $self->{'REGISTER_NAME'}
26             (
27             R_SECTION CHAR(255),
28             R_KEY CHAR(255),
29             R_VALUE CHAR(255)
30             )
31             };
32 0           $self->{DBH}->do($sql);
33             };
34              
35 0           bless $self;
36             }
37              
38             sub getsettings {
39 0     0 0   my $self=shift;
40 0           my ($SEC,$KEY)=@_;
41 0           my ($row)={};
42              
43 0           $row->{'R_VALUE'}="";
44 0           my ($sql) = qq {
45             SELECT
46             *
47             FROM
48             $self->{'REGISTER_NAME'}
49             WHERE
50             R_SECTION=?
51             AND
52             R_KEY=?
53             };
54 0           my($sth)=$self->{DBH}->prepare($sql);
55 0           $sth->execute($SEC,$KEY);
56 0           $row=$sth->fetchrow_hashref;
57 0 0         if ($row->{'R_VALUE'} ne "") {
58 0           return $row->{'R_VALUE'};
59             } else {
60 0           return "";
61             };
62             }
63              
64             sub getsections {
65 0     0 0   my $self=shift;
66 0           my ($oldsec)="";
67 0           my (@retval)=();
68 0           my ($row)={};
69              
70 0           my ($sql) = qq {
71             SELECT
72             R_SECTION
73             FROM
74             $self->{'REGISTER_NAME'}
75             ORDER BY
76             R_SECTION
77             };
78 0           my($sth)=$self->{DBH}->prepare($sql);
79 0           $sth->execute();
80 0           while ($row=$sth->fetchrow_hashref) {
81 0 0         if ($oldsec ne $row->{R_SECTION}) {
82 0           push @retval,$row->{R_SECTION};
83 0           $oldsec=$row->{R_SECTION};
84             };
85             };
86 0           $sth->finish;
87 0           return @retval;
88             };
89              
90             sub savesettings {
91 0     0 0   my $self=shift;
92 0           my ($SEC,$KEY,$VAL)=@_;
93 0           my ($row)={};
94            
95 0           my($sql) = qq {
96             SELECT
97             *
98             FROM
99             $self->{'REGISTER_NAME'}
100             WHERE
101             R_SECTION=?
102             AND
103             R_KEY=?
104             };
105 0           my($sth)=$self->{DBH}->prepare($sql);
106 0           $sth->execute($SEC,$KEY);
107 0           $row=$sth->fetchrow_hashref;
108 0 0         if ($row->{'R_SECTION'} ne "") {
109 0           $self->updatekey($SEC,$KEY,$VAL);
110             } else {
111 0           $self->addkey($SEC,$KEY,$VAL);
112             };
113 0           $sth->finish();
114            
115             }
116              
117             sub deletesection {
118 0     0 0   my $self=shift;
119 0           my($SEC)=@_;
120              
121 0           $sql=qq {
122             DELETE FROM
123             $self->{'REGISTER_NAME'}
124             WHERE
125             R_SECTION=?
126             };
127 0           $self->{DBH}->do($sql,undef,$SEC);
128             }
129              
130             sub deletesettings {
131 0     0 0   my $self=shift;
132 0           my($SEC,$KEY)=@_;
133              
134 0           my($sql)=qq {
135             DELETE FROM
136             $self->{'REGISTER_NAME'}
137             WHERE
138             R_SECTION=?
139             AND
140             R_KEY=?
141             };
142 0           $self->{DBH}->do($sql,undef,$SEC,$KEY);
143             }
144              
145             sub updatekey {
146 0     0 0   my $self=shift;
147 0           my ($SEC,$KEY,$VAL)=@_;
148              
149 0           my($sql)=qq {
150             UPDATE
151             $self->{'REGISTER_NAME'}
152             SET
153             R_VALUE=?
154             WHERE
155             R_SECTION=?
156             AND
157             R_KEY=?
158             };
159 0           $self->{DBH}->do($sql,undef,$VAL,$SEC,$KEY);
160             }
161              
162             sub addkey {
163 0     0 0   my $self=shift;
164 0           my ($SEC,$KEY,$VAL)=@_;
165              
166 0           my($sql)=qq {
167             INSERT INTO
168             $self->{'REGISTER_NAME'}
169             (
170             R_SECTION,
171             R_KEY,
172             R_VALUE
173             )
174             VALUES ( ?,?,? )
175             };
176              
177 0           $self->{DBH}->do($sql,undef,$SEC,$KEY,$VAL);
178             }
179              
180             1;
181              
182             =head1 NAME
183              
184             Register::Generic - Implementation of the windows ini like structure
185              
186             =head1 SYNOPSIS
187              
188             use Register;
189            
190             $genreg=new Register::Generic (
191             'regpath' => "/home/myprog",
192             'regname' => "INITFILE"
193             );
194              
195             $genreg->savesettings("SECTION","KEY","VALUE");
196             $value=$genreg->getsettings("SECTION","KEY");
197             $sections=$genreg->getsections;
198             $genreg->deletesettings(,"SECTION","KEY");
199             $genreg->deletesection("SECTION");
200              
201             =head1 DESCRIPTION
202              
203             The Register::Generic module permit to create an ini file like
204             Windows for save generic information about your program.
205             With the use of CSV dbd , the file created is readable by DBI without
206             problem.
207             Here CSV table specifics:
208              
209             FIELD_NAME FIELD_TYPE
210             ----------------------------------
211             R_SECTION CHAR
212             R_KEY CHAR
213             R_VALUE CHAR
214              
215             =head1 FUNCTIONS
216              
217              
218              
219             =head2 Function
220              
221             The statament create the Register::Generic object and return the
222             reference to him.
223              
224             $genreg=new Register::System (
225             'regpath' => "/home/myprog",
226             'regname' => "INIFILE"
227             );
228              
229             Parameter :
230              
231             regpath specify the path where new statament search for
232             file.
233              
234             regname specify the name of the file to use.
235              
236             Finaly if regpath don't exist the program return an error message at compile
237             time, if the register don't exist it is maked.
238              
239             =head2 Function
240              
241             The savesettings function , save the value argument in the key of the
242             section of the program.
243              
244             $genreg->savesettings("SECTION","KEY","VALUE");
245              
246             If the key don't exist it make (it make also section without specify befor), else if
247             key already exist and value is different from previous it update value.
248              
249             =head2 Function
250              
251             The getsettings function retrieve the value of the specified key.
252              
253             $value=$genreg->getsettings("SECTION","KEY");
254              
255             =head2 Function
256              
257             The getsections function retrieve the section in the ini file and
258             return an array.
259              
260             @sections=$genreg->getsections;
261              
262             =head2 Function
263              
264             The deletesettings function delete the entry key specified.
265              
266             $genreg->deletesettings("SECTION","KEY");
267              
268             =head2 Function
269              
270             The deletesection function delete the entry section specified.
271              
272             $genreg->deletesection("SECTION");
273              
274             =head1 AUTHOR
275              
276             Vecchio Fabrizio
277              
278             =head1 SEE ALSO
279              
280             L,L,L
281              
282             =cut