| 1 | package User::Config::DB::DBIC; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | with 'User::Config::DB'; | ||
| 8 | use DBI; | ||
| 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::DBIC - Use an DBIx::Class schema to store the user-configuration | ||
| 18 | data. | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use User::Config; | ||
| 23 | |||
| 24 | my $uc = User::Config->instance; | ||
| 25 | $uc->db("DBIC", { | ||
| 26 | schema => "My::DBIC::Schema", | ||
| 27 | db => "dbi:SQLite:user.sqlite", | ||
| 28 | resultset => "Configuration", | ||
| 29 | }); | ||
| 30 | |||
| 31 | This assumes you want the resultset | ||
| 32 | C<My::DBIX::Schema::ResultSet::Confiuration> to store your options. | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | This is a database-backend for L<User::Config>. How the options get stored in which table | ||
| 37 | will be defined using a L<DBIx::Class> schema and some optional helper-routines. | ||
| 38 | |||
| 39 | =head2 ATTRIBUTES | ||
| 40 | |||
| 41 | All attributes are read-only and should be given at the time of initialization. | ||
| 42 | |||
| 43 | =head3 db | ||
| 44 | |||
| 45 | This attribute contains a L<DBI>-string to connect to the database. | ||
| 46 | Its presence is mandatory. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | has db => ( | ||
| 51 | is => "ro", | ||
| 52 | isa => "Str", | ||
| 53 | required => 1, | ||
| 54 | ); | ||
| 55 | |||
| 56 | =head3 schema | ||
| 57 | |||
| 58 | This contains the name of the schema used to interact with the database. | ||
| 59 | Mandatory. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | has schema => ( | ||
| 64 | is => "ro", | ||
| 65 | required => 1, | ||
| 66 | ); | ||
| 67 | |||
| 68 | =head3 resultset | ||
| 69 | |||
| 70 | The name of the resultset within the schema to use. | ||
| 71 | Mandatory. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | has resultset => ( | ||
| 76 | is => "ro", | ||
| 77 | isa => "Str", | ||
| 78 | required => 1, | ||
| 79 | ); | ||
| 80 | |||
| 81 | =head3 usercol | ||
| 82 | |||
| 83 | The name of the primary key to use for filtering for a certain user. | ||
| 84 | Optional. Default: "uid" | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | has usercol => ( | ||
| 89 | is => "ro", | ||
| 90 | isa => "Str", | ||
| 91 | default => "uid", | ||
| 92 | ); | ||
| 93 | |||
| 94 | =head3 db_user and db_pwd | ||
| 95 | |||
| 96 | These optional attributes will contain the username and password needed to | ||
| 97 | connect to the database. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | has [ qw/db_user db_pwd/ ] => is => "ro", default => undef; | ||
| 102 | |||
| 103 | =head2 METHODS | ||
| 104 | |||
| 105 | =for comment | ||
| 106 | BUILD will connect the schema and prepare the handle for further interaction | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub BUILD { | ||
| 111 | my ($self) = @_; | ||
| 112 | |||
| 113 | my $cls = $self->schema; | ||
| 114 | my $schema = eval "require $cls; $cls"."->connect(". | ||
| 115 | "'".$self->db."', ". | ||
| 116 | (defined($self->db_user)?"'".$self->db_user."',":"undef,"). | ||
| 117 | (defined($self->db_pwd)?"'".$self->db_pwd."',":"undef,"). | ||
| 118 | "{ AutoCommit => 1 })"; | ||
| 119 | die $@ if $@; | ||
| 120 | $self->{schema} = $schema->resultset($self->resultset); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub _get_rs { | ||
| 124 | my ($self, $namespace, $user, $name) = @_; | ||
| 125 | my $col = $namespace."::".$name; | ||
| 126 | $col =~ s/::/_/g; | ||
| 127 | my $rs = $self->{schema}->search( | ||
| 128 | { $self->usercol => $user }, | ||
| 129 | { columns => [ $col ] } | ||
| 130 | ); | ||
| 131 | return ($rs, $col); | ||
| 132 | } | ||
| 133 | |||
| 134 | =head3 C<<$db->set($package, $user, $option_name, $context, $value)>> | ||
| 135 | |||
| 136 | assigns the value for the given user to the option within a package. | ||
| 137 | See L<User::Config::DB> | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub set { | ||
| 142 | my ($self, $namespace, $user, $name, $ctx, $value) = @_; | ||
| 143 | my ($rs, $col) = $self->_get_rs($namespace, $user, $name); | ||
| 144 | if($rs == 0) { | ||
| 145 | return $rs->create({$col => $value}); | ||
| 146 | } else { | ||
| 147 | return $rs->update({$col => $value}); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | =head3 C<<$db->get($package, $user, $option_name, $context)>> | ||
| 152 | |||
| 153 | returns the set value. | ||
| 154 | See L<User::Config::DB> | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub get { | ||
| 159 | my $self = shift; | ||
| 160 | my ( $rs, $col ) = $self->_get_rs(@_); | ||
| 161 | return $rs->get_column($col)->first(); | ||
| 162 | } | ||
| 163 | |||
| 164 | =head1 SEE ALSO | ||
| 165 | |||
| 166 | L<User::Config> | ||
| 167 | L<DBIx::Class> | ||
| 168 | L<User::Config::DB> | ||
| 169 | |||
| 170 | =head1 AUTHOR | ||
| 171 | |||
| 172 | Benjamin Tietz E<lt>benjamin@micronet24.deE<gt> | ||
| 173 | |||
| 174 | =head1 COPYRIGHT AND LICENSE | ||
| 175 | |||
| 176 | Copyright (C) 2011 by Benjamin Tietz | ||
| 177 | |||
| 178 | This library is free software; you can redistribute it and/or modify | ||
| 179 | it under the same terms as Perl itself, either Perl version 5.10.1 or, | ||
| 180 | at your option, any later version of Perl 5 you may have available. | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | no Moose; | ||
| 185 | __PACKAGE__->meta->make_immutable; | ||
| 186 | 1; | ||
| 187 |