88 *
99 *
1010 * 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 $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
1515
1616#include "postgres.h"
1717
18+ #include <ctype.h>
19+
1820#include "libpq/pqformat.h"
1921#include "utils/builtins.h"
2022
3335Datum
3436boolin (PG_FUNCTION_ARGS )
3537{
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 ((unsignedchar )* str ))
47+ str ++ ;
48+
49+ len = strlen (str );
50+ while (len > 0 && isspace ((unsignedchar )str [len - 1 ]))
51+ len -- ;
52+
53+ switch (* str )
3954{
4055case 't' :
4156case 'T' :
42- if (pg_strncasecmp (b ,"true" ,strlen ( b ) )== 0 )
57+ if (pg_strncasecmp (str ,"true" ,len )== 0 )
4358PG_RETURN_BOOL (true);
4459break ;
4560
4661case 'f' :
4762case 'F' :
48- if (pg_strncasecmp (b ,"false" ,strlen ( b ) )== 0 )
63+ if (pg_strncasecmp (str ,"false" ,len )== 0 )
4964PG_RETURN_BOOL (false);
5065break ;
5166
5267case 'y' :
5368case 'Y' :
54- if (pg_strncasecmp (b ,"yes" ,strlen ( b ) )== 0 )
69+ if (pg_strncasecmp (str ,"yes" ,len )== 0 )
5570PG_RETURN_BOOL (true);
5671break ;
5772
5873case '1' :
59- if (pg_strncasecmp (b ,"1" ,strlen ( b ) )== 0 )
74+ if (pg_strncasecmp (str ,"1" ,len )== 0 )
6075PG_RETURN_BOOL (true);
6176break ;
6277
6378case 'n' :
6479case 'N' :
65- if (pg_strncasecmp (b ,"no" ,strlen ( b ) )== 0 )
80+ if (pg_strncasecmp (str ,"no" ,len )== 0 )
6681PG_RETURN_BOOL (false);
6782break ;
6883
6984case '0' :
70- if (pg_strncasecmp (b ,"0" ,strlen ( b ) )== 0 )
85+ if (pg_strncasecmp (str ,"0" ,len )== 0 )
7186PG_RETURN_BOOL (false);
7287break ;
7388
@@ -77,7 +92,7 @@ boolin(PG_FUNCTION_ARGS)
7792
7893ereport (ERROR ,
7994(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 )));
8196
8297/* not reached */
8398PG_RETURN_BOOL (false);
@@ -127,6 +142,37 @@ boolsend(PG_FUNCTION_ARGS)
127142PG_RETURN_BYTEA_P (pq_endtypsend (& buf ));
128143}
129144
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+
130176
131177/*****************************************************************************
132178 * PUBLIC ROUTINES *