Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 05a7db0

Browse files
committed
Accept 'on' and 'off' as input for boolean data type, unifying the syntax
that the data type and GUC accepts. ITAGAKI Takahiro
1 parent fd497ab commit 05a7db0

File tree

7 files changed

+149
-115
lines changed

7 files changed

+149
-115
lines changed

doc/src/sgml/datatype.sgml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.235 2008/12/19 01:34:19 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.236 2009/03/09 14:34:34 petere Exp $ -->
22

33
<chapter id="datatype">
44
<title id="datatype-title">Data Types</title>
@@ -2686,6 +2686,7 @@ P <optional> <replaceable>years</>-<replaceable>months</>-<replaceable>days</> <
26862686
<member><literal>'true'</literal></member>
26872687
<member><literal>'y'</literal></member>
26882688
<member><literal>'yes'</literal></member>
2689+
<member><literal>'on'</literal></member>
26892690
<member><literal>'1'</literal></member>
26902691
</simplelist>
26912692
For the <quote>false</quote> state, the following values can be
@@ -2696,6 +2697,7 @@ P <optional> <replaceable>years</>-<replaceable>months</>-<replaceable>days</> <
26962697
<member><literal>'false'</literal></member>
26972698
<member><literal>'n'</literal></member>
26982699
<member><literal>'no'</literal></member>
2700+
<member><literal>'off'</literal></member>
26992701
<member><literal>'0'</literal></member>
27002702
</simplelist>
27012703
Leading and trailing whitespace is ignored. Using the key words

src/backend/utils/adt/bool.c

Lines changed: 99 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.45 2009/01/01 17:23:49 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.46 2009/03/09 14:34:34 petere Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -20,15 +20,108 @@
2020
#include "libpq/pqformat.h"
2121
#include "utils/builtins.h"
2222

23+
/*
24+
* Try to interpret value as boolean value. Valid values are: true,
25+
* false, yes, no, on, off, 1, 0; as well as unique prefixes thereof.
26+
* If the string parses okay, return true, else false.
27+
* If okay and result is not NULL, return the value in *result.
28+
*/
29+
bool
30+
parse_bool(const char *value, bool *result)
31+
{
32+
return parse_bool_with_len(value, strlen(value), result);
33+
}
34+
35+
bool
36+
parse_bool_with_len(const char *value, size_t len, bool *result)
37+
{
38+
switch (*value)
39+
{
40+
case 't':
41+
case 'T':
42+
if (pg_strncasecmp(value, "true", len) == 0)
43+
{
44+
if (result)
45+
*result = true;
46+
return true;
47+
}
48+
break;
49+
case 'f':
50+
case 'F':
51+
if (pg_strncasecmp(value, "false", len) == 0)
52+
{
53+
if (result)
54+
*result = false;
55+
return true;
56+
}
57+
break;
58+
case 'y':
59+
case 'Y':
60+
if (pg_strncasecmp(value, "yes", len) == 0)
61+
{
62+
if (result)
63+
*result = true;
64+
return true;
65+
}
66+
break;
67+
case 'n':
68+
case 'N':
69+
if (pg_strncasecmp(value, "no", len) == 0)
70+
{
71+
if (result)
72+
*result = false;
73+
return true;
74+
}
75+
break;
76+
case 'o':
77+
case 'O':
78+
/* 'o' is not unique enough */
79+
if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
80+
{
81+
if (result)
82+
*result = true;
83+
return true;
84+
}
85+
else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
86+
{
87+
if (result)
88+
*result = false;
89+
return true;
90+
}
91+
break;
92+
case '1':
93+
if (len == 1)
94+
{
95+
if (result)
96+
*result = true;
97+
return true;
98+
}
99+
break;
100+
case '0':
101+
if (len == 1)
102+
{
103+
if (result)
104+
*result = false;
105+
return true;
106+
}
107+
break;
108+
default:
109+
break;
110+
}
111+
112+
*result = false; /* suppress compiler warning */
113+
return false;
114+
}
115+
23116
/*****************************************************************************
24117
* USER I/O ROUTINES *
25118
*****************************************************************************/
26119

27120
/*
28121
* boolin - converts "t" or "f" to 1 or 0
29122
*
30-
* Check explicitly for "true/false" and TRUE/FALSE, 1/0, YES/NO.
31-
* Reject other values. - thomas 1997-10-05
123+
* Check explicitly for "true/false" and TRUE/FALSE, 1/0, YES/NO, ON/OFF.
124+
* Reject other values.
32125
*
33126
* In the switch statement, check the most-used possibilities first.
34127
*/
@@ -38,6 +131,7 @@ boolin(PG_FUNCTION_ARGS)
38131
const char *in_str = PG_GETARG_CSTRING(0);
39132
const char *str;
40133
size_t len;
134+
bool result;
41135

42136
/*
43137
* Skip leading and trailing whitespace
@@ -50,45 +144,8 @@ boolin(PG_FUNCTION_ARGS)
50144
while (len > 0 && isspace((unsigned char) str[len - 1]))
51145
len--;
52146

53-
switch (*str)
54-
{
55-
case 't':
56-
case 'T':
57-
if (pg_strncasecmp(str, "true", len) == 0)
58-
PG_RETURN_BOOL(true);
59-
break;
60-
61-
case 'f':
62-
case 'F':
63-
if (pg_strncasecmp(str, "false", len) == 0)
64-
PG_RETURN_BOOL(false);
65-
break;
66-
67-
case 'y':
68-
case 'Y':
69-
if (pg_strncasecmp(str, "yes", len) == 0)
70-
PG_RETURN_BOOL(true);
71-
break;
72-
73-
case '1':
74-
if (pg_strncasecmp(str, "1", len) == 0)
75-
PG_RETURN_BOOL(true);
76-
break;
77-
78-
case 'n':
79-
case 'N':
80-
if (pg_strncasecmp(str, "no", len) == 0)
81-
PG_RETURN_BOOL(false);
82-
break;
83-
84-
case '0':
85-
if (pg_strncasecmp(str, "0", len) == 0)
86-
PG_RETURN_BOOL(false);
87-
break;
88-
89-
default:
90-
break;
91-
}
147+
if (parse_bool_with_len(str, len, &result))
148+
PG_RETURN_BOOL(result);
92149

93150
ereport(ERROR,
94151
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),

src/backend/utils/misc/guc.c

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Written by Peter Eisentraut <[email protected]>.
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.496 2009/02/28 00:10:51 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.497 2009/03/09 14:34:34 petere Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -4086,74 +4086,6 @@ ReportGUCOption(struct config_generic * record)
40864086
}
40874087
}
40884088

4089-
4090-
/*
4091-
* Try to interpret value as boolean value. Valid values are: true,
4092-
* false, yes, no, on, off, 1, 0; as well as unique prefixes thereof.
4093-
* If the string parses okay, return true, else false.
4094-
* If okay and result is not NULL, return the value in *result.
4095-
*/
4096-
bool
4097-
parse_bool(const char *value, bool *result)
4098-
{
4099-
size_t len = strlen(value);
4100-
4101-
if (pg_strncasecmp(value, "true", len) == 0)
4102-
{
4103-
if (result)
4104-
*result = true;
4105-
}
4106-
else if (pg_strncasecmp(value, "false", len) == 0)
4107-
{
4108-
if (result)
4109-
*result = false;
4110-
}
4111-
4112-
else if (pg_strncasecmp(value, "yes", len) == 0)
4113-
{
4114-
if (result)
4115-
*result = true;
4116-
}
4117-
else if (pg_strncasecmp(value, "no", len) == 0)
4118-
{
4119-
if (result)
4120-
*result = false;
4121-
}
4122-
4123-
/* 'o' is not unique enough */
4124-
else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
4125-
{
4126-
if (result)
4127-
*result = true;
4128-
}
4129-
else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
4130-
{
4131-
if (result)
4132-
*result = false;
4133-
}
4134-
4135-
else if (pg_strcasecmp(value, "1") == 0)
4136-
{
4137-
if (result)
4138-
*result = true;
4139-
}
4140-
else if (pg_strcasecmp(value, "0") == 0)
4141-
{
4142-
if (result)
4143-
*result = false;
4144-
}
4145-
4146-
else
4147-
{
4148-
if (result)
4149-
*result = false; /* suppress compiler warning */
4150-
return false;
4151-
}
4152-
return true;
4153-
}
4154-
4155-
4156-
41574089
/*
41584090
* Try to parse value as an integer. The accepted formats are the
41594091
* usual decimal, octal, or hexadecimal formats, optionally followed by

src/include/utils/builtins.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.331 2009/02/06 21:15:12 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.332 2009/03/09 14:34:34 petere Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -109,6 +109,8 @@ extern Datum boolle(PG_FUNCTION_ARGS);
109109
extern Datum boolge(PG_FUNCTION_ARGS);
110110
extern Datum booland_statefunc(PG_FUNCTION_ARGS);
111111
extern Datum boolor_statefunc(PG_FUNCTION_ARGS);
112+
extern bool parse_bool(const char *value, bool *result);
113+
extern bool parse_bool_with_len(const char *value, size_t len, bool *result);
112114

113115
/* char.c */
114116
extern Datum charin(PG_FUNCTION_ARGS);

src/include/utils/guc.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Copyright (c) 2000-2009, PostgreSQL Global Development Group
88
* Written by Peter Eisentraut <[email protected]>.
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/guc.h,v 1.100 2009/01/01 17:24:02 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/guc.h,v 1.101 2009/03/09 14:34:35 petere Exp $
1111
*--------------------------------------------------------------------
1212
*/
1313
#ifndef GUC_H
@@ -257,7 +257,6 @@ extern int NewGUCNestLevel(void);
257257
extern void AtEOXact_GUC(bool isCommit, int nestLevel);
258258
extern void BeginReportingGUCOptions(void);
259259
extern void ParseLongOption(const char *string, char **name, char **value);
260-
extern bool parse_bool(const char *value, bool *result);
261260
extern bool parse_int(const char *value, int *result, int flags,
262261
const char **hintmsg);
263262
extern bool parse_real(const char *value, double *result);

src/test/regress/expected/boolean.out

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,36 @@ SELECT bool 'nay' AS error;
8888
ERROR: invalid input syntax for type boolean: "nay"
8989
LINE 1: SELECT bool 'nay' AS error;
9090
^
91+
SELECT bool 'on' AS true;
92+
true
93+
------
94+
t
95+
(1 row)
96+
97+
SELECT bool 'off' AS false;
98+
false
99+
-------
100+
f
101+
(1 row)
102+
103+
SELECT bool 'of' AS false;
104+
false
105+
-------
106+
f
107+
(1 row)
108+
109+
SELECT bool 'o' AS error;
110+
ERROR: invalid input syntax for type boolean: "o"
111+
LINE 1: SELECT bool 'o' AS error;
112+
^
113+
SELECT bool 'on_' AS error;
114+
ERROR: invalid input syntax for type boolean: "on_"
115+
LINE 1: SELECT bool 'on_' AS error;
116+
^
117+
SELECT bool 'off_' AS error;
118+
ERROR: invalid input syntax for type boolean: "off_"
119+
LINE 1: SELECT bool 'off_' AS error;
120+
^
91121
SELECT bool '1' AS true;
92122
true
93123
------

src/test/regress/sql/boolean.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ SELECT bool 'no' AS false;
4040

4141
SELECT bool 'nay' AS error;
4242

43+
SELECT bool 'on' AS true;
44+
45+
SELECT bool 'off' AS false;
46+
47+
SELECT bool 'of' AS false;
48+
49+
SELECT bool 'o' AS error;
50+
51+
SELECT bool 'on_' AS error;
52+
53+
SELECT bool 'off_' AS error;
54+
4355
SELECT bool '1' AS true;
4456

4557
SELECT bool '11' AS error;

0 commit comments

Comments
 (0)