I have the following code that currently works against DB2.
There's the SQLDA declaration...
extern struct sqlca sqlca;
struct{struct sqlda daNM;
struct sqlvar vaNM[6];
} C_NM={"SQLDA ",280,6,6,
492, 8,(char*)&NMTBL.namn, 0,4,"NAMN",
452, 1,(char*)&NMTBL.ssncd, 0,5,"SSNCD",
497, 4,(char*)&NMTBL.ssn, &NMTBL.Fssn ,3,"SSN",
448, 65,(char*)&NMTBL.lna, 0,3,"LNA",
449, 46,(char*)&NMTBL.fna, &NMTBL.Ffna ,3,"FNA",
449, 80,(char*)&NMTBL.nm2ln, &NMTBL.Fnm2ln ,5,"NM2LN"};
struct sqlda *pNM = (struct sqlda*)&C_NM;
Then there's the cursor declaration...
EXEC SQL DECLARE C_NM_# CURSOR FOR SELECT NAMN, SSNCD, SSN, LNA, FNA, NM2LN FROM ASL.NMTBL
WHERE NAMN = :Q_namn FOR FETCH ONLY;
And finally a method that opens, fetches and closes the cursor.
void fetch_name_nd(__int64 namn,__int32 commit)
{ /** fetch name with no display **/
struct sqlca ret_sqlca;
Q_namn = namn;
EXEC SQL OPEN C_NM_#;
if (sqlca.sqlcode != 0)
show_error("Error in opening nametable in 'REA_LIB'",1);
memset(&NMTBL,0,sizeof(NMTBL));
EXEC SQL FETCH C_NM_# USING DESCRIPTOR :*pNM;
if (sqlca.sqlcode != 0)
if (sqlca.sqlcode == 100)
show_error("Couldn't find the correct nam# in NMTBL",1);
else show_error("Error in Fetching from the NMTBL",1);
ret_sqlca = sqlca;
EXEC SQL CLOSE C_NM_#;
if (sqlca.sqlcode != 0)
show_error("Error in closing table after reading name",1);
if (commit)
{
EXEC SQL COMMIT;
if (sqlca.sqlcode != 0)
show_error("Error commiting after fetching name from Database",1);
}
sqlca = ret_sqlca; /** copy value from fetch **/
}
At the end of that method I have the database values available in a struct to use wherever I'd like, NMTBL.lna, etc.
I've read through https://www.postgresql.org/docs/current/static/ecpg.html and https://www.postgresql.org/docs/current/static/ecpg-variables.html, it all seems very similar to DB2.
However https://www.postgresql.org/docs/9.1/static/ecpg-descriptors.html loses me when it comes to my SQLDA definitions and usage.
Is there anyone that uses similar SQLDA descriptors to query data in embedded SQL C/C++ programs against a PostgreSQL database that might be able to advise?
It appears ecpg does not allow you to pre-define where the cursor results will go. In the end, rather than pre-defining an SQLDA structure that would place the cursor results in my desired struct, I wrote a function to fill the desired struct from the dynamic sqlda that ecpg filled.
For example, now I just define an sqlda pointer
Then I fetch my cursor into that
Then I call my function to pull the results out of the sqlda pointer and fill my struct.
This walks through each of the members of the sqlvar[] and pulls out the corresponding values.
That lets me access the values from the struct as I did originally, so the rest of the program remains unchanged. If that happens to save someone else some time down the road you're welcome. :) Chances are there really aren't that many people migrating from DB2's embedded sql to PostgreSQL's ecpg embedded sql...but oh well.
Update: Here's a bit of code to take an old SQLDA struct (DB2 style) and generate a function to read a dynamic SQLDA and fill the originally-used struct. You just need a window with two text boxes, txtOld and txtNew.