libpqtypes - misc.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  * misc.c
  4  *   Type handler for CHAR, BOOL, MONEY, UUID 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 int
 15 pqt_put_char(PGtypeArgs *args)
 16 {
 17   *args->put.out = (PGchar) va_arg(args->ap, int);
 18   return 1;
 19 }
 20 
 21 int
 22 pqt_get_char(PGtypeArgs *args)
 23 {
 24   DECLVALUE(args);
 25   PGchar *chp = va_arg(args->ap, PGchar *);
 26   CHKGETVALS(args, chp);
 27   *chp = *value;
 28   return 0;
 29 }
 30 
 31 int
 32 pqt_put_bool(PGtypeArgs *args)
 33 {
 34   *args->put.out = ((char) va_arg(args->ap, PGbool)!=0) ? 1 : 0;
 35   return 1;
 36 }
 37 
 38 int
 39 pqt_get_bool(PGtypeArgs *args)
 40 {
 41   DECLVALUE(args);
 42   PGbool *boolp = va_arg(args->ap, PGbool *);
 43 
 44   CHKGETVALS(args, boolp);
 45 
 46   if (args->format == TEXTFMT)
 47     *boolp = (*value == 't') ? 1 : 0;
 48   else
 49     *boolp = (*value)!=0 ? 1 : 0;
 50   return 0;
 51 }
 52 
 53 int
 54 pqt_put_money(PGtypeArgs *args)
 55 {
 56   PGmoney money = va_arg(args->ap, PGmoney);
 57   int len = args->fmtinfo->sversion >= 80300 ? 8 : 4;
 58 
 59   if (len == 8)
 60     pqt_swap8(args->put.out, &money, 1);
 61   else /* truncate: pre 8.3 server expecting a 4 byte money */
 62     pqt_buf_putint4(args->put.out, (int) money);
 63 
 64   return len;
 65 }
 66 
 67 int
 68 pqt_get_money(PGtypeArgs *args)
 69 {
 70   DECLVALUE(args);
 71   DECLLENGTH(args);
 72   PGmoney *moneyp = va_arg(args->ap, PGmoney *);
 73 
 74   CHKGETVALS(args, moneyp);
 75 
 76   if (args->format == TEXTFMT)
 77   {
 78     char buf[64];
 79     char c, *p = buf;
 80     char *bufend = buf + sizeof(buf);
 81 
 82     for (; (c = *value) != '\0' && p<bufend; ++value)
 83     {
 84       if (isdigit(c) || c == '-')
 85       {
 86         *p = c;
 87         p++;
 88       }
 89     }
 90 
 91     buf[p - buf] = 0;
 92 
 93     if (pqt_text_to_int8(buf, moneyp) == -1)
 94       RERR_STR2INT(args);
 95 
 96     return 0;
 97   }
 98 
 99   /* 8.3 uses a 64-bit money type. Need compatibility
100    * for communicating with older servers.
101    */
102   if (valuel == 4)
103     pqt_buf_putint4(moneyp, pqt_buf_getint4(value));
104   else
105     pqt_swap8(moneyp, value, 0);
106 
107   return 0;
108 }
109 
110 int
111 pqt_put_uuid(PGtypeArgs *args)
112 {
113   PGuuid uuid = va_arg(args->ap, PGuuid);
114   PUTNULLCHK(args, uuid);
115   args->put.out = uuid;
116   return 16;
117 }
118 
119 int
120 pqt_get_uuid(PGtypeArgs *args)
121 {
122   DECLVALUE(args);
123   PGuuid *uuid = va_arg(args->ap, PGuuid *);
124 
125   CHKGETVALS(args, uuid);
126 
127   if (args->format == TEXTFMT)
128   {
129     int i;
130     char buf[128];
131     char *p = buf;
132     DECLLENGTH(args);
133 
134     /* format: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11 */
135     for (i=0; i < valuel; i++)
136     {
137       if (value[i] != '-')
138       {
139         *p++ = (pqt_hex_to_dec(value[i]) << 4) | pqt_hex_to_dec(value[i+1]);
140         i++;
141       }
142     }
143 
144     *uuid = (char *) PQresultAlloc(args->get.result, p - buf);
145     if (!*uuid)
146       RERR_MEM(args);
147 
148     memcpy(*uuid, buf, p - buf);
149     return 0;
150   }
151 
152   *uuid = value;
153   return 0;
154 }
155 
156 int
157 pqt_put_null(PGtypeArgs *args)
158 {
159   args->put.out = NULL;
160   return 0;
161 }