dbTalk Databases Forums  

Use of sqlca for error reporting

comp.databases.postgresql comp.databases.postgresql


Discuss Use of sqlca for error reporting in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Al Balmer
 
Posts: n/a

Default Use of sqlca for error reporting - 11-06-2006 , 06:24 PM






We're converting an Informix application to Postgresql. We relied on
the sqlca structure to report the number of rows changed and errors,
but the structure seems to be always set to 0. Here's some actual code
(Translated literally from the Informix code, no attempt to optimize,
so if it looks especially dumb, that's my excuse :-) Am I missing
something obvious?

EXEC SQL execute immediate :stmt;
/* stmt is an update statement, presumed to be OK since the
update actually happens */

if(error = sqlerror()) goto sqlcleanup

EXEC SQL commit;

/* this never shows any error */
if(error = sqlerror()) goto sqlcleanup

/* this always shows zero rows */
if(error = sqlnorows()) goto sqlcleanup


/* The error checking and reporting */
int sqlerror()
{
static char msg[MSG_LEN];

EXEC SQL begin declare section;
int exception_count;
int i;
char sqlstate[6];
char message_text_val[255];
EXEC SQL end declare section;


last_sqlcode = sqlca.sqlcode;

if(sqlca.sqlcode)
{
sprintf(msg, "SQLCODE = %ld", last_sqlcode);
LOGVIDERR(5, msg);

sprintf(msg, "sqlcode %ld, sqlstate %5.5s: %s", sqlca.sqlcode,
sqlca.sqlstate, sqlca.sqlerrm.sqlerrmc);
LOGVIDERR(5, msg);

return 1;
}
return 0;
}

/* the rows affected counter */
int sqlnorows()
{
int row_count = sqlca.sqlerrd[2];

if(row_count == 0)
{
/* following three statements are added for debug.
Log shows all zeros. */
char tmp[200];
sprintf(tmp, "ssqlerrd: '%s' %ld, %ld, %ld, %ld, %ld, %ld",

sqlca.sqlerrm.sqlerrmc,sqlca.sqlerrd[0],sqlca.sqlerrd[1],sqlca.sqlerrd[2],
sqlca.sqlerrd[3],sqlca.sqlerrd[4],sqlca.sqlerrd[5]);
LOGVIDERR(5, tmp);
/* end of debug code */
LOGVIDERR(5, "No rows effected.");
return 1;
}
return 0;
}


--
Al Balmer
Sun City, AZ

Reply With Quote
  #2  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: Use of sqlca for error reporting - 11-07-2006 , 04:19 AM






Al Balmer <albalmer (AT) att (DOT) net> wrote:
Quote:
We're converting an Informix application to Postgresql. We relied on
the sqlca structure to report the number of rows changed and errors,
but the structure seems to be always set to 0. Here's some actual code
(Translated literally from the Informix code, no attempt to optimize,
so if it looks especially dumb, that's my excuse :-) Am I missing
something obvious?
I tried your code, and I think it behaves as expected.
I'll add comments to the code where appropriate.

Quote:
EXEC SQL execute immediate :stmt;
/* stmt is an update statement, presumed to be OK since the
update actually happens */

if(error = sqlerror()) goto sqlcleanup
You should insert the call to sqlnorows() here if you want to get the
rowcount from the UPDATE statement.

Quote:
EXEC SQL commit;

/* this never shows any error */
if(error = sqlerror()) goto sqlcleanup
It is hardly surprising that no error is reported, as the COMMIT
statement executes correctly according to your comment above.

Quote:
/* this always shows zero rows */
if(error = sqlnorows()) goto sqlcleanup
This is also as expected, as COMMIT affects zero rows.

Quote:
/* The error checking and reporting */
int sqlerror()
{
[...]
if(sqlca.sqlcode)
{
[...]
return 1;
}
return 0;
}

/* the rows affected counter */
int sqlnorows()
{
int row_count = sqlca.sqlerrd[2];

if(row_count == 0)
{
[...]
LOGVIDERR(5, "No rows effected.");
return 1;
}
return 0;
}
Summary: it seems that in Informix a COMMIT statement will not set the
number of rows affected in the sqlca, else your code would not work there.

Yours,
Laurenz Albe


Reply With Quote
  #3  
Old   
Al Balmer
 
Posts: n/a

Default Re: Use of sqlca for error reporting - 11-07-2006 , 11:28 AM



On 07 Nov 2006 09:19:32 GMT, Laurenz Albe <invite (AT) spam (DOT) to.invalid>
wrote:

Quote:
/* this always shows zero rows */
if(error = sqlnorows()) goto sqlcleanup

This is also as expected, as COMMIT affects zero rows.
That's it! That's the point we were missing. If you ever get near
Phoenix, Arizona, USA, I'll buy lunch <g>.

--
Al Balmer
Sun City, AZ


Reply With Quote
  #4  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: Use of sqlca for error reporting - 11-08-2006 , 03:12 AM



Al Balmer <albalmer (AT) att (DOT) net> wrote:
Quote:
That's it! That's the point we were missing. If you ever get near
Phoenix, Arizona, USA, I'll buy lunch <g>.
If the e-mail address is valid, I'll collect it some day :^)

Yours,
Laurenz Albe


Reply With Quote
  #5  
Old   
Al Balmer
 
Posts: n/a

Default Re: Use of sqlca for error reporting - 11-08-2006 , 10:54 AM



On 08 Nov 2006 08:12:06 GMT, Laurenz Albe <invite (AT) spam (DOT) to.invalid>
wrote:

Quote:
Al Balmer <albalmer (AT) att (DOT) net> wrote:
That's it! That's the point we were missing. If you ever get near
Phoenix, Arizona, USA, I'll buy lunch <g>.

If the e-mail address is valid,
It is.

Quote:
I'll collect it some day :^)
I'll look forward to it.
Quote:
Yours,
Laurenz Albe
--
Al Balmer
Sun City, AZ


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.