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

Skip to content

Commit 977ac90

Browse files
author
Michael Meskes
committed
Joachim fixed some bugs in numeric handling in pgtypeslib.
Fixed and cleaned up some regression tests. Also added a new one.
1 parent 23dc308 commit 977ac90

23 files changed

+825
-361
lines changed

src/interfaces/ecpg/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,5 +2068,9 @@ We Aug 2 13:15:25 CEST 2006
20682068
Fr Aug 4 10:44:30 CEST 2006
20692069

20702070
- Applied test suite update by Joachim Wieland <[email protected]>.
2071+
2072+
Mo Aug 7 14:56:44 CEST 2006
2073+
2074+
- Joachim fixed some bugs in numeric handling in pgtypeslib.
20712075
- Set ecpg library version to 5.2.
20722076
- Set ecpg version to 4.2.1.

src/interfaces/ecpg/pgtypeslib/numeric.c

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/pgtypeslib/numeric.c,v 1.27 2006/06/21 10:24:41 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/pgtypeslib/numeric.c,v 1.28 2006/08/07 13:17:01 meskes Exp $ */
22

33
#include "postgres_fe.h"
44
#include <ctype.h>
@@ -386,10 +386,18 @@ PGTYPESnumeric_from_asc(char *str, char **endptr)
386386
char *
387387
PGTYPESnumeric_to_asc(numeric *num, int dscale)
388388
{
389+
numeric *numcopy = PGTYPESnumeric_new();
390+
char *s;
391+
389392
if (dscale < 0)
390393
dscale = num->dscale;
391394

392-
return (get_str_from_var(num, dscale));
395+
if (PGTYPESnumeric_copy(num, numcopy) < 0)
396+
return NULL;
397+
/* get_str_from_var may change its argument */
398+
s = get_str_from_var(numcopy, dscale);
399+
PGTYPESnumeric_free(numcopy);
400+
return (s);
393401
}
394402

395403
/* ----------
@@ -1448,6 +1456,7 @@ PGTYPESnumeric_from_double(double d, numeric *dst)
14481456
if (PGTYPESnumeric_copy(tmp, dst) != 0)
14491457
return -1;
14501458
PGTYPESnumeric_free(tmp);
1459+
errno = 0;
14511460
return 0;
14521461
}
14531462

@@ -1457,21 +1466,32 @@ numericvar_to_double_no_overflow(numeric *var, double *dp)
14571466
char *tmp;
14581467
double val;
14591468
char *endptr;
1469+
numeric *varcopy = PGTYPESnumeric_new();
14601470

1461-
if ((tmp = get_str_from_var(var, var->dscale)) == NULL)
1471+
if (PGTYPESnumeric_copy(var, varcopy) < 0)
14621472
return -1;
1473+
if ((tmp = get_str_from_var(varcopy, varcopy->dscale)) == NULL)
1474+
return -1;
1475+
PGTYPESnumeric_free(varcopy);
14631476

1464-
/* unlike float8in, we ignore ERANGE from strtod */
14651477
val = strtod(tmp, &endptr);
1478+
if (errno == ERANGE)
1479+
{
1480+
free(tmp);
1481+
errno = PGTYPES_NUM_OVERFLOW;
1482+
return -1;
1483+
}
1484+
1485+
/* can't free tmp yet, endptr points still into it */
14661486
if (*endptr != '\0')
14671487
{
14681488
/* shouldn't happen ... */
14691489
free(tmp);
14701490
errno = PGTYPES_NUM_BAD_NUMERIC;
14711491
return -1;
14721492
}
1473-
*dp = val;
14741493
free(tmp);
1494+
*dp = val;
14751495
return 0;
14761496
}
14771497

@@ -1509,28 +1529,23 @@ PGTYPESnumeric_to_int(numeric *nv, int *ip)
15091529
int
15101530
PGTYPESnumeric_to_long(numeric *nv, long *lp)
15111531
{
1512-
int i;
1513-
long l = 0;
1532+
char *s = PGTYPESnumeric_to_asc(nv, 0);
1533+
char *endptr;
15141534

1515-
for (i = 1; i < nv->weight + 2; i++)
1516-
{
1517-
l *= 10;
1518-
l += nv->buf[i];
1519-
}
1520-
if (nv->buf[i] >= 5)
1521-
{
1522-
/* round up */
1523-
l++;
1524-
}
1525-
if (l > LONG_MAX || l < 0)
1535+
if (s == NULL)
1536+
return -1;
1537+
1538+
errno = 0;
1539+
*lp = strtol(s, &endptr, 10);
1540+
if (endptr == s)
1541+
/* this should not happen actually */
1542+
return -1;
1543+
if (errno == ERANGE)
15261544
{
15271545
errno = PGTYPES_NUM_OVERFLOW;
15281546
return -1;
15291547
}
1530-
1531-
if (nv->sign == NUMERIC_NEG)
1532-
l *= -1;
1533-
*lp = l;
1548+
free(s);
15341549
return 0;
15351550
}
15361551

src/interfaces/ecpg/test/compat_informix/test_informix.pgc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ int main(void)
3737

3838
/* this will fail (more than one row in subquery) */
3939
$select i from test where j=(select j from test);
40+
$rollback;
4041

4142
/* this however should be ok */
42-
$select i from test where j=(select j from test limit 1);
43+
$select i from test where j=(select j from test order by i limit 1);
4344
printf("SELECT: %ld=%s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
4445
if (sqlca.sqlcode != 0) $rollback;
4546

src/interfaces/ecpg/test/expected/compat_informix-test_informix.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,31 @@ if (sqlca.sqlcode < 0) dosqlprint ( );}
118118
if (sqlca.sqlcode < 0) dosqlprint ( );}
119119
#line 39 "test_informix.pgc"
120120

121+
{ ECPGtrans(__LINE__, NULL, "rollback");
122+
#line 40 "test_informix.pgc"
123+
124+
if (sqlca.sqlcode < 0) dosqlprint ( );}
125+
#line 40 "test_informix.pgc"
126+
121127

122128
/* this however should be ok */
123-
{ ECPGdo(__LINE__, 1, 1, NULL, "select i from test where j = ( select j from test limit 1 ) ", ECPGt_EOIT, ECPGt_EORT);
124-
#line 42 "test_informix.pgc"
129+
{ ECPGdo(__LINE__, 1, 1, NULL, "select i from test where j = ( select j from test order by i limit 1 ) ", ECPGt_EOIT, ECPGt_EORT);
130+
#line 43 "test_informix.pgc"
125131

126132
if (sqlca.sqlcode < 0) dosqlprint ( );}
127-
#line 42 "test_informix.pgc"
133+
#line 43 "test_informix.pgc"
128134

129135
printf("SELECT: %ld=%s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
130136
if (sqlca.sqlcode != 0) { ECPGtrans(__LINE__, NULL, "rollback");
131-
#line 44 "test_informix.pgc"
137+
#line 45 "test_informix.pgc"
132138

133139
if (sqlca.sqlcode < 0) dosqlprint ( );}
134-
#line 44 "test_informix.pgc"
140+
#line 45 "test_informix.pgc"
135141

136142

137143
ECPG_informix_set_var( 0, &( i ), __LINE__);\
138144
/* declare c cursor for select * from test where i <= ? */
139-
#line 46 "test_informix.pgc"
145+
#line 47 "test_informix.pgc"
140146

141147
openit();
142148

@@ -149,10 +155,10 @@ if (sqlca.sqlcode < 0) dosqlprint ( );}
149155
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
150156
ECPGt_decimal,&(j),(long)1,(long)1,sizeof(decimal),
151157
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
152-
#line 53 "test_informix.pgc"
158+
#line 54 "test_informix.pgc"
153159

154160
if (sqlca.sqlcode < 0) dosqlprint ( );}
155-
#line 53 "test_informix.pgc"
161+
#line 54 "test_informix.pgc"
156162

157163
if (sqlca.sqlcode == 100) break;
158164
else if (sqlca.sqlcode != 0) printf ("Error: %ld\n", sqlca.sqlcode);
@@ -174,53 +180,53 @@ if (sqlca.sqlcode < 0) dosqlprint ( );}
174180
{ ECPGdo(__LINE__, 1, 1, NULL, "delete from test where i = ?",
175181
ECPGt_decimal,&(n),(long)1,(long)1,sizeof(decimal),
176182
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
177-
#line 71 "test_informix.pgc"
183+
#line 72 "test_informix.pgc"
178184

179185
if (sqlca.sqlcode < 0) dosqlprint ( );}
180-
#line 71 "test_informix.pgc"
186+
#line 72 "test_informix.pgc"
181187

182188
printf("DELETE: %ld\n", sqlca.sqlcode);
183189

184190
{ ECPGdo(__LINE__, 1, 1, NULL, "select 1 from test where i = 14 ", ECPGt_EOIT, ECPGt_EORT);
185-
#line 74 "test_informix.pgc"
191+
#line 75 "test_informix.pgc"
186192

187193
if (sqlca.sqlcode < 0) dosqlprint ( );}
188-
#line 74 "test_informix.pgc"
194+
#line 75 "test_informix.pgc"
189195

190196
printf("Exists: %ld\n", sqlca.sqlcode);
191197

192198
{ ECPGdo(__LINE__, 1, 1, NULL, "select 1 from test where i = 147 ", ECPGt_EOIT, ECPGt_EORT);
193-
#line 77 "test_informix.pgc"
199+
#line 78 "test_informix.pgc"
194200

195201
if (sqlca.sqlcode < 0) dosqlprint ( );}
196-
#line 77 "test_informix.pgc"
202+
#line 78 "test_informix.pgc"
197203

198204
printf("Does not exist: %ld\n", sqlca.sqlcode);
199205

200206
{ ECPGtrans(__LINE__, NULL, "commit");
201-
#line 80 "test_informix.pgc"
207+
#line 81 "test_informix.pgc"
202208

203209
if (sqlca.sqlcode < 0) dosqlprint ( );}
204-
#line 80 "test_informix.pgc"
210+
#line 81 "test_informix.pgc"
205211

206212
{ ECPGdo(__LINE__, 1, 1, NULL, "drop table test ", ECPGt_EOIT, ECPGt_EORT);
207-
#line 81 "test_informix.pgc"
213+
#line 82 "test_informix.pgc"
208214

209215
if (sqlca.sqlcode < 0) dosqlprint ( );}
210-
#line 81 "test_informix.pgc"
216+
#line 82 "test_informix.pgc"
211217

212218
{ ECPGtrans(__LINE__, NULL, "commit");
213-
#line 82 "test_informix.pgc"
219+
#line 83 "test_informix.pgc"
214220

215221
if (sqlca.sqlcode < 0) dosqlprint ( );}
216-
#line 82 "test_informix.pgc"
222+
#line 83 "test_informix.pgc"
217223

218224

219225
{ ECPGdisconnect(__LINE__, "CURRENT");
220-
#line 84 "test_informix.pgc"
226+
#line 85 "test_informix.pgc"
221227

222228
if (sqlca.sqlcode < 0) dosqlprint ( );}
223-
#line 84 "test_informix.pgc"
229+
#line 85 "test_informix.pgc"
224230

225231

226232
return 0;
@@ -231,10 +237,10 @@ static void openit(void)
231237
{ ECPGdo(__LINE__, 1, 1, NULL, "declare c cursor for select * from test where i <= ? ",
232238
ECPGt_int,&(*( int *)(ECPG_informix_get_var( 0))),(long)1,(long)1,sizeof(int),
233239
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
234-
#line 91 "test_informix.pgc"
240+
#line 92 "test_informix.pgc"
235241

236242
if (sqlca.sqlcode < 0) dosqlprint ( );}
237-
#line 91 "test_informix.pgc"
243+
#line 92 "test_informix.pgc"
238244

239245
}
240246

src/interfaces/ecpg/test/expected/compat_informix-test_informix.stderr

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,63 +32,61 @@
3232
[NO_PID]: sqlca: code: 0, state: 00000
3333
[NO_PID]: raising sqlstate 21000 (sqlcode: -284) in line 39, ''more than one row returned by a subquery used as an expression' in line 39.'.
3434
[NO_PID]: sqlca: code: -284, state: 21000
35-
[NO_PID]: ECPGexecute line 42: QUERY: select i from test where j = ( select j from test limit 1 ) on connection regress1
35+
[NO_PID]: ECPGtrans line 40 action = rollback connection = regress1
3636
[NO_PID]: sqlca: code: 0, state: 00000
37-
[NO_PID]: ECPGexecute line 42: Error: ERROR: current transaction is aborted, commands ignored until end of transaction block
37+
[NO_PID]: ECPGexecute line 43: QUERY: select i from test where j = ( select j from test order by i limit 1 ) on connection regress1
3838
[NO_PID]: sqlca: code: 0, state: 00000
39-
[NO_PID]: raising sqlstate 25P02 (sqlcode: -400) in line 42, ''current transaction is aborted, commands ignored until end of transaction block' in line 42.'.
40-
[NO_PID]: sqlca: code: -400, state: 25P02
41-
[NO_PID]: ECPGtrans line 44 action = rollback connection = regress1
39+
[NO_PID]: ECPGexecute line 43: Correctly got 1 tuples with 1 fields
4240
[NO_PID]: sqlca: code: 0, state: 00000
43-
[NO_PID]: ECPGexecute line 91: QUERY: declare c cursor for select * from test where i <= 14 on connection regress1
41+
[NO_PID]: ECPGexecute line 92: QUERY: declare c cursor for select * from test where i <= 14 on connection regress1
4442
[NO_PID]: sqlca: code: 0, state: 00000
45-
[NO_PID]: ECPGexecute line 91 Ok: DECLARE CURSOR
43+
[NO_PID]: ECPGexecute line 92 Ok: DECLARE CURSOR
4644
[NO_PID]: sqlca: code: 0, state: 00000
47-
[NO_PID]: ECPGexecute line 53: QUERY: fetch forward from c on connection regress1
45+
[NO_PID]: ECPGexecute line 54: QUERY: fetch forward from c on connection regress1
4846
[NO_PID]: sqlca: code: 0, state: 00000
49-
[NO_PID]: ECPGexecute line 53: Correctly got 1 tuples with 2 fields
47+
[NO_PID]: ECPGexecute line 54: Correctly got 1 tuples with 2 fields
5048
[NO_PID]: sqlca: code: 0, state: 00000
51-
[NO_PID]: ECPGget_data line 53: RESULT: 7 offset: 4 array: Yes
49+
[NO_PID]: ECPGget_data line 54: RESULT: 7 offset: 4 array: Yes
5250
[NO_PID]: sqlca: code: 0, state: 00000
53-
[NO_PID]: ECPGget_data line 53: RESULT: 0 offset: 52 array: Yes
51+
[NO_PID]: ECPGget_data line 54: RESULT: 0 offset: 52 array: Yes
5452
[NO_PID]: sqlca: code: 0, state: 00000
55-
[NO_PID]: ECPGexecute line 53: QUERY: fetch forward from c on connection regress1
53+
[NO_PID]: ECPGexecute line 54: QUERY: fetch forward from c on connection regress1
5654
[NO_PID]: sqlca: code: 0, state: 00000
57-
[NO_PID]: ECPGexecute line 53: Correctly got 1 tuples with 2 fields
55+
[NO_PID]: ECPGexecute line 54: Correctly got 1 tuples with 2 fields
5856
[NO_PID]: sqlca: code: 0, state: 00000
59-
[NO_PID]: ECPGget_data line 53: RESULT: 14 offset: 4 array: Yes
57+
[NO_PID]: ECPGget_data line 54: RESULT: 14 offset: 4 array: Yes
6058
[NO_PID]: sqlca: code: 0, state: 00000
61-
[NO_PID]: ECPGget_data line 53: RESULT: 1 offset: 52 array: Yes
59+
[NO_PID]: ECPGget_data line 54: RESULT: 1 offset: 52 array: Yes
6260
[NO_PID]: sqlca: code: 0, state: 00000
63-
[NO_PID]: ECPGexecute line 53: QUERY: fetch forward from c on connection regress1
61+
[NO_PID]: ECPGexecute line 54: QUERY: fetch forward from c on connection regress1
6462
[NO_PID]: sqlca: code: 0, state: 00000
65-
[NO_PID]: ECPGexecute line 53: Correctly got 0 tuples with 2 fields
63+
[NO_PID]: ECPGexecute line 54: Correctly got 0 tuples with 2 fields
6664
[NO_PID]: sqlca: code: 0, state: 00000
67-
[NO_PID]: raising sqlcode 100 in line 53, 'No data found in line 53.'.
65+
[NO_PID]: raising sqlcode 100 in line 54, 'No data found in line 54.'.
6866
[NO_PID]: sqlca: code: 100, state: 02000
69-
[NO_PID]: ECPGexecute line 71: QUERY: delete from test where i = 21.0 on connection regress1
67+
[NO_PID]: ECPGexecute line 72: QUERY: delete from test where i = 21.0 on connection regress1
7068
[NO_PID]: sqlca: code: 0, state: 00000
71-
[NO_PID]: ECPGexecute line 71 Ok: DELETE 0
69+
[NO_PID]: ECPGexecute line 72 Ok: DELETE 0
7270
[NO_PID]: sqlca: code: 0, state: 00000
73-
[NO_PID]: raising sqlcode 100 in line 71, 'No data found in line 71.'.
71+
[NO_PID]: raising sqlcode 100 in line 72, 'No data found in line 72.'.
7472
[NO_PID]: sqlca: code: 100, state: 02000
75-
[NO_PID]: ECPGexecute line 74: QUERY: select 1 from test where i = 14 on connection regress1
73+
[NO_PID]: ECPGexecute line 75: QUERY: select 1 from test where i = 14 on connection regress1
7674
[NO_PID]: sqlca: code: 0, state: 00000
77-
[NO_PID]: ECPGexecute line 74: Correctly got 1 tuples with 1 fields
75+
[NO_PID]: ECPGexecute line 75: Correctly got 1 tuples with 1 fields
7876
[NO_PID]: sqlca: code: 0, state: 00000
79-
[NO_PID]: ECPGexecute line 77: QUERY: select 1 from test where i = 147 on connection regress1
77+
[NO_PID]: ECPGexecute line 78: QUERY: select 1 from test where i = 147 on connection regress1
8078
[NO_PID]: sqlca: code: 0, state: 00000
81-
[NO_PID]: ECPGexecute line 77: Correctly got 0 tuples with 1 fields
79+
[NO_PID]: ECPGexecute line 78: Correctly got 0 tuples with 1 fields
8280
[NO_PID]: sqlca: code: 0, state: 00000
83-
[NO_PID]: raising sqlcode 100 in line 77, 'No data found in line 77.'.
81+
[NO_PID]: raising sqlcode 100 in line 78, 'No data found in line 78.'.
8482
[NO_PID]: sqlca: code: 100, state: 02000
85-
[NO_PID]: ECPGtrans line 80 action = commit connection = regress1
83+
[NO_PID]: ECPGtrans line 81 action = commit connection = regress1
8684
[NO_PID]: sqlca: code: 0, state: 00000
87-
[NO_PID]: ECPGexecute line 81: QUERY: drop table test on connection regress1
85+
[NO_PID]: ECPGexecute line 82: QUERY: drop table test on connection regress1
8886
[NO_PID]: sqlca: code: 0, state: 00000
89-
[NO_PID]: ECPGexecute line 81 Ok: DROP TABLE
87+
[NO_PID]: ECPGexecute line 82 Ok: DROP TABLE
9088
[NO_PID]: sqlca: code: 0, state: 00000
91-
[NO_PID]: ECPGtrans line 82 action = commit connection = regress1
89+
[NO_PID]: ECPGtrans line 83 action = commit connection = regress1
9290
[NO_PID]: sqlca: code: 0, state: 00000
9391
[NO_PID]: ecpg_finish: Connection regress1 closed.
9492
[NO_PID]: sqlca: code: 0, state: 00000

src/interfaces/ecpg/test/expected/compat_informix-test_informix.stdout

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
doSQLprint: Error: 'duplicate key violates unique constraint "test_pkey"' in line 31.
22
INSERT: -239='duplicate key violates unique constraint "test_pkey"' in line 31.
33
doSQLprint: Error: 'more than one row returned by a subquery used as an expression' in line 39.
4-
doSQLprint: Error: 'current transaction is aborted, commands ignored until end of transaction block' in line 42.
5-
SELECT: -400='current transaction is aborted, commands ignored until end of transaction block' in line 42.
4+
SELECT: 0=
65
7 0
76
14 1
87
DELETE: 100

0 commit comments

Comments
 (0)