ECPG 不直接支持大对象,但是 ECPG 应用程序可以通过 libpq 大对象函数来操作大对象,通过调用 ECPGget_PGconn() 函数获取必要的 PGconn 对象。(然而,ECPGget_PGconn() 函数的使用和直接操作 PGconn 对象应该非常小心,并且理想情况下不要与其他 ECPG 数据库访问调用混合使用。)
有关 ECPGget_PGconn() 的更多详细信息,请参阅第 34.11 节。有关大对象函数接口的信息,请参阅第 33 章。
大对象函数必须在事务块中调用,因此当自动提交关闭时,必须显式发出 BEGIN 命令。
示例 34.2 展示了一个示例程序,说明如何在 ECPG 应用程序中创建、写入和读取大对象。
示例 34.2. 访问大对象的 ECPG 程序
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include <libpq/libpq-fs.h>
EXEC SQL WHENEVER SQLERROR STOP;
int
main(void)
{
PGconn *conn;
Oid loid;
int fd;
char buf[256];
int buflen = 256;
char buf2[256];
int rc;
memset(buf, 1, buflen);
EXEC SQL CONNECT TO testdb AS con1;
EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
conn = ECPGget_PGconn("con1");
printf("conn = %p\n", conn);
/* create */
loid = lo_create(conn, 0);
if (loid < 0)
printf("lo_create() failed: %s", PQerrorMessage(conn));
printf("loid = %d\n", loid);
/* write test */
fd = lo_open(conn, loid, INV_READ|INV_WRITE);
if (fd < 0)
printf("lo_open() failed: %s", PQerrorMessage(conn));
printf("fd = %d\n", fd);
rc = lo_write(conn, fd, buf, buflen);
if (rc < 0)
printf("lo_write() failed\n");
rc = lo_close(conn, fd);
if (rc < 0)
printf("lo_close() failed: %s", PQerrorMessage(conn));
/* read test */
fd = lo_open(conn, loid, INV_READ);
if (fd < 0)
printf("lo_open() failed: %s", PQerrorMessage(conn));
printf("fd = %d\n", fd);
rc = lo_read(conn, fd, buf2, buflen);
if (rc < 0)
printf("lo_read() failed\n");
rc = lo_close(conn, fd);
if (rc < 0)
printf("lo_close() failed: %s", PQerrorMessage(conn));
/* check */
rc = memcmp(buf, buf2, buflen);
printf("memcmp() = %d\n", rc);
/* cleanup */
rc = lo_unlink(conn, loid);
if (rc < 0)
printf("lo_unlink() failed: %s", PQerrorMessage(conn));
EXEC SQL COMMIT;
EXEC SQL DISCONNECT ALL;
return 0;
}
如果您在文档中发现任何不正确的内容、与您使用特定功能的经验不符或需要进一步澄清的内容,请使用此表单报告文档问题。