~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

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

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * quote.c
 
4
 *        Functions for quoting identifiers and literals
 
5
 *
 
6
 * Portions Copyright (c) 2000-2005, PostgreSQL Global Development Group
 
7
 *
 
8
 *
 
9
 * IDENTIFICATION
 
10
 *        $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.14.4.1 2005-03-21 16:29:31 tgl Exp $
 
11
 *
 
12
 *-------------------------------------------------------------------------
 
13
 */
 
14
#include "postgres.h"
 
15
 
 
16
#include "utils/builtins.h"
 
17
 
 
18
 
 
19
/*
 
20
 * quote_ident -
 
21
 *        returns a properly quoted identifier
 
22
 */
 
23
Datum
 
24
quote_ident(PG_FUNCTION_ARGS)
 
25
{
 
26
        text       *t = PG_GETARG_TEXT_P(0);
 
27
        text       *result;
 
28
        const char *qstr;
 
29
        char       *str;
 
30
        int                     len;
 
31
 
 
32
        /* We have to convert to a C string to use quote_identifier */
 
33
        len = VARSIZE(t) - VARHDRSZ;
 
34
        str = (char *) palloc(len + 1);
 
35
        memcpy(str, VARDATA(t), len);
 
36
        str[len] = '\0';
 
37
 
 
38
        qstr = quote_identifier(str);
 
39
 
 
40
        len = strlen(qstr);
 
41
        result = (text *) palloc(len + VARHDRSZ);
 
42
        VARATT_SIZEP(result) = len + VARHDRSZ;
 
43
        memcpy(VARDATA(result), qstr, len);
 
44
 
 
45
        PG_RETURN_TEXT_P(result);
 
46
}
 
47
 
 
48
/*
 
49
 * quote_literal -
 
50
 *        returns a properly quoted literal
 
51
 */
 
52
Datum
 
53
quote_literal(PG_FUNCTION_ARGS)
 
54
{
 
55
        text       *t = PG_GETARG_TEXT_P(0);
 
56
        text       *result;
 
57
        char       *cp1;
 
58
        char       *cp2;
 
59
        int                     len;
 
60
 
 
61
        len = VARSIZE(t) - VARHDRSZ;
 
62
        /* We make a worst-case result area; wasting a little space is OK */
 
63
        result = (text *) palloc(len * 2 + 2 + VARHDRSZ);
 
64
 
 
65
        cp1 = VARDATA(t);
 
66
        cp2 = VARDATA(result);
 
67
 
 
68
        *cp2++ = '\'';
 
69
        while (len-- > 0)
 
70
        {
 
71
                if (*cp1 == '\'')
 
72
                        *cp2++ = '\'';
 
73
                else if (*cp1 == '\\')
 
74
                        *cp2++ = '\\';
 
75
 
 
76
                *cp2++ = *cp1++;
 
77
        }
 
78
        *cp2++ = '\'';
 
79
 
 
80
        VARATT_SIZEP(result) = cp2 - ((char *) result);
 
81
 
 
82
        PG_RETURN_TEXT_P(result);
 
83
}