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
/*!
Fetching data from ODBC Cursor and conversion to Rust native types.
!*/

use error_context::prelude::*;
use odbc::ffi::SqlDataType;
use odbc::{ColumnDescriptor, DiagnosticRecord, OdbcType};
use odbc::{SqlDate, SqlSsTime2, SqlTime, SqlTimestamp};
use std::convert::TryFrom;
use std::error::Error;
use std::fmt;
use std::string::FromUtf16Error;
use std::convert::TryInto;

#[cfg(feature = "rust_decimal")]
use rust_decimal::Decimal;
#[cfg(feature = "rust_decimal")]
use std::str::FromStr;

/// Data access configuration that can be used to configure data retrieval and conversion configured per `ResultSet` for given `Item` type.
/// Configuration can be attached to `Handle` and will be cloned per query so it can store per query state.
pub trait Configuration: Default + Clone + fmt::Debug {}

/// Default configuration that allows converting rows to types supported by this crate.
#[derive(Debug, Default, Clone)]
pub struct DefaultConfiguration;
impl Configuration for DefaultConfiguration {}

/// Runtime settings configured per connection.
#[derive(Debug, Default)]
pub struct Settings {
    /// When `true` the `ResultSet` iterator will try to fetch strings as UTF-16 (wide) strings before converting them to Rust's UTF-8 `String`.
    pub utf_16_strings: bool,
}

/// This error can be returned if database provided column type does not match type requested by
/// client
#[derive(Debug)]
pub struct SqlDataTypeMismatch {
    requested: &'static str,
    queried: SqlDataType,
}

impl fmt::Display for SqlDataTypeMismatch {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "requested SQL column data type '{:?}' does not match queried data type '{:?}'",
            self.requested, self.queried
        )
    }
}

impl Error for SqlDataTypeMismatch {}

/// This error can be returned if database provided column of type that currently cannot be mapped to `Value` type.
#[derive(Debug)]
pub struct UnsupportedSqlDataType(SqlDataType);

impl fmt::Display for UnsupportedSqlDataType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "unsupported SQL data type: {:?}", self.0)
    }
}

impl Error for UnsupportedSqlDataType {}

/// Errors related to datum access of ODBC cursor.
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum DatumAccessError {
    OdbcCursorError(DiagnosticRecord),
    SqlDataTypeMismatch(SqlDataTypeMismatch),
    FromUtf16Error(FromUtf16Error, &'static str),
    #[cfg(feature = "serde_json")]
    JsonError(serde_json::Error),
}

impl fmt::Display for DatumAccessError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            DatumAccessError::OdbcCursorError(_) => {
                write!(f, "failed to access data in ODBC cursor")
            }
            DatumAccessError::SqlDataTypeMismatch(_) => {
                write!(f, "failed to handle data type conversion")
            }
            DatumAccessError::FromUtf16Error(_, context) => write!(
                f,
                "failed to create String from UTF-16 column data while {}",
                context
            ),
            #[cfg(feature = "serde_json")]
            DatumAccessError::JsonError(_) => write!(f, "failed to convert data to JSON Value"),
        }
    }
}

impl Error for DatumAccessError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            DatumAccessError::OdbcCursorError(err) => Some(err),
            DatumAccessError::SqlDataTypeMismatch(err) => Some(err),
            DatumAccessError::FromUtf16Error(err, _) => Some(err),
            #[cfg(feature = "serde_json")]
            DatumAccessError::JsonError(err) => Some(err),
        }
    }
}

impl From<ErrorContext<FromUtf16Error, &'static str>> for DatumAccessError {
    fn from(err: ErrorContext<FromUtf16Error, &'static str>) -> DatumAccessError {
        DatumAccessError::FromUtf16Error(err.error, err.context)
    }
}

#[cfg(feature = "serde_json")]
impl From<serde_json::Error> for DatumAccessError {
    fn from(err: serde_json::Error) -> DatumAccessError {
        DatumAccessError::JsonError(err)
    }
}

/// Description of column type, name and nullability properties used to represent row schema.
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnType {
    /// Supported type of datum that this column holds. See `DatumType` documentation of usage of corresponding `Column::into_*()` functions.
    pub datum_type: DatumType,
    /// ODBC SQL Data Type as returned by the driver.
    pub odbc_type: SqlDataType,
    /// `true` if column can contain `NULL` value. If `false` the `Column::into_*()` functions should always return `Some` value.
    pub nullable: bool,
    /// Name of the column as provided by the ODBC driver.
    pub name: String,
}

/// Types of values that column can be converted to.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DatumType {
    /// Use `Column::into_bool()` to get column value.
    Bit,
    /// Use `Column::into_i8()` to get column value.
    Tinyint,
    /// Use `Column::into_i16()` to get column value.
    Smallint,
    /// Use `Column::into_i32()` to get column value.
    Integer,
    /// Use `Column::into_i64()` to get column value.
    Bigint,
    /// Use `Column::into_f32()` to get column value.
    Float,
    /// Use `Column::into_f64()` to get column value.
    Double,
    #[cfg(feature = "rust_decimal")]
    /// Use `Column::into_decimal()` to get column value.
    Decimal,
    /// Use `Column::into_string()` to get column value.
    String,
    /// Use `Column::into_timestamp()` to get column value.
    Timestamp,
    /// Use `Column::into_date()` to get column value.
    Date,
    /// Use `Column::into_time()` to get column value.
    Time,
    #[cfg(feature = "serde_json")]
    /// Use `Column::into_json()` to parse as `serde_json::Value` or `Column::into_string()` to get it as `String`.
    Json,
}

impl DatumType {
    /// Static string describing type of column datum.
    pub fn description(self) -> &'static str {
        match self {
            DatumType::Bit => "BIT",
            DatumType::Tinyint => "TINYINT",
            DatumType::Smallint => "SMALLINT",
            DatumType::Integer => "INTEGER",
            DatumType::Bigint => "BIGINT",
            DatumType::Float => "FLOAT",
            DatumType::Double => "DOUBLE",
            #[cfg(feature = "rust_decimal")]
            DatumType::Decimal => "DECIMAL",
            DatumType::String => "STRING",
            DatumType::Timestamp => "TIMESTAMP",
            DatumType::Date => "DATE",
            DatumType::Time => "TIME",
            #[cfg(feature = "serde_json")]
            DatumType::Json => "JSON",
        }
    }
}

impl TryFrom<ColumnDescriptor> for ColumnType {
    type Error = UnsupportedSqlDataType;

    fn try_from(column_descriptor: ColumnDescriptor) -> Result<ColumnType, UnsupportedSqlDataType> {
        use SqlDataType::*;
        let datum_type = match column_descriptor.data_type {
            SQL_EXT_BIT => DatumType::Bit,
            SQL_EXT_TINYINT => DatumType::Tinyint,
            SQL_SMALLINT => DatumType::Smallint,
            SQL_INTEGER => DatumType::Integer,
            SQL_EXT_BIGINT => DatumType::Bigint,
            SQL_FLOAT | SQL_REAL => DatumType::Float,
            SQL_DOUBLE => DatumType::Double,
            #[cfg(feature = "rust_decimal")]
            SQL_DECIMAL | SQL_NUMERIC => DatumType::Decimal,
            SQL_CHAR | SQL_VARCHAR | SQL_EXT_LONGVARCHAR | SQL_EXT_WCHAR | SQL_EXT_WVARCHAR
            | SQL_EXT_WLONGVARCHAR => DatumType::String,
            SQL_TIMESTAMP => DatumType::Timestamp,
            SQL_DATE => DatumType::Date,
            SQL_TIME | SQL_SS_TIME2 => DatumType::Time,
            SQL_UNKNOWN_TYPE => {
                #[cfg(feature = "serde_json")]
                {
                    DatumType::Json
                }
                #[cfg(not(feature = "serde_json"))]
                {
                    DatumType::String
                }
            }
            _ => return Err(UnsupportedSqlDataType(column_descriptor.data_type)),
        };

        Ok(ColumnType {
            datum_type,
            odbc_type: column_descriptor.data_type,
            nullable: column_descriptor.nullable.unwrap_or(true),
            name: column_descriptor.name,
        })
    }
}

/// Represents SQL table column which can be converted to Rust native type.
pub struct Column<'r, 's, 'c, S, C: Configuration> {
    /// Type information about this column
    pub column_type: &'r ColumnType,
    /// Data access configuration
    pub configuration: &'s C,
    /// Runtime settings
    settings: &'r Settings,
    /// ODBC Cursor object pointed at this column
    cursor: &'r mut odbc::Cursor<'s, 'c, 'c, S>,
    /// Which column are we at
    index: u16,
}

impl<'r, 's, 'c, S, C: Configuration> fmt::Debug for Column<'r, 's, 'c, S, C> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Column")
            .field("column_type", &self.column_type)
            .field("settings", &self.settings)
            .field("configuration", &self.configuration)
            .field("index", &self.index)
            .finish()
    }
}

impl<'r, 's, 'c, S, C: Configuration> Column<'r, 's, 'c, S, C> {
    fn into<T: OdbcType<'r>>(self) -> Result<Option<T>, DatumAccessError> {
        self.cursor
            .get_data::<T>(self.index + 1)
            .map_err(DatumAccessError::OdbcCursorError)
    }

    // https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/c-data-types?view=sql-server-2017

    /// Reads `bool` value from column.
    pub fn into_bool(self) -> Result<Option<bool>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_EXT_BIT => self.into::<u8>()?.map(|byte| byte != 0),
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "BIT",
                    queried,
                }))
            }
        })
    }

    /// Reads `i8` value from column.
    pub fn into_i8(self) -> Result<Option<i8>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_EXT_TINYINT => self.into::<i8>()?,
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "TINYINT",
                    queried,
                }))
            }
        })
    }

    /// Reads `i16` value from column.
    pub fn into_i16(self) -> Result<Option<i16>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_SMALLINT => self.into::<i16>()?,
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "SMALLINT",
                    queried,
                }))
            }
        })
    }

    /// Reads `i32` value from column.
    pub fn into_i32(self) -> Result<Option<i32>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_INTEGER => self.into::<i32>()?,
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "INTEGER",
                    queried,
                }))
            }
        })
    }

    /// Reads `i64` value from column.
    pub fn into_i64(self) -> Result<Option<i64>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_EXT_BIGINT => self.into::<i64>()?,
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "BIGINT",
                    queried,
                }))
            }
        })
    }

    /// Reads `f32` value from column.
    pub fn into_f32(self) -> Result<Option<f32>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_REAL | SqlDataType::SQL_FLOAT => self.into::<f32>()?,
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "FLOAT",
                    queried,
                }))
            }
        })
    }

    /// Reads `f64` value from column.
    pub fn into_f64(self) -> Result<Option<f64>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_DOUBLE => self.into::<f64>()?,
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "DOUBLE",
                    queried,
                }))
            }
        })
    }

    #[cfg(feature = "rust_decimal")]
    /// Reads `Decimal` value from column.
    pub fn into_decimal(self) -> Result<Option<Decimal>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_DECIMAL | SqlDataType::SQL_NUMERIC => {
                // Since Decimal isn't an OdbcType, get the String representation and convert that to a Decimal instead
                let decimal_as_string = &self.into::<String>()?;
                match decimal_as_string {
                    Some(s) => Some(Decimal::from_str(&s).unwrap()),
                    None => None
                }
            },
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "DECIMAL",
                    queried,
                }))
            }
        })
    }

    /// Reads `String` value from column.
    pub fn into_string(self) -> Result<Option<String>, DatumAccessError> {
        use SqlDataType::*;
        Ok(match self.column_type.odbc_type {
            SQL_CHAR | SQL_VARCHAR | SQL_EXT_LONGVARCHAR => self.into::<String>()?,
            SQL_EXT_WCHAR | SQL_EXT_WVARCHAR | SQL_EXT_WLONGVARCHAR |
            SQL_UNKNOWN_TYPE => {
                if self.settings.utf_16_strings {
                    self.into::<&[u16]>()?
                        .map(|bytes| String::from_utf16(bytes).wrap_error_while("getting UTF-16 string (SQL_EXT_WCHAR | SQL_EXT_WVARCHAR | SQL_EXT_WLONGVARCHAR)"))
                        .transpose()?
                } else {
                    self.into::<String>()?
                }
            }
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "STRING",
                    queried,
                }))
            }
        })
    }

    /// Reads `SqlTimestamp` value from column.
    pub fn into_timestamp(self) -> Result<Option<SqlTimestamp>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_TIMESTAMP => self.into::<SqlTimestamp>()?,
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "TIMESTAMP",
                    queried,
                }))
            }
        })
    }

    /// Reads `SqlDate` value from column.
    pub fn into_date(self) -> Result<Option<SqlDate>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_DATE => self.into::<SqlDate>()?,
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "DATE",
                    queried,
                }))
            }
        })
    }

    /// Reads `SqlSsTime2` value from column.
    pub fn into_time(self) -> Result<Option<SqlSsTime2>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            SqlDataType::SQL_TIME => self.into::<SqlTime>()?.map(|ss| SqlSsTime2 {
                hour: ss.hour,
                minute: ss.minute,
                second: ss.second,
                fraction: 0,
            }),
            SqlDataType::SQL_SS_TIME2 => self.into::<SqlSsTime2>()?,
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "TIME",
                    queried,
                }))
            }
        })
    }

    #[cfg(feature = "serde_json")]
    /// Reads `serde_json::Value` value from column.
    pub fn into_json(self) -> Result<Option<serde_json::Value>, DatumAccessError> {
        Ok(match self.column_type.odbc_type {
            queried @ SqlDataType::SQL_UNKNOWN_TYPE => {
                self.into::<String>()?
                    .map(|data| {
                        // MonetDB can only store arrays or objects as top level JSON values so check if data looks like JSON in case we are not talking to MonetDB
                        if (data.starts_with("[") && data.ends_with("]"))
                            || (data.starts_with("{") && data.ends_with("}"))
                        {
                            serde_json::from_str(&data).map_err(Into::into)
                        } else {
                            //TOOD: better error?
                            return Err(DatumAccessError::SqlDataTypeMismatch(
                                SqlDataTypeMismatch {
                                    requested: "JSON",
                                    queried,
                                },
                            ));
                        }
                    })
                    .transpose()?
            }
            queried => {
                return Err(DatumAccessError::SqlDataTypeMismatch(SqlDataTypeMismatch {
                    requested: "JSON",
                    queried,
                }))
            }
        })
    }

    /// Gets column number in the row (first column is 0)
    pub fn index(&self) -> u16 {
        self.index
    }
}

/// Represents SQL table row of `Column` objects.
pub struct Row<'r, 's, 'c, S, C: Configuration> {
    /// Schema information about this row
    pub schema: &'r [ColumnType],
    /// Data access configuration
    pub configuration: &'s C,
    /// Runtime settings
    settings: &'r Settings,
    /// ODBC Cursor object
    cursor: odbc::Cursor<'s, 'c, 'c, S>,
    /// Which column will shift next
    index: u16,
    /// Number of columns
    columns: u16,
}

impl<'r, 's, 'c, S, C: Configuration> fmt::Debug for Row<'r, 's, 'c, S, C> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Row")
            .field("schema", &self.schema)
            .field("settings", &self.settings)
            .field("configuration", &self.configuration)
            .field("index", &self.index)
            .field("columns", &self.columns)
            .finish()
    }
}

impl<'r, 's, 'c, S, C: Configuration> Row<'r, 's, 'c, S, C> {
    pub fn new(
        cursor: odbc::Cursor<'s, 'c, 'c, S>,
        schema: &'r [ColumnType],
        settings: &'r Settings,
        configuration: &'s C,
    ) -> Row<'r, 's, 'c, S, C> {
        Row {
            schema,
            configuration,
            settings,
            cursor,
            index: 0,
            columns: schema.len() as u16,
        }
    }

    pub fn shift_column<'i>(&'i mut self) -> Option<Column<'i, 's, 'c, S, C>> {
        self.schema
            .get(self.index as usize)
            .map(move |column_type| {
                let column = Column {
                    column_type,
                    configuration: self.configuration,
                    settings: &self.settings,
                    cursor: &mut self.cursor,
                    index: self.index,
                };

                self.index += 1;
                column
            })
    }

    /// Gets number of columns
    pub fn columns(&self) -> u16 {
        self.columns
    }
}


/// Column values can be converted to types implementing this trait.
///
/// This trait is implemented for primitive Rust types, `String` and `chrono` date and time types.
pub trait TryFromColumn<C: Configuration>: Sized {
    type Error: Error + 'static;
    /// Create `Self` from row column.
    fn try_from_column<'i, 's, 'c, S>(column: Column<'i, 's, 'c, S, C>) -> Result<Self, Self::Error>;
}

/// This traits allow for conversion of `Row` type representing ODBC cursor used internally by `ResultSet` iterator to any other type returned as `Item` that implements it.
///
/// This trait is implemented for Rust tuple type enabling conversion of rows to tuples of types implementing `TryFromValue`.
/// Also this trait implementation allows to convert single column rows to types implementing `TryFromColumn`.
///
/// This trait can be implemented for custom objects. This will enable them to be queried directly from database as `Item` of `ResultSet` iterator.
pub trait TryFromRow<C: Configuration>: Sized {
    type Error: Error + 'static;
    /// Given `ColumnType` convert from `Row` to other type of value representing table row.
    fn try_from_row<'r, 's, 'c, S>(row: Row<'r, 's, 'c, S, C>) -> Result<Self, Self::Error>;
}

/// Error type that represents different problems when converting column values to specific types.
#[derive(Debug)]
pub enum ColumnConvertError {
    UnexpectedNullValue(&'static str),
    DatumAccessError(DatumAccessError),
    ValueOutOfRange {
        expected: &'static str,
    },
}

impl From<DatumAccessError> for ColumnConvertError {
    fn from(err: DatumAccessError) -> ColumnConvertError {
        ColumnConvertError::DatumAccessError(err)
    }
}

impl fmt::Display for ColumnConvertError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ColumnConvertError::UnexpectedNullValue(t) => {
                write!(f, "expecting value of type {} but got NULL", t)
            }
            ColumnConvertError::DatumAccessError(_) => {
                write!(f, "problem accessing datum")
            }
            ColumnConvertError::ValueOutOfRange { expected } => {
                write!(f, "value is out of range for type {}", expected)
            }
        }
    }
}

impl Error for ColumnConvertError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            ColumnConvertError::DatumAccessError(err) => Some(err),
            ColumnConvertError::UnexpectedNullValue(_) |
            ColumnConvertError::ValueOutOfRange { .. } => None,
        }
    }
}

macro_rules! try_from_row_not_null {
    ($t:ty) => {
        impl<C: Configuration> TryFromColumn<C> for $t {
            type Error = ColumnConvertError;
            fn try_from_column<'i, 's, 'c, S>(column: Column<'i, 's, 'c, S, C>) -> Result<Self, Self::Error> {
                let value: Option<$t> = TryFromColumn::try_from_column(column)?;
                value.ok_or_else(|| ColumnConvertError::UnexpectedNullValue(stringify!($t)))
            }
        }
    }
}

macro_rules! try_from_row {
    ($t:ty, $f:ident) => {
        impl<C: Configuration> TryFromColumn<C> for Option<$t> {
            type Error = ColumnConvertError;
            fn try_from_column<'i, 's, 'c, S>(column: Column<'i, 's, 'c, S, C>) -> Result<Self, Self::Error> {
                column.$f().map_err(Into::into)
            }
        }

        try_from_row_not_null!($t);
    };
}

macro_rules! try_from_row_unsigned {
    ($it:ty, $t:ty) => {
        impl<C: Configuration> TryFromColumn<C> for Option<$t> {
            type Error = ColumnConvertError;
            fn try_from_column<'i, 's, 'c, S>(column: Column<'i, 's, 'c, S, C>) -> Result<Self, Self::Error> {
                let value: Option<$it> = TryFromColumn::try_from_column(column)?;
                value.map(|value|
                    value
                    .try_into()
                    .map_err(|_| ColumnConvertError::ValueOutOfRange {
                        expected: stringify!($t),
                    })
                ).transpose()
            }
        }

        try_from_row_not_null!($t);
    };
}

try_from_row![bool, into_bool];
try_from_row![i8, into_i8];
try_from_row_unsigned![i8, u8];
try_from_row![i16, into_i16];
try_from_row_unsigned![i16, u16];
try_from_row![i32, into_i32];
try_from_row_unsigned![i32, u32];
try_from_row![i64, into_i64];
try_from_row_unsigned![i64, u64];
try_from_row![f32, into_f32];
try_from_row![f64, into_f64];
try_from_row![String, into_string];
try_from_row![SqlTimestamp, into_timestamp];
try_from_row![SqlDate, into_date];
try_from_row![SqlSsTime2, into_time];
#[cfg(feature = "serde_json")]
try_from_row![serde_json::Value, into_json];

#[cfg(feature = "chrono")]
use chrono::{NaiveDateTime, NaiveDate, NaiveTime};

#[cfg(feature = "chrono")]
impl<C: Configuration> TryFromColumn<C> for Option<NaiveDateTime> {
    type Error = ColumnConvertError;
    fn try_from_column<'i, 's, 'c, S>(column: Column<'i, 's, 'c, S, C>) -> Result<Self, Self::Error> {
        let value: Option<SqlTimestamp> = TryFromColumn::try_from_column(column)?;

        Ok(value.map(|value| {
            NaiveDate::from_ymd(
                i32::from(value.year),
                u32::from(value.month),
                u32::from(value.day),
            )
            .and_hms_nano(
                u32::from(value.hour),
                u32::from(value.minute),
                u32::from(value.second),
                value.fraction,
            )
        }))
    }
}

#[cfg(feature = "chrono")]
try_from_row_not_null!(NaiveDateTime);

#[cfg(feature = "chrono")]
impl<C: Configuration> TryFromColumn<C> for Option<NaiveDate> {
    type Error = ColumnConvertError;
    fn try_from_column<'i, 's, 'c, S>(column: Column<'i, 's, 'c, S, C>) -> Result<Self, Self::Error> {
        let value: Option<SqlDate> = TryFromColumn::try_from_column(column)?;

        Ok(value.map(|value| {
            NaiveDate::from_ymd(
                i32::from(value.year),
                u32::from(value.month),
                u32::from(value.day),
            )
        }))
    }
}

#[cfg(feature = "chrono")]
try_from_row_not_null!(NaiveDate);

#[cfg(feature = "chrono")]
impl<C: Configuration> TryFromColumn<C> for Option<NaiveTime> {
    type Error = ColumnConvertError;
    fn try_from_column<'i, 's, 'c, S>(column: Column<'i, 's, 'c, S, C>) -> Result<Self, Self::Error> {
        let value: Option<SqlSsTime2> = TryFromColumn::try_from_column(column)?;

        Ok(value.map(|value| {
            NaiveTime::from_hms_nano(
                u32::from(value.hour),
                u32::from(value.minute),
                u32::from(value.second),
                value.fraction,
            )
        }))
    }
}

#[cfg(feature = "chrono")]
try_from_row_not_null!(NaiveTime);

/// Errors that may happen during conversion of `ValueRow` to given type.
#[derive(Debug)]
pub enum RowConvertError {
    UnexpectedNullValue(&'static str),
    UnexpectedValue,
    UnexpectedNumberOfColumns { expected: u16, got: u16 },
    ColumnConvertError(Box<dyn Error>),
}

impl From<ColumnConvertError> for RowConvertError {
    fn from(err: ColumnConvertError) -> RowConvertError {
        RowConvertError::ColumnConvertError(Box::new(err))
    }
}

impl fmt::Display for RowConvertError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            RowConvertError::UnexpectedNullValue(t) => {
                write!(f, "expecting value of type {} but got NULL", t)
            }
            RowConvertError::UnexpectedValue => write!(f, "expecting no data (unit) but got a row"),
            RowConvertError::UnexpectedNumberOfColumns { expected, got } => write!(
                f,
                "unexpected number of columns: expected {} but got {}",
                expected, got
            ),
            RowConvertError::ColumnConvertError(_) => {
                write!(f, "failed to convert column value to target type")
            }
        }
    }
}

impl Error for RowConvertError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            RowConvertError::UnexpectedNullValue(_)
            | RowConvertError::UnexpectedValue
            | RowConvertError::UnexpectedNumberOfColumns { .. } => None,
            RowConvertError::ColumnConvertError(err) => Some(err.as_ref()),
        }
    }
}

/// Unit can be used to signal that no rows of data should be produced.
impl TryFromRow<DefaultConfiguration> for () {
    type Error = RowConvertError;
    fn try_from_row<'r, 's, 'c, S>(_row: Row<'r, 's, 'c, S, DefaultConfiguration>) -> Result<Self, Self::Error> {
        Err(RowConvertError::UnexpectedValue)
    }
}

/// Convert row with single column to any type implementing `TryFromColumn`.
impl<T> TryFromRow<DefaultConfiguration> for T
where
    T: TryFromColumn<DefaultConfiguration>,
{
    type Error = RowConvertError;
    fn try_from_row<'r, 's, 'c, S>(mut row: Row<'r, 's, 'c, S, DefaultConfiguration>) -> Result<Self, Self::Error> {
        if row.columns() != 1 {
            return Err(RowConvertError::UnexpectedNumberOfColumns {
                expected: 1,
                got: row.columns(),
            });
        }

        let column = row.shift_column().unwrap();

        TryFromColumn::try_from_column(column)
            .map_err(|e| RowConvertError::ColumnConvertError(Box::new(e)))

    }
}

/// Errors that my arise when converting rows to tuples.
#[derive(Debug)]
pub enum RowConvertTupleError {
    UnexpectedNumberOfColumns { expected: u16, tuple: &'static str },
    ValueConvertError(Box<dyn Error>),
}

impl fmt::Display for RowConvertTupleError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            RowConvertTupleError::UnexpectedNumberOfColumns { expected, tuple } => write!(
                f,
                "failed to convert row with {} columns to tuple {}",
                expected, tuple
            ),
            RowConvertTupleError::ValueConvertError(_) => {
                write!(f, "failed to convert column value to target type")
            }
        }
    }
}

impl Error for RowConvertTupleError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            RowConvertTupleError::UnexpectedNumberOfColumns { .. } => None,
            RowConvertTupleError::ValueConvertError(err) => Some(err.as_ref()),
        }
    }
}

macro_rules! count {
    () => (0u16);
    ( $x:tt $($xs:tt)* ) => (1u16 + count!($($xs)*));
}

macro_rules! try_from_tuple {
    ($(
        $Tuple:ident {
            $(($idx:tt) -> $T:ident)+
        }
    )+) => {
        $(
            impl<C: Configuration, $($T: TryFromColumn<C>),+> TryFromRow<C> for ($($T,)+) {
                type Error = RowConvertTupleError;
                fn try_from_row<'r, 's, 'c, S>(mut row: Row<'r, 's, 'c, S, C>) -> Result<($($T,)+), Self::Error> {
                    if row.columns() != count!($($T)+) {
                        return Err(RowConvertTupleError::UnexpectedNumberOfColumns { expected: row.columns(), tuple: stringify![($($T,)+)] })
                    }
                    Ok(($({ let x: $T = $T::try_from_column(row.shift_column().unwrap()).map_err(|err| RowConvertTupleError::ValueConvertError(Box::new(err)))?; x},)+))
                }
            }
        )+
    }
}

try_from_tuple! {
    Tuple1 {
        (0) -> TA
    }
    Tuple2 {
        (0) -> TA
        (1) -> TB
    }
    Tuple3 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
    }
    Tuple4 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
    }
    Tuple5 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
    }
    Tuple6 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
    }
    Tuple7 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
    }
    Tuple8 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
    }
    Tuple9 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
    }
    Tuple10 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
    }
    Tuple11 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
    }
    Tuple12 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
    }
    Tuple13 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
    }
    Tuple14 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
    }
    Tuple15 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
        (15) -> TO
    }
    Tuple16 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
        (15) -> TO
        (16) -> TP
    }
    Tuple17 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
        (15) -> TO
        (16) -> TP
        (17) -> TQ
    }
    Tuple18 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
        (15) -> TO
        (16) -> TP
        (17) -> TQ
        (18) -> TR
    }
    Tuple19 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
        (15) -> TO
        (16) -> TP
        (17) -> TQ
        (18) -> TR
        (19) -> TS
    }
    Tuple20 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
        (15) -> TO
        (16) -> TP
        (17) -> TQ
        (18) -> TR
        (19) -> TS
        (20) -> TT
    }
    Tuple21 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
        (15) -> TO
        (16) -> TP
        (17) -> TQ
        (18) -> TR
        (19) -> TS
        (20) -> TT
        (21) -> TU
    }
    Tuple22 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
        (15) -> TO
        (16) -> TP
        (17) -> TQ
        (18) -> TR
        (19) -> TS
        (20) -> TT
        (21) -> TU
        (22) -> TV
    }
    Tuple23 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
        (15) -> TO
        (16) -> TP
        (17) -> TQ
        (18) -> TR
        (19) -> TS
        (20) -> TT
        (21) -> TU
        (22) -> TV
        (23) -> TW
    }
    Tuple24 {
        (0) -> TA
        (1) -> TB
        (2) -> TC
        (3) -> TD
        (4) -> TE
        (5) -> TF
        (6) -> TG
        (7) -> TH
        (8) -> TI
        (9) -> TJ
        (10) -> TK
        (11) -> TL
        (13) -> TM
        (14) -> TN
        (15) -> TO
        (16) -> TP
        (17) -> TQ
        (18) -> TR
        (19) -> TS
        (20) -> TT
        (21) -> TU
        (22) -> TV
        (23) -> TW
        (24) -> TY
    }
}