我找了很多的地方,都没有找到合适的例子。最后从PostgreSQL 的代码入手,找到一些例子,于是我做了一个小练习,还不错。
[作者:技术者高健@博客园 mail: luckyjackgao@gmail.com ]
#include "postgres.h" #include "executor/spi.h" #include "utils/builtins.h" #include "catalog/pg_type.h" #ifdef PG_MODULE_MAGIC PG_MODULE_MAGIC; #endif int plantest(); int plantest() { char *command; int ret; int proc; SPI_connect(); //////////////////////////////////////////// static SPIPlanPtr plan_getviewrule = NULL; static const char *query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_class = $1 AND rulename = $2"; /* * On the first call prepare the plan to lookup pg_rewrite. We read * pg_rewrite over the SPI manager instead of using the syscache to be * checked for read access on pg_rewrite. */ if (plan_getviewrule == NULL) { Oid argtypes[2]; SPIPlanPtr plan; argtypes[0] = OIDOID; argtypes[1] = NAMEOID; plan = SPI_prepare(query_getviewrule, 2, argtypes); if (plan == NULL) elog(ERROR, "SPI_prepare failed for \"%s\"", query_getviewrule); SPI_keepplan(plan); plan_getviewrule = plan; } //////////////////////////////////////////// //ret = SPI_exec(command, cnt); //proc = SPI_processed; SPI_finish(); pfree(command); return 0; }
至少它可以运行。
[作者:技术者高健@博客园 mail: luckyjackgao@gmail.com ]