File: helper.c

package info (click to toggle)
unixodbc 2.2.11-16
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 17,332 kB
  • ctags: 12,399
  • sloc: ansic: 116,624; cpp: 29,333; sh: 25,024; makefile: 3,002; lex: 241; yacc: 182; perl: 142; sed: 16; sql: 1
file content (991 lines) | stat: -rw-r--r-- 36,788 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/*
 *  $Id: helper.c,v 1.2 2001/12/13 13:00:34 lurcher Exp $
 *
 *  $Log: helper.c,v $
 *  Revision 1.2  2001/12/13 13:00:34  lurcher
 *
 *  Remove most if not all warnings on 64 bit platforms
 *  Add support for new MS 3.52 64 bit changes
 *  Add override to disable the stopping of tracing
 *  Add MAX_ROWS support in postgres driver
 *
 *  Revision 1.1.1.1  2001/10/17 16:40:32  lurcher
 *
 *  First upload to SourceForge
 *
 *  Revision 1.2  2001/05/23 13:48:37  nick
 *
 *  Remove unwanted include
 *
 *  Revision 1.1  2001/05/04 15:32:54  nick
 *
 *  Some makefile tweeks
 *
 *  Revision 1.3  2001/04/18 15:58:27  martin
 *  Add string_param_status.
 *  Make sure strings passed to ODBC fns are SQLCHAR.
 *
 *  Revision 1.2  1999/05/19 12:06:10  martin
 *  Quote table name to avoid reserved words problems.
 *
 *  Revision 1.1  1999/02/12 15:16:07  martin
 *  Initial revision
 *
 */
#include <stdlib.h>
#include <string.h>
#if defined (WIN32)
# include <windows.h>
#endif

#include <stdio.h>
#include <sql.h>
#include <sqlext.h>

#define IN_HELPER_C 1
#include "helper.h"

#define TABLE_ROWS 50
#define paramset_size 2
#define rowset_size 2

#ifndef FALSE
#define FALSE 0
#endif


/************************************************************************/
/*                                                                      */
/*  do_create_table                                                     */
/*  ===============                                                     */
/*                                                                      */
/*  Delete the named table and create a new one.                        */
/*                                                                      */
/************************************************************************/
SQLRETURN do_create_table(
    lpSERVERINFO srv_info, 
    SQLHDBC *hdbc,
    char *table_name,
    unsigned long flags,
    SQLINTEGER length,
    char *type,
    char *params)
{
    SQLHSTMT    hstmt;                          /* statement handle */
    SQLRETURN   ret;                            /* function status return */
    char        qbuf[1024];                     /* query buffer */
    
    szLogPrintf( srv_info, FALSE,"---------- do_create_table ----------\n");
    /*
     *  Allocate a statement and delete existing table.
     */
    if (SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt) != SQL_SUCCESS)
    {
        do_a_error(srv_info, SQL_HANDLE_DBC, hdbc, "SQLAllocHandle");
        return SQL_ERROR;
    }
    sprintf(qbuf, "drop table \"%s\"", table_name);
    szLogPrintf( srv_info, FALSE,"\"%s\"\n", qbuf);
    if (!SQL_SUCCEEDED(SQLExecDirect(hstmt, (SQLCHAR *)qbuf, SQL_NTS)))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLExecDirect");
    }
    if (flags & CREATE_TABLE_BIGCOL)
    {
        if (strstr(params, "length"))
        {
            sprintf(qbuf, "create table \"%s\" "
                    "(a INTEGER PRIMARY KEY, b %s(%ld))",
                    table_name, type, length);
        }
        else
        {
            sprintf(qbuf, "create table \"%s\" "
                    "(a INTEGER PRIMARY KEY, b %s)",
                    table_name, type);
        }
    }
    else
    {
        sprintf(qbuf, "create table \"%s\" "
                "(a INTEGER PRIMARY KEY, b CHARACTER VARYING(20))",
                table_name);
    }
    szLogPrintf( srv_info, FALSE,"\"%s\"\n", qbuf);
    ret = SQLExecDirect(hstmt, (SQLCHAR *)qbuf, SQL_NTS);
    if (!SQL_SUCCEEDED(ret))
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLExecDirect");
    
    ret = SQLFreeStmt(hstmt, SQL_DROP);
    if (!SQL_SUCCEEDED(ret))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLFreeStmt");
    }
    
    return ret;
}


/************************************************************************/
/*                                                                      */
/*  do_type_info                                                        */
/*                                                                      */
/************************************************************************/
SQLRETURN do_type_info(
    lpSERVERINFO srv_info,
    SQLHDBC *hdbc,
    typeinfo_t *typeinfo)
{
    struct types_s
    {
        SQLSMALLINT       type;
        char              *name;
    };
    struct types_s        types[] =
    {
        {SQL_ALL_TYPES,"ALL"},          
        {SQL_CHAR,"CHAR"},
        {SQL_NUMERIC,"NUMERIC"},
        {SQL_DECIMAL,"DECIMAL"},
        {SQL_INTEGER,"INTEGER"},
        {SQL_SMALLINT,"SMALLINT"},
        {SQL_FLOAT,"FLOAT"},
        {SQL_REAL,"REAL"},
        {SQL_DOUBLE,"DOUBLE"},
        {SQL_DATETIME,"DATETIME"},
        {SQL_VARCHAR,"VARCHAR"},
        {SQL_TIME,"TIME"},
        {SQL_DATE,"DATE"},        
        {SQL_TIMESTAMP,"TIMESTAMP"},
        {SQL_LONGVARCHAR,"LONGVARCHAR"},
        {SQL_BINARY,"BINARY"},
        {SQL_VARBINARY,"VARBINARY"},
        {SQL_LONGVARBINARY,"LONGVARBINARY"},
        {SQL_BIGINT,"BIGINT"},
        {SQL_TINYINT,"TINYINT"},
        {SQL_BIT,"BIT"},
        {0,NULL}
    };
    SQLRETURN           ret;                    /* function status return */
    unsigned int        t_index;                /* type index */
    SQLHSTMT            hstmt;                  /* statement handle */
    unsigned int        varchar_found=0;        /* found SQL_VARCHAR */
    unsigned int        integer_found=0;        /* found SQL_INTEGER */
    SQLSMALLINT         columns;                /* no. of resultant columns */
    
    szLogPrintf( srv_info, FALSE,"---------- do_type_info ----------\n");
    /*
     *  Allocate a statement and delete existing table.
     */
    if (SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt) != SQL_SUCCESS)
    {
        do_a_error(srv_info, SQL_HANDLE_DBC, hdbc, "SQLAllocHandle");
        return SQL_ERROR;
    }
    szLogPrintf( srv_info, FALSE,"\tCollecting type information with SQLGetTypeInfo\n");
    t_index = 0;
    while(types[t_index].name)
    {
        char            local_name[50];
        char            type_name[50];        
        SQLSMALLINT     sql_data_type;
        SQLINTEGER      column_size;
        char            create_params[256];
        SQLSMALLINT     min_scale;
        SQLSMALLINT     max_scale;        
        SQLSMALLINT     fix_prec_scale;
        
        if (srv_info->fDebug) szLogPrintf( srv_info, FALSE,"%s:\n", types[t_index].name);
        ret = SQLGetTypeInfo(hstmt, types[t_index].type);
        if (!SQL_SUCCEEDED(ret))
        {
            do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetTypeInfo");
            ret = SQLCloseCursor(hstmt);
            if (!SQL_SUCCEEDED(ret))
                do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLCloseCursor");
            t_index++;
            continue;
        }
        if (!SQL_SUCCEEDED(SQLNumResultCols(hstmt, &columns)))
        {
            do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLNumResultCols");
            return SQL_ERROR;
        }
        if (columns < 19)
        {
             szLogPrintf( srv_info, FALSE,
                    "** Can't find right number of columns in the result **\n");
             szLogPrintf( srv_info, FALSE, "** Found %d columns\n", columns);
        }
        while(SQL_SUCCEEDED(ret = SQLFetch(hstmt)))
        {
            SQLLEN          ind;
            
            local_name[0] = '\0';
            if (!SQL_SUCCEEDED(ret = SQLGetData(hstmt, 1, SQL_C_CHAR,
                                                type_name, sizeof(type_name),
                                                &ind)))
            
            {
                do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetData");
            }
            if (!SQL_SUCCEEDED(ret = SQLGetData(hstmt, 2, SQL_C_SHORT,
                                                &sql_data_type,
                                                sizeof(sql_data_type),
                                                &ind)))
            {
                do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetData");
            }
            if (!SQL_SUCCEEDED(ret = SQLGetData(hstmt, 3, SQL_C_LONG,
                                                &column_size,
                                                sizeof(column_size),
                                                &ind)))
            {
                do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetData");
            }
            /*
             *  We are specifically interested in column size as we can use
             *  this to choose a big data type for blob testing. However,
             *  driver can legitimately return SQL_NULL_DATA (which we will
             *  class as 0 length) and SQL_NO_TOTAL which sort of implies
             *  very big (no limit).
             */
            if (ind == SQL_NO_TOTAL) column_size = SQL_NO_TOTAL;
            if (ind == SQL_NULL_DATA) column_size = 0;
            create_params[0] = '\0';
            if (!SQL_SUCCEEDED(ret = SQLGetData(hstmt, 6, SQL_C_CHAR,
                                                create_params,
                                                sizeof(create_params),
                                                &ind)))
            {
                do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetData");
            }
            if (!SQL_SUCCEEDED(ret = SQLGetData(hstmt, 11, SQL_C_SHORT,
                                                &fix_prec_scale,
                                                sizeof(fix_prec_scale),
                                                &ind)))
            {
                do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetData");
            }
            if (!SQL_SUCCEEDED(ret = SQLGetData(hstmt, 13, SQL_C_CHAR,
                                                local_name, sizeof(local_name),
                                                &ind)))
            {
                do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetData");
            }
            if (!SQL_SUCCEEDED(ret = SQLGetData(hstmt, 14, SQL_C_SHORT,
                                                &min_scale, sizeof(min_scale),
                                                &ind)))
            {
                do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetData");
            }
            if (ind == SQL_NULL_DATA) min_scale = -1;
            
            if (!SQL_SUCCEEDED(ret = SQLGetData(hstmt, 15, SQL_C_SHORT,
                                                &max_scale, sizeof(max_scale),
                                                &ind)))
            {
                do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetData");
            }
            if (ind == SQL_NULL_DATA) max_scale = -1;
            if (srv_info -> fDebug)
            {
                if (column_size == SQL_NO_TOTAL)
                {
                    szLogPrintf( srv_info, FALSE,"%20s %20s %d oo %d %d %d (%16s)\n",
                           type_name, local_name, sql_data_type,
                           min_scale, max_scale, fix_prec_scale, create_params);
                }
                else
                {
                    szLogPrintf( srv_info, FALSE,"%20s %20s %d %ld %d %d %d (%16s)\n",
                           type_name, local_name, sql_data_type, column_size,
                           min_scale, max_scale, fix_prec_scale, create_params);
                }
            }
            if (types[t_index].type == SQL_VARCHAR) varchar_found = 1;
            if (types[t_index].type == SQL_VARCHAR) integer_found = 1;
            if (!strcmp(types[t_index].name, "ALL"))
            {
                if (typeinfo)
                {
                    strcpy(typeinfo->local_name, local_name);
                    strcpy(typeinfo->type_name, type_name);
                    strcpy(typeinfo->create_params, create_params);
                    typeinfo->sql_data_type = sql_data_type;
                    typeinfo->column_size = column_size;
                    typeinfo++;
                }
            }
        }
        if (ret != SQL_NO_DATA)
        {
            do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLFetch");
        }
        ret = SQLCloseCursor(hstmt);
        if (!SQL_SUCCEEDED(ret))
            do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLCloseCursor");
        t_index++;
    }
    if (srv_info->fDebug) szLogPrintf( srv_info, FALSE,"\n");

    ret = SQLFreeStmt(hstmt, SQL_DROP);
    if (!SQL_SUCCEEDED(ret))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLFreeStmt");
    }
    if (!varchar_found || !integer_found)
    {
         szLogPrintf( srv_info, FALSE,
                "** Can't seem to locate the INTEGER and VARCHAR types **\n");
        return SQL_ERROR;
    }
    return ret;
    
}


/************************************************************************/
/*                                                                      */
/*  do_table_info                                                       */
/*  =============                                                       */
/*                                                                      */
/*  Display a list of tables in the database.                           */
/*                                                                      */
/************************************************************************/
SQLRETURN do_table_info(lpSERVERINFO srv_info, SQLHSTMT *hdbc)
{
    SQLSMALLINT         columns;
    SQLRETURN   	ret;
    SQLSMALLINT         col;
    SQLHSTMT            hstmt;
    
    szLogPrintf( srv_info, FALSE,"---------- do_table_info ----------\n");
    /*
     *  Allocate a statement and delete existing table.
     */
    if (SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt) != SQL_SUCCESS)
    {
        do_a_error(srv_info, SQL_HANDLE_DBC, hdbc, "SQLAllocHandle");
        return SQL_ERROR;
    }
    /*
     *  Find out what tables we have.
     */
    szLogPrintf( srv_info, FALSE,"\tFinding out what tables there are with SQLTables\n");
    if (!SQL_SUCCEEDED(SQLTables(hstmt, NULL, 0, NULL, 0, NULL, 0, NULL, 0)))
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLTables");

    if (!SQL_SUCCEEDED(SQLNumResultCols(hstmt, &columns)))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLNumResultCols");
        return SQL_ERROR;
    }
    if (columns < 5)
    {
         szLogPrintf( srv_info, FALSE, "** Can't find enough columns in the table info **\n");
        return SQL_ERROR;
    }
    if (srv_info->fDebug) szLogPrintf( srv_info, FALSE,"\tColumns: %d\n", columns);
    
    szLogPrintf( srv_info, FALSE,"\tCollecting column information for Tables call\n");
    for (col = 1; col <= columns; col++)
    {
        SQLCHAR         column_name[100];
        SQLSMALLINT     column_name_len;
        SQLSMALLINT     data_type;
        SQLULEN         column_size;
        
        
        ret = SQLDescribeCol(hstmt, col, column_name, sizeof(column_name),
                             &column_name_len, &data_type, &column_size,
                             NULL, NULL);
        if (!SQL_SUCCEEDED(ret))
            do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLDescribeCol");
        if (column_name_len > sizeof(column_name))
            column_name[sizeof(column_name) - 1] = '\0';
        else
            column_name[column_name_len] = '\0';
        if (srv_info->fDebug) szLogPrintf( srv_info, FALSE,"\tname:%s type=%d size=%lu\n",
               column_name, data_type, column_size);
    }
    
    /*
     *  Fetch the names of the tables.
     */
    szLogPrintf( srv_info, FALSE,"\tFetching the names of the tables\n");
    while(SQL_SUCCEEDED(SQLFetch(hstmt)))
    {
        char            buf[1024];
        SQLLEN          slen;
        
        ret = SQLGetData(hstmt, 3, SQL_C_CHAR, buf, sizeof(buf), &slen);
        if (SQL_SUCCEEDED(ret))
        {
            buf[slen] = '\0';
            if (srv_info->fDebug) szLogPrintf( srv_info, FALSE,"%s ", buf);
        }
    }
    if (srv_info->fDebug) szLogPrintf( srv_info, FALSE,"\n");
    if (!SQL_SUCCEEDED(SQLCloseCursor(hstmt)))
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLCloseCursor");

    ret = SQLFreeStmt(hstmt, SQL_DROP);
    if (!SQL_SUCCEEDED(ret))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLCloseCursor");
    }
    return ret;
}


/************************************************************************/
/*                                                                      */
/*  do_create_rows                                                      */
/*                                                                      */
/************************************************************************/
SQLRETURN do_create_rows(
    lpSERVERINFO srv_info,
    SQLHDBC *hdbc,
    char *table)
{
    SQLLEN      	len_ind[2][paramset_size]; /* parameter lengths */
    SQLINTEGER  	p1[paramset_size];      /* first parameter array */
    SQLCHAR     	p2[paramset_size][30];  /* second parameter array */
    SQLUSMALLINT        param_status[paramset_size]; /* parameter status */
    SQLUINTEGER         params_processed;       /* parameters processed */
    SQLLEN          row_counts[paramset_size]; /* rows affected */
    SQLRETURN   	ret;                    /* function status return */
    unsigned int        row;                    /* current row */
    unsigned int        i;                      /* loop variable */
    SQLHSTMT            hstmt;                  /* statement handle */
    char                qbuf[1024];             /* query buffer */
    
    szLogPrintf( srv_info, FALSE,"---------- do_create_rows ----------\n");
    szLogPrintf( srv_info, FALSE,"-- Creating rows with column-wise bound parameters --\n");

    if (SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt) != SQL_SUCCESS)
    {
        do_a_error(srv_info, SQL_HANDLE_DBC, hdbc, "SQLAllocHandle");
        return SQL_ERROR;
    }
    /*
     *  Delete all the rows.
     */
    sprintf(qbuf, "delete from \"%s\"", table);
    szLogPrintf( srv_info, FALSE,"\"%s\"\n", qbuf);
    ret = SQLExecDirect(hstmt, (SQLCHAR *)qbuf, SQL_NTS);
    if (ret != SQL_SUCCESS)
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLExecDirect");

    /*
     *  Set the parameter binding type.
     */
    szLogPrintf( srv_info, FALSE,"\tSetting bind by column\n");
    
    if (!SQL_SUCCEEDED(SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_BIND_TYPE,
                                      SQL_PARAM_BIND_BY_COLUMN, 0)))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLSetStmtAttr");
        return SQL_ERROR;
    }
    /*
     *  Set the parameter status array and parameters processed.
     */
    szLogPrintf( srv_info, FALSE,"\tSetting Parameter Status Array Ptr\n");

    ret = SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_STATUS_PTR, &param_status[0], 0);
    if (!SQL_SUCCEEDED(ret))
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLSetStmtAttr");

    szLogPrintf( srv_info, FALSE,"\tSetting Parameters Processed Ptr\n");

    ret = SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR,
                         &params_processed, 0);
    if (!SQL_SUCCEEDED(ret))
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLSetStmtAttr");

    /*
     *  Set the parameter set size.
     */
    szLogPrintf( srv_info, FALSE,"\tSetting PARAMSETSIZE to %d\n", paramset_size);

    ret = SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE,
                         (SQLPOINTER)paramset_size, 0);
    if (!SQL_SUCCEEDED(ret))
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLSetStmtAttr");
    
    /*
     *  Create the rows.
     */
    sprintf(qbuf, "insert into \"%s\" (a,b) values(?,?)", table);
    szLogPrintf( srv_info, FALSE,"\"%s\"\n", qbuf);
    if (SQLPrepare(hstmt, (SQLCHAR *)qbuf, SQL_NTS) != SQL_SUCCESS)
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLPrepare");
        return SQL_ERROR;
    }

    /*
     *  Now look at what the driver thinks the parameters are.
     */
    (void) do_describe_params(srv_info, hstmt, paramset_size);

    /*
     *  Bind Parameters
     */
    ret = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
                           5, 0, p1, 0, len_ind[0]);
    if (!SQL_SUCCEEDED(ret))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLBindParameter");
        return ret;
    }
    ret = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
                           sizeof(p2[0]) - 1, 0, p2, sizeof(p2[0]),
                           len_ind[1]);
    if (!SQL_SUCCEEDED(ret))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLBindParameter");
        return ret;
    }
    szLogPrintf( srv_info, FALSE,"\tInserting rows into table\n");
    for (row = 0; row < TABLE_ROWS; row++)
    {
        /* a */
        p1[0] = row;
        len_ind[0][0] = sizeof(p1[0]);
        p1[1] = TABLE_ROWS + row;
        len_ind[0][1] = sizeof(p1[1]);

        /* b */
        sprintf((char*)p2[0], "this is row %u", row);
        len_ind[1][0] = SQL_NTS;
        sprintf((char*)p2[1], "and this is row %u", TABLE_ROWS + row);
        len_ind[1][1] = SQL_NTS;
        
        memset(param_status, 0xff, sizeof(param_status));
        if (!SQL_SUCCEEDED(ret = SQLExecute(hstmt)))
        {
            do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLExecute");
            return SQL_ERROR;
        }
        else if (ret != SQL_SUCCESS)
        {
            do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLExecute");
        }
        if (!SQL_SUCCEEDED(SQLRowCount(hstmt, row_counts)))
        {
            do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLRowCount");
        }
        if (row_counts[0] != paramset_size)
        {
             szLogPrintf( srv_info, FALSE, "** RowCount=%ld, expected %d **\n",
                    row_counts[0], paramset_size);
        }
        
        for (i = 0; i < paramset_size; i++)
        {
            if ((param_status[i] != SQL_PARAM_SUCCESS))
            {
                 szLogPrintf( srv_info, FALSE, "** Row %u not executed, status=%u**\n",
                        i+1, param_status[i]);
            }
        }
        if (params_processed != paramset_size)
        {
             szLogPrintf( srv_info, FALSE, "** Only %ld rows processed **\n",
                    params_processed);
        }
        szLogPrintf( srv_info, FALSE,".");
        fflush(stdout);
    }
    szLogPrintf( srv_info, FALSE,"\n");

    szLogPrintf( srv_info, FALSE,"\tResetting parameters\n");
    if (!SQL_SUCCEEDED(SQLFreeStmt(hstmt, SQL_RESET_PARAMS)))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLFreeStmt");
    }
    
    szLogPrintf( srv_info, FALSE,"\tClosing statement\n");
    if (!SQL_SUCCEEDED(SQLFreeStmt(hstmt, SQL_CLOSE)))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLFreeStmt");
    }
    
    szLogPrintf( srv_info, FALSE,"\tClearing Parameter Status Array Ptr\n");
    ret = SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_STATUS_PTR, NULL, 0);
    if (!SQL_SUCCEEDED(ret))
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLSetStmtAttr");

    szLogPrintf( srv_info, FALSE,"\tClearing Parameters Processed Ptr\n");
    ret = SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR, NULL, 0);
    if (!SQL_SUCCEEDED(ret))
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLSetStmtAttr");

    /*
     *  Set the parameter set size.
     */
    szLogPrintf( srv_info, FALSE,"\tSetting PARAMSETSIZE to 1\n");
    ret = SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE,
                         (SQLPOINTER)1, 0);
    if (!SQL_SUCCEEDED(ret))
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLSetStmtAttr");
    
    szLogPrintf( srv_info, FALSE,"\tDropping Statement\n");
    ret = SQLFreeStmt(hstmt, SQL_DROP);
    if (!SQL_SUCCEEDED(ret))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLFreeStmt");
    }
    
    return ret;
}


/************************************************************************/
/*                                                                      */
/*  do_describe_params                                                  */
/*                                                                      */
/************************************************************************/
SQLSMALLINT do_describe_params(
    lpSERVERINFO srv_info,
    SQLHSTMT *hstmt,
    SQLSMALLINT params)
{
    unsigned int        i;
    SQLRETURN           ret;
    SQLSMALLINT         data_type;
    SQLULEN             param_size;
    SQLSMALLINT         decimal_digits;
    SQLSMALLINT         nullable;
    SQLSMALLINT         parameters;
    
    szLogPrintf( srv_info, FALSE,"---------- do_describe_params ----------\n");
    /*
     *  Find out how many parameters the driver thinks we need.
     */
    if (!SQL_SUCCEEDED(SQLNumParams(hstmt, &parameters)))
    {
         szLogPrintf( srv_info, FALSE, "** SQLNumParams() failed **\n");
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLNumParams");
        return 0;
    }
    szLogPrintf( srv_info, FALSE,"\tDriver thinks we have %d parameters\n", parameters);
    if (parameters != params)
    {
         szLogPrintf( srv_info, FALSE,
                "** Inconsistent parameter counts expected %d, got %d **\n",
                params, parameters);
    }
    
    for (i = 1; i <= parameters; i++)
    {
        ret = SQLDescribeParam(hstmt, i, &data_type, &param_size,
                               &decimal_digits, &nullable);
        if (!SQL_SUCCEEDED(ret))
        {
            do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLDescribeParam");
        }
        else
        {
            szLogPrintf( srv_info, FALSE,"\tparam:%u type:%d size=%lu digits:%d nullable:%d\n",
                   i, data_type, param_size, decimal_digits, nullable);
        }
    }
    return parameters;
}


/************************************************************************/
/*                                                                      */
/*  do_a_error                                                          */
/*                                                                      */
/************************************************************************/
SQLRETURN do_a_error(
    lpSERVERINFO srv_info,
    SQLSMALLINT type,
    SQLHANDLE handle,
    char *fn)
{
    SQLRETURN           ret;
    SQLCHAR             state[7];
    SQLCHAR             text[1024];
    SQLSMALLINT         len;
    int                 i=0;
    SQLINTEGER          native;
    char                cbuf[2048];
    SQLINTEGER          ndiags;
    
    szLogPrintf( srv_info, FALSE,"** Error from %s **\n", fn);
    ret = SQLGetDiagField(type, handle, 0, SQL_DIAG_NUMBER, &ndiags, 0, NULL);
    szLogPrintf( srv_info, FALSE,"%ld diagnostics found\n", ndiags);
    do
    {
        ret = SQLGetDiagRec(type, handle, ++i, state,
                            &native, text, sizeof(text), &len);
        if (SQL_SUCCEEDED(ret))
        {
            sprintf(cbuf, "** error: %s:%d:%ld:%s **\n",
                    state, i, native, text);
             szLogPrintf( srv_info, FALSE, cbuf);
        }
    }
    while (SQL_SUCCEEDED(ret));
    return ret;
}
    

/************************************************************************/
/*                                                                      */
/*  do_get_info                                                         */
/*  ===========                                                         */
/*                                                                      */
/************************************************************************/
SQLRETURN do_get_info(
    lpSERVERINFO srv_info,
    SQLHDBC *hdbc,
    SQLUINTEGER *array_row_counts,
    SQLUINTEGER *array_selects,
    SQLUINTEGER *static_ca1,
    SQLUINTEGER *static_ca2)
{
    SQLRETURN           ret;

    szLogPrintf( srv_info, FALSE,"---------- do_get_info ----------\n");
    ret = SQLGetInfo(hdbc, SQL_PARAM_ARRAY_ROW_COUNTS, array_row_counts, 0, NULL);
    if (!SQL_SUCCEEDED(ret))
        do_a_error(srv_info, SQL_HANDLE_DBC, hdbc, "SQLGetInfo");
    if (*array_row_counts == SQL_PARC_BATCH)
    {
        szLogPrintf( srv_info, FALSE,"Driver: SQL_PARAM_ARRAY_ROW_COUNTS = SQL_PARC_BATCH\n");
    }
    else if (*array_row_counts == SQL_PARC_NO_BATCH)
    {
        szLogPrintf( srv_info, FALSE,"Driver: SQL_PARAM_ARRAY_ROW_COUNTS = SQL_PARC_BATCH\n");
    }
    else
    {
        szLogPrintf( srv_info, FALSE,"Driver: SQL_PARAM_ARRAY_ROW_COUNTS = unknown, %lu\n",
               *array_row_counts);
    }

    ret = SQLGetInfo(hdbc, SQL_PARAM_ARRAY_SELECTS, array_selects, 0, NULL);
    if (!SQL_SUCCEEDED(ret))
        do_a_error(srv_info, SQL_HANDLE_DBC, hdbc, "SQLGetInfo");
    if (*array_selects == SQL_PAS_BATCH)
    {
        szLogPrintf( srv_info, FALSE,"Driver: SQL_PARAM_ARRAY_SELECTS = SQL_PAS_BATCH\n");
    }
    else if (*array_selects == SQL_PAS_NO_BATCH)
    {
        szLogPrintf( srv_info, FALSE,"Driver: SQL_PARAM_ARRAY_SELECTS = SQL_PAS_NO_BATCH\n");
    }
    else if (*array_selects == SQL_PAS_NO_SELECT)
    {
        szLogPrintf( srv_info, FALSE,"Driver: SQL_PARAM_ARRAY_SELECTS = SQL_PAS_NO_SELECT\n");
    }
    else
    {
        szLogPrintf( srv_info, FALSE,"Driver: SQL_PARAM_ARRAY_SELECTS = unknown, %lu\n",
               *array_selects);
    }

    if (static_ca1)
    {
        ret = SQLGetInfo(hdbc, SQL_STATIC_CURSOR_ATTRIBUTES1, static_ca1,
                         0, NULL);
        if (!SQL_SUCCEEDED(ret))
            do_a_error(srv_info, SQL_HANDLE_DBC, hdbc, "SQLGetInfo");
        if (*static_ca1 & SQL_CA1_POS_POSITION)
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES1 says SQL_CA1_POS_POSITION"
                   "\nand so a static cursor can be positioned with SQLSetPos\n");
        }
        else
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES1 says !SQL_CA1_POS_POSITION"
                   "\nand so a static cursor can NOT be positioned with SQLSetPos\n");
        }
        if (*static_ca1 & SQL_CA1_POS_UPDATE)
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES1 says SQL_CA1_POS_UPDATE"
                   "\nand so a static cursor can be used to SQL_UPDATE with SQLSetPos\n");
        }
        else
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES1 says !SQL_CA1_POS_UPDATE"
                   "\nand so a static cursor can NOT be used to SQL_UPDATE with SQLSetPos\n");
        }
        if (*static_ca1 & SQL_CA1_POS_DELETE)
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES1 says SQL_CA1_POS_DELETE"
                   "\nand so a static cursor can be used to SQL_DELETE with SQLSetPos\n");
        }
        else
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES1 says !SQL_CA1_POS_DELETE"
                   "\nand so a static cursor can NOT be used to SQL_DELETE with SQLSetPos\n");
        }
        if (*static_ca1 & SQL_CA1_POS_REFRESH)
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES1 says SQL_CA1_POS_REFRESH"
                   "\nand so a static cursor can be used to SQL_REFRESH with SQLSetPos\n");
        }
        else
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES1 says !SQL_CA1_POS_REFRESH"
                   "\nand so a static cursor can NOT be used to SQL_REFRESH with SQLSetPos\n");
        }
        
    }
    if (static_ca2)
    {
        ret = SQLGetInfo(hdbc, SQL_STATIC_CURSOR_ATTRIBUTES2, static_ca2,
                         0, NULL);
        if (!SQL_SUCCEEDED(ret))
            do_a_error(srv_info, SQL_HANDLE_DBC, hdbc, "SQLGetInfo");
        if (*static_ca2 & SQL_CA2_SENSITIVITY_ADDITIONS)
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES2 says SQL_CA2_SENSITIVITY_ADDITIONS"
                   "\nand so added rows are visible with a static cursor\n");
        }
        else
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES2 says !SQL_CA2_SENSITIVITY_ADDITIONS"
                   "\nand so added rows NOT are visible with a static cursor\n");
        }
        if (*static_ca2 & SQL_CA2_SENSITIVITY_DELETIONS)
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES2 says SQL_CA2_SENSITIVITY_DELETIONS"
                   "\nand so deleted rows are visible with a static cursor\n");
        }
        else
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES2 says !SQL_CA2_SENSITIVITY_DELETIONS"
                   "\nand so deleted rows NOT are visible with a static cursor\n");
        }
        if (*static_ca2 & SQL_CA2_SENSITIVITY_UPDATES)
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES2 says SQL_CA2_SENSITIVITY_UPDATES"
                   "\nand so updated rows are visible with a static cursor\n");
        }
        else
        {
            szLogPrintf( srv_info, FALSE,"Driver: SQL_STATIC_CURSOR_ATTRIBUTES2 says !SQL_CA2_SENSITIVITY_UPDATES"
                   "\nand so updated rows NOT are visible with a static cursor\n");
        }
    }
    
    return ret;
    
}    

/************************************************************************/
/*                                                                      */
/*  do_get_dbtype                                                       */
/*  =============                                                       */
/*                                                                      */
/************************************************************************/
SQLRETURN do_get_dbtype(
    lpSERVERINFO srv_info,
    SQLHDBC *hdbc,
    SQLSMALLINT type,
    char *type_name,
    size_t len)
{
    SQLRETURN           ret;
    SQLHSTMT            hstmt;
    SQLLEN              ind;
    
    /*
     *  Find out what type to use for a timestamp.
     */
    if (!SQL_SUCCEEDED(ret = SQLAllocStmt(hdbc, &hstmt)))
    {
        do_a_error(srv_info, SQL_HANDLE_DBC, hdbc, "SQLAllocStmt");
        return ret;
    }
    
    if (!SQL_SUCCEEDED(ret = SQLGetTypeInfo(hstmt, type)))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetTypeInfo");
        SQLFreeStmt(hstmt, SQL_DROP);
        return ret;
    }
    if (!SQL_SUCCEEDED(ret = SQLFetch(hstmt)))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLFetch");
        SQLFreeStmt(hstmt, SQL_DROP);
        return ret;
    }
    if (!SQL_SUCCEEDED(ret = SQLGetData(hstmt, 1, SQL_C_CHAR, type_name,
                                        len, &ind)))
    {
        do_a_error(srv_info, SQL_HANDLE_STMT, hstmt, "SQLGetData");
        SQLFreeStmt(hstmt, SQL_DROP);
        return ret;
    }
    return SQLFreeStmt(hstmt, SQL_DROP);
}


/************************************************************************/
/*                                                                      */
/*  string_row_status                                                   */
/*  =================                                                   */
/*                                                                      */
/************************************************************************/
void string_row_status(
    SQLUSMALLINT status,
    char *string)
{
    switch(status)
    {
      case SQL_ROW_SUCCESS:
        strcpy(string, "SQL_ROW_SUCCESS");
        break;
      case SQL_ROW_SUCCESS_WITH_INFO:
        strcpy(string, "SQL_ROW_SUCCESS_WITH_INFO");
        break;
      case SQL_ROW_ERROR:
        strcpy(string, "SQL_ROW_ERROR");
        break;
      case SQL_ROW_UPDATED:
        strcpy(string, "SQL_ROW_UPDATED");
        break;
      case SQL_ROW_DELETED:
        strcpy(string, "SQL_ROW_DELETED");
        break;
      case SQL_ROW_ADDED:
        strcpy(string, "SQL_ROW_ADDED");
        break;
      case SQL_ROW_NOROW:
        strcpy(string, "SQL_ROW_NOROW");
        break;
      default:
        sprintf(string, "%d", status);
        break;
    }
}


char *string_param_status(SQLUSMALLINT status)
{
    switch (status)
    {
      case SQL_PARAM_SUCCESS:
        return "SQL_PARAM_SUCCESS";
      case SQL_PARAM_SUCCESS_WITH_INFO:
        return "SQL_PARAM_SUCCESS_WITH_INFO";
      case SQL_PARAM_ERROR:
        return "SQL_PARAM_ERROR";
      case SQL_PARAM_UNUSED:
        return "SQL_PARAM_UNUSED";
      case SQL_PARAM_DIAG_UNAVAILABLE:
        return "SQL_PARAM_DIAG_UNAVAILABLE";
      default:
        return "UNKNOWN";
    }
}