Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Gpdb
提交
11d33718
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,发现更多精彩内容 >>
提交
11d33718
编写于
11月 23, 2002
作者:
T
Tom Lane
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix inappropriate quoting in dblink. From Joe Conway.
上级
c3637205
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
116 addition
and
12 deletion
+116
-12
contrib/dblink/README.dblink
contrib/dblink/README.dblink
+4
-0
contrib/dblink/dblink.c
contrib/dblink/dblink.c
+55
-12
contrib/dblink/expected/dblink.out
contrib/dblink/expected/dblink.out
+37
-0
contrib/dblink/sql/dblink.sql
contrib/dblink/sql/dblink.sql
+20
-0
未找到文件。
contrib/dblink/README.dblink
浏览文件 @
11d33718
...
...
@@ -151,6 +151,10 @@ Installation:
Documentation:
Note: Parameters representing relation names must include double
quotes if the names are mixed-case or contain special characters. They
must also be appropriately qualified with schema name if applicable.
See the following files:
doc/connection
doc/cursor
...
...
contrib/dblink/dblink.c
浏览文件 @
11d33718
...
...
@@ -71,6 +71,7 @@ static dblink_results *get_res_ptr(int32 res_id_index);
static
void
append_res_ptr
(
dblink_results
*
results
);
static
void
remove_res_ptr
(
dblink_results
*
results
);
static
TupleDesc
pgresultGetTupleDesc
(
PGresult
*
res
);
static
char
*
generate_relation_name
(
Oid
relid
);
/* Global */
List
*
res_id
=
NIL
;
...
...
@@ -171,7 +172,7 @@ dblink_open(PG_FUNCTION_ARGS)
}
PQclear
(
res
);
appendStringInfo
(
str
,
"DECLARE %s CURSOR FOR %s"
,
quote_ident_cstr
(
curname
)
,
sql
);
appendStringInfo
(
str
,
"DECLARE %s CURSOR FOR %s"
,
curname
,
sql
);
res
=
PQexec
(
conn
,
str
->
data
);
if
(
!
res
||
(
PQresultStatus
(
res
)
!=
PGRES_COMMAND_OK
&&
...
...
@@ -210,7 +211,7 @@ dblink_close(PG_FUNCTION_ARGS)
else
elog
(
ERROR
,
"dblink_close: no connection available"
);
appendStringInfo
(
str
,
"CLOSE %s"
,
quote_ident_cstr
(
curname
)
);
appendStringInfo
(
str
,
"CLOSE %s"
,
curname
);
/* close the cursor */
res
=
PQexec
(
conn
,
str
->
data
);
...
...
@@ -287,7 +288,7 @@ dblink_fetch(PG_FUNCTION_ARGS)
else
elog
(
ERROR
,
"dblink_fetch: no connection available"
);
appendStringInfo
(
str
,
"FETCH %d FROM %s"
,
howmany
,
quote_ident_cstr
(
curname
)
);
appendStringInfo
(
str
,
"FETCH %d FROM %s"
,
howmany
,
curname
);
res
=
PQexec
(
conn
,
str
->
data
);
if
(
!
res
||
...
...
@@ -306,7 +307,7 @@ dblink_fetch(PG_FUNCTION_ARGS)
{
/* cursor does not exist - closed already or bad name */
PQclear
(
res
);
elog
(
ERROR
,
"dblink_fetch: cursor %s does not exist"
,
quote_ident_cstr
(
curname
)
);
elog
(
ERROR
,
"dblink_fetch: cursor %s does not exist"
,
curname
);
}
funcctx
->
max_calls
=
PQntuples
(
res
);
...
...
@@ -1527,11 +1528,13 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
int
i
;
bool
needComma
;
/* get relation name including any needed schema prefix and quoting */
relname
=
generate_relation_name
(
relid
);
/*
* Open relation using relid
*/
rel
=
relation_open
(
relid
,
AccessShareLock
);
relname
=
RelationGetRelationName
(
rel
);
tupdesc
=
rel
->
rd_att
;
natts
=
tupdesc
->
natts
;
...
...
@@ -1539,7 +1542,7 @@ get_sql_insert(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
if
(
!
tuple
)
elog
(
ERROR
,
"dblink_build_sql_insert: row not found"
);
appendStringInfo
(
str
,
"INSERT INTO %s("
,
quote_ident_cstr
(
relname
)
);
appendStringInfo
(
str
,
"INSERT INTO %s("
,
relname
);
needComma
=
false
;
for
(
i
=
0
;
i
<
natts
;
i
++
)
...
...
@@ -1610,15 +1613,17 @@ get_sql_delete(Oid relid, int16 *pkattnums, int16 pknumatts, char **tgt_pkattval
char
*
val
;
int
i
;
/* get relation name including any needed schema prefix and quoting */
relname
=
generate_relation_name
(
relid
);
/*
* Open relation using relid
*/
rel
=
relation_open
(
relid
,
AccessShareLock
);
relname
=
RelationGetRelationName
(
rel
);
tupdesc
=
rel
->
rd_att
;
natts
=
tupdesc
->
natts
;
appendStringInfo
(
str
,
"DELETE FROM %s WHERE "
,
quote_ident_cstr
(
relname
)
);
appendStringInfo
(
str
,
"DELETE FROM %s WHERE "
,
relname
);
for
(
i
=
0
;
i
<
pknumatts
;
i
++
)
{
int16
pkattnum
=
pkattnums
[
i
];
...
...
@@ -1669,11 +1674,13 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
int
i
;
bool
needComma
;
/* get relation name including any needed schema prefix and quoting */
relname
=
generate_relation_name
(
relid
);
/*
* Open relation using relid
*/
rel
=
relation_open
(
relid
,
AccessShareLock
);
relname
=
RelationGetRelationName
(
rel
);
tupdesc
=
rel
->
rd_att
;
natts
=
tupdesc
->
natts
;
...
...
@@ -1681,7 +1688,7 @@ get_sql_update(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_pkattval
if
(
!
tuple
)
elog
(
ERROR
,
"dblink_build_sql_update: row not found"
);
appendStringInfo
(
str
,
"UPDATE %s SET "
,
quote_ident_cstr
(
relname
)
);
appendStringInfo
(
str
,
"UPDATE %s SET "
,
relname
);
needComma
=
false
;
for
(
i
=
0
;
i
<
natts
;
i
++
)
...
...
@@ -1813,11 +1820,13 @@ get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_p
int
i
;
char
*
val
=
NULL
;
/* get relation name including any needed schema prefix and quoting */
relname
=
generate_relation_name
(
relid
);
/*
* Open relation using relid
*/
rel
=
relation_open
(
relid
,
AccessShareLock
);
relname
=
RelationGetRelationName
(
rel
);
tupdesc
=
CreateTupleDescCopy
(
rel
->
rd_att
);
relation_close
(
rel
,
AccessShareLock
);
...
...
@@ -1831,7 +1840,7 @@ get_tuple_of_interest(Oid relid, int16 *pkattnums, int16 pknumatts, char **src_p
* Build sql statement to look up tuple of interest Use src_pkattvals
* as the criteria.
*/
appendStringInfo
(
str
,
"SELECT * FROM %s WHERE "
,
quote_ident_cstr
(
relname
)
);
appendStringInfo
(
str
,
"SELECT * FROM %s WHERE "
,
relname
);
for
(
i
=
0
;
i
<
pknumatts
;
i
++
)
{
...
...
@@ -2003,3 +2012,37 @@ pgresultGetTupleDesc(PGresult *res)
return
desc
;
}
/*
* generate_relation_name - copied from ruleutils.c
* Compute the name to display for a relation specified by OID
*
* The result includes all necessary quoting and schema-prefixing.
*/
static
char
*
generate_relation_name
(
Oid
relid
)
{
HeapTuple
tp
;
Form_pg_class
reltup
;
char
*
nspname
;
char
*
result
;
tp
=
SearchSysCache
(
RELOID
,
ObjectIdGetDatum
(
relid
),
0
,
0
,
0
);
if
(
!
HeapTupleIsValid
(
tp
))
elog
(
ERROR
,
"cache lookup of relation %u failed"
,
relid
);
reltup
=
(
Form_pg_class
)
GETSTRUCT
(
tp
);
/* Qualify the name if not visible in search path */
if
(
RelationIsVisible
(
relid
))
nspname
=
NULL
;
else
nspname
=
get_namespace_name
(
reltup
->
relnamespace
);
result
=
quote_qualified_identifier
(
nspname
,
NameStr
(
reltup
->
relname
));
ReleaseSysCache
(
tp
);
return
result
;
}
contrib/dblink/expected/dblink.out
浏览文件 @
11d33718
...
...
@@ -59,6 +59,43 @@ SELECT dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}');
DELETE FROM foo WHERE f1 = '0' AND f2 = 'a'
(1 row)
-- retest using a quoted and schema qualified table
CREATE SCHEMA "MySchema";
CREATE TABLE "MySchema"."Foo"(f1 int, f2 text, f3 text[], primary key (f1,f2));
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'Foo_pkey' for table 'Foo'
INSERT INTO "MySchema"."Foo" VALUES (0,'a','{"a0","b0","c0"}');
-- list the primary key fields
SELECT *
FROM dblink_get_pkey('"MySchema"."Foo"');
position | colname
----------+---------
1 | f1
2 | f2
(2 rows)
-- build an insert statement based on a local tuple,
-- replacing the primary key values with new ones
SELECT dblink_build_sql_insert('"MySchema"."Foo"','1 2',2,'{"0", "a"}','{"99", "xyz"}');
dblink_build_sql_insert
------------------------------------------------------------------------
INSERT INTO "MySchema"."Foo"(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}')
(1 row)
-- build an update statement based on a local tuple,
-- replacing the primary key values with new ones
SELECT dblink_build_sql_update('"MySchema"."Foo"','1 2',2,'{"0", "a"}','{"99", "xyz"}');
dblink_build_sql_update
-----------------------------------------------------------------------------------------------------
UPDATE "MySchema"."Foo" SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz'
(1 row)
-- build a delete statement based on a local tuple,
SELECT dblink_build_sql_delete('"MySchema"."Foo"','1 2',2,'{"0", "a"}');
dblink_build_sql_delete
----------------------------------------------------------
DELETE FROM "MySchema"."Foo" WHERE f1 = '0' AND f2 = 'a'
(1 row)
-- regular old dblink
SELECT *
FROM dblink('dbname=regression','SELECT * FROM foo') AS t(a int, b text, c text[])
...
...
contrib/dblink/sql/dblink.sql
浏览文件 @
11d33718
...
...
@@ -44,6 +44,26 @@ SELECT dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
-- build a delete statement based on a local tuple,
SELECT
dblink_build_sql_delete
(
'foo'
,
'1 2'
,
2
,
'{"0", "a"}'
);
-- retest using a quoted and schema qualified table
CREATE
SCHEMA
"MySchema"
;
CREATE
TABLE
"MySchema"
.
"Foo"
(
f1
int
,
f2
text
,
f3
text
[],
primary
key
(
f1
,
f2
));
INSERT
INTO
"MySchema"
.
"Foo"
VALUES
(
0
,
'a'
,
'{"a0","b0","c0"}'
);
-- list the primary key fields
SELECT
*
FROM
dblink_get_pkey
(
'"MySchema"."Foo"'
);
-- build an insert statement based on a local tuple,
-- replacing the primary key values with new ones
SELECT
dblink_build_sql_insert
(
'"MySchema"."Foo"'
,
'1 2'
,
2
,
'{"0", "a"}'
,
'{"99", "xyz"}'
);
-- build an update statement based on a local tuple,
-- replacing the primary key values with new ones
SELECT
dblink_build_sql_update
(
'"MySchema"."Foo"'
,
'1 2'
,
2
,
'{"0", "a"}'
,
'{"99", "xyz"}'
);
-- build a delete statement based on a local tuple,
SELECT
dblink_build_sql_delete
(
'"MySchema"."Foo"'
,
'1 2'
,
2
,
'{"0", "a"}'
);
-- regular old dblink
SELECT
*
FROM
dblink
(
'dbname=regression'
,
'SELECT * FROM foo'
)
AS
t
(
a
int
,
b
text
,
c
text
[])
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录