aboutsummaryrefslogtreecommitdiffstats
path: root/lib/odbc/c_src/odbcserver.c
blob: 78a6fc41a8a036f67c8b7e654436183a165c07c1 (plain) (blame)
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
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 1999-2010. All Rights Reserved.
 *
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * %CopyrightEnd%
 *

 */

/*
  DESCRIPTION: Erlang ODBC (Open Database Connectivity) application. An
  erlang control process sends request to the c-process that queries the
  database using the Microsoft ODBC API. The c-process is implemented
  using two threads the supervisor thread and the database handler thread.
  If the database thread should hang erlang can close the c-process down
  by sendig a shutdown request to the supervisor thread.

  Erlang will start this c-process as a port-program and send information
  regarding inet-port nummbers through the erlang-port.
  After that c-process will communicate via sockets with erlang. The
  reason for this is that some odbc-drivers do unexpected things with
  stdin/stdout messing up the erlang-port communication.
  
  
  Command protocol between Erlang and C
   -------------------------------------
   The requests from Erlang to C are byte lists composed as [CommandByte,
   Bytes, StringTerminator]
   
   CommandByte - constants between 0 and 255
   identifing the request defined in odbc_internal.hrl and odbcserver.h

   Bytes - How to interpret this sequence of bytes depends on the
   CommandByte.

   StringTerminator - 0

   When the C-program processed the request from erlang it will use the
   ei-interface to create an Erlang term. This term will be sent the
   erlang via a socket. The Erlang control process, will forward
   it to the client that does binary_to_term before returning the result
   to the client program.

   Here follows a list of [CommandByte, Bytes] that describes the possible
   values. Note the Bytes part may be empty as in the case
   of ?CLOSE_CONNECTION and if integer values may be larger than 255
   they are converted to string values.

   [?OPEN_CONNECTION, C_AutoCommitMode, C_TraceDriver, C_SrollableCursors,
   C_TupelRow, BinaryStrings, ConnectionStr]
   [?CLOSE_CONNECTION]		     
   [?COMMIT_TRANSACTION, CommitMode]
   [?QUERY, SQLQuery]
   [?SELECT_COUNT, SQLQuery]
   [?SELECT, ?SELECT_FIRST]
   [?SELECT, ?SELECT_LAST]	
   [?SELECT, ?SELECT_NEXT]
   [?SELECT, ?SELECT_PREV]
   [?SELECT, CursorRelation, integer_to_list(OffSet), ";",
   integer_to_list(N), ";"]
   [?PARAM_QUERY, Binary] 

   C_AutoCommitMode - ?ON | ?OFF
   C_TraceDriver - ?ON | ?OFF
   C_SrollableCursors - ?ON | ?OFF
   C_TupelRow -  - ?ON | ?OFF
   BinaryStrings - ?ON | ?OFF
   ConnectionStr -  String
   CommitMode -  ?COMMIT | ?ROLLBACK
   SQLQuery  - String
   CursorRelation - ?SELECT_RELATIVE | ?SELECT_ABSOLUTE | ?SELECT_N_NEXT
   OffSet - integer
   N - integer
   Binary - binary encodede tuple of {SQLQuery, NoRows, Parameters}
   NoRows - integer
   Parameters - [{Datatype, InOrOut, Value}]
   InOrOut = [ERL_ODBC_IN | ERL_ODBC_OUT | ERL_ODBC_INOUT]
   Datatype -  USER_INT | USER_SMALL_INT | {USER_DECIMAL, Precision, Scale} |
   {USER_NMERIC, Precision, Scale} | {USER_CHAR, Max} | {USER_VARCHAR, Max} |
   {USER_WVARCHAR, Max} | {USER_FLOAT, Precision} | USER_REAL | USER_DOUBLE |
   USER_TIMESTAMP
   Scale - integer
   Precision - integer
   Max - integer
*/

/* ----------------------------- INCLUDES ------------------------------*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#ifdef UNIX
#include <unistd.h>
#endif 

#if defined WIN32
#include <winsock2.h> 
/*  #include <ws2tcpip.h >  When we can support a newer c-compiler*/
#include <windows.h> 
#include <fcntl.h>
#include <sql.h>
#include <sqlext.h>
#else
#include "sql.h"
#include "sqlext.h"
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netdb.h>
#include <netinet/in.h>
#endif

#include <limits.h>

#include "ei.h"
#include "odbcserver.h"

/* ---------------- Main functions ---------------------------------------*/
static void spawn_sup(const char *port);
#ifdef WIN32
DWORD WINAPI database_handler(const char *port);
#else
void database_handler(const char *port);
#endif
static db_result_msg handle_db_request(byte *reqstring, db_state *state);
static void supervise(const char *port);
/* ----------------- ODBC functions --------------------------------------*/

static db_result_msg db_connect(byte *connStrIn, db_state *state);
static db_result_msg db_close_connection(db_state *state);
static db_result_msg db_end_tran(byte compleationtype, db_state *state);
static db_result_msg db_query(byte *sql, db_state *state);
static db_result_msg db_select_count(byte *sql,db_state *state);
static db_result_msg db_select(byte *args, db_state *state);
static db_result_msg db_param_query(byte *buffer, db_state *state);
static db_result_msg db_describe_table(byte *sql, db_state *state);

/* ------------- Encode/decode functions -------- ------------------------*/

static db_result_msg encode_empty_message(void);
static db_result_msg encode_error_message(char *reason);
static db_result_msg encode_atom_message(char *atom);
static db_result_msg encode_result(db_state *state);
static db_result_msg encode_result_set(SQLSMALLINT num_of_columns,
				       db_state *state);
static db_result_msg encode_out_params(db_state *state,
                                       int cols,
                                       param_array *params,
                                       int num_param_values);
static db_result_msg encode_column_name_list(SQLSMALLINT num_of_columns,
					     db_state *state);
static db_result_msg encode_value_list(SQLSMALLINT num_of_columns,
				       db_state *state);
static db_result_msg encode_value_list_scroll(SQLSMALLINT num_of_columns,
					      SQLSMALLINT Orientation,
					      SQLINTEGER OffSet, int N,
					      db_state *state);
static db_result_msg encode_row_count(SQLINTEGER num_of_rows,
				      db_state *state);
static void encode_column_dyn(db_column column, int column_nr,
			      db_state *state);
static void encode_data_type(SQLINTEGER sql_type, SQLINTEGER size,
			     SQLSMALLINT decimal_digits, db_state *state);
static Boolean decode_params(db_state *state, byte *buffer, int *index, param_array **params,
			  int i, int j);

/*------------- Erlang port communication functions ----------------------*/

static int read_exact(byte *buf, int len);
static byte * receive_erlang_port_msg(void);

/* ------------- Socket communication functions --------------------------*/

#ifdef WIN32
static SOCKET connect_to_erlang(const char *port); 
static void send_msg(db_result_msg *msg, SOCKET socket);
static byte *receive_msg(SOCKET socket);
static Boolean receive_msg_part(SOCKET socket,
				byte * buffer, size_t msg_len);
static Boolean send_msg_part(SOCKET socket, byte * buffer, size_t msg_len);
static void close_socket(SOCKET socket);
static void init_winsock(void);
#elif defined(UNIX)
static int connect_to_erlang(const char *port);
static void send_msg(db_result_msg *msg, int socket);
static byte *receive_msg(int socket);
static Boolean receive_msg_part(int socket, byte * buffer, size_t msg_len);
static Boolean send_msg_part(int socket, byte * buffer, size_t msg_len);
static void close_socket(int socket); 
#endif
static void clean_socket_lib(void);

/*------------- Memory handling funtions --------------------------------*/

static void * safe_malloc(int size);
static void * safe_realloc(void * ptr, int size);
static db_column * alloc_column_buffer(int n);
static void free_column_buffer(db_column **columns, int n);
static void free_params(param_array **params, int cols);
static void clean_state(db_state *state);

/* ------------- Init/map/bind/retrive functions -------------------------*/

static void init_driver(int erl_auto_commit_mode, int erl_trace_driver,
			   db_state *state);
static void init_param_column(param_array *params, byte *buffer, int *index,
			      int num_param_values, db_state* state);

static void init_param_statement(int cols,
				 int num_param_values, 
				 db_state *state,
				 param_status *status);

static void map_dec_num_2_c_column(col_type *type, int precision,
				   int scale);
static db_result_msg map_sql_2_c_column(db_column* column);


static param_array * bind_parameter_arrays(byte *buffer, int *index,
					   int cols,
					   int num_param_values,
					   db_state *state);
static void * retrive_param_values(param_array *Param);

static db_column retrive_binary_data(db_column column, int column_nr,
				     db_state *state);
static db_result_msg retrive_scrollable_cursor_support_info(db_state
							    *state);
static int num_out_params(int num_of_params, param_array* params);
/* ------------- Error handling functions --------------------------------*/

static diagnos get_diagnos(SQLSMALLINT handleType, SQLHANDLE handle);

/* ------------- Boolean functions ---------------------------------------*/

static db_result_msg more_result_sets(db_state *state);
static Boolean sql_success(SQLRETURN result);
static void str_tolower(char *str, int len);

/* ----------------------------- CODE ------------------------------------*/

#if defined(WIN32)
#  define DO_EXIT(code) do { ExitProcess((code)); exit((code));} while (0)
/* exit() called only to avoid a warning */
#else
#  define DO_EXIT(code) exit((code))
#endif

/* ----------------- Main functions --------------------------------------*/

int main(void)
{
    byte *msg = NULL;
    char *temp = NULL, *supervisor_port = NULL, *odbc_port = NULL;
    size_t length;
#ifdef WIN32
    _setmode(_fileno( stdin),  _O_BINARY);
#endif
    
    msg = receive_erlang_port_msg();

    temp = strtok(msg, ";");
    length = strlen(temp);
    supervisor_port = safe_malloc(length + 1);
    strcpy(supervisor_port, temp);

    temp = strtok(NULL, ";");
    length = strlen(temp);
    odbc_port = safe_malloc(length + 1);
    strcpy(odbc_port, temp);
    
    free(msg);

    spawn_sup(supervisor_port);
    database_handler(odbc_port);

    return 0;
}

#ifdef WIN32
static void spawn_sup(const char *port)
{
    DWORD threadId;
    (HANDLE)_beginthreadex(NULL, 0, supervise, port, 0, &threadId);
}
#elif defined(UNIX)
static void spawn_sup(const char *port)
{
    pthread_t thread;
    int result;
    
    result = pthread_create(&thread, NULL,
			    (void *(*)(void *))supervise,
			    (void *)port);
    if (result != 0)
	DO_EXIT(EXIT_THREAD);
}
#endif   

void supervise(const char *port) {
    byte *msg = NULL;
    int reason;
#ifdef WIN32
    SOCKET socket;
    init_winsock();
#elif defined(UNIX)
    int socket; 
#endif
    
    socket = connect_to_erlang(port);
    msg = receive_msg(socket);

    if(msg[0] == SHUTDOWN) {
	reason = EXIT_SUCCESS;
    } else {
	reason = EXIT_FAILURE; /* Should not happen */
    }

    free(msg);
    close_socket(socket);
    clean_socket_lib();
    DO_EXIT(reason);    
}

#ifdef WIN32
DWORD WINAPI database_handler(const char *port)
#else
    void database_handler(const char *port) 
#endif
{ 
    db_result_msg msg;
    byte *request_buffer = NULL;
    db_state state =
    {NULL, NULL, NULL, NULL, 0, {NULL, 0, 0},
     FALSE, FALSE, FALSE, FALSE, FALSE, FALSE};
    byte request_id;
#ifdef WIN32
    SOCKET socket;
    init_winsock();
#elif defined(UNIX)
    int socket;
#endif

    socket = connect_to_erlang(port);
  
    do {      
	request_buffer = receive_msg(socket);

	request_id = request_buffer[0];
	msg = handle_db_request(request_buffer, &state);

	send_msg(&msg, socket); /* Send answer to erlang */
	    
	if (msg.dyn_alloc) {
	    ei_x_free(&(state.dynamic_buffer));
	} else {
	    free(msg.buffer);
	    msg.buffer = NULL;
	}   
	
	free(request_buffer);
	request_buffer = NULL;
    
    } while(request_id != CLOSE_CONNECTION);

    shutdown(socket, 2);
    close_socket(socket);
    clean_socket_lib();
    /* Exit will be done by suervisor thread */ 
}
 
/* Description: Calls the appropriate function to handle the database
   request recived from the erlang-process. Returns a message to send back
   to erlang. */
static db_result_msg handle_db_request(byte *reqstring, db_state *state)
{
    byte *args;
    byte request_id;
  
    /* First byte is an index that identifies the requested command the
       rest is the argument string. */
    request_id = reqstring[0]; 
    args = reqstring + sizeof(byte);
  
    switch(request_id) {
    case OPEN_CONNECTION:
	return db_connect(args, state); 
    case CLOSE_CONNECTION:
	return db_close_connection(state);
    case COMMIT_TRANSACTION:
	if(args[0] == COMMIT) {
	    return db_end_tran((byte)SQL_COMMIT, state);
	} else { /* args[0] == ROLLBACK */
	    return db_end_tran((byte)SQL_ROLLBACK, state);
	}
    case QUERY:
	return db_query(args, state);
    case SELECT_COUNT:
	return db_select_count(args, state);
    case SELECT:
	return db_select(args, state);
    case PARAM_QUERY:
	return db_param_query(args, state);
    case DESCRIBE:
	return db_describe_table(args, state);
    default:
	DO_EXIT(EXIT_FAILURE); /* Should not happen */
    }  
}
 
/* ----------------- ODBC-functions  ----------------------------------*/
 
/* Description: Tries to open a connection to the database using
   <connStrIn>, returns a message indicating the outcome. */
static db_result_msg db_connect(byte *args, db_state *state)
{
    SQLCHAR connStrOut[MAX_CONN_STR_OUT + 1] = {0};
    SQLRETURN result;
    SQLSMALLINT stringlength2ptr = 0, connlen;
    db_result_msg msg;
    diagnos diagnos;
    byte *connStrIn; 
    int erl_auto_commit_mode, erl_trace_driver,
	use_srollable_cursors, tuple_row_state, binary_strings;
  
    erl_auto_commit_mode = args[0];
    erl_trace_driver = args[1];
    use_srollable_cursors = args[2];
    tuple_row_state = args[3];
    binary_strings = args[4];
    connStrIn = args + 5 * sizeof(byte);

    if(tuple_row_state == ON) {
	    tuple_row(state) = TRUE;  
    } else {
	    tuple_row(state) = FALSE;  
    }
    
    if(binary_strings == ON) {
	    binary_strings(state) = TRUE;  
    } else {
	    binary_strings(state) = FALSE;  
    }
    
    if(use_srollable_cursors == ON) {
	    use_srollable_cursors(state) = TRUE;
    } else {
	    use_srollable_cursors(state) = FALSE;
    }

    init_driver(erl_auto_commit_mode, erl_trace_driver, state); 
      
    connlen = (SQLSMALLINT)strlen((const char*)connStrIn);
    result = SQLDriverConnect(connection_handle(state), NULL,
			      (SQLCHAR *)connStrIn, 
			      connlen, 
			      connStrOut, (SQLSMALLINT)MAX_CONN_STR_OUT,
			      &stringlength2ptr, SQL_DRIVER_NOPROMPT);
  
    if (!sql_success(result)) {
	diagnos = get_diagnos(SQL_HANDLE_STMT, statement_handle(state));
	strcat((char *)diagnos.error_msg,
	       " Connection to database failed.");
	msg = encode_error_message(diagnos.error_msg);
    
	if(!sql_success(SQLFreeHandle(SQL_HANDLE_DBC,
				      connection_handle(state))))
	    DO_EXIT(EXIT_FREE);
	if(!sql_success(SQLFreeHandle(SQL_HANDLE_ENV,
				      environment_handle(state))))
	    DO_EXIT(EXIT_FREE);
    
	return msg;
    }

    msg = retrive_scrollable_cursor_support_info(state);
  
    return msg;
}

/* Close the connection to the database. Returns an ok or error message. */
static db_result_msg db_close_connection(db_state *state)
{
    int index;
    SQLRETURN result;
    diagnos diagnos;

    if (associated_result_set(state)) {
	clean_state(state);
    }
  
    result = SQLDisconnect(connection_handle(state));
                       
    if (!sql_success(result)) {
	diagnos = get_diagnos(SQL_HANDLE_DBC, connection_handle(state));
	return encode_error_message(diagnos.error_msg);
    }

    if(!sql_success(SQLFreeHandle(SQL_HANDLE_DBC,
				  connection_handle(state))))
	DO_EXIT(EXIT_FREE);
    if(!sql_success(SQLFreeHandle(SQL_HANDLE_ENV,
				  environment_handle(state))))
	DO_EXIT(EXIT_FREE);

    return encode_atom_message("ok");
}
 

/* Description: Requests a commit or rollback operation for all active
   operations on all statements associated with the connection
   handle <connection_handle(state)>. Returns an ok or error message. */
static db_result_msg db_end_tran(byte compleationtype, db_state *state)
{
    SQLRETURN result;
    diagnos diagnos;

    result = SQLEndTran(SQL_HANDLE_DBC, connection_handle(state),
			(SQLSMALLINT)compleationtype);

    if (!sql_success(result)) {
	diagnos = get_diagnos(SQL_HANDLE_DBC, connection_handle(state));
	return encode_error_message(diagnos.error_msg);
    } else {
	return encode_atom_message("ok");
    }
}
 
/* Description: Executes an sql query and encodes the result set as an
   erlang term into the message buffer of the returned message-struct. */
static db_result_msg db_query(byte *sql, db_state *state)
{
    char *atom;
    int num_of_rows, elements, update;
    SQLSMALLINT num_of_columns;
    SQLRETURN result;
    SQLINTEGER RowCountPtr;
    db_result_msg msg;
    diagnos diagnos;
    byte is_error[6];
    
    if (associated_result_set(state)) {
	clean_state(state);
    }
    associated_result_set(state) = FALSE;

    msg = encode_empty_message();
    
    if(!sql_success(SQLAllocHandle(SQL_HANDLE_STMT,
				   connection_handle(state),
				   &statement_handle(state))))
	DO_EXIT(EXIT_ALLOC);

    result = SQLExecDirect(statement_handle(state), sql, SQL_NTS);
    
    /* SQL_SUCCESS_WITH_INFO at this point may indicate an error in user input. */
    if (result != SQL_SUCCESS && result != SQL_NO_DATA_FOUND) {
	    diagnos =  get_diagnos(SQL_HANDLE_STMT, statement_handle(state));
	    if(strcmp((char *)diagnos.sqlState, INFO) == 0) { 
		    is_error[0] = 0;
		    strncat((char *)is_error, (char *)diagnos.error_msg, 
			    5);
		    str_tolower((char *)&is_error, 5);
		    /* The ODBC error handling could have been more
		       predictable but alas ... we try to make the best of
		       it as we want a nice and clean Erlang API  */
		    if((strcmp((char *)is_error, "error") == 0))
		    { 
 			    msg = encode_error_message((char *)diagnos.error_msg); 
 			    clean_state(state); 
			    return msg;
		    }  
	    } else {
		    msg = encode_error_message((char *)diagnos.error_msg); 
		    clean_state(state); 
		    return msg;   
	    }	    
    }

    ei_x_new_with_version(&dynamic_buffer(state));

    /* OTP-5759, fails when 0 rows deleted */
    if (result == SQL_NO_DATA_FOUND) {
	msg = encode_result(state);
    } else {
	/* Handle multiple result sets */
	do {
	    ei_x_encode_list_header(&dynamic_buffer(state), 1);
	    msg = encode_result(state);
	    /* We don't want to continue if an error occured */
	    if (msg.length != 0) { 
		break;
	    }
	    msg = more_result_sets(state);
	    /* We don't want to continue if an error occured */
	    if (msg.length != 0) { 
		break;
	    }
	} while (exists_more_result_sets(state)); 
	
	ei_x_encode_empty_list(&dynamic_buffer(state));
    }
	
    clean_state(state);
    
    if (msg.length != 0) { /* An error has occurred */
	ei_x_free(&(dynamic_buffer(state))); 
	return msg;
    } else {
	msg.buffer = dynamic_buffer(state).buff;
	msg.length = dynamic_buffer(state).index;
	msg.dyn_alloc = TRUE;
	return msg;
    }
}
 
/* Description: Executes an sql query. Returns number of rows in the result
   set. */
static db_result_msg db_select_count(byte *sql, db_state *state)
{
    SQLSMALLINT num_of_columns, intresult;
    SQLLEN num_of_rows;
    SQLRETURN result;
    diagnos diagnos;
    db_result_msg msg;
    int index;

    if (associated_result_set(state)) {
	clean_state(state);
    }
    associated_result_set(state) = TRUE;
  
    if(!sql_success(SQLAllocHandle(SQL_HANDLE_STMT,
				   connection_handle(state),
				   &statement_handle(state))))
	DO_EXIT(EXIT_ALLOC);

    if(use_srollable_cursors(state)) {
	/* This function will fail if the driver does not support scrollable
	   cursors, this is expected and will not cause any damage*/
	SQLSetStmtAttr(statement_handle(state),
		       (SQLINTEGER)SQL_ATTR_CURSOR_SCROLLABLE,
		       (SQLPOINTER)SQL_SCROLLABLE, (SQLINTEGER)0);
    }
    
    if(!sql_success(SQLExecDirect(statement_handle(state), sql, SQL_NTS))) {
	diagnos = get_diagnos(SQL_HANDLE_STMT, statement_handle(state));
	clean_state(state);
	return encode_error_message(diagnos.error_msg);
    }
  
    if(!sql_success(SQLNumResultCols(statement_handle(state),
				     &num_of_columns)))
	DO_EXIT(EXIT_COLS);

    nr_of_columns(state) = (int)num_of_columns;
    columns(state) = alloc_column_buffer(nr_of_columns(state));
  
    if(!sql_success(SQLRowCount(statement_handle(state), &num_of_rows)))
	DO_EXIT(EXIT_ROWS);
  
    return encode_row_count(num_of_rows, state);
}

/* Description: Fetches rows from the result set associated with the
   connection by db_select_count. The method of seletion will be according
   too <args> */
static db_result_msg db_select(byte *args, db_state *state)
{
    db_result_msg msg;
    SQLSMALLINT num_of_columns;
    int offset, n, orientation;
    byte erlOrientation;

    erlOrientation = args[0];

    switch(erlOrientation) {
    case SELECT_FIRST:
	orientation = SQL_FETCH_FIRST;
	offset = DUMMY_OFFSET;
	n = 1;
	break;
    case SELECT_LAST:
	orientation = SQL_FETCH_LAST;
	offset = DUMMY_OFFSET;
	n = 1;
	break;
    case SELECT_NEXT:
	orientation = SQL_FETCH_NEXT;
	offset = DUMMY_OFFSET;
	n = 1;
	break;
    case SELECT_PREV:
	orientation = SQL_FETCH_PRIOR;
	offset = DUMMY_OFFSET;
	n = 1;
	break;
    case SELECT_ABSOLUTE:
	orientation = SQL_FETCH_ABSOLUTE;
	offset = atoi(strtok((char *)(args + sizeof(byte)), ";"));
	n =  atoi(strtok(NULL, ";"));
	break;
    case SELECT_RELATIVE:
	orientation = SQL_FETCH_RELATIVE;
	offset = atoi(strtok((char *)(args + sizeof(byte)), ";"));
	n =  atoi(strtok(NULL, ";"));
	break;
    case SELECT_N_NEXT:
	orientation = SQL_FETCH_NEXT;
	offset = atoi(strtok((char *)(args + sizeof(byte)), ";"));
	n =  atoi(strtok(NULL, ";"));
    }

    msg = encode_empty_message();
    
    ei_x_new(&dynamic_buffer(state));
    ei_x_new_with_version(&dynamic_buffer(state));
    ei_x_encode_tuple_header(&dynamic_buffer(state), 3);
    ei_x_encode_atom(&dynamic_buffer(state), "selected");

    num_of_columns = nr_of_columns(state);
    msg = encode_column_name_list(num_of_columns, state);
    if (msg.length == 0) { /* If no error has occurred */   
	msg = encode_value_list_scroll(num_of_columns,
				       (SQLSMALLINT)orientation,
				       (SQLINTEGER)offset,
				       n, state);
    }
  
    if (msg.length != 0) { /* An error has occurred */
	ei_x_free(&(dynamic_buffer(state))); 
	return msg;
    } else {
	msg.buffer = dynamic_buffer(state).buff;
  	msg.length = dynamic_buffer(state).index; 
	msg.dyn_alloc = TRUE;
	return msg;
    }
}

/* Description: Handles parameterized queries ex:
   INSERT INTO FOO VALUES(?, ?) */
static db_result_msg db_param_query(byte *buffer, db_state *state)
{
    byte *sql; 
    db_result_msg msg; 
    int i, num_param_values, ver = 0,
       erl_type = 0, index = 0, size = 0, cols = 0;
    long long_num_param_values;  
    param_status param_status;
    diagnos diagnos;
    param_array *params;
    SQLRETURN result;

    if (associated_result_set(state)) {
	clean_state(state);
    }
    associated_result_set(state) = FALSE;
    param_query(state) = TRUE;

    msg = encode_empty_message();

    ei_decode_version(buffer, &index, &ver);
    
    ei_decode_tuple_header(buffer, &index, &size);

    ei_get_type(buffer, &index, &erl_type, &size);

    sql = (byte*)safe_malloc((sizeof(byte) * (size + 1)));
    ei_decode_string(buffer, &index, sql); 

    ei_decode_long(buffer, &index, &long_num_param_values);

    num_param_values = (int)long_num_param_values;
    ei_decode_list_header(buffer, &index, &cols);

    
    init_param_statement(cols, num_param_values, state, &param_status);

    params = bind_parameter_arrays(buffer, &index, cols,  
  				   num_param_values, state);  
    
    if(params != NULL) {

	result = SQLExecDirect(statement_handle(state), sql, SQL_NTS);
	if (!sql_success(result) || result == SQL_NO_DATA) {
		diagnos = get_diagnos(SQL_HANDLE_STMT, statement_handle(state));
	}
	/* SQL_NO_DATA and SQLSTATE 00000 indicate success for
	   updates/deletes that affect no rows */
	if(!sql_success(result) &&
	   !(result == SQL_NO_DATA && !strcmp((char *)diagnos.sqlState, INFO))) {
		msg = encode_error_message(diagnos.error_msg);
	} else {
	    for (i = 0; i < param_status.params_processed; i++) {
		switch (param_status.param_status_array[i]) {
		case SQL_PARAM_SUCCESS:
		case SQL_PARAM_SUCCESS_WITH_INFO:
			/* SQL_PARAM_DIAG_UNAVAILABLE is entered when the
			 * driver treats arrays of parameters as a monolithic
			 * unit, so it does not generate this individual
			 * parameter level of error information. */	
		case SQL_PARAM_DIAG_UNAVAILABLE:
		break;
		default:
		    diagnos =
			get_diagnos(SQL_HANDLE_STMT, statement_handle(state));
		    msg = encode_error_message(diagnos.error_msg);
		    i = param_status.params_processed;
		    break;
		}
	    }
	    if(msg.length == 0) {
		ei_x_new_with_version(&dynamic_buffer(state));
                if(out_params(state)){
                    msg = encode_out_params(state, cols, params, num_param_values);
                }else{
                    msg = encode_result(state);
                }
		if(msg.length == 0) {
		    msg.buffer = dynamic_buffer(state).buff;
  		    msg.length = dynamic_buffer(state).index; 
		    msg.dyn_alloc = TRUE;
		} else { /* Error occurred */
		    ei_x_free(&(dynamic_buffer(state)));
		}
	    }
	}
    
	if(!sql_success(SQLFreeStmt(statement_handle(state),
				    SQL_RESET_PARAMS))) {
	    DO_EXIT(EXIT_FREE);
	} 
    } else {
	msg = encode_atom_message("param_badarg");
    }
    
    free(sql); 

    free_params(&params, cols);
    
    free(param_status.param_status_array);     

    if(!sql_success(SQLFreeHandle(SQL_HANDLE_STMT,
				  statement_handle(state)))){
	DO_EXIT(EXIT_FREE);
    }
    statement_handle(state) = NULL;
    param_query(state) = FALSE;
    return msg;
}


static db_result_msg db_describe_table(byte *sql, db_state *state)
{
    db_result_msg msg; 
    SQLSMALLINT num_of_columns;
    SQLCHAR name[MAX_NAME];
    SQLSMALLINT name_len, sql_type, dec_digits, nullable;
    SQLLEN size;
    diagnos diagnos;
    int i;
    
    if (associated_result_set(state)) {
	clean_state(state);
    }
    associated_result_set(state) = FALSE;

    msg = encode_empty_message();
    
    if(!sql_success(SQLAllocHandle(SQL_HANDLE_STMT,
				   connection_handle(state),
				   &statement_handle(state))))
	DO_EXIT(EXIT_ALLOC);
    
    if (!sql_success(SQLPrepare(statement_handle(state), sql, SQL_NTS))){
	diagnos =  get_diagnos(SQL_HANDLE_STMT, statement_handle(state));
	msg = encode_error_message(diagnos.error_msg);
	clean_state(state);
	return msg;
    }
    
    if(!sql_success(SQLNumResultCols(statement_handle(state),
				     &num_of_columns))) {
	diagnos =  get_diagnos(SQL_HANDLE_STMT, statement_handle(state));
	msg = encode_error_message(diagnos.error_msg);
	clean_state(state);
	return msg;
    }
    
    ei_x_new_with_version(&dynamic_buffer(state));
    ei_x_encode_tuple_header(&dynamic_buffer(state), 2);
    ei_x_encode_atom(&dynamic_buffer(state), "ok");
    ei_x_encode_list_header(&dynamic_buffer(state), num_of_columns);
    
    for (i = 0; i < num_of_columns; ++i) {
	
	if(!sql_success(SQLDescribeCol(statement_handle(state),
				       (SQLUSMALLINT)(i+1),
				       name, sizeof(name), &name_len,
				       &sql_type, &size, &dec_digits,
				       &nullable)))
	    DO_EXIT(EXIT_DESC);

	ei_x_encode_tuple_header(&dynamic_buffer(state), 2);
	ei_x_encode_string_len(&dynamic_buffer(state),
			       (char *)name, name_len);
	encode_data_type(sql_type, size, dec_digits, state);
    }

    ei_x_encode_empty_list(&dynamic_buffer(state));
    
    clean_state(state);
    msg.buffer = dynamic_buffer(state).buff;
    msg.length = dynamic_buffer(state).index; 
    msg.dyn_alloc = TRUE;
    return msg;
}


/* ----------------- Encode/decode functions -----------------------------*/

static db_result_msg encode_empty_message(void)
{
    db_result_msg msg;

    msg.length = 0;
    msg.buffer = NULL;
    msg.dyn_alloc = FALSE;

    return msg;
}

/* Description: Encode an error-message to send back to erlang*/
static db_result_msg encode_error_message(char *reason)
{
    int index;
    db_result_msg msg;
  
    index = 0;
    ei_encode_version(NULL, &index);
    ei_encode_tuple_header(NULL, &index, 2);
    ei_encode_atom(NULL, &index, "error");
    ei_encode_string(NULL, &index, reason);
  
    msg.length = index;
    msg.buffer = (byte *)safe_malloc(index);
    msg.dyn_alloc = FALSE;
  
    index = 0;
    ei_encode_version((char *)msg.buffer, &index);
    ei_encode_tuple_header((char *)msg.buffer, &index, 2);
    ei_encode_atom((char *)msg.buffer, &index, "error");
    ei_encode_string((char *)msg.buffer, &index, reason);
  
    return msg;
}

/* Description: Encode a messge that is a erlang atom */
static db_result_msg encode_atom_message(char* atom)
{
    int index;
    db_result_msg msg;
  
    index = 0;
    ei_encode_version(NULL, &index);
    ei_encode_atom(NULL, &index, atom);

    msg.length = index;
    msg.buffer = (byte *)safe_malloc(index);
    msg.dyn_alloc = FALSE;
  
    index = 0;
    ei_encode_version((char *)msg.buffer, &index);
    ei_encode_atom((char *)msg.buffer, &index, atom);
  
    return msg;
}


/* Top encode function for db_query that encodes the resulting erlang
   term to be returned to the erlang client. */
static db_result_msg encode_result(db_state *state)
{
    SQLSMALLINT num_of_columns = 0;
    SQLLEN RowCountPtr = 0;
    SQLINTEGER paramBatch = 0;
    db_result_msg msg;
    int elements, update, num_of_rows = 0;
    char *atom;

    msg = encode_empty_message();
    
    if(!sql_success(SQLNumResultCols(statement_handle(state), 
  				     &num_of_columns))) { 
  	DO_EXIT(EXIT_COLS); 
    } 
    
    if (num_of_columns == 0) { 
        elements = 2;
        atom = "updated";
        update = TRUE;
    } else {
	elements = 3;
	atom = "selected";
	update = FALSE;
    }
    
    if(!sql_success(SQLRowCount(statement_handle(state), &RowCountPtr))) { 
	DO_EXIT(EXIT_ROWS); 
    }

    if(param_query(state) && update) {
	if(!sql_success(SQLGetInfo(connection_handle(state),
				   SQL_PARAM_ARRAY_ROW_COUNTS,
				   (SQLPOINTER)&paramBatch,
				   sizeof(paramBatch),
				   NULL))) {
	    DO_EXIT(EXIT_DRIVER_INFO);
	}
	
	if(paramBatch == SQL_PARC_BATCH ) {
	    /* Individual row counts (one for each parameter set)
	       are available, sum them up */
	    do {
		num_of_rows = num_of_rows + (int)RowCountPtr;
		msg = more_result_sets(state);
		/* We don't want to continue if an error occured */
		if (msg.length != 0) { 
		    return msg;
		}
		if(exists_more_result_sets(state)) {
		    if(!sql_success(SQLRowCount(statement_handle(state),
						&RowCountPtr))) { 
			DO_EXIT(EXIT_ROWS); 
		    }
		}
	    } while (exists_more_result_sets(state));
	} else {
	    /* Row counts are rolled up into one (SQL_PARC_NO_BATCH) */
	    num_of_rows = (int)RowCountPtr;
	}    
    } else { 
	num_of_rows = (int)RowCountPtr;
    }
    ei_x_encode_tuple_header(&dynamic_buffer(state), elements);
    ei_x_encode_atom(&dynamic_buffer(state), atom);
    if (update) {
	if(num_of_rows < 0 ) {
	    ei_x_encode_atom(&dynamic_buffer(state), "undefined");
	} else {
	    ei_x_encode_long(&dynamic_buffer(state), num_of_rows);
	}
    } else {
	msg = encode_result_set(num_of_columns, state);
    }
    return msg;
}
 
static db_result_msg encode_out_params(db_state *state,
                                       int num_of_params,
                                       param_array* params,
                                       int num_param_values)
{
    int num_of_columns = 0;
    int i = 0;
    int j = 0;
    param_array column;
    db_result_msg msg;
    TIMESTAMP_STRUCT* ts;
    msg = encode_empty_message();
    
    ei_x_encode_tuple_header(&dynamic_buffer(state), 3);
    ei_x_encode_atom(&dynamic_buffer(state), "executed");

    num_of_columns = num_out_params(num_of_params, params);
    ei_x_encode_long(&dynamic_buffer(state), num_of_columns);

    ei_x_encode_list_header(&dynamic_buffer(state), num_param_values);
    for(j =0; j < num_param_values; j ++){
    
        if(tuple_row(state)) {
            ei_x_encode_tuple_header(&dynamic_buffer(state), num_of_columns);

        } else {
            ei_x_encode_list_header(&dynamic_buffer(state), num_of_columns);
        }
    
        for (i = 0; i< num_of_params; i++) {
            if(params[i].input_output_type==SQL_PARAM_INPUT){
                continue;
            }
            column = params[i];
            if (column.type.len == 0 ||
                column.type.strlen_or_indptr == SQL_NULL_DATA) {
                ei_x_encode_atom(&dynamic_buffer(state), "null");
            } else {
                void* values = retrive_param_values(&column);
                switch(column.type.c) {
                case SQL_C_TYPE_TIMESTAMP:
                  ts = (TIMESTAMP_STRUCT*) values;
                  ei_x_encode_tuple_header(&dynamic_buffer(state), 2);
                  ei_x_encode_tuple_header(&dynamic_buffer(state), 3);
                  ei_x_encode_long(&dynamic_buffer(state), (long)(ts->year));
                  ei_x_encode_long(&dynamic_buffer(state), (long)(ts->month));
                  ei_x_encode_long(&dynamic_buffer(state), (long)(ts->day));
                  ei_x_encode_tuple_header(&dynamic_buffer(state), 3);
                  ei_x_encode_long(&dynamic_buffer(state), (long)(ts->hour));
                  ei_x_encode_long(&dynamic_buffer(state), (long)(ts->minute));
                  ei_x_encode_long(&dynamic_buffer(state), (long)(ts->second));
                  break;
                case SQL_C_CHAR:
			if binary_strings(state) {
				ei_x_encode_binary(&dynamic_buffer(state),
						   ((char*)values)+j*column.type.len,
						   (column.type.strlen_or_indptr_array[j]));
			}
			else {
				ei_x_encode_string(&dynamic_buffer(state),
						   ((char*)values)+j*column.type.len);
			}
			break;
                case SQL_C_WCHAR:
			ei_x_encode_binary(&dynamic_buffer(state),
					   ((char*)values)+j*column.type.len,
					   (column.type.strlen_or_indptr_array[j]));
			break;
                case SQL_C_SLONG:
                    ei_x_encode_long(&dynamic_buffer(state), ((long*)values)[j]);
                    break;
                case SQL_C_DOUBLE:
                    ei_x_encode_double(&dynamic_buffer(state),
                                       ((double*)values)[j]);
                    break;
                case SQL_C_BIT:
                    ei_x_encode_atom(&dynamic_buffer(state),
                                     ((Boolean*)values)[j]==TRUE?"true":"false");
                    break;
                default:
                    ei_x_encode_atom(&dynamic_buffer(state), "error");
                    break;
                }
            } 
        } 
        if(!tuple_row(state)) {
            ei_x_encode_empty_list(&dynamic_buffer(state));
        }
    }
    ei_x_encode_empty_list(&dynamic_buffer(state));
    return msg;
}  

static int num_out_params(int num_of_params, param_array* params)
{
    int ret = 0;
    int i = 0;
    for(i=0; i < num_of_params; i++){
        if(params[i].input_output_type==SQL_PARAM_INPUT_OUTPUT ||
           params[i].input_output_type==SQL_PARAM_OUTPUT)
            ret++;
    }
    return ret;
}

/* Description: Encodes the result set into the "ei_x" - dynamic_buffer
   held by the state variable */
static db_result_msg encode_result_set(SQLSMALLINT num_of_columns,
				       db_state *state)
{
    db_result_msg msg;

    columns(state) = alloc_column_buffer(num_of_columns);
  
    msg = encode_column_name_list(num_of_columns, state);
    if (msg.length == 0) { /* If no error has occurred */   
	msg = encode_value_list(num_of_columns, state);
    }

    free_column_buffer(&(columns(state)), num_of_columns);
    
    return msg;
}  

/* Description: Encodes the list of column names into the "ei_x" -
   dynamic_buffer held by the state variable */
static db_result_msg encode_column_name_list(SQLSMALLINT num_of_columns,
					     db_state *state)
{
    int i;
    db_result_msg msg;
    SQLCHAR name[MAX_NAME];
    SQLSMALLINT name_len, sql_type, dec_digits, nullable;
    SQLLEN size; 
    SQLRETURN result;

    msg = encode_empty_message();
    
    ei_x_encode_list_header(&dynamic_buffer(state), num_of_columns);
  
    for (i = 0; i < num_of_columns; ++i) {

	if(!sql_success(SQLDescribeCol(statement_handle(state),
				       (SQLSMALLINT)(i+1),
				       name, sizeof(name), &name_len,
				       &sql_type, &size, &dec_digits,
				       &nullable)))
	    DO_EXIT(EXIT_DESC);

	if(sql_type == SQL_LONGVARCHAR || sql_type == SQL_LONGVARBINARY)
	    size = MAXCOLSIZE;
    
	(columns(state)[i]).type.decimal_digits = dec_digits;
	(columns(state)[i]).type.sql = sql_type;
	(columns(state)[i]).type.col_size = size;
      
	msg = map_sql_2_c_column(&columns(state)[i]);
	if (msg.length > 0) {
	    return msg; /* An error has occurred */
	} else {
	    if (columns(state)[i].type.len > 0) {
		columns(state)[i].buffer =
		    (char *)safe_malloc(columns(state)[i].type.len);
	
		if (columns(state)[i].type.c == SQL_C_BINARY) {
		    /* retrived later by retrive_binary_data */
		}else {
		    if(!sql_success(
			SQLBindCol
			(statement_handle(state),
			 (SQLSMALLINT)(i+1),
			 columns(state)[i].type.c,
			 columns(state)[i].buffer,
			 columns(state)[i].type.len,
			 &columns(state)[i].type.strlen_or_indptr)))
			DO_EXIT(EXIT_BIND);
		}
		ei_x_encode_string_len(&dynamic_buffer(state),
				       name, name_len);
	    }
	    else {
		columns(state)[i].type.len = 0;
		columns(state)[i].buffer = NULL;
	    }
	}  
    }
    ei_x_encode_empty_list(&dynamic_buffer(state)); 

    return msg;
}

/* Description: Encodes the list(s) of row values fetched by SQLFetch into
   the "ei_x" - dynamic_buffer held by the state variable */
static db_result_msg encode_value_list(SQLSMALLINT num_of_columns,
				       db_state *state)
{
    int i, msg_len;
    SQLRETURN result;
    db_result_msg list_result;
    db_result_msg msg;

    msg = encode_empty_message();
        
    for (;;) {
	/* fetch the next row */
	result = SQLFetch(statement_handle(state)); 
    
	if (result == SQL_NO_DATA) /* Reached end of result set */
	{
	    break;
	}

	ei_x_encode_list_header(&dynamic_buffer(state), 1);

	if(tuple_row(state)) {
	    ei_x_encode_tuple_header(&dynamic_buffer(state),
				     num_of_columns);
	} else {
	    ei_x_encode_list_header(&dynamic_buffer(state), num_of_columns);
	}
    
	for (i = 0; i < num_of_columns; i++) {
	    encode_column_dyn(columns(state)[i], i, state);
	}

	if(!tuple_row(state)) {
	    ei_x_encode_empty_list(&dynamic_buffer(state));
	}
    } 
    ei_x_encode_empty_list(&dynamic_buffer(state));
    return msg;
}

/* Description: Encodes the list(s) of row values fetched with
   SQLFetchScroll into the "ei_x" - dynamic_buffer held by the state
   variable */
static db_result_msg encode_value_list_scroll(SQLSMALLINT num_of_columns,
					      SQLSMALLINT Orientation,
					      SQLINTEGER OffSet, int N,
					      db_state *state)
{
    int i, j,  msg_len;
    SQLRETURN result;
    db_result_msg list_result;
    db_result_msg msg;

    msg = encode_empty_message();
    
    for (j = 0; j < N; j++) {
	if((j > 0) && (Orientation == SQL_FETCH_ABSOLUTE)) {
	    OffSet++;
	}
    
	if((j == 1) && (Orientation == SQL_FETCH_RELATIVE)) {
	    OffSet = 1;
	}

	result = SQLFetchScroll(statement_handle(state), Orientation,
				OffSet); 
    
	if (result == SQL_NO_DATA) /* Reached end of result set */
	{
	    break;
	}
	ei_x_encode_list_header(&dynamic_buffer(state), 1);

	if(tuple_row(state)) {
	    ei_x_encode_tuple_header(&dynamic_buffer(state),
				     num_of_columns);
	} else {
	    ei_x_encode_list_header(&dynamic_buffer(state), num_of_columns);
	}
	for (i = 0; i < num_of_columns; i++) {
	    encode_column_dyn(columns(state)[i], i, state);
	}
	if(!tuple_row(state)) {
	    ei_x_encode_empty_list(&dynamic_buffer(state));
	}
    } 
    ei_x_encode_empty_list(&dynamic_buffer(state));
    return msg;
}

/* Encodes row count result for erlang  */
static db_result_msg encode_row_count(SQLINTEGER num_of_rows,
				      db_state *state)
{
    db_result_msg msg;
    int index;
  
    index = 0;
    ei_encode_version(NULL, &index);
    ei_encode_tuple_header(NULL, &index, 2);
    ei_encode_atom(NULL, &index, "ok");
    if(num_of_rows == -1)
    {
	ei_encode_atom(NULL, &index, "undefined");
    } else {
	ei_encode_long(NULL, &index, num_of_rows);
    } 
    msg.length = index;
    msg.buffer = (byte *)safe_malloc(index);
    msg.dyn_alloc = FALSE;
  
    index = 0;
    ei_encode_version((char *)msg.buffer, &index);
    ei_encode_tuple_header((char *)msg.buffer, &index, 2);
    ei_encode_atom((char *)msg.buffer, &index, "ok");
  
    if(num_of_rows == -1)
    {
	ei_encode_atom((char *)msg.buffer, &index, "undefined");
    } else {
	ei_encode_long((char *)msg.buffer, &index, num_of_rows);
    }
    return msg;
}
 
/* Description: Encodes the a column value into the "ei_x" - dynamic_buffer
   held by the state variable */
static void encode_column_dyn(db_column column, int column_nr,
			      db_state *state)
{
    TIMESTAMP_STRUCT* ts;
    if (column.type.len == 0 ||
	column.type.strlen_or_indptr == SQL_NULL_DATA) {
	ei_x_encode_atom(&dynamic_buffer(state), "null");
    } else {
	switch(column.type.c) {
	case SQL_C_TYPE_TIMESTAMP:
            ts = (TIMESTAMP_STRUCT*)column.buffer;
            ei_x_encode_tuple_header(&dynamic_buffer(state), 2);
            ei_x_encode_tuple_header(&dynamic_buffer(state), 3);
            ei_x_encode_ulong(&dynamic_buffer(state), ts->year);
            ei_x_encode_ulong(&dynamic_buffer(state), ts->month);
            ei_x_encode_ulong(&dynamic_buffer(state), ts->day);
            ei_x_encode_tuple_header(&dynamic_buffer(state), 3);
            ei_x_encode_ulong(&dynamic_buffer(state), ts->hour);
            ei_x_encode_ulong(&dynamic_buffer(state), ts->minute);
            ei_x_encode_ulong(&dynamic_buffer(state), ts->second);
            break;
	case SQL_C_CHAR:
		if binary_strings(state) {
			 ei_x_encode_binary(&dynamic_buffer(state), 
					    column.buffer,column.type.strlen_or_indptr);
		} else {
			ei_x_encode_string(&dynamic_buffer(state), column.buffer);
		}
	    break;
	case SQL_C_WCHAR:
            ei_x_encode_binary(&dynamic_buffer(state), 
                               column.buffer,column.type.strlen_or_indptr);
	    break;
	case SQL_C_SLONG:
	    ei_x_encode_long(&dynamic_buffer(state),
	    	*(SQLINTEGER*)column.buffer);
	    break;
	case SQL_C_DOUBLE:
	    ei_x_encode_double(&dynamic_buffer(state),
			       *(double*)column.buffer);
	    break;
	case SQL_C_BIT:
	    ei_x_encode_atom(&dynamic_buffer(state),
			     column.buffer[0]?"true":"false");
	    break;
	case SQL_C_BINARY:		
	    column = retrive_binary_data(column, column_nr, state);
	    if binary_strings(state) {
		    ei_x_encode_binary(&dynamic_buffer(state), 
				       column.buffer,column.type.strlen_or_indptr);
	    } else {
		    ei_x_encode_string(&dynamic_buffer(state), (void *)column.buffer);
	    }
	    break;
	default:
	    ei_x_encode_atom(&dynamic_buffer(state), "error");
	    break;
	}
    } 
}

static void encode_data_type(SQLINTEGER sql_type, SQLINTEGER size,
			     SQLSMALLINT decimal_digits, db_state *state)
{
    switch(sql_type) {
    case SQL_CHAR:
	ei_x_encode_tuple_header(&dynamic_buffer(state), 2);	
	ei_x_encode_atom(&dynamic_buffer(state), "sql_char");
	ei_x_encode_long(&dynamic_buffer(state), size);
	break;
    case SQL_VARCHAR:
	ei_x_encode_tuple_header(&dynamic_buffer(state), 2);	
	ei_x_encode_atom(&dynamic_buffer(state), "sql_varchar");
	ei_x_encode_long(&dynamic_buffer(state), size);
	break;
    case SQL_WCHAR:
	ei_x_encode_tuple_header(&dynamic_buffer(state), 2);	
	ei_x_encode_atom(&dynamic_buffer(state), "sql_wchar");
	ei_x_encode_long(&dynamic_buffer(state), size);
	break;
    case SQL_WVARCHAR:
	ei_x_encode_tuple_header(&dynamic_buffer(state), 2);	
	ei_x_encode_atom(&dynamic_buffer(state), "sql_wvarchar");
	ei_x_encode_long(&dynamic_buffer(state), size);
	break;
    case SQL_NUMERIC:
	ei_x_encode_tuple_header(&dynamic_buffer(state), 3);	
	ei_x_encode_atom(&dynamic_buffer(state), "sql_numeric");
	ei_x_encode_long(&dynamic_buffer(state), size);
	ei_x_encode_long(&dynamic_buffer(state), decimal_digits);
	break;
    case SQL_DECIMAL:
	ei_x_encode_tuple_header(&dynamic_buffer(state), 3);	
	ei_x_encode_atom(&dynamic_buffer(state), "sql_decimal");
	ei_x_encode_long(&dynamic_buffer(state), size);
	ei_x_encode_long(&dynamic_buffer(state), decimal_digits);
	break;
    case SQL_INTEGER:
	ei_x_encode_atom(&dynamic_buffer(state), "sql_integer");
	break;
    case SQL_TINYINT:
	ei_x_encode_atom(&dynamic_buffer(state), "sql_tinyint");
	break;
    case SQL_SMALLINT:
	ei_x_encode_atom(&dynamic_buffer(state), "sql_smallint");
	break;
    case SQL_REAL:
	ei_x_encode_atom(&dynamic_buffer(state), "sql_real");
	break;
    case SQL_FLOAT:
	ei_x_encode_tuple_header(&dynamic_buffer(state), 2);	
	ei_x_encode_atom(&dynamic_buffer(state), "sql_float");
	ei_x_encode_long(&dynamic_buffer(state), size);
	break;
    case SQL_DOUBLE:
	ei_x_encode_atom(&dynamic_buffer(state), "sql_double");
	break;
    case SQL_BIT:
	ei_x_encode_atom(&dynamic_buffer(state), "sql_bit");
	break;
    case SQL_TYPE_DATE:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_TYPE_DATE");
	break;
    case SQL_TYPE_TIME:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_TYPE_TIME");
	break;
    case SQL_TYPE_TIMESTAMP:
	ei_x_encode_atom(&dynamic_buffer(state), "sql_timestamp");
	break;
    case SQL_BIGINT:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_BIGINT");
	break;
    case SQL_BINARY:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_BINARY");
	break;
    case SQL_LONGVARCHAR:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_LONGVARCHAR");
	break;
    case SQL_VARBINARY:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_VARBINARY");
	break;
    case SQL_LONGVARBINARY:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_LONGVARBINARY");
	break;
    case SQL_INTERVAL_MONTH:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_INTERVAL_MONTH");
	break;
    case SQL_INTERVAL_YEAR:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_INTERVAL_YEAR");
	break;
    case SQL_INTERVAL_DAY:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_INTERVAL_DAY");
	break;
    case SQL_INTERVAL_MINUTE:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_INTERVAL_MINUTE");
	break;
    case SQL_INTERVAL_HOUR_TO_SECOND:
	ei_x_encode_atom(&dynamic_buffer(state),
			 "SQL_INTERVAL_HOUR_TO_SECOND");
	break;
    case SQL_INTERVAL_MINUTE_TO_SECOND:
	ei_x_encode_atom(&dynamic_buffer(state),
			 "SQL_INTERVAL_MINUTE_TO_SECOND");
	break;
    case SQL_UNKNOWN_TYPE:
	ei_x_encode_atom(&dynamic_buffer(state), "SQL_UNKNOWN_TYPE");
	break;
    default: /* Will probably never happen */
	ei_x_encode_atom(&dynamic_buffer(state), "ODBC_UNSUPPORTED_TYPE");
	break;
    }
}

static Boolean decode_params(db_state *state, byte *buffer, int *index, param_array **params,
			  int i, int j)
{
    int erl_type, size;
    long bin_size, l64;
    long val;
    param_array* param;
    TIMESTAMP_STRUCT* ts;
    
    ei_get_type(buffer, index, &erl_type, &size);
    param = &(*params)[i];

    switch (param->type.c) {
    case SQL_C_CHAR:
	    if (binary_strings(state)) {
		    ei_decode_binary(buffer, index,
				     &(param->values.string[param->offset]), &bin_size);
		    param->offset += param->type.len;
		    param->type.strlen_or_indptr_array[j] = SQL_NTS;
	    } else {
		    if(erl_type != ERL_STRING_EXT) {
			    return FALSE;
		    }
		    ei_decode_string(buffer, index, &(param->values.string[param->offset]));
		    param->offset += param->type.len;
		    param->type.strlen_or_indptr_array[j] = SQL_NTS;
	    }
	    break;
    case SQL_C_WCHAR:
	    ei_decode_binary(buffer, index, &(param->values.string[param->offset]), &bin_size);
	    param->offset += param->type.len;
	    param->type.strlen_or_indptr_array[j] = SQL_NTS;
	    break;
    case SQL_C_TYPE_TIMESTAMP:
	    ts = (TIMESTAMP_STRUCT*) param->values.string;
	    ei_decode_tuple_header(buffer, index, &size);
	    ei_decode_long(buffer, index, &val);
	    ts[j].year = (SQLUSMALLINT)val;
	    ei_decode_long(buffer, index, &val);
	    ts[j].month = (SQLUSMALLINT)val;
	    ei_decode_long(buffer, index, &val);
	    ts[j].day = (SQLUSMALLINT)val;
	    ei_decode_long(buffer, index, &val);
	    ts[j].hour = (SQLUSMALLINT)val;
	    ei_decode_long(buffer, index, &val);
	    ts[j].minute = (SQLUSMALLINT)val;
	    ei_decode_long(buffer, index, &val);
	    ts[j].second = (SQLUSMALLINT)val;
	    ts[j].fraction = (SQLINTEGER)0;
	    break;
    case SQL_C_SLONG:
	    if(!((erl_type == ERL_SMALL_INTEGER_EXT) ||
		 (erl_type == ERL_INTEGER_EXT) ||
		 (erl_type == ERL_SMALL_BIG_EXT) ||
		 (erl_type == ERL_LARGE_BIG_EXT))) {
		    return FALSE;
	    }
	    
	    if(ei_decode_long(buffer, index, &l64)) {
		    return FALSE;
	    }
	    
	    /* For 64-bit platforms we downcast 8-byte long
	     * to 4-byte SQLINTEGER, checking for overflow */
	    
	    if(l64>INT_MAX || l64<INT_MIN) {
		    return FALSE;
	    }

	param->values.integer[j]=(SQLINTEGER)l64;
	break;
	
    case SQL_C_DOUBLE: 
	    if((erl_type != ERL_FLOAT_EXT)) { 
		    return FALSE;
	    } 
	    ei_decode_double(buffer, index, &(param->values.floating[j])); 
	    break;
	    
    case SQL_C_BIT:
	if((erl_type != ERL_ATOM_EXT)) {
		return FALSE;
	}
	ei_decode_boolean(buffer, index, &(param->values.bool[j]));
	break;
	
    default:
	    return FALSE;
    }
    
    return TRUE;
}  

/*------------- Erlang port communication functions ----------------------*/

/* read from stdin */ 
#ifdef WIN32
static int read_exact(byte *buffer, int len)
{
    HANDLE standard_input = GetStdHandle(STD_INPUT_HANDLE);
    
    unsigned read_result;
    unsigned sofar = 0;
    
    if (!len) { /* Happens for "empty packages */
	return 0;
    }
    for (;;) {
	if (!ReadFile(standard_input, buffer + sofar,
		      len - sofar, &read_result, NULL)) {
	    return -1; /* EOF */
	}
	if (!read_result) {
	    return -2; /* Interrupted while reading? */
	}
	sofar += read_result;
	if (sofar == len) {
	    return len;
	}
    }
} 
#elif defined(UNIX)
static int read_exact(byte *buffer, int len) {
    int i, got = 0;
    
    do {
	if ((i = read(0, buffer + got, len - got)) <= 0)
	    return(i);
	got += i;
    } while (got < len);
    return len;
   
}
#endif


/* Recieive (read) data from erlang on stdin */
static byte * receive_erlang_port_msg(void)
{
    int i, len = 0;
    byte *buffer;
    byte lengthstr[LENGTH_INDICATOR_SIZE];

    if(read_exact(lengthstr, LENGTH_INDICATOR_SIZE) !=
       LENGTH_INDICATOR_SIZE)
    {
	DO_EXIT(EXIT_STDIN_HEADER);
    }
    for(i=0; i < LENGTH_INDICATOR_SIZE; i++) {
	len <<= 8;
	len |= lengthstr[i];
    }
    
    buffer = (byte *)safe_malloc(len);
    
    if (read_exact(buffer, len) <= 0) {
	DO_EXIT(EXIT_STDIN_BODY);
    }

    return buffer;
}
 
/* ------------- Socket communication functions --------------------------*/

#if defined(WIN32)
static SOCKET connect_to_erlang(const char *port)
#elif defined(UNIX)
static int connect_to_erlang(const char *port)
#endif
{
#if defined(WIN32)
	SOCKET sock;
#elif defined(UNIX)
	int sock;
#endif
	struct sockaddr_in sin;

#if defined(AF_INET6)
	struct sockaddr_in6 sin6;

	sock = socket(AF_INET6, SOCK_STREAM, 0);

	memset(&sin6, 0, sizeof(sin6));
	sin6.sin6_port = htons ((unsigned short)atoi(port));
	sin6.sin6_family = AF_INET6;
	sin6.sin6_addr = in6addr_loopback;
    
	if (connect(sock, (struct sockaddr*)&sin6, sizeof(sin6)) == 0) {
		return sock;
	}
	close_socket(sock);
#endif
	sock = socket(AF_INET, SOCK_STREAM, 0);

	memset(&sin, 0, sizeof(sin));
	sin.sin_port = htons ((unsigned short)atoi(port));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	
	if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
		close_socket(sock);
		DO_EXIT(EXIT_SOCKET_CONNECT);
	}
	return sock;
}

#ifdef WIN32
static void close_socket(SOCKET socket)
{
    closesocket(socket);
}
#elif defined(UNIX)
static void close_socket(int socket)
{
    close(socket);
}
#endif

#ifdef WIN32
static byte * receive_msg(SOCKET socket) 
#elif defined(UNIX)
static byte * receive_msg(int socket) 
#endif
{
    byte lengthstr[LENGTH_INDICATOR_SIZE];
    size_t msg_len = 0;
    int i;
    byte *buffer = NULL;
    
    if(!receive_msg_part(socket, lengthstr, LENGTH_INDICATOR_SIZE)) {
	close_socket(socket);
	DO_EXIT(EXIT_SOCKET_RECV_HEADER);
    }
    
    for(i = 0; i < LENGTH_INDICATOR_SIZE; i++) {
	msg_len <<= 8;
	msg_len |= lengthstr[i];
    }
    
    buffer = (byte *)safe_malloc(msg_len);

    if(!receive_msg_part(socket, buffer, msg_len)) {
	close_socket(socket);
	DO_EXIT(EXIT_SOCKET_RECV_BODY);
    }

    return buffer;
}

#ifdef WIN32
static Boolean receive_msg_part(SOCKET socket, byte * buffer, size_t msg_len)
#elif defined(UNIX)  
static Boolean receive_msg_part(int socket, byte * buffer, size_t msg_len)
#endif
{
    int nr_bytes_received = 0;
    
    nr_bytes_received = recv(socket, (void *)buffer, msg_len, 0);
    
    if(nr_bytes_received == msg_len) {
	return TRUE; 
    } else if(nr_bytes_received > 0 && nr_bytes_received < msg_len) {
	return receive_msg_part(socket, buffer + nr_bytes_received,
				msg_len - nr_bytes_received); 
    } else if(nr_bytes_received == -1) { 
	return FALSE; 
    } else {  /* nr_bytes_received > msg_len */
	close_socket(socket); 
	DO_EXIT(EXIT_SOCKET_RECV_MSGSIZE); 
    }
}

#ifdef WIN32
static void send_msg(db_result_msg *msg, SOCKET socket)
#elif defined(UNIX)   
static void send_msg(db_result_msg *msg, int socket)
#endif
{
    byte lengthstr[LENGTH_INDICATOR_SIZE];
    int len;
    len = msg ->length;
    
    lengthstr[0] = (len >> 24) & 0x000000FF;
    lengthstr[1] = (len >> 16) & 0x000000FF;
    lengthstr[2] = (len >> 8) & 0x000000FF;
    lengthstr[3] = len &  0x000000FF;

    if(!send_msg_part(socket, lengthstr, LENGTH_INDICATOR_SIZE)) {
	close_socket(socket);
	DO_EXIT(EXIT_SOCKET_SEND_HEADER);
    }
    
    if(!send_msg_part(socket, msg->buffer, len)) {
	close_socket(socket);
	DO_EXIT(EXIT_SOCKET_SEND_BODY);
    }
}

#ifdef WIN32
static Boolean send_msg_part(SOCKET socket, byte * buffer, size_t msg_len)
#elif defined(UNIX)  
static Boolean send_msg_part(int socket, byte * buffer, size_t msg_len)
#endif
{
    int nr_bytes_sent = 0;
    
    nr_bytes_sent = send(socket, (void *)buffer, msg_len, 0);
    
    if(nr_bytes_sent == msg_len) {
	return TRUE; 
    } else if(nr_bytes_sent > 0 && nr_bytes_sent < msg_len) {
	return send_msg_part(socket, buffer + nr_bytes_sent,
				msg_len - nr_bytes_sent); 
    } else if(nr_bytes_sent == -1) { 
	return FALSE; 
    } else {  /* nr_bytes_sent > msg_len */
	close_socket(socket); 
	DO_EXIT(EXIT_SOCKET_SEND_MSGSIZE); 
    }
}

#ifdef WIN32
static void init_winsock(void)
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
    
    wVersionRequested = MAKEWORD( 2, 0 );
 
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
	DO_EXIT(EXIT_OLD_WINSOCK);
    }

    if ( LOBYTE( wsaData.wVersion ) != 2 ||
	 HIBYTE( wsaData.wVersion ) != 0 ) {
	clean_socket_lib();
	DO_EXIT(EXIT_OLD_WINSOCK);
    }
}
#endif

static void clean_socket_lib(void)
{
#ifdef WIN32
    WSACleanup();
#endif
}
    

/*------------- Memmory handling funtions -------------------------------*/
static void *safe_malloc(int size)
{
    void *memory;
  
    memory = (void *)malloc(size);
    if (memory == NULL) 
	DO_EXIT(EXIT_ALLOC);

    return memory;
}

static void *safe_realloc(void *ptr, int size)
{
    void *memory;

    memory = (void *)realloc(ptr, size);

    if (memory == NULL)
    {
	free(ptr);
	DO_EXIT(EXIT_ALLOC);
    }
    return memory;
}
  
/* Description: Allocate memory for n columns */
static db_column * alloc_column_buffer(int n)
{
    int i;
    db_column *columns;
  
    columns = (db_column *)safe_malloc(n * sizeof(db_column));
    for(i = 0; i < n; i++)
	columns[i].buffer = NULL;
  
    return columns;
}
 
/* Description: Deallocate memory allocated by alloc_column_buffer */
static void free_column_buffer(db_column **columns, int n)
{
    int i;
    if(*columns != NULL) {
	for (i = 0; i < n; i++) {
	    if((*columns)[i].buffer != NULL) {
		free((*columns)[i].buffer);
	    }
	}
	free(*columns);
	*columns = NULL;
    }
}

static void free_params(param_array **params, int cols)
{
    int i;
    if(*params != NULL) {
	for (i = 0; i < cols; i++) {
	    if((*params)[i].type.strlen_or_indptr_array != NULL){
		free((*params)[i].type.strlen_or_indptr_array);
	    }    
	    free(retrive_param_values(&((*params)[i])));
	} 
	free(*params);
	*params = NULL;
    }
}   

/* Description: Frees resources associated with the current statement handle
   keeped in the state.*/
static void clean_state(db_state *state)
{
    if(statement_handle(state) != NULL) {
	if(!sql_success(SQLFreeHandle(SQL_HANDLE_STMT,
				      statement_handle(state)))) {
	    DO_EXIT(EXIT_FREE);
	}
	statement_handle(state) = NULL;
    }
    free_column_buffer(&(columns(state)), nr_of_columns(state));
    columns(state) = NULL;
    nr_of_columns(state) = 0;
}
 
/* ------------- Init/map/bind/retrive functions  ------------------------*/

/* Prepare the state for a connection */
static void init_driver(int erl_auto_commit_mode, int erl_trace_driver,
			db_state *state)
{
  
    int auto_commit_mode, trace_driver;
  
    if(erl_auto_commit_mode == ON) {
	auto_commit_mode = SQL_AUTOCOMMIT_ON;
    } else {
	auto_commit_mode = SQL_AUTOCOMMIT_OFF;
    }

    if(erl_trace_driver == ON) {
	trace_driver = SQL_OPT_TRACE_ON;
    } else {
	trace_driver = SQL_OPT_TRACE_OFF;
    }
  
    if(!sql_success(SQLAllocHandle(SQL_HANDLE_ENV,
				   SQL_NULL_HANDLE,
				   &environment_handle(state))))
	DO_EXIT(EXIT_ALLOC);
    if(!sql_success(SQLSetEnvAttr(environment_handle(state),
				  SQL_ATTR_ODBC_VERSION,
				  (SQLPOINTER)SQL_OV_ODBC3, 0)))
	DO_EXIT(EXIT_ENV);
    if(!sql_success(SQLAllocHandle(SQL_HANDLE_DBC,
				   environment_handle(state),
				   &connection_handle(state))))
	DO_EXIT(EXIT_ALLOC);
    /* By default Erlang handles all timeouts */
    if(!sql_success(SQLSetConnectAttr(connection_handle(state),
				      SQL_ATTR_CONNECTION_TIMEOUT,
				      (SQLPOINTER)TIME_OUT, 0)))
	    DO_EXIT(EXIT_CONNECTION);
    if(!sql_success(SQLSetConnectAttr(connection_handle(state),
				      SQL_ATTR_AUTOCOMMIT,
				      (SQLPOINTER)auto_commit_mode, 0)))
	    DO_EXIT(EXIT_CONNECTION);
    if(!sql_success(SQLSetConnectAttr(connection_handle(state),
				      SQL_ATTR_TRACE,
				      (SQLPOINTER)trace_driver, 0)))
	    DO_EXIT(EXIT_CONNECTION);
}

static void init_param_column(param_array *params, byte *buffer, int *index,
			      int num_param_values, db_state* state)
{
    int size, erl_type;
    long user_type, precision, scale, length, dummy;
    long in_or_out;
    
    ei_decode_long(buffer, index, &user_type);

    params->type.strlen_or_indptr = (SQLINTEGER)NULL;
    params->type.strlen_or_indptr_array = NULL;
    params->type.decimal_digits = (SQLINTEGER)0;
  
    switch (user_type) {
    case USER_SMALL_INT:
	params->type.sql = SQL_SMALLINT;
	params->type.c = SQL_C_SLONG;
	params->type.len = sizeof(SQLINTEGER);
	params->type.col_size = COL_SQL_SMALLINT;
	params->values.integer =
	    (SQLINTEGER*)safe_malloc(num_param_values * params->type.len);
	break;
    case USER_INT:
	params->type.sql = SQL_INTEGER;
	params->type.c = SQL_C_SLONG;
	params->type.len = sizeof(SQLINTEGER);
	params->type.col_size = COL_SQL_INTEGER;
	params->values.integer =
	    (SQLINTEGER*)safe_malloc(num_param_values * params->type.len);
	break;
    case USER_TINY_INT:
	params->type.sql = SQL_TINYINT;
	params->type.c = SQL_C_SLONG;
	params->type.len = sizeof(SQLINTEGER);
	params->type.col_size = COL_SQL_TINYINT;
	params->values.integer =
	    (SQLINTEGER*)safe_malloc(num_param_values * params->type.len);
	break;
    case USER_DECIMAL:
    case USER_NMERIC:
	if(user_type == USER_NMERIC) {
	   params->type.sql = SQL_NUMERIC;
	} else {
	    params->type.sql = SQL_DECIMAL;
	}
	ei_decode_long(buffer, index, &precision);
	ei_decode_long(buffer, index, &scale);
	map_dec_num_2_c_column(&params->type, (int)precision, (int)scale);
	if( params->type.c == SQL_C_SLONG) {
	    params->values.integer =
		(SQLINTEGER *)safe_malloc(num_param_values * params->type.len);
	} else if( params->type.c == SQL_C_DOUBLE) {
	    params->values.floating =
		(double *)safe_malloc(num_param_values * params->type.len);
	} else if(params->type.c == SQL_C_CHAR) {
	    params->type.strlen_or_indptr_array
		= (SQLLEN*)safe_malloc(num_param_values * sizeof(SQLINTEGER));
	    params->values.string =
		(byte *)safe_malloc(num_param_values *
				    sizeof(byte)* params->type.len);
	}    
	break;
    case USER_CHAR:
    case USER_VARCHAR:
	if(user_type == USER_CHAR) {
	     params->type.sql = SQL_CHAR;
	} else {
	     params->type.sql = SQL_VARCHAR;
	}
	ei_decode_long(buffer, index, &length);
	/* Max string length + string terminator */
	 params->type.len = length+1;
	 params->type.c = SQL_C_CHAR;
	 params->type.col_size = (SQLUINTEGER)length;
	 params->type.strlen_or_indptr_array =
	     (SQLLEN*)safe_malloc(num_param_values * sizeof(SQLINTEGER));
	 params->values.string =
	    (byte *)safe_malloc(num_param_values *
				sizeof(byte)* params->type.len);
	
	break;
    case USER_WCHAR:
    case USER_WVARCHAR:
        if(user_type == USER_WCHAR) {
            params->type.sql = SQL_WCHAR;
	} else {
            params->type.sql = SQL_WVARCHAR;
	}
	ei_decode_long(buffer, index, &length);
	/* Max string length + string terminator */
	params->type.len = (length+1)*sizeof(SQLWCHAR);
        params->type.c = SQL_C_WCHAR;
        params->type.col_size = (SQLUINTEGER)length;
        params->type.strlen_or_indptr_array =
          (SQLLEN*)safe_malloc(num_param_values * sizeof(SQLINTEGER));
        params->values.string =
          (byte *)safe_malloc(num_param_values * sizeof(byte) * params->type.len);
	
	break;
    case USER_TIMESTAMP:
      params->type.sql = SQL_TYPE_TIMESTAMP;
      params->type.len = sizeof(TIMESTAMP_STRUCT);
      params->type.c = SQL_C_TYPE_TIMESTAMP;
      params->type.col_size = (SQLUINTEGER)COL_SQL_TIMESTAMP;
      params->values.string =
        (byte *)safe_malloc(num_param_values * params->type.len);
      break;
    case USER_FLOAT:
	params->type.sql = SQL_FLOAT;
	params->type.c = SQL_C_DOUBLE;
	params->type.len = sizeof(double);
	ei_decode_long(buffer, index, &length);
	params->type.col_size = (SQLUINTEGER)length;
	params->values.floating =
	    (double *)safe_malloc(num_param_values * params->type.len);
	break;
    case USER_REAL:
	params->type.sql = SQL_REAL;
	params->type.c = SQL_C_DOUBLE;
	params->type.len = sizeof(double);
	params->type.col_size = COL_SQL_REAL;
	params->values.floating =
	    (double *)safe_malloc(num_param_values * params->type.len);
	break;
    case USER_DOUBLE:
	params->type.sql = SQL_DOUBLE;
	params->type.c = SQL_C_DOUBLE;
	params->type.len = sizeof(double);
	params->type.col_size = COL_SQL_DOUBLE;
	params->values.floating =
	    (double *)safe_malloc(num_param_values * params->type.len);
	break;
    case USER_BOOLEAN:
	params->type.sql = SQL_BIT;
	params->type.c = SQL_C_BIT;
	params->type.len = sizeof(Boolean);
	params->type.col_size = params->type.len;
	params->values.bool =
	    (Boolean *)safe_malloc(num_param_values * params->type.len);
	break;
    }
    params->offset = 0;

    ei_decode_long(buffer, index, &in_or_out);
    switch((in_or_out_type)in_or_out){
    case(ERL_ODBC_OUT):
        out_params(state) = TRUE;
        params->input_output_type = SQL_PARAM_OUTPUT; break;
    case(ERL_ODBC_INOUT):
        out_params(state) = TRUE;
        params->input_output_type = SQL_PARAM_INPUT_OUTPUT; break;
    case(ERL_ODBC_IN):
    default:
        params->input_output_type = SQL_PARAM_INPUT; break;
    }

}

static void init_param_statement(int cols, int num_param_values, 
				 db_state *state, param_status *status)
{
    int i;

    status -> param_status_array =
	(SQLUSMALLINT *)safe_malloc(num_param_values * sizeof(SQLUSMALLINT));

    for(i=0; i<num_param_values; i++) {
    	status -> param_status_array[i] = SQL_PARAM_PROCEED;
    }

    status -> params_processed = 0;
    
    if(!sql_success(SQLAllocHandle(SQL_HANDLE_STMT,
				   connection_handle(state),
				   &statement_handle(state)))) {
	DO_EXIT(EXIT_ALLOC);
    }
    
    if(num_param_values <= 1) return;

    if(!sql_success(SQLSetStmtAttr(statement_handle(state),
				   SQL_ATTR_PARAM_BIND_TYPE,
				   SQL_PARAM_BIND_BY_COLUMN, 0))) {
	DO_EXIT(EXIT_PARAM_ARRAY);
    }

    /* Note the (int *) cast is correct as the API function SQLSetStmtAttr
       takes either an interger or a pointer depending on the attribute */
    if(!sql_success(SQLSetStmtAttr(statement_handle(state),
				   SQL_ATTR_PARAMSET_SIZE,
				   (int *)num_param_values,
				   0))) {
	DO_EXIT(EXIT_PARAM_ARRAY);
    }
    
    if(!sql_success(SQLSetStmtAttr(statement_handle(state),
				   SQL_ATTR_PARAM_STATUS_PTR,
				   (status -> param_status_array), 0))) {
	DO_EXIT(EXIT_PARAM_ARRAY);
    }
    
    if(!sql_success(SQLSetStmtAttr(statement_handle(state),
				   SQL_ATTR_PARAMS_PROCESSED_PTR,
				   &(status -> params_processed), 0))) {
	DO_EXIT(EXIT_PARAM_ARRAY);
    }
}

static void map_dec_num_2_c_column(col_type *type, int precision, int scale)
{
    type -> col_size = (SQLINTEGER)precision;
    type -> decimal_digits = (SQLSMALLINT)scale;

    if(precision >= 0 && precision <= 4 && scale == 0) {
	type->len = sizeof(SQLINTEGER);
	type->c = SQL_C_SLONG;
    } else if(precision >= 5 && precision <= 9 && scale == 0) {
	type->len = sizeof(SQLINTEGER);
	type->c = SQL_C_SLONG;
    }  else if((precision >= 10 && precision <= 15 && scale == 0)
	       || (precision <= 15 && scale > 0)) {
	type->len = sizeof(double);
	type->c = SQL_C_DOUBLE;
    } else {
	type->len = DEC_NUM_LENGTH;
	type->c = SQL_C_CHAR;
    }
}

/* Description: Transform SQL columntype to C columntype. Returns a dummy
 db_result_msg with length 0 on success and an errormessage otherwise.*/
static db_result_msg map_sql_2_c_column(db_column* column)
{
    db_result_msg msg;

    msg = encode_empty_message();
        
    switch(column -> type.sql) {
    case SQL_CHAR:
    case SQL_VARCHAR:
    case SQL_BINARY:
    case SQL_LONGVARCHAR:
    case SQL_VARBINARY:
    case SQL_LONGVARBINARY:
        column -> type.len = (column -> type.col_size) +
            /* Make place for NULL termination */
            sizeof(byte);
        column -> type.c = SQL_C_CHAR;
        column -> type.strlen_or_indptr = SQL_NTS;
        break;
    case SQL_WCHAR:
    case SQL_WVARCHAR:
        column -> type.len = (column -> type.col_size + 1)*sizeof(SQLWCHAR); 
        column -> type.c = SQL_C_WCHAR;
        column -> type.strlen_or_indptr = SQL_NTS;
	break;
    case SQL_NUMERIC:
    case SQL_DECIMAL:
	map_dec_num_2_c_column(&(column -> type), column -> type.col_size,
			       column -> type.decimal_digits);
	column -> type.strlen_or_indptr = (SQLINTEGER)NULL;
	break;
    case SQL_TINYINT:
    case SQL_INTEGER:
    case SQL_SMALLINT:
	column -> type.len = sizeof(SQLINTEGER);
	column -> type.c = SQL_C_SLONG;
	column -> type.strlen_or_indptr = (SQLINTEGER)NULL;
	break;
    case SQL_REAL:
    case SQL_FLOAT:
    case SQL_DOUBLE:
	column -> type.len = sizeof(double);
	column -> type.c = SQL_C_DOUBLE;
	column -> type.strlen_or_indptr = (SQLINTEGER)NULL;
	break;
    case SQL_TYPE_DATE:
    case SQL_TYPE_TIME:
	column -> type.len = (column -> type.col_size) +
	    sizeof(byte);
	column -> type.c = SQL_C_CHAR;
	column -> type.strlen_or_indptr = SQL_NTS;
	break;
    case SQL_TYPE_TIMESTAMP:
      column -> type.len = sizeof(TIMESTAMP_STRUCT);
      column -> type.c = SQL_C_TYPE_TIMESTAMP;
      column -> type.strlen_or_indptr = (SQLINTEGER)NULL;
      break;
    case SQL_BIGINT:
	column -> type.len = DEC_NUM_LENGTH;
	column -> type.c = SQL_C_CHAR;
	column -> type.strlen_or_indptr = (SQLINTEGER)NULL;
	break;
    case SQL_BIT:
	column -> type.len = sizeof(byte);
	column -> type.c = SQL_C_BIT;
	column -> type.strlen_or_indptr = (SQLINTEGER)NULL;
	break;
    case SQL_UNKNOWN_TYPE:
	msg = encode_error_message("Unknown column type");
	break;
    default:
	msg = encode_error_message("Column type not supported");
	break;
    }
    return msg;
}

static param_array * bind_parameter_arrays(byte *buffer, int *index,
					   int cols, int num_param_values,
					   db_state *state)
{
    int i, j, k, size, erl_type;
    db_result_msg msg;
    long dummy;
    void *Values;
    param_array *params;
    
    params = (param_array *)safe_malloc(cols * sizeof(param_array)); 
    
    for (i = 0; i < cols; i++) {
    
	ei_get_type(buffer, index, &erl_type, &size);

	if(erl_type == ERL_NIL_EXT) {
	    /* End of previous list of column values when i > 0 */
	    ei_decode_list_header(buffer, index, &size);
	}

	ei_decode_tuple_header(buffer, index, &size);

	init_param_column(&params[i], buffer, index, num_param_values, state);

	ei_decode_list_header(buffer, index, &size);

	if(params[i].type.c == SQL_C_SLONG) {
         /* Get rid of the dummy value 256 that is added as the first value
	    of all integer parameter value lists. This is to avoid that the
	    list will be encoded as a string if all values are less
	    than 256 */
	    ei_decode_long(buffer, index, &dummy); 
	}
  
	for (j = 0; j < num_param_values; j++) {
	    if(!decode_params(state, buffer, index, &params, i, j)) {
		/* An input parameter was not of the expected type */  
		free_params(&params, i);
		return params;
	    }
	}

	Values = retrive_param_values(&params[i]); 

	if(!sql_success(
	    SQLBindParameter(statement_handle(state), i + 1,
			     params[i].input_output_type,
			     params[i].type.c,
			     params[i].type.sql,
			     params[i].type.col_size,
			     params[i].type.decimal_digits, Values,
			     params[i].type.len,
			     params[i].type.strlen_or_indptr_array))) {
	    DO_EXIT(EXIT_BIND);
	}
    }

    return params;
}
 
static void * retrive_param_values(param_array *Param)
{
    switch(Param->type.c) {
    case SQL_C_CHAR:
    case SQL_C_WCHAR:
    case SQL_C_TYPE_TIMESTAMP:
        return (void *)Param->values.string;
    case SQL_C_SLONG:
	return (void *)Param->values.integer;
    case SQL_C_DOUBLE: 
	return (void *)Param->values.floating;
    case SQL_C_BIT:
	return (void *)Param->values.bool;
    default:
	DO_EXIT(EXIT_FAILURE); /* Should not happen */
    }
}

/* Description: More than one call to SQLGetData may be required to retrieve
   data from a single column with  binary data. SQLGetData then returns
   SQL_SUCCESS_WITH_INFO nd the SQLSTATE will have the value 01004 (Data
   truncated). The application can then use the same column number to
   retrieve subsequent parts of the data until SQLGetData returns
   SQL_SUCCESS, indicating that all data for the column has been retrieved.
*/

static db_column retrive_binary_data(db_column column, int column_nr,
				     db_state *state)
{ 
    char *outputptr;
    char *sqlState;
    int blocklen, outputlen, result;
    diagnos diagnos;
  
    blocklen = column.type.len;
    outputptr = column.buffer;
    result = SQLGetData(statement_handle(state), (SQLSMALLINT)(column_nr+1),
			SQL_C_CHAR, outputptr,
			blocklen, &column.type.strlen_or_indptr);

    while (result == SQL_SUCCESS_WITH_INFO) {

	diagnos = get_diagnos(SQL_HANDLE_STMT, statement_handle(state));
    
	if(strcmp((char *)diagnos.sqlState, TRUNCATED) == 0) {
	    outputlen = column.type.len - 1;
	    column.type.len = outputlen + blocklen;
	    column.buffer =
		safe_realloc((void *)column.buffer, column.type.len);
	    outputptr = column.buffer + outputlen;
	    result = SQLGetData(statement_handle(state),
				(SQLSMALLINT)(column_nr+1), SQL_C_CHAR,
				outputptr, blocklen,
				&column.type.strlen_or_indptr);
	}
    }
  
    if (result == SQL_SUCCESS) {
	return column;
    } else {
	DO_EXIT(EXIT_BIN); 
    }
}

/* Description: Returns information about support for scrollable cursors */ 
static db_result_msg retrive_scrollable_cursor_support_info(db_state *state)
{
    db_result_msg msg;
    SQLUINTEGER supportMask;
  
    ei_x_new_with_version(&dynamic_buffer(state));
    ei_x_encode_tuple_header(&dynamic_buffer(state), 3);
    ei_x_encode_atom(&dynamic_buffer(state), "ok");

    if(use_srollable_cursors(state)) {
    
	if(!sql_success(SQLGetInfo(connection_handle(state),
				   SQL_DYNAMIC_CURSOR_ATTRIBUTES1,
				   (SQLPOINTER)&supportMask,
				   sizeof(supportMask),
				   NULL))) {
	    DO_EXIT(EXIT_DRIVER_INFO);
	}
    
	if ((supportMask & SQL_CA1_ABSOLUTE)) {
	    ei_x_encode_atom(&dynamic_buffer(state), "true");
	}
	else {
	    ei_x_encode_atom(&dynamic_buffer(state), "false");    
	}
    
	if ((supportMask & SQL_CA1_RELATIVE)) {
	    ei_x_encode_atom(&dynamic_buffer(state), "true");    
	}
	else {
	    ei_x_encode_atom(&dynamic_buffer(state), "false");
	}
    } else { /* Scrollable cursors disabled by the user */
	ei_x_encode_atom(&dynamic_buffer(state), "false");  
	ei_x_encode_atom(&dynamic_buffer(state), "false");
    } 
    msg.buffer = dynamic_buffer(state).buff;
    msg.length = dynamic_buffer(state).index; 
    msg.dyn_alloc = TRUE;
    return msg;
}
 
/* ------------- Boolean functions ---------------------------------------*/

/* Check if there are any more result sets */
static db_result_msg more_result_sets(db_state *state)
{
    SQLRETURN result;
    diagnos diagnos;
    db_result_msg msg;

    msg = encode_empty_message();
    result = SQLMoreResults(statement_handle(state));
  
    if(sql_success(result)){
	exists_more_result_sets(state) = TRUE;
	return msg;
    } else if(result == SQL_NO_DATA) {
	exists_more_result_sets(state) = FALSE;
	return msg;
    } else {
	/* As we found an error we do not care about any potential more result
	   sets */
	exists_more_result_sets(state) = FALSE;
	diagnos = get_diagnos(SQL_HANDLE_STMT, statement_handle(state));
	strcat((char *)diagnos.error_msg,
	       "Failed to create on of the result sets");
	msg = encode_error_message(diagnos.error_msg);
	return msg;
    }
}
 
static Boolean sql_success(SQLRETURN result)
{
    return result == SQL_SUCCESS || result == SQL_SUCCESS_WITH_INFO;
}

/* ------------- Error handling functions --------------------------------*/

/* Description: An ODBC function can post zero or more diagnostic records
   each time it is called. This function loops through the current set of
   diagnostic records scaning for error messages and the sqlstate.
   If this function is called when no error has ocurred only the sqlState
   field may be referenced.*/
static diagnos get_diagnos(SQLSMALLINT handleType, SQLHANDLE handle)
{
    diagnos diagnos;
    SQLINTEGER nativeError;
    SQLSMALLINT errmsg_buffer_size, record_nr, errmsg_size;
    int acc_errmsg_size;
    byte *current_errmsg_pos;
    SQLCHAR current_sql_state[SQL_STATE_SIZE];
    SQLRETURN result;

    diagnos.error_msg[0] = 0;
    
    current_errmsg_pos = (byte *)diagnos.error_msg;

    /* number bytes free in error message buffer */
    errmsg_buffer_size = MAX_ERR_MSG - ERRMSG_HEADR_SIZE;  
    acc_errmsg_size = 0; /* number bytes used in the error message buffer */

    /* Foreach diagnostic record in the current set of diagnostic records
       the error message is obtained */
    for(record_nr = 1; ;record_nr++) {    
        result = SQLGetDiagRec(handleType, handle, record_nr, current_sql_state,
			 &nativeError, current_errmsg_pos,
							   (SQLSMALLINT)errmsg_buffer_size, &errmsg_size);
	if(result != SQL_SUCCESS && result != SQL_NO_DATA) {

      
	    break;
	} else {
	    /* update the sqlstate in the diagnos record, because the SQLGetDiagRec
	       call succeeded */
	    memcpy(diagnos.sqlState, current_sql_state, SQL_STATE_SIZE);
	    errmsg_buffer_size = errmsg_buffer_size - errmsg_size;
	    acc_errmsg_size = acc_errmsg_size + errmsg_size;
	    current_errmsg_pos = current_errmsg_pos + errmsg_size;
	}
    }
    
    if(acc_errmsg_size == 0) {
	strcat((char *)diagnos.error_msg,
	       "No SQL-driver information available.");
    }
    else {
	strcat(strcat((char *)diagnos.error_msg, " SQLSTATE IS: "),
	       (char *)diagnos.sqlState);
    }
    return diagnos;
}

static void str_tolower(char *str, int len)
{
	int i;
	
	for(i = 0; i <= len; i++) {
		str[i] = tolower(str[i]);
	}
}