@@ -48,7 +48,7 @@ static void _SPI_fetch(FetchStmt * stmt);
48
48
#endif
49
49
static int
50
50
_SPI_execute_plan (_SPI_plan * plan ,
51
- Datum * Values , char * Nulls , int tcount );
51
+ Datum * Values , char * Nulls , int tcount );
52
52
53
53
#define _SPI_CPLAN_CURCXT 0
54
54
#define _SPI_CPLAN_PROCXT 1
@@ -199,7 +199,7 @@ SPI_exec(char *src, int tcount)
199
199
}
200
200
201
201
int
202
- SPI_execp (void * plan , Datum * Values , char * Nulls , int tcount )
202
+ SPI_execp (void * plan , Datum * Values , char * Nulls , int tcount )
203
203
{
204
204
int res ;
205
205
@@ -278,11 +278,108 @@ SPI_saveplan(void *plan)
278
278
279
279
}
280
280
281
+ HeapTuple
282
+ SPI_copytuple (HeapTuple tuple )
283
+ {
284
+ MemoryContext oldcxt = NULL ;
285
+ HeapTuple ctuple ;
286
+
287
+ if (tuple == NULL )
288
+ {
289
+ SPI_result = SPI_ERROR_ARGUMENT ;
290
+ return (NULL );
291
+ }
292
+
293
+ if (_SPI_curid + 1 == _SPI_connected ) /* connected */
294
+ {
295
+ if (_SPI_current != & (_SPI_stack [_SPI_curid + 1 ]))
296
+ elog (FATAL , "SPI: stack corrupted" );
297
+ oldcxt = MemoryContextSwitchTo (_SPI_current -> savedcxt );
298
+ }
299
+
300
+ ctuple = heap_copytuple (tuple );
301
+
302
+ if (oldcxt )
303
+ MemoryContextSwitchTo (oldcxt );
304
+
305
+ return (ctuple );
306
+ }
307
+
308
+ HeapTuple
309
+ SPI_modifytuple (Relation rel , HeapTuple tuple , int natts , int * attnum ,
310
+ Datum * Values , char * Nulls )
311
+ {
312
+ MemoryContext oldcxt = NULL ;
313
+ HeapTuple mtuple ;
314
+ int numberOfAttributes ;
315
+ uint8 infomask ;
316
+ Datum * v ;
317
+ char * n ;
318
+ bool isnull ;
319
+ int i ;
320
+
321
+ if (rel == NULL || tuple == NULL || natts <= 0 || attnum == NULL || Values == NULL )
322
+ {
323
+ SPI_result = SPI_ERROR_ARGUMENT ;
324
+ return (NULL );
325
+ }
326
+
327
+ if (_SPI_curid + 1 == _SPI_connected ) /* connected */
328
+ {
329
+ if (_SPI_current != & (_SPI_stack [_SPI_curid + 1 ]))
330
+ elog (FATAL , "SPI: stack corrupted" );
331
+ oldcxt = MemoryContextSwitchTo (_SPI_current -> savedcxt );
332
+ }
333
+ SPI_result = 0 ;
334
+ numberOfAttributes = rel -> rd_att -> natts ;
335
+ v = (Datum * ) palloc (numberOfAttributes * sizeof (Datum ));
336
+ n = (char * ) palloc (numberOfAttributes * sizeof (char ));
337
+
338
+ /* fetch old values and nulls */
339
+ for (i = 0 ; i < numberOfAttributes ; i ++ )
340
+ {
341
+ v [i ] = heap_getattr (tuple , InvalidBuffer , i + 1 , rel -> rd_att , & isnull );
342
+ n [i ] = (isnull ) ? 'n' : ' ' ;
343
+ }
344
+
345
+ /* replace values and nulls */
346
+ for (i = 0 ; i < natts ; i ++ )
347
+ {
348
+ if (attnum [i ] <= 0 || attnum [i ] > numberOfAttributes )
349
+ break ;
350
+ v [attnum [i ] - 1 ] = Values [i ];
351
+ n [attnum [i ] - 1 ] = (Nulls && Nulls [i ] == 'n' ) ? 'n' : ' ' ;
352
+ }
353
+
354
+ if (i == natts ) /* no errors in attnum[] */
355
+ {
356
+ mtuple = heap_formtuple (rel -> rd_att , v , n );
357
+ infomask = mtuple -> t_infomask ;
358
+ memmove (& (mtuple -> t_ctid ), & (tuple -> t_ctid ),
359
+ ((char * ) & (tuple -> t_hoff ) - (char * ) & (tuple -> t_ctid )));
360
+ mtuple -> t_infomask = infomask ;
361
+ mtuple -> t_natts = numberOfAttributes ;
362
+ }
363
+ else
364
+ {
365
+ mtuple = NULL ;
366
+ SPI_result = SPI_ERROR_NOATTRIBUTE ;
367
+ }
368
+
369
+ pfree (v );
370
+ pfree (n );
371
+
372
+ if (oldcxt )
373
+ MemoryContextSwitchTo (oldcxt );
374
+
375
+ return (mtuple );
376
+ }
377
+
281
378
int
282
379
SPI_fnumber (TupleDesc tupdesc , char * fname )
283
380
{
284
381
int res ;
285
-
382
+
286
383
for (res = 0 ; res < tupdesc -> natts ; res ++ )
287
384
{
288
385
if (strcasecmp (tupdesc -> attrs [res ]-> attname .data , fname ) == 0 )
@@ -333,7 +430,7 @@ SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
333
430
Datum
334
431
SPI_getbinval (HeapTuple tuple , TupleDesc tupdesc , int fnumber , bool * isnull )
335
432
{
336
- Datum val ;
433
+ Datum val ;
337
434
338
435
* isnull = true;
339
436
SPI_result = 0 ;
@@ -539,7 +636,7 @@ _SPI_execute(char *src, int tcount, _SPI_plan * plan)
539
636
}
540
637
541
638
static int
542
- _SPI_execute_plan (_SPI_plan * plan , Datum * Values , char * Nulls , int tcount )
639
+ _SPI_execute_plan (_SPI_plan * plan , Datum * Values , char * Nulls , int tcount )
543
640
{
544
641
QueryTreeList * queryTree_list = plan -> qtlist ;
545
642
List * planTree_list = plan -> ptlist ;
@@ -591,7 +688,7 @@ _SPI_execute_plan(_SPI_plan * plan, Datum *Values, char *Nulls, int tcount)
591
688
{
592
689
paramLI -> kind = PARAM_NUM ;
593
690
paramLI -> id = k + 1 ;
594
- paramLI -> isnull = (Nulls != NULL && Nulls [k ] ! = 'n' );
691
+ paramLI -> isnull = (Nulls && Nulls [k ] = = 'n' );
595
692
paramLI -> value = Values [k ];
596
693
}
597
694
paramLI -> kind = PARAM_INVALID ;
0 commit comments