blib/lib/User/Config/DB/Keyed.pm

filter: Statementstmt 38.4% 15 / 39 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 55.5% 5 / 9 Podpod n/a totaltotal 38.4% 20 / 52 CC 3 SCAR 11.0
CC
3
Cov
35%
BUILD
6.0
set
6.0
isset
2.0
CRAP 3.0
↑ Summary
1 package User::Config::DB::Keyed;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 with 'User::Config::DB';
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 our $VERSION = '0.01_01';
11 $VERSION = eval $VERSION; # see L<perlmodstyle>
12  
13 =pod
14  
15 =head1 NAME
16  
17 User::Config::DB::Keyed - Store User-Configuration in a large Key-Value-Table.
18  
19 =head1 SYNOPSIS
20  
21 use User::Config;
22  
23 my $uc = User::Config->instance;
24 $uc->db("Keyed", { table => "user", db => "dbi:SQLite:user.sqlite" });
25  
26 =head1 DESCRIPTION
27  
28 This is a database-backend for L<User::Config>. The options will be stored
29 within a table consisting of - at least - three columns:
30  
31 =over 4
32  
33 =item user - will save the current users name (default: "uid")
34  
35 =item item - will save the namespace and the name of the option
36 (default: "item")
37  
38 =item value - will store the value (default: "value")
39  
40 =back
41  
42 The names of these columns as well of the tables are configurable. Further columns
43 wont be touched.
44  
45 =head2 ATTRIBUTES
46  
47 All attributes are read-only and should be given at the time of initialization.
48  
49 =head3 table
50  
51 This attribute must be given and contain the name of the table within the
52 database to use.
53  
54 =cut
55  
56 has table => (
57 is => "ro",
58 required => 1,
59 isa => "Str",
60 );
61  
62 =head3 db
63  
64 This attribute must be given, too. It consits of a L<DBI>-string to connect to the
65 database.
66  
67 =cut
68  
69 has db => (
70 is => "ro",
71 required => 1,
72 isa => "Str",
73 );
74  
75 =head3 db_user and db_pwd
76  
77 These attributes contain optional username and password for the database-connection
78  
79 =cut
80  
81 has [ qw/db_user db_pwd/ ] => (
82 is => "ro",
83 isa => "Str",
84 );
85  
86 =head3 user_column, item_column and value_column
87  
88 this will contain the names of the columns used to store the user, item
89 and value. The default values are shown above.
90  
91 =cut
92  
93 has value_column => (
94 is => "ro",
95 default => "value",
96 isa => "Str",
97 );
98 has user_column => (
99 is => "ro",
100 default => "uid",
101 isa => "Str",
102 );
103 has item_column => (
104 is => "ro",
105 default => "item",
106 isa => "Str",
107 );
108  
109 =head2 METHODS
110  
111 =for comment
112 C<BUILD> will prepare the statemennts used too communicate with the database
113  
114 =cut
115  
116 0 ▶sub BUILD {
Subroutine BUILD: not called CC=2 SCAR=17.9
117 0 my ($self) = @_;
118  
119 0 my $valcol = $self->value_column;
120 0 my $usercol = $self->user_column;
121 0 my $itemcol = $self->item_column;
122 0 my $table = $self->table;
123 0 my $gstmt = "SELECT $valcol FROM $table ".
124 "WHERE $usercol = ? AND $itemcol = ?";
125 0 my $pstmt = "SELECT COUNT(*) FROM $table ".
126 "WHERE $usercol = ? AND $itemcol = ?";
127 0 my $istmt = "INSERT INTO $table ( $valcol, $usercol, $itemcol ) ".
128 "VALUES ( ?, ?, ? )";
129 0 my $ustmt = "UPDATE $table SET $valcol = ? ".
130 "WHERE $usercol = ? AND $itemcol = ?";
131  
132 0 my $dbh = DBI->connect( $self->db, $self->db_user, $self->db_pwd,
133 {AutoCommit => 1} );
134 0 ▶ croak $dbh->errstr if $dbh->err;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->err 0 0 0
135 $self->{get} = $dbh->prepare($gstmt),
136 $self->{insert} = $dbh->prepare($istmt),
137 $self->{update} = $dbh->prepare($ustmt),
138 0 $self->{isset} = $dbh->prepare($pstmt),
139 }
140  
141 =head3 C<<$db->set($package, $user, $option_name, $context, $value)>>
142  
143 assigns the value for the given user to the option within a package.
144 See L<User::Config::DB>
145  
146 =cut
147  
148 0 ▶sub set {
Subroutine set: not called CC=2 SCAR=17.9
149 0 my ($self, $namespace, $user, $name, $ctx, $value) = @_;
150 0 my $item = $namespace."::".$name;
151 my $stmt = $self->isset($namespace, $user, $name, $ctx)
152 ? $self->{update}
153 0 ▶ : $self->{insert};
Branch: 1 branch
BranchTrueFalseTotal
$self->isset($namespace, $user, $name, $ctx) ? : 0 0 0
154 0 return $stmt->execute($value, $user, $item);
155 }
156  
157 =head3 C<<$db->isset($package, $user, $option_name, $context)>>
158  
159 Checks wether the option was set.
160 See L<User::Config::DB>
161  
162 =cut
163  
164 0 ▶sub isset {
Subroutine isset: not called CC=1 SCAR=6.9
165 0 my ($self, $namespace, $user, $name, $ctx) = @_;
166 0 $self->{isset}->execute($user, $namespace."::".$name);
167 0 return ${$self->{isset}->fetchrow_arrayref()}[0];
168 }
169  
170 =head3 C<<$db->get($package, $user, $option_name, $context)>>
171  
172 retrieves the currently set value.
173 See L<User::Config::DB>
174  
175 =cut
176  
177 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
178 0 my ($self, $namespace, $user, $name) = @_;
179 0 $self->{get}->execute($user, $namespace."::".$name);
180 0 return ${$self->{get}->fetchrow_arrayref()}[0];
181 }
182  
183 =head1 SEE ALSO
184  
185 L<User::Config>
186 L<User::Config::DB>
187 L<DBI>
188  
189 =head1 AUTHOR
190  
191 Benjamin Tietz E<lt>benjamin@micronet24.deE<gt>
192  
193 =head1 COPYRIGHT AND LICENSE
194  
195 Copyright (C) 2011 by Benjamin Tietz
196  
197 This library is free software; you can redistribute it and/or modify
198 it under the same terms as Perl itself, either Perl version 5.10.1 or,
199 at your option, any later version of Perl 5 you may have available.
200  
201 =cut
202  
203 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
204 __PACKAGE__->meta->make_immutable;
205 1;
206  
↑ Summary