File: dbdimp.c

package info (click to toggle)
msql-modules 0.91-7
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 248 kB
  • ctags: 142
  • sloc: perl: 1,144; ansic: 549; makefile: 72
file content (662 lines) | stat: -rw-r--r-- 18,476 bytes parent folder | download | duplicates (2)
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
/**
 * $Id: dbdimp.c,v 1.1 1997/07/09 18:59:55 k Exp $
 * 
 * (c)1994-1997 Alligator Descartes, based on work by Tim Bunce
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Artistic License, as specified in the Perl README file.
 *
 */

#include "mSQL.h"

DBISTATE_DECLARE;

void
dbd_init(dbistate)
    dbistate_t *dbistate;
{
    DBIS = dbistate;
    dbd_errnum = GvSV(gv_fetchpv("DBD::mSQL::err",    1, SVt_IV));
    dbd_errstr = GvSV(gv_fetchpv("DBD::mSQL::errstr", 1, SVt_PV));
}

void do_mSQL_error( sb2 rc, char *what ) {
    sv_setiv(dbd_errnum, (IV)rc);
    sv_setpv(dbd_errstr, (char*)what);
}

void
fbh_dump(fbh, i)
    imp_fbh_t *fbh;
    int i;
{
    FILE *fp = DBILOGFP;
    fprintf(fp, "fbh %d: '%s' %s, ",
        i, fbh->cbuf, (fbh->nullok) ? "NULLable" : "");
    fprintf(fp, "type %d,  dbsize %ld, dsize %ld, p%d s%d\n",
        fbh->dbtype, (long)fbh->dbsize, (long)fbh->dsize, fbh->prec, fbh->scale);
    fprintf(fp, "   out: ftype %d, indp %d, bufl %d, rlen %d, rcode %d\n",
        fbh->ftype, fbh->indp, fbh->bufl, fbh->rlen, fbh->rcode);
}


/* ================================================================== */

int
dbd_db_login( dbh, host, dbname, junk )
    SV *dbh;
    char *host;
    char *dbname;
    char *junk;
{
    D_imp_dbh(dbh);

/*    printf( "%s:%d: dbd_db_login: %s, %s\n", 
            __FILE__, __LINE__, 
            host, dbname ); */

    if (host && !*host) host = 0;    /* Patch by Sven Verdoolaege */
    imp_dbh->lda.svsock = msqlConnect( host ); 

    if ( imp_dbh->lda.svsock == -1 ) {
        do_mSQL_error( (sb2)( imp_dbh->lda.rc ), msqlErrMsg );
        return 0;
      } 

    if ( strlen( dbname ) != 0 ) {
        if ( msqlSelectDB( imp_dbh->lda.svsock, dbname ) == -1 ) {
            do_mSQL_error( (sb2)(imp_dbh->lda.rc ), msqlErrMsg );
            return 0;
          } 
      }
    /** Dump the information we have into the Lda_Def */
    imp_dbh->lda.svdb = dbname;
    imp_dbh->lda.svhost = host;

    /** Return after setting various doo-dads */
    DBIc_IMPSET_on(imp_dbh);    /* imp_dbh set up now                   */
    DBIc_ACTIVE_on(imp_dbh);    /* call disconnect before freeing       */
    return 1;
  }

/* Commit and Rollback don't exist in mSQL but we'll stub them anyway... */

int
dbd_db_commit(dbh)
    SV *dbh;
{
    D_imp_dbh(dbh);
    return 1;
}

int
dbd_db_rollback(dbh)
    SV *dbh;
{
    D_imp_dbh(dbh);
    return 1;
}

int
dbd_db_disconnect(dbh)
    SV *dbh;
{
    D_imp_dbh(dbh);
    /* We assume that disconnect will always work       */
    /* since most errors imply already disconnected.    */
    DBIc_ACTIVE_off(imp_dbh);
    if ( dbis->debug >= 2 )
        printf( "imp_dbh->sock: %i\n", imp_dbh->lda.svsock );

    msqlClose( imp_dbh->lda.svsock );

    /* We don't free imp_dbh since a reference still exists    */
    /* The DESTROY method is the only one to 'free' memory.    */
    return 1;
}

void
dbd_db_destroy(dbh)
    SV *dbh;
{
    D_imp_dbh(dbh);
    if (DBIc_ACTIVE(imp_dbh))
        dbd_db_disconnect(dbh);
    /* XXX free contents of imp_dbh */
    DBIc_IMPSET_off(imp_dbh);
}

int
dbd_db_STORE(dbh, keysv, valuesv)
    SV *dbh;
    SV *keysv;
    SV *valuesv;
{
    D_imp_dbh(dbh);
    STRLEN kl;
    char *key = SvPV(keysv,kl);
    SV *cachesv = NULL;

    if (kl==10 && strEQ(key, "AutoCommit")){
        /* Ignore SvTRUE warning: '=' where '==' may have been intended. */
/*        if ( (on) ? ocon(&imp_dbh->lda) : ocof(&imp_dbh->lda) ) {
            ora_error(dbh, &imp_dbh->lda, imp_dbh->lda.rc, "ocon/ocof failed");
        } else {
            cachesv = (on) ? &sv_yes : &sv_no;
        } */
    } else {
        return FALSE;
    }
    if (cachesv) /* cache value for later DBI 'quick' fetch? */
        hv_store((HV*)SvRV(dbh), key, kl, cachesv, 0);
    return TRUE;
}

SV *
dbd_db_FETCH(dbh, keysv)
    SV *dbh;
    SV *keysv;
{
    D_imp_dbh(dbh);
    return sv_2mortal(NULL);
}

SV *
dbd_db_fieldlist(res)
    m_result *    res;
{
    m_field *curField;
    HV * hv;
    SV * rv;
    AV * avkey;
    AV * avnam;
    AV * avnnl;
    AV * avtab;
    AV * avtyp;
    AV * avlength;
    hv = (HV*)sv_2mortal((SV*)newHV());
    hv_store(hv,"NUMROWS",7,(SV *)newSViv((IV)msqlNumRows(res)),0);
    hv_store(hv,"NUMFIELDS",9,(SV *)newSViv((IV)msqlNumFields(res)),0);
    msqlFieldSeek(res,0);
    avkey = (AV*)sv_2mortal((SV*)newAV());
    avnam = (AV*)sv_2mortal((SV*)newAV());
    avnnl = (AV*)sv_2mortal((SV*)newAV());
    avtab = (AV*)sv_2mortal((SV*)newAV());
    avtyp = (AV*)sv_2mortal((SV*)newAV());
    avlength = (AV*)sv_2mortal((SV*)newAV());
    while ( ( curField = msqlFetchField( res ) ) ) {
        av_push(avnam,(SV*)newSVpv(curField->name,strlen(curField->name)));
        av_push(avtab,(SV*)newSVpv(curField->table,strlen(curField->table)));
        av_push(avtyp,(SV*)newSViv(curField->type));
#ifdef MSQL1
        av_push(avkey,(SV*)newSViv(IS_PRI_KEY(curField->flags)));
#else
        av_push(avkey,(SV*)newSVpv("Deprecated", strlen( "Deprecated" ) ) );
#endif
        av_push(avnnl,(SV*)newSViv(IS_NOT_NULL(curField->flags)));
        av_push(avlength,(SV*)newSViv(curField->length));
      }
    rv = newRV((SV*)avnam); hv_store(hv,"NAME",4,rv,0);
    rv = newRV((SV*)avtab); hv_store(hv,"TABLE",5,rv,0);
    rv = newRV((SV*)avtyp); hv_store(hv,"TYPE",4,rv,0);
    rv = newRV((SV*)avkey); hv_store(hv,"IS_PRI_KEY",10,rv,0);
    rv = newRV((SV*)avnnl); hv_store(hv,"IS_NOT_NULL",11,rv,0);
    rv = newRV((SV*)avlength); hv_store(hv,"LENGTH",6,rv,0);
    hv_store(hv,"RESULT",6,(SV *)newSViv((IV)res),0);
    rv = newRV((SV*)hv);
    return rv;
}


/* ================================================================== */

int
dbd_st_prepare(sth, statement, attribs)
    SV *sth;
    char *statement;
    SV *attribs;
{
    D_imp_sth(sth);
    D_imp_dbh_from_sth;

    int i;
    char func[64];

    imp_sth->done_desc = 0;
    imp_sth->cda = &imp_sth->cdabuf;

    /* Parse statement for binds ( also, INSERTS! ) */
    /* Lowercase the statement first */

/*    for ( i = 0 ; i < strlen( statement ) ; i++ ) {
        if ( ( statement[i] == '\'' ) || ( statement[i] == '"' ) )
            if ( inside_quote == 1 ) 
                inside_quote = 0;
            else
                inside_quote = 1;
        if ( isupper( statement[i] ) && ( inside_quote != 1 ) ) 
            statement[i] = tolower( statement[i] );
      }
*/

    sscanf( statement, "%s", func );
    for ( i = 0 ; i < strlen( func ) ; i++ )
        if ( isupper( func[i] ) )
            func[i] = tolower( func[i] );

    if ( strstr( func, "insert" ) != 0 ) {
        if ( dbis->debug >= 2 )
            warn( "INSERT present in statement\n" );
        imp_sth->is_insert = 1;
      }

    if ( strstr( func, "create" ) != 0 ) {
        if ( dbis->debug >= 2 )
            warn( "CREATE present in statement\n" );
        imp_sth->is_create = 1;
      }

    if ( strstr( func, "update" ) != 0 ) {
        if ( dbis->debug >= 2 )
            warn( "UPDATE present in statement\n" );
        imp_sth->is_update = 1;
      }

    if ( strstr( func, "drop" ) != 0 ) {
        if ( dbis->debug >= 2 )
            warn( "DROP present in statement\n" );
        imp_sth->is_drop = 1;
      }

    if ( strstr( func, "delete" ) != 0 ) {
        if ( dbis->debug >= 2 )
            warn( "DELETE present in statement\n" );
        imp_sth->is_delete = 1;
      }

    if ( strstr( func, "select" ) != 0 ) {
        if ( dbis->debug >= 2 )
            warn( "SELECT present in statement\n" );
        imp_sth->is_select = 1;
      }

    if ( strstr( func, "systables" ) != 0 ) {
        if ( dbis->debug >= 2 )
            warn( "dumping tables\n" );
        imp_sth->is_delete = 1;
      }

    /** Copy the statement into the current imp_sth */
    imp_sth->statement = (char *)malloc( strlen( statement ) + 1 );
    memcpy( imp_sth->statement, statement, strlen( statement ) );
    imp_sth->statement[strlen( statement )] = '\0';

    DBIc_IMPSET_on(imp_sth);
    return 1;
}

int
dbd_st_execute( h, imp_sth )
    SV *h;
    imp_sth_t *imp_sth;
{
    D_imp_dbh_from_sth;

    /** 
     * Check to see if we've already described this statement. If we have,
     * then return immediately. Otherwise, set the flag to stop us re-exec'ing
     * this statement.
     */
/*    fprintf( stderr, "imp_sth->done_execute: %d\n", imp_sth->done_execute ); */
    if ( imp_sth->done_execute == 1 ) {
        return 0;
      }
    imp_sth->done_execute = 1;
/*    imp_sth->done_desc = 0; */

    /** Issue the statement */
/*    fprintf( stderr, "Issuing statement: %s\n", imp_sth->statement ); */
    if ( msqlQuery( imp_dbh->lda.svsock, imp_sth->statement ) == -1 ) { 
        do_mSQL_error( (sb2)-1, msqlErrMsg );
        return -2;
      }

    /** Store the result from the Query */
    if ( imp_sth->is_insert || imp_sth->is_create || imp_sth->is_update || imp_sth->is_drop || imp_sth->is_delete ) {
        /**
         * @@For the moment, it appears we cannot store the result of the
         * non-SELECT statements, which means we cannot return the number of
         * rows affected by non-SELECT statements.
         */
/*        imp_sth->cda = msqlStoreResult();
        if ( !imp_sth->cda ) {
            do_mSQL_error( (sb2)-1, "Cannot store result for row count!" );
            return -1;
          }
        imp_sth->row_num = msqlNumRows( imp_sth->cda );
        DBIc_IMPSET_on(imp_sth);
        msqlFreeResult( imp_sth->cda );
        imp_sth->cda = NULL;
        return imp_sth->row_num; */

        imp_sth->cda = NULL;
        imp_sth->row_num = -1;
        DBIc_IMPSET_on( imp_sth );
        return -1;
      }

    /** Store the result in the current statement handle */
    imp_sth->cda = msqlStoreResult();
    if ( !imp_sth->cda ) {
        do_mSQL_error( (sb2)-1, msqlErrMsg );
        return -2;
      }

    imp_sth->row_num = msqlNumRows( imp_sth->cda );

    if ( dbis->debug >= 2 )
        printf( "%d rows matched\n", imp_sth->row_num );

    /** Get number of fields and space needed for field names      */
    imp_sth->fbh_num = msqlNumFields( imp_sth->cda );
    if ( dbis->debug >= 2 )
        printf( "DBD::mSQL::dbd_db_prepare'imp_sth->fbh_num: %d\n",
                imp_sth->fbh_num );

    return imp_sth->row_num;
  }

int
dbd_describe(h, imp_sth)
     SV *h;
     imp_sth_t *imp_sth;
{
    D_imp_dbh_from_sth;

    sb1 *cbuf_ptr;
    int t_cbufl=0;
    sb4 f_cbufl[MAX_COLS];
    int i, field_info_loop;
    m_row cur;
    m_field *curField;
    int length;
/*  FILE *fp = DBILOGFP; */
  
    if ( dbis->debug >= 2 )
        warn( "In: DBD::mSQL::dbd_describe()\n" );

    /** 
     * Check to ensure that a) we've executed the sth; b) we haven't
     * already described this sth.
     */
/*    if ( imp_sth->done_execute == 0 || imp_sth->done_desc == 1 ) {
        return 0;
      }
    imp_sth->done_desc = 1; */

    t_cbufl = 0;

    if ( imp_sth->currow >= imp_sth->row_num )
      {
        imp_sth->fbh_num = 0;
        return 0;
      }

    /** 
     * Find the row we want to be working on in the dataset returned
     * from dbd_st_execute()
     */
    msqlDataSeek( imp_sth->cda, imp_sth->currow );

    field_info_loop = 0;
    while ( ( curField = msqlFetchField( imp_sth->cda ) ) ) {
        if ( dbis->debug >= 2 ) {
            warn( "In: DBD::mSQL::dbd_describe'Fetching Field\n" );      
          }

        f_cbufl[field_info_loop] = sizeof( curField->name );
        switch(curField->type) {
            case REAL_TYPE:
                length = strlen(curField->name);
                if (length < 12) {
                    length = 12;
                  }
                break;
            case INT_TYPE:
                length = strlen( curField->name );
                if ( length < 8 ) {
                    length = 8;
                  }
                break;
            case CHAR_TYPE:
                length = ( strlen(curField->name) < curField->length ? curField->length : strlen( curField->name ) );
                break;

            case NULL_TYPE:
                length = 0;
                imp_sth->fbh_cbuf = '\0';
                break;

            default:
                length = 0;
                imp_sth->fbh_cbuf = '\0';
                break;
          }
        f_cbufl[field_info_loop] = length;
        t_cbufl += length;
        field_info_loop++;
      }
    msqlFieldSeek(imp_sth->cda,0);

    /* allocate field buffers    */
    Newz(42, imp_sth->fbh, imp_sth->fbh_num, imp_fbh_t);
    /* allocate a buffer to hold all the column names */
    Newz(42, imp_sth->fbh_cbuf, t_cbufl + imp_sth->fbh_num, char);
      
    cbuf_ptr = (sb1*)imp_sth->fbh_cbuf;
      
    cur = msqlFetchRow( imp_sth->cda );

    /** Set the number of fields within the handle */
/*    fprintf( stderr, "imp_sth->fbh_num: %d\n", imp_sth->fbh_num ); */
    DBIc_NUM_FIELDS( imp_sth ) = imp_sth->fbh_num;

    /**
     * Foreach row, we need to allocate some space and link the
     * header record to it 
     */
    for( i = 0 ; i < imp_sth->fbh_num ; ++i ) {
        imp_fbh_t *fbh = &imp_sth->fbh[i];
        fbh->imp_sth = imp_sth;
        fbh->cbuf    = cbuf_ptr;
        fbh->cbufl   = f_cbufl[i];
          
        if ( dbis->debug >= 2 )
            warn( "In: DBD::mSQL::dbd_describe'LinkRow: %d\n", i );

        if ( cur[i] == 0 ) { 
            if ( dbis->debug >= 2 )
                warn( "Looks like a NULL!\n" ); 
            fbh->cbuf[0] = '\0'; 
            fbh->cbufl = 0;
            fbh->rlen = fbh->cbufl;
	    fbh->indp = 1;
          } else {
            fbh->cbuf = (sb1*)cur[i];
            fbh->cbufl = (sb4)strlen( (const char*)fbh->cbuf );
            fbh->rlen = fbh->cbufl;
	    fbh->indp = 0;
          } 

        if ( dbis->debug >= 2 )
            warn( "Name: %s\t%i\n", fbh->cbuf, fbh->rlen );

        fbh->cbuf[fbh->cbufl] = '\0'; /* ensure null terminated */ 
        cbuf_ptr += fbh->cbufl + 1;   /* increment name pointer    */ 
          
        /* Now define the storage for this field data.        */
        /* Hack buffer length value */
  
        fbh->dsize = fbh->cbufl;
          
        /* Is it a LONG, LONG RAW, LONG VARCHAR or LONG VARRAW?    */
        /* If so we need to implement oraperl truncation hacks.    */
        /* This may change in a future release.            */

        fbh->bufl = fbh->dsize + 1;
          
        /* for the time being we fetch everything as strings    */
        /* that will change (IV, NV and binary data etc)    */
        /* currently we use an sv, later we'll use an array     */

        if ( dbis->debug >= 2 )
            warn( "In: DBD::mSQL::dbd_describe'newSV\n" );
        fbh->sv = newSV((STRLEN)fbh->bufl); 

        if ( dbis->debug >= 2 )
            warn( "In: DBD::mSQL::dbd_describe'SvUPGRADE\n" );
        (void)SvUPGRADE(fbh->sv, SVt_PV);

        if ( dbis->debug >= 2 )
            warn( "In: DBD::mSQL::dbd_describe'SvREADONLY_ON\n" );
        SvREADONLY_on(fbh->sv);

        if ( dbis->debug >= 2 )
            warn( "In: DBD::mSQL::dbd_describe'SvPOK_only\n" );
        (void)SvPOK_only(fbh->sv);

        if ( dbis->debug >= 2 )
            warn( "In: DBD::mSQL::dbd_describe'SvPVX\n" );
        fbh->buf = (ub1*)SvPVX(fbh->sv);
     }

    if ( dbis->debug >= 2 ) {
        printf( "Entering imp_sth->fbh test cycle\n" );
        for(i = 0 ; i <  imp_sth->fbh_num /* && imp_sth->cda->rc!=10 */ ; ++i ) {

            imp_fbh_t *fbh = &imp_sth->fbh[i];

            printf( "In: DBD::mSQL::dbd_describe'FBHDump[%d]: %s\t%d\n",
                    i, fbh->cbuf, fbh->rlen );
         }
      }
    if ( dbis->debug )
        printf( "Out: DBD::mSQL::dbd_describe()\n" );
    return 0;
  }

int
dbd_st_rows(sth)
    SV *sth;
{
    D_imp_sth(sth);
    return imp_sth->row_num;
  }

int
dbd_st_finish(sth)
    SV *sth;
{
    D_imp_sth(sth);
    /* Cancel further fetches from this cursor.                 */
    /* We don't close the cursor till DESTROY.                  */
    /* The application may re execute it.                       */
    DBIc_ACTIVE_off(imp_sth);
    return 1;
}

void
dbd_st_destroy(sth)
    SV *sth;
{
    D_imp_sth(sth);
    D_imp_dbh_from_sth;

    /* Free off contents of imp_sth     */
    if ( dbis->debug >= 2 ) {
        warn( "dbd_st_destroy()\n" );
      }
/** Unused in DBD::mSQL currently 
    fields = DBIc_NUM_FIELDS(imp_sth);
    imp_sth->in_cache    = 0;
    for(i=0; i < fields; ++i) {
        imp_fbh_t *fbh = &imp_sth->fbh[i];
        fb_ary_free(fbh->fb_ary);
    } */
    Safefree(imp_sth->fbh);
    Safefree(imp_sth->fbh_cbuf);
    Safefree(imp_sth->statement);

    DBIc_IMPSET_off(imp_sth);           /* let DBI know we've done it   */
}

int
dbd_st_STORE(sth, keysv, valuesv)
    SV *sth;
    SV *keysv;
    SV *valuesv;
{
    D_imp_sth(sth);
    STRLEN kl;
    char *key = SvPV(keysv,kl);
    SV *cachesv = NULL;
    int on = SvTRUE(valuesv);

    if (kl==8 && strEQ(key, "ora_long")){
        imp_sth->long_buflen = SvIV(valuesv);

    } else if (kl==9 && strEQ(key, "ora_trunc")){
        imp_sth->long_trunc_ok = on;

    } else {
        return FALSE;
    }
    if (cachesv) /* cache value for later DBI 'quick' fetch? */
        hv_store((HV*)SvRV(sth), key, kl, cachesv, 0);
    return TRUE;
}


SV *
dbd_st_FETCH(sth, keysv)
    SV *sth;
    SV *keysv;
{
    D_imp_sth(sth);
    STRLEN keyLength;
    char *key = SvPV( keysv, keyLength );
    int numFields,
        i;
    SV *retsv = NULL;

    /** Grab the number of fields from the statement handle */
    numFields = i = DBIc_NUM_FIELDS( imp_sth );
/*    fprintf( stderr, "numFields: %d\n", numFields );

    fprintf( stderr, "kl: %d\tkey: %s\n", keyLength, key ); */

    /** Return a reference to an array of the column names */
    if ( keyLength == 4 && strEQ( key, "NAME" ) ) {
        AV *av = newAV();
        m_field *field = msqlFetchField( imp_sth->cda );

        if ( field == NULL ) {
/*            fprintf( stderr, "Field is NULL!\n" ); */
            return Nullsv;
          }
        retsv = newRV( sv_2mortal( (SV *)av ) );

        while ( i >= 0 && field != NULL ) {
/*            fprintf( stderr, "Field: %s\n", field->name ); */
            av_store( av, ( numFields - i ), 
                      newSVpv( (char *)field->name, 0 ) );
            field = msqlFetchField( imp_sth->cda );
            i--;
          }
      } else {
        return Nullsv;
      }

    return sv_2mortal( retsv );
}