TDengine icon indicating copy to clipboard operation
TDengine copied to clipboard

somewhat aggressive, another stmt implementation

Open freemine opened this issue 1 year ago • 3 comments

  1. taos_stmt_prepare2 is a nearly fully function to parse the sql and get the meta info as much as possible
  2. taos_stmt_get_params2 is to fill param meta info into user provided buffer
  3. taos_stmt_bind_params2 is to bind all rows of params to the statement before taos_stmt_execute.

currently supported sql-statements as such:

  1. literal sql, select/insert/DML/DDL
  2. all kinds of valid parameterized sql statements, both select and insert
  3. all kinds of parameterized insert statements, with parameterized-tbname as well

and parameter-type-convertion is also provided in this PR, most of the TSDB_DATA_TYPE_xxx is supported converting from C string, except for TSDB_DATA_TYPE_NCHAR/VARBINARY. and. type inter-convertion currently is not implemented in this PR, but it's easy and just half way to go, no special deal.

freemine avatar Sep 14 '24 14:09 freemine

196 //////// experimental //////////////////////////////////////////////////////////////////////////////////////////////////////
197 // NOTE: 1. you can choose to either start/switch by taos_stmt_prepare2 or by taos_stmt_prepare as usual                  //
198 // NOTE: 2. these 2-tailed functions and functions as below are not interchangeable:                                      //                                                
199 //       taos_stmt_prepare                                                                                                //
200 //       taos_stmt_set_tbname_tags/taos_stmt_set_tbname/taos_stmt_set_tags/taos_stmt_set_tbname/taos_stmt_set_sub_tbname  //
201 //       taos_stmt_get_tag_fields/taos_stmt_get_col_fields                                                                //
202 //       taos_stmt_bind_param/taos_stmt_bind_param_batch/taos_stmt_bind_single_param_batch/taos_stmt_add_batch            //
203 // NOTE: 3. it means, once you start by taos_stmt_prepare2, you shall stick to 2-tailed functions                         //
204 //          until you reach taos_stmt_execute and vice versa, otherwise, function-call will fail                          //
205 // NOTE: 4. you can switch to use taos_stmt_prepare2 and taos_stmt_prepare during the lifetime of the `stmt`              //
206 DLL_EXPORT int       taos_stmt_prepare2(TAOS_STMT *stmt, const char *sql, unsigned long length);                          //
207 //                                                                                                                        //
208 // NOTE: if TAOS_FIELD_E::type == TSDB_DATA_TYPE_NULL, it means the param's type is not determined yet.                   //
209 //       basically, it happens when sql prepared is a select-statement,                                                   //
210 //       or insert-statement with parameterized tbname while without `using` clause in it.                                //
211 DLL_EXPORT int       taos_stmt_get_params2(TAOS_STMT *stmt, TAOS_FIELD_E *params, int nr_params, int *nr_real);           //
212 //                                                                                                                        //
213 // NOTE: rows of params from TAOS_MULTI_BIND::num                                                                         //
214 DLL_EXPORT int       taos_stmt_bind_params2(TAOS_STMT *stmt, TAOS_MULTI_BIND *mbs, int nr_mbs);                           //
215 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

freemine avatar Sep 14 '24 14:09 freemine

some passed test cases snipped from tests/c/api2_test.c

1123       .line      = __LINE__,                                                    
1124       .sql       = "insert into ? (ts, b, i8, u8, i16, u16, i32, u32, i64, u64, flt, dbl, nm) "
1125                    "          values (?,  ?, ?,  ?,  ?,   ?,   ?,   ?,   ?,   ?,   ?,   ?,   ?)",
1126       .is_insert = 1,                                                           
1127       .params    = {                                                            
1128         {__LINE__, {"tall1", "1726146779200", "1", "-127", "255", "-32768", "65535", "-2147483647", "4294967295",
1129                     "-9223372036854775808", "18446744073709551615", "1.23", "4.56", "helloworld",
1130                     EXEC_COL_END}},                                             
1131         {__LINE__, {EXEC_COL_END}},                                             
1261       .line      = __LINE__,                                                    
1262       .sql       = "insert into ? using st (tn, t32) tags (?, ?) (ts, nm, i32) values (?,?,?)",
1263       .is_insert = 1,                                                           
1264       .params    = {                                                            
1265         {__LINE__, {"def", "world", "5", "1726146779004", "def1", "1", EXEC_COL_END}},
1266         {__LINE__, {"def", "world", "5", "1726146779005", "def2", "2", EXEC_COL_END}},
1267         {__LINE__, {"lmn", "foolm", "5", "1726146779014", "lmn1", "5", EXEC_COL_END}},
1268         {__LINE__, {"lmn", "foolm", "5", "1726146779015", "lmn2", "6", EXEC_COL_END}},
1269         {__LINE__, {"lmn", "foolm", "5", "1726146779016", "lmn3", "7", EXEC_COL_END}},
1270         {__LINE__, {"lmn", "foolm", "5", "1726146779017", "lmn4", "8", EXEC_COL_END}},
1271         {__LINE__, {"def", "world", "5", "1726146779006", "def3", "3", EXEC_COL_END}},
1272         {__LINE__, {"def", "world", "5", "1726146779007", "def4", "4", EXEC_COL_END}},
1273         {__LINE__, {EXEC_COL_END}},                                             
1279       // TODO: do convertion                                                    
1280       .line      = __LINE__,                                                    
1281       .sql       = "insert into ? (ts, nm, i32) values(?, ?, ?)",               
1282       .is_insert = 1,                                                           
1283       .params    = {                                                            
1284         {__LINE__, {"t", "1726146978100", "yes", "987", EXEC_COL_END}},         
1285         {__LINE__, {"t", "1726146978101", NULL,  NULL,  EXEC_COL_END}},         
1286         {__LINE__, {"t", "1726146978102", "123", "789", EXEC_COL_END}},         
1287         {__LINE__, {EXEC_COL_END}},                                             
1304       // TODO: do convertion                                                    
1305       .line      = __LINE__,                                                    
1306       .sql       = "insert into ? (ts, nm, i32) values (?,?,?)",                
1307       .is_insert = 1,                                                           
1308       .params    = {                                                            
1309         {__LINE__, {"def", "1726146779014", "def11", "11", EXEC_COL_END}},      
1310         {__LINE__, {"def", "1726146779015", "def12", "12", EXEC_COL_END}},      
1311         {__LINE__, {"def", "1726146779016", "def13", "13", EXEC_COL_END}},      
1312         {__LINE__, {"aef", "1726146779124", "aef11", "91", EXEC_COL_END}},      
1313         {__LINE__, {"aef", "1726146779125", "aef12", "92", EXEC_COL_END}},      
1314         {__LINE__, {"def", "1726146779017", "def14", "14", EXEC_COL_END}},      
1315         {__LINE__, {"aef", "1726146779126", "aef13", "93", EXEC_COL_END}},      
1316         {__LINE__, {"aef", "1726146779127", "aef14", "94", EXEC_COL_END}},      
1317         {__LINE__, {EXEC_COL_END}},                                             

freemine avatar Sep 14 '24 14:09 freemine

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
4 out of 10 committers have signed the CLA.

:white_check_mark: kailixu
:white_check_mark: stephenkgu
:white_check_mark: hjxilinx
:white_check_mark: freemine
:x: 54liuyao
:x: cadem
:x: wangjiaming0909
:x: flyingangel2013
:x: hzcheng
:x: dapan1121
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Oct 16 '24 15:10 CLAassistant