pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedGetting oid with libpq
7+ messages / 3 participants
[nested] [flat]
* Getting oid with libpq
@ 2005-08-10 09:33 Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-10 10:52 ` Re: Getting oid with libpq jtv@xs4all.nl
0 siblings, 1 reply; 7+ messages in thread
From: Johan C. de Koning @ 2005-08-10 09:33 UTC (permalink / raw)
To: pgsql-interfaces
Hello,
I have stored some large objects (which are images) with the lo_import
function. Now I can read the images with the other functions like lo_read
within libpq. But I have one problem with the oid I have to give to this
method. Inside my program I will do a select like this
Select texture from building_face where face_id > 100;
Textures is the oid field. When I use PQgetValue() I get a complete
different value then when I do a select inside pgAdmin. How can I get the
same oid so that I can use this for reading the data of the images inside my
c++ program. Because it is not working with the oid from PQgetValue.
Best regards,
Johan de Koning
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Getting oid with libpq
2005-08-10 09:33 Getting oid with libpq Johan C. de Koning <johan.de.koning@geodan.nl>
@ 2005-08-10 10:52 ` jtv@xs4all.nl
2005-08-10 12:12 ` Re: Getting oid with libpq Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-11 07:06 ` Re: Getting oid with libpq jtv@xs4all.nl
0 siblings, 2 replies; 7+ messages in thread
From: jtv@xs4all.nl @ 2005-08-10 10:52 UTC (permalink / raw)
To: Johan C. de Koning <johan.de.koning@geodan.nl>; +Cc: pgsql-interfaces
Johan C. de Koning wrote:
> I have stored some large objects (which are images) with the lo_import
> function. Now I can read the images with the other functions like lo_read
> within libpq. But I have one problem with the oid I have to give to this
> method. Inside my program I will do a select like this
> Select texture from building_face where face_id > 100;
> Textures is the oid field. When I use PQgetValue() I get a complete
> different value then when I do a select inside pgAdmin. How can I get the
> same oid so that I can use this for reading the data of the images inside
> my c++ program. Because it is not working with the oid from PQgetValue.
Just a guess, but...
One mistake many people make is to cast the char * returned by
PQgetValue() to the type they expect to get. Which won't work, since it's
a pointer to a value (in this case a number) in a textual format. It
needs to be parsed so you get the actual numeric value.
Also, when you parse the number, be aware that the standard-library
functions (such as sscanf()) take the applicable locale setting into
account--which is not what you want in this case. Best reset to the
default locale before parsing.
Jeroen
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Getting oid with libpq
2005-08-10 09:33 Getting oid with libpq Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-10 10:52 ` Re: Getting oid with libpq jtv@xs4all.nl
@ 2005-08-10 12:12 ` Johan C. de Koning <johan.de.koning@geodan.nl>
1 sibling, 0 replies; 7+ messages in thread
From: Johan C. de Koning @ 2005-08-10 12:12 UTC (permalink / raw)
To: jtv@xs4all.nl; +Cc: pgsql-interfaces
Thanks for you answer,
I am not an expert in C/C++ so do you know maybe where I can find some
example code or documentation about the parsing process with sscanf?
Best regards,
Johan de Koning
-----Oorspronkelijk bericht-----
Van: pgsql-interfaces-owner@postgresql.org
[mailto:pgsql-interfaces-owner@postgresql.org] Namens jtv@xs4all.nl
Verzonden: woensdag 10 augustus 2005 12:52
Aan: Johan C. de Koning
CC: pgsql-interfaces@postgresql.org
Onderwerp: Re: [INTERFACES] Getting oid with libpq
Johan C. de Koning wrote:
> I have stored some large objects (which are images) with the lo_import
> function. Now I can read the images with the other functions like lo_read
> within libpq. But I have one problem with the oid I have to give to this
> method. Inside my program I will do a select like this
> Select texture from building_face where face_id > 100;
> Textures is the oid field. When I use PQgetValue() I get a complete
> different value then when I do a select inside pgAdmin. How can I get the
> same oid so that I can use this for reading the data of the images inside
> my c++ program. Because it is not working with the oid from PQgetValue.
Just a guess, but...
One mistake many people make is to cast the char * returned by
PQgetValue() to the type they expect to get. Which won't work, since it's
a pointer to a value (in this case a number) in a textual format. It
needs to be parsed so you get the actual numeric value.
Also, when you parse the number, be aware that the standard-library
functions (such as sscanf()) take the applicable locale setting into
account--which is not what you want in this case. Best reset to the
default locale before parsing.
Jeroen
---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Getting oid with libpq
2005-08-10 09:33 Getting oid with libpq Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-10 10:52 ` Re: Getting oid with libpq jtv@xs4all.nl
@ 2005-08-11 07:06 ` jtv@xs4all.nl
2005-08-11 13:57 ` Re: Getting oid with libpq Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 7+ messages in thread
From: jtv@xs4all.nl @ 2005-08-11 07:06 UTC (permalink / raw)
To: Johan C. de Koning <johan.de.koning@geodan.nl>; +Cc: jtv@xs4all.nl; pgsql-interfaces
Johan C. de Koning wrote:
> I am not an expert in C/C++ so do you know maybe where I can find some
> example code or documentation about the parsing process with sscanf?
In C, you'd probably want to use
const char *OldLocale = setlocale(LC_NUMERIC, "C");
Then run the pointer returned by PQgetValue() through atol() to get a
numeric value, and finally restore the previous locale with
setlocale(LC_NUMERIC, OldLocale);
If you're using C++, best thing to do may be to use libpqxx instead of
libpq. It has functions to do the conversions for you, and takes care of
locale and overflow issues as well.
Jeroen
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Getting oid with libpq
2005-08-10 09:33 Getting oid with libpq Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-10 10:52 ` Re: Getting oid with libpq jtv@xs4all.nl
2005-08-11 07:06 ` Re: Getting oid with libpq jtv@xs4all.nl
@ 2005-08-11 13:57 ` Tom Lane <tgl@sss.pgh.pa.us>
2005-08-11 14:08 ` Re: Getting oid with libpq Johan C. de Koning <johan.de.koning@geodan.nl>
0 siblings, 1 reply; 7+ messages in thread
From: Tom Lane @ 2005-08-11 13:57 UTC (permalink / raw)
To: jtv@xs4all.nl; +Cc: Johan C. de Koning <johan.de.koning@geodan.nl>; pgsql-interfaces
jtv@xs4all.nl writes:
> const char *OldLocale = setlocale(LC_NUMERIC, "C");
> Then run the pointer returned by PQgetValue() through atol() to get a
> numeric value, and finally restore the previous locale with
> setlocale(LC_NUMERIC, OldLocale);
Uh ... there is nothing at all locale-sensitive about atol, nor strtoul
which is what you really want to use for an OID.
regards, tom lane
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Getting oid with libpq
2005-08-10 09:33 Getting oid with libpq Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-10 10:52 ` Re: Getting oid with libpq jtv@xs4all.nl
2005-08-11 07:06 ` Re: Getting oid with libpq jtv@xs4all.nl
2005-08-11 13:57 ` Re: Getting oid with libpq Tom Lane <tgl@sss.pgh.pa.us>
@ 2005-08-11 14:08 ` Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-13 06:20 ` Re: Getting oid with libpq jtv@xs4all.nl
0 siblings, 1 reply; 7+ messages in thread
From: Johan C. de Koning @ 2005-08-11 14:08 UTC (permalink / raw)
To: 'Tom Lane' <tgl@sss.pgh.pa.us>; jtv@xs4all.nl; +Cc: pgsql-interfaces
I tried to use atol but i get always 0 as result. Is this because I use a
binary cursor to select the data (there are some PostGIS datatypes inside my
resultset which are binary data).
Best regards,
Johan de Koning
-----Oorspronkelijk bericht-----
Van: Tom Lane [mailto:tgl@sss.pgh.pa.us]
Verzonden: donderdag 11 augustus 2005 15:57
Aan: jtv@xs4all.nl
CC: Johan C. de Koning; pgsql-interfaces@postgresql.org
Onderwerp: Re: [INTERFACES] Getting oid with libpq
jtv@xs4all.nl writes:
> const char *OldLocale = setlocale(LC_NUMERIC, "C");
> Then run the pointer returned by PQgetValue() through atol() to get a
> numeric value, and finally restore the previous locale with
> setlocale(LC_NUMERIC, OldLocale);
Uh ... there is nothing at all locale-sensitive about atol, nor strtoul
which is what you really want to use for an OID.
regards, tom lane
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Getting oid with libpq
2005-08-10 09:33 Getting oid with libpq Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-10 10:52 ` Re: Getting oid with libpq jtv@xs4all.nl
2005-08-11 07:06 ` Re: Getting oid with libpq jtv@xs4all.nl
2005-08-11 13:57 ` Re: Getting oid with libpq Tom Lane <tgl@sss.pgh.pa.us>
2005-08-11 14:08 ` Re: Getting oid with libpq Johan C. de Koning <johan.de.koning@geodan.nl>
@ 2005-08-13 06:20 ` jtv@xs4all.nl
0 siblings, 0 replies; 7+ messages in thread
From: jtv@xs4all.nl @ 2005-08-13 06:20 UTC (permalink / raw)
To: Johan C. de Koning <johan.de.koning@geodan.nl>; +Cc: 'Tom Lane' <tgl@sss.pgh.pa.us>; jtv@xs4all.nl; pgsql-interfaces
> I tried to use atol but i get always 0 as result. Is this because I use a
> binary cursor to select the data (there are some PostGIS datatypes inside
> my resultset which are binary data).
That changes everything.
With a binary cursor you get all data in a binary format which "I'm sure
is documented somewhere." I believe in the case of oids it's a 32-bit
integer in network (big-endian) byte order. If that is indeed the case,
you should be able to read the oid as "ntohl(*(uint32_t
*)PQgetValue(...))".
On a side note, I don't think you actually need a binary cursor to
transfer binary data (apart from the fact that it's likely to improve
performance).
Jeroen
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2005-08-13 06:20 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-08-10 09:33 Getting oid with libpq Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-10 10:52 ` jtv@xs4all.nl
2005-08-10 12:12 ` Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-11 07:06 ` jtv@xs4all.nl
2005-08-11 13:57 ` Tom Lane <tgl@sss.pgh.pa.us>
2005-08-11 14:08 ` Johan C. de Koning <johan.de.koning@geodan.nl>
2005-08-13 06:20 ` jtv@xs4all.nl
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox