Hello,

I am trying to use large object in postgresql thr. soci. But getting an error, 
"cannot convert data" when I select blob.

GDB transcript and the source are attached.  The table definition is as 
follows


test=# \d blobtest;
   Table "public.blobtest"
 Column |  Type   | Modifiers
--------+---------+-----------
 id     | integer |
 img    | oid     |

This is exactly same as test-postgresql.cpp test3, except for the table name. 
Whats wrong?

I compiled test-postgresql.cpp(Had to add #include <cstring> for strncmp) but 
it failed with same error but in test 2.

Any help is appreciated. Thanks.


-- 
 Shridhar
$ bjam debug && gdb bin/gcc-4.4.2/debug/blob
...found 52 targets...
...updating 2 targets...
gcc.compile.c++ bin/gcc-4.4.2/debug/blob.o
blob.cpp: In function ‘int main(int, char**)’:
blob.cpp:61: warning: comparison between signed and unsigned integer expressions
gcc.link bin/gcc-4.4.2/debug/blob
...updated 2 targets...
GNU gdb (GDB) 7.0
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from 
/mnt1/shridhar/development/test/soci/bin/gcc-4.4.2/debug/blob...done.
(gdb) b main
Breakpoint 1 at 0x401ddf: file blob.cpp, line 22.
(gdb) r tasks.gz
Starting program: /mnt1/shridhar/development/test/soci/bin/gcc-4.4.2/debug/blob 
tasks.gz

Breakpoint 1, main (argc=2, argv=0x7fffffffe2d8) at blob.cpp:22
22              if(argc<2)
(gdb) n
32                      int fd = open(argv[1],O_RDONLY);
(gdb) n
33                      assert(fd>0);
(gdb) n
36                      assert(stat(argv[1],&stbuf)==0);
(gdb) n
38                      void *ptr = mmap(NULL,stbuf.st_size, 
PROT_READ,MAP_SHARED|MAP_POPULATE,fd,0);
(gdb)
39                      assert(ptr!=((void *)-1));
(gdb)
41                      session sql(postgresql,"dbname=test");
(gdb)
44                              transaction tr(sql);
(gdb)
45                              blob b(sql);
(gdb)
47                              sql << "insert into blobtest(id, img) values(7, 
lo_creat(-1))";
(gdb)
48                              sql << "select img from blobtest where id = 7", 
into(b);
(gdb)
Cannot convert data.

Program exited normally.

#include <soci.h>
#include <soci-postgresql.h>

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <cstdlib>

#undef NDEBUG
#include <cassert>

int main(int argc, char **argv)
{
	if(argc<2)
	{
		std::cout << "Usage:" << argv[0] << " file" << std::endl;
		return -1;
	}

	using namespace soci;

	try
	{
		int fd = open(argv[1],O_RDONLY);
		assert(fd>0);

		struct stat stbuf;
		assert(stat(argv[1],&stbuf)==0);

		void *ptr = mmap(NULL,stbuf.st_size, PROT_READ,MAP_SHARED|MAP_POPULATE,fd,0);
		assert(ptr!=((void *)-1));

		session sql(postgresql,"dbname=test");

		{
			transaction tr(sql);
			blob b(sql);

			sql << "insert into blobtest(id, img) values(7, lo_creat(-1))";
			sql << "select img from blobtest where id = 7", into(b);
			b.write(0,(char *)ptr,stbuf.st_size);

			tr.commit();
		}

		{
			transaction tr(sql);
			blob b(sql);

			sql << "insert into blobtest(id, img) values(7, lo_creat(-1))";

			sql << "select img from blobtest where id = 7", into(b);
			assert(b.get_len()==stbuf.st_size);

			void *buf = malloc(stbuf.st_size);
			assert(buf!=NULL);

			b.read(0,(char *)buf, stbuf.st_size);

			assert(memcmp(ptr,buf,stbuf.st_size)==0);
		}

		munmap(ptr,stbuf.st_size);
		close(fd);
	}
	catch(std::exception & e)
	{
		std::cout << e.what() << std::endl;
	}
}
------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
Soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users

Reply via email to