File: dbi_result.c

package info (click to toggle)
libdbi 0.9.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,280 kB
  • sloc: sh: 10,708; ansic: 4,007; makefile: 116
file content (1801 lines) | stat: -rw-r--r-- 57,505 bytes parent folder | download | duplicates (3)
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
/*
 * libdbi - database independent abstraction layer for C.
 * Copyright (C) 2001-2003, David Parker and Mark Tobenkin.
 * http://libdbi.sourceforge.net
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * $Id: dbi_result.c,v 1.57 2013/01/08 23:54:30 mhoenicka Exp $
 *
 * (anything that has to do with row seeking or fetching fields goes in this file)
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
  #include <unistd.h>
#endif
#include <math.h>
#include <limits.h>
#include <time.h>

#include <dbi/dbi.h>
#include <dbi/dbi-dev.h>

#ifdef __MINGW32__
#warning "Using the thread-unsafe strtok function!"
#define strtok_r(s1,s2,s3) strtok(s1,s2)
#endif

// cast the opaque parameter to our struct pointer
#define RESULT ((dbi_result_t*)Result)

/* declarations for internal functions -- anything declared as static won't be accessible by name from client programs */
static _field_binding_t *_find_or_create_binding_node(dbi_result_t *result, const char *fieldname);
static void _remove_binding_node(dbi_result_t *result, _field_binding_t *deadbinding);
static unsigned int _find_field(dbi_result_t *result, const char *fieldname, dbi_error_flag* errflag);
static int _is_row_fetched(dbi_result_t *result, unsigned long long row);
static int _setup_binding(dbi_result_t *result, const char *fieldname, void *bindto, void *helperfunc);
static void _activate_bindings(dbi_result_t *result);
static unsigned int _parse_field_formatstr(const char *format, char ***tokens_dest, char ***fieldnames_dest);
static void _free_string_list(char **ptrs, int total);
static void _free_result_rows(dbi_result_t *result);
int _disjoin_from_conn(dbi_result_t *result);

static void _bind_helper_char(_field_binding_t *binding);
static void _bind_helper_uchar(_field_binding_t *binding);
static void _bind_helper_short(_field_binding_t *binding);
static void _bind_helper_ushort(_field_binding_t *binding);
static void _bind_helper_int(_field_binding_t *binding);
static void _bind_helper_uint(_field_binding_t *binding);
static void _bind_helper_longlong(_field_binding_t *binding);
static void _bind_helper_ulonglong(_field_binding_t *binding);
static void _bind_helper_float(_field_binding_t *binding);
static void _bind_helper_double(_field_binding_t *binding);
static void _bind_helper_string(_field_binding_t *binding);
static void _bind_helper_binary(_field_binding_t *binding);
static void _bind_helper_string_copy(_field_binding_t *binding);
static void _bind_helper_binary_copy(_field_binding_t *binding);
static void _bind_helper_datetime(_field_binding_t *binding);

/* XXX ROW SEEKING AND FETCHING XXX */

/* returns 1 if ok, 0 on error */
int dbi_result_seek_row(dbi_result Result, unsigned long long rowidx) {
  int retval;

  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return 0;
  }	

  _reset_conn_error(RESULT->conn);

  if (RESULT->result_state == NOTHING_RETURNED 
      || rowidx <= 0 || rowidx > RESULT->numrows_matched) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return 0;
  }

  if (_is_row_fetched(RESULT, rowidx) == 1) {
    /* jump right to it */
    RESULT->currowidx = rowidx;
    _activate_bindings(RESULT);
    return 1;
  }
	
  /* row is one-based for the user, but zero-based to the dbd conn */
  retval = RESULT->conn->driver->functions->goto_row(RESULT, rowidx-1, RESULT->currowidx-1);
  if (retval == -1) {
    _error_handler(RESULT->conn, DBI_ERROR_DBD);
    return 0;
  }
  retval = RESULT->conn->driver->functions->fetch_row(RESULT, rowidx-1);
  if (retval == 0) {
    _error_handler(RESULT->conn, DBI_ERROR_DBD);
    return 0;
  }

  RESULT->currowidx = rowidx;
  _activate_bindings(RESULT);
  return retval;
}

int dbi_result_first_row(dbi_result Result) {
  return dbi_result_seek_row(Result, 1);
}

int dbi_result_last_row(dbi_result Result) {
  return dbi_result_seek_row(Result, dbi_result_get_numrows(Result));
}

int dbi_result_has_prev_row(dbi_result Result) {
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return 0;
  }	

  _reset_conn_error(RESULT->conn);

  return RESULT->result_state != NOTHING_RETURNED && RESULT->currowidx > 1;
}

int dbi_result_prev_row(dbi_result Result) {
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return 0;
  }	

  _reset_conn_error(RESULT->conn);

  if (!dbi_result_has_prev_row(Result)) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return 0;
  }
  return dbi_result_seek_row(Result, RESULT->currowidx-1);
}

int dbi_result_has_next_row(dbi_result Result) {
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return 0;
  }	

  _reset_conn_error(RESULT->conn);

  return RESULT->result_state != NOTHING_RETURNED
  		 && RESULT->currowidx < dbi_result_get_numrows(Result);
}

int dbi_result_next_row(dbi_result Result) {
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return 0;
  }	

  _reset_conn_error(RESULT->conn);

  if (!dbi_result_has_next_row(Result)) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return 0;
  }
  return dbi_result_seek_row(Result, RESULT->currowidx+1);
}

unsigned long long dbi_result_get_currow(dbi_result Result) {
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return 0;
  }	

  _reset_conn_error(RESULT->conn);

  return RESULT->currowidx;
}

unsigned long long dbi_result_get_numrows(dbi_result Result) {
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_ROW_ERROR;
  }	

  _reset_conn_error(RESULT->conn);

  return RESULT->numrows_matched;
}

unsigned long long dbi_result_get_numrows_affected(dbi_result Result) {
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_ROW_ERROR;
  }	

  _reset_conn_error(RESULT->conn);

  return RESULT->numrows_affected;
}

/* returns the length of the string or binary, excluding a trailing \0 */
size_t dbi_result_get_field_length(dbi_result Result, const char *fieldname) {
  unsigned int fieldidx = 0;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_LENGTH_ERROR;
  }

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    _error_handler(RESULT->conn, errflag);
    return DBI_LENGTH_ERROR;
  }
  
  return dbi_result_get_field_length_idx(Result, fieldidx+1);
}

size_t dbi_result_get_field_length_idx(dbi_result Result, unsigned int fieldidx) {
  unsigned long long currowidx;
  
  /* user-visible indexes start at 1 */
  fieldidx--;
  
  if (!RESULT || !RESULT->rows) {
    _error_handler(RESULT ? RESULT->conn : NULL, DBI_ERROR_BADPTR);
    return DBI_LENGTH_ERROR;
  }

  _reset_conn_error(RESULT->conn);

  currowidx = RESULT->currowidx;
  if (!RESULT->rows[currowidx] || !RESULT->rows[currowidx]->field_sizes) {
    _error_handler(RESULT->conn, DBI_ERROR_BADOBJECT);
    return DBI_LENGTH_ERROR;
  }
  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return DBI_LENGTH_ERROR;
  }
  
  return RESULT->rows[currowidx]->field_sizes[fieldidx];
}

/* the "field_size" functions are merely kept to support legacy
   code. Their purpose was, er, unclear, their implementations were,
   er, broke, and the name "field_length" is more appropriate to what
   these functions do anyway. Support for the "field_size" functions
   is likely to be dropped in the next release */
size_t dbi_result_get_field_size(dbi_result Result, const char *fieldname) {
  return dbi_result_get_field_length(Result, fieldname);
}

size_t dbi_result_get_field_size_idx(dbi_result Result, unsigned int fieldidx) {
  return dbi_result_get_field_length_idx(Result, fieldidx);
}

unsigned int dbi_result_get_field_idx(dbi_result Result, const char *fieldname) {
  unsigned int fieldidx = 0;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return 0;
  }	

  _reset_conn_error(RESULT->conn);

  /* user-visible indexes start at 1 */
  fieldidx = _find_field(RESULT, fieldname, &errflag)+1;
  if (errflag != DBI_ERROR_NONE) {
    _error_handler(RESULT->conn, errflag);
    return 0;
  }
  
  return fieldidx;
}

const char *dbi_result_get_field_name(dbi_result Result, unsigned int fieldidx) {
	
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return NULL;
  }

  _reset_conn_error(RESULT->conn);

  if (fieldidx > RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return NULL;
  }
  else if (RESULT->field_names == NULL) {
    _error_handler(RESULT->conn, DBI_ERROR_BADOBJECT);
    return NULL;
  }
	
  return (const char *) RESULT->field_names[fieldidx-1];
}

unsigned int dbi_result_get_numfields(dbi_result Result) {
  if (!RESULT) return DBI_FIELD_ERROR;

  _reset_conn_error(RESULT->conn);

  return RESULT->numfields;
}

unsigned short dbi_result_get_field_type(dbi_result Result, const char *fieldname) {
  unsigned int fieldidx = 0;
  dbi_error_flag errflag = DBI_ERROR_NONE;
	
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_TYPE_ERROR;
  }

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    _error_handler(RESULT->conn, errflag);
    return DBI_TYPE_ERROR;
  }

  return dbi_result_get_field_type_idx(Result, fieldidx+1);
}

unsigned short dbi_result_get_field_type_idx(dbi_result Result, unsigned int fieldidx) {
  fieldidx--;
	
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_TYPE_ERROR;
  }

  _reset_conn_error(RESULT->conn);

  if (!RESULT->field_types) {
    _error_handler(RESULT->conn, DBI_ERROR_BADOBJECT);
    return DBI_TYPE_ERROR;
  }
  else if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return DBI_TYPE_ERROR;
  }

  return RESULT->field_types[fieldidx];
}

unsigned int dbi_result_get_field_attrib(dbi_result Result, const char *fieldname, unsigned int attribmin, unsigned int attribmax) {
  unsigned int fieldidx = 0;
  dbi_error_flag errflag = DBI_ERROR_NONE;
	
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_ATTRIBUTE_ERROR;
  }

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    _error_handler(RESULT->conn, errflag);
    return DBI_ATTRIBUTE_ERROR;
  }

  return dbi_result_get_field_attrib_idx(Result, fieldidx+1, attribmin, attribmax);
}

unsigned int dbi_result_get_field_attrib_idx(dbi_result Result, 
		unsigned int fieldidx, unsigned int attribmin, unsigned int attribmax) {
  fieldidx--;
	
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_ATTRIBUTE_ERROR;
  }

  _reset_conn_error(RESULT->conn);

  if (!RESULT->field_attribs) {
    _error_handler(RESULT->conn, DBI_ERROR_BADOBJECT);
    return DBI_ATTRIBUTE_ERROR;
  }
  else if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return DBI_ATTRIBUTE_ERROR;
  }

  return _isolate_attrib(RESULT->field_attribs[fieldidx], attribmin, attribmax);
}

unsigned int dbi_result_get_field_attribs(dbi_result Result, const char *fieldname) {
  unsigned int fieldidx = 0;
  dbi_error_flag errflag = DBI_ERROR_NONE;
	
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_ATTRIBUTE_ERROR;
  }

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    _error_handler(RESULT->conn, errflag);
    return DBI_ATTRIBUTE_ERROR;
  }

  return dbi_result_get_field_attribs_idx(Result, fieldidx+1);
}

unsigned int dbi_result_get_field_attribs_idx(dbi_result Result, unsigned int fieldidx) {
  fieldidx--;
	
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_ATTRIBUTE_ERROR;
  }

  _reset_conn_error(RESULT->conn);

  if (!RESULT->field_attribs) {
    _error_handler(RESULT->conn, DBI_ERROR_BADOBJECT);
    return DBI_ATTRIBUTE_ERROR;
  }
  else if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return DBI_ATTRIBUTE_ERROR;
  }

  return RESULT->field_attribs[fieldidx];
}

void _set_field_flag(dbi_row_t *row, unsigned int fieldidx, unsigned char flag, unsigned char value) {
  unsigned char *flags = &row->field_flags[fieldidx];
  if (value)
    *flags |= flag; // if value = 1, set the flag
  else
    *flags &= ~flag; // set that bit to 0
}

int _get_field_flag(dbi_row_t *row, unsigned int fieldidx, unsigned char flag) {
  return (row->field_flags[fieldidx] & flag) != 0;
}

int dbi_result_field_is_null(dbi_result Result, const char *fieldname) {
  unsigned int fieldidx = 0;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_FIELD_FLAG_ERROR;
  }

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    _error_handler(RESULT->conn, errflag);
    return DBI_FIELD_FLAG_ERROR;
  }

  return dbi_result_field_is_null_idx(Result, fieldidx+1);
}

int dbi_result_field_is_null_idx(dbi_result Result, unsigned int fieldidx) {
  unsigned long long currowidx;

  fieldidx--;
	
  if (!RESULT || !RESULT->rows) {
    _error_handler(RESULT ? RESULT->conn : NULL, DBI_ERROR_BADPTR);
    return DBI_FIELD_FLAG_ERROR;
  }

  _reset_conn_error(RESULT->conn);

  currowidx = RESULT->currowidx;
  if (!RESULT->rows[currowidx] || !RESULT->rows[currowidx]->field_flags) {
    _error_handler(RESULT->conn, DBI_ERROR_BADOBJECT);
    return DBI_FIELD_FLAG_ERROR;
  }
  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return DBI_FIELD_FLAG_ERROR;
  }

  return _get_field_flag(RESULT->rows[currowidx], fieldidx, DBI_VALUE_NULL);
}

int _disjoin_from_conn(dbi_result_t *result) {
  /* todo: is int enough? */
  int idx;
  int found = -1;
  int retval;

  retval = result->conn->driver->functions->free_query(result);

  for (idx = 0; idx < result->conn->results_used; idx++) {
    if (found < 0) {
      /* keep looking */
      if (result->conn->results[idx] == result) {
        found = idx;
        result->conn->results[idx] = NULL;
      }
    }
    else {
      /* already found, shift remaining elements back one */
      result->conn->results[idx-1] = result->conn->results[idx];
    }
  }
  if (found >= 0) {
    result->conn->results[result->conn->results_used-1] = NULL;
    result->conn->results_used--;
  }

  result->conn = NULL;
	
  return retval;
}

int dbi_result_disjoin(dbi_result Result) {
  return RESULT ? _disjoin_from_conn(RESULT) : -1;
}

int dbi_result_free(dbi_result Result) {
  int retval = 0;

  if (!RESULT) return -1;
	
  if (RESULT->conn) {
    retval = _disjoin_from_conn(RESULT);
  }

  _reset_conn_error(RESULT->conn);

  while (RESULT->field_bindings) {
    _remove_binding_node(RESULT, RESULT->field_bindings);
  }
	
  if (RESULT->rows) {
    _free_result_rows(RESULT);
  }
		
  if (RESULT->numfields) {
    _free_string_list(RESULT->field_names, RESULT->numfields);
    free(RESULT->field_types);
    free(RESULT->field_attribs);
  }

  if (retval == -1) {
    _error_handler(RESULT->conn, DBI_ERROR_DBD);
  }

  free(RESULT);
  return retval;
}

dbi_conn dbi_result_get_conn(dbi_result Result) {

  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return NULL;
  }

  return RESULT->conn;
}

/* RESULT: mass retrieval functions */

static unsigned int _parse_field_formatstr(const char *format, char ***tokens_dest, char ***fieldnames_dest) {
  unsigned int found = 0;
  unsigned int cur = 0;
  char **tokens = NULL;
  char **fieldnames = NULL;
  char *chunk;
  char *fieldtype;
  char *fieldname;
  char *temp1;
  char *temp2;
  char *line = strdup(format);
	
  temp1 = line;
  while (temp1 && (temp1 = strchr(temp1, '.')) != NULL) {
    temp1++;
    found++;
  }
	
  tokens = calloc(found, sizeof(char *));
  fieldnames = calloc(found, sizeof(char *));
  if (!tokens || !fieldnames) return DBI_FIELD_ERROR;
	
  chunk = strtok_r(line, " ", &temp1);
  do {
    temp2 = strchr(chunk, '.');
    if (!temp2) continue;
    fieldname = chunk;
    *temp2 = '\0';
    fieldtype = (temp2+2); /* ignore the % */
    tokens[cur] = strdup(fieldtype);
    fieldnames[cur] = strdup(fieldname);
    cur++;
  } while ((chunk = strtok_r(NULL, " ", &temp1)));

  *tokens_dest = tokens;
  *fieldnames_dest = fieldnames;
	
  free(line);
  return found;
}

static void _free_string_list(char **ptrs, int total) {
  int i;

  if (ptrs == NULL) return;
  for (i = 0; i < total; ++i)
    if (ptrs[i]) free(ptrs[i]);
  free(ptrs);
  return;
}

static void _free_result_rows(dbi_result_t *result) {
  unsigned long long rowidx = 0;
  unsigned int fieldidx = 0;

  for (rowidx = 0; rowidx <= result->numrows_matched; rowidx++) {
    if (!result->rows[rowidx]) continue;
			
    for (fieldidx = 0; fieldidx < result->numfields; fieldidx++) {
      if ((result->field_types[fieldidx] == DBI_TYPE_STRING
        || result->field_types[fieldidx] == DBI_TYPE_BINARY) 
        && result->rows[rowidx]->field_values[fieldidx].d_string)
      {
        free(result->rows[rowidx]->field_values[fieldidx].d_string);
      }
    }
		
    free(result->rows[rowidx]->field_values);
    free(result->rows[rowidx]->field_sizes);
    free(result->rows[rowidx]->field_flags);
    free(result->rows[rowidx]);
  }
	
  free(result->rows);
}

unsigned int dbi_result_get_fields(dbi_result Result, const char *format, ...) {
  char **tokens, **fieldnames;
  unsigned int curidx = 0, numtokens = 0, uflag;
  va_list ap;

  if (!RESULT) return DBI_FIELD_ERROR;

  numtokens = _parse_field_formatstr(format, &tokens, &fieldnames);

  if (numtokens == DBI_FIELD_ERROR) {
    return numtokens;
  }
	
  va_start(ap, format);
  while (curidx < numtokens) {
    uflag = strlen(tokens[curidx]) > 1 && tokens[curidx][0] == 'u';
    switch (tokens[curidx][strlen(tokens[curidx])-1]) {
    case 'c': /* char */
      if (uflag) /* unsigned */
        *va_arg(ap, unsigned char *) = dbi_result_get_uchar(Result, fieldnames[curidx]);
      else
        *va_arg(ap, char *) = dbi_result_get_char(Result, fieldnames[curidx]);
      break;
    case 'h': /* sHort ("S"tring was taken) */
      if (uflag) /* unsigned */
        *va_arg(ap, unsigned short *) = dbi_result_get_ushort(Result, fieldnames[curidx]);
      else
        *va_arg(ap, short *) = dbi_result_get_short(Result, fieldnames[curidx]);
      break;
    case 'l': /* 4-byte integer (both l and i work) */
    case 'i':
      if (uflag) /* unsigned */
        *va_arg(ap, unsigned int *) = dbi_result_get_uint(Result, fieldnames[curidx]);
      else
        *va_arg(ap, int *) = dbi_result_get_int(Result, fieldnames[curidx]);
      break;
    case 'L': /* long long */
      if (uflag) /* unsigned */
        *va_arg(ap, unsigned long long *) = dbi_result_get_ulonglong(Result, fieldnames[curidx]);
      else
        *va_arg(ap, long long *) = dbi_result_get_longlong(Result, fieldnames[curidx]);
      break;
    case 'f': /* float */
      *va_arg(ap, float *) = dbi_result_get_float(Result, fieldnames[curidx]); 
      break;
    case 'd': /* double */
      *va_arg(ap, double *) = dbi_result_get_double(Result, fieldnames[curidx]); 
      break;
    case 's': /* string */
      *va_arg(ap, const char **) = dbi_result_get_string(Result, fieldnames[curidx]); 
      break;
    case 'b': /* binary */
      *va_arg(ap, const unsigned char **) = dbi_result_get_binary(Result, fieldnames[curidx]); 
      break;
    case 'S': /* string copy */
      *va_arg(ap, char **) = dbi_result_get_string_copy(Result, fieldnames[curidx]); 
      break;
    case 'B': /* binary copy */
      *va_arg(ap, unsigned char **) = dbi_result_get_binary_copy(Result, fieldnames[curidx]); 
      break;
    case 'm': /* datetiMe (what... you have any better ideas?? */
      *va_arg(ap, time_t *) = dbi_result_get_datetime(Result, fieldnames[curidx]);
      break;
    }
    curidx++;
  }
  va_end(ap);

  _free_string_list(tokens, numtokens);
  _free_string_list(fieldnames, numtokens);
  return numtokens;
}

unsigned int dbi_result_bind_fields(dbi_result Result, const char *format, ...) {
  char **tokens, **fieldnames;
  unsigned int curidx = 0, numtokens = 0, uflag;
  va_list ap;

  if (!RESULT) return DBI_FIELD_ERROR;

  numtokens = _parse_field_formatstr(format, &tokens, &fieldnames);

  if (numtokens == DBI_FIELD_ERROR) {
    return numtokens;
  }

  va_start(ap, format);
  while (curidx < numtokens) {
    uflag = strlen(tokens[curidx]) > 1 && tokens[curidx][0] == 'u';
    switch (tokens[curidx][strlen(tokens[curidx])-1]) {
    case 'c': /* char */
      if (uflag) /* unsigned */
        dbi_result_bind_uchar(Result, fieldnames[curidx], va_arg(ap, unsigned char *));
      else
        dbi_result_bind_char(Result, fieldnames[curidx], va_arg(ap, char *));
      break;
    case 'h': /* sHort ("S"tring was taken) */
      if (uflag) /* unsigned */
        dbi_result_bind_ushort(Result, fieldnames[curidx], va_arg(ap, unsigned short *));
      else
        dbi_result_bind_short(Result, fieldnames[curidx], va_arg(ap, short *));
      break;
    case 'l': /* 4-byte integer (both l and i work) */
    case 'i':
      if (uflag) /* unsigned */
        dbi_result_bind_uint(Result, fieldnames[curidx], va_arg(ap, unsigned int *));
      else
        dbi_result_bind_int(Result, fieldnames[curidx], va_arg(ap, int *));
      break;
    case 'L': /* long long */
      if (uflag) /* unsigned */
        dbi_result_bind_ulonglong(Result, fieldnames[curidx], va_arg(ap, unsigned long long *));
      else
        dbi_result_bind_longlong(Result, fieldnames[curidx], va_arg(ap, long long *));
      break;
    case 'f': /* float */
      dbi_result_bind_float(Result, fieldnames[curidx], va_arg(ap, float *));
      break;
    case 'd': /* double */
      dbi_result_bind_double(Result, fieldnames[curidx], va_arg(ap, double *));
      break;
    case 's': /* string */
      dbi_result_bind_string(Result, fieldnames[curidx], va_arg(ap, const char **));
      break;
    case 'b': /* binary */
      dbi_result_bind_binary(Result, fieldnames[curidx], va_arg(ap, const unsigned char **));
      break;
    case 'S': /* string copy */
      dbi_result_bind_string_copy(Result, fieldnames[curidx], va_arg(ap, char **));
      break;
    case 'B': /* binary copy */
      dbi_result_bind_binary_copy(Result, fieldnames[curidx], va_arg(ap, unsigned char **));
      break;
    case 'm': /* datetiMe (what... you have any better ideas?? */
      dbi_result_bind_datetime(Result, fieldnames[curidx], va_arg(ap, time_t *));
      break;
    }
    curidx++;
  }
  va_end(ap);

  _free_string_list(tokens, numtokens);
  _free_string_list(fieldnames, numtokens);
  return numtokens;
}

/* RESULT: get_* functions */

signed char dbi_result_get_char(dbi_result Result, const char *fieldname) {
  signed char my_ERROR = 0;
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, errflag);
    return my_ERROR;
  }
  return dbi_result_get_char_idx(Result, fieldidx+1);
}

signed char dbi_result_get_char_idx(dbi_result Result, unsigned int fieldidx) {
  signed char my_ERROR = 0;

  fieldidx--;

  _reset_conn_error(RESULT->conn);

  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return my_ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_INTEGER) {
    _verbose_handler(RESULT->conn, "%s: field `%s` is not integer type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }

  switch (RESULT->field_attribs[fieldidx] & DBI_INTEGER_SIZEMASK) {
  case DBI_INTEGER_SIZE1:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_char;
  case DBI_INTEGER_SIZE2:
  case DBI_INTEGER_SIZE3:
  case DBI_INTEGER_SIZE4:
  case DBI_INTEGER_SIZE8:
    _verbose_handler(RESULT->conn, "%s: field `%s` is more than 1 byte wide\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
  default:
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }
}

short dbi_result_get_short(dbi_result Result, const char *fieldname) {
  short my_ERROR = 0;
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return my_ERROR;
  }
  return dbi_result_get_short_idx(Result, fieldidx+1);
}

short dbi_result_get_short_idx(dbi_result Result, unsigned int fieldidx) {
  short my_ERROR = 0;
  fieldidx--;

  _reset_conn_error(RESULT->conn);

  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return my_ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_INTEGER) {
    _verbose_handler(RESULT->conn, "%s: field `%s` is not integer type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }

  switch (RESULT->field_attribs[fieldidx] & DBI_INTEGER_SIZEMASK) {
  case DBI_INTEGER_SIZE1:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_char;
  case DBI_INTEGER_SIZE2:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_short;
  case DBI_INTEGER_SIZE3:
  case DBI_INTEGER_SIZE4:
  case DBI_INTEGER_SIZE8:
    _verbose_handler(RESULT->conn, "%s: field `%s` is more than 2 bytes wide\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
  default:
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }
}

int dbi_result_get_long(dbi_result Result, const char *fieldname) {
  return dbi_result_get_int(Result, fieldname);
}

int dbi_result_get_int(dbi_result Result, const char *fieldname) {
  long my_ERROR = 0;
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return my_ERROR;
  }
  return dbi_result_get_int_idx(Result, fieldidx+1);
}

int dbi_result_get_long_idx(dbi_result Result, unsigned int fieldidx) {
  return dbi_result_get_int_idx(Result, fieldidx);
}

int dbi_result_get_int_idx(dbi_result Result, unsigned int fieldidx) {
  long my_ERROR = 0;
  fieldidx--;
	
  _reset_conn_error(RESULT->conn);

  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return my_ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_INTEGER) {
    _verbose_handler(RESULT->conn, "%s: field `%s` is not integer type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }

  switch (RESULT->field_attribs[fieldidx] & DBI_INTEGER_SIZEMASK) {
  case DBI_INTEGER_SIZE1:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_char;
  case DBI_INTEGER_SIZE2:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_short;
  case DBI_INTEGER_SIZE3:
  case DBI_INTEGER_SIZE4:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_long;
  case DBI_INTEGER_SIZE8:
    _verbose_handler(RESULT->conn, "%s: field `%s` is more than 4 bytes wide\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
  default:
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }
}

long long dbi_result_get_longlong(dbi_result Result, const char *fieldname) {
  long long my_ERROR = 0;
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;
    
  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return my_ERROR;
  }
  return dbi_result_get_longlong_idx(Result, fieldidx+1);
}

long long dbi_result_get_longlong_idx(dbi_result Result, unsigned int fieldidx) {
  long long my_ERROR = 0;
  fieldidx--;
	
  _reset_conn_error(RESULT->conn);

  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return my_ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_INTEGER) {
    _verbose_handler(RESULT->conn, "%s: field `%s` is not integer type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }

  switch (RESULT->field_attribs[fieldidx] & DBI_INTEGER_SIZEMASK) {
  case DBI_INTEGER_SIZE1:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_char;
  case DBI_INTEGER_SIZE2:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_short;
  case DBI_INTEGER_SIZE3:
  case DBI_INTEGER_SIZE4:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_long;
  case DBI_INTEGER_SIZE8:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_longlong;
  default:
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }
}

unsigned char dbi_result_get_uchar(dbi_result Result, const char *fieldname) {
  return (unsigned char)dbi_result_get_char(Result, fieldname);
}

unsigned char dbi_result_get_uchar_idx(dbi_result Result, unsigned int fieldidx) {
  return (unsigned char)dbi_result_get_char_idx(Result, fieldidx);
}

unsigned short dbi_result_get_ushort(dbi_result Result, const char *fieldname) {
  return (unsigned short)dbi_result_get_short(Result, fieldname);
}

unsigned short dbi_result_get_ushort_idx(dbi_result Result, unsigned int fieldidx) {
  return (unsigned short)dbi_result_get_short_idx(Result, fieldidx);
}

unsigned int dbi_result_get_uint(dbi_result Result, const char *fieldname) {
  return (unsigned int)dbi_result_get_int(Result, fieldname);
}

unsigned int dbi_result_get_ulong(dbi_result Result, const char *fieldname) {
  return dbi_result_get_uint(Result, fieldname);
}

unsigned int dbi_result_get_uint_idx(dbi_result Result, unsigned int fieldidx) {
  return (unsigned int)dbi_result_get_int_idx(Result, fieldidx);
}

unsigned int dbi_result_get_ulong_idx(dbi_result Result, unsigned int fieldidx) {
  return dbi_result_get_uint_idx(Result, fieldidx);
}

unsigned long long dbi_result_get_ulonglong(dbi_result Result, const char *fieldname) {
  return (unsigned long long)dbi_result_get_longlong(Result, fieldname);
}

unsigned long long dbi_result_get_ulonglong_idx(dbi_result Result, unsigned int fieldidx) {
  return (unsigned long long)dbi_result_get_longlong_idx(Result, fieldidx);
}

float dbi_result_get_float(dbi_result Result, const char *fieldname) {
  float my_ERROR = 0.0;
  dbi_error_flag errflag = DBI_ERROR_NONE;
  unsigned int fieldidx;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return my_ERROR;
  }
  return dbi_result_get_float_idx(Result, fieldidx+1);
}

float dbi_result_get_float_idx(dbi_result Result, unsigned int fieldidx) {
  float my_ERROR = 0.0;
  fieldidx--;
	
  _reset_conn_error(RESULT->conn);


  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return my_ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_DECIMAL) {
    _verbose_handler(RESULT->conn, "%s: field `%s` is not float type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }

  switch (RESULT->field_attribs[fieldidx] & DBI_DECIMAL_SIZEMASK) {
  case DBI_DECIMAL_SIZE4:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_float;
  case DBI_DECIMAL_SIZE8:
    _verbose_handler(RESULT->conn, "%s: field `%s` is double, not float\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
  default:
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }
}

double dbi_result_get_double(dbi_result Result, const char *fieldname) {
  double my_ERROR = 0.0;
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return my_ERROR;
  }
  return dbi_result_get_double_idx(Result, fieldidx+1);
}
	
double dbi_result_get_double_idx(dbi_result Result, unsigned int fieldidx) {
  double my_ERROR = 0.0;
  fieldidx--;
	
  _reset_conn_error(RESULT->conn);

  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return my_ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_DECIMAL) {
    _verbose_handler(RESULT->conn, "%s: field `%s` is not double type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }

  switch (RESULT->field_attribs[fieldidx] & DBI_DECIMAL_SIZEMASK) {
  case DBI_DECIMAL_SIZE4:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_float;
  case DBI_DECIMAL_SIZE8:
    return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_double;
  default:
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }
}

const char *dbi_result_get_string(dbi_result Result, const char *fieldname) {
  const char *my_ERROR = "ERROR";
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return my_ERROR;
  }
  return dbi_result_get_string_idx(Result, fieldidx+1);
}
	
const char *dbi_result_get_string_idx(dbi_result Result, unsigned int fieldidx) {
  const char *my_ERROR = "ERROR";
  fieldidx--;
	
  _reset_conn_error(RESULT->conn);


  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return my_ERROR;
  }

  if (RESULT->field_types[fieldidx] != DBI_TYPE_STRING) {
    dbi_conn_t *conn = RESULT->conn;
    _verbose_handler(conn, "%s: field `%s` is not string type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }
  if (RESULT->rows[RESULT->currowidx]->field_sizes[fieldidx] == 0
      && _get_field_flag(RESULT->rows[RESULT->currowidx], fieldidx, DBI_VALUE_NULL)) {
    /* string does not exist */
    return NULL;
  }
  /* else if field size == 0: empty string */
	
  return (const char *)(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string);
}

const unsigned char *dbi_result_get_binary(dbi_result Result, const char *fieldname) {
  const char *my_ERROR = "ERROR";
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return (const unsigned char*)my_ERROR;
  }
  return dbi_result_get_binary_idx(Result, fieldidx+1);
}
	
const unsigned char *dbi_result_get_binary_idx(dbi_result Result, unsigned int fieldidx) {
  const char *my_ERROR = "ERROR";
  fieldidx--;
	
  _reset_conn_error(RESULT->conn);


  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return (const unsigned char*)my_ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_BINARY) {
    _verbose_handler(RESULT->conn, "%s: field `%s` is not binary type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return (const unsigned char*)my_ERROR;
  }
  if (RESULT->rows[RESULT->currowidx]->field_sizes[fieldidx] == 0) return NULL;

  return (const unsigned char *)(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string);
}

char *dbi_result_get_string_copy(dbi_result Result, const char *fieldname) {
  char *my_ERROR = "ERROR";
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return strdup(my_ERROR);
  }
  return dbi_result_get_string_copy_idx(Result, fieldidx+1);
}
	
char *dbi_result_get_string_copy_idx(dbi_result Result, unsigned int fieldidx) {
  char *my_ERROR = "ERROR";
  char *newstring = NULL;
  fieldidx--;
	
  _reset_conn_error(RESULT->conn);


  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return strdup(my_ERROR);
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_STRING) {
    _verbose_handler(RESULT->conn, "%s: field `%s` is not string type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return strdup(my_ERROR);
  }
  if (RESULT->rows[RESULT->currowidx]->field_sizes[fieldidx] == 0
   && RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string == NULL) {
    // mysql returns 0 for the field size of an empty string, so size==0
    // doesn't necessarily mean NULL
    return NULL;
  }

  newstring = strdup(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string);

  if (newstring) {
    return newstring;
  }
  else {
    _error_handler(RESULT->conn, DBI_ERROR_NOMEM);
    return strdup(my_ERROR);
  }
}

unsigned char *dbi_result_get_binary_copy(dbi_result Result, const char *fieldname) {
  char *my_ERROR = "ERROR";
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return (unsigned char *)strdup(my_ERROR);
  }
  return dbi_result_get_binary_copy_idx(Result, fieldidx+1);
}
	
unsigned char *dbi_result_get_binary_copy_idx(dbi_result Result, unsigned int fieldidx) {
  char *my_ERROR = "ERROR";
  unsigned char *newblob = NULL;
  unsigned long long size;
  fieldidx--;

  _reset_conn_error(RESULT->conn);


  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return (unsigned char *)strdup(my_ERROR);
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_BINARY) {
    _verbose_handler(RESULT->conn, "%s: field `%s` is not binary type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return (unsigned char *)strdup(my_ERROR);
  }
  if (RESULT->rows[RESULT->currowidx]->field_sizes[fieldidx] == 0) return NULL;

  /* API function must use 1-based index */
  size = dbi_result_get_field_length_idx(Result, fieldidx+1);
  newblob = malloc(size);
  if (!newblob) {
    _error_handler(RESULT->conn, DBI_ERROR_NOMEM);
    return (unsigned char *)strdup(my_ERROR);
  }
  memcpy(newblob, RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string, size);
  return newblob;
}

time_t dbi_result_get_datetime(dbi_result Result, const char *fieldname) {
  time_t my_ERROR = 0;
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return my_ERROR;
  }
  return dbi_result_get_datetime_idx(Result, fieldidx+1);
}
	
time_t dbi_result_get_datetime_idx(dbi_result Result, unsigned int fieldidx) {
  time_t my_ERROR = 0;
  fieldidx--;

  _reset_conn_error(RESULT->conn);

  if (fieldidx >= RESULT->numfields) {
    _error_handler(RESULT->conn, DBI_ERROR_BADIDX);
    return my_ERROR;
  }
  if (RESULT->field_types[fieldidx] != DBI_TYPE_DATETIME) {
    _verbose_handler(RESULT->conn, "%s: field `%s` is not datetime type\n",
                     __func__, dbi_result_get_field_name(Result, fieldidx+1));
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }
	
  return (time_t)(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_datetime);
}

/* RESULT: get_as* functions */

long long dbi_result_get_as_longlong(dbi_result Result, const char *fieldname) {
  long long my_ERROR = 0;
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;
    
  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return my_ERROR;
  }
  return dbi_result_get_as_longlong_idx(Result, fieldidx+1);
}

long long dbi_result_get_as_longlong_idx(dbi_result Result, unsigned int fieldidx) {
  long long my_ERROR = 0;
  fieldidx--;

  switch (RESULT->field_types[fieldidx]) {
  case DBI_TYPE_INTEGER:
    switch (RESULT->field_attribs[fieldidx] & DBI_INTEGER_SIZEMASK) {
    case DBI_INTEGER_SIZE1:
      return (long long)RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_char;
    case DBI_INTEGER_SIZE2:
      return (long long)RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_short;
    case DBI_INTEGER_SIZE3:
    case DBI_INTEGER_SIZE4:
      return (long long)RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_long;
    case DBI_INTEGER_SIZE8:
      return RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_longlong;
    default:
      _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
      return my_ERROR;
    }
  case DBI_TYPE_DECIMAL:
    switch (RESULT->field_attribs[fieldidx] & DBI_DECIMAL_SIZEMASK) {
    case DBI_DECIMAL_SIZE4:
      return (long long)RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_float;
    case DBI_DECIMAL_SIZE8:
      return (long long)RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_double;
    default:
      _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
      return my_ERROR;
    }
  case DBI_TYPE_STRING:
    if (RESULT->rows[RESULT->currowidx]->field_sizes[fieldidx] == 0
	&& RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string == NULL) {
      /* string does not exist */
      return 0; /* do not raise an error */
    }
    /* else if field size == 0: empty string */
    /* todo: do we need strtoll() error handling? */
    return strtoll((const char *)(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string), NULL, 10);
  case DBI_TYPE_BINARY:
    return 0; /* do not raise an error */
  case DBI_TYPE_DATETIME:
    return (long long)(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_datetime);
  default:
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    return my_ERROR;
  }
}

char *dbi_result_get_as_string_copy(dbi_result Result, const char *fieldname) {
  char *my_ERROR = "ERROR";
  unsigned int fieldidx;
  dbi_error_flag errflag = DBI_ERROR_NONE;

  _reset_conn_error(RESULT->conn);

  fieldidx = _find_field(RESULT, fieldname, &errflag);
  if (errflag != DBI_ERROR_NONE) {
    dbi_conn_t *conn = RESULT->conn;
    _error_handler(conn, DBI_ERROR_BADNAME);
    return strdup(my_ERROR);
  }
  return dbi_result_get_as_string_copy_idx(Result, fieldidx+1);
}

char *dbi_result_get_as_string_copy_idx(dbi_result Result, unsigned int fieldidx) {
  char *my_ERROR = "ERROR";
  char *newstring = NULL;
  char *oldstring = NULL;
  struct tm utctime;

  fieldidx--;

  newstring = malloc(32); /* sufficient for integers, decimal, and datetime */

  if (!newstring) {
    _error_handler(RESULT->conn, DBI_ERROR_NOMEM);
    return my_ERROR;
  }
  *newstring = '\0';

  /* output depends on: type, size, sign */
  switch (RESULT->field_types[fieldidx]) {
  case DBI_TYPE_INTEGER:
    switch (RESULT->field_attribs[fieldidx] & DBI_INTEGER_SIZEMASK) {
    case DBI_INTEGER_SIZE1:
      if (RESULT->field_attribs[fieldidx] & DBI_INTEGER_UNSIGNED) {
	snprintf(newstring, 32, "%hu", RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_char);
      }
      else {
	snprintf(newstring, 32, "%hd", RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_char);
      }
      break;
    case DBI_INTEGER_SIZE2:
      if (RESULT->field_attribs[fieldidx] & DBI_INTEGER_UNSIGNED) {
	snprintf(newstring, 32, "%hu", RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_short);
      }
      else {
	snprintf(newstring, 32, "%hd", RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_short);
      }
      break;
    case DBI_INTEGER_SIZE3:
    case DBI_INTEGER_SIZE4:
      if (RESULT->field_attribs[fieldidx] & DBI_INTEGER_UNSIGNED) {
	snprintf(newstring, 32, "%u", RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_long);
      }
      else {
	snprintf(newstring, 32, "%d", RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_long);
      }
      break;
    case DBI_INTEGER_SIZE8:
      if (RESULT->field_attribs[fieldidx] & DBI_INTEGER_UNSIGNED) {
	snprintf(newstring, 32, "%llu", RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_longlong);
      }
      else {
	snprintf(newstring, 32, "%lld", RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_longlong);
      }
      break;
    default:
      _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    }
    break;
  case DBI_TYPE_DECIMAL:
    switch (RESULT->field_attribs[fieldidx] & DBI_DECIMAL_SIZEMASK) {
    case DBI_DECIMAL_SIZE4:
      snprintf(newstring, 32, "%e", RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_float);
      break;
    case DBI_DECIMAL_SIZE8:
      snprintf(newstring, 32, "%e", RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_double);
      break;
    default:
      _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
    }
    break;
  case DBI_TYPE_STRING:
    if (RESULT->rows[RESULT->currowidx]->field_sizes[fieldidx] == 0
	&& RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string == NULL) {
      /* string does not exist */
      /* return an empty string instead, no error */
    }
    else {
      /* else if field size == 0: empty string */

      oldstring = newstring;
      if ((newstring = strdup(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_string)) == NULL) {
	_error_handler(RESULT->conn, DBI_ERROR_NOMEM);
	newstring = oldstring;
      }
      else {
	/* don't free in case of an error as we'd return a dangling pointer */
	free(oldstring);
      }
    }
    break;
  case DBI_TYPE_BINARY:
    break; /* return empty string, do not raise an error */
  case DBI_TYPE_DATETIME:
    gmtime_r(&(RESULT->rows[RESULT->currowidx]->field_values[fieldidx].d_datetime), &utctime);
    snprintf(newstring, 32, "%04d-%02d-%02d %02d:%02d:%02d", utctime.tm_year+1900, utctime.tm_mon+1, utctime.tm_mday, utctime.tm_hour, utctime.tm_min, utctime.tm_sec);
    break;
  default:
    _error_handler(RESULT->conn, DBI_ERROR_BADTYPE);
  }

  return newstring; /* is still empty string in case of an error */
}


/* RESULT: bind_* functions */

static int _setup_binding(dbi_result_t *result, const char *fieldname, void *bindto, void *helperfunc) {
  _field_binding_t *binding;
  if (!result) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return DBI_BIND_ERROR;
  }

  _reset_conn_error(result->conn);

  if (!fieldname) {
    _error_handler(result->conn, DBI_ERROR_BADNAME);
    return DBI_BIND_ERROR;
  }
  binding = _find_or_create_binding_node(result, fieldname);
  if (!binding) {
    _error_handler(result->conn, DBI_ERROR_NOMEM);
    return DBI_BIND_ERROR;
  }

  if (bindto == NULL) {
    _remove_binding_node(result, binding);
  }
  else {
    binding->bindto = bindto;
    binding->helper_function = (void*)(_field_binding_t *)helperfunc;
  }

  return 0;
}

static void _activate_bindings(dbi_result_t *result) {
  _field_binding_t *binding = result->field_bindings;
  void (*helper_function)(_field_binding_t *);

  while (binding) {
    helper_function = binding->helper_function;
    helper_function(binding);
    binding = binding->next;
  }
  return;
}

int dbi_result_bind_char(dbi_result Result, const char *fieldname, char *bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_char);
}

int dbi_result_bind_uchar(dbi_result Result, const char *fieldname, unsigned char *bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_uchar);
}

int dbi_result_bind_short(dbi_result Result, const char *fieldname, short *bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_short);
}

int dbi_result_bind_ushort(dbi_result Result, const char *fieldname, unsigned short *bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_ushort);
}

int dbi_result_bind_int(dbi_result Result, const char *fieldname, int *bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_int);
}

int dbi_result_bind_long(dbi_result Result, const char *fieldname, int *bindto) {
  return dbi_result_bind_int(Result, fieldname, bindto);
}

int dbi_result_bind_uint(dbi_result Result, const char *fieldname, unsigned int *bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_uint);
}

int dbi_result_bind_ulong(dbi_result Result, const char *fieldname, unsigned int *bindto) {
  return dbi_result_bind_uint(Result, fieldname, bindto);
}

int dbi_result_bind_longlong(dbi_result Result, const char *fieldname, long long *bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_longlong);
}

int dbi_result_bind_ulonglong(dbi_result Result, const char *fieldname, unsigned long long *bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_ulonglong);
}

int dbi_result_bind_float(dbi_result Result, const char *fieldname, float *bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_float);
}

int dbi_result_bind_double(dbi_result Result, const char *fieldname, double *bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_double);
}

int dbi_result_bind_string(dbi_result Result, const char *fieldname, const char **bindto) {
  return _setup_binding(RESULT, fieldname, (char **)bindto, _bind_helper_string);
}

int dbi_result_bind_binary(dbi_result Result, const char *fieldname, const unsigned char **bindto) {
  return _setup_binding(RESULT, fieldname, (unsigned char **)bindto, _bind_helper_binary);
}

int dbi_result_bind_string_copy(dbi_result Result, const char *fieldname, char **bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_string_copy);
}

int dbi_result_bind_binary_copy(dbi_result Result, const char *fieldname, unsigned char **bindto) {
  return _setup_binding(RESULT, fieldname, bindto, _bind_helper_binary_copy);
}

int dbi_result_bind_datetime(dbi_result Result, const char *fieldname, time_t *bindto) {
  return _setup_binding(RESULT, fieldname, (time_t *)bindto, _bind_helper_datetime);
}

static _field_binding_t *_find_or_create_binding_node(dbi_result_t *result, const char *fieldname) {
  _field_binding_t *prevbinding = NULL;
  _field_binding_t *binding = result->field_bindings;

  while (binding && strcasecmp(fieldname, binding->fieldname)) {
    prevbinding = binding;
    binding = binding->next;
  }
  if (!binding) {
    /* allocate a new option node */
    binding = malloc(sizeof(_field_binding_t));
    if (!binding) {
      return NULL;
    }
    binding->result = result;
    binding->fieldname = strdup(fieldname);
    binding->next = NULL;
    if (result->field_bindings == NULL) {
      result->field_bindings = binding;
    }
    else {
      prevbinding->next = binding;
    }
  }

  return binding;
}

static void _remove_binding_node(dbi_result_t *result, _field_binding_t *deadbinding) {
  _field_binding_t *prevbinding = NULL;
  _field_binding_t *binding = result->field_bindings;

  while (binding && (binding != deadbinding)) {
    prevbinding = binding;
    binding = binding->next;
  }
  if (!binding) {
    /* this should never ever happen. silently pretend it never did. */
    return;
  }
  free((char *)deadbinding->fieldname);
  if (result->field_bindings == deadbinding) {
    result->field_bindings = deadbinding->next;
  }
  else {
    prevbinding->next = deadbinding->next;
  }
  free(deadbinding);
}

/* returns the field index (>= 1), or 0 if no such field */
static unsigned int _find_field(dbi_result_t *result, const char *fieldname, dbi_error_flag *errflag) {
  unsigned long i = 0;
  if (!result || !result->field_names) return DBI_FIELD_ERROR;
  while (i < result->numfields) {
    if (strcasecmp(result->field_names[i], fieldname) == 0) {
      *errflag = DBI_ERROR_NONE;
      return i;
    }
    i++;
  }
  _verbose_handler(result->conn, "result row has no field `%s`\n", fieldname);
  *errflag = DBI_ERROR_BADNAME;
  return 0;
}

static int _is_row_fetched(dbi_result_t *result, unsigned long long row) {
  /* Bull patch reported by Tom Lane */
  /*  if (!result->rows || (row >= result->numrows_matched)) return -1; */
  if (!result->rows || (row > result->numrows_matched)) return -1;
  return !(result->rows[row] == NULL);
}


/* PRIVATE: bind helpers */

static void _bind_helper_char(_field_binding_t *binding) {
  *(char *)binding->bindto = dbi_result_get_char((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_uchar(_field_binding_t *binding) {
  *(unsigned char *)binding->bindto = dbi_result_get_uchar((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_short(_field_binding_t *binding) {
  *(short *)binding->bindto = dbi_result_get_short((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_ushort(_field_binding_t *binding) {
  *(unsigned short *)binding->bindto = dbi_result_get_ushort((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_int(_field_binding_t *binding) {
  *(int *)binding->bindto = dbi_result_get_int((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_uint(_field_binding_t *binding) {
  *(unsigned int *)binding->bindto = dbi_result_get_uint((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_longlong(_field_binding_t *binding) {
  *(long long *)binding->bindto = dbi_result_get_longlong((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_ulonglong(_field_binding_t *binding) {
  *(unsigned long long *)binding->bindto = dbi_result_get_ulonglong((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_float(_field_binding_t *binding) {
  *(float *)binding->bindto = dbi_result_get_float((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_double(_field_binding_t *binding) {
  *(double *)binding->bindto = dbi_result_get_double((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_string(_field_binding_t *binding) {
  *(const char **)binding->bindto = dbi_result_get_string((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_binary(_field_binding_t *binding) {
  *(const unsigned char **)binding->bindto = dbi_result_get_binary((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_string_copy(_field_binding_t *binding) {
  *(char **)binding->bindto = dbi_result_get_string_copy((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_binary_copy(_field_binding_t *binding) {
  *(unsigned char **)binding->bindto = dbi_result_get_binary_copy((dbi_result)binding->result, binding->fieldname);
}

static void _bind_helper_datetime(_field_binding_t *binding) {
  *(time_t *)binding->bindto = dbi_result_get_datetime((dbi_result)binding->result, binding->fieldname);
}