package GT::DB::Postgresql; # Copyright 2003 Oliver Bossert # This file is distributed under the terms of the General Public License # version 2 or (at your option) any later version. use strict; use vars qw(@ISA); use GT::DB; use GT::DB::CSV; use GT::Prices; use GT::Conf; use GT::DateTime; use Time::Local; use DBI; @ISA = qw(GT::DB::CSV); =head1 NAME GT::DB::Postgresql - Access to a Postgresql-database =head1 DESCRIPTION This module handels the access to a Postgresql-db. You need to install DBD::Pg and authorize the user: > su postgres > createuser olf > createdb quotes =head2 Configuration You can put some configuration items in ~/.gt/options to indicate where the database is. =over =item DB::csv::database : the type of the database ("Pg" by default) =item DB::csv::dbname : the name of the database ("quotes" by default) =item DB::csv::dbhost : the host of the database ("" = localhost by default) =item DB::csv::dbuser : the user account on the database =item DB::csv::dbpasswd : the password of the user account =back =head2 Functions =over =item C<< GT::DB::csv->new() >> =cut sub new { my $type = shift; my $class = ref($type) || $type; GT::Conf::default("DB::postgresql::database", "Pg"); GT::Conf::default("DB::postgresql::dbname", "quotes"); GT::Conf::default("DB::postgresql::dbhost", ""); #aka localhost GT::Conf::default("DB::postgresql::dbuser", ""); #aka current user GT::Conf::default("DB::postgresql::dbpasswd", ""); #aka user is already identified my $self = { 'database' => GT::Conf::get("DB::postgresql::database"), 'dbname' => GT::Conf::get("DB::postgresql::dbname"), 'dbhost' => GT::Conf::get("DB::postgresql::dbhost"), 'dbuser' => GT::Conf::get("DB::postgresql::dbuser"), 'dbpasswd' => GT::Conf::get("DB::postgresql::dbpasswd"), @_ }; my $addstring = ""; if ($self->{'dbhost'}) { $addstring .= ";host=" . $self->{'dbhost'}; } GT::Conf::default("DB::postgresql::connectstring", "DBI:" . $self->{'database'} . ":dbname=" . $self->{'dbname'} . $addstring ); my $connect_string = GT::Conf::get("DB::postgresql::connectstring"); $self->{'_dbh'} = DBI->connect($connect_string, $self->{'dbuser'}, $self->{'dbpasswd'}, {PrintError=>0, AutoCommit=>1,} ) or die "Couldn't connect to database !\n"; return bless $self, $class; } =item C<< $db->init_table($code) >> Creates the table of stock $code. =cut sub init_table { my ($self, $code) = @_; # If we use a CVS-Database, create the directory if ( $self->{'database'} eq "CSV" ) { my $DB_DIR = $self->{'dbname'}; if (! -d $DB_DIR) { mkdir($DB_DIR, 0755) || die "Could not create directory $DB_DIR."; } } $self->{'_dbh'}->do("CREATE TABLE PRICES_$code ( open REAL, close REAL, high REAL, low REAL, volume REAL, date CHAR(10) PRIMARY KEY )") or die "Could not create table PRICES_$code."; } =pod =back =cut 1;