|
6 | 6 | |
7 | 7 | * + Xavier Caron <[email protected]> |
8 | 8 | * |
9 | | - * Copyright 2010-2021 The pg-semver Maintainers. This program is Free |
| 9 | + * Copyright 2010-2022 The pg-semver Maintainers. This program is Free |
10 | 10 | * Software; see the LICENSE file for the license conditions. |
11 | 11 | */ |
12 | 12 |
|
@@ -343,42 +343,58 @@ semver_out(PG_FUNCTION_ARGS) { |
343 | 343 | PG_RETURN_CSTRING(result); |
344 | 344 | } |
345 | 345 |
|
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); |
347 | 355 | 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)); |
359 | 365 | pfree(str); |
360 | | - if (!result) PG_RETURN_NULL(); |
361 | 366 |
|
362 | | - PG_RETURN_POINTER(result); |
| 367 | + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); |
363 | 368 | } |
364 | 369 |
|
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); |
366 | 379 | 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 | + } |
378 | 391 |
|
| 392 | + str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); |
| 393 | + result = parse_semver(str, false, true, &bad); |
379 | 394 | pfree(str); |
380 | 395 |
|
381 | | - PG_RETURN_BYTEA_P(output); |
| 396 | + if (!result) PG_RETURN_NULL(); |
| 397 | + PG_RETURN_POINTER(result); |
382 | 398 | } |
383 | 399 |
|
384 | 400 | PG_FUNCTION_INFO_V1(text_to_semver); |
|
0 commit comments