如何在PostgreSQL中获取函数参数?适配Apache AGE的pg-pool改造
获取Cypher函数参数的实现方案
在你现有代码的基础上,要获取Cypher函数的参数,只需利用PostgreSQL FuncCall 结构体的 args 字段——这个字段存储了函数的所有参数列表。具体实现步骤如下:
- 确认找到
"cypher"函数后,直接访问fntree->args获取参数列表 - 遍历参数列表,逐个取出参数节点
- 使用你已经用到的
_outNode函数,将参数节点转换为可读的字符串形式 - 根据需求处理参数(比如存储、日志输出等)
修改后的完整代码片段
if (IsA(node, SelectStmt)) { SelectStmt *stmt = (SelectStmt *) node; List* fromClause = stmt->fromClause; ListCell *fl; //Match the first cypher function call in the FROM clause. Could be multiple tables // e.g. FROM table1, table2, cypher(),table3.... foreach(fl, fromClause) { Node *n = lfirst(fl); if (IsA(n, RangeFunction)) { RangeFunction *rf = (RangeFunction*) n; List* functions = rf->functions; if (functions->length == 1) { List *sublist = (List *) lfirst(list_head(functions)); FuncCall *fntree = (FuncCall *) lfirst(list_head(sublist)); StringInfoData str; initStringInfo(&str); _outNode(&str,fntree->funcname); if (!strcmp("\"cypher\"",str.data)){ // 遍历并处理Cypher函数的参数 ListCell *arg_cell; foreach(arg_cell, fntree->args) { Node *arg_node = lfirst(arg_cell); StringInfoData arg_str; initStringInfo(&arg_str); _outNode(&arg_str, arg_node); // 示例:打印参数到日志,可根据需求替换为存储逻辑 elog(INFO, "Cypher函数参数: %s", arg_str.data); // 释放字符串内存,避免泄漏 pfree(arg_str.data); } return true; } // 释放函数名字符串内存 pfree(str.data); } } } }
关键说明
fntree->args是PostgreSQL定义的FuncCall结构体成员,类型为List*,每个元素对应一个参数的Node节点_outNode函数可以统一处理不同类型的参数节点(比如字符串、数字、参数占位符等),输出其对应的SQL文本形式- 注意在使用完
StringInfoData的data字段后,调用pfree释放内存,避免内存泄漏
内容的提问来源于stack exchange,提问作者Muhammad Zahid
相关产品推荐
相关产品推荐

