Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Gpdb
提交
d9eb7d8f
G
Gpdb
项目概览
Greenplum
/
Gpdb
通知
7
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Gpdb
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d9eb7d8f
编写于
8月 24, 2000
作者:
T
Tom Lane
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update funcs tutorial for new function manager.
上级
782c16c6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
183 addition
and
38 deletion
+183
-38
src/tutorial/funcs.c
src/tutorial/funcs.c
+67
-38
src/tutorial/funcs_new.c
src/tutorial/funcs_new.c
+116
-0
未找到文件。
src/tutorial/funcs.c
浏览文件 @
d9eb7d8f
...
...
@@ -4,75 +4,104 @@
The calling format for these functions is defined by the CREATE FUNCTION
SQL statement that binds them to the backend.
NOTE: this file shows examples of "old style" function call conventions.
See funcs_new.c for examples of "new style".
*****************************************************************************/
#include "postgres.h"
/* for variable length type */
#include <string.h>
#include "postgres.h"
/* general Postgres declarations */
#include "executor/executor.h"
/* for GetAttributeByName() */
#include "utils/geo_decls.h"
/* for point type */
/* The following prototypes declare what we assume the user declares to
Postgres in his CREATE FUNCTION statement.
*/
/* These prototypes just prevent possible warnings from gcc. */
int
add_one
(
int
arg
);
float8
*
add_one_float8
(
float8
*
arg
);
Point
*
makepoint
(
Point
*
pointx
,
Point
*
pointy
);
text
*
copytext
(
text
*
t
);
text
*
concat_text
(
text
*
arg1
,
text
*
arg2
);
bool
c_overpaid
(
TupleTableSlot
*
t
,
/* the current instance of EMP */
int4
limit
);
int32
limit
);
/* By Value */
int
add_one
(
int
arg
)
{
return
arg
+
1
;
return
arg
+
1
;
}
/* By Reference, Fixed Length */
float8
*
add_one_float8
(
float8
*
arg
)
{
float8
*
result
=
(
float8
*
)
palloc
(
sizeof
(
float8
));
*
result
=
*
arg
+
1
.
0
;
return
result
;
}
Point
*
makepoint
(
Point
*
pointx
,
Point
*
pointy
)
{
Point
*
new_point
=
(
Point
*
)
palloc
(
sizeof
(
Point
));
Point
*
new_point
=
(
Point
*
)
palloc
(
sizeof
(
Point
));
new_point
->
x
=
pointx
->
x
;
new_point
->
y
=
pointy
->
y
;
return
new_point
;
new_point
->
x
=
pointx
->
x
;
new_point
->
y
=
pointy
->
y
;
return
new_point
;
}
/* By Reference, Variable Length */
text
*
copytext
(
text
*
t
)
{
/*
* VARSIZE is the total size of the struct in bytes.
*/
text
*
new_t
=
(
text
*
)
palloc
(
VARSIZE
(
t
));
VARATT_SIZEP
(
new_t
)
=
VARSIZE
(
t
);
/*
* VARDATA is a pointer to the data region of the struct.
*/
memcpy
((
void
*
)
VARDATA
(
new_t
),
/* destination */
(
void
*
)
VARDATA
(
t
),
/* source */
VARSIZE
(
t
)
-
VARHDRSZ
);
/* how many bytes */
return
new_t
;
}
/*
* VARSIZE is the total size of the struct in bytes.
*/
text
*
new_t
=
(
text
*
)
palloc
(
VARSIZE
(
t
));
MemSet
(
new_t
,
0
,
VARSIZE
(
t
));
VARSIZE
(
new_t
)
=
VARSIZE
(
t
);
/*
* VARDATA is a pointer to the data region of the struct.
*/
memcpy
((
void
*
)
VARDATA
(
new_t
),
/* destination */
(
void
*
)
VARDATA
(
t
),
/* source */
VARSIZE
(
t
)
-
VARHDRSZ
);
/* how many bytes */
return
new_t
;
text
*
concat_text
(
text
*
arg1
,
text
*
arg2
)
{
int32
new_text_size
=
VARSIZE
(
arg1
)
+
VARSIZE
(
arg2
)
-
VARHDRSZ
;
text
*
new_text
=
(
text
*
)
palloc
(
new_text_size
);
memset
((
void
*
)
new_text
,
0
,
new_text_size
);
VARATT_SIZEP
(
new_text
)
=
new_text_size
;
strncpy
(
VARDATA
(
new_text
),
VARDATA
(
arg1
),
VARSIZE
(
arg1
)
-
VARHDRSZ
);
strncat
(
VARDATA
(
new_text
),
VARDATA
(
arg2
),
VARSIZE
(
arg2
)
-
VARHDRSZ
);
return
new_text
;
}
/* Composite types */
bool
c_overpaid
(
TupleTableSlot
*
t
,
/* the current instance of EMP */
int
4
limit
)
int
32
limit
)
{
bool
isnull
=
false
;
int4
salary
;
salary
=
(
int4
)
GetAttributeByName
(
t
,
"salary"
,
&
isnull
);
bool
isnull
;
int32
salary
;
if
(
isnull
)
return
false
;
return
salary
>
limit
;
salary
=
DatumGetInt32
(
GetAttributeByName
(
t
,
"salary"
,
&
isnull
));
if
(
isnull
)
return
(
false
);
return
salary
>
limit
;
}
src/tutorial/funcs_new.c
0 → 100644
浏览文件 @
d9eb7d8f
/******************************************************************************
These are user-defined functions that can be bound to a Postgres backend
and called by Postgres to execute SQL functions of the same name.
The calling format for these functions is defined by the CREATE FUNCTION
SQL statement that binds them to the backend.
NOTE: this file shows examples of "new style" function call conventions.
See funcs.c for examples of "old style".
*****************************************************************************/
#include <string.h>
#include "postgres.h"
/* general Postgres declarations */
#include "fmgr.h"
/* for argument/result macros */
#include "executor/executor.h"
/* for GetAttributeByName() */
#include "utils/geo_decls.h"
/* for point type */
/* These prototypes just prevent possible warnings from gcc. */
Datum
add_one
(
PG_FUNCTION_ARGS
);
Datum
add_one_float8
(
PG_FUNCTION_ARGS
);
Datum
makepoint
(
PG_FUNCTION_ARGS
);
Datum
copytext
(
PG_FUNCTION_ARGS
);
Datum
concat_text
(
PG_FUNCTION_ARGS
);
Datum
c_overpaid
(
PG_FUNCTION_ARGS
);
/* By Value */
Datum
add_one
(
PG_FUNCTION_ARGS
)
{
int32
arg
=
PG_GETARG_INT32
(
0
);
PG_RETURN_INT32
(
arg
+
1
);
}
/* By Reference, Fixed Length */
Datum
add_one_float8
(
PG_FUNCTION_ARGS
)
{
/* The macros for FLOAT8 hide its pass-by-reference nature */
float8
arg
=
PG_GETARG_FLOAT8
(
0
);
PG_RETURN_FLOAT8
(
arg
+
1
.
0
);
}
Datum
makepoint
(
PG_FUNCTION_ARGS
)
{
Point
*
pointx
=
PG_GETARG_POINT_P
(
0
);
Point
*
pointy
=
PG_GETARG_POINT_P
(
1
);
Point
*
new_point
=
(
Point
*
)
palloc
(
sizeof
(
Point
));
new_point
->
x
=
pointx
->
x
;
new_point
->
y
=
pointy
->
y
;
PG_RETURN_POINT_P
(
new_point
);
}
/* By Reference, Variable Length */
Datum
copytext
(
PG_FUNCTION_ARGS
)
{
text
*
t
=
PG_GETARG_TEXT_P
(
0
);
/*
* VARSIZE is the total size of the struct in bytes.
*/
text
*
new_t
=
(
text
*
)
palloc
(
VARSIZE
(
t
));
VARATT_SIZEP
(
new_t
)
=
VARSIZE
(
t
);
/*
* VARDATA is a pointer to the data region of the struct.
*/
memcpy
((
void
*
)
VARDATA
(
new_t
),
/* destination */
(
void
*
)
VARDATA
(
t
),
/* source */
VARSIZE
(
t
)
-
VARHDRSZ
);
/* how many bytes */
PG_RETURN_TEXT_P
(
new_t
);
}
Datum
concat_text
(
PG_FUNCTION_ARGS
)
{
text
*
arg1
=
PG_GETARG_TEXT_P
(
0
);
text
*
arg2
=
PG_GETARG_TEXT_P
(
1
);
int32
new_text_size
=
VARSIZE
(
arg1
)
+
VARSIZE
(
arg2
)
-
VARHDRSZ
;
text
*
new_text
=
(
text
*
)
palloc
(
new_text_size
);
memset
((
void
*
)
new_text
,
0
,
new_text_size
);
VARATT_SIZEP
(
new_text
)
=
new_text_size
;
strncpy
(
VARDATA
(
new_text
),
VARDATA
(
arg1
),
VARSIZE
(
arg1
)
-
VARHDRSZ
);
strncat
(
VARDATA
(
new_text
),
VARDATA
(
arg2
),
VARSIZE
(
arg2
)
-
VARHDRSZ
);
PG_RETURN_TEXT_P
(
new_text
);
}
/* Composite types */
Datum
c_overpaid
(
PG_FUNCTION_ARGS
)
{
TupleTableSlot
*
t
=
(
TupleTableSlot
*
)
PG_GETARG_POINTER
(
0
);
int32
limit
=
PG_GETARG_INT32
(
1
);
bool
isnull
;
int32
salary
;
salary
=
DatumGetInt32
(
GetAttributeByName
(
t
,
"salary"
,
&
isnull
));
if
(
isnull
)
PG_RETURN_BOOL
(
false
);
/* Alternatively, we might prefer to do PG_RETURN_NULL() for null salary */
PG_RETURN_BOOL
(
salary
>
limit
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录