Last change
on this file since 176 was
160,
checked in by nanardon, 15 years ago
|
- use an OO interface for each type of object
|
-
Property svn:keywords set to
Id Rev
|
File size:
1.6 KB
|
Line | |
---|
1 | package Vote::DB::common; |
---|
2 | |
---|
3 | # $Id$ |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use Vote; |
---|
8 | use DBI; |
---|
9 | use vars qw(@EXPORT); |
---|
10 | use base 'Exporter'; |
---|
11 | @EXPORT = qw(gen_uid); |
---|
12 | |
---|
13 | =head1 NAME |
---|
14 | |
---|
15 | Vote::Model::Vote - Catalyst Model |
---|
16 | |
---|
17 | =head1 DESCRIPTION |
---|
18 | |
---|
19 | Catalyst Model. |
---|
20 | |
---|
21 | =cut |
---|
22 | |
---|
23 | sub _newdb { |
---|
24 | my ($dbstring) = @_; |
---|
25 | my $db = DBI->connect_cached( |
---|
26 | 'dbi:Pg:' . $dbstring, |
---|
27 | undef, undef, |
---|
28 | { |
---|
29 | RaiseError => 0, |
---|
30 | AutoCommit => 0, |
---|
31 | PrintWarn => 0, |
---|
32 | PrintError => 1, |
---|
33 | } |
---|
34 | ) or return; |
---|
35 | $db->do(q{set DATESTYLE to 'DMY'}); |
---|
36 | return $db; |
---|
37 | } |
---|
38 | |
---|
39 | sub db { |
---|
40 | my ($self) = @_; |
---|
41 | return $_[0]->{db} && $_[0]->{db}->ping |
---|
42 | ? $_[0]->{db} |
---|
43 | : $_[0]->_newdb($self->{dbstring}); |
---|
44 | } |
---|
45 | |
---|
46 | sub mail_header { |
---|
47 | return( |
---|
48 | 'Content-Type' => 'text/plain; charset=UTF-8; format=flowed', |
---|
49 | 'Content-Transfer-Encoding' => '8bit', |
---|
50 | 'X-Epoll-version' => $Vote::VERSION, |
---|
51 | ); |
---|
52 | } |
---|
53 | |
---|
54 | sub random_string { |
---|
55 | my $lenght = $_[-1] || 8; |
---|
56 | |
---|
57 | return join('', map { ('a'..'z', 'A'..'Z', 0..9)[rand 62] } (1..$lenght)); |
---|
58 | } |
---|
59 | |
---|
60 | sub gen_enc_passwd { |
---|
61 | my ($self, $passwd) = @_; |
---|
62 | |
---|
63 | $passwd ||= random_string(8); |
---|
64 | return(crypt($passwd, '$1$' . random_string(8) . '$')); |
---|
65 | } |
---|
66 | |
---|
67 | sub dbtime { |
---|
68 | my ($self) = @_; |
---|
69 | my $sth = $self->db->prepare( |
---|
70 | q{select to_char(now(), 'DD/MM/YYYY HH24:MI:SS') as d} |
---|
71 | ); |
---|
72 | |
---|
73 | $sth->execute(); |
---|
74 | my $res = $sth->fetchrow_hashref; |
---|
75 | $sth->finish; |
---|
76 | $res->{d}; |
---|
77 | } |
---|
78 | |
---|
79 | sub gen_uid { |
---|
80 | unpack("H*", join("", map { chr(rand(256)) } (0..15))) |
---|
81 | } |
---|
82 | |
---|
83 | =head1 AUTHOR |
---|
84 | |
---|
85 | Thauvin Olivier |
---|
86 | |
---|
87 | =head1 LICENSE |
---|
88 | |
---|
89 | This library is free software, you can redistribute it and/or modify |
---|
90 | it under the same terms as Perl itself or CeCILL. |
---|
91 | |
---|
92 | =cut |
---|
93 | |
---|
94 | 1; |
---|
Note: See
TracBrowser
for help on using the repository browser.