Skip to content

Commit 7c9e2c7

Browse files
author
Michael Meskes
committed
Fixed error handling in numeric conversion (Joachim).
Further regression cleanup.
1 parent 8a7a6af commit 7c9e2c7

File tree

6 files changed

+189
-60
lines changed

6 files changed

+189
-60
lines changed

src/interfaces/ecpg/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,5 +2076,9 @@ Mo Aug 7 14:56:44 CEST 2006
20762076
Tu Aug 8 13:26:25 CEST 2006
20772077

20782078
- Made parser check for valid copy to/from stdin/stdout combinations.
2079+
2080+
We Aug 9 09:28:56 CEST 2006
2081+
2082+
- Fixed error handling in numeric conversion (Joachim).
20792083
- Set ecpg library version to 5.2.
20802084
- Set ecpg version to 4.2.1.

src/interfaces/ecpg/include/pgtypes_numeric.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/include/pgtypes_numeric.h,v 1.15 2006/03/11 04:38:39 momjian Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/include/pgtypes_numeric.h,v 1.16 2006/08/09 07:30:56 meskes Exp $ */
22

33
#ifndef PGTYPES_NUMERIC
44
#define PGTYPES_NUMERIC
@@ -41,7 +41,9 @@ extern "C"
4141
#endif
4242

4343
numeric *PGTYPESnumeric_new(void);
44+
decimal *PGTYPESdecimal_new(void);
4445
void PGTYPESnumeric_free(numeric *);
46+
void PGTYPESdecimal_free(decimal *);
4547
numeric *PGTYPESnumeric_from_asc(char *, char **);
4648
char *PGTYPESnumeric_to_asc(numeric *, int);
4749
int PGTYPESnumeric_add(numeric *, numeric *, numeric *);

src/interfaces/ecpg/pgtypeslib/numeric.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/pgtypeslib/numeric.c,v 1.28 2006/08/07 13:17:01 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/pgtypeslib/numeric.c,v 1.29 2006/08/09 07:30:56 meskes Exp $ */
22

33
#include "postgres_fe.h"
44
#include <ctype.h>
@@ -139,6 +139,18 @@ PGTYPESnumeric_new(void)
139139
return var;
140140
}
141141

142+
decimal *
143+
PGTYPESdecimal_new(void)
144+
{
145+
decimal *var;
146+
if ((var = (decimal *) pgtypes_alloc(sizeof(decimal))) == NULL)
147+
return NULL;
148+
149+
memset(var, 0, sizeof(decimal));
150+
151+
return var;
152+
}
153+
142154
/* ----------
143155
* set_var_from_str()
144156
*
@@ -425,6 +437,12 @@ PGTYPESnumeric_free(numeric *var)
425437
free(var);
426438
}
427439

440+
void
441+
PGTYPESdecimal_free(decimal *var)
442+
{
443+
free(var);
444+
}
445+
428446
/* ----------
429447
* cmp_abs() -
430448
*
@@ -1461,19 +1479,25 @@ PGTYPESnumeric_from_double(double d, numeric *dst)
14611479
}
14621480

14631481
static int
1464-
numericvar_to_double_no_overflow(numeric *var, double *dp)
1482+
numericvar_to_double(numeric *var, double *dp)
14651483
{
14661484
char *tmp;
14671485
double val;
14681486
char *endptr;
14691487
numeric *varcopy = PGTYPESnumeric_new();
1488+
int i;
14701489

14711490
if (PGTYPESnumeric_copy(var, varcopy) < 0)
14721491
return -1;
14731492
if ((tmp = get_str_from_var(varcopy, varcopy->dscale)) == NULL)
14741493
return -1;
14751494
PGTYPESnumeric_free(varcopy);
14761495

1496+
/*
1497+
* strtod seems to not reset errno to 0 in case of success.
1498+
* at least on aome architectures
1499+
*/
1500+
errno = 0;
14771501
val = strtod(tmp, &endptr);
14781502
if (errno == ERANGE)
14791503
{
@@ -1501,7 +1525,7 @@ PGTYPESnumeric_to_double(numeric *nv, double *dp)
15011525
double tmp;
15021526
int i;
15031527

1504-
if ((i = numericvar_to_double_no_overflow(nv, &tmp)) != 0)
1528+
if ((i = numericvar_to_double(nv, &tmp)) != 0)
15051529
return -1;
15061530
*dp = tmp;
15071531
return 0;

src/interfaces/ecpg/test/expected/pgtypeslib-num_test2.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
#line 7 "num_test2.pgc"
2525

2626

27-
char* nums[] = { "2E394", "-2", ".794", "3.44", "592.49E07", "-32.84e4",
27+
char* nums[] = { "2E394", "-2", ".794", "3.44", "592.49E21", "-32.84e4",
2828
"2E-394", ".1E-2", "+.0", "-592.49E-07", "+32.84e-4",
2929
".500001", "-.5000001",
30+
"1234567890123456789012345678.91", /* 30 digits should fit
31+
into decimal */
32+
"1234567890123456789012345678.921", /* 31 digits should NOT
33+
fit into decimal */
3034
NULL};
3135

3236

@@ -39,8 +43,9 @@ main(void)
3943
char *text="error\n";
4044
char *endptr;
4145
numeric *num, *nin;
46+
decimal *dec;
4247
long l;
43-
int i, r, k;
48+
int i, q, r, k;
4449
double d;
4550

4651
ECPGdebug(1, stderr);
@@ -79,8 +84,9 @@ main(void)
7984
r = PGTYPESnumeric_from_long(l, nin);
8085
check_errno();
8186
text = PGTYPESnumeric_to_asc(nin, 2);
82-
r = PGTYPESnumeric_cmp(num, nin);
83-
printf("num[%d,7]: %s (cmp: %d)\n", i, text, r); free(text);
87+
q = PGTYPESnumeric_cmp(num, nin);
88+
printf("num[%d,7]: %s (r: %d - cmp: %d)\n", i, text, r, q);
89+
free(text);
8490
}
8591

8692
r = PGTYPESnumeric_to_int(num, &k);
@@ -91,8 +97,9 @@ main(void)
9197
r = PGTYPESnumeric_from_int(k, nin);
9298
check_errno();
9399
text = PGTYPESnumeric_to_asc(nin, 2);
94-
r = PGTYPESnumeric_cmp(num, nin);
95-
printf("num[%d,9]: %s (cmp: %d)\n", i, text, r); free(text);
100+
q = PGTYPESnumeric_cmp(num, nin);
101+
printf("num[%d,9]: %s (r: %d - cmp: %d)\n", i, text, r, q);
102+
free(text);
96103
}
97104

98105
r = PGTYPESnumeric_to_double(num, &d);
@@ -103,11 +110,28 @@ main(void)
103110
r = PGTYPESnumeric_from_double(d, nin);
104111
check_errno();
105112
text = PGTYPESnumeric_to_asc(nin, 2);
106-
r = PGTYPESnumeric_cmp(num, nin);
107-
printf("num[%d,11]: %s (cmp: %d)\n", i, text, r); free(text);
113+
q = PGTYPESnumeric_cmp(num, nin);
114+
printf("num[%d,11]: %s (r: %d - cmp: %d)\n", i, text, r, q);
115+
free(text);
108116
}
109117

110-
/* xxx decimal conversions still missing */
118+
dec = PGTYPESdecimal_new();
119+
r = PGTYPESnumeric_to_decimal(num, dec);
120+
check_errno();
121+
/* we have no special routine for outputting decimal, it would
122+
* convert to a numeric anyway */
123+
printf("num[%d,12]: - (r: %d)\n", i, r);
124+
if (r == 0)
125+
{
126+
r = PGTYPESnumeric_from_decimal(dec, nin);
127+
check_errno();
128+
text = PGTYPESnumeric_to_asc(nin, 2);
129+
q = PGTYPESnumeric_cmp(num, nin);
130+
printf("num[%d,13]: %s (r: %d - cmp: %d)\n", i, text, r, q);
131+
free(text);
132+
}
133+
134+
PGTYPESdecimal_free(dec);
111135
PGTYPESnumeric_free(nin);
112136
printf("\n");
113137
}

0 commit comments

Comments
 (0)