]> granicus.if.org Git - postgresql/blob - src/include/access/tupmacs.h
Update copyright to 2004.
[postgresql] / src / include / access / tupmacs.h
1 /*-------------------------------------------------------------------------
2  *
3  * tupmacs.h
4  *        Tuple macros used by both index tuples and heap tuples.
5  *
6  *
7  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/include/access/tupmacs.h,v 1.26 2004/08/29 04:13:04 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef TUPMACS_H
15 #define TUPMACS_H
16
17 #include "utils/memutils.h"
18
19 /*
20  * check to see if the ATT'th bit of an array of 8-bit bytes is set.
21  */
22 #define att_isnull(ATT, BITS) (!((BITS)[(ATT) >> 3] & (1 << ((ATT) & 0x07))))
23
24 /*
25  * Given a Form_pg_attribute and a pointer into a tuple's data area,
26  * return the correct value or pointer.
27  *
28  * We return a Datum value in all cases.  If the attribute has "byval" false,
29  * we return the same pointer into the tuple data area that we're passed.
30  * Otherwise, we return the correct number of bytes fetched from the data
31  * area and extended to Datum form.
32  *
33  * On machines where Datum is 8 bytes, we support fetching 8-byte byval
34  * attributes; otherwise, only 1, 2, and 4-byte values are supported.
35  *
36  * Note that T must already be properly aligned for this to work correctly.
37  */
38 #define fetchatt(A,T) fetch_att(T, (A)->attbyval, (A)->attlen)
39
40 /*
41  * Same, but work from byval/len parameters rather than Form_pg_attribute.
42  */
43 #if SIZEOF_DATUM == 8
44
45 #define fetch_att(T,attbyval,attlen) \
46 ( \
47         (attbyval) ? \
48         ( \
49                 (attlen) == (int) sizeof(Datum) ? \
50                         *((Datum *)(T)) \
51                 : \
52           ( \
53                 (attlen) == (int) sizeof(int32) ? \
54                         Int32GetDatum(*((int32 *)(T))) \
55                 : \
56                 ( \
57                         (attlen) == (int) sizeof(int16) ? \
58                                 Int16GetDatum(*((int16 *)(T))) \
59                         : \
60                         ( \
61                                 AssertMacro((attlen) == 1), \
62                                 CharGetDatum(*((char *)(T))) \
63                         ) \
64                 ) \
65           ) \
66         ) \
67         : \
68         PointerGetDatum((char *) (T)) \
69 )
70
71 #else                                                   /* SIZEOF_DATUM != 8 */
72
73 #define fetch_att(T,attbyval,attlen) \
74 ( \
75         (attbyval) ? \
76         ( \
77                 (attlen) == (int) sizeof(int32) ? \
78                         Int32GetDatum(*((int32 *)(T))) \
79                 : \
80                 ( \
81                         (attlen) == (int) sizeof(int16) ? \
82                                 Int16GetDatum(*((int16 *)(T))) \
83                         : \
84                         ( \
85                                 AssertMacro((attlen) == 1), \
86                                 CharGetDatum(*((char *)(T))) \
87                         ) \
88                 ) \
89         ) \
90         : \
91         PointerGetDatum((char *) (T)) \
92 )
93 #endif   /* SIZEOF_DATUM == 8 */
94
95 /*
96  * att_align aligns the given offset as needed for a datum of alignment
97  * requirement attalign.  The cases are tested in what is hopefully something
98  * like their frequency of occurrence.
99  */
100 #define att_align(cur_offset, attalign) \
101 ( \
102         ((attalign) == 'i') ? INTALIGN(cur_offset) : \
103          (((attalign) == 'c') ? ((long)(cur_offset)) : \
104           (((attalign) == 'd') ? DOUBLEALIGN(cur_offset) : \
105                 ( \
106                         AssertMacro((attalign) == 's'), \
107                         SHORTALIGN(cur_offset) \
108                 ))) \
109 )
110
111 /*
112  * att_addlength increments the given offset by the length of the attribute.
113  * attval is only accessed if we are dealing with a variable-length attribute.
114  */
115 #define att_addlength(cur_offset, attlen, attval) \
116 ( \
117         ((attlen) > 0) ? \
118         ( \
119                 (cur_offset) + (attlen) \
120         ) \
121         : (((attlen) == -1) ? \
122         ( \
123                 (cur_offset) + VARATT_SIZE(DatumGetPointer(attval)) \
124         ) \
125         : \
126         ( \
127                 AssertMacro((attlen) == -2), \
128                 (cur_offset) + (strlen(DatumGetCString(attval)) + 1) \
129         )) \
130 )
131
132 /*
133  * store_att_byval is a partial inverse of fetch_att: store a given Datum
134  * value into a tuple data area at the specified address.  However, it only
135  * handles the byval case, because in typical usage the caller needs to
136  * distinguish by-val and by-ref cases anyway, and so a do-it-all macro
137  * wouldn't be convenient.
138  */
139 #if SIZEOF_DATUM == 8
140
141 #define store_att_byval(T,newdatum,attlen) \
142         do { \
143                 switch (attlen) \
144                 { \
145                         case sizeof(char): \
146                                 *(char *) (T) = DatumGetChar(newdatum); \
147                                 break; \
148                         case sizeof(int16): \
149                                 *(int16 *) (T) = DatumGetInt16(newdatum); \
150                                 break; \
151                         case sizeof(int32): \
152                                 *(int32 *) (T) = DatumGetInt32(newdatum); \
153                                 break; \
154                         case sizeof(Datum): \
155                                 *(Datum *) (T) = (newdatum); \
156                                 break; \
157                         default: \
158                                 elog(ERROR, "unsupported byval length: %d", \
159                                          (int) (attlen)); \
160                                 break; \
161                 } \
162         } while (0)
163
164 #else                                                   /* SIZEOF_DATUM != 8 */
165
166 #define store_att_byval(T,newdatum,attlen) \
167         do { \
168                 switch (attlen) \
169                 { \
170                         case sizeof(char): \
171                                 *(char *) (T) = DatumGetChar(newdatum); \
172                                 break; \
173                         case sizeof(int16): \
174                                 *(int16 *) (T) = DatumGetInt16(newdatum); \
175                                 break; \
176                         case sizeof(int32): \
177                                 *(int32 *) (T) = DatumGetInt32(newdatum); \
178                                 break; \
179                         default: \
180                                 elog(ERROR, "unsupported byval length: %d", \
181                                          (int) (attlen)); \
182                                 break; \
183                 } \
184         } while (0)
185 #endif   /* SIZEOF_DATUM == 8 */
186
187 #endif