libpqtypes - varlena.c

Home Page

array.c
datetime.c
error.c
events.c
exec.c
geo.c
getaddrinfo.h
handler.c
libpqtypes-int.h
libpqtypes.h
misc.c
network.c
numerics.c
param.c
port.c
record.c
regression-test.c
spec.c
utils.c
varlena.c
 1 
 2 /*
 3  * varlena.c
 4  *   Type handler for the variable length data types.
 5  *
 6  * Copyright (c) 2011 eSilo, LLC. All rights reserved.
 7  * This is free software; see the source for copying conditions.  There is
 8  * NO warranty; not even for MERCHANTABILITY or  FITNESS FOR A  PARTICULAR
 9  * PURPOSE.
10  */
11 
12 #include "libpqtypes-int.h"
13 
14 /* put a type in its string representation, text format */
15 int
16 pqt_put_str(PGtypeArgs *args)
17 {
18   args->format = TEXTFMT;
19   args->put.out = va_arg(args->ap, char *);
20   return args->put.out ? (int)strlen(args->put.out)+1 : 0;
21 }
22 
23 /* varchar, bpchar and name use the text handlers */
24 int
25 pqt_put_text(PGtypeArgs *args)
26 {
27   args->put.out = va_arg(args->ap, PGtext);
28   return args->put.out ? (int)strlen(args->put.out) : 0;
29 }
30 
31 /* varchar, bpchar and name use the text handlers */
32 int
33 pqt_get_text(PGtypeArgs *args)
34 {
35   DECLVALUE(args);
36   PGtext *textp = va_arg(args->ap, PGtext *);
37   CHKGETVALS(args, textp);
38   *textp = value;
39   return 0;
40 }
41 
42 int
43 pqt_put_bytea(PGtypeArgs *args)
44 {
45   PGbytea *bytea = va_arg(args->ap, PGbytea *);
46   PUTNULLCHK(args, bytea);
47   args->put.out = bytea->data;
48   return bytea->len;
49 }
50 
51 int
52 pqt_get_bytea(PGtypeArgs *args)
53 {
54   DECLVALUE(args);
55   DECLLENGTH(args);
56   PGbytea *bytea = va_arg(args->ap, PGbytea *);
57 
58   CHKGETVALS(args, bytea);
59 
60   if (args->format == TEXTFMT)
61   {
62     size_t len = 0;
63 
64     value = (char *) PQunescapeBytea((const unsigned char *) value, &len);
65     if (!value)
66       RERR_STR2INT(args);
67 
68     bytea->data = (char *) PQresultAlloc(args->get.result, len);
69     if (!bytea->data)
70     {
71       PQfreemem(value);
72       RERR_MEM(args);
73     }
74 
75     bytea->len = (int) len;
76     memcpy(bytea->data, value, len);
77     PQfreemem(value);
78     return 0;
79   }
80 
81   /* binary format */
82   bytea->len = valuel;
83   bytea->data = value;
84   return 0;
85 }