source: trunk/lib/Vote/DB/common.pm @ 199

Last change on this file since 199 was 199, checked in by nanardon, 15 years ago
  • add an uid to the vote, currently poll are identified by an id generated by the database (sequence), in the case poll is dump/restore, this id may change, we hope uuid will never
  • Property svn:keywords set to Id Rev
File size: 3.0 KB
Line 
1package Vote::DB::common;
2
3# $Id$
4
5use strict;
6use warnings;
7use DBI;
8use vars qw(@EXPORT);
9use base 'Exporter';
10@EXPORT = qw(gen_uid);
11
12=head1 NAME
13
14Vote::Model::Vote - Catalyst Model
15
16=head1 DESCRIPTION
17
18Catalyst Model.
19
20=cut
21
22sub _newdb {
23    my ($dbstring) = @_;
24    my $db = DBI->connect_cached(
25        'dbi:Pg:' . $dbstring,
26        undef, undef,
27        {
28            RaiseError => 0,
29            AutoCommit => 0,
30            PrintWarn => 0,
31            PrintError => 1,
32        }
33    ) or return;
34    $db->do(q{set DATESTYLE to 'DMY'});
35    return $db;
36}
37
38sub db {
39    my ($self) = @_;
40    return $_[0]->{db} && $_[0]->{db}->ping
41        ? $_[0]->{db}
42        : ($self->{db} = $_[0]->_newdb($self->{dbstring}));
43}
44
45sub commit {
46    my ($self) = @_;
47    $self->{db} or return;
48    # If EPOLL_NO_COMMIT is true, we never commit, use for test
49    if ($ENV{EPOLL_NO_COMMIT}) { return 1 }
50    $self->{db}->commit;
51}
52
53sub rollback {
54    my ($self) = @_;
55    $self->{db} or return;
56    $self->{db}->rollback;
57}
58
59sub mail_header {
60    return(
61        'Content-Type' => 'text/plain; charset=UTF-8; format=flowed',
62        'Content-Transfer-Encoding' => '8bit',
63        'X-Epoll-version' => $Vote::VERSION,
64    );
65}
66
67sub random_string {
68    my $lenght = $_[-1] || 8;
69
70    return join('', map { ('a'..'z', 'A'..'Z', 0..9)[rand 62] } (1..$lenght));
71}
72
73sub gen_enc_passwd {
74    my ($self, $passwd) = @_;
75
76    $passwd ||= random_string(8);
77    return(crypt($passwd, '$1$' . random_string(8) . '$'));
78}
79
80sub dbtime {
81    my ($self) = @_;
82    my $sth = $self->db->prepare(
83        q{select to_char(now(), 'DD/MM/YYYY HH24:MI:SS') as d}
84    );
85
86    $sth->execute();
87    my $res = $sth->fetchrow_hashref;
88    $sth->finish;
89    $res->{d};
90}
91
92sub valid_date {
93    my ($self, $date) = @_;
94    my $res = $self->db->do(
95        sprintf(
96            q{ select %s::timestamp },
97            $self->db->quote($date),
98        )
99    );
100    $res or $self->rollback;
101}
102
103sub check_date_max {
104    my ($self, $maxdate, $mindate) = @_;
105    my $sth = $self->db->prepare(
106        sprintf(
107            q{ select %s::timestamp > %s::timestamp as res },
108            $self->db->quote($maxdate),
109            $mindate ? $self->db->quote($mindate) : 'now()',
110        )
111    );
112    $sth->execute or do {
113        $self->rollback;
114        return;
115    };
116    my $res = $sth->fetchrow_hashref;
117    $sth->finish;
118    $res->{res}
119}
120
121sub poll_id_from_uid {
122    my ($self, $uid) = @_;
123    if (length($uid) == 32) {
124        my $sth = $self->db->prepare_cached(
125            q{ select poll from settings where var = 'uid' and val = ?
126                order by poll }
127        );
128        $sth->execute($uid);
129        my $res = $sth->fetchrow_hasref;
130        $sth->finish;
131        return $res->{poll} || $uid;
132    }
133    $uid
134}
135
136sub gen_uid {
137    unpack("H*", join("", map { chr(rand(256)) } (0..15)))
138}
139
140=head1 AUTHOR
141
142Thauvin Olivier
143
144=head1 LICENSE
145
146This library is free software, you can redistribute it and/or modify
147it under the same terms as Perl itself or CeCILL.
148
149=cut
150
1511;
Note: See TracBrowser for help on using the repository browser.