GeniusTrader/DB/bean.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 14 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 / 118 CC 8 SCAR 25.8
CC
8
Cov
0%
get_last_prices
30.0
new
20.0
disconnect
2.0
CRAP 13.2
1 package Finance::GeniusTrader::DB::bean;
2  
3 # Copyright 2003 Sai-kee Wong
4 # This file is distributed under the terms of the General Public License
5 # version 2 or (at your option) any later version.
6  
7 use strict;
8 use vars qw(@ISA);
9  
10 use Finance::GeniusTrader::DB;
11 use Finance::GeniusTrader::Prices;
12 use Finance::GeniusTrader::Conf;
13 use Finance::GeniusTrader::DateTime;
14 use DBI;
15  
16 @ISA = qw(Finance::GeniusTrader::DB);
17  
18 =head1 NAME
19  
20 Finance::GeniusTrader::DB::bean - Access to beancounter database of quotes
21  
22 =head1 DESCRIPTION
23  
24 This module is used to retrieve quotes from a MySQL/PostgreSQL database
25 as setup by beancounter.
26 By default, the database is supposed to be running on localhost and
27 the only authentication done is the standard Unix one.
28  
29 =head2 Configuration
30  
31 You can put some configuration items in ~/.gt/options to indicate where
32 the database is.
33  
34 =over
35  
36 =item DB::bean::dbname : the name of the database ("beancounter" by default)
37  
38 =item DB::bean::dbhost : the host of the database ("" = localhost by default)
39  
40 =item DB::bean::dbport : the port where the server is running ("" = default port number)
41  
42 =item DB::bean::dbuser : the user account on the database (current user by default)
43  
44 =item DB::bean::dbpasswd : the password of the user account
45  
46 =item DB::bean::db : the database being used (mysql|Pg) ("mysql" by default)
47  
48 =back
49  
50 =head2 Functions
51  
52 =over
53  
54 =item C<< Finance::GeniusTrader::DB::mysql->new() >>
55  
56 =cut
57 sub new {
58 my $type = shift;
59 my $class = ref($type) || $type;
60  
61 Finance::GeniusTrader::Conf::default("DB::bean::dbname", "beancounter");
62 Finance::GeniusTrader::Conf::default("DB::bean::dbhost", ""); #aka localhost
63 Finance::GeniusTrader::Conf::default("DB::bean::dbport", ""); #aka std port
64 Finance::GeniusTrader::Conf::default("DB::bean::dbuser", ""); #aka current user
65 Finance::GeniusTrader::Conf::default("DB::bean::dbpasswd", "");#aka user is already identified
66 Finance::GeniusTrader::Conf::default("DB::bean::db", "mysql");
67  
68 # Avoid problem with careless users
69 if (Finance::GeniusTrader::Conf::get("DB::bean::db") eq "pg") {
70 Finance::GeniusTrader::Conf::set("DB::bean::db", "Pg");
71 }
72
73  
74 eval "use DBD::" . Finance::GeniusTrader::Conf::get("DB::bean::db") . ";";
75  
76 my $self = { 'dbname' => Finance::GeniusTrader::Conf::get("DB::bean::dbname"),
77 'dbhost' => Finance::GeniusTrader::Conf::get("DB::bean::dbhost"),
78 'dbport' => Finance::GeniusTrader::Conf::get("DB::bean::dbport"),
79 'dbuser' => Finance::GeniusTrader::Conf::get("DB::bean::dbuser"),
80 'dbpasswd' => Finance::GeniusTrader::Conf::get("DB::bean::dbpasswd"),
81 @_
82 };
83
84 my $connect_string = 'dbi:' . Finance::GeniusTrader::Conf::get("DB::bean::db") .
85 ':dbname=' . $self->{'dbname'};
86 if ($self->{'dbhost'}) {
87 $connect_string .= ";host=" . $self->{'dbhost'};
88 }
89 if ($self->{'dbport'}) {
90 $connect_string .= ";port=" . $self->{'dbport'};
91 }
92 $self->{'_dbh'} = DBI->connect($connect_string, $self->{'dbuser'},
93 $self->{'dbpasswd'}) || die "Couldn't connect to database !\n";
94  
95 return bless $self, $class;
96 }
97  
98 =item C<< $db->disconnect >>
99  
100 Disconnects from the database.
101  
102 =cut
103 sub disconnect {
104 my $self = shift;
105 $self->{'_dbh'}->disconnect;
106 delete $self->{'prices'};
107 delete $self->{'dates'};
108 }
109  
110 =item C<< $db->get_prices($code, $timeframe) >>
111  
112 Returns a Finance::GeniusTrader::Prices object containing all known prices for the symbol $code.
113  
114 =cut
115 sub get_prices {
116 return get_last_prices(@_, -1);
117 }
118  
119 =item C<< $db->get_last_prices($code, $limit, $timeframe) >>
120  
121 Returns a Finance::GeniusTrader::Prices object containing the $limit last known prices for
122 the symbol $code.
123  
124 Notice that beancounter only supports daily data, therefore it will
125 throw an error if you try to retrieve data in timeframes smaller than daily.
126  
127 =cut
128 sub get_last_prices {
129 my ($self, $code, $limit, $timeframe) = @_;
130  
131 $timeframe = $DAY unless($timeframe);
132 die "The beancounter DB module does not support intraday data.\n" if ($timeframe < $DAY);
133 return Finance::GeniusTrader::Prices->new() if ($timeframe > $DAY);
134  
135 my $q = Finance::GeniusTrader::Prices->new($limit);
136 $q->set_timeframe($timeframe);
137  
138 my $sql = qq{ SELECT day_open, day_high, day_low, day_close, volume, date
139 FROM stockprices WHERE symbol = '$code' ORDER BY date DESC };
140 if ($limit > 0) {
141 $sql .= "LIMIT $limit";
142 }
143 my $sth = $self->{'_dbh'}->prepare($sql)
144 || die $self->{'_dbh'}->errstr;
145 $sth->execute() || die $self->{'_dbh'}->errstr;
146 my $array_ref = $sth->fetchall_arrayref( );
147 $q->add_prices_array(reverse(@$array_ref));
148 return $q;
149 }
150  
151 =item C<< $db->get_db_name($code) >>
152  
153 Returns the name of the stock designated by $code.
154  
155 =cut
156 sub get_db_name {
157 my ($self, $code) = @_;
158  
159 my $sql = "SELECT name FROM stockinfo WHERE symbol = '$code'";
160 my $sth = $self->{'_dbh'}->prepare($sql) || die $self->{'_dbh'}->errstr;
161 $sth->execute() || die $self->{'_dbh'}->errstr;
162 my $res = $sth->fetchrow_arrayref;
163 return $res->[0];
164 }
165  
166 =pod
167  
168 =back
169  
170 =cut
171 1;