| 1 | ############################################################################### | ||
| 2 | # XML::Template::Source::DBI | ||
| 3 | # | ||
| 4 | # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@bbl.med.upenn.edu> | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | ############################################################################### | ||
| 10 | package XML::Template::Source::DBI; | ||
| 11 | use base 'XML::Template::Base'; | ||
| 12 | |||
| 13 | |||
| 14 | use base 'DBIx::Wrap'; | ||
| 15 | |||
| 16 | |||
| 17 | =pod | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | XML::Template::DBI - A module for handling DBI sources. | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | This module is used to handle access to DBI sources from XML::Template. | ||
| 26 | |||
| 27 | =head2 CONSTRUCTOR | ||
| 28 | |||
| 29 | my $dbi = XML::Template::Source::DBI->new ($sourcename) | ||
| 30 | || die XML::Template::Source::DBI->error (); | ||
| 31 | |||
| 32 | Given the source name, the constructor method C<new> retrieves source | ||
| 33 | information from the XML::Template configuration file, creates a new DBI | ||
| 34 | connection to a database, and returns an object with which you can make | ||
| 35 | database queries. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub new { | ||
| 40 | my $proto = shift; | ||
| 41 | my $sourcename = shift; | ||
| 42 | |||
| 43 | my $class = ref ($proto) || $proto; | ||
| 44 | |||
| 45 | my $self = $class->SUPER::new (); | ||
| 46 | |||
| 47 | # Get source info. | ||
| 48 | my $sourceinfo = $self->get_source_info ($sourcename); | ||
| 49 | |||
| 50 | # Get password. | ||
| 51 | open (PWDFILE, $sourceinfo->{pwdfile}) | ||
| 52 | || return $proto->error ('DBI', "Could not open password file '$sourceinfo->{pwdfile}' for data source '$sourcename': $!"); | ||
| 53 | my $pwd = <PWDFILE>; | ||
| 54 | chomp $pwd; | ||
| 55 | close PWDFILE; | ||
| 56 | |||
| 57 | my $source = DBIx::Wrap->new (DSN => $sourceinfo->{dsn}, | ||
| 58 | User => $sourceinfo->{user}, | ||
| 59 | Password => $pwd) | ||
| 60 | || return $proto->error ('DBI', DBIx::Wrap->error); | ||
| 61 | bless ($source, $class); | ||
| 62 | |||
| 63 | return $source; | ||
| 64 | } | ||
| 65 | |||
| 66 | =pod | ||
| 67 | |||
| 68 | =head1 PUBLIC METHODS | ||
| 69 | |||
| 70 | =head2 is_modified | ||
| 71 | |||
| 72 | my $is_modified = $source->is_modified ($cache_mtime, $table); | ||
| 73 | |||
| 74 | This method returns C<1> if the table specified by the second parameter | ||
| 75 | has changed since C<cache_mtime>, the first parameter, or C<0> otherwise. | ||
| 76 | C<cache_mtime> must be in unix time format. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub is_modified { | ||
| 81 | my $self = shift; | ||
| 82 | my ($cache_mtime, $table) = @_; | ||
| 83 | |||
| 84 | my ($table_status) = $self->show (Action => 'table_status', | ||
| 85 | Table => $table, | ||
| 86 | Format => 'unix'); | ||
| 87 | #warn "$table: $table_status->{Update_time} <> $cache_mtime\n"; | ||
| 88 | return ($table_status->{Update_time} > $cache_mtime) ? 1 : 0; | ||
| 89 | } | ||
| 90 | |||
| 91 | =pod | ||
| 92 | |||
| 93 | =head1 AUTHOR | ||
| 94 | |||
| 95 | Jonathan Waxman | ||
| 96 | <jowaxman@bbl.med.upenn.edu> | ||
| 97 | |||
| 98 | =head1 COPYRIGHT | ||
| 99 | |||
| 100 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 101 | All rights reserved. | ||
| 102 | |||
| 103 | This program is free software; you can redistribute it and/or | ||
| 104 | modify it under the same terms as Perl itself. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | |||
| 109 | 1; |