对于PostgreSQL中Datum的解释
发布时间:2020-12-13 17:07:50 所属栏目:百科 来源:网络整理
导读:Datum类型是PG系统函数大量引用的类型,其定义为: typedef uintptr_t Datum;typedef unsigned long long uintptr; 这里举一个比较简单的例子进行解释: Datumcstring_in(PG_FUNCTION_ARGS){ char *str = PG_GETARG_CSTRING(0); PG_RETURN_CSTRING(pstrdup(st
Datum类型是PG系统函数大量引用的类型,其定义为:
typedef uintptr_t Datum; typedef unsigned long long uintptr; 这里举一个比较简单的例子进行解释: Datum cstring_in(PG_FUNCTION_ARGS) { char *str = PG_GETARG_CSTRING(0); PG_RETURN_CSTRING(pstrdup(str)); } /* * cstring_out - output routine for pseudo-type CSTRING. * * We allow this mainly so that "SELECT some_output_function(...)" does * what the user will expect. */ Datum cstring_out(PG_FUNCTION_ARGS) { char *str = PG_GETARG_CSTRING(0); PG_RETURN_CSTRING(pstrdup(str)); } 这里主要说明的是PG_GETARG_***和PG_RETURN_***。 这些函数的定义可以在fmgr.h中看到,时间类型的需要在其头文件看到。 以PG_GETARG_CSTRING来说明一下,其定义为: #define PG_GETARG_CSTRING(n) DatumGetCString(PG_GETARG_DATUM(n)) #define PG_GETARG_DATUM(n) (fcinfo->arg[n]) #define DatumGetCString(X) ((char *) DatumGetPointer(X)) #define DatumGetPointer(X) ((Pointer) (X))PG_GETARG_DATUM是获取参数中第一个Datum类型的值。 PG_GETARG_CSTRING是将Datum这种类型转换为字符指针类型,基本都是用char *来转化。 下面看一下: #define PG_RETURN_CSTRING(x) return CStringGetDatum(x) #define CStringGetDatum(X) PointerGetDatum(X) #define PointerGetDatum(X) ((Datum) (X))和上面的类似也是对类型的转换,这里是将字符指针类型转换为无符号长整形。 所以PG内部进行传参实际上并没有对指针进行传递,而是将指针转化为整形,然后进行传递。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |