Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9305fc7

Browse files
committed
Fixes:
Attached is a patch to allow libpq to determine if a field is null.This is needed because text fields will return a PQgetlength() of 0whether it is '' or NULL. There is even a comment in the source notingthe fact.I have changed the value of the 'len' field for NULL result fields. Ifthe field is null, the len is set to -1 (NULL_LEN). I have changedPQgetlength() to return a 0 length for both '' and NULL. A new functionPQgetisnull() returns true or false for NULL.The only risk is to applications that do not use the suggestedPQgetlength() call, but read the result 'len' field directly.As this is not recommended, I think we are safe here.A separate documentation patch will be sent.Submitted by: Bruce Momjian <maillist@candle.pha.pa.us>
1 parent78d56d0 commit9305fc7

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

‎src/interfaces/libpq/fe-exec.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.14 1996/08/10 00:22:48 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.15 1996/08/13 01:34:27 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -156,11 +156,9 @@ getTuple(PGconn *conn, PGresult* result, int binary)
156156
for (i=0;i<nfields;i++) {
157157
if (!(bmap&0200)) {
158158
/* if the field value is absent, make it '\0' */
159-
/* XXX this makes it impossible to distinguish NULL
160-
attributes from "". Is that OK? */
161159
tup[i].value= (char*)malloc(1);
162160
tup[i].value[0]='\0';
163-
tup[i].len=0;
161+
tup[i].len=NULL_LEN;
164162
}
165163
else {
166164
/* get the value length (the first four bytes are for length) */
@@ -1469,6 +1467,35 @@ PQgetlength(PGresult *res, int tup_num, int field_num)
14691467
"PQgetlength: ERROR! field %d(of %d) of tuple %d(of %d) is not available",
14701468
field_num,res->numAttributes-1,tup_num,res->ntups);
14711469
}
1472-
1473-
returnres->tuples[tup_num][field_num].len;
1470+
1471+
if (res->tuples[tup_num][field_num].len!=NULL_LEN)
1472+
returnres->tuples[tup_num][field_num].len;
1473+
else
1474+
return0;
14741475
}
1476+
1477+
/* PQgetisnull:
1478+
returns the null status of a field value.
1479+
*/
1480+
int
1481+
PQgetisnull(PGresult*res,inttup_num,intfield_num)
1482+
{
1483+
if (!res) {
1484+
fprintf(stderr,"PQgetisnull() -- pointer to PQresult is null");
1485+
return (int)NULL;
1486+
}
1487+
1488+
if (tup_num> (res->ntups-1 )||
1489+
field_num> (res->numAttributes-1)) {
1490+
fprintf(stderr,
1491+
"PQgetisnull: ERROR! field %d(of %d) of tuple %d(of %d) is not available",
1492+
field_num,res->numAttributes-1,tup_num,res->ntups);
1493+
}
1494+
1495+
if (res->tuples[tup_num][field_num].len==NULL_LEN)
1496+
return1;
1497+
else
1498+
return0;
1499+
}
1500+
1501+

‎src/interfaces/libpq/libpq-fe.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: libpq-fe.h,v 1.6 1996/08/06 16:16:50 scrappy Exp $
9+
* $Id: libpq-fe.h,v 1.7 1996/08/13 01:34:29 scrappy Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -83,6 +83,9 @@ typedef struct pgresAttDesc {
8383
For binary tuples, the first four bytes of the value is the size,
8484
and the bytes afterwards are the value. The binary value is
8585
not guaranteed to be null-terminated. In fact, it can have embedded nulls*/
86+
87+
#defineNULL_LEN(-1)/* pg_result len for NULL value */
88+
8689
typedefstructpgresAttValue {
8790
intlen;/* length in bytes of the value */
8891
char*value;/* actual value */
@@ -178,6 +181,7 @@ extern char* PQcmdStatus(PGresult *res);
178181
externconstchar*PQoidStatus(PGresult*res);
179182
externchar*PQgetvalue(PGresult*res,inttup_num,intfield_num);
180183
externintPQgetlength(PGresult*res,inttup_num,intfield_num);
184+
externintPQgetisnull(PGresult*res,inttup_num,intfield_num);
181185
externvoidPQclear(PGresult*res);
182186
/* PQdisplayTuples() is a better version of PQprintTuples() */
183187
externvoidPQdisplayTuples(PGresult*res,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp