8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.58 2001/10/05 17:28:12 tgl Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.59 2001/10/23 17:38:25 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
15
15
#include "postgres.h"
16
16
17
17
#include "access/printtup.h"
18
+ #include "catalog/heap.h"
18
19
#include "commands/command.h"
19
20
#include "executor/spi_priv.h"
20
21
@@ -435,28 +436,42 @@ int
435
436
SPI_fnumber (TupleDesc tupdesc , char * fname )
436
437
{
437
438
int res ;
439
+ Form_pg_attribute sysatt ;
438
440
439
441
for (res = 0 ; res < tupdesc -> natts ; res ++ )
440
442
{
441
- if (strcasecmp ( NameStr ( tupdesc -> attrs [res ]-> attname ) , fname ) == 0 )
443
+ if (namestrcmp ( & tupdesc -> attrs [res ]-> attname , fname ) == 0 )
442
444
return res + 1 ;
443
445
}
444
446
447
+ sysatt = SystemAttributeByName (fname , true /* "oid" will be accepted */ );
448
+ if (sysatt != NULL )
449
+ return sysatt -> attnum ;
450
+
451
+ /* SPI_ERROR_NOATTRIBUTE is different from all sys column numbers */
445
452
return SPI_ERROR_NOATTRIBUTE ;
446
453
}
447
454
448
455
char *
449
456
SPI_fname (TupleDesc tupdesc , int fnumber )
450
457
{
458
+ Form_pg_attribute att ;
451
459
452
460
SPI_result = 0 ;
453
- if (tupdesc -> natts < fnumber || fnumber <= 0 )
461
+
462
+ if (fnumber > tupdesc -> natts || fnumber == 0 ||
463
+ fnumber <= FirstLowInvalidHeapAttributeNumber )
454
464
{
455
465
SPI_result = SPI_ERROR_NOATTRIBUTE ;
456
466
return NULL ;
457
467
}
458
468
459
- return pstrdup (NameStr (tupdesc -> attrs [fnumber - 1 ]-> attname ));
469
+ if (fnumber > 0 )
470
+ att = tupdesc -> attrs [fnumber - 1 ];
471
+ else
472
+ att = SystemAttributeDefinition (fnumber , true);
473
+
474
+ return pstrdup (NameStr (att -> attname ));
460
475
}
461
476
462
477
char *
@@ -466,12 +481,16 @@ SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
466
481
val ,
467
482
result ;
468
483
bool isnull ;
469
- Oid foutoid ,
484
+ Oid typoid ,
485
+ foutoid ,
470
486
typelem ;
487
+ int32 typmod ;
471
488
bool typisvarlena ;
472
489
473
490
SPI_result = 0 ;
474
- if (tuple -> t_data -> t_natts < fnumber || fnumber <= 0 )
491
+
492
+ if (fnumber > tuple -> t_data -> t_natts || fnumber == 0 ||
493
+ fnumber <= FirstLowInvalidHeapAttributeNumber )
475
494
{
476
495
SPI_result = SPI_ERROR_NOATTRIBUTE ;
477
496
return NULL ;
@@ -480,8 +499,19 @@ SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
480
499
origval = heap_getattr (tuple , fnumber , tupdesc , & isnull );
481
500
if (isnull )
482
501
return NULL ;
483
- if (!getTypeOutputInfo (tupdesc -> attrs [fnumber - 1 ]-> atttypid ,
484
- & foutoid , & typelem , & typisvarlena ))
502
+
503
+ if (fnumber > 0 )
504
+ {
505
+ typoid = tupdesc -> attrs [fnumber - 1 ]-> atttypid ;
506
+ typmod = tupdesc -> attrs [fnumber - 1 ]-> atttypmod ;
507
+ }
508
+ else
509
+ {
510
+ typoid = (SystemAttributeDefinition (fnumber , true))-> atttypid ;
511
+ typmod = -1 ;
512
+ }
513
+
514
+ if (!getTypeOutputInfo (typoid , & foutoid , & typelem , & typisvarlena ))
485
515
{
486
516
SPI_result = SPI_ERROR_NOOUTFUNC ;
487
517
return NULL ;
@@ -499,7 +529,7 @@ SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
499
529
result = OidFunctionCall3 (foutoid ,
500
530
val ,
501
531
ObjectIdGetDatum (typelem ),
502
- Int32GetDatum (tupdesc -> attrs [ fnumber - 1 ] -> atttypmod ));
532
+ Int32GetDatum (typmod ));
503
533
504
534
/* Clean up detoasted copy, if any */
505
535
if (val != origval )
@@ -511,36 +541,42 @@ SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
511
541
Datum
512
542
SPI_getbinval (HeapTuple tuple , TupleDesc tupdesc , int fnumber , bool * isnull )
513
543
{
514
- Datum val ;
515
-
516
- * isnull = true;
517
544
SPI_result = 0 ;
518
- if (tuple -> t_data -> t_natts < fnumber || fnumber <= 0 )
545
+
546
+ if (fnumber > tuple -> t_data -> t_natts || fnumber == 0 ||
547
+ fnumber <= FirstLowInvalidHeapAttributeNumber )
519
548
{
520
549
SPI_result = SPI_ERROR_NOATTRIBUTE ;
550
+ * isnull = true;
521
551
return (Datum ) NULL ;
522
552
}
523
553
524
- val = heap_getattr (tuple , fnumber , tupdesc , isnull );
525
-
526
- return val ;
554
+ return heap_getattr (tuple , fnumber , tupdesc , isnull );
527
555
}
528
556
529
557
char *
530
558
SPI_gettype (TupleDesc tupdesc , int fnumber )
531
559
{
560
+ Oid typoid ;
532
561
HeapTuple typeTuple ;
533
562
char * result ;
534
563
535
564
SPI_result = 0 ;
536
- if (tupdesc -> natts < fnumber || fnumber <= 0 )
565
+
566
+ if (fnumber > tupdesc -> natts || fnumber == 0 ||
567
+ fnumber <= FirstLowInvalidHeapAttributeNumber )
537
568
{
538
569
SPI_result = SPI_ERROR_NOATTRIBUTE ;
539
570
return NULL ;
540
571
}
541
572
573
+ if (fnumber > 0 )
574
+ typoid = tupdesc -> attrs [fnumber - 1 ]-> atttypid ;
575
+ else
576
+ typoid = (SystemAttributeDefinition (fnumber , true))-> atttypid ;
577
+
542
578
typeTuple = SearchSysCache (TYPEOID ,
543
- ObjectIdGetDatum ( tupdesc -> attrs [ fnumber - 1 ] -> atttypid ),
579
+ ObjectIdGetDatum ( typoid ),
544
580
0 , 0 , 0 );
545
581
546
582
if (!HeapTupleIsValid (typeTuple ))
@@ -557,15 +593,19 @@ SPI_gettype(TupleDesc tupdesc, int fnumber)
557
593
Oid
558
594
SPI_gettypeid (TupleDesc tupdesc , int fnumber )
559
595
{
560
-
561
596
SPI_result = 0 ;
562
- if (tupdesc -> natts < fnumber || fnumber <= 0 )
597
+
598
+ if (fnumber > tupdesc -> natts || fnumber == 0 ||
599
+ fnumber <= FirstLowInvalidHeapAttributeNumber )
563
600
{
564
601
SPI_result = SPI_ERROR_NOATTRIBUTE ;
565
602
return InvalidOid ;
566
603
}
567
604
568
- return tupdesc -> attrs [fnumber - 1 ]-> atttypid ;
605
+ if (fnumber > 0 )
606
+ return tupdesc -> attrs [fnumber - 1 ]-> atttypid ;
607
+ else
608
+ return (SystemAttributeDefinition (fnumber , true))-> atttypid ;
569
609
}
570
610
571
611
char *
0 commit comments