sql_wrapper 0.1.6

Generate table Struct and Sql function for table based on sqlx and sql_builder
Documentation
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
use sqlx::{Connection, MySqlConnection};
use crate::generator::{ColumnInfo, Generator};


pub struct MysqlGenerator {}

impl Generator for MysqlGenerator {
    async fn query_columns(&self, conn_url: &str, table_name: &str) -> Vec<ColumnInfo> {
        let mut conn: MySqlConnection = MySqlConnection::connect(conn_url).await.unwrap();
        let sql = format!(r#"select COLUMN_NAME as column_name, ORDINAL_POSITION as ordinal_position,
     IS_NULLABLE as is_nullable, DATA_TYPE as data_type, CHARACTER_MAXIMUM_LENGTH as character_maximum_length
      from information_schema.columns where table_name = '{table_name}' order by ordinal_position asc; "#);

        let columns: Vec<ColumnInfo> = sqlx::query_as(sql.as_str()).fetch_all(&mut conn).await.unwrap();
        return columns;
    }

    fn get_mapping_type(&self, sql_type: &str) -> String {
        let sql_type = sql_type.to_uppercase();
        let ret = if sql_type == "TINYINT" {
            "i8"
        } else if sql_type == "SMALLINT" {
            "i16"
        } else if sql_type == "INT" {
            "i32"
        } else if sql_type == "SERIAL" {
            "i32"
        } else if sql_type == "BIGINT" {
            "i64"
        } else if sql_type == "TINYINT UNSIGNED" {
            "u8"
        } else if sql_type == "SMALLINT UNSIGNED" {
            "u16"
        } else if sql_type == "INT UNSIGNED" {
            "u32"
        } else if sql_type == "BIGINT UNSIGNED" {
            "u64"
        } else if sql_type == "FLOAT" {
            "f32"
        } else if sql_type == "DOUBLE" {
            "f64"
        } else if sql_type == "VARCHAR" {
            "String"
        } else if sql_type == "TEXT" || sql_type == "TINYTEXT" || sql_type == "LONGTEXT" || sql_type == "MEDIUMTEXT" {
            "String"
        } else if sql_type == "CHAR" {
            "String"
        } else if sql_type == "VARBINARY" {
            "Vec<u8>"
        } else if sql_type == "BINARY" {
            "Vec<u8>"
        } else if sql_type == "BLOB" ||  sql_type == "LONGBLOB" ||  sql_type == "MEDIUMBLOB" || sql_type == "TINYBLOB" {
            "Vec<u8>"
        } else if sql_type == "TIMESTAMP" {
            "chrono::DateTime<chrono::Local>"
        } else if sql_type == "DATETIME" {
            "chrono::NaiveDateTime"
        } else if sql_type == "DATE" {
            "chrono::NaiveDate"
        } else if sql_type == "TIME" {
            "chrono::NaiveTime"
        } else if sql_type == "DECIMAL" {
            "sqlx::types::Decimal"
        } else if sql_type == "UUID" {
            "uuid::Uuid"
        } else if sql_type == "JSON" {
            "serde_json::Value"
        } else {
            panic!("{}", format!("not support type:{}", sql_type))
        };
        ret.to_string()
    }

    fn gen_insert_returning_id_fn(&self,table_name: &str, column_infos: &Vec<ColumnInfo>) -> String {
        let struct_name = self.gen_struct_name(table_name);
        let ret = self.gen_field_and_value_str(column_infos, false);

        let fn_str = format!(r#"
pub async fn insert_returning_id(conn: &mut sqlx::MySqlConnection, obj: {struct_name}) -> i64 {{
    let mut sql = sql_builder::SqlBuilder::insert_into("{table_name}");
{ret}
   let sql = sql.sql().unwrap();
   let  result = sqlx::query(sql.as_str()).execute(conn).await;
   if result.is_ok() {{
       return result.unwrap().last_insert_id() as i64;
   }}
   println!("insert failed:{{:?}}", result);
   return -1;
}}
    "#);

        return fn_str
    }

    fn gen_insert_fn(&self, table_name: &str, column_infos: &Vec<ColumnInfo>) -> String {
        let struct_name = self.gen_struct_name(table_name);
        let ret = self.gen_field_and_value_str(column_infos, true);

        let fn_str = format!(r#"
pub async fn insert(conn: &mut sqlx::MySqlConnection, obj: {struct_name}) -> Result<sqlx::mysql::MySqlQueryResult, sqlx::Error>  {{
    let mut sql = sql_builder::SqlBuilder::insert_into("{table_name}");
{ret}
    let sql = sql.sql().unwrap();
    sqlx::query(sql.as_str()).execute(conn).await

}}
    "#);

        return fn_str
    }

    fn gen_batch_insert_returning_id_fn(&self, table_name: &str, column_infos: &Vec<ColumnInfo>) -> String {
        let struct_name = self.gen_struct_name(table_name);
        let ret = self.gen_field_and_batch_values_str(column_infos, false);
        let fn_str = format!(r#"

pub async fn batch_insert_returning_id(conn: &mut sqlx::MySqlConnection, objs: Vec<{struct_name}>) -> Vec<i64> {{
    let len = objs.len();
    let mut sql = sql_builder::SqlBuilder::insert_into("{table_name}");
{ret}

    let sql = sql.sql().unwrap();
    let result = sqlx::query(sql.as_str()).execute(conn).await;
    if result.is_ok() {{
        let last_id = result.unwrap().last_insert_id() as i64;
        println!("last id:{{last_id}}");
        let mut list = vec![];
        for idx in 0..len {{
            list.push(last_id - len as i64 + idx as i64 + 1)
        }}
        return list;
    }}
    println!("insert failed:{{:?}}", result);
    return vec![]

}}
    "#);

        fn_str
    }

    fn gen_batch_insert_fn(&self, table_name: &str, column_infos: &Vec<ColumnInfo>) -> String {
        let struct_name = self.gen_struct_name(table_name);

        let ret = self.gen_field_and_batch_values_str(column_infos, true);

        let fn_str = format!(r#"

pub async fn batch_insert(conn: &mut sqlx::MySqlConnection, objs: Vec<{struct_name}>) -> Result<sqlx::mysql::MySqlQueryResult, sqlx::Error>  {{
    let mut sql = sql_builder::SqlBuilder::insert_into("{table_name}");
{ret}

    let sql = sql.sql().unwrap();
    sqlx::query(sql.as_str()).execute(conn).await

}}
    "#);

        fn_str
    }

    fn gen_select_by_id_fn(&self, table_name: &str, column_infos: &Vec<ColumnInfo>) -> String {
        let sql = self.gen_select_sql(table_name, column_infos);
        let struct_name = self.gen_struct_name(table_name);
        format!(r#"
pub async fn select_by_id(conn: &mut sqlx::MySqlConnection,id: i64) -> Result<{struct_name}, sqlx::Error> {{
    let sql = format!("{sql} where id='{{}}'", id);
    let result = sqlx::query_as(sql.as_str()).fetch_one(conn).await;
    result
}}

        "#)
    }

    fn gen_delete_by_id_fn(&self, _table_name: &str) -> String {
        let sql = self.gen_delete_by_id_sql(_table_name);
        format!(r#"
pub async fn delete_by_id(conn: &mut sqlx::MySqlConnection,id: i64) -> Result<sqlx::mysql::MySqlQueryResult, sqlx::Error> {{
    let sql = format!("{sql}'{{}}'", id);
    sqlx::query(sql.as_str()).execute(conn).await
}}
        "#)
    }
}





#[cfg(test)]
mod test {
    use std::str::FromStr;
    use std::time::SystemTime;
    use chrono::{DateTime, NaiveDate, NaiveDateTime};
    use sqlx::{Connection, MySqlConnection};
    use sqlx::types::Decimal;
    use crate::field_to_string::FieldToString;
    use crate::generator::Generator;
    use crate::mysql_generator::MysqlGenerator;

    #[tokio::test]
    async fn gen_file_test() {
        let conn_url = "mysql://root:123456@localhost/test_db";
        let table_name = "test_table";
        let gen = MysqlGenerator{};
        let result = gen.gen_file(conn_url, table_name).await;
        println!("result:{:?}", result)
    }

    #[tokio::test]
    async fn insert_test() {
        let conn_url = "mysql://root:123456@localhost/test_db";
        let mut conn: MySqlConnection = MySqlConnection::connect(conn_url).await.unwrap();

        let mut obj1 = gen_test_table_obj();
        obj1.id = 1;
        let result = insert(&mut conn, obj1).await;
        println!("insert result:{:?}", result);
    }

    #[tokio::test]
    async fn insert_returning_id_test() {
        let conn_url = "mysql://root:123456@localhost/test_db";
        let mut conn: MySqlConnection = MySqlConnection::connect(conn_url).await.unwrap();
        let obj1 = gen_test_table_obj();
        let result = insert_returning_id(&mut conn, obj1).await;
        println!("insert result:{:?}", result);
    }

    #[tokio::test]
    async fn batch_insert_test() {
        let conn_url = "mysql://root:123456@localhost/test_db";
        let mut conn: MySqlConnection = MySqlConnection::connect(conn_url).await.unwrap();

        let mut obj1 = gen_test_table_obj();
        obj1.id = 2;
        let mut obj2 = gen_test_table_obj();
        obj2.id = 3;
        let result = batch_insert(&mut conn, vec![obj1, obj2]).await;
        println!("insert result:{:?}", result);
    }


    #[tokio::test]
    async fn batch_insert_returning_id_test() {
        let conn_url = "mysql://root:123456@localhost/test_db";
        let mut conn: MySqlConnection = MySqlConnection::connect(conn_url).await.unwrap();
        let obj1 = gen_test_table_obj();
        let obj2 = gen_test_table_obj();
        let result = batch_insert_returning_id(&mut conn, vec![obj1, obj2]).await;
        println!("insert result:{:?}", result);
    }


    #[tokio::test]
    async fn select_by_id_test() {
        let conn_url = "mysql://root:123456@localhost/test_db";
        let mut conn: MySqlConnection = MySqlConnection::connect(conn_url).await.unwrap();
        let result = select_by_id(&mut conn, 19).await;
        println!("{:?}", result)
    }

    #[tokio::test]
    async fn delete_by_id_test() {
        let conn_url = "mysql://root:123456@localhost/test_db";
        let mut conn: MySqlConnection = MySqlConnection::connect(conn_url).await.unwrap();
        let result = delete_by_id(&mut conn, 1).await;
        print!("{:?}", result);
    }

    fn gen_test_table_obj() -> TestTable {
        TestTable {
            id: 0,
            b1: 3,
            b2: Some(4),
            c1: "c".to_string(),
            c2: Some("c".to_string()),
            i4: 44,
            i41: Some(455),
            r1: 0.0,
            r2: Some(3.14),
            d1: 0.0,
            d2: Some(345.0),
            t1: "4".to_string(),
            tx1: Some("tet3434".to_string()),
            tx2: Some("tet343432".to_string()),
            tx3: Some("tet34343".to_string()),
            t2: "5da".to_string(),
            t3: Some("test".to_string()),
            t4: Some("adf".to_string()),
            byte1: Some(Vec::from("안녕하세요你好こんにちはЗдравствуйте💖💖💖💖💖")),
            blob4: Some(Vec::from("안녕하세요你好こんにちはЗдравствуйте💖💖💖💖💖")),
            big1: Some(Decimal::new(234,1)),
            blob2: Some(vec![3,4,5]),
            big2: Some(Decimal::new(223434, 2)),
            ts1:  DateTime::from(SystemTime::now()),
            date1: Some(NaiveDate::default()),
            time1: Default::default(),
            i5: Some(12),
            blob3: Some(vec![3,4,5]),
            dt: NaiveDateTime::from_str("2015-09-18T23:56:04").unwrap(),
        }
    }
    #[derive(sqlx::FromRow, Debug, PartialEq)]
    pub struct TestTable {
        id: i64,
        b1: i8,
        b2: Option<i8>,
        c1: String,
        c2: Option<String>,
        i4: i32,
        i41: Option<i32>,
        r1: f64,
        r2: Option<f64>,
        d1: f64,
        d2: Option<f64>,
        t1: String,
        tx1: Option<String>,
        tx2: Option<String>,
        tx3: Option<String>,
        t2: String,
        t3: Option<String>,
        t4: Option<String>,
        byte1: Option<Vec<u8>>,
        blob4: Option<Vec<u8>>,
        blob3: Option<Vec<u8>>,
        big1: Option<sqlx::types::Decimal>,
        blob2: Option<Vec<u8>>,
        big2: Option<sqlx::types::Decimal>,
        ts1: chrono::DateTime<chrono::Local>,
        dt: chrono::NaiveDateTime,
        date1: Option<chrono::NaiveDate>,
        time1: chrono::NaiveTime,
        i5: Option<i16>,
    }



    pub async fn insert_returning_id(conn: &mut sqlx::MySqlConnection, obj: TestTable) -> i64 {
        let mut sql = sql_builder::SqlBuilder::insert_into("test_table");
        sql.field("b1");
        sql.field("b2");
        sql.field("c1");
        sql.field("c2");
        sql.field("i4");
        sql.field("i41");
        sql.field("r1");
        sql.field("r2");
        sql.field("d1");
        sql.field("d2");
        sql.field("t1");
        sql.field("tx1");
        sql.field("tx2");
        sql.field("tx3");
        sql.field("t2");
        sql.field("t3");
        sql.field("t4");
        sql.field("byte1");
        sql.field("blob4");
        sql.field("blob3");
        sql.field("big1");
        sql.field("blob2");
        sql.field("big2");
        sql.field("ts1");
        sql.field("dt");
        sql.field("date1");
        sql.field("time1");
        sql.field("i5");
        sql.values(&[
            sql_builder::quote(obj.b1.field_to_string()),
            sql_builder::quote(obj.b2.unwrap().field_to_string()),
            sql_builder::quote(obj.c1.field_to_string()),
            sql_builder::quote(obj.c2.unwrap().field_to_string()),
            sql_builder::quote(obj.i4.field_to_string()),
            sql_builder::quote(obj.i41.unwrap().field_to_string()),
            sql_builder::quote(obj.r1.field_to_string()),
            sql_builder::quote(obj.r2.unwrap().field_to_string()),
            sql_builder::quote(obj.d1.field_to_string()),
            sql_builder::quote(obj.d2.unwrap().field_to_string()),
            sql_builder::quote(obj.t1.field_to_string()),
            sql_builder::quote(obj.tx1.unwrap().field_to_string()),
            sql_builder::quote(obj.tx2.unwrap().field_to_string()),
            sql_builder::quote(obj.tx3.unwrap().field_to_string()),
            sql_builder::quote(obj.t2.field_to_string()),
            sql_builder::quote(obj.t3.unwrap().field_to_string()),
            sql_builder::quote(obj.t4.unwrap().field_to_string()),
            sql_builder::quote(obj.byte1.unwrap().field_to_string()),
            sql_builder::quote(obj.blob4.unwrap().field_to_string()),
            sql_builder::quote(obj.blob3.unwrap().field_to_string()),
            sql_builder::quote(obj.big1.unwrap().field_to_string()),
            sql_builder::quote(obj.blob2.unwrap().field_to_string()),
            sql_builder::quote(obj.big2.unwrap().field_to_string()),
            sql_builder::quote(obj.ts1.field_to_string()),
            sql_builder::quote(obj.dt.field_to_string()),
            sql_builder::quote(obj.date1.unwrap().field_to_string()),
            sql_builder::quote(obj.time1.field_to_string()),
            sql_builder::quote(obj.i5.unwrap().field_to_string())
        ]);

        let sql = sql.sql().unwrap();
        let  result = sqlx::query(sql.as_str()).execute(conn).await;
        if result.is_ok() {
            return result.unwrap().last_insert_id() as i64;
        }
        println!("insert failed:{:?}", result);
        return -1;
    }

    pub async fn insert(conn: &mut sqlx::MySqlConnection, obj: TestTable) -> Result<sqlx::mysql::MySqlQueryResult, sqlx::Error>  {
        let mut sql = sql_builder::SqlBuilder::insert_into("test_table");
        sql.field("id");
        sql.field("b1");
        sql.field("b2");
        sql.field("c1");
        sql.field("c2");
        sql.field("i4");
        sql.field("i41");
        sql.field("r1");
        sql.field("r2");
        sql.field("d1");
        sql.field("d2");
        sql.field("t1");
        sql.field("tx1");
        sql.field("tx2");
        sql.field("tx3");
        sql.field("t2");
        sql.field("t3");
        sql.field("t4");
        sql.field("byte1");
        sql.field("blob4");
        sql.field("blob3");
        sql.field("big1");
        sql.field("blob2");
        sql.field("big2");
        sql.field("ts1");
        sql.field("dt");
        sql.field("date1");
        sql.field("time1");
        sql.field("i5");
        sql.values(&[
            sql_builder::quote(obj.id.field_to_string()),
            sql_builder::quote(obj.b1.field_to_string()),
            sql_builder::quote(obj.b2.unwrap().field_to_string()),
            sql_builder::quote(obj.c1.field_to_string()),
            sql_builder::quote(obj.c2.unwrap().field_to_string()),
            sql_builder::quote(obj.i4.field_to_string()),
            sql_builder::quote(obj.i41.unwrap().field_to_string()),
            sql_builder::quote(obj.r1.field_to_string()),
            sql_builder::quote(obj.r2.unwrap().field_to_string()),
            sql_builder::quote(obj.d1.field_to_string()),
            sql_builder::quote(obj.d2.unwrap().field_to_string()),
            sql_builder::quote(obj.t1.field_to_string()),
            sql_builder::quote(obj.tx1.unwrap().field_to_string()),
            sql_builder::quote(obj.tx2.unwrap().field_to_string()),
            sql_builder::quote(obj.tx3.unwrap().field_to_string()),
            sql_builder::quote(obj.t2.field_to_string()),
            sql_builder::quote(obj.t3.unwrap().field_to_string()),
            sql_builder::quote(obj.t4.unwrap().field_to_string()),
            sql_builder::quote(obj.byte1.unwrap().field_to_string()),
            sql_builder::quote(obj.blob4.unwrap().field_to_string()),
            sql_builder::quote(obj.blob3.unwrap().field_to_string()),
            sql_builder::quote(obj.big1.unwrap().field_to_string()),
            sql_builder::quote(obj.blob2.unwrap().field_to_string()),
            sql_builder::quote(obj.big2.unwrap().field_to_string()),
            sql_builder::quote(obj.ts1.field_to_string()),
            sql_builder::quote(obj.dt.field_to_string()),
            sql_builder::quote(obj.date1.unwrap().field_to_string()),
            sql_builder::quote(obj.time1.field_to_string()),
            sql_builder::quote(obj.i5.unwrap().field_to_string())
        ]);

        let sql = sql.sql().unwrap();
        sqlx::query(sql.as_str()).execute(conn).await

    }


    pub async fn batch_insert_returning_id(conn: &mut sqlx::MySqlConnection, objs: Vec<TestTable>) -> Vec<i64> {
        let len = objs.len();
        let mut sql = sql_builder::SqlBuilder::insert_into("test_table");
        sql.field("b1");
        sql.field("b2");
        sql.field("c1");
        sql.field("c2");
        sql.field("i4");
        sql.field("i41");
        sql.field("r1");
        sql.field("r2");
        sql.field("d1");
        sql.field("d2");
        sql.field("t1");
        sql.field("tx1");
        sql.field("tx2");
        sql.field("tx3");
        sql.field("t2");
        sql.field("t3");
        sql.field("t4");
        sql.field("byte1");
        sql.field("blob4");
        sql.field("blob3");
        sql.field("big1");
        sql.field("blob2");
        sql.field("big2");
        sql.field("ts1");
        sql.field("dt");
        sql.field("date1");
        sql.field("time1");
        sql.field("i5");
        for obj in objs {
            sql.values(&[
                sql_builder::quote(obj.b1.field_to_string()),
                sql_builder::quote(obj.b2.unwrap().field_to_string()),
                sql_builder::quote(obj.c1.field_to_string()),
                sql_builder::quote(obj.c2.unwrap().field_to_string()),
                sql_builder::quote(obj.i4.field_to_string()),
                sql_builder::quote(obj.i41.unwrap().field_to_string()),
                sql_builder::quote(obj.r1.field_to_string()),
                sql_builder::quote(obj.r2.unwrap().field_to_string()),
                sql_builder::quote(obj.d1.field_to_string()),
                sql_builder::quote(obj.d2.unwrap().field_to_string()),
                sql_builder::quote(obj.t1.field_to_string()),
                sql_builder::quote(obj.tx1.unwrap().field_to_string()),
                sql_builder::quote(obj.tx2.unwrap().field_to_string()),
                sql_builder::quote(obj.tx3.unwrap().field_to_string()),
                sql_builder::quote(obj.t2.field_to_string()),
                sql_builder::quote(obj.t3.unwrap().field_to_string()),
                sql_builder::quote(obj.t4.unwrap().field_to_string()),
                sql_builder::quote(obj.byte1.unwrap().field_to_string()),
                sql_builder::quote(obj.blob4.unwrap().field_to_string()),
                sql_builder::quote(obj.blob3.unwrap().field_to_string()),
                sql_builder::quote(obj.big1.unwrap().field_to_string()),
                sql_builder::quote(obj.blob2.unwrap().field_to_string()),
                sql_builder::quote(obj.big2.unwrap().field_to_string()),
                sql_builder::quote(obj.ts1.field_to_string()),
                sql_builder::quote(obj.dt.field_to_string()),
                sql_builder::quote(obj.date1.unwrap().field_to_string()),
                sql_builder::quote(obj.time1.field_to_string()),
                sql_builder::quote(obj.i5.unwrap().field_to_string())
            ]);
        }


        let sql = sql.sql().unwrap();
        let result = sqlx::query(sql.as_str()).execute(conn).await;
        if result.is_ok() {
            let last_id = result.unwrap().last_insert_id() as i64;
            println!("last id:{last_id}");
            let mut list = vec![];
            for idx in 0..len {
                list.push(last_id - len as i64 + idx as i64 + 1)
            }
            return list;
        }
        println!("insert failed:{:?}", result);
        return vec![]

    }


    pub async fn batch_insert(conn: &mut sqlx::MySqlConnection, objs: Vec<TestTable>) -> Result<sqlx::mysql::MySqlQueryResult, sqlx::Error>  {
        let mut sql = sql_builder::SqlBuilder::insert_into("test_table");
        sql.field("id");
        sql.field("b1");
        sql.field("b2");
        sql.field("c1");
        sql.field("c2");
        sql.field("i4");
        sql.field("i41");
        sql.field("r1");
        sql.field("r2");
        sql.field("d1");
        sql.field("d2");
        sql.field("t1");
        sql.field("tx1");
        sql.field("tx2");
        sql.field("tx3");
        sql.field("t2");
        sql.field("t3");
        sql.field("t4");
        sql.field("byte1");
        sql.field("blob4");
        sql.field("blob3");
        sql.field("big1");
        sql.field("blob2");
        sql.field("big2");
        sql.field("ts1");
        sql.field("dt");
        sql.field("date1");
        sql.field("time1");
        sql.field("i5");
        for obj in objs {
            sql.values(&[
                sql_builder::quote(obj.id.field_to_string()),
                sql_builder::quote(obj.b1.field_to_string()),
                sql_builder::quote(obj.b2.unwrap().field_to_string()),
                sql_builder::quote(obj.c1.field_to_string()),
                sql_builder::quote(obj.c2.unwrap().field_to_string()),
                sql_builder::quote(obj.i4.field_to_string()),
                sql_builder::quote(obj.i41.unwrap().field_to_string()),
                sql_builder::quote(obj.r1.field_to_string()),
                sql_builder::quote(obj.r2.unwrap().field_to_string()),
                sql_builder::quote(obj.d1.field_to_string()),
                sql_builder::quote(obj.d2.unwrap().field_to_string()),
                sql_builder::quote(obj.t1.field_to_string()),
                sql_builder::quote(obj.tx1.unwrap().field_to_string()),
                sql_builder::quote(obj.tx2.unwrap().field_to_string()),
                sql_builder::quote(obj.tx3.unwrap().field_to_string()),
                sql_builder::quote(obj.t2.field_to_string()),
                sql_builder::quote(obj.t3.unwrap().field_to_string()),
                sql_builder::quote(obj.t4.unwrap().field_to_string()),
                sql_builder::quote(obj.byte1.unwrap().field_to_string()),
                sql_builder::quote(obj.blob4.unwrap().field_to_string()),
                sql_builder::quote(obj.blob3.unwrap().field_to_string()),
                sql_builder::quote(obj.big1.unwrap().field_to_string()),
                sql_builder::quote(obj.blob2.unwrap().field_to_string()),
                sql_builder::quote(obj.big2.unwrap().field_to_string()),
                sql_builder::quote(obj.ts1.field_to_string()),
                sql_builder::quote(obj.dt.field_to_string()),
                sql_builder::quote(obj.date1.unwrap().field_to_string()),
                sql_builder::quote(obj.time1.field_to_string()),
                sql_builder::quote(obj.i5.unwrap().field_to_string())
            ]);
        }


        let sql = sql.sql().unwrap();
        sqlx::query(sql.as_str()).execute(conn).await

    }

    pub fn select_sql() -> String {
        "select id, b1, b2, c1, c2, i4, i41, r1, r2, d1, d2, t1, tx1, tx2, tx3, t2, t3, t4, byte1, blob4, blob3, big1, blob2, big2, ts1, dt, date1, time1, i5  from test_table".to_string()
    }

    pub async fn select_by_id(conn: &mut sqlx::MySqlConnection,id: i64) -> Result<TestTable, sqlx::Error> {
        let sql = format!("select id, b1, b2, c1, c2, i4, i41, r1, r2, d1, d2, t1, tx1, tx2, tx3, t2, t3, t4, byte1, blob4, blob3, big1, blob2, big2, ts1, dt, date1, time1, i5  from test_table where id='{}'", id);
        let result = sqlx::query_as(sql.as_str()).fetch_one(conn).await;
        result
    }


    pub async fn delete_by_id(conn: &mut sqlx::MySqlConnection,id: i64) -> Result<sqlx::mysql::MySqlQueryResult, sqlx::Error> {
        let sql = format!("delete from test_table where id='{}'", id);
        sqlx::query(sql.as_str()).execute(conn).await
    }

}