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
use crate::bindings::*;
use crate::*;

use std::ffi::CStr;
use std::os::raw::c_void;

mod bind;
pub use bind::{BindParam, IntoBindParam};

pub trait IntoParams {
    fn into_params(self) -> Vec<BindParam>;
}

impl<F: IntoBindParam, T: IntoIterator<Item = F>> IntoParams for T {
    fn into_params(self) -> Vec<BindParam> {
        self.into_iter().map(|v| v.into_bind_param()).collect()
    }
}

pub struct Stmt {
    taos: *mut c_void,
    stmt: *mut c_void,
}

impl Stmt {
    fn err_or(&self, res: i32) -> Result<(), TaosError> {
        if res != 0 {
            let code: TaosCode = (res & 0x0000ffff).into();
            let err = unsafe { taos_stmt_errstr(self.stmt) };
            if !err.is_null() {
                let err = unsafe { CStr::from_ptr(err as _) }
                    .to_string_lossy()
                    .to_owned();
                trace!("stmt error: {:?}", err);
                return Err(TaosError { code, err });
            }
            return Err(TaosError {
                code,
                err: "unknown".into(),
            });
        }
        Ok(())
    }
    /// NOT a public method
    fn prepare(&mut self, sql: impl ToCString) -> Result<(), TaosError> {
        let res = unsafe { taos_stmt_prepare(self.stmt, sql.to_c_string().as_ptr(), 0) };
        self.err_or(res)
    }
    pub fn execute(&self) -> Result<(), TaosError> {
        unsafe {
            let res = taos_stmt_execute(self.stmt);
            let res = self.err_or(res);

            if let Err(TaosError { code, err }) = res {
                if code == TaosCode::RpcFqdnError {
                    let res = taos_query(self.taos, b"select server_version()\0" as *const u8 as _);
                    let errno = taos_errno(res);
                    if errno == 0 {
                        return Ok(());
                    } else {
                        Err(TaosError { code, err })
                    }
                } else {
                    Err(TaosError { code, err })
                }
            } else {
                Ok(())
            }
        }
    }

    /// To bind one row with params
    pub fn bind(&mut self, params: impl IntoParams) -> Result<(), TaosError> {
        let params = params.into_params();
        //assert_eq!(self.num_params(), params.len());
        unsafe {
            let res = taos_stmt_bind_param(self.stmt, params.as_ptr() as _);
            self.err_or(res)?;
            let res = taos_stmt_add_batch(self.stmt);
            self.err_or(res)?;
        }
        Ok(())
    }

    /// Bind params for one record.
    pub fn bind_inplace(&mut self, params: &[BindParam]) -> Result<(), TaosError> {
        unsafe {
            let res = taos_stmt_bind_param(self.stmt, params.as_ptr() as _);
            self.err_or(res)?;
            let res = taos_stmt_add_batch(self.stmt);
            self.err_or(res)
        }
    }

    pub fn bind_batch_at_col<T>(&mut self, _params: T) -> Result<(), TaosError>
    where
        T: IntoIterator,
        T::Item: IntoBindParam,
    {
        Ok(())
    }

    pub fn num_params(&self) -> usize {
        unsafe {
            let mut num = 0;
            taos_stmt_num_params(self.stmt, &mut num as _);
            num as _
        }
    }
    pub fn set_tbname_tags(
        &mut self,
        tbname: impl ToCString,
        tags: impl IntoParams,
    ) -> Result<(), TaosError> {
        let tags = tags.into_params();
        unsafe {
            let res = taos_stmt_set_tbname_tags(
                self.stmt,
                tbname.to_c_string().as_ptr(),
                tags.as_ptr() as _,
            );
            self.err_or(res)
        }
    }
    pub fn set_tbname(&mut self, tbname: impl ToCString) -> Result<(), TaosError> {
        unsafe {
            let res = taos_stmt_set_tbname(self.stmt, tbname.to_c_string().as_ptr());
            self.err_or(res)
        }
    }
    pub fn set_sub_tbname(&mut self, tbname: impl ToCString) -> Result<(), TaosError> {
        unsafe {
            let res = taos_stmt_set_sub_tbname(self.stmt, tbname.to_c_string().as_ptr());
            self.err_or(res)
        }
    }
    pub fn is_insert(&self) -> bool {
        unsafe {
            let mut res = 0;
            taos_stmt_is_insert(self.stmt, &mut res as _);
            res != 0
        }
    }
    pub fn use_result(&self) -> Result<TaosQueryData, TaosError> {
        Ok(CTaosResult::new(unsafe { taos_stmt_use_result(self.stmt) })?.fetch_fields())
    }
    fn close(&mut self) {
        unsafe {
            taos_stmt_close(self.stmt);
        }
    }
}

impl Drop for Stmt {
    fn drop(&mut self) {
        self.close();
    }
}

impl Taos {
    /// Create stmt with sql
    pub fn stmt(&self, sql: impl ToCString) -> Result<Stmt, TaosError> {
        let sql = sql.to_c_string();
        unsafe {
            let stmt = taos_stmt_init(self.as_raw());
            // let res = taos_stmt_prepare(stmt, sql.as_ptr(), 0);
            let mut stmt = Stmt {
                taos: self.as_raw(),
                stmt,
            };
            stmt.prepare(sql)?;
            Ok(stmt)
        }
    }
}

#[cfg(test)]
mod test {
    use crate::test::taos;
    use crate::*;
    use proc_test_catalog::test_catalogue;

    async fn stmt_test(db: &str, ty: &str, value: Field) -> Result<(), Error> {
        let taos = taos()?;
        println!("test {} using {}", ty, db);
        taos.exec(format!("drop database if exists {}", db)).await?;
        taos.exec(format!("create database if not exists {} keep 36500", db))
            .await?;
        taos.exec(format!("use {}", db)).await?;
        taos.exec(format!(
            "create table if not exists tb0 (ts timestamp, n {})",
            ty
        ))
        .await?;
        let mut stmt = taos.stmt("insert into ? values(?,?)")?;
        stmt.set_tbname("tb0")?;
        assert!(stmt.is_insert());
        assert_eq!(stmt.num_params(), 2);
        let ts = Field::Timestamp(Timestamp::now());
        stmt.bind(vec![ts, value.clone()].iter())?;
        let _ = stmt.execute()?;
        let _ = taos.query("select n from tb0").await?;
        //assert_eq!(value, res.rows[0][0]);
        taos.exec(format!("drop database {}", db)).await?;
        Ok(())
    }

    macro_rules! _test_column_null {
        ($ty:ty, $v:expr) => {
            paste::paste! {
                #[tokio::test]
                #[test_catalogue()]
                #[doc = "Test bind null to type " $ty]
                async fn [<null_ $ty:snake>]() -> Result<(), Error> {
                    let db = stdext::function_name!()
                        .replace("::{{closure}}", "")
                        .replace("::", "_");
                    stmt_test(&db, $v, Field::Null).await
                }
            }
        };
    }
    _test_column_null!(bool, "bool");
    _test_column_null!(tinyint, "tinyint");
    _test_column_null!(smallint, "smallint");
    _test_column_null!(int, "int");
    _test_column_null!(bigint, "bigint");
    _test_column_null!(utinyint, "tinyint unsigned");
    _test_column_null!(usmallint, "smallint unsigned");
    _test_column_null!(uint, "int unsigned");
    _test_column_null!(ubigint, "bigint unsigned");
    _test_column_null!(timestamp, "timestamp");
    _test_column_null!(float, "float");
    _test_column_null!(double, "double");
    _test_column_null!(binary, "binary(10)");
    _test_column_null!(nchar, "nchar(10)");
    #[should_panic]
    #[tokio::test]
    #[test_catalogue()]
    /// Test bind null to json tag, should panic because json is not supported in cols
    async fn null_json() -> () {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "json", Field::Null).await.unwrap()
    }

    async fn stmt_tag(db: &str, ty: &str, tag: Field) -> Result<(), Error> {
        let taos = taos()?;
        println!("test {} using {}", ty, db);
        taos.exec(format!("drop database if exists {}", db)).await?;
        taos.exec(format!("create database if not exists {} keep 36500", db))
            .await?;
        taos.exec(format!("use {}", db)).await?;
        taos.exec(format!(
            "create table if not exists stb0 (ts timestamp, v int) tags (n {})",
            ty
        ))
        .await?;
        println!("start stmt");
        let mut stmt = taos.stmt("insert into ? using stb0 tags(?) values(?,?)")?;
        println!("set tags");
        stmt.set_tbname_tags("tb0", [&tag])?;
        assert!(stmt.is_insert());
        assert_eq!(stmt.num_params(), 2);
        let ts = Field::Timestamp(Timestamp::now());
        println!("bind stmt");
        stmt.bind(vec![ts, Field::Null].iter())?;
        println!("execute");
        stmt.execute()?;
        println!("execute stmt done");
        let res = taos.query("select n from tb0").await?;
        assert_eq!(tag, res.rows[0][0]);
        taos.exec(format!("drop database {}", db)).await?;
        Ok(())
    }
    macro_rules! _test_tag_null {
        ($ty:ty, $v:expr) => {
            paste::paste! {
                #[tokio::test]
                #[test_catalogue()]
                #[doc = "Test bind null to type " $ty]
                async fn [<null_tag_ $ty:snake>]() -> Result<(), Error> {
                    let db = stdext::function_name!()
                        .replace("::{{closure}}", "")
                        .replace("::", "_")
                        .replace("libtaos_", "");
                    stmt_tag(&db, $v, Field::Null).await
                }
            }
        };
    }
    _test_tag_null!(bool, "bool");
    _test_tag_null!(tinyint, "tinyint");
    _test_tag_null!(smallint, "smallint");
    _test_tag_null!(int, "int");
    _test_tag_null!(bigint, "bigint");
    _test_tag_null!(utinyint, "tinyint unsigned");
    _test_tag_null!(usmallint, "smallint unsigned");
    _test_tag_null!(uint, "int unsigned");
    _test_tag_null!(ubigint, "bigint unsigned");
    _test_tag_null!(timestamp, "timestamp");
    _test_tag_null!(float, "float");
    _test_tag_null!(double, "double");
    _test_tag_null!(binary, "binary(10)");
    _test_tag_null!(nchar, "nchar(10)");
    // set null in json tag is currently abort taosd. see TD-12452.
    // TD-12452 is fixed by https://github.com/taosdata/TDengine/pull/9317
    _test_tag_null!(json, "json");
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with bool values.
    async fn bool() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "bool", Field::Bool(true)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with tiny int values.
    async fn tinyint() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "tinyint", Field::TinyInt(-0x7f)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with small int values.
    async fn smallint() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "smallint", Field::SmallInt(0x7fff)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with int values.
    async fn int() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "int", Field::Int(0x7fffffff)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with bigint values.
    async fn bigint() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "bigint", Field::BigInt(0x7fffffff_ffffffff)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with unsigned tinyint values.
    async fn utinyint() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "tinyint unsigned", Field::UTinyInt(0)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with unsigned smallint values.
    async fn usmallint() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "smallint unsigned", Field::USmallInt(1)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with unsigned int values.
    async fn uint() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "int unsigned", Field::UInt(1)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with unsigned bigint values.
    async fn ubigint() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "bigint unsigned", Field::UBigInt(1)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with float values.
    async fn float() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "float", Field::Float(1.0)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with double values.
    async fn double() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        stmt_test(&db, "double", Field::Double(1.0)).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with binary values.
    async fn binary() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        let v = Field::Binary("0123456789".into());
        stmt_test(&db, "binary(10)", v).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with nchar(unicode) values.
    async fn nchar() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        let v = Field::NChar("一二三四五六七八九十".into());
        stmt_test(&db, "nchar(10)", v).await
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with json values.
    async fn json() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        let v = Field::Json(serde_json::from_str("{\"tag1\":\"一二三四五六七八九十\"}").unwrap());

        let taos = taos()?;
        println!("test json using {}", db);
        taos.exec(format!("drop database if exists {}", db)).await?;
        taos.exec(format!("create database if not exists {} keep 36500", db))
            .await?;
        taos.exec(format!("use {}", db)).await?;
        taos.exec("create stable if not exists stb0 (ts timestamp, n int) tags(j json)")
            .await?;
        let mut stmt = taos.stmt("insert into ? using stb0 tags(?) values(?,?)")?;
        println!("set tbname with tags");
        stmt.set_tbname_tags("tb0", [&v])?;
        println!("bind values");
        assert!(stmt.is_insert());
        assert_eq!(stmt.num_params(), 2);
        let ts = Field::Timestamp(Timestamp::now());
        stmt.bind(vec![ts, Field::Int(3)].iter())?;
        let _ = stmt.execute()?;
        let res = taos.query("select j from stb0").await?;
        let row = res.rows.iter().next().unwrap();
        assert_eq!(&v, row.iter().next().unwrap());
        taos.exec(format!("drop database {}", db)).await?;
        Ok(())
    }

    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT inserting with all types of values.
    async fn all_types() -> Result<(), Error> {
        let taos = taos()?;
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        println!("{}", db);
        taos.exec(format!("drop database if exists {}", db)).await?;
        taos.exec(format!("create database if not exists {} keep 36500", db))
            .await?;
        taos.exec(format!("use {}", db)).await?;
        taos.exec(
            "create table if not exists tb0 (ts timestamp,
             c1 tinyint, c2 smallint, c3 int, c4 bigint,
             c5 tinyint unsigned, c6 smallint unsigned, c7 int unsigned, c8 bigint unsigned,
             c9 float, c10 double, c11 binary(10), c12 nchar(10))",
        )
        .await?;
        let mut stmt = taos.stmt("insert into ? values(?,?,?,?,?,?,?,?,?,?,?,?,?)")?;
        stmt.set_tbname("tb0")?;
        assert!(stmt.is_insert());

        assert_eq!(stmt.num_params(), 13);
        let ts = Field::Timestamp(Timestamp::now());
        let c1 = Field::TinyInt(1);
        let c2 = Field::SmallInt(2);
        let c3 = Field::Int(3);
        let c4 = Field::BigInt(4);
        let c5 = Field::UTinyInt(5);
        let c6 = Field::USmallInt(6);
        let c7 = Field::UInt(7);
        let c8 = Field::UBigInt(8);
        let c9 = Field::Float(9.0);
        let c10 = Field::Double(9.0);
        let c11 = Field::Binary("binary".into());
        let c12 = Field::NChar("nchar".into());
        stmt.bind(vec![ts, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12].iter())?;
        let _ = stmt.execute()?;
        let res = taos.query("select count(*) as count from tb0").await?;
        println!("{:?}", res);
        taos.exec(format!("drop database {}", db)).await?;
        Ok(())
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT set tbname with upper-case stable, see jira TD-12977
    async fn test_uppercase_tbname() -> Result<(), Error> {
        let db = "uppercase_test";
        let taos = taos()?;
        taos.exec(format!("drop database if exists {db}")).await?;
        taos.exec(format!("create database {db}")).await?;
        taos.exec(format!("use {db}")).await?;
        taos.exec(format!(
            "create stable STB(ts timestamp, n int) tags(b int)"
        ))
        .await?;
        let mut stmt = taos.stmt("insert into ? using STB tags(?) values(?, ?)")?;

        stmt.set_tbname_tags("tb0", [0i32])?;
        // stmt.bind(&[0i32])?;
        let values = vec![Field::Timestamp(Timestamp::now()), Field::Int(10)];
        stmt.bind(&values)?;

        assert!(stmt.is_insert());
        assert_eq!(stmt.num_params(), 2);

        let _ = stmt.execute()?;
        const LIMIT: i64 = 100;

        for i in 1..LIMIT {
            stmt.set_tbname_tags(format!("tb{}", i), &[2i32])?;
            stmt.bind(&values)?;
        }
        let _ = stmt.execute()?;
        let _res = taos.query("select count(*) as count from stb").await?;
        //assert_eq!(res.rows[0][0], Field::BigInt(LIMIT));
        taos.exec(format!("drop database {}", db)).await?;

        Ok(())
    }
    #[tokio::test]
    #[test_catalogue()]
    /// Test STMT API insertion with tags
    async fn test_stmt_tags() -> Result<(), Error> {
        let db = stdext::function_name!()
            .replace("::{{closure}}", "")
            .replace("::", "_");
        println!("{:?}", db);
        let taos = taos()?;
        taos.exec(format!("drop database if exists {}", db)).await?;
        taos.exec(format!("create database if not exists {} keep 36500", db))
            .await?;
        taos.exec(format!("use {}", db)).await?;
        taos.exec("create table if not exists stb (ts timestamp, n int) tags(b int)")
            .await?;

        let mut stmt = taos.stmt("insert into ? using stb tags(?) values(?, ?)")?;

        stmt.set_tbname_tags("tb0", [0i32])?;
        // stmt.bind(&[0i32])?;
        let values = vec![Field::Timestamp(Timestamp::now()), Field::Int(10)];
        stmt.bind(&values)?;

        assert!(stmt.is_insert());
        assert_eq!(stmt.num_params(), 2);

        let _ = stmt.execute()?;
        const LIMIT: i64 = 100;

        for i in 1..LIMIT {
            stmt.set_tbname_tags(format!("tb{}", i), &[2i32])?;
            stmt.bind(&values)?;
        }
        let _ = stmt.execute()?;
        let stmt = taos.stmt("select count(*) as count from stb")?;
        stmt.execute()?;
        let _ = stmt.use_result()?;
        taos.exec(format!("drop database {}", db)).await?;
        Ok(())
    }
}