exarrow-rs 0.12.0

ADBC-compatible driver for Exasol with Arrow data format support
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
//! WebSocket message types for Exasol protocol.
//!
//! This module defines the JSON message structures used in Exasol's WebSocket API.
//! Messages follow the Exasol WebSocket protocol specification.

use arrow::record_batch::RecordBatch;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ============================================================================
// Login Protocol Messages (4-step handshake)
// ============================================================================

/// Step 1: Login init request - initiates the login handshake.
///
/// This is the first message sent to start the authentication process.
/// The server responds with a public key for encrypting credentials.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LoginInitRequest {
    /// Command name (always "login")
    pub command: String,
    /// Protocol version
    pub protocol_version: i32,
}

impl LoginInitRequest {
    /// Create a new login init request.
    pub fn new() -> Self {
        Self {
            command: "login".to_string(),
            protocol_version: 3,
        }
    }
}

impl Default for LoginInitRequest {
    fn default() -> Self {
        Self::new()
    }
}

/// Step 2: Public key response - server returns RSA public key.
///
/// The server responds to LoginInitRequest with this message containing
/// the RSA public key to use for encrypting the password.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyResponse {
    /// Status of the response ("ok" or "error")
    pub status: String,
    /// Response data containing the public key
    pub response_data: Option<PublicKeyData>,
    /// Exception information if failed
    pub exception: Option<ExceptionInfo>,
}

/// Public key data returned by the server.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyData {
    /// PEM-encoded RSA public key
    pub public_key_pem: String,
    /// Hex-encoded RSA modulus
    pub public_key_modulus: String,
    /// Hex-encoded RSA exponent
    pub public_key_exponent: String,
}

/// Step 3: Authentication request - client sends encrypted credentials.
///
/// After receiving the public key, the client encrypts the password
/// using RSA and sends this authentication request.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthRequest {
    /// Username
    pub username: String,
    /// Base64-encoded RSA-encrypted password
    pub password: String,
    /// Whether to use compression
    pub use_compression: bool,
    /// Client name identifier
    pub client_name: String,
    /// Driver name identifier
    pub driver_name: String,
    /// Client version string
    pub client_version: String,
    /// Optional client OS username
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_os_username: Option<String>,
    /// Optional session attributes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attributes: Option<HashMap<String, serde_json::Value>>,
}

impl AuthRequest {
    /// Create a new authentication request.
    ///
    /// # Arguments
    /// * `username` - The database username
    /// * `encrypted_password` - The Base64-encoded RSA-encrypted password
    /// * `client_name` - Name of the client application
    pub fn new(username: String, encrypted_password: String, client_name: String) -> Self {
        Self {
            username,
            password: encrypted_password,
            use_compression: false,
            client_name: client_name.clone(),
            driver_name: client_name,
            client_version: env!("CARGO_PKG_VERSION").to_string(),
            client_os_username: None,
            attributes: None,
        }
    }

    /// Set the driver name.
    pub fn with_driver_name(mut self, driver_name: String) -> Self {
        self.driver_name = driver_name;
        self
    }

    /// Set the client version.
    pub fn with_client_version(mut self, version: String) -> Self {
        self.client_version = version;
        self
    }

    /// Set the client OS username.
    pub fn with_os_username(mut self, username: String) -> Self {
        self.client_os_username = Some(username);
        self
    }

    /// Set session attributes.
    pub fn with_attributes(mut self, attributes: HashMap<String, serde_json::Value>) -> Self {
        self.attributes = Some(attributes);
        self
    }
}

// ============================================================================
// Legacy Login Request (kept for reference/compatibility)
// ============================================================================

/// Login request message (legacy single-step - NOT USED for actual authentication).
///
/// Note: Exasol requires a 4-step handshake. Use `LoginInitRequest` and `AuthRequest`
/// for proper authentication.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LoginRequest {
    /// Command name
    pub command: String,
    /// Protocol version
    pub protocol_version: i32,
    /// Authentication credentials
    #[serde(flatten)]
    pub credentials: Credentials,
}

/// Authentication credentials.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Credentials {
    /// Username
    pub username: String,
    /// Password
    pub password: String,
}

impl LoginRequest {
    /// Create a new login request.
    pub fn new(username: String, password: String) -> Self {
        Self {
            command: "login".to_string(),
            protocol_version: 3,
            credentials: Credentials { username, password },
        }
    }
}

// ============================================================================
// Step 4: Login Response (final authentication response)
// ============================================================================

/// Login response message (Step 4 of the handshake).
///
/// This is the final response after successful authentication,
/// containing session information.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LoginResponse {
    /// Status of the response
    pub status: String,
    /// Response data
    pub response_data: Option<LoginResponseData>,
    /// Exception information if failed
    pub exception: Option<ExceptionInfo>,
}

/// Login response data.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LoginResponseData {
    /// Session ID (numeric in Exasol WebSocket API)
    pub session_id: i64,
    /// Protocol version accepted
    pub protocol_version: i32,
    /// Release version
    pub release_version: String,
    /// Database name
    pub database_name: String,
    /// Product name
    pub product_name: String,
    /// Maximum data message size
    pub max_data_message_size: Option<i64>,
    /// Maximum varchar size
    pub max_varchar_size: Option<i64>,
    /// Maximum identifier length
    pub max_identifier_length: Option<i64>,
    /// Identifier quote string
    pub identifier_quote_string: Option<String>,
    /// Time zone
    pub time_zone: Option<String>,
    /// Time zone behavior
    pub time_zone_behavior: Option<String>,
}

// ============================================================================
// Query Execution Messages
// ============================================================================

/// Execute SQL statement request.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExecuteRequest {
    /// Command name
    pub command: String,
    /// SQL text to execute
    pub sql_text: String,
    /// Result set max rows (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result_set_max_rows: Option<i64>,
    /// Attributes (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attributes: Option<HashMap<String, String>>,
}

impl ExecuteRequest {
    /// Create a new execute request.
    pub fn new(sql: String) -> Self {
        Self {
            command: "execute".to_string(),
            sql_text: sql,
            result_set_max_rows: None,
            attributes: None,
        }
    }

    /// Set maximum rows for result set.
    pub fn with_max_rows(mut self, max_rows: i64) -> Self {
        self.result_set_max_rows = Some(max_rows);
        self
    }

    /// Add an attribute.
    pub fn with_attribute(mut self, key: String, value: String) -> Self {
        self.attributes
            .get_or_insert_with(HashMap::new)
            .insert(key, value);
        self
    }
}

/// Execute response message.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExecuteResponse {
    /// Status of the response
    pub status: String,
    /// Response data
    pub response_data: Option<ExecuteResponseData>,
    /// Exception information if failed
    pub exception: Option<ExceptionInfo>,
}

/// Execute response data.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExecuteResponseData {
    /// Number of result sets
    pub num_results: i32,
    /// Result sets
    pub results: Vec<ResultSetInfo>,
    /// Attributes
    pub attributes: Option<HashMap<String, String>>,
}

/// Result set information (outer wrapper in results array).
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResultSetInfo {
    /// Result type: "resultSet" for SELECT, "rowCount" for DML
    pub result_type: String,
    /// Row count (for DML statements like INSERT/UPDATE/DELETE)
    pub row_count: Option<i64>,
    /// Nested result set data (for SELECT statements)
    pub result_set: Option<ResultSetData>,
}

/// Result set data (nested inside ResultSetInfo for SELECT queries).
///
/// **Note**: Data is deserialized from Exasol's column-major format to row-major format.
/// Access data as `data[row_idx][col_idx]`.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResultSetData {
    /// Result set handle for fetching more data
    pub result_set_handle: Option<i32>,
    /// Number of columns
    pub num_columns: Option<i32>,
    /// Total number of rows in result set
    pub num_rows: Option<i64>,
    /// Number of rows in this message chunk
    pub num_rows_in_message: Option<i64>,
    /// Column information
    pub columns: Option<Vec<ColumnInfo>>,
    /// Data in row-major format: data[row_idx][col_idx]
    /// Deserialized and transposed from Exasol's column-major wire format.
    #[serde(default, deserialize_with = "super::deserialize::to_row_major_option")]
    pub data: Option<Vec<Vec<serde_json::Value>>>,
}

/// Column metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ColumnInfo {
    /// Column name
    pub name: String,
    /// Data type
    pub data_type: DataType,
}

/// Exasol data type.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataType {
    /// Type name
    #[serde(rename = "type")]
    pub type_name: String,
    /// Precision (for numeric types)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub precision: Option<i32>,
    /// Scale (for decimal types)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scale: Option<i32>,
    /// Size (for string types)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<i64>,
    /// Character set (for string types)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub character_set: Option<String>,
    /// With local time zone (for timestamp types)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub with_local_time_zone: Option<bool>,
    /// Fraction (for interval types)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fraction: Option<i32>,
}

impl DataType {
    /// Create a DECIMAL type with specified precision and scale.
    pub fn decimal(precision: i32, scale: i32) -> Self {
        Self {
            type_name: "DECIMAL".to_string(),
            precision: Some(precision),
            scale: Some(scale),
            size: None,
            character_set: None,
            with_local_time_zone: None,
            fraction: None,
        }
    }

    /// Create a DOUBLE type.
    pub fn double() -> Self {
        Self {
            type_name: "DOUBLE".to_string(),
            precision: None,
            scale: None,
            size: None,
            character_set: None,
            with_local_time_zone: None,
            fraction: None,
        }
    }

    /// Create a VARCHAR type with specified size.
    pub fn varchar(size: i64) -> Self {
        Self {
            type_name: "VARCHAR".to_string(),
            precision: None,
            scale: None,
            size: Some(size),
            character_set: Some("UTF8".to_string()),
            with_local_time_zone: None,
            fraction: None,
        }
    }

    /// Create a BOOLEAN type.
    pub fn boolean() -> Self {
        Self {
            type_name: "BOOLEAN".to_string(),
            precision: None,
            scale: None,
            size: None,
            character_set: None,
            with_local_time_zone: None,
            fraction: None,
        }
    }

    /// Infer a DataType from a JSON value.
    /// Used when parameter types are not provided by the server.
    pub fn infer_from_json(value: &serde_json::Value) -> Self {
        match value {
            serde_json::Value::Null => Self::varchar(2_000_000),
            serde_json::Value::Bool(_) => Self::boolean(),
            serde_json::Value::Number(n) => {
                if n.is_f64() {
                    Self::double()
                } else {
                    // Integer - use DECIMAL(18, 0) to handle large integers
                    Self::decimal(18, 0)
                }
            }
            serde_json::Value::String(_) => Self::varchar(2_000_000),
            // Arrays and objects are serialized as strings
            serde_json::Value::Array(_) | serde_json::Value::Object(_) => Self::varchar(2_000_000),
        }
    }
}

// ============================================================================
// Fetch Messages
// ============================================================================

/// Fetch request to retrieve more result data.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchRequest {
    /// Command name
    pub command: String,
    /// Result set handle
    pub result_set_handle: i32,
    /// Starting position (0-based)
    pub start_position: i64,
    /// Number of rows to fetch
    pub num_bytes: i64,
}

impl FetchRequest {
    /// Create a new fetch request.
    pub fn new(result_set_handle: i32, start_position: i64, num_bytes: i64) -> Self {
        Self {
            command: "fetch".to_string(),
            result_set_handle,
            start_position,
            num_bytes,
        }
    }
}

/// Fetch response message.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchResponse {
    /// Status of the response
    pub status: String,
    /// Response data
    pub response_data: Option<FetchResponseData>,
    /// Exception information if failed
    pub exception: Option<ExceptionInfo>,
}

/// Fetch response data.
///
/// **Note**: Data is deserialized from Exasol's column-major format to row-major format.
/// Access data as `data[row_idx][col_idx]`.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchResponseData {
    /// Number of rows in this message
    pub num_rows: i64,
    /// Data in row-major format: data[row_idx][col_idx]
    /// Deserialized and transposed from Exasol's column-major wire format.
    #[serde(deserialize_with = "super::deserialize::to_row_major")]
    pub data: Vec<Vec<serde_json::Value>>,
}

// ============================================================================
// Result Set Management Messages
// ============================================================================

/// Close result set request.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloseResultSetRequest {
    /// Command name
    pub command: String,
    /// Result set handles to close
    pub result_set_handles: Vec<i32>,
}

impl CloseResultSetRequest {
    /// Create a new close result set request.
    pub fn new(handles: Vec<i32>) -> Self {
        Self {
            command: "closeResultSet".to_string(),
            result_set_handles: handles,
        }
    }
}

/// Close result set response.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CloseResultSetResponse {
    /// Status of the response
    pub status: String,
    /// Exception information if failed
    pub exception: Option<ExceptionInfo>,
}

// ============================================================================
// Prepared Statement Messages
// ============================================================================

/// Request to create a prepared statement.
///
/// Sends SQL to the server for parsing and returns a statement handle
/// along with parameter metadata.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreatePreparedStatementRequest {
    /// Command name (always "createPreparedStatement")
    pub command: String,
    /// SQL text with parameter placeholders
    pub sql_text: String,
    /// Optional session attributes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attributes: Option<serde_json::Value>,
}

impl CreatePreparedStatementRequest {
    /// Create a new prepared statement request.
    ///
    /// # Arguments
    /// * `sql` - SQL text with parameter placeholders (?)
    pub fn new(sql: impl Into<String>) -> Self {
        Self {
            command: "createPreparedStatement".to_string(),
            sql_text: sql.into(),
            attributes: None,
        }
    }

    /// Set optional attributes.
    pub fn with_attributes(mut self, attributes: serde_json::Value) -> Self {
        self.attributes = Some(attributes);
        self
    }
}

/// Parameter metadata from prepared statement.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ParameterInfo {
    /// Parameter name (provided by Exasol in prepared statement metadata)
    #[serde(default)]
    pub name: Option<String>,
    /// Data type of the parameter
    pub data_type: DataType,
}

/// Parameter data in prepared statement response.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ParameterData {
    /// Number of parameter columns
    pub num_columns: i32,
    /// Parameter column metadata
    pub columns: Vec<ParameterInfo>,
}

/// Response from createPreparedStatement.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreatePreparedStatementResponse {
    /// Status of the response ("ok" or "error")
    pub status: String,
    /// Response data containing statement handle and metadata
    pub response_data: Option<PreparedStatementResponseData>,
    /// Exception information if failed
    pub exception: Option<ExceptionInfo>,
}

/// Response data containing statement handle and metadata.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PreparedStatementResponseData {
    /// Server-assigned statement handle
    pub statement_handle: i32,
    /// Parameter metadata (if statement has parameters)
    pub parameter_data: Option<ParameterData>,
    /// Number of results (for SELECT statements)
    pub num_results: Option<i32>,
    /// Result metadata (for SELECT statements)
    pub results: Option<Vec<ResultSetInfo>>,
}

/// Request to execute a prepared statement.
///
/// Executes a previously prepared statement with optional parameter values.
/// Parameters are provided in column-major format.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExecutePreparedStatementRequest {
    /// Command name (always "executePreparedStatement")
    pub command: String,
    /// Statement handle from createPreparedStatement
    pub statement_handle: i32,
    /// Number of parameter columns
    pub num_columns: i32,
    /// Number of rows of parameters (for batch execution)
    pub num_rows: i32,
    /// Column metadata for parameters
    #[serde(skip_serializing_if = "Option::is_none")]
    pub columns: Option<Vec<ColumnInfo>>,
    /// Parameter data in column-major format
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Vec<Vec<serde_json::Value>>>,
    /// Optional session attributes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attributes: Option<serde_json::Value>,
}

impl ExecutePreparedStatementRequest {
    /// Create a new execute prepared statement request.
    ///
    /// # Arguments
    /// * `statement_handle` - Handle from createPreparedStatement
    pub fn new(statement_handle: i32) -> Self {
        Self {
            command: "executePreparedStatement".to_string(),
            statement_handle,
            num_columns: 0,
            num_rows: 0,
            columns: None,
            data: None,
            attributes: None,
        }
    }

    /// Set parameter data for execution.
    ///
    /// # Arguments
    /// * `columns` - Column metadata describing parameter types
    /// * `data` - Parameter values in column-major format (each inner Vec is one column)
    pub fn with_data(
        mut self,
        columns: Vec<ColumnInfo>,
        data: Vec<Vec<serde_json::Value>>,
    ) -> Self {
        self.num_columns = columns.len() as i32;
        self.num_rows = if data.is_empty() {
            0
        } else {
            data[0].len() as i32
        };
        self.columns = Some(columns);
        self.data = Some(data);
        self
    }

    /// Set optional attributes.
    pub fn with_attributes(mut self, attributes: serde_json::Value) -> Self {
        self.attributes = Some(attributes);
        self
    }
}

/// Request to close a prepared statement.
///
/// Releases server-side resources associated with a prepared statement.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ClosePreparedStatementRequest {
    /// Command name (always "closePreparedStatement")
    pub command: String,
    /// Statement handle to close
    pub statement_handle: i32,
}

impl ClosePreparedStatementRequest {
    /// Create a new close prepared statement request.
    ///
    /// # Arguments
    /// * `statement_handle` - Handle from createPreparedStatement
    pub fn new(statement_handle: i32) -> Self {
        Self {
            command: "closePreparedStatement".to_string(),
            statement_handle,
        }
    }
}

/// Response from closePreparedStatement.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClosePreparedStatementResponse {
    /// Status of the response ("ok" or "error")
    pub status: String,
    /// Exception information if failed
    pub exception: Option<ExceptionInfo>,
}

// ============================================================================
// Session Management Messages
// ============================================================================

/// Disconnect request.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DisconnectRequest {
    /// Command name
    pub command: String,
}

impl DisconnectRequest {
    /// Create a new disconnect request.
    pub fn new() -> Self {
        Self {
            command: "disconnect".to_string(),
        }
    }
}

impl Default for DisconnectRequest {
    fn default() -> Self {
        Self::new()
    }
}

/// Disconnect response.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DisconnectResponse {
    /// Status of the response
    pub status: String,
    /// Exception information if failed
    pub exception: Option<ExceptionInfo>,
}

/// Set attributes request (e.g., to toggle autocommit mid-session).
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetAttributesRequest {
    /// Command name
    pub command: String,
    /// Attributes to set
    pub attributes: std::collections::HashMap<String, serde_json::Value>,
}

impl SetAttributesRequest {
    /// Create a new set attributes request.
    pub fn new(attributes: std::collections::HashMap<String, serde_json::Value>) -> Self {
        Self {
            command: "setAttributes".to_string(),
            attributes,
        }
    }

    /// Create a request to set the autocommit attribute.
    pub fn autocommit(enabled: bool) -> Self {
        let mut attributes = std::collections::HashMap::new();
        attributes.insert("autocommit".to_string(), serde_json::Value::Bool(enabled));
        Self::new(attributes)
    }
}

/// Set attributes response.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SetAttributesResponse {
    /// Status of the response
    pub status: String,
    /// Exception information if failed
    pub exception: Option<ExceptionInfo>,
}

// ============================================================================
// Common Types
// ============================================================================

/// Exception information from Exasol.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExceptionInfo {
    /// SQL code
    pub sql_code: Option<String>,
    /// Error message
    pub text: String,
}

/// Session information returned after successful login.
#[derive(Debug, Clone)]
pub struct SessionInfo {
    /// Session ID
    pub session_id: String,
    /// Protocol version
    pub protocol_version: i32,
    /// Database release version
    pub release_version: String,
    /// Database name
    pub database_name: String,
    /// Product name
    pub product_name: String,
    /// Maximum data message size in bytes
    pub max_data_message_size: i64,
    /// Time zone
    pub time_zone: Option<String>,
}

impl From<LoginResponseData> for SessionInfo {
    fn from(data: LoginResponseData) -> Self {
        Self {
            session_id: data.session_id.to_string(),
            protocol_version: data.protocol_version,
            release_version: data.release_version,
            database_name: data.database_name,
            product_name: data.product_name,
            max_data_message_size: data.max_data_message_size.unwrap_or(1024 * 1024), // 1MB default
            time_zone: data.time_zone,
        }
    }
}

/// Result set handle for fetching data.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ResultSetHandle(pub i32);

impl ResultSetHandle {
    /// Create a new result set handle.
    pub fn new(handle: i32) -> Self {
        Self(handle)
    }

    /// Get the raw handle value.
    pub fn as_i32(&self) -> i32 {
        self.0
    }
}

impl From<i32> for ResultSetHandle {
    fn from(handle: i32) -> Self {
        Self(handle)
    }
}

/// Data payload for result sets, supporting both JSON and pre-built Arrow formats.
///
/// The WebSocket transport produces `Json` payloads (row-major JSON values),
/// while the native TCP transport produces `Arrow` payloads (pre-built RecordBatch
/// directly from binary wire data, bypassing JSON entirely).
#[derive(Debug, Clone)]
pub enum ResultPayload {
    /// Row-major JSON data from WebSocket transport: `data[row_idx][col_idx]`.
    Json(Vec<Vec<serde_json::Value>>),
    /// Pre-built Arrow RecordBatch from native transport.
    Arrow(RecordBatch),
}

impl ResultPayload {
    /// Returns true if the payload contains no rows.
    pub fn is_empty(&self) -> bool {
        match self {
            ResultPayload::Json(rows) => rows.is_empty(),
            ResultPayload::Arrow(batch) => batch.num_rows() == 0,
        }
    }

    /// Returns the number of rows in the payload.
    pub fn num_rows(&self) -> usize {
        match self {
            ResultPayload::Json(rows) => rows.len(),
            ResultPayload::Arrow(batch) => batch.num_rows(),
        }
    }

    /// Returns a reference to the JSON data, if this is a JSON payload.
    pub fn as_json(&self) -> Option<&Vec<Vec<serde_json::Value>>> {
        match self {
            ResultPayload::Json(rows) => Some(rows),
            ResultPayload::Arrow(_) => None,
        }
    }

    /// Returns a reference to the RecordBatch, if this is an Arrow payload.
    pub fn as_arrow(&self) -> Option<&RecordBatch> {
        match self {
            ResultPayload::Json(_) => None,
            ResultPayload::Arrow(batch) => Some(batch),
        }
    }

    /// Consumes the payload and returns the RecordBatch, if this is an Arrow payload.
    pub fn into_arrow(self) -> Option<RecordBatch> {
        match self {
            ResultPayload::Json(_) => None,
            ResultPayload::Arrow(batch) => Some(batch),
        }
    }
}

/// Result data containing query results and metadata.
///
/// Supports both JSON payloads (from WebSocket transport) and pre-built Arrow
/// RecordBatch payloads (from native TCP transport).
#[derive(Debug, Clone)]
pub struct ResultData {
    /// Column metadata
    pub columns: Vec<ColumnInfo>,
    /// Data payload (JSON or Arrow)
    pub data: ResultPayload,
    /// Total number of rows
    pub total_rows: i64,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_login_init_request_serialization() {
        let request = LoginInitRequest::new();
        let json = serde_json::to_string(&request).unwrap();

        assert!(json.contains("\"command\":\"login\""));
        assert!(json.contains("\"protocolVersion\":3"));
        // Should NOT contain username or password
        assert!(!json.contains("username"));
        assert!(!json.contains("password"));
    }

    #[test]
    fn test_public_key_response_deserialization() {
        let json = r#"{
            "status": "ok",
            "responseData": {
                "publicKeyPem": "-----BEGIN RSA PUBLIC KEY-----\nMIIBCg...\n-----END RSA PUBLIC KEY-----",
                "publicKeyModulus": "abc123",
                "publicKeyExponent": "010001"
            }
        }"#;

        let response: PublicKeyResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.status, "ok");

        let data = response.response_data.unwrap();
        assert!(data.public_key_pem.contains("BEGIN RSA PUBLIC KEY"));
        assert_eq!(data.public_key_modulus, "abc123");
        assert_eq!(data.public_key_exponent, "010001");
    }

    #[test]
    fn test_auth_request_serialization() {
        let request = AuthRequest::new(
            "sys".to_string(),
            "encrypted_password_base64".to_string(),
            "exarrow-rs".to_string(),
        );
        let json = serde_json::to_string(&request).unwrap();

        assert!(json.contains("\"username\":\"sys\""));
        assert!(json.contains("\"password\":\"encrypted_password_base64\""));
        assert!(json.contains("\"useCompression\":false"));
        assert!(json.contains("\"clientName\":\"exarrow-rs\""));
        assert!(json.contains("\"driverName\":\"exarrow-rs\""));
        assert!(json.contains("\"clientVersion\":"));
        // Should not contain null attributes
        assert!(!json.contains("\"attributes\":null"));
    }

    #[test]
    fn test_login_request_serialization() {
        let request = LoginRequest::new("sys".to_string(), "exasol".to_string());
        let json = serde_json::to_string(&request).unwrap();

        assert!(json.contains("\"command\":\"login\""));
        assert!(json.contains("\"username\":\"sys\""));
        assert!(json.contains("\"password\":\"exasol\""));
        assert!(json.contains("\"protocolVersion\":3"));
    }

    #[test]
    fn test_execute_request_serialization() {
        let request = ExecuteRequest::new("SELECT * FROM test".to_string())
            .with_max_rows(1000)
            .with_attribute("autocommit".to_string(), "true".to_string());

        let json = serde_json::to_string(&request).unwrap();

        assert!(json.contains("\"command\":\"execute\""));
        assert!(json.contains("SELECT * FROM test"));
        assert!(json.contains("\"resultSetMaxRows\":1000"));
        assert!(json.contains("\"autocommit\""));
    }

    #[test]
    fn test_fetch_request_serialization() {
        let request = FetchRequest::new(1, 0, 1024 * 1024);
        let json = serde_json::to_string(&request).unwrap();

        assert!(json.contains("\"command\":\"fetch\""));
        assert!(json.contains("\"resultSetHandle\":1"));
        assert!(json.contains("\"startPosition\":0"));
    }

    #[test]
    fn test_disconnect_request_serialization() {
        let request = DisconnectRequest::new();
        let json = serde_json::to_string(&request).unwrap();

        assert!(json.contains("\"command\":\"disconnect\""));
    }

    #[test]
    fn test_login_response_deserialization() {
        let json = r#"{
            "status": "ok",
            "responseData": {
                "sessionId": 1234567890,
                "protocolVersion": 3,
                "releaseVersion": "8.0.0",
                "databaseName": "MYDB",
                "productName": "Exasol",
                "maxDataMessageSize": 2097152,
                "timeZone": "UTC"
            }
        }"#;

        let response: LoginResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.status, "ok");

        let data = response.response_data.unwrap();
        assert_eq!(data.session_id, 1234567890);
        assert_eq!(data.protocol_version, 3);
        assert_eq!(data.release_version, "8.0.0");
        assert_eq!(data.database_name, "MYDB");
    }

    #[test]
    fn test_execute_response_deserialization() {
        // Use the correct nested structure: results[].resultSet.{handle, columns, data}
        // Note: data is in COLUMN-MAJOR format
        let json = r#"{
            "status": "ok",
            "responseData": {
                "numResults": 1,
                "results": [
                    {
                        "resultType": "resultSet",
                        "resultSet": {
                            "resultSetHandle": 1,
                            "numColumns": 2,
                            "numRows": 2,
                            "numRowsInMessage": 2,
                            "columns": [
                                {
                                    "name": "ID",
                                    "dataType": {
                                        "type": "DECIMAL",
                                        "precision": 18,
                                        "scale": 0
                                    }
                                },
                                {
                                    "name": "NAME",
                                    "dataType": {
                                        "type": "VARCHAR",
                                        "size": 100,
                                        "characterSet": "UTF8"
                                    }
                                }
                            ],
                            "data": [
                                [1, 2],
                                ["Alice", "Bob"]
                            ]
                        }
                    }
                ]
            }
        }"#;

        let response: ExecuteResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.status, "ok");

        let data = response.response_data.unwrap();
        assert_eq!(data.num_results, 1);
        assert_eq!(data.results.len(), 1);

        let result = &data.results[0];
        assert_eq!(result.result_type, "resultSet");

        let result_set = result.result_set.as_ref().unwrap();
        assert_eq!(result_set.result_set_handle.unwrap(), 1);
        assert_eq!(result_set.num_rows.unwrap(), 2);

        let columns = result_set.columns.as_ref().unwrap();
        assert_eq!(columns.len(), 2);
        assert_eq!(columns[0].name, "ID");
        assert_eq!(columns[0].data_type.type_name, "DECIMAL");
        assert_eq!(columns[1].name, "NAME");
        assert_eq!(columns[1].data_type.type_name, "VARCHAR");

        // Column-major: 2 columns, each with 2 values
        let data_cols = result_set.data.as_ref().unwrap();
        assert_eq!(data_cols.len(), 2);
        assert_eq!(data_cols[0].len(), 2); // Column 0 has 2 rows
        assert_eq!(data_cols[1].len(), 2); // Column 1 has 2 rows
    }

    #[test]
    fn test_error_response_deserialization() {
        let json = r#"{
            "status": "error",
            "exception": {
                "sqlCode": "42000",
                "text": "Syntax error at position 10"
            }
        }"#;

        let response: ExecuteResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.status, "error");

        let exception = response.exception.unwrap();
        assert_eq!(exception.sql_code.unwrap(), "42000");
        assert!(exception.text.contains("Syntax error"));
    }

    #[test]
    fn test_result_set_handle() {
        let handle = ResultSetHandle::new(42);
        assert_eq!(handle.as_i32(), 42);

        let handle2: ResultSetHandle = 42.into();
        assert_eq!(handle, handle2);
    }

    #[test]
    fn test_session_info_from_login_response() {
        let response_data = LoginResponseData {
            session_id: 1234567890,
            protocol_version: 3,
            release_version: "8.0.0".to_string(),
            database_name: "TEST_DB".to_string(),
            product_name: "Exasol".to_string(),
            max_data_message_size: Some(5242880),
            max_varchar_size: None,
            max_identifier_length: None,
            identifier_quote_string: None,
            time_zone: Some("Europe/Berlin".to_string()),
            time_zone_behavior: None,
        };

        let session_info: SessionInfo = response_data.into();
        assert_eq!(session_info.session_id, "1234567890");
        assert_eq!(session_info.protocol_version, 3);
        assert_eq!(session_info.max_data_message_size, 5242880);
        assert_eq!(session_info.time_zone.unwrap(), "Europe/Berlin");
    }

    // ========================================================================
    // Prepared Statement Tests
    // ========================================================================

    #[test]
    fn test_create_prepared_statement_request_serialization() {
        let request = CreatePreparedStatementRequest::new("SELECT * FROM test WHERE id = ?");
        let json = serde_json::to_string(&request).unwrap();

        assert!(json.contains("\"command\":\"createPreparedStatement\""));
        assert!(json.contains("\"sqlText\":\"SELECT * FROM test WHERE id = ?\""));
        // Should not contain null attributes
        assert!(!json.contains("\"attributes\":null"));
    }

    #[test]
    fn test_create_prepared_statement_response_deserialization() {
        let json = r#"{
            "status": "ok",
            "responseData": {
                "statementHandle": 42,
                "parameterData": {
                    "numColumns": 1,
                    "columns": [
                        {
                            "dataType": {
                                "type": "DECIMAL",
                                "precision": 18,
                                "scale": 0
                            }
                        }
                    ]
                }
            }
        }"#;

        let response: CreatePreparedStatementResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.status, "ok");

        let data = response.response_data.unwrap();
        assert_eq!(data.statement_handle, 42);

        let param_data = data.parameter_data.unwrap();
        assert_eq!(param_data.num_columns, 1);
        assert_eq!(param_data.columns.len(), 1);
        assert_eq!(param_data.columns[0].data_type.type_name, "DECIMAL");
    }

    #[test]
    fn test_execute_prepared_statement_request_serialization() {
        let columns = vec![ColumnInfo {
            name: "ID".to_string(),
            data_type: DataType {
                type_name: "DECIMAL".to_string(),
                precision: Some(18),
                scale: Some(0),
                size: None,
                character_set: None,
                with_local_time_zone: None,
                fraction: None,
            },
        }];
        let data = vec![vec![serde_json::json!(1), serde_json::json!(2)]];

        let request = ExecutePreparedStatementRequest::new(42).with_data(columns, data);
        let json = serde_json::to_string(&request).unwrap();

        assert!(json.contains("\"command\":\"executePreparedStatement\""));
        assert!(json.contains("\"statementHandle\":42"));
        assert!(json.contains("\"numColumns\":1"));
        assert!(json.contains("\"numRows\":2"));
        assert!(json.contains("\"columns\""));
        assert!(json.contains("\"data\""));
    }

    #[test]
    fn test_execute_prepared_statement_request_no_params() {
        let request = ExecutePreparedStatementRequest::new(42);
        let json = serde_json::to_string(&request).unwrap();

        assert!(json.contains("\"command\":\"executePreparedStatement\""));
        assert!(json.contains("\"statementHandle\":42"));
        assert!(json.contains("\"numColumns\":0"));
        assert!(json.contains("\"numRows\":0"));
        // Should not contain null columns/data
        assert!(!json.contains("\"columns\":null"));
        assert!(!json.contains("\"data\":null"));
    }

    #[test]
    fn test_close_prepared_statement_request_serialization() {
        let request = ClosePreparedStatementRequest::new(42);
        let json = serde_json::to_string(&request).unwrap();

        assert!(json.contains("\"command\":\"closePreparedStatement\""));
        assert!(json.contains("\"statementHandle\":42"));
    }

    #[test]
    fn test_close_prepared_statement_response_deserialization() {
        let json = r#"{
            "status": "ok"
        }"#;

        let response: ClosePreparedStatementResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.status, "ok");
        assert!(response.exception.is_none());
    }

    #[test]
    fn test_close_prepared_statement_error_response() {
        let json = r#"{
            "status": "error",
            "exception": {
                "sqlCode": "00000",
                "text": "Invalid statement handle"
            }
        }"#;

        let response: ClosePreparedStatementResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.status, "error");

        let exception = response.exception.unwrap();
        assert!(exception.text.contains("Invalid statement handle"));
    }

    // ========================================================================
    // DataType Helper Tests
    // ========================================================================

    #[test]
    fn test_data_type_decimal() {
        let dt = DataType::decimal(18, 0);
        assert_eq!(dt.type_name, "DECIMAL");
        assert_eq!(dt.precision, Some(18));
        assert_eq!(dt.scale, Some(0));
        assert!(dt.size.is_none());
    }

    #[test]
    fn test_data_type_double() {
        let dt = DataType::double();
        assert_eq!(dt.type_name, "DOUBLE");
        assert!(dt.precision.is_none());
        assert!(dt.scale.is_none());
    }

    #[test]
    fn test_data_type_varchar() {
        let dt = DataType::varchar(2_000_000);
        assert_eq!(dt.type_name, "VARCHAR");
        assert_eq!(dt.size, Some(2_000_000));
        assert_eq!(dt.character_set, Some("UTF8".to_string()));
    }

    #[test]
    fn test_data_type_boolean() {
        let dt = DataType::boolean();
        assert_eq!(dt.type_name, "BOOLEAN");
    }

    #[test]
    fn test_data_type_infer_from_json_null() {
        let dt = DataType::infer_from_json(&serde_json::Value::Null);
        assert_eq!(dt.type_name, "VARCHAR");
        assert_eq!(dt.size, Some(2_000_000));
    }

    #[test]
    fn test_data_type_infer_from_json_bool() {
        let dt = DataType::infer_from_json(&serde_json::json!(true));
        assert_eq!(dt.type_name, "BOOLEAN");
    }

    #[test]
    fn test_data_type_infer_from_json_integer() {
        let dt = DataType::infer_from_json(&serde_json::json!(42));
        assert_eq!(dt.type_name, "DECIMAL");
        assert_eq!(dt.precision, Some(18));
        assert_eq!(dt.scale, Some(0));
    }

    #[test]
    fn test_data_type_infer_from_json_float() {
        let dt = DataType::infer_from_json(&serde_json::json!(3.125));
        assert_eq!(dt.type_name, "DOUBLE");
    }

    #[test]
    fn test_data_type_infer_from_json_string() {
        let dt = DataType::infer_from_json(&serde_json::json!("hello"));
        assert_eq!(dt.type_name, "VARCHAR");
        assert_eq!(dt.size, Some(2_000_000));
    }

    #[test]
    fn test_data_type_infer_from_json_array() {
        let dt = DataType::infer_from_json(&serde_json::json!([1, 2, 3]));
        assert_eq!(dt.type_name, "VARCHAR");
    }

    #[test]
    fn test_data_type_infer_from_json_object() {
        let dt = DataType::infer_from_json(&serde_json::json!({"key": "value"}));
        assert_eq!(dt.type_name, "VARCHAR");
    }
}