~ubuntu-branches/ubuntu/hardy/postgresql-8.4/hardy-backports

« back to all changes in this revision

Viewing changes to src/include/access/reloptions.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * reloptions.h
 
4
 *        Core support for relation options (pg_class.reloptions)
 
5
 *
 
6
 * Note: the functions dealing with text-array reloptions values declare
 
7
 * them as Datum, not ArrayType *, to avoid needing to include array.h
 
8
 * into a lot of low-level code.
 
9
 *
 
10
 *
 
11
 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
 
12
 * Portions Copyright (c) 1994, Regents of the University of California
 
13
 *
 
14
 * $PostgreSQL$
 
15
 *
 
16
 *-------------------------------------------------------------------------
 
17
 */
 
18
#ifndef RELOPTIONS_H
 
19
#define RELOPTIONS_H
 
20
 
 
21
#include "access/htup.h"
 
22
#include "nodes/pg_list.h"
 
23
 
 
24
/* types supported by reloptions */
 
25
typedef enum relopt_type
 
26
{
 
27
        RELOPT_TYPE_BOOL,
 
28
        RELOPT_TYPE_INT,
 
29
        RELOPT_TYPE_REAL,
 
30
        RELOPT_TYPE_STRING
 
31
} relopt_type;
 
32
 
 
33
/* kinds supported by reloptions */
 
34
typedef enum relopt_kind
 
35
{
 
36
        RELOPT_KIND_HEAP,
 
37
        /* XXX do we need a separate kind for TOAST tables? */
 
38
        RELOPT_KIND_BTREE,
 
39
        RELOPT_KIND_HASH,
 
40
        RELOPT_KIND_GIN,
 
41
        RELOPT_KIND_GIST,
 
42
        /* if you add a new kind, make sure you update "last_default" too */
 
43
        RELOPT_KIND_LAST_DEFAULT = RELOPT_KIND_GIST,
 
44
        RELOPT_KIND_MAX = 255
 
45
} relopt_kind;
 
46
 
 
47
/* reloption namespaces allowed for heaps -- currently only TOAST */
 
48
#define HEAP_RELOPT_NAMESPACES { "toast", NULL }
 
49
 
 
50
/* generic struct to hold shared data */
 
51
typedef struct relopt_gen
 
52
{
 
53
        const char *name;       /* must be first (used as list termination marker) */
 
54
        const char *desc;
 
55
        relopt_kind     kind;
 
56
        int                     namelen;
 
57
        relopt_type     type;
 
58
} relopt_gen;
 
59
 
 
60
/* holds a parsed value */
 
61
typedef struct relopt_value
 
62
{
 
63
        relopt_gen *gen;
 
64
        bool            isset;
 
65
        union
 
66
        {
 
67
                bool    bool_val;
 
68
                int             int_val;
 
69
                double  real_val;
 
70
                char   *string_val;     /* allocated separately */
 
71
        } values;
 
72
} relopt_value;
 
73
 
 
74
/* reloptions records for specific variable types */
 
75
typedef struct relopt_bool
 
76
{
 
77
        relopt_gen      gen;
 
78
        bool            default_val;
 
79
} relopt_bool;
 
80
 
 
81
typedef struct relopt_int
 
82
{
 
83
        relopt_gen      gen;
 
84
        int                     default_val;
 
85
        int                     min;
 
86
        int                     max;
 
87
} relopt_int;
 
88
 
 
89
typedef struct relopt_real
 
90
{
 
91
        relopt_gen      gen;
 
92
        double          default_val;
 
93
        double          min;
 
94
        double          max;
 
95
} relopt_real;
 
96
 
 
97
/* validation routines for strings */
 
98
typedef void (*validate_string_relopt) (char *value);
 
99
 
 
100
typedef struct relopt_string
 
101
{
 
102
        relopt_gen      gen;
 
103
        int                     default_len;
 
104
        bool            default_isnull;
 
105
        validate_string_relopt  validate_cb;
 
106
        char            default_val[1]; /* variable length, zero-terminated */
 
107
} relopt_string;
 
108
 
 
109
/* This is the table datatype for fillRelOptions */
 
110
typedef struct
 
111
{
 
112
        const char *optname;            /* option's name */
 
113
        relopt_type     opttype;                /* option's datatype */
 
114
        int                     offset;                 /* offset of field in result struct */
 
115
} relopt_parse_elt;
 
116
 
 
117
 
 
118
/*
 
119
 * These macros exist for the convenience of amoptions writers (but consider
 
120
 * using fillRelOptions, which is a lot simpler).  Beware of multiple
 
121
 * evaluation of arguments!
 
122
 *
 
123
 * The last argument in the HANDLE_*_RELOPTION macros allows the caller to
 
124
 * determine whether the option was set (true), or its value acquired from
 
125
 * defaults (false); it can be passed as (char *) NULL if the caller does not
 
126
 * need this information.
 
127
 *
 
128
 * optname is the option name (a string), var is the variable
 
129
 * on which the value should be stored (e.g. StdRdOptions->fillfactor), and
 
130
 * option is a relopt_value pointer.
 
131
 *
 
132
 * The normal way to use this is to loop on the relopt_value array returned by
 
133
 * parseRelOptions:
 
134
 * for (i = 0; options[i].gen->name; i++)
 
135
 * {
 
136
 *              if (HAVE_RELOPTION("fillfactor", options[i])
 
137
 *              {
 
138
 *                      HANDLE_INT_RELOPTION("fillfactor", rdopts->fillfactor, options[i], &isset);
 
139
 *                      continue;
 
140
 *              }
 
141
 *              if (HAVE_RELOPTION("default_row_acl", options[i])
 
142
 *              {
 
143
 *                      ...
 
144
 *              }
 
145
 *              ...
 
146
 *              if (validate)
 
147
 *                      ereport(ERROR,
 
148
 *                                      (errmsg("unknown option")));
 
149
 *      }
 
150
 *
 
151
 *      Note that this is more or less the same that fillRelOptions does, so only
 
152
 *      use this if you need to do something non-standard within some option's
 
153
 *      code block.
 
154
 */
 
155
#define HAVE_RELOPTION(optname, option) \
 
156
        (pg_strncasecmp(option.gen->name, optname, option.gen->namelen + 1) == 0)
 
157
 
 
158
#define HANDLE_INT_RELOPTION(optname, var, option, wasset)              \
 
159
        do {                                                                                                            \
 
160
                if (option.isset)                                                                               \
 
161
                        var = option.values.int_val;                                            \
 
162
                else                                                                                                    \
 
163
                        var = ((relopt_int *) option.gen)->default_val;         \
 
164
                (wasset) != NULL ? *(wasset) = option.isset : (dummyret)NULL; \
 
165
        } while (0)
 
166
 
 
167
#define HANDLE_BOOL_RELOPTION(optname, var, option, wasset)                     \
 
168
        do {                                                                                                                    \
 
169
                if (option.isset)                                                                               \
 
170
                        var = option.values.bool_val;                                           \
 
171
                else                                                                                                    \
 
172
                        var = ((relopt_bool *) option.gen)->default_val;        \
 
173
                (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
 
174
        } while (0)
 
175
 
 
176
#define HANDLE_REAL_RELOPTION(optname, var, option, wasset)     \
 
177
        do {                                                                                                            \
 
178
                if (option.isset)                                                                               \
 
179
                        var = option.values.real_val;                                           \
 
180
                else                                                                                                    \
 
181
                        var = ((relopt_real *) option.gen)->default_val;        \
 
182
                (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
 
183
        } while (0)
 
184
 
 
185
/*
 
186
 * Note that this assumes that the variable is already allocated at the tail of
 
187
 * reloptions structure (StdRdOptions or equivalent).
 
188
 *
 
189
 * "base" is a pointer to the reloptions structure, and "offset" is an integer
 
190
 * variable that must be initialized to sizeof(reloptions structure).  This
 
191
 * struct must have been allocated with enough space to hold any string option
 
192
 * present, including terminating \0 for every option.  SET_VARSIZE() must be
 
193
 * called on the struct with this offset as the second argument, after all the
 
194
 * string options have been processed.
 
195
 */
 
196
#define HANDLE_STRING_RELOPTION(optname, var, option, base, offset, wasset)     \
 
197
        do {                                                                                                            \
 
198
                relopt_string *optstring = (relopt_string *) option.gen;\
 
199
                char *string_val;                                                                               \
 
200
                if (option.isset)                                                                               \
 
201
                        string_val = option.values.string_val;                          \
 
202
                else if (!optstring->default_isnull)                                    \
 
203
                        string_val = optstring->default_val;                            \
 
204
                else                                                                                                    \
 
205
                        string_val = NULL;                                                                      \
 
206
                (wasset) != NULL ? *(wasset) = option.isset : (dummyret) NULL; \
 
207
                if (string_val == NULL)                                                                 \
 
208
                        var = 0;                                                                                        \
 
209
                else                                                                                                    \
 
210
                {                                                                                                               \
 
211
                        strcpy(((char *)(base)) + (offset), string_val);        \
 
212
                        var = (offset);                                                                         \
 
213
                        (offset) += strlen(string_val) + 1;                                     \
 
214
                }                                                                                                               \
 
215
        } while (0)
 
216
 
 
217
/*
 
218
 * For use during amoptions: get the strlen of a string option
 
219
 * (either default or the user defined value)
 
220
 */
 
221
#define GET_STRING_RELOPTION_LEN(option) \
 
222
        ((option).isset ? strlen((option).values.string_val) : \
 
223
         ((relopt_string *) (option).gen)->default_len)
 
224
 
 
225
/*
 
226
 * For use by code reading options already parsed: get a pointer to the string
 
227
 * value itself.  "optstruct" is the StdRdOption struct or equivalent, "member"
 
228
 * is the struct member corresponding to the string option
 
229
 */
 
230
#define GET_STRING_RELOPTION(optstruct, member) \
 
231
        ((optstruct)->member == 0 ? NULL : \
 
232
         (char *)(optstruct) + (optstruct)->member)
 
233
 
 
234
 
 
235
extern int add_reloption_kind(void);
 
236
extern void add_bool_reloption(int kind, char *name, char *desc,
 
237
                                   bool default_val);
 
238
extern void add_int_reloption(int kind, char *name, char *desc,
 
239
                                  int default_val, int min_val, int max_val);
 
240
extern void add_real_reloption(int kind, char *name, char *desc,
 
241
                                   double default_val, double min_val, double max_val);
 
242
extern void add_string_reloption(int kind, char *name, char *desc,
 
243
                                         char *default_val, validate_string_relopt validator);
 
244
 
 
245
extern Datum transformRelOptions(Datum oldOptions, List *defList,
 
246
                                        char *namspace, char *validnsps[],
 
247
                                        bool ignoreOids, bool isReset);
 
248
extern List *untransformRelOptions(Datum options);
 
249
extern bytea *extractRelOptions(HeapTuple tuple, TupleDesc tupdesc,
 
250
                                  Oid amoptions);
 
251
extern relopt_value *parseRelOptions(Datum options, bool validate,
 
252
                                relopt_kind kind, int *numrelopts);
 
253
extern void *allocateReloptStruct(Size base, relopt_value *options,
 
254
                                         int numoptions);
 
255
extern void fillRelOptions(void *rdopts, Size basesize,
 
256
                                                   relopt_value *options, int numoptions,
 
257
                                                   bool validate,
 
258
                                                   const relopt_parse_elt *elems, int nelems);
 
259
 
 
260
extern bytea *default_reloptions(Datum reloptions, bool validate,
 
261
                                   relopt_kind kind);
 
262
extern bytea *heap_reloptions(char relkind, Datum reloptions, bool validate);
 
263
extern bytea *index_reloptions(RegProcedure amoptions, Datum reloptions,
 
264
                                bool validate);
 
265
 
 
266
#endif   /* RELOPTIONS_H */