|
| 1 | +############################################################################ |
| 2 | +# |
| 3 | +# PostgresVersion.pm |
| 4 | +# |
| 5 | +# Module encapsulating Postgres Version numbers |
| 6 | +# |
| 7 | +# Copyright (c) 2021, PostgreSQL Global Development Group |
| 8 | +# |
| 9 | +############################################################################ |
| 10 | + |
| 11 | +=pod |
| 12 | +
|
| 13 | +=head1 NAME |
| 14 | +
|
| 15 | +PostgresVersion - class representing PostgreSQL version numbers |
| 16 | +
|
| 17 | +=head1 SYNOPSIS |
| 18 | +
|
| 19 | + use PostgresVersion; |
| 20 | +
|
| 21 | + my $version = PostgresVersion->new($version_arg); |
| 22 | +
|
| 23 | + # compare two versions |
| 24 | + my $bool = $version1 <= $version2; |
| 25 | +
|
| 26 | + # or compare with a number |
| 27 | + $bool = $version < 12; |
| 28 | +
|
| 29 | + # or with a string |
| 30 | + $bool = $version lt "13.1"; |
| 31 | +
|
| 32 | + # interpolate in a string |
| 33 | + my $stringyval = "version: $version"; |
| 34 | +
|
| 35 | +=head1 DESCRIPTION |
| 36 | +
|
| 37 | +PostgresVersion encapsulated Postgres version numbers, providing parsing |
| 38 | +of common version formats and comparison operations. |
| 39 | +
|
| 40 | +=cut |
| 41 | + |
| 42 | +package PostgresVersion; |
| 43 | + |
| 44 | +use strict; |
| 45 | +use warnings; |
| 46 | + |
| 47 | +use Scalar::Util qw(blessed); |
| 48 | + |
| 49 | +use overload |
| 50 | + '<=>' => \&_version_cmp, |
| 51 | + 'cmp' => \&_version_cmp, |
| 52 | + '""' => \&_stringify; |
| 53 | + |
| 54 | +=pod |
| 55 | +
|
| 56 | +=head1 METHODS |
| 57 | +
|
| 58 | +=over |
| 59 | +
|
| 60 | +=item PostgresVersion->new($version) |
| 61 | +
|
| 62 | +Create a new PostgresVersion instance. |
| 63 | +
|
| 64 | +The argument can be a number like 12, or a string like '12.2' or the output |
| 65 | +of a Postgres command like `psql --version` or `pg_config --version`; |
| 66 | +
|
| 67 | +=back |
| 68 | +
|
| 69 | +=cut |
| 70 | + |
| 71 | +sub new |
| 72 | +{ |
| 73 | + my $class = shift; |
| 74 | + my $arg = shift; |
| 75 | + |
| 76 | + # Accept standard formats, in case caller has handed us the output of a |
| 77 | + # postgres command line tool |
| 78 | + $arg = $1 |
| 79 | + if ($arg =~ m/\(?PostgreSQL\)? (\d+(?:\.\d+)*(?:devel)?)/); |
| 80 | + |
| 81 | + # Split into an array |
| 82 | + my @result = split(/\./, $arg); |
| 83 | + |
| 84 | + # Treat development versions as having a minor/micro version one less than |
| 85 | + # the first released version of that branch. |
| 86 | + if ($result[$#result] =~ m/^(\d+)devel$/) |
| 87 | + { |
| 88 | + pop(@result); |
| 89 | + push(@result, $1, -1); |
| 90 | + } |
| 91 | + |
| 92 | + my $res = [@result]; |
| 93 | + bless $res, $class; |
| 94 | + return $res; |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +# Routine which compares the _pg_version_array obtained for the two |
| 99 | +# arguments and returns -1, 0, or 1, allowing comparison between two |
| 100 | +# PostgresVersion objects or a PostgresVersion and a version string or number. |
| 101 | +# |
| 102 | +# If the second argument is not a blessed object we call the constructor |
| 103 | +# to make one. |
| 104 | +# |
| 105 | +# Because we're overloading '<=>' and 'cmp' this function supplies us with |
| 106 | +# all the comparison operators ('<' and friends, 'gt' and friends) |
| 107 | +# |
| 108 | +sub _version_cmp |
| 109 | +{ |
| 110 | + my ($a, $b) = @_; |
| 111 | + |
| 112 | + $b = __PACKAGE__->new($b) unless blessed($b); |
| 113 | + |
| 114 | + for (my $idx = 0;; $idx++) |
| 115 | + { |
| 116 | + return 0 unless (defined $a->[$idx] && defined $b->[$idx]); |
| 117 | + return $a->[$idx] <=> $b->[$idx] |
| 118 | + if ($a->[$idx] <=> $b->[$idx]); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +# Render the version number in the standard "joined by dots" notation if |
| 123 | +# interpolated into a string. Put back 'devel' if we previously turned it |
| 124 | +# into a -1. |
| 125 | +sub _stringify |
| 126 | +{ |
| 127 | + my $self = shift; |
| 128 | + my @sections = @$self; |
| 129 | + if ($sections[-1] == -1) |
| 130 | + { |
| 131 | + pop @sections; |
| 132 | + $sections[-1] = "$sections[-1]devel"; |
| 133 | + } |
| 134 | + return join('.', @sections); |
| 135 | +} |
| 136 | + |
| 137 | +1; |
0 commit comments