1
- /* $PostgreSQL: pgsql/src/interfaces/ecpg/compatlib/informix.c,v 1.46 2006/06/26 09:20:09 meskes Exp $ */
1
+ /* $PostgreSQL: pgsql/src/interfaces/ecpg/compatlib/informix.c,v 1.47 2006/08/15 06:40:19 meskes Exp $ */
2
2
3
3
#include <stdlib.h>
4
4
#include <string.h>
@@ -122,12 +122,15 @@ deccall3(decimal *arg1, decimal *arg2, decimal *result, int (*ptr) (numeric *, n
122
122
int
123
123
decadd (decimal * arg1 , decimal * arg2 , decimal * sum )
124
124
{
125
+ errno = 0 ;
125
126
deccall3 (arg1 , arg2 , sum , PGTYPESnumeric_add );
126
127
127
128
if (errno == PGTYPES_NUM_OVERFLOW )
128
129
return ECPG_INFORMIX_NUM_OVERFLOW ;
129
- else if (errno != 0 )
130
+ else if (errno == PGTYPES_NUM_UNDERFLOW )
130
131
return ECPG_INFORMIX_NUM_UNDERFLOW ;
132
+ else if (errno != 0 )
133
+ return -1 ;
131
134
else
132
135
return 0 ;
133
136
}
@@ -179,6 +182,7 @@ deccvasc(char *cp, int len, decimal *np)
179
182
ret = ECPG_INFORMIX_NUM_UNDERFLOW ;
180
183
else
181
184
{
185
+ errno = 0 ;
182
186
result = PGTYPESnumeric_from_asc (str , NULL );
183
187
if (!result )
184
188
{
@@ -280,6 +284,7 @@ decdiv(decimal *n1, decimal *n2, decimal *result)
280
284
281
285
int i ;
282
286
287
+ errno = 0 ;
283
288
i = deccall3 (n1 , n2 , result , PGTYPESnumeric_div );
284
289
285
290
if (i != 0 )
@@ -304,6 +309,7 @@ decmul(decimal *n1, decimal *n2, decimal *result)
304
309
{
305
310
int i ;
306
311
312
+ errno = 0 ;
307
313
i = deccall3 (n1 , n2 , result , PGTYPESnumeric_mul );
308
314
309
315
if (i != 0 )
@@ -325,6 +331,7 @@ decsub(decimal *n1, decimal *n2, decimal *result)
325
331
{
326
332
int i ;
327
333
334
+ errno = 0 ;
328
335
i = deccall3 (n1 , n2 , result , PGTYPESnumeric_sub );
329
336
330
337
if (i != 0 )
@@ -371,13 +378,25 @@ dectoasc(decimal *np, char *cp, int len, int right)
371
378
return -1 ;
372
379
373
380
/*
374
- * TODO: have to take care of len here and create exponatial notion if
375
- * necessary
381
+ * TODO: have to take care of len here and create exponential notation
382
+ * if necessary
376
383
*/
377
- strncpy (cp , str , len );
378
- free (str );
379
-
380
- return 0 ;
384
+ if ((int ) (strlen (str ) + 1 ) > len )
385
+ {
386
+ if (len > 1 )
387
+ {
388
+ cp [0 ] = '*' ;
389
+ cp [1 ] = '\0' ;
390
+ }
391
+ free (str );
392
+ return -1 ;
393
+ }
394
+ else
395
+ {
396
+ strcpy (cp , str );
397
+ free (str );
398
+ return 0 ;
399
+ }
381
400
}
382
401
383
402
int
@@ -474,59 +493,7 @@ rdatestr(date d, char *str)
474
493
int
475
494
rstrdate (char * str , date * d )
476
495
{
477
- date dat ;
478
- char strbuf [10 ];
479
- int i ,
480
- j ;
481
-
482
- rsetnull (CDATETYPE , (char * ) & dat );
483
-
484
- /*
485
- * we have to flip the year month date around for postgres expects
486
- * yyyymmdd
487
- *
488
- */
489
-
490
- for (i = 0 , j = 0 ; i < 10 ; i ++ )
491
- {
492
- /* ignore non-digits */
493
- if (isdigit ((unsigned char ) str [i ]))
494
- {
495
-
496
- /* j only increments if it is a digit */
497
- switch (j )
498
- {
499
- /* stick the month into the 4th, 5th position */
500
- case 0 :
501
- case 1 :
502
- strbuf [j + 4 ] = str [i ];
503
- break ;
504
- /* stick the day into the 6th, and 7th position */
505
- case 2 :
506
- case 3 :
507
- strbuf [j + 4 ] = str [i ];
508
- break ;
509
-
510
- /* stick the year into the first 4 positions */
511
- case 4 :
512
- case 5 :
513
- case 6 :
514
- case 7 :
515
- strbuf [j - 4 ] = str [i ];
516
- break ;
517
-
518
- }
519
- j ++ ;
520
- }
521
- }
522
- strbuf [8 ] = '\0' ;
523
- dat = PGTYPESdate_from_asc (strbuf , NULL );
524
-
525
- if (errno && errno != PGTYPES_DATE_BAD_DATE )
526
- return ECPG_INFORMIX_BAD_DATE ;
527
-
528
- * d = dat ;
529
- return 0 ;
496
+ return rdefmtdate (d , "mm/dd/yyyy" , str );
530
497
}
531
498
532
499
void
@@ -554,6 +521,7 @@ rdefmtdate(date * d, char *fmt, char *str)
554
521
/* TODO: take care of DBCENTURY environment variable */
555
522
/* PGSQL functions allow all centuries */
556
523
524
+ errno = 0 ;
557
525
if (PGTYPESdate_defmt_asc (d , fmt , str ) == 0 )
558
526
return 0 ;
559
527
@@ -576,6 +544,7 @@ rdefmtdate(date * d, char *fmt, char *str)
576
544
int
577
545
rfmtdate (date d , char * fmt , char * str )
578
546
{
547
+ errno = 0 ;
579
548
if (PGTYPESdate_fmt_asc (d , fmt , str ) == 0 )
580
549
return 0 ;
581
550
@@ -618,22 +587,31 @@ dtcvasc(char *str, timestamp * ts)
618
587
int i ;
619
588
char * * endptr = & str ;
620
589
590
+ errno = 0 ;
621
591
ts_tmp = PGTYPEStimestamp_from_asc (str , endptr );
622
592
i = errno ;
623
593
if (i )
594
+ /* TODO: rewrite to Informix error codes */
624
595
return i ;
625
596
if (* * endptr )
626
597
{
627
598
/* extra characters exist at the end */
628
599
return ECPG_INFORMIX_EXTRA_CHARS ;
629
600
}
601
+ /* TODO: other Informix error codes missing */
630
602
631
603
/* everything went fine */
632
604
* ts = ts_tmp ;
633
605
634
606
return 0 ;
635
607
}
636
608
609
+ int
610
+ dtcvfmtasc (char * inbuf , char * fmtstr , timestamp * dtvalue )
611
+ {
612
+ return PGTYPEStimestamp_defmt_asc (inbuf , fmtstr , dtvalue );
613
+ }
614
+
637
615
int
638
616
dtsub (timestamp * ts1 , timestamp * ts2 , interval * iv )
639
617
{
@@ -659,6 +637,7 @@ dttofmtasc(timestamp * ts, char *output, int str_len, char *fmtstr)
659
637
int
660
638
intoasc (interval * i , char * str )
661
639
{
640
+ errno = 0 ;
662
641
str = PGTYPESinterval_to_asc (i );
663
642
664
643
if (!str )
@@ -797,17 +776,21 @@ rfmtlong(long lng_val, char *fmt, char *outbuf)
797
776
/* qualify, where we are in the value_string */
798
777
if (k < 0 )
799
778
{
800
- if (leftalign )
801
- {
802
- /* can't use strncat(,,0) here, Solaris would freek out */
803
- temp [j ] = '\0' ;
804
- break ;
805
- }
806
779
blank = 1 ;
807
780
if (k == -2 )
808
781
entity = 1 ;
809
782
else if (k == -1 )
810
783
sign = 1 ;
784
+ if (leftalign )
785
+ {
786
+ /* can't use strncat(,,0) here, Solaris would freek out */
787
+ if (sign )
788
+ if (signdone )
789
+ {
790
+ temp [j ] = '\0' ;
791
+ break ;
792
+ }
793
+ }
811
794
}
812
795
/* if we're right side of the right-most dot, print '0' */
813
796
if (dotpos >= 0 && dotpos <= i )
@@ -829,6 +812,9 @@ rfmtlong(long lng_val, char *fmt, char *outbuf)
829
812
fmtchar = lastfmt ;
830
813
else
831
814
fmtchar = fmt [i ];
815
+ /* waiting for the sign */
816
+ if (k < 0 && leftalign && sign && !signdone && fmtchar != '+' && fmtchar != '-' )
817
+ continue ;
832
818
/* analyse this format-char */
833
819
switch (fmtchar )
834
820
{
@@ -854,9 +840,6 @@ rfmtlong(long lng_val, char *fmt, char *outbuf)
854
840
else
855
841
tmp [0 ] = value .val_string [k ];
856
842
break ;
857
- case '<' :
858
- tmp [0 ] = value .val_string [k ];
859
- break ;
860
843
case '-' :
861
844
if (sign && value .sign == '-' && !signdone )
862
845
{
@@ -904,6 +887,9 @@ rfmtlong(long lng_val, char *fmt, char *outbuf)
904
887
else
905
888
tmp [0 ] = value .val_string [k ];
906
889
break ;
890
+ case '<' :
891
+ tmp [0 ] = value .val_string [k ];
892
+ break ;
907
893
default :
908
894
tmp [0 ] = fmt [i ];
909
895
}
@@ -950,8 +936,9 @@ byleng(char *str, int len)
950
936
void
951
937
ldchar (char * src , int len , char * dest )
952
938
{
953
- memmove (dest , src , len );
954
- dest [len ] = 0 ;
939
+ int dlen = byleng (src , len );
940
+ memmove (dest , src , dlen );
941
+ dest [dlen ] = '\0' ;
955
942
}
956
943
957
944
int
@@ -978,12 +965,6 @@ rtypwidth(int sqltype, int sqllen)
978
965
return 0 ;
979
966
}
980
967
981
- int
982
- dtcvfmtasc (char * inbuf , char * fmtstr , timestamp * dtvalue )
983
- {
984
- return PGTYPEStimestamp_defmt_asc (inbuf , fmtstr , dtvalue );
985
- }
986
-
987
968
static struct var_list
988
969
{
989
970
int number ;
0 commit comments