reord
[libpgclient.git] / include / pgclient / pgfld.h
blobb91f49e5a0908cfa876b09d92f5bee35e8f860ac
1 #ifndef __PGFLD_H__
2 #define __PGFLD_H__
4 #include <stdlib.h>
5 #include <string.h>
6 #include <stdint.h>
7 #include <endian.h>
8 #include <netinet/in.h>
9 #include <gmp.h>
10 #include <uuid/uuid.h>
11 #include <libex/str.h>
12 #include "pgconsts.h"
14 #define NUMERIC_POS 0x0000
15 #define NUMERIC_NEG 0x4000
16 #define NUMERIC_SHORT 0x8000
17 #define NUMERIC_NAN 0xC000
18 #define NUMERIC_DSCALE_MASK 0x3FFF
20 typedef struct in_addr inet_t;
21 typedef int64_t tm_t;
22 typedef int32_t date_t;
23 typedef int32_t fsec_t;
25 typedef struct {
26 int32_t len;
27 uint32_t bit;
28 } pg_bit32_t;
30 typedef struct {
31 int64_t time;
32 int32_t day;
33 int32_t month;
34 } pg_intv_t;
36 typedef union {
37 int32_t i4;
38 int64_t i8;
39 float f4;
40 double f8;
41 char *s;
42 int8_t b;
43 int32_t d;
44 tm_t tm;
45 pg_intv_t intv;
46 mpq_t q;
47 pg_bit32_t bit32;
48 inet_t sin_addr;
49 uuid_t uuid;
50 } pgval_t;
52 int timestamp_to_tm (tm_t dt, struct tm *tm);
53 struct tm *date_to_tm (date_t dt, struct tm *tm);
54 struct tm *interval_to_tm(pg_intv_t *span, struct tm *tm);
55 int tm_to_timestamp (struct tm *tm, fsec_t fsec, int *tzp, tm_t *result);
57 double pg_conv_double (double d);
58 float pg_conv_float (float f);
60 void pg_get_numeric (uint8_t *buf, mpq_t res, uint16_t *scale);
61 str_t *pg_numstr (mpq_ptr x, int base, uint16_t dscale, int float_prec);
63 //typedef void (*pgset_h) (pgmsg_t**, pgfld_t*);
64 typedef struct pgfld pgfld_t;
65 struct pgfld {
66 int is_null;
67 int oid;
68 pgval_t data;
69 size_t len;
70 int fmt;
71 int16_t dscale;
72 // pgset_h pgset;
74 typedef pgfld_t* pgfld_ptr_t;
76 #define PG_DEF_MPZ { ._mp_alloc = 0, ._mp_size = 0, ._mp_d = NULL }
77 #define PG_DEF_MPQ { ._mp_num = PG_DEF_MPZ, ._mp_den = PG_DEF_MPZ }
78 #define PG_DEF_NUMERIC_DATA { .q = {PG_DEF_MPQ} }
80 #define PG_DEF { .is_null = 1, .oid = 0, .fmt = 0 }
81 #define PG_DEF_INT { .is_null = 1, .oid = OID_INT4, .fmt = 1 }
82 #define PG_DEF_BIGINT { .is_null = 1, .oid = OID_INT8, .fmt = 1 }
83 #define PG_DEF_FLOAT { .is_null = 1, .oid = OID_FLOAT4, .fmt = 1 }
84 #define PG_DEF_DOUBLE { .is_null = 1, .oid = OID_FLOAT8, .fmt = 1 }
85 #define PG_DEF_VARCHAR { .is_null = 1, .oid = OID_VARCHAR, .fmt = 0 }
86 #define PG_DEF_CHAR { .is_null = 1, .oid = OID_CHAR, .fmt = 0 }
87 #define PG_DEF_BOOL { .is_null = 1, .oid = OID_BOOL, .fmt = 1 }
88 #define PG_DEF_DATE { .is_null = 1, .oid = OID_DATE, .fmt = 1 }
89 #define PG_DEF_TIMESTAMP { .is_null = 1, .oid = OID_TIMESTAMP, .fmt = 1 }
90 #define PG_DEF_UUID { .is_null = 1, .oid = OID_UUID, .fmt = 1 }
91 #define PG_DEF_BYTEA { .is_null = 1, .oid = OID_BYTEA, .fmt = 1 }
92 #define PG_DEF_BIT32 { .is_null = 1, .oid = OID_BIT, .fmt = 1 }
93 #define PG_DEF_MONEY { .is_null = 1, .oid = OID_MONEY, .fmt = 1 }
94 #define PG_DEF_TEXT { .is_null = 1, .oid = OID_TEXT, .fmt = 0 }
96 #define PG_SET(X,V,L) X.is_null = 0; X.data.s = V, X.len = 0 == L ? strlen(V) : L
97 #define PG_SET_NULL(X) X.is_null = 1
98 #define PG_SET_INT(X,V) X.is_null = 0; X.data.i4 = htobe32(V), X.len = sizeof(int32_t)
99 #define PG_SET_BIGINT(X,V) X.is_null = 0; X.data.i8 = htobe64(V), X.len = sizeof(int64_t)
100 #define PG_SET_FLOAT(X,V) X.is_null = 0; X.data.f4 = pg_conv_float(V), X.len = sizeof(float)
101 #define PG_SET_DOUBLE(X,V) X.is_null = 0; X.data.f8 = pg_conv_double(V), X.len = sizeof(double)
102 #define PG_SET_VARCHAR(X,V,L) X.is_null = 0; X.data.s = V, X.len = 0 == L ? strlen(V) : L
103 #define PG_SET_CHAR(X,V,L) X.is_null = 0; X.data.s = V, X.len = 0 == L ? strlen(V) : L
104 #define PG_SET_BOOL(X,V) X.is_null = 0; X.data.b = V ? 1 : 0, X.len = sizeof(int8_t)
105 #define PG_SET_DATE(X,V) X.is_null = 0; X.data.d = htobe32(V), X.len = sizeof(date_t)
106 #define PG_SET_TIMESTAMP(X,V) X.is_null = 0; X.data.tm = htobe64(V), X.len = sizeof(tm_t)
107 #define PG_SET_BYTEA(X,V,L) X.is_null = 0; X.data.s = V; X.len = L
108 #define PG_SET_UUID(X,V) X.is_null = 0; memcpy(X.data.uuid, V, sizeof(uuid_t)), X.len = sizeof(uuid_t)
109 #define PG_SET_BIT32(X,V) X.is_null = 0, X.data.bit32.bit = htobe32(V), X.data.bit32.len = htobe32(32), X.len = sizeof(pg_bit32_t)
110 #define PG_SET_TEXT(X,V,L) X.is_null = 0; X.data.s = V, X.len = 0 == L ? strlen(V) : L
111 #define PG_SET_MONEY(X,V) PG_SET_BIGINT(X,V)
113 #endif