Skip to content

Commit e30b8b0

Browse files
committed
Revamp send/receive and test it.
Follow ltree's example by prepending a version and using the pg_msg functions. Add tests. A little hinky to use `\copy` to write to a file and then read it back in, but it is the only way I could figure out to test the receive function, which binary COPY FROM uses. Increment to v0.32.0, update the copyright date, and test on Postgres 15.
1 parent ab13f60 commit e30b8b0

File tree

12 files changed

+133
-40
lines changed

12 files changed

+133
-40
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
build:
1010
strategy:
1111
matrix:
12-
pg: [14, 13, 12, 11, 10, 9.6, 9.5, 9.4, 9.3, 9.2]
12+
pg: [15, 14, 13, 12, 11, 10, 9.6, 9.5, 9.4, 9.3, 9.2]
1313
name: 🐘 PostgreSQL ${{ matrix.pg }}
1414
runs-on: ubuntu-latest
1515
container: pgxn/pgxn-tools

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ regression.out
88
/semver-*
99
/latest-changes.md
1010
/src/*.bc
11+
/semver_binary_copy.bin
12+
/.vscode

Changes

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Revision history for PostgreSQL extension semver.
22

3-
0.31.3
3+
0.32.0
4+
- Add support for binary input (receive) and output (send) functions.
5+
Thanks to Anna Clemens for the pull request (#61)!
46

57
0.31.2 2021-09-28T02:03:35Z
68
- Add an overflow check and properly compare the max size of INT32

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2010-2021 The pg-semver Maintainers: David E. Wheeler, Sam
1+
Copyright (c) 2010-2022 The pg-semver Maintainers: David E. Wheeler, Sam
22
Vilain, Tom Davis, and Xavier Caron.
33

44
This module is free software; you can redistribute it and/or modify it under

META.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "semver",
33
"abstract": "A semantic version data type",
44
"description": "A Postgres data type for the Semantic Version format with support for btree and hash indexing.",
5-
"version": "0.31.2",
5+
"version": "0.32.0",
66
"maintainer": [
77
"David E. Wheeler <[email protected]>",
88
"Sam Vilain <[email protected]>",
@@ -15,7 +15,7 @@
1515
"abstract": "A semantic version data type",
1616
"file": "sql/semver.sql",
1717
"docfile": "doc/semver.mmd",
18-
"version": "0.31.2"
18+
"version": "0.32.0"
1919
}
2020
},
2121
"prereqs": {

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
semver 0.31.2
1+
semver 0.32.0
22
=============
33

44
[![PGXN version](https://badge.fury.io/pg/semver.svg)](https://badge.fury.io/pg/semver)
@@ -78,7 +78,7 @@ for testing, PL/pgSQL.
7878
Copyright and License
7979
---------------------
8080

81-
Copyright (c) 2010-2021 The pg-semver Maintainers: David E. Wheeler, Sam
81+
Copyright (c) 2010-2022 The pg-semver Maintainers: David E. Wheeler, Sam
8282
Vilain, Tom Davis, and Xavier Caron.
8383

8484
This module is free software; you can redistribute it and/or modify it under

doc/semver.mmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
semver 0.31.2
1+
semver 0.32.0
22
=============
33

44
Synopsis
@@ -342,7 +342,7 @@ Authors
342342
Copyright and License
343343
---------------------
344344

345-
Copyright (c) 2010-2021 The pg-semver Maintainers: David E. Wheeler, Sam
345+
Copyright (c) 2010-2022 The pg-semver Maintainers: David E. Wheeler, Sam
346346
Vilain, Tom Davis, and Xavier Caron.
347347

348348
This module is free software; you can redistribute it and/or modify it under

semver.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# semver extension
22
comment = 'Semantic version data type'
3-
default_version = '0.31.2'
3+
default_version = '0.32.0'
44
module_pathname = '$libdir/semver'
55
relocatable = true

sql/semver--0.31.2--0.32.0.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE OR REPLACE FUNCTION semver_recv(internal)
2+
RETURNS semver
3+
AS 'semver'
4+
LANGUAGE C STRICT IMMUTABLE;
5+
6+
CREATE OR REPLACE FUNCTION semver_send(semver)
7+
RETURNS bytea
8+
AS 'semver'
9+
LANGUAGE C STRICT IMMUTABLE;
10+
11+
ALTER TYPE semver SET
12+
RECEIVE = semver_recv,
13+
SEND = semver_send;

src/semver.c

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* + Tom Davis <[email protected]>
77
* + Xavier Caron <[email protected]>
88
*
9-
* Copyright 2010-2021 The pg-semver Maintainers. This program is Free
9+
* Copyright 2010-2022 The pg-semver Maintainers. This program is Free
1010
* Software; see the LICENSE file for the license conditions.
1111
*/
1212

@@ -343,42 +343,58 @@ semver_out(PG_FUNCTION_ARGS) {
343343
PG_RETURN_CSTRING(result);
344344
}
345345

346-
PG_FUNCTION_INFO_V1(semver_recv);
346+
/*
347+
* semver type send function
348+
*
349+
* The type is sent as text in binary mode, so this is almost the same as the
350+
* output function, but it's prefixed with a version number so we can change the
351+
* binary format sent in future if necessary. For now, only version 1 is
352+
* supported.
353+
*/
354+
PG_FUNCTION_INFO_V1(semver_send);
347355
Datum
348-
semver_recv(PG_FUNCTION_ARGS) {
349-
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
350-
const int len = buf->len - buf->cursor;
351-
char* str = palloc(len + 1);
352-
bool bad = false;
353-
semver* result;
354-
355-
pq_copymsgbytes(buf, str, len);
356-
str[len] = 0;
357-
358-
result = parse_semver(str, false, true, &bad);
356+
semver_send(PG_FUNCTION_ARGS) {
357+
semver *result = PG_GETARG_SEMVER_P(0);
358+
char *str = emit_semver(result);
359+
char version = 1;
360+
361+
StringInfoData buf;
362+
pq_begintypsend(&buf);
363+
pq_sendbyte(&buf, version);
364+
pq_sendtext(&buf, str, strlen(str));
359365
pfree(str);
360-
if (!result) PG_RETURN_NULL();
361366

362-
PG_RETURN_POINTER(result);
367+
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
363368
}
364369

365-
PG_FUNCTION_INFO_V1(semver_send);
370+
/*
371+
* semver type recv function
372+
*
373+
* The type is sent as text in binary mode, so this is almost the same as the
374+
* input function, but it's prefixed with a version number so we can change the
375+
* binary format sent in future if necessary. For now, only version 1 is
376+
* supported.
377+
*/
378+
PG_FUNCTION_INFO_V1(semver_recv);
366379
Datum
367-
semver_send(PG_FUNCTION_ARGS) {
368-
bytea* output;
369-
370-
semver* version = PG_GETARG_SEMVER_P(0);
371-
char* str = emit_semver(version);
372-
int len = strlen(str);
373-
374-
output = palloc(VARHDRSZ + len);
375-
376-
memcpy(VARDATA(output), str, len);
377-
SET_VARSIZE(output, VARHDRSZ + len);
380+
semver_recv(PG_FUNCTION_ARGS) {
381+
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
382+
char version = pq_getmsgbyte(buf);
383+
char *str;
384+
int nbytes;
385+
bool bad = false;
386+
semver *result;
387+
388+
if (version != 1) {
389+
elog(ERROR, "unsupported semver type version number %d", version);
390+
}
378391

392+
str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
393+
result = parse_semver(str, false, true, &bad);
379394
pfree(str);
380395

381-
PG_RETURN_BYTEA_P(output);
396+
if (!result) PG_RETURN_NULL();
397+
PG_RETURN_POINTER(result);
382398
}
383399

384400
PG_FUNCTION_INFO_V1(text_to_semver);

0 commit comments

Comments
 (0)