Add PostgresVersion.pm method to emit the major version string
authorAndrew Dunstan
Thu, 29 Jul 2021 09:58:08 +0000 (05:58 -0400)
committerAndrew Dunstan
Thu, 29 Jul 2021 09:58:08 +0000 (05:58 -0400)
For versions before 10, this will produce dotted notation unless a
separator argument is given, in which case it is used.

src/test/perl/PostgresVersion.pm

index 4e764c36a55cbeea9f3664b73f349a08804bdf0f..5ff701ce112d4ca863282fa563fb2623d540ab8b 100644 (file)
@@ -32,6 +32,9 @@ PostgresVersion - class representing PostgreSQL version numbers
   # interpolate in a string
   my $stringyval = "version: $version";
 
+  # get the major version
+  my $maj = $version->major;
+
 =head1 DESCRIPTION
 
 PostgresVersion encapsulates Postgres version numbers, providing parsing
@@ -133,4 +136,29 @@ sub _stringify
    return $self->{str};
 }
 
+=pod
+
+=over
+
+=item major([separator => 'char'])
+
+Returns the major version. For versions before 10 the parts are separated by
+a dot unless the separator argument is given.
+
+=back
+
+=cut
+
+sub major
+{
+    my ($self, %params) = @_;
+    my $result = $self->{num}->[0];
+    if ($result + 0 < 10)
+    {
+        my $sep = $params{separator} || '.';
+        $result .= "$sep$self->{num}->[1]";
+    }
+    return $result;
+}
+
 1;