专为通过 SQL 操纵大对象而设计的服务器端函数列在Table 34.1中。
Table 34.1. 面向 SQL 的大对象函数
前面介绍的每个客户端函数都有对应的服务器端函数;实际上,大多数客户端函数都只是等价服务器端函数的接口。通过 SQL 命令调用起来同样方便的有lo_creat、lo_create、lo_unlink、lo_import以及lo_export。下面是它们的使用示例:
CREATE TABLE image (
name text,
raster oid
);
SELECT lo_creat(-1); -- returns OID of new, empty large object
SELECT lo_create(43213); -- attempts to create large object with OID 43213
SELECT lo_unlink(173454); -- deletes large object with OID 173454
INSERT INTO image (name, raster)
VALUES ('beautiful image', lo_import('/etc/motd'));
INSERT INTO image (name, raster) -- same as above, but specify OID to use
VALUES ('beautiful image', lo_import('/etc/motd', 68583));
SELECT lo_export(image.raster, '/tmp/motd') FROM image
WHERE name = 'beautiful image';
服务器端 lo_import 和 lo_export 函数的行为与客户端对应函数有很大不同。这两个函数使用数据库拥有者用户的权限,读写服务器文件系统中的文件,因此仅限超级用户使用。相比之下,客户端导入和导出函数使用客户端程序的权限,读写客户端文件系统中的文件。客户端函数不要求超级用户权限。
lo_read和lo_write的功能也可以通过服务器端调用获得,但服务器端函数名与客户端接口不同,因为它们不包含下划线。必须将这些函数调用为loread和lowrite。