X-Original-To: pgsql-interfaces-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 261F8D8B78 for ; Mon, 12 Sep 2005 11:17:38 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 41040-09 for ; Mon, 12 Sep 2005 14:17:28 +0000 (GMT) Received: from nproxy.gmail.com (nproxy.gmail.com [64.233.182.204]) by svr1.postgresql.org (Postfix) with ESMTP id 54E1AD86E1 for ; Mon, 12 Sep 2005 11:17:28 -0300 (ADT) Received: by nproxy.gmail.com with SMTP id c2so404492nfe for ; Mon, 12 Sep 2005 07:17:33 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=WWRzdg7wQhVNPptvLjakwuhH6kDezVqkp8XtCcYd/lIE5xVesxRG6eEx2LWpfDOp3RuxLVSZLMLOH4X7zYcTmiARPW1SSXuy4PWJhgkq9Zj+J6ykpcudcquLPR0+MX1kvTZFJKnLkxXPBiD9Sj69wYRcZbD5H5VrcNkr23TH2vs= Received: by 10.48.31.13 with SMTP id e13mr55583nfe; Mon, 12 Sep 2005 07:17:33 -0700 (PDT) Received: by 10.48.250.5 with HTTP; Mon, 12 Sep 2005 07:17:33 -0700 (PDT) Message-ID: <1532344d050912071740e7c5d9@mail.gmail.com> Date: Mon, 12 Sep 2005 16:17:33 +0200 From: fabrizio picca Reply-To: fabrizio.picca@gmail.com To: pgsql-interfaces@postgresql.org Subject: help needed on SPI_modifytuple. Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.024 required=5 tests=[RCVD_BY_IP=0.024] X-Spam-Level: X-Archive-Number: 200509/13 X-Sequence-Number: 5736 I'm trying to sipmply modify a tuple before it will be inserted in the db. The problem is that when it try to insert the modified tuple with SPI_modifytuple all i get is a SPI_ERROR_ARGUMENT negative (-6) . Could someone help me? i'm reallygoing crazy. What i did is just a fire-before C trigger that acts like this: #include #include /* this is what you need to work with SPI *= / #include /* ... and triggers */ #include #include extern Datum trgupdana(PG_FUNCTION_ARGS); char *checkFieldData(char *); PG_FUNCTION_INFO_V1(trgupdana); Datum trgupdana(PG_FUNCTION_ARGS) { TriggerData *trigdata =3D (TriggerData *) fcinfo->context; TupleDesc tupdesc; HeapTuple rettuple, =09=09oldtuple, =09=09newtuple; char *rs1,*rs2,*rs3,*relname; int ret, i,j; bool isnull; Relation rel; /* make sure it's called as a trigger at all */ if (!CALLED_AS_TRIGGER(fcinfo)) elog(ERROR, "trgchkneg: not called by trigger manager\n"); if (TRIGGER_FIRED_BEFORE(trigdata->tg_event) && TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) //trigger fires when called for rows and before updating { tupdesc =3D trigdata->tg_relation->rd_att;=20 oldtuple =3D trigdata->tg_trigtuple;=20 =09newtuple =3D trigdata->tg_newtuple;=20 =09rettuple=3DNULL; =09rel =3D trigdata->tg_relation; =09relname=3D SPI_getrelname(rel); =09=09 =09if ((ret =3D SPI_connect()) < 0) // SPI manager initialization =09=09elog(NOTICE, "trgCheckNeg : SPI_connect returned: %d", ret); =09=09 =20 rs1=3DSPI_getvalue(oldtuple,tupdesc,5); rs2=3DSPI_getvalue(oldtuple,tupdesc,6); =20 =09elog(NOTICE,"%s,%s",rs1,rs2); =09 int attnum; Datum new_value; =20 =09rs3=3D(char*) malloc(80); sprintf(rs3,""); =09 =09if(rs1[strlen(rs1)-1]=3D=3D'&'){ rs3=3Dstrncat(rs3,rs1,strlen(rs1)-1); =09 rs3=3Dstrcat(rs3,"E "); =09}=20 =09else if(rs1[strlen(rs1)-1]=3D=3D',') =09{ =09 rs3=3Dstrncat(rs3,rs1,strlen(rs1)-1); =09 rs3=3Dstrcat(rs3,", "); =09} else { =09 rs3=3Drs1;=09 =09} =20 elog(NOTICE,"1:%s",rs3);=09 =09 =09if(strlen(rs2)!=3D0){ =09 rs3=3Dstrcat(rs3,rs2); =09} =09 =09elog(NOTICE,"2:%s",rs3); =09 =09new_value=3DDirectFunctionCall1(textin,PointerGetDatum(checkFieldData(rs= 3))); =09 attnum=3DSPI_fnumber(tupdesc,"ARGSL1"); =20 if(rel=3D=3DNULL) elog(NOTICE,"rel NULL"); =09if(&attnum=3D=3DNULL) elog(NOTICE,"attnum NULL"); =09if(&new_value=3D=3DNULL) elog(NOTICE,"new_value NULL"); =09if(&isnull=3D=3DNULL) elog(NOTICE,"isnull NULL"); =09=09 rettuple=3DSPI_modifytuple(rel,newtuple,1,&attnum,&new_value,&isnul= l); =09if(rettuple=3D=3DNULL){ =09=09elog(ERROR,"trgupdana (%s):%d returned by SPI_modifytuple",relname,SPI_result); =09} =09return PointerGetDatum(rettuple); } return PointerGetDatum(oldtuple); } char *checkFieldData(char *valueToCheck){ =09int i=3D0; =09for(i=3D0;i