Skip to content

Commit

Permalink
Add support for PostgreSQL 15, 16
Browse files Browse the repository at this point in the history
This requires putting in a replacement for the pg_atoi() function,
which was removed in PostgreSQL.

fixes #20, fixes #21, fixes #25
  • Loading branch information
petere committed Dec 5, 2023
1 parent ff24a49 commit 7e6d6a6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ task:
image: ubuntu:20.04
env:
matrix:
- PGVERSION: 16
- PGVERSION: 15
- PGVERSION: 14
- PGVERSION: 13
- PGVERSION: 12
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.dylib
*.o
*.so
/results/
Expand Down
54 changes: 53 additions & 1 deletion inout.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,65 @@
#include <limits.h>


/*
* Copy of old pg_atoi() from PostgreSQL, cut down to support int8 only.
*/
static int8
my_pg_atoi8(const char *s)
{
long result;
char *badp;

/*
* Some versions of strtol treat the empty string as an error, but some
* seem not to. Make an explicit test to be sure we catch it.
*/
if (s == NULL)
elog(ERROR, "NULL pointer");
if (*s == 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"integer", s)));

errno = 0;
result = strtol(s, &badp, 10);

/* We made no progress parsing the string, so bail out */
if (s == badp)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"integer", s)));

if (errno == ERANGE || result < SCHAR_MIN || result > SCHAR_MAX)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for 8-bit integer", s)));

/*
* Skip any trailing whitespace; if anything but whitespace remains before
* the terminating character, bail out
*/
while (*badp && isspace((unsigned char) *badp))
badp++;

if (*badp)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"integer", s)));

return (int8) result;
}

PG_FUNCTION_INFO_V1(int1in);
Datum
int1in(PG_FUNCTION_ARGS)
{
char *s = PG_GETARG_CSTRING(0);

PG_RETURN_INT8(pg_atoi(s, sizeof(int8), '\0'));
PG_RETURN_INT8(my_pg_atoi8(s));
}

PG_FUNCTION_INFO_V1(int1out);
Expand Down

0 comments on commit 7e6d6a6

Please sign in to comment.