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
use once_cell::sync::OnceCell;
use taos_query::common::SmlData;
use taos_query::prelude::RawResult;
use taos_query::{common::RawMeta, AsyncQueryable};

pub mod asyn;
pub(crate) mod infra;
// pub mod sync;

pub use asyn::Error;
pub use asyn::ResultSet;
pub(crate) use asyn::WsTaos;
pub(crate) use infra::WsConnReq;

use crate::TaosBuilder;

#[derive(Debug)]
pub struct Taos {
    pub(crate) dsn: TaosBuilder,
    pub(crate) async_client: OnceCell<WsTaos>,
    pub(crate) async_sml: OnceCell<crate::schemaless::WsTaos>,
}

impl Taos {
    pub fn version(&self) -> &str {
        crate::block_in_place_or_global(self.client()).version()
    }

    async fn client(&self) -> &WsTaos {
        if let Some(ws) = self.async_client.get() {
            ws
        } else {
            let async_client = WsTaos::from_wsinfo(&self.dsn).await.unwrap();
            self.async_client.get_or_init(|| async_client)
        }
    }
}

unsafe impl Send for Taos {}

unsafe impl Sync for Taos {}

#[async_trait::async_trait]
impl taos_query::AsyncQueryable for Taos {
    type AsyncResultSet = asyn::ResultSet;

    async fn query<T: AsRef<str> + Send + Sync>(&self, sql: T) -> RawResult<Self::AsyncResultSet> {
        if let Some(ws) = self.async_client.get() {
            ws.s_query(sql.as_ref()).await
        } else {
            let async_client = WsTaos::from_wsinfo(&self.dsn).await?;
            self.async_client
                .get_or_init(|| async_client)
                .s_query(sql.as_ref())
                .await
        }
    }

    async fn query_with_req_id<T: AsRef<str> + Send + Sync>(
        &self,
        sql: T,
        req_id: u64,
    ) -> RawResult<Self::AsyncResultSet> {
        if let Some(ws) = self.async_client.get() {
            ws.s_query_with_req_id(sql.as_ref(), req_id).await
        } else {
            let async_client = WsTaos::from_wsinfo(&self.dsn).await?;
            self.async_client
                .get_or_init(|| async_client)
                .s_query_with_req_id(sql.as_ref(), req_id)
                .await
        }
    }

    async fn write_raw_meta(&self, raw: &RawMeta) -> RawResult<()> {
        if let Some(ws) = self.async_client.get() {
            ws.write_meta(raw).await
        } else {
            let async_client = WsTaos::from_wsinfo(&self.dsn).await?;
            self.async_client
                .get_or_init(|| async_client)
                .write_meta(raw)
                .await
        }
    }

    async fn write_raw_block(&self, block: &taos_query::RawBlock) -> RawResult<()> {
        if let Some(ws) = self.async_client.get() {
            ws.write_raw_block(block).await
        } else {
            let async_client = WsTaos::from_wsinfo(&self.dsn).await?;
            self.async_client
                .get_or_init(|| async_client)
                .write_raw_block(block)
                .await
        }
    }

    async fn write_raw_block_with_req_id(
        &self,
        block: &taos_query::RawBlock,
        req_id: u64,
    ) -> RawResult<()> {
        if let Some(ws) = self.async_client.get() {
            ws.write_raw_block_with_req_id(block, req_id).await
        } else {
            let async_client = WsTaos::from_wsinfo(&self.dsn).await?;
            self.async_client
                .get_or_init(|| async_client)
                .write_raw_block_with_req_id(block, req_id)
                .await
        }
    }

    async fn put(&self, data: &SmlData) -> RawResult<()> {
        if let Some(ws) = self.async_sml.get() {
            ws.s_put(data).await
        } else {
            let async_sml = crate::schemaless::WsTaos::from_wsinfo(&self.dsn).await?;
            self.async_sml.get_or_init(|| async_sml).s_put(data).await
        }
    }
}

impl taos_query::Queryable for Taos {
    type ResultSet = asyn::ResultSet;

    fn query<T: AsRef<str>>(&self, sql: T) -> RawResult<Self::ResultSet> {
        let sql = sql.as_ref();
        taos_query::block_in_place_or_global(<Self as AsyncQueryable>::query(self, sql))
    }

    fn query_with_req_id<T: AsRef<str>>(&self, sql: T, req_id: u64) -> RawResult<Self::ResultSet> {
        let sql = sql.as_ref();
        taos_query::block_in_place_or_global(<Self as AsyncQueryable>::query_with_req_id(
            self, sql, req_id,
        ))
    }

    fn write_raw_meta(&self, meta: &RawMeta) -> RawResult<()> {
        crate::block_in_place_or_global(<Self as AsyncQueryable>::write_raw_meta(self, meta))
    }

    fn write_raw_block(&self, block: &taos_query::RawBlock) -> RawResult<()> {
        crate::block_in_place_or_global(<Self as AsyncQueryable>::write_raw_block(self, block))
    }

    fn write_raw_block_with_req_id(
        &self,
        block: &taos_query::RawBlock,
        req_id: u64,
    ) -> RawResult<()> {
        crate::block_in_place_or_global(<Self as AsyncQueryable>::write_raw_block_with_req_id(
            self, block, req_id,
        ))
    }

    fn put(&self, sml_data: &SmlData) -> RawResult<()> {
        crate::block_in_place_or_global(<Self as AsyncQueryable>::put(self, sml_data))
    }
}

#[cfg(test)]
mod tests {
    use crate::TaosBuilder;

    #[test]
    fn ws_sync_json() -> anyhow::Result<()> {
        std::env::set_var("RUST_LOG", "debug");
        // pretty_env_logger::init();
        use taos_query::prelude::sync::*;
        let client = TaosBuilder::from_dsn("taosws://localhost:6041/")?.build()?;
        let db = "ws_sync_json";
        assert_eq!(client.exec(format!("drop database if exists {db}"))?, 0);
        assert_eq!(client.exec(format!("create database {db} keep 36500"))?, 0);
        assert_eq!(
            client.exec(
                format!("create table {db}.stb1(ts timestamp,\
                    b1 bool, c8i1 tinyint, c16i1 smallint, c32i1 int, c64i1 bigint,\
                    c8u1 tinyint unsigned, c16u1 smallint unsigned, c32u1 int unsigned, c64u1 bigint unsigned,\
                    cb1 binary(100), cn1 nchar(10),

                    b2 bool, c8i2 tinyint, c16i2 smallint, c32i2 int, c64i2 bigint,\
                    c8u2 tinyint unsigned, c16u2 smallint unsigned, c32u2 int unsigned, c64u2 bigint unsigned,\
                    cb2 binary(10), cn2 nchar(16)) tags (jt json)")
            )?,
            0
        );
        assert_eq!(
            client.exec(format!(
                r#"insert into {db}.tb1 using {db}.stb1 tags('{{"key":"数据"}}')
                   values(0,    true, -1,  -2,  -3,  -4,   1,   2,   3,   4,   'abc', '涛思',
                                false,-5,  -6,  -7,  -8,   5,   6,   7,   8,   'def', '数据')
                         (65535,NULL, NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL, NULL,  NULL,
                                NULL, NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL, NULL,  NULL)"#
            ))?,
            2
        );
        assert_eq!(
            client.exec(format!(
                r#"insert into {db}.tb2 using {db}.stb1 tags(NULL)
                   values(1,    true, -1,  -2,  -3,  -4,   1,   2,   3,   4,   'abc', '涛思',
                                false,-5,  -6,  -7,  -8,   5,   6,   7,   8,   'def', '数据')
                         (65536,NULL, NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL, NULL,  NULL,
                                NULL, NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL, NULL,  NULL)"#
            ))?,
            2
        );

        // let mut rs = client.s_query("select * from ws_sync.tb1").unwrap().unwrap();
        let mut rs = client.query(format!("select * from {db}.tb1 order by ts limit 1"))?;

        #[derive(Debug, serde::Deserialize, PartialEq, Eq)]
        #[allow(dead_code)]
        struct A {
            ts: String,
            b1: bool,
            c8i1: i8,
            c16i1: i16,
            c32i1: i32,
            c64i1: i64,
            c8u1: u8,
            c16u1: u16,
            c32u1: u32,
            c64u1: u64,

            c8i2: i8,
            c16i2: i16,
            c32i2: i32,
            c64i2: i64,
            c8u2: u8,
            c16u2: u16,
            c32u2: u32,
            c64u2: u64,

            cb1: String,
            cb2: String,
            cn1: String,
            cn2: String,
        }

        use itertools::Itertools;
        let values: Vec<A> = rs.deserialize::<A>().try_collect()?;

        dbg!(&values);

        assert_eq!(
            values[0],
            A {
                ts: "1970-01-01T08:00:00+08:00".to_string(),
                b1: true,
                c8i1: -1,
                c16i1: -2,
                c32i1: -3,
                c64i1: -4,
                c8u1: 1,
                c16u1: 2,
                c32u1: 3,
                c64u1: 4,
                c8i2: -5,
                c16i2: -6,
                c32i2: -7,
                c64i2: -8,
                c8u2: 5,
                c16u2: 6,
                c32u2: 7,
                c64u2: 8,
                cb1: "abc".to_string(),
                cb2: "def".to_string(),
                cn1: "涛思".to_string(),
                cn2: "数据".to_string(),
            }
        );

        assert_eq!(client.exec(format!("drop database {db}"))?, 0);
        Ok(())
    }

    #[test]
    fn ws_sync() -> anyhow::Result<()> {
        use taos_query::prelude::sync::*;
        let client = TaosBuilder::from_dsn("ws://localhost:6041/")?.build()?;
        assert_eq!(client.exec("drop database if exists ws_sync")?, 0);
        assert_eq!(client.exec("create database ws_sync keep 36500")?, 0);
        assert_eq!(
            client.exec(
                "create table ws_sync.tb1(ts timestamp,\
                    c8i1 tinyint, c16i1 smallint, c32i1 int, c64i1 bigint,\
                    c8u1 tinyint unsigned, c16u1 smallint unsigned, c32u1 int unsigned, c64u1 bigint unsigned,\
                    cb1 binary(100), cn1 nchar(10),

                    c8i2 tinyint, c16i2 smallint, c32i2 int, c64i2 bigint,\
                    c8u2 tinyint unsigned, c16u2 smallint unsigned, c32u2 int unsigned, c64u2 bigint unsigned,\
                    cb2 binary(10), cn2 nchar(16))"
            )?,
            0
        );
        assert_eq!(
            client.exec(
                "insert into ws_sync.tb1 values(65535,\
                -1,-2,-3,-4, 1,2,3,4, 'abc', '涛思',\
                -5,-6,-7,-8, 5,6,7,8, 'def', '数据')"
            )?,
            1
        );

        let mut rs = client.query("select * from ws_sync.tb1")?;

        #[derive(Debug, serde::Deserialize, PartialEq, Eq)]
        #[allow(dead_code)]
        struct A {
            ts: String,
            c8i1: i8,
            c16i1: i16,
            c32i1: i32,
            c64i1: i64,
            c8u1: u8,
            c16u1: u16,
            c32u1: u32,
            c64u1: u64,

            c8i2: i8,
            c16i2: i16,
            c32i2: i32,
            c64i2: i64,
            c8u2: u8,
            c16u2: u16,
            c32u2: u32,
            c64u2: u64,

            cb1: String,
            cb2: String,
            cn1: String,
            cn2: String,
        }

        use itertools::Itertools;
        let values: Vec<A> = rs.deserialize::<A>().try_collect()?;

        dbg!(&values);

        assert_eq!(
            values[0],
            A {
                ts: "1970-01-01T08:01:05.535+08:00".to_string(),
                c8i1: -1,
                c16i1: -2,
                c32i1: -3,
                c64i1: -4,
                c8u1: 1,
                c16u1: 2,
                c32u1: 3,
                c64u1: 4,
                c8i2: -5,
                c16i2: -6,
                c32i2: -7,
                c64i2: -8,
                c8u2: 5,
                c16u2: 6,
                c32u2: 7,
                c64u2: 8,
                cb1: "abc".to_string(),
                cb2: "def".to_string(),
                cn1: "涛思".to_string(),
                cn2: "数据".to_string(),
            }
        );

        assert_eq!(client.exec("drop database ws_sync")?, 0);
        Ok(())
    }

    #[test]
    fn ws_show_databases() -> anyhow::Result<()> {
        use taos_query::prelude::sync::*;
        let dsn = std::env::var("TEST_DSN").unwrap_or("taos:///".to_string());

        let client = TaosBuilder::from_dsn(dsn)?.build()?;
        let mut rs = client.query("show databases")?;
        let values = rs.to_rows_vec()?;

        dbg!(values);
        Ok(())
    }

    // #[tokio::test]
    async fn _ws_select_from_meters() -> anyhow::Result<()> {
        std::env::set_var("RUST_LOG", "info");
        // pretty_env_logger::init_timed();
        use taos_query::prelude::*;
        let dsn = "taos+ws:///test";
        let client = TaosBuilder::from_dsn(dsn)?.build().await?;

        let mut rs = client.query("select * from meters").await?;

        let mut blocks = rs.blocks();
        let mut nb = 0;
        let mut nr = 0;
        while let Some(block) = blocks.try_next().await? {
            nb += 1;
            nr += block.nrows();
        }
        let summary = rs.summary();
        dbg!(summary, (nb, nr));
        Ok(())
    }

    #[cfg(feature = "async")]
    #[tokio::test]
    async fn test_client() -> anyhow::Result<()> {
        std::env::set_var("RUST_LOG", "debug");
        // pretty_env_logger::init();
        use futures::TryStreamExt;
        use taos_query::{AsyncFetchable, AsyncQueryable};

        let client = TaosBuilder::from_dsn("ws://localhost:6041/")?.build()?;
        assert_eq!(
            client
                .exec("create database if not exists ws_test_client")
                .await?,
            0
        );
        assert_eq!(
            client
                .exec("create table if not exists ws_test_client.tb1(ts timestamp, v int)")
                .await?,
            0
        );
        assert_eq!(
            client
                .exec("insert into ws_test_client.tb1 values(1655793421375, 1)")
                .await?,
            1
        );

        // let mut rs = client.s_query("select * from ws_test_client.tb1").unwrap().unwrap();
        let mut rs = client.query("select * from ws_test_client.tb1").await?;

        #[derive(Debug, serde::Deserialize)]
        #[allow(dead_code)]
        struct A {
            ts: String,
            v: i32,
        }

        let values: Vec<A> = rs.deserialize_stream().try_collect().await?;

        dbg!(values);

        assert_eq!(client.exec("drop database ws_test_client").await?, 0);
        Ok(())
    }
}