33.12. Большие объекты
ECPG не поддерживает большие объекты напрямую, но приложение на базе ECPG может работать с большими объектами, используя предназначенные для этого функции, получив необходимый объект PGconn
в результате вызова ECPGget_PGconn()
. (Однако использовать функцию ECPGget_PGconn()
и напрямую воздействовать на объекты PGconn
следует очень осторожно; в идеале стоит исключить при этом другие обращения к базе данных через ECPG.)
Подробнее функция ECPGget_PGconn()
описана в Разделе 33.11. Интерфейс функций для работы с большими объектами рассмотрен в Главе 32.
Функции для работы с большими объектами должны вызываться в блоке транзакций, поэтому если режим автофиксации отключён, необходимо явно выдавать команды BEGIN
.
В Примере 33.2 приведён пример программы, показывающий, как создать, записать и прочитать большой объект в приложении ECPG.
Пример 33.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; }
36.3. User-Defined Functions
Postgres Pro provides four kinds of functions:
query language functions (functions written in SQL) (Section 36.5)
procedural language functions (functions written in, for example, PL/pgSQL or PL/Tcl) (Section 36.8)
internal functions (Section 36.9)
C-language functions (Section 36.10)
Every kind of function can take base types, composite types, or combinations of these as arguments (parameters). In addition, every kind of function can return a base type or a composite type. Functions can also be defined to return sets of base or composite values.
Many kinds of functions can take or return certain pseudo-types (such as polymorphic types), but the available facilities vary. Consult the description of each kind of function for more details.
It's easiest to define SQL functions, so we'll start by discussing those. Most of the concepts presented for SQL functions will carry over to the other types of functions.
Throughout this chapter, it can be useful to look at the reference page of the CREATE FUNCTION command to understand the examples better.