]>
git.saurik.com Git - wxWidgets.git/blob - src/iodbc/herr.c
1 /** Error stack management functions
3 Copyright (C) 1995 by Ke Jin <kejin@empress.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
16 #include <../iodbc/iodbc.h>
18 #include <../iodbc/isql.h>
19 #include <../iodbc/isqlext.h>
21 #include <../iodbc/dlproc.h>
23 #include <../iodbc/herr.h>
24 #include <../iodbc/henv.h>
25 #include <../iodbc/hdbc.h>
26 #include <../iodbc/hstmt.h>
28 #include <../iodbc/itrace.h>
30 #include "../iodbc/herr.ci"
34 static HERR
_iodbcdm_popsqlerr( HERR herr
)
36 sqlerr_t
* list
= (sqlerr_t
*)herr
;
39 if( herr
== SQL_NULL_HERR
)
51 void _iodbcdm_freesqlerrlist( HERR herrlist
)
55 for(list
= herrlist
; list
!= 0; )
57 list
= _iodbcdm_popsqlerr(list
);
61 HERR
_iodbcdm_pushsqlerr (
67 sqlerr_t
* perr
= (sqlerr_t
*)herr
;
70 if(herr
!= SQL_NULL_HERR
)
76 /* over wirte the top entry to prevent error stack blow out */
84 ebuf
= (sqlerr_t
*)MEM_ALLOC (sizeof(sqlerr_t
));
94 ebuf
->next
= (sqlerr_t
*)herr
;
99 static char FAR
* _iodbcdm_getsqlstate (
103 sqlerr_t
* perr
= (sqlerr_t
*)herr
;
106 if( herr
== SQL_NULL_HERR
|| tab
== NULL
)
108 return (char FAR
*)NULL
;
112 ptr
->code
!= en_sqlstat_total
;
115 if(ptr
->code
== perr
->code
)
117 return (char FAR
*)(ptr
->stat
);
121 return (char FAR
*)NULL
;
124 static char FAR
* _iodbcdm_getsqlerrmsg(
128 sqlerr_t
* perr
= (sqlerr_t
*)herr
;
131 if( herr
== SQL_NULL_HERR
)
136 if( perr
->msg
== NULL
&& errtab
== NULL
)
141 if( perr
->msg
!= NULL
)
146 for( ptr
= (sqlerrmsg_t
*)errtab
;
147 ptr
->code
!= en_sqlstat_total
;
150 if( ptr
->code
== perr
->code
)
152 return (char FAR
*)ptr
->msg
;
156 return (char FAR
*)NULL
;
159 RETCODE SQL_API
SQLError (
163 UCHAR FAR
* szSqlstate
,
164 SDWORD FAR
* pfNativeError
,
165 UCHAR FAR
* szErrorMsg
,
167 SWORD FAR
* pcbErrorMsg
)
169 GENV_t FAR
* genv
= (GENV_t FAR
*) henv
;
170 DBC_t FAR
* pdbc
= (DBC_t FAR
*) hdbc
;
171 STMT_t FAR
* pstmt
= (STMT_t FAR
*)hstmt
;
174 HENV dhenv
= SQL_NULL_HENV
;
175 HDBC dhdbc
= SQL_NULL_HDBC
;
176 HSTMT dhstmt
= SQL_NULL_HSTMT
;
178 HERR herr
= SQL_NULL_HERR
;
179 HPROC hproc
= SQL_NULL_HPROC
;
181 char FAR
* errmsg
= NULL
;
182 char FAR
* ststr
= NULL
;
185 RETCODE retcode
= SQL_SUCCESS
;
187 if( hstmt
!= SQL_NULL_HSTMT
) /* retrive stmt err */
192 if( thdbc
== SQL_NULL_HDBC
)
194 return SQL_INVALID_HANDLE
;
196 hproc
= _iodbcdm_getproc( thdbc
, en_Error
);
197 dhstmt
= pstmt
->dhstmt
;
200 else if( hdbc
!= SQL_NULL_HDBC
) /* retrive dbc err */
204 if( thdbc
== SQL_NULL_HDBC
)
206 return SQL_INVALID_HANDLE
;
208 hproc
= _iodbcdm_getproc( thdbc
, en_Error
);
212 if( herr
== SQL_NULL_HERR
213 && pdbc
->henv
== SQL_NULL_HENV
)
215 return SQL_NO_DATA_FOUND
;
218 else if( henv
!= SQL_NULL_HENV
) /* retrive env err */
222 /* Drivers shouldn't push error message
223 * on envoriment handle */
225 if( herr
== SQL_NULL_HERR
)
227 return SQL_NO_DATA_FOUND
;
234 return SQL_INVALID_HANDLE
;
237 if( szErrorMsg
!= NULL
)
239 if( cbErrorMsgMax
< 0
240 || cbErrorMsgMax
> SQL_MAX_MESSAGE_LENGTH
- 1 )
243 /* SQLError() doesn't post error for itself */
247 if( herr
== SQL_NULL_HERR
) /* no err on drv mng */
250 if( hproc
== SQL_NULL_HPROC
)
252 PUSHSQLERR ( herr
, en_IM001
);
257 CALL_DRIVER ( thdbc
, retcode
, hproc
, en_Error
, (
258 dhenv
, dhdbc
, dhstmt
,
259 szSqlstate
, pfNativeError
,
260 szErrorMsg
, cbErrorMsgMax
, pcbErrorMsg
) )
263 retcode
= hproc(dhenv
, dhdbc
, dhstmt
,
264 szSqlstate
, pfNativeError
,
265 szErrorMsg
, cbErrorMsgMax
, pcbErrorMsg
);
271 if( szSqlstate
!= NULL
)
275 /* get sql state string */
276 ststr
= (char FAR
*)_iodbcdm_getsqlstate( herr
,
277 (void FAR
*)sqlerrmsg_tab
);
285 len
= (int)STRLEN(ststr
);
288 STRNCPY ( szSqlstate
, ststr
, len
);
290 /* buffer size of szSqlstate is not checked. Applications
291 * suppose provide enough ( not less than 6 bytes ) buffer
296 if( pfNativeError
!= NULL
)
298 /* native error code is specific to data source */
299 *pfNativeError
= (SDWORD
)0L;
302 if( szErrorMsg
== NULL
|| cbErrorMsgMax
== 0 )
304 if( pcbErrorMsg
!= NULL
)
306 *pcbErrorMsg
= (SWORD
)0;
312 char msgbuf
[256] = { '\0' };
314 /* get sql state message */
315 errmsg
= _iodbcdm_getsqlerrmsg(herr
,
316 (void FAR
*)sqlerrmsg_tab
);
320 errmsg
= (char FAR
*)"";
323 sprintf(msgbuf
, "%s%s", sqlerrhd
, errmsg
);
325 len
= STRLEN( msgbuf
);
327 if( len
< cbErrorMsgMax
- 1 )
329 retcode
= SQL_SUCCESS
;
333 len
= cbErrorMsgMax
- 1;
334 retcode
= SQL_SUCCESS_WITH_INFO
;
335 /* and not posts error for itself */
338 STRNCPY((char*)szErrorMsg
, msgbuf
, len
);
341 if( pcbErrorMsg
!= NULL
)
343 *pcbErrorMsg
= (SWORD
)len
;
347 switch(handle
) /* free this err */
350 genv
->herr
= _iodbcdm_popsqlerr(genv
->herr
);
354 pdbc
->herr
= _iodbcdm_popsqlerr(pdbc
->herr
);
358 pstmt
->herr
= _iodbcdm_popsqlerr(pstmt
->herr
);