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
992
993
994
995
996
997
998
|
//
// infdb - NT data base daemon for Inferno
//
// Copyright 1997 Lucent Technologies
//
// May 1997
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#ifdef WIN32
#include <winsock.h>
#include <SQL.h>
#include <SQLEXT.h>
#else
#include <unistd.h>
#include <sql.h>
#include <sqlext.h>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define strnicmp strncasecmp
#endif
#define MAXCOLS 100
#define BUFSIZE 8192
#define REQ_HEADER_SIZE 18
#define RES_HEADER_SIZE 22
#define OFFSET_LENGTH 2
#define OFFSET_STREAM 14
#define OFFSET_REQ_DATA 18
#define OFFSET_RETURN 18
#define OFFSET_RES_DATA 22
#define CONN_ALLOC_FAIL 1
#define STREAM_ALLOC_FAIL 2
#define STREAM_BAD_ID 3
#define LAST_ERROR_NO 4
//
// Deal with one connection. Use stdin and stdout to read and write messages.
// Each incoming message is answered before reading the next incoming message.
//
typedef int STATUS;
#define OK 0
#define WARN -1
#define ERR -2
typedef struct {
int state;
#define SQLC_FREE 0
#define SQLC_INUSE 1
int connid;
int refcount;
UCHAR user[48];
UCHAR passwd[48];
UCHAR dbname[48];
UCHAR errmsg[256];
HDBC hdbc;
} SQLConn;
typedef struct {
int state;
#define SQLS_FREE 0
#define SQLS_INUSE 1
int streamid;
int connid;
HSTMT hstmt;
UCHAR errmsg[256];
UCHAR colname[MAXCOLS][32];
SWORD coltype[MAXCOLS];
SWORD colnamelen;
SWORD nullable;
UDWORD collen[MAXCOLS];
SWORD scale;
SDWORD outlen[MAXCOLS];
UCHAR *data[MAXCOLS];
SWORD nresultcols;
SDWORD rowcount;
SWORD rownum;
RETCODE rc;
UCHAR *setdata[MAXCOLS];
SDWORD setdatalen[MAXCOLS];
} SQLStream;
typedef struct {
HENV henv;
int maxconn;
int numconn;
SQLConn **scarray;
int maxstream;
int numstream;
SQLStream **ssarray;
} SQLEnv;
typedef struct {
char mtype;
char version;
int nbytes;
int sstream;
int retcode;
int bytesNotRead;
char *data;
} DBMSG, *DBMSGP;
int getCommand (DBMSGP msgp, UCHAR *buf, int bufsiz);
void sendResponse (char type, int lendata, int sstream, int retcode, char *data);
void sendError (char *errmsg, int sstream);
void print_err (SQLEnv *sqle, int connid, int streamid, UCHAR * buf, int bufsiz);
UDWORD display_size (SWORD coltype, UDWORD collen, UCHAR *colname);
STATUS newSqlEnv (SQLEnv **sqle);
STATUS freeSqlEnv (SQLEnv **sqle);
STATUS newSqlConn (SQLEnv *sqle, char *info, int *connid);
STATUS mapSqlConn (SQLEnv *sqle, int connid, SQLConn **sqlc);
STATUS freeSqlConn (SQLEnv *sqle, int connid);
STATUS newSqlStream (SQLEnv *sqle, int connid, int *streamid);
STATUS mapSqlStream (SQLEnv *sqle, int streamid, SQLStream **sqls);
STATUS freeSqlStream (SQLEnv *sqle, int streamid);
STATUS parseConnInfo (SQLConn *sqlc, char *info);
char *iError[] = {
"INFDB: DB connection allocation failed",
"INFDB: couldn't allocate SQL stream",
"INFDB: bad SQL stream identifier"
};
int
main(int argc, char *argv[])
{
int i;
int notdone = 1;
int infErrno;
DBMSG msg;
SQLEnv *sqle = NULL;
SQLStream *sqls;
char buf[BUFSIZE];
char outbuf[BUFSIZE];
char errmsg[256];
STATUS rc;
// We just have to talk to stdin and stdout. However, stdout may be open
// in text mode, which is bad for data. Set it to binary mode.
#ifdef WIN32
_setmode(0, _O_BINARY);
_setmode(1, _O_BINARY);
#endif
rc = newSqlEnv(&sqle);
if ( rc != OK ) {
sendError("INFDB: Failed to allocate SQL environment.", -1);
return -1;
}
while ( notdone ) {
int bytesRead;
bytesRead = 0;
if ( (bytesRead = getCommand(&msg, buf, sizeof(buf))) <= 0 ) {
continue;
}
msg.retcode = 0;
infErrno = 0;
switch ( msg.mtype ) {
// Initiate a new connection.
case 'I':
{
int connid;
rc = newSqlConn(sqle, msg.data, &connid);
if ( rc != OK ) {
infErrno = CONN_ALLOC_FAIL;
break;
}
/*
// Need a new SQLStream to make subsequent requests.
rc = newSqlStream(sqle, connid, &streamid);
if ( rc != OK ) {
infErrno = STREAM_ALLOC_FAIL;
break;
}
sprintf(outbuf, "%d", streamid);
*/
sprintf(outbuf, "%d", connid);
sendResponse('i', strlen(outbuf), msg.sstream, 0, outbuf);
break;
}
case 'O':
{
// open an SQL stream.
int connid, streamid;
connid = atoi(msg.data);
rc = newSqlStream(sqle, connid, &streamid);
if (rc != OK) {
infErrno = STREAM_ALLOC_FAIL;
break;
}
sprintf(outbuf, "%d", streamid);
sendResponse('o', strlen(outbuf), msg.sstream, 0, outbuf);
break;
}
case 'K':
// klose an SQL stream
rc = freeSqlStream(sqle, msg.sstream);
sendResponse('k', 0, msg.sstream, 0, "");
break;
case 'C':
// request number of columns
rc = mapSqlStream(sqle, msg.sstream, &sqls);
if ( rc != OK ) {
infErrno = STREAM_BAD_ID;
break;
}
sprintf(outbuf, "%d", sqls->nresultcols);
sendResponse('c', strlen(outbuf), msg.sstream, 0, outbuf);
break;
case 'N':
// fetch next row
rc = mapSqlStream(sqle, msg.sstream, &sqls);
if ( rc != OK ) {
infErrno = STREAM_BAD_ID;
break;
}
sqls->errmsg[0] = '\0';
rc = SQLFetch(sqls->hstmt);
if ( rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO ) {
sqls->rownum++;
// if ( rc == SQL_SUCCESS_WITH_INFO ) {
// fprintf(stderr, "SQLFetch got SQL_SUCCESS_WITH_INFO\n");
// }
/* Get the data for all columns */
for ( i = 0; i < sqls->nresultcols; i++ ) {
rc = SQLGetData(sqls->hstmt, (UWORD)(i+1),
(sqls->coltype[i] == SQL_LONGVARBINARY ||
sqls->coltype[i] == SQL_LONGVARCHAR) ? SQL_C_DEFAULT :
SQL_C_CHAR,
sqls->data[i], sqls->collen[i], &sqls->outlen[i]);
if ( rc == SQL_SUCCESS_WITH_INFO &&
(UDWORD) sqls->outlen[i] > sqls->collen[i] ) {
UCHAR *tmp;
tmp = (UCHAR *) realloc(sqls->data[i], sqls->outlen[i]+1);
if ( tmp != NULL ) {
SDWORD dummy;
sqls->data[i] = tmp;
rc = SQLGetData(sqls->hstmt, (UWORD)(i+1), SQL_C_DEFAULT,
&tmp[sqls->collen[i]], sqls->outlen[i], &dummy);
sqls->collen[i] = sqls->outlen[i];
sqls->data[i][sqls->outlen[i]] = 0;
}
}
else if ( rc != SQL_SUCCESS ) {
sprintf(sqls->errmsg, "Problem retrieving data from data base, col %d", i+1);
msg.retcode = 2;
}
}
}
else if ( rc == SQL_NO_DATA_FOUND ) {
sqls->rownum = 0;
msg.retcode = 1;
}
else {
sqls->rownum = -1;
// Probably should get some status from ODBC for message
sprintf(sqls->errmsg, "Error occurred in fetching data");
}
if ( sqls->rownum < 0 ) {
sendError(errmsg, msg.sstream);
}
else {
sprintf(outbuf, "%d", sqls->rownum);
// rownum should be <= rowcount
sendResponse('n', strlen(outbuf), msg.sstream, msg.retcode, outbuf);
}
break;
case 'H':
// request an error message, if any
rc = mapSqlStream(sqle, msg.sstream, &sqls);
if ( rc != OK ) {
infErrno = STREAM_BAD_ID;
break;
}
sendError(sqls->errmsg, msg.sstream);
sqls->errmsg[0] = 0;
break;
case 'P':
// request write data
// in Inferno, param nums start at 0; in ODBC/SQL, they start at 1
// This leaves outdatalen[0] unused, hence available as final SQLBindParameter arg.
rc = mapSqlStream(sqle, msg.sstream, &sqls);
if ( rc != OK ) {
infErrno = STREAM_BAD_ID;
break;
}
sqls->errmsg[0] = 0;
if ( (i = atoi(msg.data) + 1) < 1 || i >= MAXCOLS ) {
sendError("Illegal param number", msg.sstream);
}
else {
int len;
char *p = msg.data + 4; // data points to param number
len = msg.nbytes - 4; // number of data chars
if ( len < 0 ) {
sendError("Write phase error II", msg.sstream);
break;
}
if ( sqls->setdata[i] != NULL ) {
free(sqls->setdata[i]);
}
sqls->setdata[i] = (char *) malloc(len + 1);
if ( sqls->setdata[i] == NULL ) {
sendError("Allocation error in server", msg.sstream);
break;
}
// Copy data we have into buffer, and if we don't have it all yet,
// try to get the rest.
sqls->setdatalen[i] = len++; // adjust len for trailing \n
bytesRead = &buf[bytesRead] - p; // number data bytes we have read
memcpy(sqls->setdata[i], p, bytesRead);
len -= bytesRead; // number bytes still to read,
while ( len > 0 ) {
int n;
if ( (n = read(0, sqls->setdata[i] + bytesRead, len)) <= 0 ) {
break;
}
bytesRead += n;
len -= n;
}
if ( len > 0 ) {
sendError("Couldn't read all of parameter", msg.sstream);
break;
}
rc = SQLBindParameter(sqls->hstmt, (UWORD)i, SQL_PARAM_INPUT,
SQL_C_BINARY, SQL_LONGVARBINARY,
sqls->setdatalen[i], 0, (PTR) i, 0, sqls->setdatalen);
if ( rc != SQL_SUCCESS ) {
sendError("BindParameter failed: maybe not supported", msg.sstream);
break;
}
sqls->setdatalen[0] = SQL_LEN_DATA_AT_EXEC(0);
sprintf(outbuf, "%d", bytesRead - 1);
sendResponse('p', strlen(outbuf), msg.sstream, 0, outbuf);
}
break;
case 'R':
// request read data
rc = mapSqlStream(sqle, msg.sstream, &sqls);
if ( rc != OK ) {
infErrno = STREAM_BAD_ID;
break;
}
if ( (i = atoi(msg.data)) < 0 || i >= sqls->nresultcols || sqls->rownum <= 0 ) {
sendError(sqls->rownum <= 0 ? "No current row" : "Illegal column number", msg.sstream);
}
else if ( sqls->outlen[i] == SQL_NULL_DATA || sqls->outlen[i] == SQL_NO_TOTAL ) {
sendResponse('r', 0, msg.sstream, 0, "");
}
else {
if ( sqls->coltype[i] == SQL_VARCHAR ) {
sqls->outlen[i] = strlen(sqls->data[i]);
}
sendResponse('r', sqls->outlen[i], msg.sstream, 0, sqls->data[i]);
}
break;
case 'T':
// request column title
rc = mapSqlStream(sqle, msg.sstream, &sqls);
if ( rc != OK ) {
infErrno = STREAM_BAD_ID;
break;
}
if ( (i = atoi(msg.data)) < 0 || i >= sqls->nresultcols ) {
sendError("Illegal column number", msg.sstream);
}
else {
sendResponse('t', strlen(sqls->colname[i]), msg.sstream, 0, sqls->colname[i]);
}
break;
case 'W':
// execute command
rc = mapSqlStream(sqle, msg.sstream, &sqls);
if ( rc != OK ) {
infErrno = STREAM_BAD_ID;
break;
}
if ( sqls->hstmt ) {
SQLFreeStmt(sqls->hstmt, SQL_CLOSE);
SQLFreeStmt(sqls->hstmt, SQL_UNBIND);
}
// Look for special extensions
if ( strnicmp(msg.data, "commit", 6) == 0 ) {
SQLConn *sqlc;
rc = mapSqlConn(sqle, sqls->connid, &sqlc);
rc = SQLTransact(SQL_NULL_HENV, sqlc->hdbc, SQL_COMMIT);
}
else if ( strnicmp(msg.data, "rollback", 8) == 0 ) {
SQLConn *sqlc;
rc = mapSqlConn(sqle, sqls->connid, &sqlc);
rc = SQLTransact(SQL_NULL_HENV, sqlc->hdbc, SQL_ROLLBACK);
}
else if ( strnicmp(msg.data, "tables", 6) == 0 ) {
rc = SQLTables(sqls->hstmt, NULL, 0, NULL, 0, NULL, 0, NULL, 0);
}
else if ( strnicmp(msg.data, "columns", 7) == 0 ) {
UCHAR *tbl;
for ( tbl = msg.data+8; *tbl == ' ' || *tbl == '\t'; tbl++ ) { }
rc = SQLColumns(sqls->hstmt, NULL, 0, NULL, 0, tbl, SQL_NTS, NULL, 0);
}
else {
rc = SQLExecDirect(sqls->hstmt, msg.data, SQL_NTS);
}
outbuf[0] = '\0';
while ( rc == SQL_NEED_DATA ) {
PTR pToken;
// SDWORD pnum;
rc = SQLParamData(sqls->hstmt, &pToken);
// pnum = (SDWORD) pToken;
#define pnum (int)pToken
if ( rc == SQL_NEED_DATA ) {
int retcode;
if ( sqls->setdata[pnum] == NULL || sqls->setdatalen[pnum] <= 0 ) {
sprintf(outbuf, "Parameter %d not set\n", pnum);
break;
}
for ( i = 0; i < sqls->setdatalen[pnum]; ) {
int togo;
togo = 1024;
if ( sqls->setdatalen[pnum] - i < 1024 ) {
togo = sqls->setdatalen[pnum] - i;
}
retcode = SQLPutData(sqls->hstmt,
sqls->setdata[pnum] + i, togo);
i += togo;
if ( retcode != SQL_SUCCESS ) {
print_err(sqle, -1, msg.sstream, &outbuf[strlen(outbuf)], sizeof (outbuf) - strlen(outbuf));
break;
}
}
if ( retcode != SQL_SUCCESS /* && retcode != SQL_SUCCESS_WITH_INFO */) {
break;
}
}
}
if ( rc != SQL_SUCCESS ) {
strcat(outbuf, "Command execution failed\n");
switch ( rc ) {
case SQL_SUCCESS_WITH_INFO:
strcat(outbuf, ": SQL_SUCCESS_WITH_INFO");
print_err(sqle, -1, msg.sstream, &outbuf[strlen(outbuf)], sizeof (outbuf) - strlen(outbuf));
break;
case SQL_ERROR:
strcat(outbuf, ": SQL_ERROR");
print_err(sqle, -1, msg.sstream, &outbuf[strlen(outbuf)], sizeof (outbuf) - strlen(outbuf));
break;
case SQL_NEED_DATA:
strcat(outbuf, ": SQL_NEED_DATA");
break;
case SQL_STILL_EXECUTING:
strcat(outbuf, ": SQL_STILL_EXECUTING");
break;
case SQL_INVALID_HANDLE:
strcat(outbuf, ": SQL_INVALID_HANDLE");
break;
}
sendError(outbuf, msg.sstream);
break;
}
SQLNumResultCols(sqls->hstmt, &sqls->nresultcols);
if ( sqls->nresultcols == 0 ) { // was not 'select' command
SQLRowCount(sqls->hstmt, &sqls->rowcount); // we don't use this, do we?
}
else { // get the column labels, save for later
for ( i = 0; i < sqls->nresultcols; i++ ) {
int newlen;
SQLDescribeCol(sqls->hstmt, (UWORD) (i+1), sqls->colname[i],
(SWORD)sizeof(sqls->colname[i]),
&sqls->colnamelen, &sqls->coltype[i], &sqls->collen[i],
&sqls->scale, &sqls->nullable);
sqls->colname[i][sqls->colnamelen] = 0;
// Adjust the length, since we are converting everything to strings.
if ( (newlen = display_size(sqls->coltype[i], sqls->collen[i],
sqls->colname[i])) != 0 ) {
sqls->collen[i] = newlen;
}
if ( sqls->collen[i] == 0 ) {
sqls->collen[i] = BUFSIZE;
}
sqls->data[i] = (UCHAR *) malloc(sqls->collen[i] + 1);
/*
SQLBindCol(sqls->hstmt, (UWORD) (i+1), newlen > 0 ? SQL_C_CHAR : SQL_C_DEFAULT,
sqls->data[i], sqls->collen[i], &sqls->outlen[i]);
*/
}
sqls->rownum = 0;
}
sendResponse('w', 0, msg.sstream, 0, "");
break;
case 'X':
notdone = 0;
break;
default:
sprintf(sqls->errmsg, "Unknown command: %c", msg.mtype);
sendError(sqls->errmsg, msg.sstream);
sqls->errmsg[0] = '\0';
break;
} // end of switch (msg.mtype)
if ( infErrno > 0 && infErrno < LAST_ERROR_NO ) {
sendError(iError[infErrno - 1], msg.sstream);
}
} // end of while (notdone)
rc = freeSqlEnv(&sqle);
return 0;
}
//
// All the incoming commands should end with a newline character.
// We read until we get one. Then we verify that we have read as
// many bytes as the count in message says we should.
//
int
getCommand(DBMSGP msgp, UCHAR *buf, int bufsiz)
{
int bytesRead = 0;
int rc = 0;
msgp->mtype = '\0';
while ( bufsiz > 0 && (rc = read(0, &buf[bytesRead], bufsiz)) > 0 ) {
bytesRead += rc;
bufsiz -= rc;
msgp->bytesNotRead -= rc;
if ( msgp->mtype == '\0' && bytesRead >= REQ_HEADER_SIZE ) {
if ( (msgp->version = buf[1]) != '1' ) { // wrong version, give up
char *wrong_version = "Message has wrong version number";
sendResponse('h', strlen(wrong_version), 0, 0, wrong_version);
return -1;
}
msgp->mtype = buf[0];
msgp->nbytes = atoi(buf+OFFSET_LENGTH);
msgp->sstream = atoi(buf+OFFSET_STREAM);
msgp->data = buf+OFFSET_REQ_DATA;
msgp->bytesNotRead = REQ_HEADER_SIZE + msgp->nbytes + 1 - bytesRead;
if ( bufsiz > msgp->bytesNotRead ) {
bufsiz = msgp->bytesNotRead;
}
}
}
if ( rc < 0 ) {
// log a problem
// fprintf(stderr, "Problem reading from client\n");
return rc;
}
if ( msgp->bytesNotRead == 0 ) {
msgp->data[msgp->nbytes] = 0; // discard final newline
}
return bytesRead;
}
void
sendResponse(char type, int lendata, int sstream, int retcode, char *data)
{
char hdr[RES_HEADER_SIZE+2];
sprintf(hdr, "%c1%11d %3d %3d ", type, lendata, sstream, retcode);
write(1, hdr, RES_HEADER_SIZE);
write(1, data, lendata);
write(1, "\n", 1);
}
void
sendError(char *errmsg, int sstream)
{
sendResponse('h', strlen(errmsg), sstream, 0, errmsg);
}
void
print_err(SQLEnv *sqle, int connid, int streamid, UCHAR * buf, int bufsiz)
{
RETCODE rc;
UCHAR stateString[40];
SDWORD native;
SWORD msglen;
SQLConn *sqlc;
SQLStream *sqls;
HENV *henv;
HDBC *hdbc;
HSTMT *hstmt;
henv = sqle->henv;
rc = mapSqlConn(sqle, connid, &sqlc);
hdbc = rc == OK ? sqlc->hdbc : SQL_NULL_HDBC;
rc = mapSqlStream(sqle, streamid, &sqls);
hstmt = rc == OK ? sqls->hstmt : SQL_NULL_HSTMT;
rc = SQLError(henv, hdbc, hstmt, stateString, &native, buf, (SWORD) bufsiz, &msglen);
}
#define MAX_NUM_PRECISION 15
/* Define max length of char string representation of number as: */
/* = max(precision) + leading sign + E + exp sign + max exp length */
/* = 15 + 1 + 1 + 1 + 2 */
/* = 15 + 5 */
#define MAX_NUM_STRING_SIZE (MAX_NUM_PRECISION + 5)
UDWORD
display_size(SWORD coltype, UDWORD collen, UCHAR *colname)
{
switch (coltype) {
case SQL_CHAR:
case SQL_VARCHAR:
return max(collen, strlen(colname));
case SQL_SMALLINT:
case SQL_TINYINT:
case SQL_BIT:
return max(6, strlen(colname));
case SQL_INTEGER:
return max(11, strlen(colname));
case SQL_BIGINT:
return max(30, strlen(colname));
case SQL_DATE:
case SQL_TIME:
case SQL_TIMESTAMP:
return max(50, strlen(colname));
case SQL_DECIMAL:
case SQL_NUMERIC:
case SQL_REAL:
case SQL_FLOAT:
case SQL_DOUBLE:
return(max(MAX_NUM_STRING_SIZE, strlen(colname)));
case SQL_LONGVARBINARY:
case SQL_LONGVARCHAR:
return BUFSIZE;
/* Note that this function only supports the core data types. */
/* For unknown data types, the caller should assume binary data */
default:
/* fprintf(stderr, "Unknown datatype, %d\n", coltype); */
return 0;
}
}
STATUS
newSqlEnv(SQLEnv **sqle)
{
SQLEnv *newenv;
STATUS rc;
newenv = (SQLEnv *) calloc(1, sizeof(SQLEnv));
if (newenv == NULL) {
return ERR;
}
rc = SQLAllocEnv(&newenv->henv);
if ( rc != SQL_SUCCESS) {
free (newenv);
return ERR;
}
*sqle = newenv;
return OK;
}
STATUS
freeSqlEnv(SQLEnv **sqle)
{
int i;
STATUS rc;
for (i = 0; i < (*sqle)->maxstream; i++) {
// Free this stream.
// Connection will be freed automatically.
rc = freeSqlStream(*sqle, i);
}
// dealloc the stream structures
// dealloc the connect structures
SQLFreeEnv((*sqle)->henv);
// dealloc the env structure
return OK;
}
STATUS
mapSqlConn(SQLEnv *sqle, int connid, SQLConn **sqlc)
{
if ( connid >= 0 && connid < sqle->maxconn ) {
*sqlc = sqle->scarray[connid];
if ( (*sqlc)->state == SQLC_INUSE )
return OK;
}
return ERR;
}
STATUS
newSqlConn(SQLEnv *sqle, char *info, int *connid)
{
SQLConn **newarray, *sqlc;
int newid = -1, i;
STATUS rc;
*connid = -1;
// Connect to the database.
// Search for an available connection structure to reuse
for ( i = 0; i < sqle->maxconn; i++ ) {
sqlc = sqle->scarray[i];
if ( sqlc != NULL && sqlc->state == SQLC_FREE ) {
newid = i;
break;
}
}
if ( newid == -1 ) {
// Assign a new connection id
newid = sqle->maxconn++;
// Extend the connection pointer array
newarray = (SQLConn **) realloc((char *) sqle->scarray,
sqle->maxconn * sizeof(SQLConn*));
if ( newarray == NULL ) {
return ERR;
}
sqle->scarray = newarray;
// Allocate a new connection structure
sqlc = (SQLConn *) calloc(1, sizeof(SQLConn));
if ( sqlc == NULL ) {
return ERR;
}
sqle->scarray[newid] = sqlc;
}
// Ask ODBC for a new connection handle
rc = SQLAllocConnect(sqle->henv, &sqlc->hdbc);
if (rc == SQL_ERROR) {
return ERR;
}
sqlc->refcount = 0;
sqlc->state = SQLC_INUSE;
// Extract the username, password, and database name
rc = parseConnInfo(sqlc, info);
if ( rc != OK ) {
return ERR;
}
// Request an ODBC connection to the database
rc = SQLConnect(sqlc->hdbc, sqlc->dbname, SQL_NTS, sqlc->user, SQL_NTS,
sqlc->passwd, SQL_NTS);
if ( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO ) {
// log error?
// Should try to get something more specific from ODBC
sprintf(sqlc->errmsg, "Connect failed: user = %s, passwd = %s, dbname = %s",
sqlc->user, sqlc->passwd, sqlc->dbname);
SQLDisconnect(sqlc->hdbc);
SQLFreeConnect(sqlc->hdbc);
return ERR;
}
*connid = newid;
// Set connect option to disable auto commit
rc = SQLSetConnectOption(sqlc->hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
if ( rc != SQL_SUCCESS ) {
return WARN;
}
return OK;
}
STATUS
freeSqlConn(SQLEnv *sqle, int connid)
{
SQLConn *sqlc;
STATUS rc;
rc = mapSqlConn(sqle, connid, &sqlc);
if ( rc != OK ) {
return WARN;
}
SQLDisconnect(sqlc->hdbc);
SQLFreeConnect(sqlc->hdbc);
sqlc->state = SQLC_FREE;
return OK;
}
STATUS
mapSqlStream(SQLEnv *sqle, int streamid, SQLStream **sqls)
{
if ( streamid >= 0 && streamid < sqle->maxstream ) {
*sqls = sqle->ssarray[streamid];
if ( (*sqls)->state == SQLS_INUSE )
return OK;
}
return ERR;
}
STATUS
newSqlStream(SQLEnv *sqle, int connid, int *streamid)
{
HSTMT hstmt;
SQLConn *sqlc;
SQLStream **newarray, *sqls;
int newid = -1, i;
STATUS rc;
rc = mapSqlConn(sqle, connid, &sqlc);
if (rc != OK) {
return ERR;
}
// Search for an available stream structure to reuse
for ( i = 0; i < sqle->maxstream; i++ ) {
sqls = sqle->ssarray[i];
if ( sqls != NULL && sqls->state == SQLS_FREE ) {
newid = i;
break;
}
}
if ( newid == -1 ) {
// Assign a new stream id
newid = sqle->maxstream++;
// Extend the stream pointer array
newarray = (SQLStream **) realloc((char *) sqle->ssarray,
sqle->maxstream * sizeof(SQLStream*));
if ( newarray == NULL ) {
return ERR;
}
sqle->ssarray = newarray;
// Allocate a new stream structure
sqls = (SQLStream *) calloc(1, sizeof(SQLStream));
if ( sqls == NULL ) {
return ERR;
}
sqle->ssarray[newid] = sqls;
}
// Associate new stream with specified connection
sqls->connid = connid;
sqlc->refcount++;
// Ask ODBC to allocate a new statement handle
rc = SQLAllocStmt(sqlc->hdbc, &hstmt);
if (rc == SQL_ERROR) {
return ERR;
}
sqls->hstmt = hstmt;
sqls->state = SQLS_INUSE;
*streamid = newid;
return OK;
}
STATUS
freeSqlStream(SQLEnv *sqle, int streamid)
{
SQLConn *sqlc;
SQLStream *sqls;
STATUS rc;
rc = mapSqlStream(sqle, streamid, &sqls);
if ( rc != OK ) {
return WARN;
}
sqls->state = SQLS_FREE;
rc = SQLFreeStmt(sqls->hstmt, SQL_DROP);
rc = mapSqlConn(sqle, sqls->connid, &sqlc);
if ( rc != OK ) {
return WARN;
}
if ( --sqlc->refcount == 0 )
{
rc = freeSqlConn(sqle, sqls->connid);
if (rc != OK) {
return WARN;
}
}
return OK;
}
STATUS
parseConnInfo(SQLConn *sqlc, char *info)
{
UCHAR *temp;
// The argument 'info' points to a buffer containing a string
// of the form "username/password/dbname\n". We will use 'strtok'
// to tokenize the string into the parts we need, keeping
// copies in the 'sqlc' structure.
temp = strtok(info, "/\n");
if ( temp == NULL ) {
return ERR;
}
strncpy(sqlc->user, temp, 48);
temp = strtok(NULL, "/\n");
if ( temp == NULL ) {
return ERR;
}
strncpy(sqlc->passwd, temp, 48);
temp = strtok(NULL, "/\n");
if ( temp == NULL ) {
return ERR;
}
strncpy(sqlc->dbname, temp, 48);
return OK;
}
|