Two seperate problems with libpq - arrays and bytea edit

Поиск
Список
Период
Сортировка
Искать

Two seperate problems with libpq - arrays and bytea edit

От:
Teunis Peters <teunis@wintersgift.com>
Дата:
I'm trying to figure out how to read from and write to bytea values and 
array (specifically "text[2][]" for a key:value 'dictionary') and 
cannot figure it out.   Arrays I'm completely lost - I haven't a clue 
what the format would be.
bytea - my code segmentation faults.  PQgetlength returns 0.   I don't 
know if I'm putting it in the database correctly.

some sample code.  Yes it's in Objective C.   [db_result] embeds a 
"PGresult" value and does a lot of error checking.  image is of type 
NSData which basically holds binary data.  The rest SHOULD be clear.
I'd provide a smaller example except I'm not yet sure how to create 
console apps in MacOSX...   I suppose I could do it in linux though.

Any advice helpful.   I've been stewing my brain on this for a while 
now and am on something of a time limit.

getValue == PQgetvalue(res,tupple,field)
getlength == PQgetlength(res,tupple,field)
I hope that helps.

commit (subset of full code)
	sprintf(buf, "%i", cat_id);
	paramValues[np] = buf;           paramLen[np] = strlen(buf);    np++;
	paramValues[np] = [image bytes]; paramLen[np] = [image length]; np++;
	PGresult* r;
	r = PQexecParams([conn conn], "UPDATE images SET image = $2 WHERE id = 
$1;",
					 np, NULL, paramValues, paramLen, NULL, 1);
	if (PQresultStatus(r) != PGRES_COMMAND_OK) {
		NSLog(@"UPDATE failed: %s", PQerrorMessage([conn conn]));
	}
	PQclear(r);

access (subset of full code)

	sprintf(buf, "%i", cat_id);
	paramValues[0] = buf;
	res = [db_result alloc];
	[res init:PQexecParams([conn conn], "SELECT image FROM images WHERE id 
= $1",1,NULL,paramValues,NULL,NULL,1)];
	if (!res) return nil;
	if ([res ntuples] < 1) return nil;
	{
		char*     ptr;
		char*     decoded;
		size_t    i, decodedlen;
		NSString* ns;
		ptr = [res getValue:0 field:0];
		if (ptr != NULL) {
			i   = [res getlength:0 field:0];
			decoded = PQunescapeBytea(ptr, &decodedlen);
			image = [[NSData dataWithBytes:decoded length:decodedlen] retain];
			PQfreemem(decoded);
		}
	}

Do I access bytea ("image" is a bytea type) this way, or this way:
		ptr = [res getValue:0 field:0];
		if (ptr != NULL) {
			i   = [res getlength:0 field:0];
			image = [[NSData dataWithBytes:decoded length:i] retain];
		}

Re: Two seperate problems with libpq - arrays and bytea edit

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Teunis Peters  writes:
> I'm trying to figure out how to read from and write to bytea values and 
> array (specifically "text[2][]" for a key:value 'dictionary') and 
> cannot figure it out.

Well, since you are asking for binary output format from PQexecParams,
the bytea result is going to just be raw binary data: you should not be
trying to apply PQunescapeBytea to it.  I don't know the notation you
are using well enough to tell what's wrong with your second variant.

> Arrays I'm completely lost - I haven't a clue what the format would be.

Looking at array_send, the binary wire format is
number of dimensions (int4)flags (int4)element type id (Oid)for each dimension:	dimension length (int4)	dimension lower subscript bound (int4)for each array element:	element value, in the appropriate format

The "flags" are currently always 0, and I would recommend punting if
you see something else, since something else would very possibly
indicate a change in the wire format.

Don't forget that the wire format for integers is network byte order
(ie, big-endian).
		regards, tom lane

FAQ