33 *
44 * Copyright 2000 by PostgreSQL Global Development Group
55 *
6- * $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.16 2001/08/10 18:57:39 tgl Exp $
6+ * $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.17 2001/10/04 22:39:34 tgl Exp $
77 */
88#include "postgres_fe.h"
99#include "large_obj.h"
1616#include "print.h"
1717
1818
19+ #define atooid (x ) ((Oid) strtoul((x), NULL, 10))
20+
21+
1922/*
2023 * Since all large object ops must be in a transaction, we must do some magic
2124 * here. You can set the variable lo_transaction to one of commit|rollback|
@@ -107,7 +110,7 @@ do_lo_export(const char *loid_arg, const char *filename_arg)
107110PQclear (res );
108111}
109112
110- status = lo_export (pset .db ,atol (loid_arg ),filename_arg );
113+ status = lo_export (pset .db ,atooid (loid_arg ),filename_arg );
111114if (status != 1 )
112115{/* of course this status is documented
113116 * nowhere :( */
@@ -187,7 +190,9 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
187190}
188191
189192/* insert description if given */
190- if (comment_arg )
193+ /* XXX don't try to hack pg_description if not superuser */
194+ /* XXX ought to replace this with some kind of COMMENT command */
195+ if (comment_arg && pset .issuper )
191196{
192197char * cmdbuf ;
193198char * bufptr ;
@@ -204,7 +209,7 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
204209return false;
205210}
206211sprintf (cmdbuf ,
207- "INSERT INTO pg_description VALUES (%u , "
212+ "INSERT INTO pg_description VALUES ('%u' , "
208213"(SELECT oid FROM pg_class WHERE relname = 'pg_largeobject'),"
209214" 0, '" ,loid );
210215bufptr = cmdbuf + strlen (cmdbuf );
@@ -263,7 +268,7 @@ do_lo_unlink(const char *loid_arg)
263268{
264269PGresult * res ;
265270int status ;
266- Oid loid = ( Oid ) atol (loid_arg );
271+ Oid loid = atooid (loid_arg );
267272char buf [256 ];
268273bool own_transaction = true;
269274const char * var = GetVariable (pset .vars ,"LO_TRANSACTION" );
@@ -301,20 +306,24 @@ do_lo_unlink(const char *loid_arg)
301306}
302307
303308/* remove the comment as well */
304- sprintf (buf ,"DELETE FROM pg_description WHERE objoid = %u "
305- "AND classoid = (SELECT oid FROM pg_class WHERE relname = 'pg_largeobject')" ,
306- loid );
307- if (!(res = PSQLexec (buf )))
309+ /* XXX don't try to hack pg_description if not superuser */
310+ /* XXX ought to replace this with some kind of COMMENT command */
311+ if (pset .issuper )
308312{
309- if (own_transaction )
313+ sprintf (buf ,"DELETE FROM pg_description WHERE objoid = '%u' "
314+ "AND classoid = (SELECT oid FROM pg_class WHERE relname = 'pg_largeobject')" ,
315+ loid );
316+ if (!(res = PSQLexec (buf )))
310317{
311- res = PQexec (pset .db ,"ROLLBACK" );
312- PQclear (res );
318+ if (own_transaction )
319+ {
320+ res = PQexec (pset .db ,"ROLLBACK" );
321+ PQclear (res );
322+ }
323+ return false;
313324}
314- return false;
315325}
316326
317-
318327if (own_transaction )
319328{
320329if (!(res = PSQLexec ("COMMIT" )))
@@ -327,7 +336,7 @@ do_lo_unlink(const char *loid_arg)
327336}
328337
329338
330- fprintf (pset .queryFout ,"lo_unlink %d \n" ,loid );
339+ fprintf (pset .queryFout ,"lo_unlink %u \n" ,loid );
331340
332341return true;
333342}