Debian Codegraph @

src/test/perl/PostgreSQL/Version.pm

raw
1 ############################################################################
2 #
3 # PostgreSQL/Version.pm
4 #
5 # Module encapsulating Postgres Version numbers
6 #
7 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
8 #
9 ############################################################################
10
11 =pod
12
13 =head1 NAME
14
15 PostgreSQL::Version - class representing PostgreSQL version numbers
16
17 =head1 SYNOPSIS
18
19 use PostgreSQL::Version;
20
21 my $version = PostgreSQL::Version->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 # get the major version
36 my $maj = $version->major;
37
38 =head1 DESCRIPTION
39
40 PostgreSQL::Version encapsulates Postgres version numbers, providing parsing
41 of common version formats and comparison operations.
42
43 =cut
44
45 package PostgreSQL::Version;
46
47 use strict;
48 use warnings FATAL => 'all';
49
50 use Scalar::Util qw(blessed);
51
52 use overload
53 '<=>' => \&_version_cmp,
54 'cmp' => \&_version_cmp,
55 '""' => \&_stringify;
56
57 =pod
58
59 =head1 METHODS
60
61 =over
62
63 =item PostgreSQL::Version->new($version)
64
65 Create a new PostgreSQL::Version instance.
66
67 The argument can be a number like 12, or a string like '12.2' or the output
68 of a Postgres command like `psql --version` or `pg_config --version`;
69
70 =back
71
72 =cut
73
74 sub new
75 {
76 my $class = shift;
77 my $arg = shift;
78
79 chomp $arg;
80
81 # Accept standard formats, in case caller has handed us the output of a
82 # postgres command line tool
83 my $devel;
84 ($arg, $devel) = ($1, $2)
85 if (
86 $arg =~ m!^ # beginning of line
87 (?:\(?PostgreSQL\)?\s)? # ignore PostgreSQL marker
88 (\d+(?:\.\d+)*) # version number, dotted notation
89 (devel|(?:alpha|beta|rc)\d+)? # dev marker - see version_stamp.pl
90 !x);
91
92 # Split into an array
93 my @numbers = split(/\./, $arg);
94
95 # Treat development versions as having a minor/micro version one less than
96 # the first released version of that branch.
97 push @numbers, -1 if ($devel);
98
99 $devel ||= "";
100
101 return bless { str => "$arg$devel", num => \@numbers }, $class;
102 }
103
104 # Routine which compares the _pg_version_array obtained for the two
105 # arguments and returns -1, 0, or 1, allowing comparison between two
106 # PostgreSQL::Version objects or a PostgreSQL::Version and a version string or number.
107 #
108 # If the second argument is not a blessed object we call the constructor
109 # to make one.
110 #
111 # Because we're overloading '<=>' and 'cmp' this function supplies us with
112 # all the comparison operators ('<' and friends, 'gt' and friends)
113 #
114 sub _version_cmp
115 {
116 my ($a, $b, $swapped) = @_;
117
118 $b = __PACKAGE__->new($b) unless blessed($b);
119
120 ($a, $b) = ($b, $a) if $swapped;
121
122 my ($an, $bn) = ($a->{num}, $b->{num});
123
124 for (my $idx = 0;; $idx++)
125 {
126 return 0
127 if ($idx >= @$an && $idx >= @$bn);
128 # treat a missing number as 0
129 my ($anum, $bnum) = ($an->[$idx] || 0, $bn->[$idx] || 0);
130 return $anum <=> $bnum
131 if ($anum <=> $bnum);
132 }
133 }
134
135 # Render the version number using the saved string.
136 sub _stringify
137 {
138 my $self = shift;
139 return $self->{str};
140 }
141
142 =pod
143
144 =over
145
146 =item major([separator => 'char'])
147
148 Returns the major version. For versions before 10 the parts are separated by
149 a dot unless the separator argument is given.
150
151 =back
152
153 =cut
154
155 sub major
156 {
157 my ($self, %params) = @_;
158 my $result = $self->{num}->[0];
159 if ($result + 0 < 10)
160 {
161 my $sep = $params{separator} || '.';
162 $result .= "$sep$self->{num}->[1]";
163 }
164 return $result;
165 }
166
167 1;