[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GT] Mapping of database tables in genericdbi
The attached patch is specific to generic dbi and solves the problem
of mapping database tables names to the timeframe which is being
requested.
Currently, genericdbi assumes that a GT timeframe code will be present
in either a table name or column value in the database.
For instance, the GT code for hourly data is 60.
If you have hourly data, genericdbi requires that either your table be
called EURUSD_60, or has a column with value 60.
With the attached patch you can have whatever name you want and then
map the names to GT timeframe codes in ~/.gt/options using:
DB::genericdbi::tf_map::60 3600
I needed this to be able to use GT with a database from another
application which has different timeframe codes than GT.
Index: Conf.pm
===================================================================
--- Conf.pm (revision 644)
+++ Conf.pm (working copy)
@@ -148,13 +148,20 @@
close FILE;
}
-=item C<< GT::Conf::get($key) >>
+=item C<< GT::Conf::get($key,$defaultValue) >>
-Return the configuration value for the given key. Returns undef if the
-key doesn't exist.
+Return the configuration value for the given key. If the
+key doesn't exist, it returns the optional defaultValue.
+If neither the key nor defaultValue exist, it returns undef.
+
=cut
-sub get { return $conf{lc($_[0])}; }
+sub get {
+ my $value = $conf{lc($_[0])};
+ return $value if (defined($value));
+ return $_[1] if(defined($_[1]));
+ return undef;
+}
=item C<< GT::Conf::set($key, $value) >>
Index: DB/genericdbi.pm
===================================================================
--- DB/genericdbi.pm (revision 644)
+++ DB/genericdbi.pm (working copy)
@@ -85,8 +85,8 @@
my $dbdriver= GT::Conf::get("DB::genericdbi::db");
my $dbname = GT::Conf::get("DB::genericdbi::dbname");
- die("Invalid configuration. Please specify a valid dbi driver in your options file") unless ($dbdriver);
- die("Invalid configuration. Please specify a valid database name in your options file") unless ($dbname);
+ die("Invalid configuration. Please specify a valid dbi driver in your options file (DB::genericdbi::db)") unless ($dbdriver);
+ die("Invalid configuration. Please specify a valid database name in your options file (DB::genericdbi::dbname)") unless ($dbname);
eval "use DBD::" . $dbdriver . ";";
@@ -123,7 +123,7 @@
=cut
sub get_prices {
- my ($self, $code, $timeframe) = @_;
+ my ($self, $code, $timeframe) = @_;
return get_last_prices($self, $code, -1, $timeframe);
}
@@ -136,23 +136,24 @@
sub get_last_prices {
my ($self, $code, $limit, $timeframe) = @_;
$timeframe = $DAY unless ($timeframe);
- $limit = 99999999 if ($limit==-1);
+ $limit = 99999999 if ($limit==-1);
my $q = GT::Prices->new($limit);
$q->set_timeframe($timeframe);
- my $sql = GT::Conf::get("DB::genericdbi::prices_sql::$timeframe");
- if (!defined($sql)) { $sql = GT::Conf::get("DB::genericdbi::prices_sql") || die("Invalid configuration. You must specify a valid prices sql statment for your database in the options file")};
- $sql =~ s/\$code/$code/;
- $sql =~ s/\$timeframe/$timeframe/;
- $sql =~ s/\$limit/$limit/;
+ my $sql = GT::Conf::get("DB::genericdbi::prices_sql::$timeframe", GT::Conf::get('DB::genericdbi::prices_sql'));
+ die("Invalid configuration. You must specify a valid prices sql statment for your database in the options file") if (!defined($sql));
+ $sql =~ s/\$code/$code/;
+ my $tf_map_value = GT::Conf::get("DB::genericdbi::tf_map::$timeframe",$timeframe);
+ $sql =~ s/\$timeframe/$tf_map_value/;
+ $sql =~ s/\$limit/$limit/;
my $sth = $self->{'_dbh'}->prepare($sql)
- || die $self->{'_dbh'}->errstr;
+ || die $self->{'_dbh'}->errstr;
if ($sth->execute()) {# || die $self->{'_dbh'}->errstr;
my $array_ref = $sth->fetchall_arrayref();
$q->add_prices_array(reverse(@$array_ref));
- }
+ }
return $q;
}