8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.38 2007/01/05 22:19:40 momjian Exp $
11
+ * $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.39 2007/06/01 23:40:18 neilc Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
15
15
16
16
#include "postgres.h"
17
17
18
+ #include <ctype.h>
19
+
18
20
#include "libpq/pqformat.h"
19
21
#include "utils/builtins.h"
20
22
33
35
Datum
34
36
boolin (PG_FUNCTION_ARGS )
35
37
{
36
- char * b = PG_GETARG_CSTRING (0 );
37
-
38
- switch (* b )
38
+ const char * in_str = PG_GETARG_CSTRING (0 );
39
+ const char * str ;
40
+ size_t len ;
41
+
42
+ /*
43
+ * Skip leading and trailing whitespace
44
+ */
45
+ str = in_str ;
46
+ while (isspace ((unsigned char ) * str ))
47
+ str ++ ;
48
+
49
+ len = strlen (str );
50
+ while (len > 0 && isspace ((unsigned char ) str [len - 1 ]))
51
+ len -- ;
52
+
53
+ switch (* str )
39
54
{
40
55
case 't' :
41
56
case 'T' :
42
- if (pg_strncasecmp (b , "true" , strlen ( b ) ) == 0 )
57
+ if (pg_strncasecmp (str , "true" , len ) == 0 )
43
58
PG_RETURN_BOOL (true);
44
59
break ;
45
60
46
61
case 'f' :
47
62
case 'F' :
48
- if (pg_strncasecmp (b , "false" , strlen ( b ) ) == 0 )
63
+ if (pg_strncasecmp (str , "false" , len ) == 0 )
49
64
PG_RETURN_BOOL (false);
50
65
break ;
51
66
52
67
case 'y' :
53
68
case 'Y' :
54
- if (pg_strncasecmp (b , "yes" , strlen ( b ) ) == 0 )
69
+ if (pg_strncasecmp (str , "yes" , len ) == 0 )
55
70
PG_RETURN_BOOL (true);
56
71
break ;
57
72
58
73
case '1' :
59
- if (pg_strncasecmp (b , "1" , strlen ( b ) ) == 0 )
74
+ if (pg_strncasecmp (str , "1" , len ) == 0 )
60
75
PG_RETURN_BOOL (true);
61
76
break ;
62
77
63
78
case 'n' :
64
79
case 'N' :
65
- if (pg_strncasecmp (b , "no" , strlen ( b ) ) == 0 )
80
+ if (pg_strncasecmp (str , "no" , len ) == 0 )
66
81
PG_RETURN_BOOL (false);
67
82
break ;
68
83
69
84
case '0' :
70
- if (pg_strncasecmp (b , "0" , strlen ( b ) ) == 0 )
85
+ if (pg_strncasecmp (str , "0" , len ) == 0 )
71
86
PG_RETURN_BOOL (false);
72
87
break ;
73
88
@@ -77,7 +92,7 @@ boolin(PG_FUNCTION_ARGS)
77
92
78
93
ereport (ERROR ,
79
94
(errcode (ERRCODE_INVALID_TEXT_REPRESENTATION ),
80
- errmsg ("invalid input syntax for type boolean: \"%s\"" , b )));
95
+ errmsg ("invalid input syntax for type boolean: \"%s\"" , in_str )));
81
96
82
97
/* not reached */
83
98
PG_RETURN_BOOL (false);
@@ -127,6 +142,37 @@ boolsend(PG_FUNCTION_ARGS)
127
142
PG_RETURN_BYTEA_P (pq_endtypsend (& buf ));
128
143
}
129
144
145
+ /*
146
+ * textbool - cast function for text => bool
147
+ */
148
+ Datum
149
+ textbool (PG_FUNCTION_ARGS )
150
+ {
151
+ Datum in_text = PG_GETARG_DATUM (0 );
152
+ char * str ;
153
+
154
+ str = DatumGetCString (DirectFunctionCall1 (textout , in_text ));
155
+
156
+ PG_RETURN_DATUM (DirectFunctionCall1 (boolin , CStringGetDatum (str )));
157
+ }
158
+
159
+ /*
160
+ * booltext - cast function for bool => text
161
+ */
162
+ Datum
163
+ booltext (PG_FUNCTION_ARGS )
164
+ {
165
+ bool arg1 = PG_GETARG_BOOL (0 );
166
+ char * str ;
167
+
168
+ if (arg1 )
169
+ str = "true" ;
170
+ else
171
+ str = "false" ;
172
+
173
+ PG_RETURN_DATUM (DirectFunctionCall1 (textin , CStringGetDatum (str )));
174
+ }
175
+
130
176
131
177
/*****************************************************************************
132
178
* PUBLIC ROUTINES *
0 commit comments