1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/lobj.sgml,v 1.14 2000/12/21 22:55:27 petere Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/lobj.sgml,v 1.15 2001/01/20 00:05:54 momjian Exp $
3
3
-->
4
4
5
5
<chapter id="largeObjects">
@@ -316,48 +316,52 @@ SELECT lo_export(image.raster, '/tmp/motd') from image
316
316
*--------------------------------------------------------------
317
317
*/
318
318
#include <stdio.h>
319
- #include " libpq-fe.h"
320
- #include " libpq/libpq-fs.h"
319
+ #include " libpq-fe.h"
320
+ #include " libpq/libpq-fs.h"
321
321
322
322
#define BUFSIZE 1024
323
323
324
324
/*
325
- * importFile * import file "in_filename" into database as large object "lobjOid"
325
+ * importFile * import file "in_filename" into database as large object "lob
326
+ jOid"
326
327
*
327
328
*/
328
- Oid importFile(PGconn *conn, char *filename)
329
+ Oid
330
+ importFile(PGconn *conn, char *filename)
329
331
{
330
- Oid lobjId;
331
- int lobj_fd;
332
- char buf[BUFSIZE];
333
- int nbytes, tmp;
334
- int fd;
332
+ Oid lobjId;
333
+ int lobj_fd;
334
+ char buf[BUFSIZE];
335
+ int nbytes,
336
+ tmp;
337
+ int fd;
335
338
336
339
/*
337
340
* open the file to be read in
338
341
*/
339
342
fd = open(filename, O_RDONLY, 0666);
340
- if (fd < 0) { /* error */
341
- fprintf(stderr, "can't open unix file %s\n", filename);
343
+ if (fd < 0)
344
+ { /* error */
345
+ fprintf(stderr, "can't open unix file %s\n", filename);
342
346
}
343
347
344
348
/*
345
349
* create the large object
346
350
*/
347
- lobjId = lo_creat(conn, INV_READ|INV_WRITE);
348
- if (lobjId == 0) {
349
- fprintf(stderr, "can't create large object\n");
350
- }
351
+ lobjId = lo_creat(conn, INV_READ | INV_WRITE);
352
+ if (lobjId == 0)
353
+ fprintf(stderr, "can't create large object\n");
351
354
352
355
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
356
+
353
357
/*
354
358
* read in from the Unix file and write to the inversion file
355
359
*/
356
- while ((nbytes = read(fd, buf, BUFSIZE)) > 0) {
357
- tmp = lo_write(conn, lobj_fd, buf, nbytes);
358
- if ( tmp < nbytes) {
359
- fprintf(stderr, "error while reading large object\n");
360
- }
360
+ while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
361
+ {
362
+ tmp = lo_write(conn, lobj_fd, buf, nbytes);
363
+ if (tmp < nbytes)
364
+ fprintf(stderr, "error while reading large object\n");
361
365
}
362
366
363
367
(void) close(fd);
@@ -366,101 +370,115 @@ Oid importFile(PGconn *conn, char *filename)
366
370
return lobjId;
367
371
}
368
372
369
- void pickout(PGconn *conn, Oid lobjId, int start, int len)
373
+ void
374
+ pickout(PGconn *conn, Oid lobjId, int start, int len)
370
375
{
371
- int lobj_fd;
372
- char* buf;
373
- int nbytes;
374
- int nread;
376
+ int lobj_fd;
377
+ char * buf;
378
+ int nbytes;
379
+ int nread;
375
380
376
381
lobj_fd = lo_open(conn, lobjId, INV_READ);
377
- if (lobj_fd < 0) {
378
- fprintf(stderr,"can't open large object %d\n",
379
- lobjId);
382
+ if (lobj_fd < 0)
383
+ {
384
+ fprintf(stderr, "can't open large object %d\n",
385
+ lobjId);
380
386
}
381
387
382
388
lo_lseek(conn, lobj_fd, start, SEEK_SET);
383
- buf = malloc(len+ 1);
389
+ buf = malloc(len + 1);
384
390
385
391
nread = 0;
386
- while (len - nread > 0) {
387
- nbytes = lo_read(conn, lobj_fd, buf, len - nread);
388
- buf[nbytes] = ' ';
389
- fprintf(stderr,">>> %s", buf);
390
- nread += nbytes;
392
+ while (len - nread > 0)
393
+ {
394
+ nbytes = lo_read(conn, lobj_fd, buf, len - nread);
395
+ buf[nbytes] = ' ';
396
+ fprintf(stderr, ">>> %s", buf);
397
+ nread += nbytes;
391
398
}
392
- fprintf(stderr,"\n");
399
+ free(buf);
400
+ fprintf(stderr, "\n");
393
401
lo_close(conn, lobj_fd);
394
402
}
395
403
396
- void overwrite(PGconn *conn, Oid lobjId, int start, int len)
404
+ void
405
+ overwrite(PGconn *conn, Oid lobjId, int start, int len)
397
406
{
398
- int lobj_fd;
399
- char* buf;
400
- int nbytes;
401
- int nwritten;
402
- int i;
407
+ int lobj_fd;
408
+ char * buf;
409
+ int nbytes;
410
+ int nwritten;
411
+ int i;
403
412
404
413
lobj_fd = lo_open(conn, lobjId, INV_READ);
405
- if (lobj_fd < 0) {
406
- fprintf(stderr,"can't open large object %d\n",
407
- lobjId);
414
+ if (lobj_fd < 0)
415
+ {
416
+ fprintf(stderr, "can't open large object %d\n",
417
+ lobjId);
408
418
}
409
419
410
420
lo_lseek(conn, lobj_fd, start, SEEK_SET);
411
- buf = malloc(len+ 1);
421
+ buf = malloc(len + 1);
412
422
413
- for (i=0;i <len;i++)
414
- buf[i] = 'X';
423
+ for (i = 0; i < len; i++)
424
+ buf[i] = 'X';
415
425
buf[i] = ' ';
416
426
417
427
nwritten = 0;
418
- while (len - nwritten > 0) {
419
- nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
420
- nwritten += nbytes;
428
+ while (len - nwritten > 0)
429
+ {
430
+ nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
431
+ nwritten += nbytes;
421
432
}
422
- fprintf(stderr,"\n");
433
+ free(buf);
434
+ fprintf(stderr, "\n");
423
435
lo_close(conn, lobj_fd);
424
436
}
425
437
426
438
/*
427
- * exportFile * export large object " lobjOid" to file " out_filename"
439
+ * exportFile * export large object " lobjOid" to file " out_filename"
428
440
*
429
441
*/
430
- void exportFile(PGconn *conn, Oid lobjId, char *filename)
442
+ void
443
+ exportFile(PGconn *conn, Oid lobjId, char *filename)
431
444
{
432
- int lobj_fd;
433
- char buf[BUFSIZE];
434
- int nbytes, tmp;
435
- int fd;
445
+ int lobj_fd;
446
+ char buf[BUFSIZE];
447
+ int nbytes,
448
+ tmp;
449
+ int fd;
436
450
437
451
/*
438
- * create an inversion " object"
452
+ * create an inversion " object"
439
453
*/
440
454
lobj_fd = lo_open(conn, lobjId, INV_READ);
441
- if (lobj_fd < 0) {
442
- fprintf(stderr,"can't open large object %d\n",
443
- lobjId);
455
+ if (lobj_fd < 0)
456
+ {
457
+ fprintf(stderr, "can't open large object %d\n",
458
+ lobjId);
444
459
}
445
460
446
461
/*
447
462
* open the file to be written to
448
463
*/
449
- fd = open(filename, O_CREAT|O_WRONLY, 0666);
450
- if (fd < 0) { /* error */
451
- fprintf(stderr, "can't open unix file %s\n",
452
- filename);
464
+ fd = open(filename, O_CREAT | O_WRONLY, 0666);
465
+ if (fd < 0)
466
+ { /* error */
467
+ fprintf(stderr, "can't open unix file %s\n",
468
+ filename);
453
469
}
454
470
455
471
/*
456
472
* read in from the Unix file and write to the inversion file
457
473
*/
458
- while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0) {
459
- tmp = write(fd, buf, nbytes);
460
- if (tmp < nbytes) {
461
- fprintf(stderr,"error while writing %s\n",
462
- filename);
463
- }
474
+ while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
475
+ {
476
+ tmp = write(fd, buf, nbytes);
477
+ if (tmp < nbytes)
478
+ {
479
+ fprintf(stderr, "error while writing %s\n",
480
+ filename);
481
+ }
464
482
}
465
483
466
484
(void) lo_close(conn, lobj_fd);
@@ -470,25 +488,27 @@ void exportFile(PGconn *conn, Oid lobjId, char *filename)
470
488
}
471
489
472
490
void
473
- exit_nicely(PGconn* conn)
491
+ exit_nicely(PGconn * conn)
474
492
{
475
- PQfinish(conn);
476
- exit(1);
493
+ PQfinish(conn);
494
+ exit(1);
477
495
}
478
496
479
497
int
480
498
main(int argc, char **argv)
481
499
{
482
- char *in_filename, *out_filename;
483
- char *database;
484
- Oid lobjOid;
485
- PGconn *conn;
486
- PGresult *res;
487
-
488
- if (argc != 4) {
489
- fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
490
- argv[0]);
491
- exit(1);
500
+ char *in_filename,
501
+ *out_filename;
502
+ char *database;
503
+ Oid lobjOid;
504
+ PGconn *conn;
505
+ PGresult *res;
506
+
507
+ if (argc != 4)
508
+ {
509
+ fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
510
+ argv[0]);
511
+ exit(1);
492
512
}
493
513
494
514
database = argv[1];
@@ -501,33 +521,34 @@ main(int argc, char **argv)
501
521
conn = PQsetdb(NULL, NULL, NULL, NULL, database);
502
522
503
523
/* check to see that the backend connection was successfully made */
504
- if (PQstatus(conn) == CONNECTION_BAD) {
505
- fprintf(stderr,"Connection to database '%s' failed.\n", database);
506
- fprintf(stderr,"%s",PQerrorMessage(conn));
507
- exit_nicely(conn);
524
+ if (PQstatus(conn) == CONNECTION_BAD)
525
+ {
526
+ fprintf(stderr, "Connection to database '%s' failed.\n", database);
527
+ fprintf(stderr, "%s", PQerrorMessage(conn));
528
+ exit_nicely(conn);
508
529
}
509
530
510
- res = PQexec(conn, " begin" );
531
+ res = PQexec(conn, " begin" );
511
532
PQclear(res);
512
533
513
- printf(" importing file %s\n" , in_filename);
534
+ printf(" importing file %s\n" , in_filename);
514
535
/* lobjOid = importFile(conn, in_filename); */
515
536
lobjOid = lo_import(conn, in_filename);
516
537
/*
517
- printf(" as large object %d.\n" , lobjOid);
538
+ printf(" as large object %d.\n" , lobjOid);
518
539
519
- printf(" picking out bytes 1000-2000 of the large object\n" );
540
+ printf(" picking out bytes 1000-2000 of the large object\n" );
520
541
pickout(conn, lobjOid, 1000, 1000);
521
542
522
- printf(" overwriting bytes 1000-2000 of the large object with X's\n" );
543
+ printf(" overwriting bytes 1000-2000 of the large object with X's\n" );
523
544
overwrite(conn, lobjOid, 1000, 1000);
524
545
*/
525
546
526
- printf(" exporting large object to file %s\n" , out_filename);
547
+ printf(" exporting large object to file %s\n" , out_filename);
527
548
/* exportFile(conn, lobjOid, out_filename); */
528
- lo_export(conn, lobjOid,out_filename);
549
+ lo_export(conn, lobjOid, out_filename);
529
550
530
- res = PQexec(conn, " end" );
551
+ res = PQexec(conn, " end" );
531
552
PQclear(res);
532
553
PQfinish(conn);
533
554
exit(0);
0 commit comments