GeniusTrader/DB/pg.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 108 CC 6 SCAR 23.7
CC
6
Cov
0%
get_last_prices
30.0
new
6.0
disconnect
2.0
CRAP 10.7
1  
2 package Finance::GeniusTrader::DB::pg;
3  
4 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
5 # This file is distributed under the terms of the General Public License
6 # version 2 or (at your option) any later version.
7  
8 use strict;
9 use vars qw(@ISA);
10  
11 use Finance::GeniusTrader::DB;
12 use Finance::GeniusTrader::Prices;
13 use Finance::GeniusTrader::Conf;
14 use Finance::GeniusTrader::DateTime;
15  
16 use DBD::Pg;
17  
18 @ISA = qw(Finance::GeniusTrader::DB);
19  
20 =head1 NAME
21  
22 Finance::GeniusTrader::DB::pg - Access to PostgreSQL database of quotes
23  
24 =head1 DESCRIPTION
25  
26 This module is used to retry quotes from a Postgresql database.
27 By default, the database is supposed to be running on localhost and
28 the only authentication done is the standard Unix one.
29  
30 =head2 Configuration
31  
32 You can put some configuration items in ~/.gt/options to indicate where
33 the database is.
34  
35 =over
36  
37 =item DB::pg::dbname : the name of the database ("cours" by default)
38  
39 =item DB::pg::dbhost : the host of the database ("" = localhost by default)
40  
41 =item DB::pg::dbuser : the user account on the database
42  
43 =item DB::pg::dbpasswd : the password of the user account
44  
45 =back
46  
47 =head2 Functions
48  
49 =over
50  
51 =item C<< Finance::GeniusTrader::DB::pg->new() >>
52  
53 =cut
54 sub new {
55 my $type = shift;
56 my $class = ref($type) || $type;
57  
58 Finance::GeniusTrader::Conf::default("DB::pg::dbname", "cours");
59 Finance::GeniusTrader::Conf::default("DB::pg::dbhost", ""); #aka localhost
60 Finance::GeniusTrader::Conf::default("DB::pg::dbuser", ""); #aka current user
61 Finance::GeniusTrader::Conf::default("DB::pg::dbpasswd", ""); #aka user is already identified
62  
63 my $self = { 'dbname' => Finance::GeniusTrader::Conf::get("DB::pg::dbname"),
64 'dbhost' => Finance::GeniusTrader::Conf::get("DB::pg::dbhost"),
65 'dbuser' => Finance::GeniusTrader::Conf::get("DB::pg::dbuser"),
66 'dbpasswd' => Finance::GeniusTrader::Conf::get("DB::pg::dbpasswd"),
67 @_
68 };
69
70 my $connect_string = 'dbi:Pg:dbname=' . $self->{'dbname'};
71 if ($self->{'dbhost'}) {
72 $connect_string .= ";host=" . $self->{'dbhost'};
73 }
74 $self->{'_dbh'} = DBI->connect($connect_string, $self->{'dbuser'},
75 $self->{'dbpasswd'}) || die "Couldn't connect to database !\n";
76  
77 return bless $self, $class;
78 }
79  
80 =item C<< $db->disconnect >>
81  
82 Disconnects from the database.
83  
84 =cut
85 sub disconnect {
86 my $self = shift;
87 $self->{'_dbh'}->disconnect;
88 delete $self->{'prices'};
89 delete $self->{'dates'};
90 }
91  
92 =item C<< $db->get_prices($code, $timeframe) >>
93  
94 Returns a Finance::GeniusTrader::Prices object containing all known prices for the symbol $code.
95  
96 =cut
97 sub get_prices {
98 my($self, $code, $timeframe) = @_;
99 return get_last_prices($self, $code, -1, $timeframe);
100 }
101  
102 =item C<< $db->get_last_prices($code, $limit, $timeframe) >>
103  
104 Returns a Finance::GeniusTrader::Prices object containing the $limit last known prices for
105 the symbol $code.
106  
107 =cut
108 sub get_last_prices {
109 my ($self, $code, $limit, $timeframe) = @_;
110  
111 my $q = Finance::GeniusTrader::Prices->new($limit);
112 $timeframe = $DAY unless ($timeframe);
113 die "Intraday support not implemented in DB::pg" if ($timeframe < $DAY);
114 return Finance::GeniusTrader::Prices->new() if ($timeframe > $DAY);
115  
116 $q->set_timeframe($timeframe);
117  
118 my $sql = qq{ SELECT first, high, low, last, volume, date
119 FROM PRICES_$code ORDER BY date DESC };
120 if ($limit > 0) {
121 $sql .= "LIMIT $limit";
122 }
123 my $sth = $self->{'_dbh'}->prepare($sql)
124 || die $self->{'_dbh'}->errstr;
125 $sth->execute() || die $self->{'_dbh'}->errstr;
126 my $array_ref = $sth->fetchall_arrayref( );
127 $q->add_prices_array(reverse(@$array_ref));
128 return $q;
129 }
130  
131 =item C<< $db->get_db_name($code) >>
132  
133 Returns the name of the stock designated by $code.
134  
135 =cut
136 sub get_db_name {
137 my ($self, $code) = @_;
138  
139 my $sql = "SELECT name FROM shares WHERE code = '$code'";
140 my $sth = $self->{'_dbh'}->prepare($sql) || die $self->{'_dbh'}->errstr;
141 $sth->execute() || die $self->{'_dbh'}->errstr;
142 my $res = $sth->fetchrow_arrayref;
143 return $res->[0];
144 }
145  
146 =pod
147  
148 =back
149  
150 =cut
151 1;