@@ -122,9 +122,13 @@ anychar_typmodout(int32 typmod)
122
122
*
123
123
* If the input string is too long, raise an error, unless the extra
124
124
* characters are spaces, in which case they're truncated. (per SQL)
125
+ *
126
+ * If escontext points to an ErrorSaveContext node, that is filled instead
127
+ * of throwing an error; the caller must check SOFT_ERROR_OCCURRED()
128
+ * to detect errors.
125
129
*/
126
130
static BpChar *
127
- bpchar_input (const char * s , size_t len , int32 atttypmod )
131
+ bpchar_input (const char * s , size_t len , int32 atttypmod , Node * escontext )
128
132
{
129
133
BpChar * result ;
130
134
char * r ;
@@ -153,7 +157,7 @@ bpchar_input(const char *s, size_t len, int32 atttypmod)
153
157
for (j = mbmaxlen ; j < len ; j ++ )
154
158
{
155
159
if (s [j ] != ' ' )
156
- ereport ( ERROR ,
160
+ ereturn ( escontext , NULL ,
157
161
(errcode (ERRCODE_STRING_DATA_RIGHT_TRUNCATION ),
158
162
errmsg ("value too long for type character(%d)" ,
159
163
(int ) maxlen )));
@@ -195,14 +199,13 @@ Datum
195
199
bpcharin (PG_FUNCTION_ARGS )
196
200
{
197
201
char * s = PG_GETARG_CSTRING (0 );
198
-
199
202
#ifdef NOT_USED
200
203
Oid typelem = PG_GETARG_OID (1 );
201
204
#endif
202
205
int32 atttypmod = PG_GETARG_INT32 (2 );
203
206
BpChar * result ;
204
207
205
- result = bpchar_input (s , strlen (s ), atttypmod );
208
+ result = bpchar_input (s , strlen (s ), atttypmod , fcinfo -> context );
206
209
PG_RETURN_BPCHAR_P (result );
207
210
}
208
211
@@ -228,7 +231,6 @@ Datum
228
231
bpcharrecv (PG_FUNCTION_ARGS )
229
232
{
230
233
StringInfo buf = (StringInfo ) PG_GETARG_POINTER (0 );
231
-
232
234
#ifdef NOT_USED
233
235
Oid typelem = PG_GETARG_OID (1 );
234
236
#endif
@@ -238,7 +240,7 @@ bpcharrecv(PG_FUNCTION_ARGS)
238
240
int nbytes ;
239
241
240
242
str = pq_getmsgtext (buf , buf -> len - buf -> cursor , & nbytes );
241
- result = bpchar_input (str , nbytes , atttypmod );
243
+ result = bpchar_input (str , nbytes , atttypmod , NULL );
242
244
pfree (str );
243
245
PG_RETURN_BPCHAR_P (result );
244
246
}
@@ -448,11 +450,12 @@ bpchartypmodout(PG_FUNCTION_ARGS)
448
450
* If the input string is too long, raise an error, unless the extra
449
451
* characters are spaces, in which case they're truncated. (per SQL)
450
452
*
451
- * Uses the C string to text conversion function, which is only appropriate
452
- * if VarChar and text are equivalent types.
453
+ * If escontext points to an ErrorSaveContext node, that is filled instead
454
+ * of throwing an error; the caller must check SOFT_ERROR_OCCURRED()
455
+ * to detect errors.
453
456
*/
454
457
static VarChar *
455
- varchar_input (const char * s , size_t len , int32 atttypmod )
458
+ varchar_input (const char * s , size_t len , int32 atttypmod , Node * escontext )
456
459
{
457
460
VarChar * result ;
458
461
size_t maxlen ;
@@ -468,7 +471,7 @@ varchar_input(const char *s, size_t len, int32 atttypmod)
468
471
for (j = mbmaxlen ; j < len ; j ++ )
469
472
{
470
473
if (s [j ] != ' ' )
471
- ereport ( ERROR ,
474
+ ereturn ( escontext , NULL ,
472
475
(errcode (ERRCODE_STRING_DATA_RIGHT_TRUNCATION ),
473
476
errmsg ("value too long for type character varying(%d)" ,
474
477
(int ) maxlen )));
@@ -477,6 +480,10 @@ varchar_input(const char *s, size_t len, int32 atttypmod)
477
480
len = mbmaxlen ;
478
481
}
479
482
483
+ /*
484
+ * We can use cstring_to_text_with_len because VarChar and text are
485
+ * binary-compatible types.
486
+ */
480
487
result = (VarChar * ) cstring_to_text_with_len (s , len );
481
488
return result ;
482
489
}
@@ -489,14 +496,13 @@ Datum
489
496
varcharin (PG_FUNCTION_ARGS )
490
497
{
491
498
char * s = PG_GETARG_CSTRING (0 );
492
-
493
499
#ifdef NOT_USED
494
500
Oid typelem = PG_GETARG_OID (1 );
495
501
#endif
496
502
int32 atttypmod = PG_GETARG_INT32 (2 );
497
503
VarChar * result ;
498
504
499
- result = varchar_input (s , strlen (s ), atttypmod );
505
+ result = varchar_input (s , strlen (s ), atttypmod , fcinfo -> context );
500
506
PG_RETURN_VARCHAR_P (result );
501
507
}
502
508
@@ -522,7 +528,6 @@ Datum
522
528
varcharrecv (PG_FUNCTION_ARGS )
523
529
{
524
530
StringInfo buf = (StringInfo ) PG_GETARG_POINTER (0 );
525
-
526
531
#ifdef NOT_USED
527
532
Oid typelem = PG_GETARG_OID (1 );
528
533
#endif
@@ -532,7 +537,7 @@ varcharrecv(PG_FUNCTION_ARGS)
532
537
int nbytes ;
533
538
534
539
str = pq_getmsgtext (buf , buf -> len - buf -> cursor , & nbytes );
535
- result = varchar_input (str , nbytes , atttypmod );
540
+ result = varchar_input (str , nbytes , atttypmod , NULL );
536
541
pfree (str );
537
542
PG_RETURN_VARCHAR_P (result );
538
543
}
0 commit comments