Skip to content

Commit

Permalink
[agroal#291] Make the version number comparable
Browse files Browse the repository at this point in the history
Three new constants are introduced here:
- PGAGROAL_MAJOR_VERSION (number)
- PGAGROAL_MINOR_VERSION (number)
- PGAGROAL_PATCH_VERSION (number)
- PGAGROAL_VERSION       (string)

that represent, as integer literals and string composition,
the parts of the version number.
The values are copied from the variables in the `CmakeLists.txt` file
that creates the building flow, so changing the values in a single
place changes and propagates the changes all over the source code.

A few utility functions have been introduced to compose the three
constant into a unique number that can be used to compare the version
currently running, so that features and/or message can be targeted as
conditionals.

Introduced functions are:
- pgagroal_version_as_number() returns an unique sortable version
number. For example, version 1.5.0 is returned as 15000, version 1.6.2
as 16002, and so on;
- pgagroal_version_number() returns the currently running version
number, that is invokes pgagroal_version_as_number() with the
predefined constants.

The `pgagroal_version_as_number()` accepts individual values, while
`pgagroal_version_number()` provides the currently running version
number. The idea is that, thanks to both the functions, it is possible
to check for a feature that depends on a version in a way like the
following one:

   if (pgagroal_version_numer() >=
             pgagroal_version_as_number(2,1,0))
       // version is greater than 2.1.0 !

The utility function `pgagroal_version_ge()` does exactly the above:
it accepts the three parts of a version number and checks if the
currently running version is greater or equal (hence, 'ge') of the
specified triplet, so that the above piece of code becomes:

   if (pgagroal_version_ge(2,1,0))
     // version greater or equal 2.1.0

Close agroal#292
See agroal#253, agroal#289, agroal#290, agroal#293
  • Loading branch information
fluca1978 committed Jul 28, 2022
1 parent b089d43 commit cce145a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ add_compile_options(-D_GNU_SOURCE)
add_compile_options(-Wno-deprecated)
add_compile_options(-Wno-deprecated-declarations)

#
# version number and string management
#
add_compile_options(-DPGAGROAL_MAJOR_VERSION=${VERSION_MAJOR})
add_compile_options(-DPGAGROAL_MINOR_VERSION=${VERSION_MINOR})
add_compile_options(-DPGAGROAL_PATCH_VERSION=${VERSION_PATCH})
add_compile_options(-DPGAGROAL_VERSION="${VERSION_STRING}")

set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")

if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
Expand Down
4 changes: 2 additions & 2 deletions src/admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ static char* generate_password(int pwd_length);
static void
version(void)
{
printf("pgagroal-admin %s\n", VERSION);
printf("pgagroal-admin %s\n", PGAGROAL_VERSION);
exit(1);
}

static void
usage(void)
{
printf("pgagroal-admin %s\n", VERSION);
printf("pgagroal-admin %s\n", PGAGROAL_VERSION);
printf(" Administration utility for pgagroal\n");
printf("\n");

Expand Down
4 changes: 2 additions & 2 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ static int reload(SSL* ssl, int socket);
static void
version(void)
{
printf("pgagroal-cli %s\n", VERSION);
printf("pgagroal-cli %s\n", PGAGROAL_VERSION);
exit(1);
}

static void
usage(void)
{
printf("pgagroal-cli %s\n", VERSION);
printf("pgagroal-cli %s\n", PGAGROAL_VERSION);
printf(" Command line utility for pgagroal\n");
printf("\n");

Expand Down
2 changes: 0 additions & 2 deletions src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ extern "C" {
#include <sys/types.h>
#include <openssl/ssl.h>

#define VERSION "1.5.0"

#define PGAGROAL_HOMEPAGE "https://agroal.github.io/pgagroal/"
#define PGAGROAL_ISSUES "https://github.com/agroal/pgagroal/issues"

Expand Down
37 changes: 37 additions & 0 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,43 @@ pgagroal_set_proc_title(int argc, char** argv, char* s1, char* s2);
void
pgagroal_set_connection_proc_title(int argc, char** argv, struct connection* connection);

/**
* Provide the application version number as a unique value composed of the three
* specified parts. For example, when invoked with (1,5,0) it returns 10500.
* Every part of the number must be between 0 and 99, and the function
* applies a restriction on the values. For example passing 1 or 101 as one of the part
* will produce the same result.
*
* @param major the major version number
* @param minor the minor version number
* @param patch the patch level
* @returns a number made by (patch + minor * 100 + major * 10000 )
*/
unsigned int
pgagroal_version_as_number(unsigned int major, unsigned int minor, unsigned int patch);

/**
* Provides the current version number of the application.
* It relies on `pgagroal_version_as_number` and invokes it with the
* predefined constants.
*
* @returns the current version number
*/
unsigned int
pgagroal_version_number(void);

/**
* Checks if the currently running version number is
* greater or equal than the specied one.
*
* @param major the major version number
* @param minor the minor version number
* @param patch the patch level
* @returns true if the current version is greater or equal to the specified one
*/
bool
pgagroal_version_ge(unsigned int major, unsigned int minor, unsigned int patch);

#ifdef DEBUG

/**
Expand Down
29 changes: 29 additions & 0 deletions src/libpgagroal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,35 @@ pgagroal_set_connection_proc_title(int argc, char** argv, struct connection* con
pgagroal_set_proc_title(argc, argv, info, connection->database);
}

unsigned int
pgagroal_version_as_number(unsigned int major, unsigned int minor, unsigned int patch)
{
return (patch % 100)
+ (minor % 100) * 100
+ (major % 100) * 10000;
}

unsigned int
pgagroal_version_number(void)
{
return pgagroal_version_as_number(PGAGROAL_MAJOR_VERSION,
PGAGROAL_MINOR_VERSION,
PGAGROAL_PATCH_VERSION);
}

bool
pgagroal_version_ge(unsigned int major, unsigned int minor, unsigned int patch)
{
if (pgagroal_version_number() >= pgagroal_version_as_number(major, minor, patch))
{
return true;
}
else
{
return false;
}
}

#ifdef DEBUG

int
Expand Down
9 changes: 6 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ shutdown_management(void)
static void
version(void)
{
printf("pgagroal %s\n", VERSION);
printf("pgagroal %s\n", PGAGROAL_VERSION);
exit(1);
}

static void
usage(void)
{
printf("pgagroal %s\n", VERSION);
printf("pgagroal %s\n", PGAGROAL_VERSION);
printf(" High-performance connection pool for PostgreSQL\n");
printf("\n");

Expand Down Expand Up @@ -1039,7 +1039,10 @@ main(int argc, char** argv)
start_management();
}

pgagroal_log_info("pgagroal: started on %s:%d", config->host, config->port);
pgagroal_log_info("pgagroal: %s started on %s:%d",
PGAGROAL_VERSION,
config->host,
config->port);
for (int i = 0; i < main_fds_length; i++)
{
pgagroal_log_debug("Socket: %d", *(main_fds + i));
Expand Down

0 comments on commit cce145a

Please sign in to comment.