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
use futures::{SinkExt, StreamExt};
use itertools::Itertools;
// use scc::HashMap;
use dashmap::DashMap as HashMap;

use serde::Deserialize;

use taos_query::common::views::views_to_raw_block;
use taos_query::common::{ColumnView, Precision, Ty};
use taos_query::prelude::{InlinableWrite, RawResult};
use taos_query::stmt::{AsyncBindable, Bindable};
use taos_query::{block_in_place_or_global, IntoDsn, RawBlock};

use taos_query::prelude::tokio;
use tokio::sync::{oneshot, watch};

use tokio_tungstenite::tungstenite::protocol::Message;

use crate::query::infra::ToMessage;
use crate::{Taos, TaosBuilder};
use messages::*;

use std::fmt::Debug;

use std::sync::atomic::AtomicU64;
use std::sync::Arc;
use std::time::Duration;

mod messages;

use crate::query::asyn::Error;
type StmtResult = RawResult<Option<usize>>;
type StmtSender = tokio::sync::mpsc::Sender<StmtResult>;
type StmtReceiver = tokio::sync::mpsc::Receiver<StmtResult>;

type StmtFieldResult = RawResult<Vec<StmtField>>;
type StmtFieldSender = tokio::sync::mpsc::Sender<StmtFieldResult>;
type StmtFieldReceiver = tokio::sync::mpsc::Receiver<StmtFieldResult>;

type StmtParamResult = RawResult<StmtParam>;
type StmtParamSender = tokio::sync::mpsc::Sender<StmtParamResult>;
type StmtParamReceiver = tokio::sync::mpsc::Receiver<StmtParamResult>;

type StmtUseResultResult = RawResult<StmtUseResult>;
type StmtUseSender = tokio::sync::mpsc::Sender<StmtUseResultResult>;
type StmtUseReceiver = tokio::sync::mpsc::Receiver<StmtUseResultResult>;

type WsSender = tokio::sync::mpsc::Sender<Message>;

trait ToJsonValue {
    fn to_json_value(&self) -> serde_json::Value;
}

impl ToJsonValue for ColumnView {
    fn to_json_value(&self) -> serde_json::Value {
        match self {
            ColumnView::Bool(view) => serde_json::json!(view.to_vec()),
            ColumnView::TinyInt(view) => serde_json::json!(view.to_vec()),
            ColumnView::SmallInt(view) => serde_json::json!(view.to_vec()),
            ColumnView::Int(view) => serde_json::json!(view.to_vec()),
            ColumnView::BigInt(view) => serde_json::json!(view.to_vec()),
            ColumnView::Float(view) => serde_json::json!(view.to_vec()),
            ColumnView::Double(view) => serde_json::json!(view.to_vec()),
            ColumnView::VarChar(view) => serde_json::json!(view.to_vec()),
            ColumnView::Timestamp(view) => serde_json::json!(view
                .iter()
                .map(|ts| ts.map(|ts| ts.to_datetime_with_tz()))
                .collect_vec()),
            ColumnView::NChar(view) => serde_json::json!(view.to_vec()),
            ColumnView::UTinyInt(view) => serde_json::json!(view.to_vec()),
            ColumnView::USmallInt(view) => serde_json::json!(view.to_vec()),
            ColumnView::UInt(view) => serde_json::json!(view.to_vec()),
            ColumnView::UBigInt(view) => serde_json::json!(view.to_vec()),
            ColumnView::Json(view) => serde_json::json!(view.to_vec()),
        }
    }
}

impl Bindable<super::Taos> for Stmt {
    fn init(taos: &super::Taos) -> RawResult<Self> {
        let mut dsn = taos.dsn.clone();
        let database: Option<String> = block_in_place_or_global(
            <Taos as taos_query::AsyncQueryable>::query_one(taos, "select database()"),
        )?;
        dsn.database = database;
        let mut stmt = block_in_place_or_global(Self::from_wsinfo(&dsn))?;
        crate::block_in_place_or_global(stmt.stmt_init())?;
        Ok(stmt)
    }

    fn init_with_req_id(taos: &super::Taos, req_id: u64) -> RawResult<Self> {
        let mut dsn = taos.dsn.clone();
        let database: Option<String> = block_in_place_or_global(
            <Taos as taos_query::AsyncQueryable>::query_one(taos, "select database()"),
        )?;
        dsn.database = database;
        let mut stmt = block_in_place_or_global(Self::from_wsinfo(&dsn))?;
        crate::block_in_place_or_global(stmt.taos_stmt_init_with_req_id(req_id))?;
        Ok(stmt)
    }

    fn prepare<S: AsRef<str>>(&mut self, sql: S) -> RawResult<&mut Self> {
        crate::block_in_place_or_global(self.stmt_prepare(sql.as_ref()))?;
        Ok(self)
    }

    fn set_tbname<S: AsRef<str>>(&mut self, sql: S) -> RawResult<&mut Self> {
        block_in_place_or_global(self.stmt_set_tbname(sql.as_ref()))?;
        Ok(self)
    }

    fn set_tags(&mut self, tags: &[taos_query::common::Value]) -> RawResult<&mut Self> {
        let tags = tags.iter().map(|tag| tag.to_json_value()).collect_vec();
        block_in_place_or_global(self.stmt_set_tags(tags))?;
        Ok(self)
    }

    fn bind(&mut self, params: &[taos_query::common::ColumnView]) -> RawResult<&mut Self> {
        // This json method for bind

        // let columns = params
        //     .into_iter()
        //     .map(|tag| tag.to_json_value())
        //     .collect_vec();
        // crate::block_in_place_or_global(self.stmt_bind(columns))?;

        crate::block_in_place_or_global(self.stmt_bind_block(params))?;
        Ok(self)
    }

    fn add_batch(&mut self) -> RawResult<&mut Self> {
        crate::block_in_place_or_global(self.stmt_add_batch())?;
        Ok(self)
    }

    fn execute(&mut self) -> RawResult<usize> {
        block_in_place_or_global(self.stmt_exec())
    }

    fn affected_rows(&self) -> usize {
        self.affected_rows
    }
}

#[async_trait::async_trait]
impl AsyncBindable<super::Taos> for Stmt {
    async fn init(taos: &super::Taos) -> RawResult<Self> {
        let mut dsn = taos.dsn.clone();
        let database: Option<String> =
            <Taos as taos_query::AsyncQueryable>::query_one(taos, "select database()").await?;
        dsn.database = database;
        let mut stmt = Self::from_wsinfo(&dsn).await?;
        stmt.stmt_init().await?;
        Ok(stmt)
    }

    async fn init_with_req_id(taos: &super::Taos, req_id: u64) -> RawResult<Self> {
        let mut dsn = taos.dsn.clone();
        let database: Option<String> =
            <Taos as taos_query::AsyncQueryable>::query_one(taos, "select database()").await?;
        dsn.database = database;
        let mut stmt = Self::from_wsinfo(&dsn).await?;
        stmt.taos_stmt_init_with_req_id(req_id).await?;
        Ok(stmt)
    }

    async fn prepare(&mut self, sql: &str) -> RawResult<&mut Self> {
        self.stmt_prepare(sql.as_ref()).await?;
        Ok(self)
    }

    async fn set_tbname(&mut self, sql: &str) -> RawResult<&mut Self> {
        self.stmt_set_tbname(sql.as_ref()).await?;
        Ok(self)
    }

    async fn set_tags(&mut self, tags: &[taos_query::common::Value]) -> RawResult<&mut Self> {
        let tags = tags.iter().map(|tag| tag.to_json_value()).collect_vec();
        self.stmt_set_tags(tags).await?;
        Ok(self)
    }

    async fn bind(&mut self, params: &[taos_query::common::ColumnView]) -> RawResult<&mut Self> {
        // This json method for bind

        // let columns = params
        //     .into_iter()
        //     .map(|tag| tag.to_json_value())
        //     .collect_vec();
        // crate::block_in_place_or_global(self.stmt_bind(columns))?;

        self.stmt_bind_block(params).await?;
        Ok(self)
    }

    async fn add_batch(&mut self) -> RawResult<&mut Self> {
        self.stmt_add_batch().await?;
        Ok(self)
    }

    async fn execute(&mut self) -> RawResult<usize> {
        self.stmt_exec().await
    }

    async fn affected_rows(&self) -> usize {
        self.affected_rows
    }
}

pub trait WsFieldsable {
    fn get_tag_fields(&mut self) -> RawResult<Vec<StmtField>>;

    fn get_col_fields(&mut self) -> RawResult<Vec<StmtField>>;
}

impl WsFieldsable for Stmt {
    fn get_tag_fields(&mut self) -> RawResult<Vec<StmtField>> {
        block_in_place_or_global(self.stmt_get_tag_fields())
    }

    fn get_col_fields(&mut self) -> RawResult<Vec<StmtField>> {
        block_in_place_or_global(self.stmt_get_col_fields())
    }
}

pub struct Stmt {
    req_id: Arc<AtomicU64>,
    timeout: Duration,
    ws: WsSender,
    close_signal: watch::Sender<bool>,
    queries: Arc<HashMap<ReqId, oneshot::Sender<RawResult<StmtId>>>>,
    fetches: Arc<HashMap<StmtId, StmtSender>>,
    receiver: Option<StmtReceiver>,
    args: Option<StmtArgs>,
    affected_rows: usize,
    affected_rows_once: usize,
    fields_fetches: Arc<HashMap<StmtId, StmtFieldSender>>,
    fields_receiver: Option<StmtFieldReceiver>,
    param_fetches: Arc<HashMap<StmtId, StmtParamSender>>,
    param_receiver: Option<StmtParamReceiver>,
    use_result_fetches: Arc<HashMap<StmtId, StmtUseSender>>,
    use_result_receiver: Option<StmtUseReceiver>,
}

#[repr(C)]
#[derive(Debug, Deserialize, Clone)]
pub struct StmtParam {
    pub index: i64,
    pub data_type: Ty,
    pub length: i64,
}

#[repr(C)]
#[derive(Debug, Deserialize, Clone)]
pub struct StmtUseResult {
    pub result_id: u64,
    pub fields_count: i64,
    pub fields_names: Option<Vec<String>>,
    pub fields_types: Option<Vec<Ty>>,
    pub fields_lengths: Option<Vec<u32>>,
    pub precision: Precision,
}

#[repr(C)]
#[derive(Debug, Deserialize, Clone)]
pub struct StmtField {
    pub name: String,
    pub field_type: i8,
    pub precision: u8,
    pub scale: u8,
    pub bytes: i32,
}
impl Debug for Stmt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WsClient")
            .field("req_id", &self.req_id)
            .field("...", &"...")
            .finish()
    }
}
impl Drop for Stmt {
    fn drop(&mut self) {
        // send close signal to reader/writer spawned tasks.
        let _ = self.close_signal.send(true);
    }
}

impl Stmt {
    pub(crate) async fn from_wsinfo(info: &TaosBuilder) -> RawResult<Self> {
        let ws = info.build_stream(info.to_stmt_url()).await?;

        let req_id = 0;
        let (mut sender, mut reader) = ws.split();

        let login = StmtSend::Conn {
            req_id,
            req: info.to_conn_request(),
        };
        sender.send(login.to_msg()).await.map_err(Error::from)?;
        if let Some(Ok(message)) = reader.next().await {
            match message {
                Message::Text(text) => {
                    let v: StmtRecv = serde_json::from_str(&text).unwrap();
                    match v.data {
                        StmtRecvData::Conn => (),
                        _ => unreachable!(),
                    }
                }
                _ => unreachable!(),
            }
        }

        let queries = Arc::new(HashMap::<ReqId, tokio::sync::oneshot::Sender<_>>::new());
        let fetches = Arc::new(HashMap::<StmtId, StmtSender>::new());

        let queries_sender = queries.clone();
        let fetches_sender = fetches.clone();

        let fields_fetches = Arc::new(HashMap::<StmtId, StmtFieldSender>::new());
        let fields_fetches_sender = fields_fetches.clone();

        let param_fetches = Arc::new(HashMap::<StmtId, StmtParamSender>::new());
        let param_fetches_sender = param_fetches.clone();

        let use_result_fetches = Arc::new(HashMap::<StmtId, StmtUseSender>::new());
        let use_result_fetches_sender = use_result_fetches.clone();

        let (ws, mut msg_recv) = tokio::sync::mpsc::channel(100);
        let ws2 = ws.clone();

        // Connection watcher
        let (tx, mut rx) = watch::channel(false);
        let mut close_listener = rx.clone();

        tokio::spawn(async move {
            loop {
                tokio::select! {
                    Some(msg) = msg_recv.recv() => {
                        if let Err(err) = sender.send(msg).await {
                            //
                            log::warn!("Sender error: {err:#}");
                            break;
                        }
                    }
                    _ = rx.changed() => {
                        log::trace!("close sender task");
                        break;
                    }
                }
            }
        });

        // message handler for query/fetch/fetch_block
        tokio::spawn(async move {
            loop {
                tokio::select! {
                    Some(message) = reader.next() => {
                        match message {
                            Ok(message) => match message {
                                Message::Text(text) => {
                                    log::trace!("json response: {}", text);
                                    let v: StmtRecv = serde_json::from_str(&text).unwrap();
                                    match v.ok() {
                                        StmtOk::Conn(_) => {
                                            log::warn!("[{req_id}] received connected response in message loop");
                                        },
                                        StmtOk::Init(req_id, stmt_id) => {
                                            log::trace!("stmt init done: {{ req_id: {}, stmt_id: {:?}}}", req_id, stmt_id);
                                            if let Some((_, sender)) = queries_sender.remove(&req_id)
                                            {
                                                sender.send(stmt_id).unwrap();
                                            }  else {
                                                log::trace!("Stmt init failed because req id {req_id} not exist");
                                            }
                                        }
                                        StmtOk::Stmt(stmt_id, res) => {
                                            if let Some(sender) = fetches_sender.get(&stmt_id) {
                                                log::trace!("send data to fetches with id {}", stmt_id);
                                                // let res = res.clone();
                                                sender.send(res).await.unwrap();
                                            // }) {

                                            } else {
                                                log::trace!("Got unknown stmt id: {stmt_id} with result: {res:?}");
                                            }
                                        }
                                        StmtOk::StmtFields(stmt_id, res) => {
                                            if let Some(sender) = fields_fetches_sender.get(&stmt_id) {
                                                log::trace!("send data to fetches with id {}", stmt_id);
                                                // let res = res.clone();
                                                sender.send(res).await.unwrap();

                                            } else {
                                                log::trace!("Got unknown stmt id: {stmt_id} with result: {res:?}");
                                            }
                                        }
                                        StmtOk::StmtParam(stmt_id, res) => {
                                            if let Some(sender) = param_fetches_sender.get(&stmt_id) {
                                                log::trace!("send data to fetches with id {}", stmt_id);
                                                sender.send(res).await.unwrap();
                                            } else {
                                                log::trace!("Got unknown stmt id: {stmt_id} with result: {res:?}");
                                            }
                                        }
                                        StmtOk::StmtUseResult(stmt_id, res) => {
                                            if let Some(sender) = use_result_fetches_sender.get(&stmt_id) {
                                                log::trace!("send data to fetches with id {}", stmt_id);
                                                sender.send(res).await.unwrap();
                                            } else {
                                                log::trace!("Got unknown stmt id: {stmt_id} with result: {res:?}");
                                            }
                                        }
                                    }
                                }
                                Message::Binary(_) => {
                                    log::warn!("received (unexpected) binary message, do nothing");
                                }
                                Message::Close(_) => {
                                    log::warn!("websocket connection is closed (unexpected?)");
                                    break;
                                }
                                Message::Ping(bytes) => {
                                    ws2.send(Message::Pong(bytes)).await.unwrap();
                                }
                                Message::Pong(_) => {
                                    // do nothing
                                    log::warn!("received (unexpected) pong message, do nothing");
                                }
                                Message::Frame(frame) => {
                                    // do nothing
                                    log::warn!("received (unexpected) frame message, do nothing");
                                    log::trace!("* frame data: {frame:?}");
                                }
                            },
                            Err(err) => {
                                log::trace!("receiving cause error: {err:?}");
                                break;
                            }
                        }
                    }
                    _ = close_listener.changed() => {
                        log::trace!("close reader task");
                        break
                    }
                }
            }
        });

        Ok(Self {
            req_id: Arc::new(AtomicU64::new(req_id + 1)),
            queries,
            timeout: Duration::from_secs(5),
            fetches,
            ws,
            close_signal: tx,
            receiver: None,
            args: None,
            affected_rows: 0,
            affected_rows_once: 0,
            fields_fetches,
            fields_receiver: None,
            param_fetches,
            param_receiver: None,
            use_result_fetches,
            use_result_receiver: None,
        })
    }
    /// Build TDengine websocket client from dsn.
    ///
    /// ```text
    /// ws://localhost:6041/
    /// ```
    ///
    pub async fn from_dsn(dsn: impl IntoDsn) -> RawResult<Self> {
        let info = TaosBuilder::from_dsn(dsn)?;
        Self::from_wsinfo(&info).await
    }

    fn req_id(&self) -> u64 {
        self.req_id
            .fetch_add(1, std::sync::atomic::Ordering::SeqCst)
    }

    pub async fn stmt_init(&mut self) -> RawResult<&mut Self> {
        let req_id = self.req_id();
        self.taos_stmt_init_with_req_id(req_id).await
    }

    pub async fn taos_stmt_init_with_req_id(&mut self, req_id: u64) -> RawResult<&mut Self> {
        let action = StmtSend::Init { req_id };
        let (tx, rx) = oneshot::channel();
        {
            self.queries.insert(req_id, tx);
            self.ws.send(action.to_msg()).await.map_err(Error::from)?;
        }
        let stmt_id = rx.await.map_err(Error::from)??; // 1. RecvError, 2. TaosError
        let args = StmtArgs { req_id, stmt_id };

        let (sender, receiver) = tokio::sync::mpsc::channel(2);

        let _ = self.fetches.insert(stmt_id, sender);

        self.args = Some(args);
        self.receiver = Some(receiver);

        let (fields_sender, fields_receiver) = tokio::sync::mpsc::channel(2);
        let _ = self.fields_fetches.insert(stmt_id, fields_sender);
        self.fields_receiver = Some(fields_receiver);

        let (param_sender, param_receiver) = tokio::sync::mpsc::channel(2);
        let _ = self.param_fetches.insert(stmt_id, param_sender);
        self.param_receiver = Some(param_receiver);

        let (use_result_sender, use_result_receiver) = tokio::sync::mpsc::channel(2);
        let _ = self.use_result_fetches.insert(stmt_id, use_result_sender);
        self.use_result_receiver = Some(use_result_receiver);

        Ok(self)
    }

    pub async fn s_stmt<'a>(&'a mut self, sql: &'a str) -> RawResult<&mut Self> {
        let stmt = self.stmt_init().await?;
        stmt.stmt_prepare(sql).await?;
        Ok(self)
    }

    pub fn set_timeout(&mut self, timeout: Duration) -> &mut Self {
        self.timeout = timeout;
        self
    }
    pub async fn stmt_prepare(&mut self, sql: &str) -> RawResult<()> {
        let prepare = StmtSend::Prepare {
            args: self.args.unwrap(),
            sql: sql.to_string(),
        };
        self.ws.send(prepare.to_msg()).await.map_err(Error::from)?;
        let _ = self.receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt prepare response"),
        )??;
        Ok(())
    }
    pub async fn stmt_add_batch(&mut self) -> RawResult<()> {
        log::trace!("add batch");
        let message = StmtSend::AddBatch(self.args.unwrap());
        self.ws.send(message.to_msg()).await.map_err(Error::from)?;
        let _ = self.receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt add batch response"),
        )??;
        Ok(())
    }
    pub async fn stmt_bind(&mut self, columns: Vec<serde_json::Value>) -> RawResult<()> {
        let message = StmtSend::Bind {
            args: self.args.unwrap(),
            columns,
        };
        {
            log::trace!("bind with: {message:?}");
            log::trace!("bind string: {}", message.to_msg());
            self.ws.send(message.to_msg()).await.map_err(Error::from)?;
        }
        log::trace!("begin receive");
        let _ = self.receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt bind response"),
        )??;
        Ok(())
    }

    async fn stmt_bind_block(&mut self, columns: &[ColumnView]) -> RawResult<()> {
        let args = self.args.unwrap();

        let mut bytes = Vec::new();
        // p0 uin64  req_id
        // p0+8 uint64 stmt_id
        // p0+16 uint64 (1 (set tag) 2 (bind))
        // p0+24 raw block
        bytes.write_u64_le(args.req_id).map_err(Error::from)?;
        bytes.write_u64_le(args.stmt_id).map_err(Error::from)?;
        bytes.write_u64_le(2).map_err(Error::from)?; // bind: 2

        let block = views_to_raw_block(columns);

        bytes.extend(&block);
        log::trace!("block: {:?}", block);
        // dbg!(bytes::Bytes::copy_from_slice(&block));
        log::trace!(
            "{:#?}",
            RawBlock::parse_from_raw_block(block, taos_query::prelude::Precision::Millisecond)
        );

        self.ws
            .send(Message::Binary(bytes))
            .await
            .map_err(Error::from)?;
        let _ = self.receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt bind block response"),
        )??;

        Ok(())
    }

    /// Call bind and add batch.
    pub async fn bind_all(&mut self, columns: Vec<serde_json::Value>) -> RawResult<()> {
        self.stmt_bind(columns).await?;
        self.stmt_add_batch().await
    }

    pub async fn stmt_set_tbname(&mut self, name: &str) -> RawResult<()> {
        let message = StmtSend::SetTableName {
            args: self.args.unwrap(),
            name: name.to_string(),
        };
        self.ws
            .send_timeout(message.to_msg(), self.timeout)
            .await
            .map_err(Error::from)?;
        let _ = self.receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt set_tbname response"),
        )??;
        Ok(())
    }

    pub async fn stmt_set_tags(&mut self, tags: Vec<serde_json::Value>) -> RawResult<()> {
        let message = StmtSend::SetTags {
            args: self.args.unwrap(),
            tags,
        };
        self.ws
            .send_timeout(message.to_msg(), self.timeout)
            .await
            .map_err(Error::from)?;
        let _ = self.receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt set_tags response"),
        )??;
        Ok(())
    }

    pub async fn stmt_exec(&mut self) -> RawResult<usize> {
        log::trace!("exec");
        let message = StmtSend::Exec(self.args.unwrap());
        self.ws
            .send_timeout(message.to_msg(), self.timeout)
            .await
            .map_err(Error::from)?;
        if let Some(affected) = self.receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt exec response"),
        )?? {
            self.affected_rows += affected;
            self.affected_rows_once = affected;
            Ok(affected)
        } else {
            panic!("")
        }
    }

    pub async fn stmt_get_tag_fields(&mut self) -> RawResult<Vec<StmtField>> {
        log::trace!("get tag fields");
        let message = StmtSend::GetTagFields(self.args.unwrap());
        self.ws
            .send_timeout(message.to_msg(), self.timeout)
            .await
            .map_err(Error::from)?;
        let fields = self.fields_receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt get_tag_fields response"),
        )??;
        Ok(fields)
    }

    pub async fn stmt_get_col_fields(&mut self) -> RawResult<Vec<StmtField>> {
        log::trace!("get col fields");
        let message = StmtSend::GetColFields(self.args.unwrap());
        self.ws
            .send_timeout(message.to_msg(), self.timeout)
            .await
            .map_err(Error::from)?;
        let fields = self.fields_receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt get_col_fields response"),
        )??;
        Ok(fields)
    }

    pub fn affected_rows_once(&self) -> usize {
        self.affected_rows_once
    }

    pub fn s_num_params(&mut self) -> RawResult<usize> {
        block_in_place_or_global(self.stmt_num_params())
    }

    pub fn s_get_param(&mut self, index: i64) -> RawResult<StmtParam> {
        block_in_place_or_global(self.stmt_get_param(index))
    }

    pub fn s_use_result(&mut self) -> RawResult<StmtUseResult> {
        block_in_place_or_global(self.use_result())
    }

    pub async fn use_result(&mut self) -> RawResult<StmtUseResult> {
        let message = StmtSend::UseResult(self.args.unwrap());
        log::trace!("use result message: {:#?}", &message);
        self.ws
            .send_timeout(message.to_msg(), self.timeout)
            .await
            .map_err(Error::from)?;
        let use_result = self
            .use_result_receiver
            .as_mut()
            .unwrap()
            .recv()
            .await
            .ok_or(taos_query::RawError::from_string(
                "Can't receive stmt use_result response",
            ))??;
        Ok(use_result)
    }

    pub async fn stmt_num_params(&mut self) -> RawResult<usize> {
        let message = StmtSend::StmtNumParams(self.args.unwrap());
        self.ws
            .send_timeout(message.to_msg(), self.timeout)
            .await
            .map_err(Error::from)?;
        let num_params = self.receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt num_params response"),
        )??;
        match num_params {
            Some(num_params) => Ok(num_params),
            None => Err(taos_query::RawError::from_string(
                "Can't receive stmt num_params response",
            )),
        }
    }

    pub async fn stmt_get_param(&mut self, index: i64) -> RawResult<StmtParam> {
        let message = StmtSend::StmtGetParam {
            args: self.args.unwrap(),
            index,
        };
        self.ws
            .send_timeout(message.to_msg(), self.timeout)
            .await
            .map_err(Error::from)?;
        let param = self.param_receiver.as_mut().unwrap().recv().await.ok_or(
            taos_query::RawError::from_string("Can't receive stmt get_param response"),
        )??;
        Ok(param)
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;
    use taos_query::{common::ColumnView, Dsn, TBuilder};

    use crate::{stmt::Stmt, TaosBuilder};

    use taos_query::prelude::tokio;

    #[tokio::test()]
    async fn test_client() -> anyhow::Result<()> {
        use taos_query::AsyncQueryable;

        let taos = TaosBuilder::from_dsn("taos://localhost:6041")?.build()?;
        taos.exec("drop database if exists stmt").await?;
        taos.exec("create database stmt").await?;
        taos.exec("create table stmt.ctb (ts timestamp, v int)")
            .await?;

        std::env::set_var("RUST_LOG", "debug");
        // pretty_env_logger::init();
        let mut client = Stmt::from_dsn("taos+ws://localhost:6041/stmt").await?;
        let stmt = client.s_stmt("insert into stmt.ctb values(?, ?)").await?;

        stmt.bind_all(vec![
            json!([
                "2022-06-07T11:02:44.022450088+08:00",
                "2022-06-07T11:02:45.022450088+08:00"
            ]),
            json!([2, 3]),
        ])
        .await?;
        let res = stmt.stmt_exec().await?;

        assert_eq!(res, 2);
        Ok(())
    }

    #[tokio::test()]
    async fn test_stmt_stable_with_json() -> anyhow::Result<()> {
        use taos_query::AsyncQueryable;

        let dsn = Dsn::try_from("taos://localhost:6041")?;
        dbg!(&dsn);

        let taos = TaosBuilder::from_dsn("taos://localhost:6041")?.build()?;
        taos.exec("drop database if exists stmt_sj").await?;
        taos.exec("create database stmt_sj").await?;
        taos.exec("create table stmt_sj.stb (ts timestamp, v int) tags(tj json)")
            .await?;

        std::env::set_var("RUST_LOG", "debug");
        // pretty_env_logger::init();
        let mut client = Stmt::from_dsn("taos+ws://localhost:6041/stmt_sj").await?;
        let stmt = client
            .s_stmt("insert into ? using stb tags(?) values(?, ?)")
            .await?;

        stmt.stmt_set_tbname("tb1").await?;

        stmt.stmt_set_tags(vec![json!(r#"{"name": "value"}"#)])
            .await?;

        stmt.bind_all(vec![
            json!([
                "2022-06-07T11:02:44.022450088+08:00",
                "2022-06-07T11:02:45.022450088+08:00"
            ]),
            json!([2, 3]),
        ])
        .await?;
        let res = stmt.stmt_exec().await?;

        assert_eq!(res, 2);
        let row: (String, i32, std::collections::HashMap<String, String>) =
            taos.query_one("select * from stmt_sj.stb").await?.unwrap();
        dbg!(row);
        taos.exec("drop database stmt_sj").await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_stmt_get_tag_and_col_fields() -> anyhow::Result<()> {
        use taos_query::AsyncQueryable;

        let dsn = Dsn::try_from("taos://localhost:6041")?;
        dbg!(&dsn);

        let taos = TaosBuilder::from_dsn("taos://localhost:6041")?.build()?;
        taos.exec("drop database if exists ws_stmt_sj2").await?;
        taos.exec("create database ws_stmt_sj2").await?;
        taos.exec("create table ws_stmt_sj2.stb (ts timestamp, v int) tags(tj json)")
            .await?;

        std::env::set_var("RUST_LOG", "trace");
        // pretty_env_logger::init();
        let mut client = Stmt::from_dsn("taos+ws://localhost:6041/ws_stmt_sj2").await?;
        let stmt = client
            .s_stmt("insert into ? using stb tags(?) values(?, ?)")
            .await?;

        let tag_fields = stmt.stmt_get_tag_fields().await;
        if let Err(err) = tag_fields {
            log::error!("tag fields error: {:?}", err);
        } else {
            log::debug!("tag fields: {:?}", tag_fields);
        }

        stmt.stmt_set_tbname("tb1").await?;

        stmt.stmt_set_tags(vec![json!(r#"{"name": "value"}"#)])
            .await?;

        stmt.bind_all(vec![
            json!([
                "2022-06-07T11:02:44.022450088+08:00",
                "2022-06-07T11:02:45.022450088+08:00"
            ]),
            json!([2, 3]),
        ])
        .await?;
        let res = stmt.stmt_exec().await?;

        assert_eq!(res, 2);
        let row: (String, i32, std::collections::HashMap<String, String>) = taos
            .query_one("select * from ws_stmt_sj2.stb")
            .await?
            .unwrap();
        dbg!(row);

        let tag_fields = stmt.stmt_get_tag_fields().await?;

        log::debug!("tag fields: {:?}", tag_fields);

        let col_fields = stmt.stmt_get_col_fields().await?;

        log::debug!("col fields: {:?}", col_fields);

        taos.exec("drop database ws_stmt_sj2").await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_stmt_use_result() -> anyhow::Result<()> {
        use taos_query::AsyncQueryable;

        let dsn = Dsn::try_from("taos://localhost:6041")?;

        let db = "ws_stmt_use_result";

        let taos = TaosBuilder::from_dsn(&dsn)?.build()?;
        taos.exec(format!("drop database if exists {db}")).await?;
        taos.exec(format!("create database {db}")).await?;
        taos.exec(format!(
            "create table {db}.stb (ts timestamp, v int) tags(utntag TINYINT UNSIGNED)"
        ))
        .await?;
        taos.exec(format!("use {db}")).await?;
        taos.exec("create table t1 using stb tags(0)").await?;
        taos.exec("insert into t1 values(1640000000000, 0)").await?;

        std::env::set_var("RUST_LOG", "debug");
        // only init for debug
        // pretty_env_logger::init();
        let mut client = Stmt::from_dsn(format!("{dsn}/{db}", dsn = &dsn)).await?;
        let stmt = client.s_stmt("select * from t1 where v < ?").await?;

        let params = vec![ColumnView::from_ints(vec![10])];
        stmt.stmt_bind_block(&params).await?;

        let res = stmt.stmt_exec().await?;

        dbg!(res);

        assert_eq!(res, 0);

        let res = stmt.use_result().await;

        log::debug!("use result: {:?}", res);

        taos.exec(format!("drop database {db}")).await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_stmt_use_result_usage_error() -> anyhow::Result<()> {
        use taos_query::AsyncQueryable;

        let dsn = Dsn::try_from("taos://localhost:6041")?;

        let db = "ws_stmt_use_result_usage_error";

        let taos = TaosBuilder::from_dsn(&dsn)?.build()?;
        taos.exec(format!("drop database if exists {db}")).await?;
        taos.exec(format!("create database {db}")).await?;
        taos.exec(format!(
            "create table {db}.stb (ts timestamp, v int) tags(utntag TINYINT UNSIGNED)"
        ))
        .await?;
        taos.exec(format!("use {db}")).await?;
        taos.exec("create table t1 using stb tags(0)").await?;
        taos.exec("insert into t1 values(1640000000000, 0)").await?;

        std::env::set_var("RUST_LOG", "debug");
        // only init for debug
        // pretty_env_logger::init();
        let mut client = Stmt::from_dsn(format!("{dsn}/{db}", dsn = &dsn)).await?;
        let stmt = client
            .s_stmt("insert into ? using stb tags(?) values(?, ?)")
            .await?;

        stmt.stmt_set_tbname("tb1").await?;

        stmt.stmt_set_tags(vec![json!(1)])
            .await?;

        stmt.bind_all(vec![
            json!([
                "2022-06-07T11:02:44.022450088+08:00",
                "2022-06-07T11:02:45.022450088+08:00"
            ]),
            json!([2, 3]),
        ])
        .await?;

        let res = stmt.stmt_exec().await?;

        dbg!(res);

        assert_eq!(res, 2);

        let res = stmt.use_result().await;

        log::debug!("use result: {:?}", res);

        assert!(res.is_err());

        taos.exec(format!("drop database {db}")).await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_stmt_num_params_and_get_param() -> anyhow::Result<()> {
        use taos_query::AsyncQueryable;

        let dsn = Dsn::try_from("taos://localhost:6041")?;

        let db = "ws_stmt_num_params";

        let taos = TaosBuilder::from_dsn(&dsn)?.build()?;
        taos.exec(format!("drop database if exists {db}")).await?;
        taos.exec(format!("create database {db}")).await?;
        taos.exec(format!(
            "create table {db}.stb (ts timestamp, v int) tags(tj json)"
        ))
        .await?;

        std::env::set_var("RUST_LOG", "debug");
        // only init for debug
        // pretty_env_logger::init();
        let mut client = Stmt::from_dsn(format!("{dsn}/{db}", dsn = &dsn)).await?;
        let stmt = client
            .s_stmt("insert into ? using stb tags(?) values(?, ?)")
            .await?;

        stmt.stmt_set_tbname("tb1").await?;

        stmt.stmt_set_tags(vec![json!(r#"{"name": "value"}"#)])
            .await?;

        stmt.bind_all(vec![
            json!([
                "2022-06-07T11:02:44.022450088+08:00",
                "2022-06-07T11:02:45.022450088+08:00"
            ]),
            json!([2, 3]),
        ])
        .await?;
        let res = stmt.stmt_exec().await?;

        assert_eq!(res, 2);
        let row: (String, i32, std::collections::HashMap<String, String>) = taos
            .query_one(format!("select * from {db}.stb"))
            .await?
            .unwrap();
        dbg!(row);

        let num_params = stmt.stmt_num_params().await?;

        log::debug!("stmt num params: {:?}", num_params);

        for i in 0..num_params {
            let param = stmt.stmt_get_param(i as i64).await?;
            log::debug!("param {}: {:?}", i, param);
        }

        taos.exec(format!("drop database {db}")).await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_stmt_stable() -> anyhow::Result<()> {
        use taos_query::AsyncQueryable;

        let dsn = Dsn::try_from("taos://localhost:6041")?;
        dbg!(&dsn);

        let taos = TaosBuilder::from_dsn("taos://localhost:6041")?.build()?;
        taos.exec("drop database if exists stmt_s").await?;
        taos.exec("create database stmt_s").await?;
        taos.exec("create table stmt_s.stb (ts timestamp, v int) tags(t1 int)")
            .await?;

        std::env::set_var("RUST_LOG", "debug");
        // pretty_env_logger::init();
        let mut client = Stmt::from_dsn("taos+ws://localhost:6041/stmt_s").await?;
        let stmt = client
            .s_stmt("insert into ? using stb tags(?) values(?, ?)")
            .await?;

        stmt.stmt_set_tbname("tb1").await?;

        // stmt.set_tags(vec![json!({"name": "value"})]).await?;

        stmt.stmt_set_tags(vec![json!(1)]).await?;

        stmt.bind_all(vec![
            json!([
                "2022-06-07T11:02:44.022450088+08:00",
                "2022-06-07T11:02:45.022450088+08:00"
            ]),
            json!([2, 3]),
        ])
        .await?;
        let res = stmt.stmt_exec().await?;

        assert_eq!(res, 2);
        taos.exec("drop database stmt_s").await?;
        Ok(())
    }
}