27
27
28
28
#include "dblink.h"
29
29
30
+
31
+ /*
32
+ * Internal declarations
33
+ */
34
+ static dblink_results * init_dblink_results (MemoryContext fn_mcxt );
35
+ static dblink_array_results * init_dblink_array_results (MemoryContext fn_mcxt );
36
+ static char * * get_pkey_attnames (Oid relid ,int16 * numatts );
37
+ static char * get_strtok (char * fldtext ,char * fldsep ,int fldnum );
38
+ static char * get_sql_insert (Oid relid ,int16 * pkattnums ,int16 pknumatts ,char * * src_pkattvals ,char * * tgt_pkattvals );
39
+ static char * get_sql_delete (Oid relid ,int16 * pkattnums ,int16 pknumatts ,char * * tgt_pkattvals );
40
+ static char * get_sql_update (Oid relid ,int16 * pkattnums ,int16 pknumatts ,char * * src_pkattvals ,char * * tgt_pkattvals );
41
+ static char * quote_literal_cstr (char * rawstr );
42
+ static char * quote_ident_cstr (char * rawstr );
43
+ static int16 get_attnum_pk_pos (int16 * pkattnums ,int16 pknumatts ,int16 key );
44
+ static HeapTuple get_tuple_of_interest (Oid relid ,int16 * pkattnums ,int16 pknumatts ,char * * src_pkattvals );
45
+ static Oid get_relid_from_relname (text * relname_text );
46
+ static dblink_results * get_res_ptr (int32 res_id_index );
47
+ static void append_res_ptr (dblink_results * results );
48
+ static void remove_res_ptr (dblink_results * results );
49
+
30
50
/* Global */
31
51
List * res_id = NIL ;
32
52
int res_id_index = 0 ;
@@ -281,7 +301,7 @@ PG_FUNCTION_INFO_V1(dblink_get_pkey);
281
301
Datum
282
302
dblink_get_pkey (PG_FUNCTION_ARGS )
283
303
{
284
- char * relname ;
304
+ text * relname_text ;
285
305
Oid relid ;
286
306
char * * result ;
287
307
text * result_text ;
@@ -294,15 +314,14 @@ dblink_get_pkey(PG_FUNCTION_ARGS)
294
314
295
315
if (fcinfo -> flinfo -> fn_extra == NULL )
296
316
{
297
- relname = NameStr ( * PG_GETARG_NAME ( 0 ) );
317
+ relname_text = PG_GETARG_TEXT_P ( 0 );
298
318
299
319
/*
300
320
* Convert relname to rel OID.
301
321
*/
302
- relid = get_relid_from_relname (relname );
322
+ relid = get_relid_from_relname (relname_text );
303
323
if (!OidIsValid (relid ))
304
- elog (ERROR ,"dblink_get_pkey: relation \"%s\" does not exist" ,
305
- relname );
324
+ elog (ERROR ,"dblink_get_pkey: relation does not exist" );
306
325
307
326
/*
308
327
* get an array of attnums.
@@ -428,7 +447,7 @@ Datum
428
447
dblink_build_sql_insert (PG_FUNCTION_ARGS )
429
448
{
430
449
Oid relid ;
431
- char * relname ;
450
+ text * relname_text ;
432
451
int16 * pkattnums ;
433
452
int16 pknumatts ;
434
453
char * * src_pkattvals ;
@@ -446,15 +465,14 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS)
446
465
char * sql ;
447
466
text * sql_text ;
448
467
449
- relname = NameStr ( * PG_GETARG_NAME ( 0 ) );
468
+ relname_text = PG_GETARG_TEXT_P ( 0 );
450
469
451
470
/*
452
471
* Convert relname to rel OID.
453
472
*/
454
- relid = get_relid_from_relname (relname );
473
+ relid = get_relid_from_relname (relname_text );
455
474
if (!OidIsValid (relid ))
456
- elog (ERROR ,"dblink_get_pkey: relation \"%s\" does not exist" ,
457
- relname );
475
+ elog (ERROR ,"dblink_build_sql_insert: relation does not exist" );
458
476
459
477
pkattnums = (int16 * )PG_GETARG_POINTER (1 );
460
478
pknumatts = PG_GETARG_INT16 (2 );
@@ -554,7 +572,7 @@ Datum
554
572
dblink_build_sql_delete (PG_FUNCTION_ARGS )
555
573
{
556
574
Oid relid ;
557
- char * relname ;
575
+ text * relname_text ;
558
576
int16 * pkattnums ;
559
577
int16 pknumatts ;
560
578
char * * tgt_pkattvals ;
@@ -567,15 +585,14 @@ dblink_build_sql_delete(PG_FUNCTION_ARGS)
567
585
char * sql ;
568
586
text * sql_text ;
569
587
570
- relname = NameStr ( * PG_GETARG_NAME ( 0 ) );
588
+ relname_text = PG_GETARG_TEXT_P ( 0 );
571
589
572
590
/*
573
591
* Convert relname to rel OID.
574
592
*/
575
- relid = get_relid_from_relname (relname );
593
+ relid = get_relid_from_relname (relname_text );
576
594
if (!OidIsValid (relid ))
577
- elog (ERROR ,"dblink_get_pkey: relation \"%s\" does not exist" ,
578
- relname );
595
+ elog (ERROR ,"dblink_build_sql_delete: relation does not exist" );
579
596
580
597
pkattnums = (int16 * )PG_GETARG_POINTER (1 );
581
598
pknumatts = PG_GETARG_INT16 (2 );
@@ -653,7 +670,7 @@ Datum
653
670
dblink_build_sql_update (PG_FUNCTION_ARGS )
654
671
{
655
672
Oid relid ;
656
- char * relname ;
673
+ text * relname_text ;
657
674
int16 * pkattnums ;
658
675
int16 pknumatts ;
659
676
char * * src_pkattvals ;
@@ -671,15 +688,14 @@ dblink_build_sql_update(PG_FUNCTION_ARGS)
671
688
char * sql ;
672
689
text * sql_text ;
673
690
674
- relname = NameStr ( * PG_GETARG_NAME ( 0 ) );
691
+ relname_text = PG_GETARG_TEXT_P ( 0 );
675
692
676
693
/*
677
694
* Convert relname to rel OID.
678
695
*/
679
- relid = get_relid_from_relname (relname );
696
+ relid = get_relid_from_relname (relname_text );
680
697
if (!OidIsValid (relid ))
681
- elog (ERROR ,"dblink_get_pkey: relation \"%s\" does not exist" ,
682
- relname );
698
+ elog (ERROR ,"dblink_build_sql_update: relation does not exist" );
683
699
684
700
pkattnums = (int16 * )PG_GETARG_POINTER (1 );
685
701
pknumatts = PG_GETARG_INT16 (2 );
@@ -841,7 +857,7 @@ dblink_replace_text(PG_FUNCTION_ARGS)
841
857
* init_dblink_results
842
858
* - create an empty dblink_results data structure
843
859
*/
844
- dblink_results *
860
+ static dblink_results *
845
861
init_dblink_results (MemoryContext fn_mcxt )
846
862
{
847
863
MemoryContext oldcontext ;
@@ -866,7 +882,7 @@ init_dblink_results(MemoryContext fn_mcxt)
866
882
* init_dblink_array_results
867
883
* - create an empty dblink_array_results data structure
868
884
*/
869
- dblink_array_results *
885
+ static dblink_array_results *
870
886
init_dblink_array_results (MemoryContext fn_mcxt )
871
887
{
872
888
MemoryContext oldcontext ;
@@ -892,7 +908,7 @@ init_dblink_array_results(MemoryContext fn_mcxt)
892
908
* Get the primary key attnames for the given relation.
893
909
* Return NULL, and set numatts = 0, if no primary key exists.
894
910
*/
895
- char * *
911
+ static char * *
896
912
get_pkey_attnames (Oid relid ,int16 * numatts )
897
913
{
898
914
Relation indexRelation ;
@@ -961,7 +977,7 @@ get_pkey_attnames(Oid relid, int16 *numatts)
961
977
* return ord item (0 based)
962
978
* based on provided field separator
963
979
*/
964
- char *
980
+ static char *
965
981
get_strtok (char * fldtext ,char * fldsep ,int fldnum )
966
982
{
967
983
int j = 0 ;
@@ -988,7 +1004,7 @@ get_strtok(char *fldtext, char *fldsep, int fldnum)
988
1004
return pstrdup (result );
989
1005
}
990
1006
991
- char *
1007
+ static char *
992
1008
get_sql_insert (Oid relid ,int16 * pkattnums ,int16 pknumatts ,char * * src_pkattvals ,char * * tgt_pkattvals )
993
1009
{
994
1010
Relation rel ;
@@ -1059,7 +1075,7 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
1059
1075
return (sql );
1060
1076
}
1061
1077
1062
- char *
1078
+ static char *
1063
1079
get_sql_delete (Oid relid ,int16 * pkattnums ,int16 pknumatts ,char * * tgt_pkattvals )
1064
1080
{
1065
1081
Relation rel ;
@@ -1112,7 +1128,7 @@ get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattval
1112
1128
return (sql );
1113
1129
}
1114
1130
1115
- char *
1131
+ static char *
1116
1132
get_sql_update (Oid relid ,int16 * pkattnums ,int16 pknumatts ,char * * src_pkattvals ,char * * tgt_pkattvals )
1117
1133
{
1118
1134
Relation rel ;
@@ -1235,7 +1251,7 @@ quote_ident_cstr(char *rawstr)
1235
1251
return result ;
1236
1252
}
1237
1253
1238
- int16
1254
+ static int16
1239
1255
get_attnum_pk_pos (int16 * pkattnums ,int16 pknumatts ,int16 key )
1240
1256
{
1241
1257
int i ;
@@ -1251,7 +1267,7 @@ get_attnum_pk_pos(int16 *pkattnums, int16 pknumatts, int16 key)
1251
1267
return -1 ;
1252
1268
}
1253
1269
1254
- HeapTuple
1270
+ static HeapTuple
1255
1271
get_tuple_of_interest (Oid relid ,int16 * pkattnums ,int16 pknumatts ,char * * src_pkattvals )
1256
1272
{
1257
1273
Relation rel ;
@@ -1340,17 +1356,24 @@ get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_p
1340
1356
return NULL ;
1341
1357
}
1342
1358
1343
- Oid
1344
- get_relid_from_relname (char * relname )
1359
+ static Oid
1360
+ get_relid_from_relname (text * relname_text )
1345
1361
{
1346
1362
#ifdef NamespaceRelationName
1347
- Oid relid ;
1363
+ RangeVar * relvar ;
1364
+ Relation rel ;
1365
+ Oid relid ;
1348
1366
1349
- relid = RelnameGetRelid (relname );
1367
+ relvar = makeRangeVarFromNameList (textToQualifiedNameList (relname_text ,"get_relid_from_relname" ));
1368
+ rel = heap_openrv (relvar ,AccessShareLock );
1369
+ relid = RelationGetRelid (rel );
1370
+ relation_close (rel ,AccessShareLock );
1350
1371
#else
1351
- Relation rel ;
1352
- Oid relid ;
1372
+ char * relname ;
1373
+ Relation rel ;
1374
+ Oid relid ;
1353
1375
1376
+ relname = DatumGetCString (DirectFunctionCall1 (textout ,PointerGetDatum (relname_text )));
1354
1377
rel = relation_openr (relname ,AccessShareLock );
1355
1378
relid = RelationGetRelid (rel );
1356
1379
relation_close (rel ,AccessShareLock );
@@ -1359,7 +1382,7 @@ get_relid_from_relname(char *relname)
1359
1382
return relid ;
1360
1383
}
1361
1384
1362
- dblink_results *
1385
+ static dblink_results *
1363
1386
get_res_ptr (int32 res_id_index )
1364
1387
{
1365
1388
List * ptr ;
@@ -1385,7 +1408,7 @@ get_res_ptr(int32 res_id_index)
1385
1408
/*
1386
1409
* Add node to global List res_id
1387
1410
*/
1388
- void
1411
+ static void
1389
1412
append_res_ptr (dblink_results * results )
1390
1413
{
1391
1414
res_id = lappend (res_id ,results );
@@ -1395,7 +1418,7 @@ append_res_ptr(dblink_results *results)
1395
1418
* Remove node from global List
1396
1419
* using res_id_index
1397
1420
*/
1398
- void
1421
+ static void
1399
1422
remove_res_ptr (dblink_results * results )
1400
1423
{
1401
1424
res_id = lremove (results ,res_id );
@@ -1404,4 +1427,3 @@ remove_res_ptr(dblink_results *results)
1404
1427
res_id_index = 0 ;
1405
1428
}
1406
1429
1407
-