1 package App::Info::RDBMS::PostgreSQL;
3 # $Id: PostgreSQL.pm,v 1.1 2004-04-29 09:21:29 ivan Exp $
7 App::Info::RDBMS::PostgreSQL - Information about PostgreSQL
11 use App::Info::RDBMS::PostgreSQL;
13 my $pg = App::Info::RDBMS::PostgreSQL->new;
16 print "App name: ", $pg->name, "\n";
17 print "Version: ", $pg->version, "\n";
18 print "Bin dir: ", $pg->bin_dir, "\n";
20 print "PostgreSQL is not installed. :-(\n";
25 App::Info::RDBMS::PostgreSQL supplies information about the PostgreSQL
26 database server installed on the local system. It implements all of the
27 methods defined by App::Info::RDBMS. Methods that trigger events will trigger
28 them only the first time they're called (See L<App::Info|App::Info> for
29 documentation on handling events). To start over (after, say, someone has
30 installed PostgreSQL) construct a new App::Info::RDBMS::PostgreSQL object to
31 aggregate new metadata.
33 Some of the methods trigger the same events. This is due to cross-calling of
34 shared subroutines. However, any one event should be triggered no more than
35 once. For example, although the info event "Executing `pg_config --version`"
36 is documented for the methods C<name()>, C<version()>, C<major_version()>,
37 C<minor_version()>, and C<patch_version()>, rest assured that it will only be
38 triggered once, by whichever of those four methods is called first.
45 use vars qw(@ISA $VERSION);
46 @ISA = qw(App::Info::RDBMS);
49 my $u = App::Info::Util->new;
57 my $pg = App::Info::RDBMS::PostgreSQL->new(@params);
59 Returns an App::Info::RDBMS::PostgreSQL object. See L<App::Info|App::Info> for
60 a complete description of argument parameters.
62 When it called, C<new()> searches the file system for the F<pg_config>
63 application. If found, F<pg_config> will be called by the object methods below
64 to gather the data necessary for each. If F<pg_config> cannot be found, then
65 PostgreSQL is assumed not to be installed, and each of the object methods will
68 App::Info::RDBMS::PostgreSQL searches for F<pg_config> along your path, as
69 defined by C<File::Spec-E<gt>path>. Failing that, it searches the following
74 =item /usr/local/pgsql/bin
76 =item /usr/local/postgres/bin
113 # Construct the object.
114 my $self = shift->SUPER::new(@_);
117 $self->info("Looking for pg_config");
118 my @paths = ($u->path,
119 qw(/usr/local/pgsql/bin
120 /usr/local/postgres/bin
128 if (my $cfg = $u->first_cat_exe('pg_config', @paths)) {
129 # We found it. Confirm.
130 $self->{pg_config} = $self->confirm( key => 'pg_config',
131 prompt => 'Path to pg_config?',
133 callback => sub { -x },
134 error => 'Not an executable');
136 # Handle an unknown value.
137 $self->{pg_config} = $self->unknown( key => 'pg_config',
138 prompt => 'Path to pg_config?',
139 callback => sub { -x },
140 error => 'Not an executable');
146 # We'll use this code reference as a common way of collecting data.
148 return unless $_[0]->{pg_config};
149 $_[0]->info("Executing `$_[0]->{pg_config} $_[1]`");
150 my $info = `$_[0]->{pg_config} $_[1]`;
155 ##############################################################################
161 my $key_name = App::Info::RDBMS::PostgreSQL->key_name;
163 Returns the unique key name that describes this class. The value returned is
164 the string "PostgreSQL".
168 sub key_name { 'PostgreSQL' }
170 ##############################################################################
172 =head2 Object Methods
176 print "PostgreSQL is ", ($pg->installed ? '' : 'not '), "installed.\n";
178 Returns true if PostgreSQL is installed, and false if it is not.
179 App::Info::RDBMS::PostgreSQL determines whether PostgreSQL is installed based
180 on the presence or absence of the F<pg_config> application on the file system
181 as found when C<new()> constructed the object. If PostgreSQL does not appear
182 to be installed, then all of the other object methods will return empty
187 sub installed { return $_[0]->{pg_config} ? 1 : undef }
189 ##############################################################################
193 my $name = $pg->name;
195 Returns the name of the application. App::Info::RDBMS::PostgreSQL parses the
196 name from the system call C<`pg_config --version`>.
204 Executing `pg_config --version`
208 Failed to find PostgreSQL version with `pg_config --version`
210 Unable to parse name from string
212 Unable to parse version from string
214 Failed to parse PostgreSQL version parts from string
218 Enter a valid PostgreSQL name
224 # This code reference is used by name(), version(), major_version(),
225 # minor_version(), and patch_version() to aggregate the data they need.
226 my $get_version = sub {
228 $self->{'--version'} = 1;
229 my $data = $get_data->($self, '--version');
231 $self->error("Failed to find PostgreSQL version with ".
232 "`$self->{pg_config} --version");
237 my ($name, $version) = split /\s+/, $data, 2;
239 # Check for and assign the name.
241 $self->{name} = $name :
242 $self->error("Unable to parse name from string '$data'");
244 # Parse the version number.
246 my ($x, $y, $z) = $version =~ /(\d+)\.(\d+).(\d+)/;
247 if (defined $x and defined $y and defined $z) {
248 @{$self}{qw(version major minor patch)} =
249 ($version, $x, $y, $z);
251 $self->error("Failed to parse PostgreSQL version parts from " .
252 "string '$version'");
255 $self->error("Unable to parse version from string '$data'");
261 return unless $self->{pg_config};
264 $get_version->($self) unless $self->{'--version'};
266 # Handle an unknown name.
267 $self->{name} ||= $self->unknown( key => 'name' );
270 return $self->{name};
273 ##############################################################################
277 my $version = $pg->version;
279 Returns the PostgreSQL version number. App::Info::RDBMS::PostgreSQL parses the
280 version number from the system call C<`pg_config --version`>.
288 Executing `pg_config --version`
292 Failed to find PostgreSQL version with `pg_config --version`
294 Unable to parse name from string
296 Unable to parse version from string
298 Failed to parse PostgreSQL version parts from string
302 Enter a valid PostgreSQL version number
310 return unless $self->{pg_config};
313 $get_version->($self) unless $self->{'--version'};
315 # Handle an unknown value.
316 unless ($self->{version}) {
317 # Create a validation code reference.
318 my $chk_version = sub {
319 # Try to get the version number parts.
320 my ($x, $y, $z) = /^(\d+)\.(\d+).(\d+)$/;
321 # Return false if we didn't get all three.
322 return unless $x and defined $y and defined $z;
323 # Save all three parts.
324 @{$self}{qw(major minor patch)} = ($x, $y, $z);
328 $self->{version} = $self->unknown( key => 'version number',
329 callback => $chk_version);
332 return $self->{version};
335 ##############################################################################
339 my $major_version = $pg->major_version;
341 Returns the PostgreSQL major version number. App::Info::RDBMS::PostgreSQL
342 parses the major version number from the system call C<`pg_config --version`>.
343 For example, C<version()> returns "7.1.2", then this method returns "7".
351 Executing `pg_config --version`
355 Failed to find PostgreSQL version with `pg_config --version`
357 Unable to parse name from string
359 Unable to parse version from string
361 Failed to parse PostgreSQL version parts from string
365 Enter a valid PostgreSQL major version number
371 # This code reference is used by major_version(), minor_version(), and
372 # patch_version() to validate a version number entered by a user.
373 my $is_int = sub { /^\d+$/ };
377 return unless $self->{pg_config};
379 $get_version->($self) unless exists $self->{'--version'};
380 # Handle an unknown value.
381 $self->{major} = $self->unknown( key => 'major version number',
383 unless $self->{major};
384 return $self->{major};
387 ##############################################################################
391 my $minor_version = $pg->minor_version;
393 Returns the PostgreSQL minor version number. App::Info::RDBMS::PostgreSQL
394 parses the minor version number from the system call C<`pg_config --version`>.
395 For example, if C<version()> returns "7.1.2", then this method returns "2".
403 Executing `pg_config --version`
407 Failed to find PostgreSQL version with `pg_config --version`
409 Unable to parse name from string
411 Unable to parse version from string
413 Failed to parse PostgreSQL version parts from string
417 Enter a valid PostgreSQL minor version number
425 return unless $self->{pg_config};
427 $get_version->($self) unless exists $self->{'--version'};
428 # Handle an unknown value.
429 $self->{minor} = $self->unknown( key => 'minor version number',
431 unless defined $self->{minor};
432 return $self->{minor};
435 ##############################################################################
439 my $patch_version = $pg->patch_version;
441 Returns the PostgreSQL patch version number. App::Info::RDBMS::PostgreSQL
442 parses the patch version number from the system call C<`pg_config --version`>.
443 For example, if C<version()> returns "7.1.2", then this method returns "1".
451 Executing `pg_config --version`
455 Failed to find PostgreSQL version with `pg_config --version`
457 Unable to parse name from string
459 Unable to parse version from string
461 Failed to parse PostgreSQL version parts from string
465 Enter a valid PostgreSQL minor version number
473 return unless $self->{pg_config};
475 $get_version->($self) unless exists $self->{'--version'};
476 # Handle an unknown value.
477 $self->{patch} = $self->unknown( key => 'patch version number',
479 unless defined $self->{patch};
480 return $self->{patch};
483 ##############################################################################
487 my $bin_dir = $pg->bin_dir;
489 Returns the PostgreSQL binary directory path. App::Info::RDBMS::PostgreSQL
490 gathers the path from the system call C<`pg_config --bindir`>.
498 Executing `pg_config --bindir`
502 Cannot find bin directory
506 Enter a valid PostgreSQL bin directory
512 # This code reference is used by bin_dir(), lib_dir(), and so_lib_dir() to
513 # validate a directory entered by the user.
514 my $is_dir = sub { -d };
518 return unless $self->{pg_config};
519 unless (exists $self->{bin_dir} ) {
520 if (my $dir = $get_data->($self, '--bindir')) {
521 $self->{bin_dir} = $dir;
523 # Handle an unknown value.
524 $self->error("Cannot find bin directory");
525 $self->{bin_dir} = $self->unknown( key => 'bin directory',
530 return $self->{bin_dir};
533 ##############################################################################
537 my $inc_dir = $pg->inc_dir;
539 Returns the PostgreSQL include directory path. App::Info::RDBMS::PostgreSQL
540 gathers the path from the system call C<`pg_config --includedir`>.
548 Executing `pg_config --includedir`
552 Cannot find include directory
556 Enter a valid PostgreSQL include directory
564 return unless $self->{pg_config};
565 unless (exists $self->{inc_dir} ) {
566 if (my $dir = $get_data->($self, '--includedir')) {
567 $self->{inc_dir} = $dir;
569 # Handle an unknown value.
570 $self->error("Cannot find include directory");
571 $self->{inc_dir} = $self->unknown( key => 'include directory',
576 return $self->{inc_dir};
579 ##############################################################################
583 my $lib_dir = $pg->lib_dir;
585 Returns the PostgreSQL library directory path. App::Info::RDBMS::PostgreSQL
586 gathers the path from the system call C<`pg_config --libdir`>.
594 Executing `pg_config --libdir`
598 Cannot find library directory
602 Enter a valid PostgreSQL library directory
610 return unless $self->{pg_config};
611 unless (exists $self->{lib_dir} ) {
612 if (my $dir = $get_data->($self, '--libdir')) {
613 $self->{lib_dir} = $dir;
615 # Handle an unknown value.
616 $self->error("Cannot find library directory");
617 $self->{lib_dir} = $self->unknown( key => 'library directory',
622 return $self->{lib_dir};
625 ##############################################################################
629 my $so_lib_dir = $pg->so_lib_dir;
631 Returns the PostgreSQL shared object library directory path.
632 App::Info::RDBMS::PostgreSQL gathers the path from the system call
633 C<`pg_config --pkglibdir`>.
641 Executing `pg_config --pkglibdir`
645 Cannot find shared object library directory
649 Enter a valid PostgreSQL shared object library directory
655 # Location of dynamically loadable modules.
658 return unless $self->{pg_config};
659 unless (exists $self->{so_lib_dir} ) {
660 if (my $dir = $get_data->($self, '--pkglibdir')) {
661 $self->{so_lib_dir} = $dir;
663 # Handle an unknown value.
664 $self->error("Cannot find shared object library directory");
665 $self->{so_lib_dir} =
666 $self->unknown( key => 'shared object library directory',
671 return $self->{so_lib_dir};
674 ##############################################################################
678 my $home_url = $pg->home_url;
680 Returns the PostgreSQL home page URL.
684 sub home_url { "http://www.postgresql.org/" }
686 ##############################################################################
690 my $download_url = $pg->download_url;
692 Returns the PostgreSQL download URL.
696 sub download_url { "http://www.ca.postgresql.org/sitess.html" }
703 Report all bugs via the CPAN Request Tracker at
704 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-Info>.
708 David Wheeler <L<david@wheeler.net|"david@wheeler.net">> based on code by Sam
709 Tregar <L<sam@tregar.com|"sam@tregar.com">>.
713 L<App::Info|App::Info> documents the event handling interface.
715 L<App::Info::RDBMS|App::Info::RDBMS> is the App::Info::RDBMS::PostgreSQL
718 L<DBD::Pg|DBD::Pg> is the L<DBI|DBI> driver for connecting to PostgreSQL
721 L<http://www.postgresql.org/> is the PostgreSQL home page.
723 =head1 COPYRIGHT AND LICENSE
725 Copyright (c) 2002, David Wheeler. All Rights Reserved.
727 This module is free software; you can redistribute it and/or modify it under the
728 same terms as Perl itself.