]> git.saurik.com Git - wxWidgets.git/blob - src/iodbc/execute.c
Added dummy DeleteChildren so the sample will at least link.
[wxWidgets.git] / src / iodbc / execute.c
1 /** Invoke a query
2
3 Copyright (C) 1995 by Ke Jin <kejin@empress.com>
4
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.
9
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.
14 **/
15
16 #include <../iodbc/iodbc.h>
17
18 #include <../iodbc/isql.h>
19 #include <../iodbc/isqlext.h>
20
21 #include <../iodbc/dlproc.h>
22
23 #include <../iodbc/herr.h>
24 #include <../iodbc/henv.h>
25 #include <../iodbc/hdbc.h>
26 #include <../iodbc/hstmt.h>
27
28 #include <../iodbc/itrace.h>
29
30 static void do_cursoropen(STMT_t FAR* pstmt)
31 {
32 RETCODE retcode;
33 SWORD ncol;
34
35 pstmt->state = en_stmt_executed;
36
37 retcode = SQLNumResultCols( pstmt, &ncol );
38
39 if( retcode == SQL_SUCCESS
40 || retcode == SQL_SUCCESS_WITH_INFO )
41 {
42 if( ncol )
43 {
44 pstmt->state = en_stmt_cursoropen;
45 pstmt->cursor_state = en_stmt_cursor_opened;
46 }
47 else
48 {
49 pstmt->state = en_stmt_executed;
50 pstmt->cursor_state = en_stmt_cursor_no;
51 }
52 }
53 }
54
55 RETCODE SQL_API SQLExecute ( HSTMT hstmt )
56 {
57 STMT_t FAR* pstmt = (STMT_t FAR*)hstmt;
58 HPROC hproc = SQL_NULL_HPROC;
59 RETCODE retcode;
60
61 int sqlstat = en_00000;
62
63 if( hstmt == SQL_NULL_HSTMT
64 || pstmt->hdbc == SQL_NULL_HDBC )
65 {
66 return SQL_INVALID_HANDLE;
67 }
68
69 /* check state */
70 if( pstmt->asyn_on == en_NullProc )
71 {
72 switch( pstmt->state )
73 {
74 case en_stmt_allocated:
75 sqlstat = en_S1010;
76 break;
77
78 case en_stmt_executed:
79 if( ! pstmt->prep_state )
80 {
81 sqlstat = en_S1010;
82 }
83 break;
84
85 case en_stmt_cursoropen:
86 if( ! pstmt->prep_state )
87 {
88 sqlstat = en_S1010;
89 }
90 break;
91
92 case en_stmt_fetched:
93 case en_stmt_xfetched:
94 if( ! pstmt->prep_state )
95 {
96 sqlstat = en_S1010;
97 }
98 else
99 {
100 sqlstat = en_24000;
101 }
102 break;
103
104 case en_stmt_needdata:
105 case en_stmt_mustput:
106 case en_stmt_canput:
107 sqlstat = en_S1010;
108 break;
109
110 default:
111 break;
112 }
113 }
114 else if( pstmt->asyn_on != en_Execute )
115 {
116 sqlstat = en_S1010;
117 }
118
119 if( sqlstat == en_00000 )
120 {
121 hproc = _iodbcdm_getproc( pstmt->hdbc, en_Execute );
122
123 if( hproc == SQL_NULL_HPROC )
124 {
125 sqlstat = en_IM001;
126 }
127 }
128
129 if( sqlstat != en_00000 )
130 {
131 PUSHSQLERR ( pstmt->herr, sqlstat );
132
133 return SQL_ERROR;
134 }
135
136 CALL_DRIVER ( pstmt->hdbc, retcode, hproc,
137 en_Execute, ( pstmt->dhstmt ) )
138
139 #if 0
140 retcode = hproc ( pstmt->dhstmt );
141 #endif
142
143 /* stmt state transition */
144 if( pstmt->asyn_on == en_Execute )
145 {
146 switch ( retcode )
147 {
148 case SQL_SUCCESS:
149 case SQL_SUCCESS_WITH_INFO:
150 case SQL_NEED_DATA:
151 case SQL_ERROR:
152 pstmt->asyn_on = en_NullProc;
153 break;
154
155 case SQL_STILL_EXECUTING:
156 default:
157 return retcode;
158 }
159 }
160
161 switch( pstmt->state )
162 {
163 case en_stmt_prepared:
164 switch( retcode )
165 {
166 case SQL_SUCCESS:
167 case SQL_SUCCESS_WITH_INFO:
168 do_cursoropen(hstmt);
169 break;
170
171 case SQL_NEED_DATA:
172 pstmt->state = en_stmt_needdata;
173 pstmt->need_on = en_Execute;
174 break;
175
176 case SQL_STILL_EXECUTING:
177 pstmt->asyn_on = en_Execute;
178 break;
179
180 default:
181 break;
182 }
183 break;
184
185 case en_stmt_executed:
186 switch( retcode )
187 {
188 case SQL_ERROR:
189 pstmt->state = en_stmt_allocated;
190 pstmt->cursor_state = en_stmt_cursor_no;
191 pstmt->prep_state = 0;
192 break;
193
194 case SQL_NEED_DATA:
195 pstmt->state = en_stmt_needdata;
196 pstmt->need_on = en_Execute;
197 break;
198
199 case SQL_STILL_EXECUTING:
200 pstmt->asyn_on = en_Execute;
201 break;
202
203 default:
204 break;
205 }
206 break;
207
208 default:
209 break;
210 }
211
212 return retcode;
213 }
214
215 RETCODE SQL_API SQLExecDirect (
216 HSTMT hstmt,
217 UCHAR FAR* szSqlStr,
218 SDWORD cbSqlStr )
219 {
220 STMT_t FAR* pstmt = (STMT_t FAR*)hstmt;
221 HPROC hproc = SQL_NULL_HPROC;
222
223 int sqlstat = en_00000;
224 RETCODE retcode = SQL_SUCCESS;
225
226 if( hstmt == SQL_NULL_HSTMT
227 || pstmt->hdbc == SQL_NULL_HDBC )
228 {
229 return SQL_INVALID_HANDLE;
230 }
231
232 /* check arguments */
233 if( szSqlStr == NULL )
234 {
235 sqlstat = en_S1009;
236 }
237 else if( cbSqlStr < 0 && cbSqlStr != SQL_NTS )
238 {
239 sqlstat = en_S1090;
240 }
241
242 if( sqlstat != en_00000 )
243 {
244 PUSHSQLERR ( pstmt->herr, sqlstat );
245
246 return SQL_ERROR;
247 }
248
249 /* check state */
250 if( pstmt->asyn_on == en_NullProc )
251 {
252 switch ( pstmt->state )
253 {
254 case en_stmt_fetched:
255 case en_stmt_xfetched:
256 sqlstat = en_24000;
257 break;
258
259 case en_stmt_needdata:
260 case en_stmt_mustput:
261 case en_stmt_canput:
262 sqlstat = en_S1010;
263 break;
264
265 default:
266 break;
267 }
268 }
269 else if( pstmt->asyn_on != en_ExecDirect )
270 {
271 sqlstat = en_S1010;
272 }
273
274 if( sqlstat != en_00000 )
275 {
276 PUSHSQLERR ( pstmt->herr, sqlstat );
277
278 return SQL_ERROR;
279 }
280
281 hproc = _iodbcdm_getproc( pstmt->hdbc, en_ExecDirect);
282
283 if( hproc == SQL_NULL_HPROC )
284 {
285 PUSHSQLERR ( pstmt->herr, en_IM001 );
286
287 return SQL_ERROR;
288 }
289
290 CALL_DRIVER ( pstmt->hdbc, retcode, hproc, en_ExecDirect, (
291 pstmt->dhstmt, szSqlStr, cbSqlStr) )
292
293
294 #if 0
295 retcode = hproc ( pstmt->dhstmt, szSqlStr, cbSqlStr );
296 #endif
297
298 /* stmt state transition */
299 if( pstmt->asyn_on == en_ExecDirect )
300 {
301 switch ( retcode )
302 {
303 case SQL_SUCCESS:
304 case SQL_SUCCESS_WITH_INFO:
305 case SQL_NEED_DATA:
306 case SQL_ERROR:
307 pstmt->asyn_on = en_NullProc;
308 break;
309
310 case SQL_STILL_EXECUTING:
311 default:
312 return retcode;
313 }
314 }
315
316 if( pstmt->state <= en_stmt_executed )
317 {
318 switch( retcode )
319 {
320 case SQL_SUCCESS:
321 case SQL_SUCCESS_WITH_INFO:
322 do_cursoropen(hstmt);
323 break;
324
325 case SQL_NEED_DATA:
326 pstmt->state = en_stmt_needdata;
327 pstmt->need_on = en_ExecDirect;
328 break;
329
330 case SQL_STILL_EXECUTING:
331 pstmt->asyn_on = en_ExecDirect;
332 break;
333
334 case SQL_ERROR:
335 pstmt->state = en_stmt_allocated;
336 pstmt->cursor_state = en_stmt_cursor_no;
337 pstmt->prep_state = 0;
338 break;
339
340 default:
341 break;
342 }
343 }
344
345 return retcode;
346 }
347
348 RETCODE SQL_API SQLPutData(
349 HSTMT hstmt,
350 PTR rgbValue,
351 SDWORD cbValue )
352 {
353 STMT_t FAR* pstmt = (STMT_t FAR*)hstmt;
354 HPROC hproc;
355 RETCODE retcode;
356
357 if( hstmt == SQL_NULL_HSTMT
358 || pstmt->hdbc == SQL_NULL_HDBC )
359 {
360 return SQL_INVALID_HANDLE;
361 }
362
363 /* check argument value */
364 if( rgbValue == NULL
365 && ( cbValue != SQL_DEFAULT_PARAM
366 && cbValue != SQL_NULL_DATA ) )
367 {
368 PUSHSQLERR ( pstmt->herr, en_S1009 );
369
370 return SQL_ERROR;
371 }
372
373 /* check state */
374 if( pstmt->asyn_on == en_NullProc )
375 {
376 if( pstmt->state <= en_stmt_xfetched )
377 {
378 PUSHSQLERR( pstmt->herr, en_S1010 );
379
380 return SQL_ERROR;
381 }
382 }
383 else if( pstmt->asyn_on != en_PutData )
384 {
385 PUSHSQLERR ( pstmt->herr, en_S1010 );
386
387 return SQL_ERROR;
388 }
389
390 /* call driver */
391 hproc = _iodbcdm_getproc( pstmt->hdbc, en_PutData );
392
393 if( hproc == SQL_NULL_HPROC )
394 {
395 PUSHSQLERR ( pstmt->herr, en_IM001 );
396
397 return SQL_ERROR;
398 }
399
400 CALL_DRIVER ( pstmt->hdbc, retcode, hproc, en_PutData, (
401 pstmt->dhstmt, rgbValue, cbValue ) )
402
403 #if 0
404 retcode = hproc(pstmt->dhstmt, rgbValue, cbValue );
405 #endif
406
407 /* state transition */
408 if( pstmt->asyn_on == en_PutData )
409 {
410 switch( retcode )
411 {
412 case SQL_SUCCESS:
413 case SQL_SUCCESS_WITH_INFO:
414 case SQL_ERROR:
415 pstmt->asyn_on = en_NullProc;
416 break;
417
418 case SQL_STILL_EXECUTING:
419 default:
420 return retcode;
421 }
422 }
423
424 /* must in mustput or canput states */
425 switch( retcode )
426 {
427 case SQL_SUCCESS:
428 case SQL_SUCCESS_WITH_INFO:
429 pstmt->state = en_stmt_canput;
430 break;
431
432 case SQL_ERROR:
433 switch( pstmt->need_on )
434 {
435 case en_ExecDirect:
436 pstmt->state = en_stmt_allocated;
437 pstmt->need_on = en_NullProc;
438 break;
439
440 case en_Execute:
441 if( pstmt->prep_state )
442 {
443 pstmt->state = en_stmt_prepared;
444 pstmt->need_on = en_NullProc;
445 }
446 break;
447
448 case en_SetPos:
449 /* Is this possible ???? */
450 pstmt->state = en_stmt_xfetched;
451 break;
452
453 default:
454 break;
455 }
456 break;
457
458 case SQL_STILL_EXECUTING:
459 pstmt->asyn_on = en_PutData;
460 break;
461
462 default:
463 break;
464 }
465
466 return retcode;
467 }
468
469 RETCODE SQL_API SQLParamData (
470 HSTMT hstmt,
471 PTR FAR* prgbValue )
472 {
473 STMT_t FAR* pstmt = (STMT_t FAR*)hstmt;
474 HPROC hproc;
475 RETCODE retcode;
476
477 if( hstmt == SQL_NULL_HSTMT
478 || pstmt->hdbc == SQL_NULL_HDBC )
479 {
480 return SQL_INVALID_HANDLE;
481 }
482
483 /* check argument */
484
485 /* check state */
486 if( pstmt->asyn_on == en_NullProc )
487 {
488 if( pstmt->state <= en_stmt_xfetched )
489 {
490 PUSHSQLERR ( pstmt->herr, en_S1010 );
491
492 return SQL_ERROR;
493 }
494 }
495 else if( pstmt->asyn_on != en_ParamData )
496 {
497 PUSHSQLERR ( pstmt->herr, en_S1010 );
498
499 return SQL_ERROR;
500 }
501
502 /* call driver */
503 hproc = _iodbcdm_getproc( pstmt->hdbc, en_ParamData );
504
505 if( hproc == SQL_NULL_HPROC )
506 {
507 PUSHSQLERR ( pstmt->herr, en_IM001 );
508
509 return SQL_ERROR;
510 }
511
512 CALL_DRIVER ( pstmt->hdbc, retcode, hproc, en_ParamData, (
513 pstmt->dhstmt, prgbValue ) )
514
515 #if 0
516 retcode = hproc ( pstmt->dhstmt, prgbValue );
517 #endif
518
519 /* state transition */
520 if( pstmt->asyn_on == en_ParamData )
521 {
522 switch( retcode )
523 {
524 case SQL_SUCCESS:
525 case SQL_SUCCESS_WITH_INFO:
526 case SQL_ERROR:
527 pstmt->asyn_on = en_NullProc;
528 break;
529
530 case SQL_STILL_EXECUTING:
531 default:
532 return retcode;
533 }
534 }
535
536 if( pstmt->state < en_stmt_needdata )
537 {
538 return retcode;
539 }
540
541 switch( retcode )
542 {
543 case SQL_ERROR:
544 switch( pstmt->need_on )
545 {
546 case en_ExecDirect:
547 pstmt->state = en_stmt_allocated;
548 break;
549
550 case en_Execute:
551 pstmt->state = en_stmt_prepared;
552 break;
553
554 case en_SetPos:
555 pstmt->state = en_stmt_xfetched;
556 pstmt->cursor_state
557 = en_stmt_cursor_xfetched;
558 break;
559
560 default:
561 break;
562 }
563 pstmt->need_on = en_NullProc;
564 break;
565
566 case SQL_SUCCESS:
567 case SQL_SUCCESS_WITH_INFO:
568 switch( pstmt->state )
569 {
570 case en_stmt_needdata:
571 pstmt->state = en_stmt_mustput;
572 break;
573
574 case en_stmt_canput:
575 switch( pstmt->need_on )
576 {
577 case en_SetPos:
578 pstmt->state
579 = en_stmt_xfetched;
580 pstmt->cursor_state
581 = en_stmt_cursor_xfetched;
582 break;
583
584 case en_ExecDirect:
585 case en_Execute:
586 do_cursoropen(hstmt);
587 break;
588
589 default:
590 break;
591 }
592 break;
593
594 default:
595 break;
596 }
597 pstmt->need_on = en_NullProc;
598 break;
599
600 case SQL_NEED_DATA:
601 pstmt->state = en_stmt_mustput;
602 break;
603
604 default:
605 break;
606 }
607
608 return retcode;
609 }
610
611
612 RETCODE SQL_API SQLNumParams (
613 HSTMT hstmt,
614 SWORD FAR* pcpar )
615 {
616 STMT_t FAR* pstmt = (STMT_t FAR*)hstmt;
617 HPROC hproc;
618 RETCODE retcode;
619
620 if( hstmt == SQL_NULL_HSTMT
621 || pstmt->hdbc == SQL_NULL_HDBC )
622 {
623 return SQL_INVALID_HANDLE;
624 }
625
626 /* check argument */
627
628 /* check state */
629 if( pstmt->asyn_on == en_NullProc )
630 {
631 switch( pstmt->state )
632 {
633 case en_stmt_allocated:
634 case en_stmt_needdata:
635 case en_stmt_mustput:
636 case en_stmt_canput:
637 PUSHSQLERR ( pstmt->herr, en_S1010 );
638 return SQL_ERROR;
639
640 default:
641 break;
642 }
643 }
644 else if( pstmt->asyn_on != en_NumParams )
645 {
646 PUSHSQLERR ( pstmt->herr, en_S1010 );
647
648 return SQL_ERROR;
649 }
650
651 /* call driver */
652 hproc = _iodbcdm_getproc ( pstmt->hdbc, en_NumParams );
653
654 if( hproc == SQL_NULL_HPROC )
655 {
656 PUSHSQLERR ( pstmt->herr, en_IM001 );
657
658 return SQL_ERROR;
659 }
660
661 CALL_DRIVER ( pstmt->hdbc, retcode, hproc, en_NumParams, (
662 pstmt->dhstmt, pcpar) )
663
664
665 #if 0
666 retcode = hproc ( pstmt->dhstmt, pcpar );
667 #endif
668
669 /* state transition */
670 if( pstmt->asyn_on == en_NumParams )
671 {
672 switch ( retcode )
673 {
674 case SQL_SUCCESS:
675 case SQL_SUCCESS_WITH_INFO:
676 case SQL_ERROR:
677 break;
678
679 default:
680 return retcode;
681 }
682 }
683
684 if( retcode == SQL_STILL_EXECUTING )
685 {
686 pstmt->asyn_on = en_NumParams;
687 }
688
689 return retcode;
690 }
691
692 RETCODE SQL_API SQLDescribeParam (
693 HSTMT hstmt,
694 UWORD ipar,
695 SWORD FAR* pfSqlType,
696 UDWORD FAR* pcbColDef,
697 SWORD FAR* pibScale,
698 SWORD FAR* pfNullable )
699 {
700 STMT_t FAR* pstmt = (STMT_t FAR*)hstmt;
701 HPROC hproc;
702 RETCODE retcode;
703
704 if( hstmt == SQL_NULL_HSTMT
705 || pstmt->hdbc == SQL_NULL_HDBC )
706 {
707 return SQL_INVALID_HANDLE;
708 }
709
710 /* check argument */
711 if( ipar == 0 )
712 {
713 PUSHSQLERR ( pstmt->herr, en_S1093 );
714
715 return SQL_ERROR;
716 }
717
718 /* check state */
719 if( pstmt->asyn_on == en_NullProc )
720 {
721 switch( pstmt->state )
722 {
723 case en_stmt_allocated:
724 case en_stmt_needdata:
725 case en_stmt_mustput:
726 case en_stmt_canput:
727 PUSHSQLERR ( pstmt->herr, en_S1010 );
728 return SQL_ERROR;
729
730 default:
731 break;
732 }
733 }
734 else if( pstmt->asyn_on != en_DescribeParam )
735 {
736 PUSHSQLERR ( pstmt->herr, en_S1010 );
737
738 return SQL_ERROR;
739 }
740
741 /* call driver */
742 hproc = _iodbcdm_getproc ( pstmt->hdbc, en_DescribeParam );
743
744 if( hproc == SQL_NULL_HPROC )
745 {
746 PUSHSQLERR ( pstmt->herr, en_IM001 );
747
748 return SQL_ERROR;
749 }
750
751 CALL_DRIVER ( pstmt->hdbc, retcode, hproc, en_DescribeParam, (
752 pstmt->dhstmt,
753 ipar,
754 pfSqlType,
755 pcbColDef,
756 pibScale,
757 pfNullable ) )
758
759 #if 0
760 retcode = hproc(pstmt->dhstmt,
761 ipar,
762 pfSqlType,
763 pcbColDef,
764 pibScale,
765 pfNullable );
766 #endif
767
768 /* state transition */
769 if( pstmt->asyn_on == en_DescribeParam )
770 {
771 switch ( retcode )
772 {
773 case SQL_SUCCESS:
774 case SQL_SUCCESS_WITH_INFO:
775 case SQL_ERROR:
776 break;
777
778 default:
779 return retcode;
780 }
781 }
782
783 if( retcode == SQL_STILL_EXECUTING )
784 {
785 pstmt->asyn_on = en_DescribeParam;
786 }
787
788 return retcode;
789 }