~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to src/backend/utils/adt/format_type.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * format_type.c
 
4
 *        Display type names "nicely".
 
5
 *
 
6
 *
 
7
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
 
8
 * Portions Copyright (c) 1994, Regents of the University of California
 
9
 *
 
10
 * IDENTIFICATION
 
11
 *        src/backend/utils/adt/format_type.c
 
12
 *
 
13
 *-------------------------------------------------------------------------
 
14
 */
 
15
 
 
16
#include "postgres.h"
 
17
 
 
18
#include <ctype.h>
 
19
 
 
20
#include "catalog/namespace.h"
 
21
#include "catalog/pg_type.h"
 
22
#include "utils/builtins.h"
 
23
#include "utils/lsyscache.h"
 
24
#include "utils/numeric.h"
 
25
#include "utils/syscache.h"
 
26
#include "mb/pg_wchar.h"
 
27
 
 
28
#define MAX_INT32_LEN 11
 
29
 
 
30
static char *format_type_internal(Oid type_oid, int32 typemod,
 
31
                                         bool typemod_given, bool allow_invalid);
 
32
static char *printTypmod(const char *typname, int32 typmod, Oid typmodout);
 
33
static char *
 
34
psnprintf(size_t len, const char *fmt,...)
 
35
/* This lets gcc check the format string for consistency. */
 
36
__attribute__((format(printf, 2, 3)));
 
37
 
 
38
 
 
39
/*
 
40
 * SQL function: format_type(type_oid, typemod)
 
41
 *
 
42
 * `type_oid' is from pg_type.oid, `typemod' is from
 
43
 * pg_attribute.atttypmod. This function will get the type name and
 
44
 * format it and the modifier to canonical SQL format, if the type is
 
45
 * a standard type. Otherwise you just get pg_type.typname back,
 
46
 * double quoted if it contains funny characters or matches a keyword.
 
47
 *
 
48
 * If typemod is NULL then we are formatting a type name in a context where
 
49
 * no typemod is available, eg a function argument or result type.      This
 
50
 * yields a slightly different result from specifying typemod = -1 in some
 
51
 * cases.  Given typemod = -1 we feel compelled to produce an output that
 
52
 * the parser will interpret as having typemod -1, so that pg_dump will
 
53
 * produce CREATE TABLE commands that recreate the original state.      But
 
54
 * given NULL typemod, we assume that the parser's interpretation of
 
55
 * typemod doesn't matter, and so we are willing to output a slightly
 
56
 * "prettier" representation of the same type.  For example, type = bpchar
 
57
 * and typemod = NULL gets you "character", whereas typemod = -1 gets you
 
58
 * "bpchar" --- the former will be interpreted as character(1) by the
 
59
 * parser, which does not yield typemod -1.
 
60
 *
 
61
 * XXX encoding a meaning in typemod = NULL is ugly; it'd have been
 
62
 * cleaner to make two functions of one and two arguments respectively.
 
63
 * Not worth changing it now, however.
 
64
 */
 
65
Datum
 
66
format_type(PG_FUNCTION_ARGS)
 
67
{
 
68
        Oid                     type_oid;
 
69
        int32           typemod;
 
70
        char       *result;
 
71
 
 
72
        /* Since this function is not strict, we must test for null args */
 
73
        if (PG_ARGISNULL(0))
 
74
                PG_RETURN_NULL();
 
75
 
 
76
        type_oid = PG_GETARG_OID(0);
 
77
 
 
78
        if (PG_ARGISNULL(1))
 
79
                result = format_type_internal(type_oid, -1, false, true);
 
80
        else
 
81
        {
 
82
                typemod = PG_GETARG_INT32(1);
 
83
                result = format_type_internal(type_oid, typemod, true, true);
 
84
        }
 
85
 
 
86
        PG_RETURN_TEXT_P(cstring_to_text(result));
 
87
}
 
88
 
 
89
/*
 
90
 * This version is for use within the backend in error messages, etc.
 
91
 * One difference is that it will fail for an invalid type.
 
92
 *
 
93
 * The result is always a palloc'd string.
 
94
 */
 
95
char *
 
96
format_type_be(Oid type_oid)
 
97
{
 
98
        return format_type_internal(type_oid, -1, false, false);
 
99
}
 
100
 
 
101
/*
 
102
 * This version allows a nondefault typemod to be specified.
 
103
 */
 
104
char *
 
105
format_type_with_typemod(Oid type_oid, int32 typemod)
 
106
{
 
107
        return format_type_internal(type_oid, typemod, true, false);
 
108
}
 
109
 
 
110
 
 
111
 
 
112
static char *
 
113
format_type_internal(Oid type_oid, int32 typemod,
 
114
                                         bool typemod_given, bool allow_invalid)
 
115
{
 
116
        bool            with_typemod = typemod_given && (typemod >= 0);
 
117
        HeapTuple       tuple;
 
118
        Form_pg_type typeform;
 
119
        Oid                     array_base_type;
 
120
        bool            is_array;
 
121
        char       *buf;
 
122
 
 
123
        if (type_oid == InvalidOid && allow_invalid)
 
124
                return pstrdup("-");
 
125
 
 
126
        tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
 
127
        if (!HeapTupleIsValid(tuple))
 
128
        {
 
129
                if (allow_invalid)
 
130
                        return pstrdup("???");
 
131
                else
 
132
                        elog(ERROR, "cache lookup failed for type %u", type_oid);
 
133
        }
 
134
        typeform = (Form_pg_type) GETSTRUCT(tuple);
 
135
 
 
136
        /*
 
137
         * Check if it's a regular (variable length) array type.  Fixed-length
 
138
         * array types such as "name" shouldn't get deconstructed.  As of Postgres
 
139
         * 8.1, rather than checking typlen we check the toast property, and don't
 
140
         * deconstruct "plain storage" array types --- this is because we don't
 
141
         * want to show oidvector as oid[].
 
142
         */
 
143
        array_base_type = typeform->typelem;
 
144
 
 
145
        if (array_base_type != InvalidOid &&
 
146
                typeform->typstorage != 'p')
 
147
        {
 
148
                /* Switch our attention to the array element type */
 
149
                ReleaseSysCache(tuple);
 
150
                tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(array_base_type));
 
151
                if (!HeapTupleIsValid(tuple))
 
152
                {
 
153
                        if (allow_invalid)
 
154
                                return pstrdup("???[]");
 
155
                        else
 
156
                                elog(ERROR, "cache lookup failed for type %u", type_oid);
 
157
                }
 
158
                typeform = (Form_pg_type) GETSTRUCT(tuple);
 
159
                type_oid = array_base_type;
 
160
                is_array = true;
 
161
        }
 
162
        else
 
163
                is_array = false;
 
164
 
 
165
        /*
 
166
         * See if we want to special-case the output for certain built-in types.
 
167
         * Note that these special cases should all correspond to special
 
168
         * productions in gram.y, to ensure that the type name will be taken as a
 
169
         * system type, not a user type of the same name.
 
170
         *
 
171
         * If we do not provide a special-case output here, the type name will be
 
172
         * handled the same way as a user type name --- in particular, it will be
 
173
         * double-quoted if it matches any lexer keyword.  This behavior is
 
174
         * essential for some cases, such as types "bit" and "char".
 
175
         */
 
176
        buf = NULL;                                     /* flag for no special case */
 
177
 
 
178
        switch (type_oid)
 
179
        {
 
180
                case BITOID:
 
181
                        if (with_typemod)
 
182
                                buf = printTypmod("bit", typemod, typeform->typmodout);
 
183
                        else if (typemod_given)
 
184
                        {
 
185
                                /*
 
186
                                 * bit with typmod -1 is not the same as BIT, which means
 
187
                                 * BIT(1) per SQL spec.  Report it as the quoted typename so
 
188
                                 * that parser will not assign a bogus typmod.
 
189
                                 */
 
190
                        }
 
191
                        else
 
192
                                buf = pstrdup("bit");
 
193
                        break;
 
194
 
 
195
                case BOOLOID:
 
196
                        buf = pstrdup("boolean");
 
197
                        break;
 
198
 
 
199
                case BPCHAROID:
 
200
                        if (with_typemod)
 
201
                                buf = printTypmod("character", typemod, typeform->typmodout);
 
202
                        else if (typemod_given)
 
203
                        {
 
204
                                /*
 
205
                                 * bpchar with typmod -1 is not the same as CHARACTER, which
 
206
                                 * means CHARACTER(1) per SQL spec.  Report it as bpchar so
 
207
                                 * that parser will not assign a bogus typmod.
 
208
                                 */
 
209
                        }
 
210
                        else
 
211
                                buf = pstrdup("character");
 
212
                        break;
 
213
 
 
214
                case FLOAT4OID:
 
215
                        buf = pstrdup("real");
 
216
                        break;
 
217
 
 
218
                case FLOAT8OID:
 
219
                        buf = pstrdup("double precision");
 
220
                        break;
 
221
 
 
222
                case INT2OID:
 
223
                        buf = pstrdup("smallint");
 
224
                        break;
 
225
 
 
226
                case INT4OID:
 
227
                        buf = pstrdup("integer");
 
228
                        break;
 
229
 
 
230
                case INT8OID:
 
231
                        buf = pstrdup("bigint");
 
232
                        break;
 
233
 
 
234
                case NUMERICOID:
 
235
                        if (with_typemod)
 
236
                                buf = printTypmod("numeric", typemod, typeform->typmodout);
 
237
                        else
 
238
                                buf = pstrdup("numeric");
 
239
                        break;
 
240
 
 
241
                case INTERVALOID:
 
242
                        if (with_typemod)
 
243
                                buf = printTypmod("interval", typemod, typeform->typmodout);
 
244
                        else
 
245
                                buf = pstrdup("interval");
 
246
                        break;
 
247
 
 
248
                case TIMEOID:
 
249
                        if (with_typemod)
 
250
                                buf = printTypmod("time", typemod, typeform->typmodout);
 
251
                        else
 
252
                                buf = pstrdup("time without time zone");
 
253
                        break;
 
254
 
 
255
                case TIMETZOID:
 
256
                        if (with_typemod)
 
257
                                buf = printTypmod("time", typemod, typeform->typmodout);
 
258
                        else
 
259
                                buf = pstrdup("time with time zone");
 
260
                        break;
 
261
 
 
262
                case TIMESTAMPOID:
 
263
                        if (with_typemod)
 
264
                                buf = printTypmod("timestamp", typemod, typeform->typmodout);
 
265
                        else
 
266
                                buf = pstrdup("timestamp without time zone");
 
267
                        break;
 
268
 
 
269
                case TIMESTAMPTZOID:
 
270
                        if (with_typemod)
 
271
                                buf = printTypmod("timestamp", typemod, typeform->typmodout);
 
272
                        else
 
273
                                buf = pstrdup("timestamp with time zone");
 
274
                        break;
 
275
 
 
276
                case VARBITOID:
 
277
                        if (with_typemod)
 
278
                                buf = printTypmod("bit varying", typemod, typeform->typmodout);
 
279
                        else
 
280
                                buf = pstrdup("bit varying");
 
281
                        break;
 
282
 
 
283
                case VARCHAROID:
 
284
                        if (with_typemod)
 
285
                                buf = printTypmod("character varying", typemod, typeform->typmodout);
 
286
                        else
 
287
                                buf = pstrdup("character varying");
 
288
                        break;
 
289
        }
 
290
 
 
291
        if (buf == NULL)
 
292
        {
 
293
                /*
 
294
                 * Default handling: report the name as it appears in the catalog.
 
295
                 * Here, we must qualify the name if it is not visible in the search
 
296
                 * path, and we must double-quote it if it's not a standard identifier
 
297
                 * or if it matches any keyword.
 
298
                 */
 
299
                char       *nspname;
 
300
                char       *typname;
 
301
 
 
302
                if (TypeIsVisible(type_oid))
 
303
                        nspname = NULL;
 
304
                else
 
305
                        nspname = get_namespace_name(typeform->typnamespace);
 
306
 
 
307
                typname = NameStr(typeform->typname);
 
308
 
 
309
                buf = quote_qualified_identifier(nspname, typname);
 
310
 
 
311
                if (with_typemod)
 
312
                        buf = printTypmod(buf, typemod, typeform->typmodout);
 
313
        }
 
314
 
 
315
        if (is_array)
 
316
                buf = psnprintf(strlen(buf) + 3, "%s[]", buf);
 
317
 
 
318
        ReleaseSysCache(tuple);
 
319
 
 
320
        return buf;
 
321
}
 
322
 
 
323
 
 
324
/*
 
325
 * Add typmod decoration to the basic type name
 
326
 */
 
327
static char *
 
328
printTypmod(const char *typname, int32 typmod, Oid typmodout)
 
329
{
 
330
        char       *res;
 
331
 
 
332
        /* Shouldn't be called if typmod is -1 */
 
333
        Assert(typmod >= 0);
 
334
 
 
335
        if (typmodout == InvalidOid)
 
336
        {
 
337
                /* Default behavior: just print the integer typmod with parens */
 
338
                res = psnprintf(strlen(typname) + MAX_INT32_LEN + 3, "%s(%d)",
 
339
                                                typname, (int) typmod);
 
340
        }
 
341
        else
 
342
        {
 
343
                /* Use the type-specific typmodout procedure */
 
344
                char       *tmstr;
 
345
 
 
346
                tmstr = DatumGetCString(OidFunctionCall1(typmodout,
 
347
                                                                                                 Int32GetDatum(typmod)));
 
348
                res = psnprintf(strlen(typname) + strlen(tmstr) + 1, "%s%s",
 
349
                                                typname, tmstr);
 
350
        }
 
351
 
 
352
        return res;
 
353
}
 
354
 
 
355
 
 
356
/*
 
357
 * type_maximum_size --- determine maximum width of a variable-width column
 
358
 *
 
359
 * If the max width is indeterminate, return -1.  In particular, we return
 
360
 * -1 for any type not known to this routine.  We assume the caller has
 
361
 * already determined that the type is a variable-width type, so it's not
 
362
 * necessary to look up the type's pg_type tuple here.
 
363
 *
 
364
 * This may appear unrelated to format_type(), but in fact the two routines
 
365
 * share knowledge of the encoding of typmod for different types, so it's
 
366
 * convenient to keep them together.  (XXX now that most of this knowledge
 
367
 * has been pushed out of format_type into the typmodout functions, it's
 
368
 * interesting to wonder if it's worth trying to factor this code too...)
 
369
 */
 
370
int32
 
371
type_maximum_size(Oid type_oid, int32 typemod)
 
372
{
 
373
        if (typemod < 0)
 
374
                return -1;
 
375
 
 
376
        switch (type_oid)
 
377
        {
 
378
                case BPCHAROID:
 
379
                case VARCHAROID:
 
380
                        /* typemod includes varlena header */
 
381
 
 
382
                        /* typemod is in characters not bytes */
 
383
                        return (typemod - VARHDRSZ) *
 
384
                                pg_encoding_max_length(GetDatabaseEncoding())
 
385
                                + VARHDRSZ;
 
386
 
 
387
                case NUMERICOID:
 
388
                        return numeric_maximum_size(typemod);
 
389
 
 
390
                case VARBITOID:
 
391
                case BITOID:
 
392
                        /* typemod is the (max) number of bits */
 
393
                        return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE
 
394
                                + 2 * sizeof(int32);
 
395
        }
 
396
 
 
397
        /* Unknown type, or unlimited-width type such as 'text' */
 
398
        return -1;
 
399
}
 
400
 
 
401
 
 
402
/*
 
403
 * oidvectortypes                       - converts a vector of type OIDs to "typname" list
 
404
 */
 
405
Datum
 
406
oidvectortypes(PG_FUNCTION_ARGS)
 
407
{
 
408
        oidvector  *oidArray = (oidvector *) PG_GETARG_POINTER(0);
 
409
        char       *result;
 
410
        int                     numargs = oidArray->dim1;
 
411
        int                     num;
 
412
        size_t          total;
 
413
        size_t          left;
 
414
 
 
415
        total = 20 * numargs + 1;
 
416
        result = palloc(total);
 
417
        result[0] = '\0';
 
418
        left = total - 1;
 
419
 
 
420
        for (num = 0; num < numargs; num++)
 
421
        {
 
422
                char       *typename = format_type_internal(oidArray->values[num], -1,
 
423
                                                                                                        false, true);
 
424
                size_t          slen = strlen(typename);
 
425
 
 
426
                if (left < (slen + 2))
 
427
                {
 
428
                        total += slen + 2;
 
429
                        result = repalloc(result, total);
 
430
                        left += slen + 2;
 
431
                }
 
432
 
 
433
                if (num > 0)
 
434
                {
 
435
                        strcat(result, ", ");
 
436
                        left -= 2;
 
437
                }
 
438
                strcat(result, typename);
 
439
                left -= slen;
 
440
        }
 
441
 
 
442
        PG_RETURN_TEXT_P(cstring_to_text(result));
 
443
}
 
444
 
 
445
 
 
446
/* snprintf into a palloc'd string */
 
447
static char *
 
448
psnprintf(size_t len, const char *fmt,...)
 
449
{
 
450
        va_list         ap;
 
451
        char       *buf;
 
452
 
 
453
        buf = palloc(len);
 
454
 
 
455
        va_start(ap, fmt);
 
456
        vsnprintf(buf, len, fmt, ap);
 
457
        va_end(ap);
 
458
 
 
459
        return buf;
 
460
}