Merge pull request #27226 from taosdata/feat/TD-30813-2

feat(stmt2): initial commit for stmt2
This commit is contained in:
Hongze Cheng 2024-09-09 08:52:59 +08:00 committed by GitHub
commit 8012a1902b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 7501 additions and 115 deletions

View File

@ -51,7 +51,7 @@ typedef void TAOS_SUB;
#define TSDB_DATA_TYPE_BLOB 18 // binary #define TSDB_DATA_TYPE_BLOB 18 // binary
#define TSDB_DATA_TYPE_MEDIUMBLOB 19 #define TSDB_DATA_TYPE_MEDIUMBLOB 19
#define TSDB_DATA_TYPE_BINARY TSDB_DATA_TYPE_VARCHAR // string #define TSDB_DATA_TYPE_BINARY TSDB_DATA_TYPE_VARCHAR // string
#define TSDB_DATA_TYPE_GEOMETRY 20 // geometry #define TSDB_DATA_TYPE_GEOMETRY 20 // geometry
#define TSDB_DATA_TYPE_MAX 21 #define TSDB_DATA_TYPE_MAX 21
typedef enum { typedef enum {
@ -168,7 +168,7 @@ DLL_EXPORT const char *taos_data_type(int type);
DLL_EXPORT TAOS_STMT *taos_stmt_init(TAOS *taos); DLL_EXPORT TAOS_STMT *taos_stmt_init(TAOS *taos);
DLL_EXPORT TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid); DLL_EXPORT TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid);
DLL_EXPORT TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS* options); DLL_EXPORT TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options);
DLL_EXPORT int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length); DLL_EXPORT int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length);
DLL_EXPORT int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags); DLL_EXPORT int taos_stmt_set_tbname_tags(TAOS_STMT *stmt, const char *name, TAOS_MULTI_BIND *tags);
DLL_EXPORT int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name); DLL_EXPORT int taos_stmt_set_tbname(TAOS_STMT *stmt, const char *name);
@ -193,6 +193,49 @@ DLL_EXPORT char *taos_stmt_errstr(TAOS_STMT *stmt);
DLL_EXPORT int taos_stmt_affected_rows(TAOS_STMT *stmt); DLL_EXPORT int taos_stmt_affected_rows(TAOS_STMT *stmt);
DLL_EXPORT int taos_stmt_affected_rows_once(TAOS_STMT *stmt); DLL_EXPORT int taos_stmt_affected_rows_once(TAOS_STMT *stmt);
typedef void TAOS_STMT2;
typedef enum {
TAOS_FIELD_COL = 1,
TAOS_FIELD_TAG,
TAOS_FIELD_QUERY,
TAOS_FIELD_TBNAME,
} TAOS_FIELD_T;
typedef struct {
int64_t reqid;
bool singleStbInsert;
bool singleTableBindOnce;
__taos_async_fn_t asyncExecFn;
void *userdata;
} TAOS_STMT2_OPTION;
typedef struct {
int buffer_type;
void *buffer;
int32_t *length;
char *is_null;
int num;
} TAOS_STMT2_BIND;
typedef struct {
int count;
char **tbnames;
TAOS_STMT2_BIND **tags;
TAOS_STMT2_BIND **bind_cols;
} TAOS_STMT2_BINDV;
DLL_EXPORT TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option);
DLL_EXPORT int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length);
DLL_EXPORT int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx);
DLL_EXPORT int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows);
DLL_EXPORT int taos_stmt2_close(TAOS_STMT2 *stmt);
DLL_EXPORT int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert);
DLL_EXPORT int taos_stmt2_get_fields(TAOS_STMT2 *stmt, TAOS_FIELD_T field_type, int *count, TAOS_FIELD_E **fields);
DLL_EXPORT void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_E *fields);
DLL_EXPORT TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt);
DLL_EXPORT char *taos_stmt2_error(TAOS_STMT2 *stmt);
DLL_EXPORT TAOS_RES *taos_query(TAOS *taos, const char *sql); DLL_EXPORT TAOS_RES *taos_query(TAOS *taos, const char *sql);
DLL_EXPORT TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqId); DLL_EXPORT TAOS_RES *taos_query_with_reqid(TAOS *taos, const char *sql, int64_t reqId);
@ -246,14 +289,15 @@ DLL_EXPORT void taos_set_hb_quit(int8_t quitByKill);
DLL_EXPORT int taos_set_notify_cb(TAOS *taos, __taos_notify_fn_t fp, void *param, int type); DLL_EXPORT int taos_set_notify_cb(TAOS *taos, __taos_notify_fn_t fp, void *param, int type);
typedef void (*__taos_async_whitelist_fn_t)(void *param, int code, TAOS *taos, int numOfWhiteLists, uint64_t* pWhiteLists); typedef void (*__taos_async_whitelist_fn_t)(void *param, int code, TAOS *taos, int numOfWhiteLists,
uint64_t *pWhiteLists);
DLL_EXPORT void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param); DLL_EXPORT void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *param);
typedef enum { typedef enum {
TAOS_CONN_MODE_BI = 0, TAOS_CONN_MODE_BI = 0,
} TAOS_CONN_MODE; } TAOS_CONN_MODE;
DLL_EXPORT int taos_set_conn_mode(TAOS* taos, int mode, int value); DLL_EXPORT int taos_set_conn_mode(TAOS *taos, int mode, int value);
/* --------------------------schemaless INTERFACE------------------------------- */ /* --------------------------schemaless INTERFACE------------------------------- */
DLL_EXPORT TAOS_RES *taos_schemaless_insert(TAOS *taos, char *lines[], int numLines, int protocol, int precision); DLL_EXPORT TAOS_RES *taos_schemaless_insert(TAOS *taos, char *lines[], int numLines, int protocol, int precision);
@ -271,10 +315,13 @@ DLL_EXPORT TAOS_RES *taos_schemaless_insert_raw_ttl(TAOS *taos, char *lines, int
int precision, int32_t ttl); int precision, int32_t ttl);
DLL_EXPORT TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows, DLL_EXPORT TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid(TAOS *taos, char *lines, int len, int32_t *totalRows,
int protocol, int precision, int32_t ttl, int64_t reqid); int protocol, int precision, int32_t ttl, int64_t reqid);
DLL_EXPORT TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(TAOS *taos, char *lines, int len, int32_t *totalRows, DLL_EXPORT TAOS_RES *taos_schemaless_insert_raw_ttl_with_reqid_tbname_key(TAOS *taos, char *lines, int len,
int protocol, int precision, int32_t ttl, int64_t reqid, char *tbnameKey); int32_t *totalRows, int protocol,
DLL_EXPORT TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines, int protocol, int precision, int32_t ttl, int64_t reqid,
int precision, int32_t ttl, int64_t reqid, char *tbnameKey); char *tbnameKey);
DLL_EXPORT TAOS_RES *taos_schemaless_insert_ttl_with_reqid_tbname_key(TAOS *taos, char *lines[], int numLines,
int protocol, int precision, int32_t ttl,
int64_t reqid, char *tbnameKey);
/* --------------------------TMQ INTERFACE------------------------------- */ /* --------------------------TMQ INTERFACE------------------------------- */
typedef struct tmq_t tmq_t; typedef struct tmq_t tmq_t;
@ -320,14 +367,17 @@ DLL_EXPORT int32_t tmq_unsubscribe(tmq_t *tmq);
DLL_EXPORT int32_t tmq_subscription(tmq_t *tmq, tmq_list_t **topics); DLL_EXPORT int32_t tmq_subscription(tmq_t *tmq, tmq_list_t **topics);
DLL_EXPORT TAOS_RES *tmq_consumer_poll(tmq_t *tmq, int64_t timeout); DLL_EXPORT TAOS_RES *tmq_consumer_poll(tmq_t *tmq, int64_t timeout);
DLL_EXPORT int32_t tmq_consumer_close(tmq_t *tmq); DLL_EXPORT int32_t tmq_consumer_close(tmq_t *tmq);
DLL_EXPORT int32_t tmq_commit_sync(tmq_t *tmq, const TAOS_RES *msg); //Commit the msgs offset + 1 DLL_EXPORT int32_t tmq_commit_sync(tmq_t *tmq, const TAOS_RES *msg); // Commit the msgs offset + 1
DLL_EXPORT void tmq_commit_async(tmq_t *tmq, const TAOS_RES *msg, tmq_commit_cb *cb, void *param); DLL_EXPORT void tmq_commit_async(tmq_t *tmq, const TAOS_RES *msg, tmq_commit_cb *cb, void *param);
DLL_EXPORT int32_t tmq_commit_offset_sync(tmq_t *tmq, const char *pTopicName, int32_t vgId, int64_t offset); DLL_EXPORT int32_t tmq_commit_offset_sync(tmq_t *tmq, const char *pTopicName, int32_t vgId, int64_t offset);
DLL_EXPORT void tmq_commit_offset_async(tmq_t *tmq, const char *pTopicName, int32_t vgId, int64_t offset, tmq_commit_cb *cb, void *param); DLL_EXPORT void tmq_commit_offset_async(tmq_t *tmq, const char *pTopicName, int32_t vgId, int64_t offset,
DLL_EXPORT int32_t tmq_get_topic_assignment(tmq_t *tmq, const char *pTopicName, tmq_topic_assignment **assignment,int32_t *numOfAssignment); tmq_commit_cb *cb, void *param);
DLL_EXPORT void tmq_free_assignment(tmq_topic_assignment* pAssignment); DLL_EXPORT int32_t tmq_get_topic_assignment(tmq_t *tmq, const char *pTopicName, tmq_topic_assignment **assignment,
int32_t *numOfAssignment);
DLL_EXPORT void tmq_free_assignment(tmq_topic_assignment *pAssignment);
DLL_EXPORT int32_t tmq_offset_seek(tmq_t *tmq, const char *pTopicName, int32_t vgId, int64_t offset); DLL_EXPORT int32_t tmq_offset_seek(tmq_t *tmq, const char *pTopicName, int32_t vgId, int64_t offset);
DLL_EXPORT int64_t tmq_position(tmq_t *tmq, const char *pTopicName, int32_t vgId); // The current offset is the offset of the last consumed message + 1 DLL_EXPORT int64_t tmq_position(tmq_t *tmq, const char *pTopicName,
int32_t vgId); // The current offset is the offset of the last consumed message + 1
DLL_EXPORT int64_t tmq_committed(tmq_t *tmq, const char *pTopicName, int32_t vgId); DLL_EXPORT int64_t tmq_committed(tmq_t *tmq, const char *pTopicName, int32_t vgId);
DLL_EXPORT TAOS *tmq_get_connect(tmq_t *tmq); DLL_EXPORT TAOS *tmq_get_connect(tmq_t *tmq);
@ -336,7 +386,7 @@ DLL_EXPORT tmq_res_t tmq_get_res_type(TAOS_RES *res);
DLL_EXPORT const char *tmq_get_topic_name(TAOS_RES *res); DLL_EXPORT const char *tmq_get_topic_name(TAOS_RES *res);
DLL_EXPORT const char *tmq_get_db_name(TAOS_RES *res); DLL_EXPORT const char *tmq_get_db_name(TAOS_RES *res);
DLL_EXPORT int32_t tmq_get_vgroup_id(TAOS_RES *res); DLL_EXPORT int32_t tmq_get_vgroup_id(TAOS_RES *res);
DLL_EXPORT int64_t tmq_get_vgroup_offset(TAOS_RES* res); DLL_EXPORT int64_t tmq_get_vgroup_offset(TAOS_RES *res);
DLL_EXPORT const char *tmq_err2str(int32_t code); DLL_EXPORT const char *tmq_err2str(int32_t code);
/* ------------------------------ TAOSX -----------------------------------*/ /* ------------------------------ TAOSX -----------------------------------*/
@ -346,15 +396,16 @@ typedef struct tmq_raw_data {
uint16_t raw_type; uint16_t raw_type;
} tmq_raw_data; } tmq_raw_data;
DLL_EXPORT int32_t tmq_get_raw(TAOS_RES *res, tmq_raw_data *raw); DLL_EXPORT int32_t tmq_get_raw(TAOS_RES *res, tmq_raw_data *raw);
DLL_EXPORT int32_t tmq_write_raw(TAOS *taos, tmq_raw_data raw); DLL_EXPORT int32_t tmq_write_raw(TAOS *taos, tmq_raw_data raw);
DLL_EXPORT int taos_write_raw_block(TAOS *taos, int numOfRows, char *pData, const char *tbname); DLL_EXPORT int taos_write_raw_block(TAOS *taos, int numOfRows, char *pData, const char *tbname);
DLL_EXPORT int taos_write_raw_block_with_reqid(TAOS *taos, int numOfRows, char *pData, const char *tbname, int64_t reqid); DLL_EXPORT int taos_write_raw_block_with_reqid(TAOS *taos, int numOfRows, char *pData, const char *tbname,
DLL_EXPORT int taos_write_raw_block_with_fields(TAOS *taos, int rows, char *pData, const char *tbname, int64_t reqid);
TAOS_FIELD *fields, int numFields); DLL_EXPORT int taos_write_raw_block_with_fields(TAOS *taos, int rows, char *pData, const char *tbname,
DLL_EXPORT int taos_write_raw_block_with_fields_with_reqid(TAOS *taos, int rows, char *pData, const char *tbname, TAOS_FIELD *fields, int numFields);
TAOS_FIELD *fields, int numFields, int64_t reqid); DLL_EXPORT int taos_write_raw_block_with_fields_with_reqid(TAOS *taos, int rows, char *pData, const char *tbname,
DLL_EXPORT void tmq_free_raw(tmq_raw_data raw); TAOS_FIELD *fields, int numFields, int64_t reqid);
DLL_EXPORT void tmq_free_raw(tmq_raw_data raw);
// Returning null means error. Returned result need to be freed by tmq_free_json_meta // Returning null means error. Returned result need to be freed by tmq_free_json_meta
DLL_EXPORT char *tmq_get_json_meta(TAOS_RES *res); DLL_EXPORT char *tmq_get_json_meta(TAOS_RES *res);
@ -370,7 +421,7 @@ typedef enum {
} TSDB_SERVER_STATUS; } TSDB_SERVER_STATUS;
DLL_EXPORT TSDB_SERVER_STATUS taos_check_server_status(const char *fqdn, int port, char *details, int maxlen); DLL_EXPORT TSDB_SERVER_STATUS taos_check_server_status(const char *fqdn, int port, char *details, int maxlen);
DLL_EXPORT char* getBuildInfo(); DLL_EXPORT char *getBuildInfo();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -378,6 +378,19 @@ typedef struct {
int32_t tRowBuildFromBind(SBindInfo *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema, int32_t tRowBuildFromBind(SBindInfo *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
SArray *rowArray); SArray *rowArray);
// stmt2 binding
int32_t tColDataAddValueByBind2(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen);
typedef struct {
int32_t columnId;
int32_t type;
int32_t bytes;
TAOS_STMT2_BIND *bind;
} SBindInfo2;
int32_t tRowBuildFromBind2(SBindInfo2 *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
SArray *rowArray);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -20,9 +20,9 @@
extern "C" { extern "C" {
#endif #endif
#include "catalog.h"
#include "query.h" #include "query.h"
#include "querynodes.h" #include "querynodes.h"
#include "catalog.h"
typedef struct SStmtCallback { typedef struct SStmtCallback {
TAOS_STMT* pStmt; TAOS_STMT* pStmt;
@ -37,15 +37,15 @@ typedef enum {
} SParseResType; } SParseResType;
typedef struct SParseSchemaRes { typedef struct SParseSchemaRes {
int8_t precision; int8_t precision;
int32_t numOfCols; int32_t numOfCols;
SSchema* pSchema; SSchema* pSchema;
} SParseSchemaRes; } SParseSchemaRes;
typedef struct SParseQueryRes { typedef struct SParseQueryRes {
SNode* pQuery; SNode* pQuery;
SCatalogReq* pCatalogReq; SCatalogReq* pCatalogReq;
SMetaData meta; SMetaData meta;
} SParseQueryRes; } SParseQueryRes;
typedef struct SParseSqlRes { typedef struct SParseSqlRes {
@ -124,11 +124,13 @@ int32_t qSetSTableIdForRsma(SNode* pStmt, int64_t uid);
int32_t qInitKeywordsTable(); int32_t qInitKeywordsTable();
void qCleanupKeywordsTable(); void qCleanupKeywordsTable();
int32_t qAppendStmtTableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx, SStbInterlaceInfo* pBuildInfo); int32_t qAppendStmtTableOutput(SQuery* pQuery, SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
int32_t qBuildStmtFinOutput(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks); SStbInterlaceInfo* pBuildInfo);
//int32_t qBuildStmtOutputFromTbList(SQuery* pQuery, SHashObj* pVgHash, SArray* pBlockList, STableDataCxt* pTbCtx, int32_t tbNum); int32_t qBuildStmtFinOutput(SQuery* pQuery, SHashObj* pAllVgHash, SArray* pVgDataBlocks);
// int32_t qBuildStmtOutputFromTbList(SQuery* pQuery, SHashObj* pVgHash, SArray* pBlockList, STableDataCxt* pTbCtx,
// int32_t tbNum);
int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash); int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash);
int32_t qResetStmtColumns(SArray* pCols, bool deepClear); int32_t qResetStmtColumns(SArray* pCols, bool deepClear);
int32_t qResetStmtDataBlock(STableDataCxt* block, bool keepBuf); int32_t qResetStmtDataBlock(STableDataCxt* block, bool keepBuf);
int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset); int32_t qCloneStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, bool reset);
int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId, int32_t qRebuildStmtDataBlock(STableDataCxt** pDst, STableDataCxt* pSrc, uint64_t uid, uint64_t suid, int32_t vgId,
@ -139,39 +141,51 @@ int32_t qCloneCurrentTbData(STableDataCxt* pDataBlock, SSubmitTbData** pData
int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx); int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx);
int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery); int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery);
int32_t qBindStmtStbColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen, STSchema** pTSchema, SBindInfo* pBindInfos); int32_t qBindStmtStbColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
STSchema** pTSchema, SBindInfo* pBindInfos);
int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen); int32_t qBindStmtColsValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen);
int32_t qBindStmtSingleColValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen, int32_t colIdx, int32_t qBindStmtSingleColValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen,
int32_t rowNum); int32_t colIdx, int32_t rowNum);
int32_t qBuildStmtColFields(void* pDataBlock, int32_t* fieldNum, TAOS_FIELD_E** fields); int32_t qBuildStmtColFields(void* pDataBlock, int32_t* fieldNum, TAOS_FIELD_E** fields);
int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields); int32_t qBuildStmtTagFields(void* pBlock, void* boundTags, int32_t* fieldNum, TAOS_FIELD_E** fields);
int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName, int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName,
TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen); TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen);
int32_t qStmtBindParams2(SQuery* pQuery, TAOS_STMT2_BIND* pParams, int32_t colIdx);
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
STSchema** pTSchema, SBindInfo2* pBindInfos);
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen);
int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
int32_t colIdx, int32_t rowNum);
int32_t qBindStmtTagsValue2(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName,
TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen);
void destroyBoundColumnInfo(void* pBoundInfo); void destroyBoundColumnInfo(void* pBoundInfo);
int32_t qCreateSName(SName* pName, const char* pTableName, int32_t acctId, char* dbName, char* msgBuf, int32_t qCreateSName(SName* pName, const char* pTableName, int32_t acctId, char* dbName, char* msgBuf,
int32_t msgBufLen); int32_t msgBufLen);
void qDestroyBoundColInfo(void* pInfo); void qDestroyBoundColInfo(void* pInfo);
int32_t smlInitHandle(SQuery** query); int32_t smlInitHandle(SQuery** query);
int32_t smlBuildRow(STableDataCxt* pTableCxt); int32_t smlBuildRow(STableDataCxt* pTableCxt);
int32_t smlBuildCol(STableDataCxt* pTableCxt, SSchema* schema, void* kv, int32_t index); int32_t smlBuildCol(STableDataCxt* pTableCxt, SSchema* schema, void* kv, int32_t index);
int32_t smlInitTableDataCtx(SQuery* query, STableMeta* pTableMeta, STableDataCxt** cxt); int32_t smlInitTableDataCtx(SQuery* query, STableMeta* pTableMeta, STableDataCxt** cxt);
void clearColValArraySml(SArray* pCols); void clearColValArraySml(SArray* pCols);
int32_t smlBindData(SQuery* handle, bool dataFormat, SArray* tags, SArray* colsSchema, SArray* cols, int32_t smlBindData(SQuery* handle, bool dataFormat, SArray* tags, SArray* colsSchema, SArray* cols,
STableMeta* pTableMeta, char* tableName, const char* sTableName, int32_t sTableNameLen, int32_t ttl, STableMeta* pTableMeta, char* tableName, const char* sTableName, int32_t sTableNameLen, int32_t ttl,
char* msgBuf, int32_t msgBufLen); char* msgBuf, int32_t msgBufLen);
int32_t smlBuildOutput(SQuery* handle, SHashObj* pVgHash); int32_t smlBuildOutput(SQuery* handle, SHashObj* pVgHash);
int rawBlockBindData(SQuery *query, STableMeta* pTableMeta, void* data, SVCreateTbReq** pCreateTb, TAOS_FIELD *fields, int rawBlockBindData(SQuery* query, STableMeta* pTableMeta, void* data, SVCreateTbReq** pCreateTb, TAOS_FIELD* fields,
int numFields, bool needChangeLength, char* errstr, int32_t errstrLen); int numFields, bool needChangeLength, char* errstr, int32_t errstrLen);
int32_t rewriteToVnodeModifyOpStmt(SQuery* pQuery, SArray* pBufArray); int32_t rewriteToVnodeModifyOpStmt(SQuery* pQuery, SArray* pBufArray);
int32_t serializeVgroupsCreateTableBatch(SHashObj* pVgroupHashmap, SArray** pOut); int32_t serializeVgroupsCreateTableBatch(SHashObj* pVgroupHashmap, SArray** pOut);
int32_t serializeVgroupsDropTableBatch(SHashObj* pVgroupHashmap, SArray** pOut); int32_t serializeVgroupsDropTableBatch(SHashObj* pVgroupHashmap, SArray** pOut);
void destoryCatalogReq(SCatalogReq *pCatalogReq); void destoryCatalogReq(SCatalogReq* pCatalogReq);
bool isPrimaryKeyImpl(SNode* pExpr); bool isPrimaryKeyImpl(SNode* pExpr);
int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx, SStbInterlaceInfo* pBuildInfo); int32_t insAppendStmtTableDataCxt(SHashObj* pAllVgHash, STableColsData* pTbData, STableDataCxt* pTbCtx,
SStbInterlaceInfo* pBuildInfo);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -0,0 +1,235 @@
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TDENGINE_CLIENTSTMT2_H
#define TDENGINE_CLIENTSTMT2_H
#ifdef __cplusplus
extern "C" {
#endif
#include "catalog.h"
/*
typedef enum {
STMT_TYPE_INSERT = 1,
STMT_TYPE_MULTI_INSERT,
STMT_TYPE_QUERY,
} STMT_TYPE;
typedef enum {
STMT_INIT = 1,
STMT_PREPARE,
STMT_SETTBNAME,
STMT_SETTAGS,
STMT_FETCH_FIELDS,
STMT_BIND,
STMT_BIND_COL,
STMT_ADD_BATCH,
STMT_EXECUTE,
STMT_MAX,
} STMT_STATUS;
#define STMT_TABLE_COLS_NUM 1000
typedef struct SStmtTableCache {
STableDataCxt *pDataCtx;
void *boundTags;
} SStmtTableCache;
typedef struct SStmtQueryResInfo {
TAOS_FIELD *fields;
TAOS_FIELD *userFields;
uint32_t numOfCols;
int32_t precision;
} SStmtQueryResInfo;
typedef struct SStmtBindInfo {
bool needParse;
bool inExecCache;
uint64_t tbUid;
uint64_t tbSuid;
int32_t tbVgId;
int32_t sBindRowNum;
int32_t sBindLastIdx;
int8_t tbType;
bool tagsCached;
void *boundTags;
char tbName[TSDB_TABLE_FNAME_LEN];
char tbFName[TSDB_TABLE_FNAME_LEN];
char stbFName[TSDB_TABLE_FNAME_LEN];
SName sname;
char statbName[TSDB_TABLE_FNAME_LEN];
} SStmtBindInfo;
typedef struct SStmtAsyncParam {
STableColsData *pTbData;
void* pStmt;
} SStmtAsyncParam;
typedef struct SStmtExecInfo {
int32_t affectedRows;
SRequestObj *pRequest;
SHashObj *pBlockHash;
STableDataCxt *pCurrBlock;
SSubmitTbData *pCurrTbData;
} SStmtExecInfo;
*/
typedef struct {
bool stbInterlaceMode;
STMT_TYPE type;
STMT_STATUS status;
uint64_t suid;
uint64_t runTimes;
SHashObj *pTableCache; // SHash<SStmtTableCache>
SQuery *pQuery;
char *sqlStr;
int32_t sqlLen;
SArray *nodeList;
SStmtQueryResInfo queryRes;
bool autoCreateTbl;
SHashObj *pVgHash;
SBindInfo2 *pBindInfo;
SStbInterlaceInfo siInfo;
} SStmtSQLInfo2;
/*
typedef struct SStmtStatInfo {
int64_t ctgGetTbMetaNum;
int64_t getCacheTbInfo;
int64_t parseSqlNum;
int64_t bindDataNum;
int64_t setTbNameUs;
int64_t bindDataUs1;
int64_t bindDataUs2;
int64_t bindDataUs3;
int64_t bindDataUs4;
int64_t addBatchUs;
int64_t execWaitUs;
int64_t execUseUs;
} SStmtStatInfo;
typedef struct SStmtQNode {
bool restoreTbCols;
STableColsData tblData;
struct SStmtQNode* next;
} SStmtQNode;
typedef struct SStmtQueue {
bool stopQueue;
SStmtQNode* head;
SStmtQNode* tail;
uint64_t qRemainNum;
} SStmtQueue;
*/
typedef struct {
STscObj *taos;
SCatalog *pCatalog;
int32_t affectedRows;
uint32_t seqId;
uint32_t seqIds[STMT_MAX];
bool bindThreadInUse;
TdThread bindThread;
TAOS_STMT2_OPTION options;
bool stbInterlaceMode;
SStmtQueue queue;
SStmtSQLInfo2 sql;
SStmtExecInfo exec;
SStmtBindInfo bInfo;
int64_t reqid;
int32_t errCode;
tsem_t asyncQuerySem;
SStmtStatInfo stat;
} STscStmt2;
/*
extern char *gStmtStatusStr[];
#define STMT_LOG_SEQ(n) \
do { \
(pStmt)->seqId++; \
(pStmt)->seqIds[n]++; \
STMT_DLOG("the %dth:%d %s", (pStmt)->seqIds[n], (pStmt)->seqId, gStmtStatusStr[n]); \
} while (0)
#define STMT_STATUS_NE(S) (pStmt->sql.status != STMT_##S)
#define STMT_STATUS_EQ(S) (pStmt->sql.status == STMT_##S)
#define STMT_ERR_RET(c) \
do { \
int32_t _code = c; \
if (_code != TSDB_CODE_SUCCESS) { \
terrno = _code; \
pStmt->errCode = _code; \
return _code; \
} \
} while (0)
#define STMT_RET(c) \
do { \
int32_t _code = c; \
if (_code != TSDB_CODE_SUCCESS) { \
terrno = _code; \
pStmt->errCode = _code; \
} \
return _code; \
} while (0)
#define STMT_ERR_JRET(c) \
do { \
code = c; \
if (code != TSDB_CODE_SUCCESS) { \
terrno = code; \
pStmt->errCode = code; \
goto _return; \
} \
} while (0)
#define STMT_ERRI_JRET(c) \
do { \
code = c; \
if (code != TSDB_CODE_SUCCESS) { \
terrno = code; \
goto _return; \
} \
} while (0)
#define STMT_FLOG(param, ...) qFatal("stmt:%p " param, pStmt, __VA_ARGS__)
#define STMT_ELOG(param, ...) qError("stmt:%p " param, pStmt, __VA_ARGS__)
#define STMT_DLOG(param, ...) qDebug("stmt:%p " param, pStmt, __VA_ARGS__)
#define STMT_ELOG_E(param) qError("stmt:%p " param, pStmt)
#define STMT_DLOG_E(param) qDebug("stmt:%p " param, pStmt)
*/
TAOS_STMT2 *stmtInit2(STscObj *taos, TAOS_STMT2_OPTION *pOptions);
int stmtClose2(TAOS_STMT2 *stmt);
int stmtExec2(TAOS_STMT2 *stmt, int *affected_rows);
int stmtPrepare2(TAOS_STMT2 *stmt, const char *sql, unsigned long length);
int stmtSetTbName2(TAOS_STMT2 *stmt, const char *tbName);
int stmtSetTbTags2(TAOS_STMT2 *stmt, TAOS_STMT2_BIND *tags);
int stmtBindBatch2(TAOS_STMT2 *stmt, TAOS_STMT2_BIND *bind, int32_t colIdx);
int stmtGetTagFields2(TAOS_STMT2 *stmt, int *nums, TAOS_FIELD_E **fields);
int stmtGetColFields2(TAOS_STMT2 *stmt, int *nums, TAOS_FIELD_E **fields);
int stmtGetParamNum2(TAOS_STMT2 *stmt, int *nums);
int stmtGetParamTbName(TAOS_STMT2 *stmt, int *nums);
int stmtIsInsert2(TAOS_STMT2 *stmt, int *insert);
TAOS_RES *stmtUseResult2(TAOS_STMT2 *stmt);
const char *stmtErrstr2(TAOS_STMT2 *stmt);
#ifdef __cplusplus
}
#endif
#endif // TDENGINE_CLIENTSTMT2_H

View File

@ -18,6 +18,7 @@
#include "clientLog.h" #include "clientLog.h"
#include "clientMonitor.h" #include "clientMonitor.h"
#include "clientStmt.h" #include "clientStmt.h"
#include "clientStmt2.h"
#include "functionMgt.h" #include "functionMgt.h"
#include "os.h" #include "os.h"
#include "query.h" #include "query.h"
@ -1923,6 +1924,160 @@ int taos_stmt_close(TAOS_STMT *stmt) {
return stmtClose(stmt); return stmtClose(stmt);
} }
TAOS_STMT2 *taos_stmt2_init(TAOS *taos, TAOS_STMT2_OPTION *option) {
STscObj *pObj = acquireTscObj(*(int64_t *)taos);
if (NULL == pObj) {
tscError("invalid parameter for %s", __FUNCTION__);
terrno = TSDB_CODE_TSC_DISCONNECTED;
return NULL;
}
TAOS_STMT2 *pStmt = stmtInit2(pObj, option);
releaseTscObj(*(int64_t *)taos);
return pStmt;
}
int taos_stmt2_prepare(TAOS_STMT2 *stmt, const char *sql, unsigned long length) {
if (stmt == NULL || sql == NULL) {
tscError("NULL parameter for %s", __FUNCTION__);
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
return stmtPrepare2(stmt, sql, length);
}
int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col_idx) {
if (stmt == NULL) {
tscError("NULL parameter for %s", __FUNCTION__);
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
int32_t code = 0;
for (int i = 0; i < bindv->count; ++i) {
if (bindv->tbnames && bindv->tbnames[i]) {
code = stmtSetTbName2(stmt, bindv->tbnames[i]);
if (code) {
return code;
}
}
if (bindv->tags && bindv->tags[i]) {
code = stmtSetTbTags2(stmt, bindv->tags[i]);
if (code) {
return code;
}
}
if (bindv->bind_cols && bindv->bind_cols[i]) {
TAOS_STMT2_BIND *bind = bindv->bind_cols[i];
if (bind->num <= 0 || bind->num > INT16_MAX) {
tscError("invalid bind num %d", bind->num);
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
int32_t insert = 0;
(void)stmtIsInsert2(stmt, &insert);
if (0 == insert && bind->num > 1) {
tscError("only one row data allowed for query");
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
code = stmtBindBatch2(stmt, bind, col_idx);
if (TSDB_CODE_SUCCESS != code) {
return code;
}
}
}
return TSDB_CODE_SUCCESS;
}
int taos_stmt2_exec(TAOS_STMT2 *stmt, int *affected_rows) {
if (stmt == NULL) {
tscError("NULL parameter for %s", __FUNCTION__);
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
return stmtExec2(stmt, affected_rows);
}
int taos_stmt2_close(TAOS_STMT2 *stmt) {
if (stmt == NULL) {
tscError("NULL parameter for %s", __FUNCTION__);
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
return stmtClose2(stmt);
}
/*
int taos_stmt2_param_count(TAOS_STMT2 *stmt, int *nums) {
if (stmt == NULL || nums == NULL) {
tscError("NULL parameter for %s", __FUNCTION__);
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
return stmtGetParamNum2(stmt, nums);
}
*/
int taos_stmt2_is_insert(TAOS_STMT2 *stmt, int *insert) {
if (stmt == NULL || insert == NULL) {
tscError("NULL parameter for %s", __FUNCTION__);
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
return stmtIsInsert2(stmt, insert);
}
int taos_stmt2_get_fields(TAOS_STMT2 *stmt, TAOS_FIELD_T field_type, int *count, TAOS_FIELD_E **fields) {
if (stmt == NULL || NULL == count) {
tscError("NULL parameter for %s", __FUNCTION__);
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
if (field_type == TAOS_FIELD_COL) {
return stmtGetColFields2(stmt, count, fields);
} else if (field_type == TAOS_FIELD_TAG) {
return stmtGetTagFields2(stmt, count, fields);
} else if (field_type == TAOS_FIELD_QUERY) {
return stmtGetParamNum2(stmt, count);
} else if (field_type == TAOS_FIELD_TBNAME) {
return stmtGetParamTbName(stmt, count);
} else {
tscError("invalid parameter for %s", __FUNCTION__);
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
}
void taos_stmt2_free_fields(TAOS_STMT2 *stmt, TAOS_FIELD_E *fields) {
(void)stmt;
if (!fields) return;
taosMemoryFree(fields);
}
TAOS_RES *taos_stmt2_result(TAOS_STMT2 *stmt) {
if (stmt == NULL) {
tscError("NULL parameter for %s", __FUNCTION__);
terrno = TSDB_CODE_INVALID_PARA;
return NULL;
}
return stmtUseResult2(stmt);
}
char *taos_stmt2_error(TAOS_STMT2 *stmt) { return (char *)stmtErrstr2(stmt); }
int taos_set_conn_mode(TAOS *taos, int mode, int value) { int taos_set_conn_mode(TAOS *taos, int mode, int value) {
if (taos == NULL) { if (taos == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;

File diff suppressed because it is too large Load Diff

View File

@ -3124,6 +3124,198 @@ _exit:
return code; return code;
} }
int32_t tColDataAddValueByBind2(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen) {
int32_t code = 0;
if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
if (!(pColData->type == pBind->buffer_type)) {
return TSDB_CODE_INVALID_PARA;
}
}
if (IS_VAR_DATA_TYPE(pColData->type)) { // var-length data type
uint8_t *buf = pBind->buffer;
for (int32_t i = 0; i < pBind->num; ++i) {
if (pBind->is_null && pBind->is_null[i]) {
if (pColData->cflag & COL_IS_KEY) {
code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
goto _exit;
}
if (pBind->is_null[i] == 1) {
code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
if (code) goto _exit;
} else {
code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
if (code) goto _exit;
}
} else if (pBind->length[i] > buffMaxLen) {
uError("var data length too big, len:%d, max:%d", pBind->length[i], buffMaxLen);
return TSDB_CODE_INVALID_PARA;
} else {
code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
buf += pBind->length[i];
}
}
} else { // fixed-length data type
bool allValue;
bool allNull;
bool allNone;
if (pBind->is_null) {
bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
allNull = (same && pBind->is_null[0] == 1);
allNone = (same && pBind->is_null[0] > 1);
allValue = (same && pBind->is_null[0] == 0);
} else {
allNull = false;
allNone = false;
allValue = true;
}
if ((pColData->cflag & COL_IS_KEY) && !allValue) {
code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
goto _exit;
}
if (allValue) {
// optimize (todo)
for (int32_t i = 0; i < pBind->num; ++i) {
uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
*val = 1;
}
code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
}
} else if (allNull) {
// optimize (todo)
for (int32_t i = 0; i < pBind->num; ++i) {
code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
if (code) goto _exit;
}
} else if (allNone) {
// optimize (todo)
for (int32_t i = 0; i < pBind->num; ++i) {
code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
if (code) goto _exit;
}
} else {
for (int32_t i = 0; i < pBind->num; ++i) {
if (pBind->is_null[i]) {
if (pBind->is_null[i] == 1) {
code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
if (code) goto _exit;
} else {
code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
if (code) goto _exit;
}
} else {
uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
*val = 1;
}
code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
}
}
}
}
_exit:
return code;
}
/* build rows to `rowArray` from bind
* `infos` is the bind information array
* `numOfInfos` is the number of bind information
* `infoSorted` is whether the bind information is sorted by column id
* `pTSchema` is the schema of the table
* `rowArray` is the array to store the rows
*/
int32_t tRowBuildFromBind2(SBindInfo2 *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
SArray *rowArray) {
if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
return TSDB_CODE_INVALID_PARA;
}
if (!infoSorted) {
taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
}
int32_t code = 0;
int32_t numOfRows = infos[0].bind->num;
SArray *colValArray, *bufArray;
SColVal colVal;
if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
return terrno;
}
if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
taosArrayDestroy(colValArray);
return TSDB_CODE_OUT_OF_MEMORY;
}
for (int i = 0; i < numOfInfos; ++i) {
if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
taosArrayDestroy(colValArray);
taosArrayDestroy(bufArray);
return TSDB_CODE_OUT_OF_MEMORY;
}
}
for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
taosArrayClear(colValArray);
for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
if (infos[iInfo].bind->is_null[iRow] == 1) {
colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
} else {
colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
}
} else {
SValue value = {
.type = infos[iInfo].type,
};
if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
int32_t length = infos[iInfo].bind->length[iRow];
uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo];
value.nData = length;
value.pData = *data;
*data += length;
// value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
} else {
uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
*val = 1;
}
(void)memcpy(&value.val, val,
/*(uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,*/
infos[iInfo].bytes /*bind->buffer_length*/);
}
colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
}
if (taosArrayPush(colValArray, &colVal) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
}
}
SRow *row;
if ((code = tRowBuild(colValArray, pTSchema, &row))) {
goto _exit;
}
if ((taosArrayPush(rowArray, &row)) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
}
}
_exit:
taosArrayDestroy(colValArray);
taosArrayDestroy(bufArray);
return code;
}
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) { static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
int32_t code = TSDB_CODE_SUCCESS; int32_t code = TSDB_CODE_SUCCESS;

View File

@ -448,7 +448,407 @@ int32_t qBindStmtSingleColValue(void* pBlock, SArray* pCols, TAOS_MULTI_BIND* bi
pBind = bind; pBind = bind;
} }
code = tColDataAddValueByBind(pCol, pBind, IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1); code = tColDataAddValueByBind(pCol, pBind,
IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1);
qDebug("stmt col %d bind %d rows data", colIdx, rowNum);
_return:
taosMemoryFree(ncharBind.buffer);
taosMemoryFree(ncharBind.length);
return code;
}
int32_t qBindStmtTagsValue2(void* pBlock, void* boundTags, int64_t suid, const char* sTableName, char* tName,
TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen) {
STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
SMsgBuf pBuf = {.buf = msgBuf, .len = msgBufLen};
int32_t code = TSDB_CODE_SUCCESS;
SBoundColInfo* tags = (SBoundColInfo*)boundTags;
if (NULL == tags) {
return TSDB_CODE_APP_ERROR;
}
SArray* pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
if (!pTagArray) {
return buildInvalidOperationMsg(&pBuf, "out of memory");
}
SArray* tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
if (!tagName) {
code = buildInvalidOperationMsg(&pBuf, "out of memory");
goto end;
}
SSchema* pSchema = getTableTagSchema(pDataBlock->pMeta);
bool isJson = false;
STag* pTag = NULL;
for (int c = 0; c < tags->numOfBound; ++c) {
if (bind[c].is_null && bind[c].is_null[0]) {
continue;
}
SSchema* pTagSchema = &pSchema[tags->pColIndex[c]];
int32_t colLen = pTagSchema->bytes;
if (IS_VAR_DATA_TYPE(pTagSchema->type)) {
colLen = bind[c].length[0];
if ((colLen + VARSTR_HEADER_SIZE) > pTagSchema->bytes) {
code = buildInvalidOperationMsg(&pBuf, "tag length is too big");
goto end;
}
}
if (NULL == taosArrayPush(tagName, pTagSchema->name)) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto end;
}
if (pTagSchema->type == TSDB_DATA_TYPE_JSON) {
if (colLen > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
code = buildSyntaxErrMsg(&pBuf, "json string too long than 4095", bind[c].buffer);
goto end;
}
isJson = true;
char* tmp = taosMemoryCalloc(1, colLen + 1);
memcpy(tmp, bind[c].buffer, colLen);
code = parseJsontoTagData(tmp, pTagArray, &pTag, &pBuf);
taosMemoryFree(tmp);
if (code != TSDB_CODE_SUCCESS) {
goto end;
}
} else {
STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
// strcpy(val.colName, pTagSchema->name);
if (pTagSchema->type == TSDB_DATA_TYPE_BINARY || pTagSchema->type == TSDB_DATA_TYPE_VARBINARY ||
pTagSchema->type == TSDB_DATA_TYPE_GEOMETRY) {
val.pData = (uint8_t*)bind[c].buffer;
val.nData = colLen;
} else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
int32_t output = 0;
void* p = taosMemoryCalloc(1, colLen * TSDB_NCHAR_SIZE);
if (p == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto end;
}
if (!taosMbsToUcs4(bind[c].buffer, colLen, (TdUcs4*)(p), colLen * TSDB_NCHAR_SIZE, &output)) {
if (errno == E2BIG) {
taosMemoryFree(p);
code = generateSyntaxErrMsg(&pBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
goto end;
}
char buf[512] = {0};
snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(errno));
taosMemoryFree(p);
code = buildSyntaxErrMsg(&pBuf, buf, bind[c].buffer);
goto end;
}
val.pData = p;
val.nData = output;
} else {
memcpy(&val.i64, bind[c].buffer, colLen);
}
if (NULL == taosArrayPush(pTagArray, &val)) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto end;
}
}
}
if (!isJson && (code = tTagNew(pTagArray, 1, false, &pTag)) != TSDB_CODE_SUCCESS) {
goto end;
}
if (NULL == pDataBlock->pData->pCreateTbReq) {
pDataBlock->pData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
if (NULL == pDataBlock->pData->pCreateTbReq) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto end;
}
}
insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName,
pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL);
pTag = NULL;
end:
for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
if (p->type == TSDB_DATA_TYPE_NCHAR) {
taosMemoryFreeClear(p->pData);
}
}
taosArrayDestroy(pTagArray);
taosArrayDestroy(tagName);
taosMemoryFree(pTag);
return code;
}
static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst) {
int32_t output = 0;
const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
dst->buffer = taosMemoryCalloc(src->num, max_buf_len);
if (NULL == dst->buffer) {
return TSDB_CODE_OUT_OF_MEMORY;
}
dst->length = taosMemoryCalloc(src->num, sizeof(int32_t));
if (NULL == dst->length) {
taosMemoryFreeClear(dst->buffer);
return TSDB_CODE_OUT_OF_MEMORY;
}
char* src_buf = src->buffer;
char* dst_buf = dst->buffer;
for (int32_t i = 0; i < src->num; ++i) {
if (src->is_null && src->is_null[i]) {
continue;
}
if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output)) {
if (errno == E2BIG) {
return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
}
char buf[512] = {0};
snprintf(buf, tListLen(buf), "%s", strerror(errno));
return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
}
dst->length[i] = output;
src_buf += src->length[i];
dst_buf += output;
}
dst->buffer_type = src->buffer_type;
dst->is_null = src->is_null;
dst->num = src->num;
return TSDB_CODE_SUCCESS;
}
int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
STSchema** pTSchema, SBindInfo2* pBindInfos) {
STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
SSchema* pSchema = getTableColumnSchema(pDataBlock->pMeta);
SBoundColInfo* boundInfo = &pDataBlock->boundColsInfo;
SMsgBuf pBuf = {.buf = msgBuf, .len = msgBufLen};
int32_t rowNum = bind->num;
SArray* ncharBinds = NULL;
TAOS_STMT2_BIND ncharBind = {0};
int32_t code = 0;
int16_t lastColId = -1;
bool colInOrder = true;
if (NULL == *pTSchema) {
*pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion);
}
for (int c = 0; c < boundInfo->numOfBound; ++c) {
SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
if (pColSchema->colId <= lastColId) {
colInOrder = false;
} else {
lastColId = pColSchema->colId;
}
if (bind[c].num != rowNum) {
code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
goto _return;
}
if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
bind[c].buffer_type != pColSchema->type) { // for rowNum ==1 , connector may not set buffer_type
code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
goto _return;
}
if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind);
if (code) {
goto _return;
}
if (!ncharBinds) {
ncharBinds = taosArrayInit(1, sizeof(ncharBind));
if (!ncharBinds) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _return;
}
}
if (!taosArrayPush(ncharBinds, &ncharBind)) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _return;
}
pBindInfos[c].bind = taosArrayGetLast(ncharBinds);
} else {
pBindInfos[c].bind = bind + c;
}
pBindInfos[c].columnId = pColSchema->colId;
pBindInfos[c].type = pColSchema->type;
pBindInfos[c].bytes = pColSchema->bytes;
}
code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols);
qDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
_return:
if (ncharBinds) {
for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) {
TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds);
taosMemoryFree(ncBind[i].buffer);
taosMemoryFree(ncBind[i].length);
}
taosArrayDestroy(ncharBinds);
}
return code;
}
static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst) {
int32_t output = 0;
const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE;
int32_t newBuflen = (pSchema->bytes - VARSTR_HEADER_SIZE) * src->num;
// if (dst->buffer_length < newBuflen) {
dst->buffer = taosMemoryRealloc(dst->buffer, newBuflen);
if (NULL == dst->buffer) {
return TSDB_CODE_OUT_OF_MEMORY;
}
//}
if (NULL == dst->length) {
dst->length = taosMemoryRealloc(dst->length, sizeof(int32_t) * src->num);
if (NULL == dst->length) {
taosMemoryFreeClear(dst->buffer);
return TSDB_CODE_OUT_OF_MEMORY;
}
}
// dst->buffer_length = pSchema->bytes - VARSTR_HEADER_SIZE;
char* src_buf = src->buffer;
char* dst_buf = dst->buffer;
for (int32_t i = 0; i < src->num; ++i) {
if (src->is_null && src->is_null[i]) {
continue;
}
/*if (!taosMbsToUcs4(((char*)src->buffer) + src->buffer_length * i, src->length[i],
(TdUcs4*)(((char*)dst->buffer) + dst->buffer_length * i), dst->buffer_length, &output)) {*/
if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output)) {
if (errno == E2BIG) {
return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name);
}
char buf[512] = {0};
snprintf(buf, tListLen(buf), "%s", strerror(errno));
return buildSyntaxErrMsg(pMsgBuf, buf, NULL);
}
dst->length[i] = output;
src_buf += src->length[i];
dst_buf += output;
}
dst->buffer_type = src->buffer_type;
dst->is_null = src->is_null;
dst->num = src->num;
return TSDB_CODE_SUCCESS;
}
int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen) {
STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
SSchema* pSchema = getTableColumnSchema(pDataBlock->pMeta);
SBoundColInfo* boundInfo = &pDataBlock->boundColsInfo;
SMsgBuf pBuf = {.buf = msgBuf, .len = msgBufLen};
int32_t rowNum = bind->num;
TAOS_STMT2_BIND ncharBind = {0};
TAOS_STMT2_BIND* pBind = NULL;
int32_t code = 0;
for (int c = 0; c < boundInfo->numOfBound; ++c) {
SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]];
SColData* pCol = taosArrayGet(pCols, c);
if (bind[c].num != rowNum) {
code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
goto _return;
}
if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) &&
bind[c].buffer_type != pColSchema->type) { // for rowNum ==1 , connector may not set buffer_type
code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
goto _return;
}
if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
code = convertStmtNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind);
if (code) {
goto _return;
}
pBind = &ncharBind;
} else {
pBind = bind + c;
}
code = tColDataAddValueByBind2(pCol, pBind,
IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1);
if (code) {
goto _return;
}
}
qDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum);
_return:
taosMemoryFree(ncharBind.buffer);
taosMemoryFree(ncharBind.length);
return code;
}
int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen,
int32_t colIdx, int32_t rowNum) {
STableDataCxt* pDataBlock = (STableDataCxt*)pBlock;
SSchema* pSchema = getTableColumnSchema(pDataBlock->pMeta);
SBoundColInfo* boundInfo = &pDataBlock->boundColsInfo;
SMsgBuf pBuf = {.buf = msgBuf, .len = msgBufLen};
SSchema* pColSchema = &pSchema[boundInfo->pColIndex[colIdx]];
SColData* pCol = taosArrayGet(pCols, colIdx);
TAOS_STMT2_BIND ncharBind = {0};
TAOS_STMT2_BIND* pBind = NULL;
int32_t code = 0;
if (bind->num != rowNum) {
return buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same");
}
// Column index exceeds the number of columns
if (colIdx >= pCols->size && pCol == NULL) {
return buildInvalidOperationMsg(&pBuf, "column index exceeds the number of columns");
}
if (bind->buffer_type != pColSchema->type) {
return buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type");
}
if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) {
code = convertStmtNcharCol2(&pBuf, pColSchema, bind, &ncharBind);
if (code) {
goto _return;
}
pBind = &ncharBind;
} else {
pBind = bind;
}
code = tColDataAddValueByBind2(pCol, pBind,
IS_VAR_DATA_TYPE(pColSchema->type) ? pColSchema->bytes - VARSTR_HEADER_SIZE : -1);
qDebug("stmt col %d bind %d rows data", colIdx, rowNum); qDebug("stmt col %d bind %d rows data", colIdx, rowNum);
@ -533,7 +933,7 @@ int32_t qResetStmtColumns(SArray* pCols, bool deepClear) {
for (int32_t i = 0; i < colNum; ++i) { for (int32_t i = 0; i < colNum; ++i) {
SColData* pCol = (SColData*)taosArrayGet(pCols, i); SColData* pCol = (SColData*)taosArrayGet(pCols, i);
if (pCol == NULL){ if (pCol == NULL) {
qError("qResetStmtColumns column is NULL"); qError("qResetStmtColumns column is NULL");
return TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_OUT_OF_MEMORY;
} }
@ -553,7 +953,7 @@ int32_t qResetStmtDataBlock(STableDataCxt* block, bool deepClear) {
for (int32_t i = 0; i < colNum; ++i) { for (int32_t i = 0; i < colNum; ++i) {
SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i); SColData* pCol = (SColData*)taosArrayGet(pBlock->pData->aCol, i);
if (pCol == NULL){ if (pCol == NULL) {
qError("qResetStmtDataBlock column is NULL"); qError("qResetStmtDataBlock column is NULL");
return TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_OUT_OF_MEMORY;
} }

View File

@ -76,33 +76,33 @@ bool qIsCreateTbFromFileSql(const char* pStr, size_t length) {
} }
bool qParseDbName(const char* pStr, size_t length, char** pDbName) { bool qParseDbName(const char* pStr, size_t length, char** pDbName) {
(void) length; (void)length;
int32_t index = 0; int32_t index = 0;
SToken t; SToken t;
if (NULL == pStr) { if (NULL == pStr) {
*pDbName = NULL; *pDbName = NULL;
return false; return false;
} }
t = tStrGetToken((char *) pStr, &index, false, NULL); t = tStrGetToken((char*)pStr, &index, false, NULL);
if (TK_INSERT != t.type && TK_IMPORT != t.type) { if (TK_INSERT != t.type && TK_IMPORT != t.type) {
*pDbName = NULL; *pDbName = NULL;
return false; return false;
} }
t = tStrGetToken((char *) pStr, &index, false, NULL); t = tStrGetToken((char*)pStr, &index, false, NULL);
if (TK_INTO != t.type) { if (TK_INTO != t.type) {
*pDbName = NULL; *pDbName = NULL;
return false; return false;
} }
t = tStrGetToken((char *) pStr, &index, false, NULL); t = tStrGetToken((char*)pStr, &index, false, NULL);
if (t.n == 0 || t.z == NULL) { if (t.n == 0 || t.z == NULL) {
*pDbName = NULL; *pDbName = NULL;
return false; return false;
} }
char *dotPos = strnchr(t.z, '.', t.n, true); char* dotPos = strnchr(t.z, '.', t.n, true);
if (dotPos != NULL) { if (dotPos != NULL) {
int dbNameLen = dotPos - t.z; int dbNameLen = dotPos - t.z;
*pDbName = taosMemoryMalloc(dbNameLen + 1); *pDbName = taosMemoryMalloc(dbNameLen + 1);
@ -331,13 +331,12 @@ int32_t qContinueParsePostQuery(SParseContext* pCxt, SQuery* pQuery, SSDataBlock
return code; return code;
} }
static void destoryTablesReq(void* p) {
static void destoryTablesReq(void *p) { STablesReq* pRes = (STablesReq*)p;
STablesReq *pRes = (STablesReq *)p;
taosArrayDestroy(pRes->pTables); taosArrayDestroy(pRes->pTables);
} }
void destoryCatalogReq(SCatalogReq *pCatalogReq) { void destoryCatalogReq(SCatalogReq* pCatalogReq) {
if (NULL == pCatalogReq) { if (NULL == pCatalogReq) {
return; return;
} }
@ -369,7 +368,6 @@ void destoryCatalogReq(SCatalogReq *pCatalogReq) {
taosArrayDestroy(pCatalogReq->pTableTag); taosArrayDestroy(pCatalogReq->pTableTag);
} }
void tfreeSParseQueryRes(void* p) { void tfreeSParseQueryRes(void* p) {
if (NULL == p) { if (NULL == p) {
return; return;
@ -410,9 +408,7 @@ int32_t qSetSTableIdForRsma(SNode* pStmt, int64_t uid) {
return TSDB_CODE_FAILED; return TSDB_CODE_FAILED;
} }
int32_t qInitKeywordsTable() { int32_t qInitKeywordsTable() { return taosInitKeywordsTable(); }
return taosInitKeywordsTable();
}
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); } void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
@ -445,6 +441,98 @@ int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx
return code; return code;
} }
static int32_t setValueByBindParam2(SValueNode* pVal, TAOS_STMT2_BIND* pParam) {
if (IS_VAR_DATA_TYPE(pVal->node.resType.type)) {
taosMemoryFreeClear(pVal->datum.p);
}
if (pParam->is_null && 1 == *(pParam->is_null)) {
pVal->node.resType.type = TSDB_DATA_TYPE_NULL;
pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
return TSDB_CODE_SUCCESS;
}
int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
pVal->node.resType.type = pParam->buffer_type;
pVal->node.resType.bytes = inputSize;
switch (pParam->buffer_type) {
case TSDB_DATA_TYPE_VARBINARY:
pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
if (NULL == pVal->datum.p) {
return TSDB_CODE_OUT_OF_MEMORY;
}
varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
memcpy(varDataVal(pVal->datum.p), pParam->buffer, pVal->node.resType.bytes);
pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
break;
case TSDB_DATA_TYPE_VARCHAR:
case TSDB_DATA_TYPE_GEOMETRY:
pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
if (NULL == pVal->datum.p) {
return TSDB_CODE_OUT_OF_MEMORY;
}
varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
break;
case TSDB_DATA_TYPE_NCHAR: {
pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
if (NULL == pVal->datum.p) {
return TSDB_CODE_OUT_OF_MEMORY;
}
int32_t output = 0;
if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
&output)) {
return errno;
}
varDataSetLen(pVal->datum.p, output);
pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
break;
}
default: {
int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
if (code) {
return code;
}
break;
}
}
pVal->translate = true;
return TSDB_CODE_SUCCESS;
}
int32_t qStmtBindParams2(SQuery* pQuery, TAOS_STMT2_BIND* pParams, int32_t colIdx) {
int32_t code = TSDB_CODE_SUCCESS;
if (colIdx < 0) {
int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
for (int32_t i = 0; i < size; ++i) {
code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i);
if (TSDB_CODE_SUCCESS != code) {
return code;
}
}
} else {
code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams);
}
if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
nodesDestroyNode(pQuery->pRoot);
pQuery->pRoot = NULL;
code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
if (NULL == pQuery->pRoot) {
code = code;
}
}
if (TSDB_CODE_SUCCESS == code) {
rewriteExprAlias(pQuery->pRoot);
}
return code;
}
int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery) { int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery) {
int32_t code = translate(pCxt, pQuery, NULL); int32_t code = translate(pCxt, pQuery, NULL);
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {

View File

@ -7,11 +7,13 @@ LFLAGS = '-Wl,-rpath,/usr/local/taos/driver/' -ltaos -lpthread -lm -lrt
CFLAGS = -O0 -g -Wno-deprecated -fPIC -Wno-unused-result -Wconversion \ CFLAGS = -O0 -g -Wno-deprecated -fPIC -Wno-unused-result -Wconversion \
-Wno-char-subscripts -D_REENTRANT -Wno-format -D_REENTRANT -DLINUX \ -Wno-char-subscripts -D_REENTRANT -Wno-format -D_REENTRANT -DLINUX \
-Wno-unused-function -D_M_X64 -I/usr/local/taos/include -std=gnu99 -Wno-sign-conversion -Wno-unused-function -D_M_X64 -I/usr/local/taos/include -std=gnu99 -Wno-sign-conversion
#CFLAGS += -fsanitize=address -fsanitize=undefined -fsanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=shift-base -fno-sanitize=alignment
all: $(TARGET) all: $(TARGET)
exe: exe:
gcc $(CFLAGS) ./batchprepare.c -o $(ROOT)batchprepare $(LFLAGS) gcc $(CFLAGS) ./batchprepare.c -o $(ROOT)batchprepare $(LFLAGS)
gcc $(CFLAGS) ./stmt2-test.c -o $(ROOT)stmt2-test $(LFLAGS)
gcc $(CFLAGS) ./stopquery.c -o $(ROOT)stopquery $(LFLAGS) gcc $(CFLAGS) ./stopquery.c -o $(ROOT)stopquery $(LFLAGS)
gcc $(CFLAGS) ./dbTableRoute.c -o $(ROOT)dbTableRoute $(LFLAGS) gcc $(CFLAGS) ./dbTableRoute.c -o $(ROOT)dbTableRoute $(LFLAGS)
gcc $(CFLAGS) ./insertSameTs.c -o $(ROOT)insertSameTs $(LFLAGS) gcc $(CFLAGS) ./insertSameTs.c -o $(ROOT)insertSameTs $(LFLAGS)
@ -20,9 +22,15 @@ exe:
gcc $(CFLAGS) ./insert_stb.c -o $(ROOT)insert_stb $(LFLAGS) gcc $(CFLAGS) ./insert_stb.c -o $(ROOT)insert_stb $(LFLAGS)
gcc $(CFLAGS) ./tmqViewTest.c -o $(ROOT)tmqViewTest $(LFLAGS) gcc $(CFLAGS) ./tmqViewTest.c -o $(ROOT)tmqViewTest $(LFLAGS)
gcc $(CFLAGS) ./stmtQuery.c -o $(ROOT)stmtQuery $(LFLAGS) gcc $(CFLAGS) ./stmtQuery.c -o $(ROOT)stmtQuery $(LFLAGS)
gcc $(CFLAGS) ./stmt.c -o $(ROOT)stmt $(LFLAGS)
gcc $(CFLAGS) ./stmt2.c -o $(ROOT)stmt2 $(LFLAGS)
gcc $(CFLAGS) ./stmt2-example.c -o $(ROOT)stmt2-example $(LFLAGS)
gcc $(CFLAGS) ./stmt2-nohole.c -o $(ROOT)stmt2-nohole $(LFLAGS)
gcc $(CFLAGS) ./stmt-crash.c -o $(ROOT)stmt-crash $(LFLAGS)
clean: clean:
rm $(ROOT)batchprepare rm $(ROOT)batchprepare
rm $(ROOT)stmt2-test
rm $(ROOT)stopquery rm $(ROOT)stopquery
rm $(ROOT)dbTableRoute rm $(ROOT)dbTableRoute
rm $(ROOT)insertSameTs rm $(ROOT)insertSameTs
@ -31,3 +39,8 @@ clean:
rm $(ROOT)insert_stb rm $(ROOT)insert_stb
rm $(ROOT)tmqViewTest rm $(ROOT)tmqViewTest
rm $(ROOT)stmtQuery rm $(ROOT)stmtQuery
rm $(ROOT)stmt
rm $(ROOT)stmt2
rm $(ROOT)stmt2-example
rm $(ROOT)stmt2-nohole
rm $(ROOT)stmt-crash

View File

@ -0,0 +1,289 @@
// TAOS standard API example. The same syntax as MySQL, but only a subet
// to compile: gcc -o prepare prepare.c -ltaos
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "taos.h"
void taosMsleep(int mseconds);
int main(int argc, char *argv[]) {
TAOS *taos;
TAOS_RES *result;
int code;
TAOS_STMT *stmt;
// connect to server
if (argc < 2) {
printf("please input server ip \n");
return 0;
}
// taos = taos_connect(argv[1], "root", "Taosdata.1234", NULL, 0);
taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("failed to connect to db, reason:%s\n", taos_errstr(taos));
exit(1);
}
result = taos_query(taos, "drop database demo");
taos_free_result(result);
result = taos_query(taos, "create database demo");
code = taos_errno(result);
if (code != 0) {
printf("failed to create database, reason:%s\n", taos_errstr(result));
taos_free_result(result);
exit(1);
}
taos_free_result(result);
result = taos_query(taos, "use demo");
taos_free_result(result);
// create table
const char *sql =
"create stable s1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin "
"binary(40), blob nchar(10), varbin varbinary(16)) tags(groupId INT);";
result = taos_query(taos, sql);
code = taos_errno(result);
if (code != 0) {
printf("failed to create table, reason:%s\n", taos_errstr(result));
taos_free_result(result);
exit(1);
}
taos_free_result(result);
// sleep for one second to make sure table is created on data node
// taosMsleep(1000);
// insert 10 records
struct {
int64_t ts;
int8_t b;
int8_t v1;
int16_t v2;
int32_t v4;
int64_t v8;
float f4;
double f8;
char bin[40];
char blob[80];
int8_t varbin[16];
} v = {0};
int32_t boolLen = sizeof(int8_t);
int32_t sintLen = sizeof(int16_t);
int32_t intLen = sizeof(int32_t);
int32_t bintLen = sizeof(int64_t);
int32_t floatLen = sizeof(float);
int32_t doubleLen = sizeof(double);
int32_t binLen = sizeof(v.bin);
int32_t ncharLen = 30;
stmt = taos_stmt_init(taos);
TAOS_MULTI_BIND params[11];
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(v.ts);
params[0].buffer = &v.ts;
params[0].length = &bintLen;
params[0].is_null = NULL;
params[0].num = 1;
params[1].buffer_type = TSDB_DATA_TYPE_BOOL;
params[1].buffer_length = sizeof(v.b);
params[1].buffer = &v.b;
params[1].length = &boolLen;
params[1].is_null = NULL;
params[1].num = 1;
params[2].buffer_type = TSDB_DATA_TYPE_TINYINT;
params[2].buffer_length = sizeof(v.v1);
params[2].buffer = &v.v1;
params[2].length = &boolLen;
params[2].is_null = NULL;
params[2].num = 1;
params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT;
params[3].buffer_length = sizeof(v.v2);
params[3].buffer = &v.v2;
params[3].length = &sintLen;
params[3].is_null = NULL;
params[3].num = 1;
params[4].buffer_type = TSDB_DATA_TYPE_INT;
params[4].buffer_length = sizeof(v.v4);
params[4].buffer = &v.v4;
params[4].length = &intLen;
params[4].is_null = NULL;
params[4].num = 1;
params[5].buffer_type = TSDB_DATA_TYPE_BIGINT;
params[5].buffer_length = sizeof(v.v8);
params[5].buffer = &v.v8;
params[5].length = &bintLen;
params[5].is_null = NULL;
params[5].num = 1;
params[6].buffer_type = TSDB_DATA_TYPE_FLOAT;
params[6].buffer_length = sizeof(v.f4);
params[6].buffer = &v.f4;
params[6].length = &floatLen;
params[6].is_null = NULL;
params[6].num = 1;
params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE;
params[7].buffer_length = sizeof(v.f8);
params[7].buffer = &v.f8;
params[7].length = &doubleLen;
params[7].is_null = NULL;
params[7].num = 1;
params[8].buffer_type = TSDB_DATA_TYPE_BINARY;
params[8].buffer_length = sizeof(v.bin);
params[8].buffer = v.bin;
params[8].length = &binLen;
params[8].is_null = NULL;
params[8].num = 1;
strcpy(v.blob, "一二三四五六七八九十");
params[9].buffer_type = TSDB_DATA_TYPE_NCHAR;
params[9].buffer_length = sizeof(v.blob);
params[9].buffer = v.blob;
params[9].length = &ncharLen;
params[9].is_null = NULL;
params[9].num = 1;
int8_t tmp[16] = {'a', 0, 1, 13, '1'};
int32_t vbinLen = 5;
memcpy(v.varbin, tmp, sizeof(v.varbin));
params[10].buffer_type = TSDB_DATA_TYPE_VARBINARY;
params[10].buffer_length = sizeof(v.varbin);
params[10].buffer = v.varbin;
params[10].length = &vbinLen;
params[10].is_null = NULL;
params[10].num = 1;
TAOS_MULTI_BIND tags[1];
int32_t id1 = 0;
tags[0].buffer_type = TSDB_DATA_TYPE_INT;
tags[0].buffer_length = sizeof(int);
tags[0].buffer = &id1;
tags[0].length = NULL;
tags[0].is_null = NULL;
tags[0].num = 1;
char is_null = 1;
sql = "insert into ? using s1 tags(?) values(?,?,?,?,?,?,?,?,?,?,?)";
code = taos_stmt_prepare(stmt, sql, 0);
if (code != 0) {
printf("failed to execute taos_stmt_prepare. code:0x%x\n", code);
}
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
exit(EXIT_FAILURE);
}
code = taos_stmt_set_tbname_tags(stmt, "m1", tags);
v.ts = 1591060628000;
v.b = (int8_t)0 % 2;
v.v1 = (int8_t)0;
v.v2 = (int16_t)(0 * 2);
v.v4 = (int32_t)(0 * 4);
v.v8 = (int64_t)(0 * 8);
v.f4 = (float)(0 * 40);
v.f8 = (double)(0 * 80);
for (int j = 0; j < sizeof(v.bin); ++j) {
v.bin[j] = (char)(0 + '0');
}
taos_stmt_bind_param(stmt, params);
taos_stmt_add_batch(stmt);
code = taos_stmt_set_tbname_tags(stmt, "m2", tags);
v.ts = 1591060628000 + 1;
v.b = (int8_t)1 % 2;
v.v1 = (int8_t)1;
v.v2 = (int16_t)(1 * 2);
v.v4 = (int32_t)(1 * 4);
v.v8 = (int64_t)(1 * 8);
v.f4 = (float)(1 * 40);
v.f8 = (double)(1 * 80);
for (int j = 0; j < sizeof(v.bin); ++j) {
v.bin[j] = (char)(1 + '0');
}
taos_stmt_bind_param(stmt, params);
taos_stmt_add_batch(stmt);
/*
for (int i = 0; i < 10; ++i) {
if(i % 2 != 0) {
code = taos_stmt_set_tbname(stmt, "m1");
} else {
code = taos_stmt_set_tbname(stmt, "m2");
}
v.ts += 1;
for (int j = 1; j < 11; ++j) {
params[j].is_null = ((i == j) ? &is_null : 0);
}
v.b = (int8_t)i % 2;
v.v1 = (int8_t)i;
v.v2 = (int16_t)(i * 2);
v.v4 = (int32_t)(i * 4);
v.v8 = (int64_t)(i * 8);
v.f4 = (float)(i * 40);
v.f8 = (double)(i * 80);
for (int j = 0; j < sizeof(v.bin); ++j) {
v.bin[j] = (char)(i + '0');
}
taos_stmt_bind_param(stmt, params);
taos_stmt_add_batch(stmt);
}
*/
// printf("taos multi bind num:%d\n", params[1].num);
if (taos_stmt_execute(stmt) != 0) {
printf("failed to execute insert statement.\n");
exit(1);
}
taos_stmt_close(stmt);
// query the records
stmt = taos_stmt_init(taos);
taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0);
v.v1 = 5;
v.v2 = 15;
taos_stmt_bind_param(stmt, params + 2);
if (taos_stmt_execute(stmt) != 0) {
printf("failed to execute select statement.\n");
exit(1);
}
result = taos_stmt_use_result(stmt);
TAOS_ROW row;
int rows = 0;
int num_fields = taos_num_fields(result);
TAOS_FIELD *fields = taos_fetch_fields(result);
// fetch the records row by row
while ((row = taos_fetch_row(result))) {
char temp[256] = {0};
rows++;
taos_print_row(temp, row, fields, num_fields);
printf("%s\n", temp);
}
if (rows == 2) {
printf("two rows are fetched as expectation\n");
} else {
printf("expect two rows, but %d rows are fetched\n", rows);
}
// taos_free_result(result);
taos_stmt_close(stmt);
return 0;
}

View File

@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "taos.h"
void do_query(TAOS* taos, const char* sql) {
TAOS_RES* result = taos_query(taos, sql);
int code = taos_errno(result);
if (code) {
printf("failed to query: %s, reason:%s\n", sql, taos_errstr(result));
taos_free_result(result);
return;
}
taos_free_result(result);
}
void do_stmt(TAOS* taos) {
do_query(taos, "drop database if exists db");
do_query(taos, "create database db");
do_query(taos, "create table db.stb (ts timestamp, b binary(10)) tags(t1 int, t2 binary(10))");
struct {
int64_t ts[2];
char b[16];
} v;
int32_t b_len[2], t64_len[2];
char is_null[2] = {0};
char is_null2[2] = {0, 2};
// TAOS_STMT2_OPTION option = {0};
// TAOS_STMT2_OPTION option = {0, true, true, stmtAsyncQueryCb, NULL};
TAOS_STMT2_OPTION option = {0, true, false, NULL, NULL};
char* tbs[2] = {"tb", "tb2"};
int t1_val[2] = {0, 1};
int t2_len[2] = {3, 3};
TAOS_STMT2_BIND tags[2][2] = {{{0, &t1_val[0], NULL, NULL, 0}, {0, "a1", &t2_len[0], NULL, 0}},
{{0, &t1_val[1], NULL, NULL, 0}, {0, "a2", &t2_len[1], NULL, 0}}};
TAOS_STMT2_BIND params[2][2] = {
{{TSDB_DATA_TYPE_TIMESTAMP, v.ts, NULL, is_null, 2}, {TSDB_DATA_TYPE_BINARY, v.b, b_len, is_null2, 2}},
{{TSDB_DATA_TYPE_TIMESTAMP, v.ts, NULL, is_null, 2}, {TSDB_DATA_TYPE_BINARY, v.b, b_len, is_null2, 2}}};
TAOS_STMT2_BIND* tagv[2] = {&tags[0][0], &tags[1][0]};
TAOS_STMT2_BIND* paramv[2] = {&params[0][0], &params[1][0]};
TAOS_STMT2_BINDV bindv = {2, &tbs[0], &tagv[0], &paramv[0]};
TAOS_STMT2* stmt = taos_stmt2_init(taos, &option);
const char* sql = "insert into db.? using db.stb tags(?, ?) values(?,?)";
int code = taos_stmt2_prepare(stmt, sql, 0);
if (code != 0) {
printf("failed to execute taos_stmt2_prepare. error:%s\n", taos_stmt2_error(stmt));
taos_stmt2_close(stmt);
return;
}
int64_t ts = 1591060628000;
for (int i = 0; i < 2; ++i) {
// v.ts[i] = ts++;
v.ts[i] = ts;
// t64_len[i] = sizeof(int64_t);
}
strcpy(v.b, "abcdefg");
b_len[0] = (int)strlen(v.b);
strcpy(v.b + b_len[0], "xyz");
b_len[1] = 3;
taos_stmt2_bind_param(stmt, &bindv, -1);
if (taos_stmt2_exec(stmt, NULL)) {
printf("failed to execute insert statement.error:%s\n", taos_stmt2_error(stmt));
taos_stmt2_close(stmt);
return;
}
taos_stmt2_close(stmt);
}
int main() {
TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0);
if (!taos) {
printf("failed to connect to db, reason:%s\n", taos_errstr(taos));
exit(1);
}
do_stmt(taos);
taos_close(taos);
taos_cleanup();
}

View File

@ -0,0 +1,369 @@
// sample code to verify all TDengine API
// to compile: gcc -o apitest apitest.c -ltaos
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "taos.h"
static int64_t count = 10000;
int64_t genReqid() {
count += 100;
return count;
}
sem_t sem;
void stmtAsyncQueryCb(void* param, TAOS_RES* pRes, int code) {
int affected_rows = taos_affected_rows(pRes);
printf("\033[31maffected rows:%d\033[0m\n", affected_rows);
(void)sem_post(&sem);
return;
/*
SSP_CB_PARAM* qParam = (SSP_CB_PARAM*)param;
if (code == 0 && pRes) {
if (qParam->fetch) {
taos_fetch_rows_a(pRes, sqAsyncFetchCb, param);
} else {
if (qParam->free) {
taos_free_result(pRes);
}
*qParam->end = 1;
}
} else {
sqError("select", taos_errstr(pRes));
*qParam->end = 1;
taos_free_result(pRes);
}
*/
}
void veriry_stmt(TAOS* taos) {
TAOS_RES* result = taos_query(taos, "drop database if exists test;");
taos_free_result(result);
usleep(100000);
result = taos_query(taos, "create database test;");
int code = taos_errno(result);
if (code != 0) {
printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result));
taos_free_result(result);
return;
}
taos_free_result(result);
usleep(100000);
taos_select_db(taos, "test");
// create table
/*
const char* sql =
"create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin "
"binary(40), blob nchar(10))";
*/
const char* sql =
"create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, blob2 "
"nchar(10), blob nchar(10))";
result = taos_query(taos, sql);
code = taos_errno(result);
if (code != 0) {
printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result));
taos_free_result(result);
return;
}
taos_free_result(result);
sql =
"create table m2 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, blob2 "
"nchar(10), blob nchar(10))";
result = taos_query(taos, sql);
code = taos_errno(result);
if (code != 0) {
printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result));
taos_free_result(result);
return;
}
taos_free_result(result);
// insert 10 records
struct {
int64_t ts[10];
int8_t b[10];
int8_t v1[10];
int16_t v2[10];
int32_t v4[10];
int64_t v8[10];
float f4[10];
double f8[10];
char bin[10][40];
// char blob[10][80];
// char blob2[10][80];
char blob[1024];
char blob2[1024];
} v;
int32_t* t8_len = malloc(sizeof(int32_t) * 10);
int32_t* t16_len = malloc(sizeof(int32_t) * 10);
int32_t* t32_len = malloc(sizeof(int32_t) * 10);
int32_t* t64_len = malloc(sizeof(int32_t) * 10);
int32_t* float_len = malloc(sizeof(int32_t) * 10);
int32_t* double_len = malloc(sizeof(int32_t) * 10);
int32_t* bin_len = malloc(sizeof(int32_t) * 10);
int32_t* blob_len = malloc(sizeof(int32_t) * 10);
int32_t* blob_len2 = malloc(sizeof(int32_t) * 10);
#include "time.h"
clock_t start, end;
TAOS_STMT2_OPTION option = {0, true, false, stmtAsyncQueryCb, NULL};
start = clock();
TAOS_STMT2* stmt = taos_stmt2_init(taos, &option);
end = clock();
printf("init time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
// TAOS_MULTI_BIND params[10];
TAOS_STMT2_BIND params[10];
char is_null2[10] = {0};
char is_null[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 2};
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
// params[0].buffer_length = sizeof(v.ts[0]);
params[0].buffer = v.ts;
params[0].length = NULL; // t64_len;
params[0].is_null = is_null2;
params[0].num = 10;
params[1].buffer_type = TSDB_DATA_TYPE_BOOL;
// params[1].buffer_length = sizeof(v.b[0]);
params[1].buffer = v.b;
params[1].length = NULL; // t8_len;
params[1].is_null = is_null;
params[1].num = 10;
params[2].buffer_type = TSDB_DATA_TYPE_TINYINT;
// params[2].buffer_length = sizeof(v.v1[0]);
params[2].buffer = v.v1;
params[2].length = NULL; // t8_len;
params[2].is_null = is_null;
params[2].num = 10;
params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT;
// params[3].buffer_length = sizeof(v.v2[0]);
params[3].buffer = v.v2;
params[3].length = NULL; // t16_len;
params[3].is_null = is_null;
params[3].num = 10;
params[4].buffer_type = TSDB_DATA_TYPE_INT;
// params[4].buffer_length = sizeof(v.v4[0]);
params[4].buffer = v.v4;
params[4].length = NULL; // t32_len;
params[4].is_null = is_null;
params[4].num = 10;
params[5].buffer_type = TSDB_DATA_TYPE_BIGINT;
// params[5].buffer_length = sizeof(v.v8[0]);
params[5].buffer = v.v8;
params[5].length = NULL; // t64_len;
params[5].is_null = is_null;
params[5].num = 10;
params[6].buffer_type = TSDB_DATA_TYPE_FLOAT;
// params[6].buffer_length = sizeof(v.f4[0]);
params[6].buffer = v.f4;
params[6].length = NULL; // float_len;
params[6].is_null = is_null;
params[6].num = 10;
params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE;
// params[7].buffer_length = sizeof(v.f8[0]);
params[7].buffer = v.f8;
params[7].length = NULL; // double_len;
params[7].is_null = is_null;
params[7].num = 10;
/*
params[8].buffer_type = TSDB_DATA_TYPE_BINARY;
//params[8].buffer_length = sizeof(v.bin[0]);
params[8].buffer = v.bin;
params[8].length = bin_len;
params[8].is_null = is_null;
params[8].num = 10;
*/
params[8].buffer_type = TSDB_DATA_TYPE_NCHAR;
// params[8].buffer_length = sizeof(v.blob2[0]);
params[8].buffer = v.blob2;
params[8].length = blob_len2;
params[8].is_null = is_null;
params[8].num = 10;
params[9].buffer_type = TSDB_DATA_TYPE_NCHAR;
// params[9].buffer_length = sizeof(v.blob[0]);
params[9].buffer = v.blob;
params[9].length = blob_len;
params[9].is_null = is_null;
params[9].num = 10;
sql = "insert into ? (ts, b, v1, v2, v4, v8, f4, f8, blob2, blob) values(?,?,?,?,?,?,?,?,?,?)";
start = clock();
code = taos_stmt2_prepare(stmt, sql, 0);
end = clock();
printf("prepare time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
/*
code = taos_stmt_set_tbname(stmt, "m1");
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
*/
int64_t ts = 1591060628000;
char* blob2_buffer = v.blob2;
char* blob_buffer = v.blob;
const char* blob2_str[] = {
"一二三四五六七十九八", "一二三四五六七十九", "一二三四五六七十", "一二三四五六七", "一二三四五六",
"一二三四五", "一二三四", "一二三", "一二", "",
};
const char* blob_str[] = {
"", "一二", "一二三", "一二三四", "一二三四五",
"一二三四五六", "一二三四五六七", "一二三四五六七八", "一二三四五六七八九", "一二三四五六七八九十",
};
for (int i = 0; i < 10; ++i) {
// is_null[i] = 0;
v.ts[i] = ts++;
// v.b[i] = (int8_t)i % 2;
v.b[i] = (int8_t)i % 10;
v.v1[i] = (int8_t)i;
v.v2[i] = (int16_t)(i * 2);
v.v4[i] = (int32_t)(i * 4);
v.v8[i] = (int64_t)(i * 8);
v.f4[i] = (float)(i * 40);
v.f8[i] = (double)(i * 80);
for (int j = 0; j < sizeof(v.bin[0]); ++j) {
v.bin[i][j] = (char)(i + '0');
}
// strcpy(v.blob2[i], "一二三四五六七十九八");
// strcpy(v.blob[i], "一二三四五六七八九十");
// const char* blob2_str = "一二三四五六七十九八";
// const char* blob_str = "一二三四五六七八九十";
strcpy(blob2_buffer, blob2_str[i]);
strcpy(blob_buffer, blob_str[i]);
t8_len[i] = sizeof(int8_t);
t16_len[i] = sizeof(int16_t);
t32_len[i] = sizeof(int32_t);
t64_len[i] = sizeof(int64_t);
float_len[i] = sizeof(float);
double_len[i] = sizeof(double);
bin_len[i] = sizeof(v.bin[0]);
// blob_len[i] = (int32_t)strlen(v.blob[i]);
// blob_len2[i] = (int32_t)strlen(v.blob2[i]);
blob_len[i] = (int32_t)strlen(blob_str[i]);
blob_len2[i] = (int32_t)strlen(blob2_str[i]);
blob_buffer += blob_len[i];
blob2_buffer += blob_len2[i];
}
int run_time = 0;
char* tbname = "m2";
TAOS_STMT2_BIND* bind_cols[1] = {&params[0]};
TAOS_STMT2_BINDV bindv = {1, &tbname, NULL, &bind_cols[0]};
_bind_again:
start = clock();
// taos_stmt2_bind_param(stmt, "m1", NULL, params, -1);
taos_stmt2_bind_param(stmt, &bindv, -1);
end = clock();
printf("bind time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
// taos_stmt_bind_param_batch(stmt, params);
// taos_stmt_add_batch(stmt);
/*
int param_count = -1;
code = taos_stmt2_param_count(stmt, &param_count);
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_param_count. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
printf("param_count: %d\n", param_count);
*/
TAOS_FIELD_E* fields = NULL;
int field_count = -1;
start = clock();
code = taos_stmt2_get_fields(stmt, TAOS_FIELD_TBNAME, &field_count, NULL);
end = clock();
printf("get fields time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_param_count. error:%s\033[0m\n", taos_stmt2_error(stmt));
taos_stmt2_close(stmt);
return;
}
printf("col field_count: %d\n", field_count);
start = clock();
taos_stmt2_free_fields(stmt, fields);
end = clock();
printf("free time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
/*
code = taos_stmt2_get_fields(stmt, TAOS_FIELD_TAG, &field_count, &fields);
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_param_count. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
printf("tag field_count: %d\n", field_count);
taos_stmt2_free_fields(stmt, fields);
*/
// if (taos_stmt_execute(stmt) != 0) {
(void)sem_init(&sem, 0, 0);
start = clock();
// if (taos_stmt2_exec(stmt, NULL, stmtAsyncQueryCb, NULL) != 0) {
if (taos_stmt2_exec(stmt, NULL) != 0) {
printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt2_close(stmt);
return;
}
end = clock();
printf("exec time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
sem_wait(&sem);
(void)sem_destroy(&sem);
if (++run_time < 2) {
goto _bind_again;
}
taos_stmt2_close(stmt);
free(t8_len);
free(t16_len);
free(t32_len);
free(t64_len);
free(float_len);
free(double_len);
free(bin_len);
free(blob_len);
free(blob_len2);
}
int main(int argc, char* argv[]) {
const char* host = "127.0.0.1";
const char* user = "root";
const char* passwd = "taosdata";
taos_options(TSDB_OPTION_TIMEZONE, "GMT-8");
TAOS* taos = taos_connect(host, user, passwd, "", 0);
if (taos == NULL) {
printf("\033[31mfailed to connect to db, reason:%s\033[0m\n", taos_errstr(taos));
exit(1);
}
printf("********* verify stmt query **********\n");
veriry_stmt(taos);
printf("done\n");
taos_close(taos);
taos_cleanup();
}

File diff suppressed because it is too large Load Diff

323
tests/script/api/stmt2.c Normal file
View File

@ -0,0 +1,323 @@
// sample code to verify all TDengine API
// to compile: gcc -o apitest apitest.c -ltaos
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "taos.h"
static int64_t count = 10000;
int64_t genReqid() {
count += 100;
return count;
}
void stmtAsyncQueryCb(void* param, TAOS_RES* pRes, int code) {
int affected_rows = taos_affected_rows(pRes);
return;
/*
SSP_CB_PARAM* qParam = (SSP_CB_PARAM*)param;
if (code == 0 && pRes) {
if (qParam->fetch) {
taos_fetch_rows_a(pRes, sqAsyncFetchCb, param);
} else {
if (qParam->free) {
taos_free_result(pRes);
}
*qParam->end = 1;
}
} else {
sqError("select", taos_errstr(pRes));
*qParam->end = 1;
taos_free_result(pRes);
}
*/
}
void veriry_stmt(TAOS* taos) {
TAOS_RES* result = taos_query(taos, "drop database if exists test;");
taos_free_result(result);
usleep(100000);
result = taos_query(taos, "create database test;");
int code = taos_errno(result);
if (code != 0) {
printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result));
taos_free_result(result);
return;
}
taos_free_result(result);
usleep(100000);
taos_select_db(taos, "test");
// create table
/*
const char* sql =
"create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin "
"binary(40), blob nchar(10))";
*/
const char* sql =
"create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, blob2 "
"nchar(10), blob nchar(10))";
result = taos_query(taos, sql);
code = taos_errno(result);
if (code != 0) {
printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result));
taos_free_result(result);
return;
}
taos_free_result(result);
// insert 10 records
struct {
int64_t ts[10];
int8_t b[10];
int8_t v1[10];
int16_t v2[10];
int32_t v4[10];
int64_t v8[10];
float f4[10];
double f8[10];
char bin[10][40];
char blob[10][80];
char blob2[10][80];
} v;
int32_t* t8_len = malloc(sizeof(int32_t) * 10);
int32_t* t16_len = malloc(sizeof(int32_t) * 10);
int32_t* t32_len = malloc(sizeof(int32_t) * 10);
int32_t* t64_len = malloc(sizeof(int32_t) * 10);
int32_t* float_len = malloc(sizeof(int32_t) * 10);
int32_t* double_len = malloc(sizeof(int32_t) * 10);
int32_t* bin_len = malloc(sizeof(int32_t) * 10);
int32_t* blob_len = malloc(sizeof(int32_t) * 10);
int32_t* blob_len2 = malloc(sizeof(int32_t) * 10);
#include "time.h"
clock_t start, end;
TAOS_STMT2_OPTION option = {0, true, true, stmtAsyncQueryCb, NULL};
start = clock();
TAOS_STMT2* stmt = taos_stmt2_init(taos, &option);
end = clock();
printf("init time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
// TAOS_MULTI_BIND params[10];
TAOS_STMT2_BIND params[10];
char is_null[10] = {0};
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
// params[0].buffer_length = sizeof(v.ts[0]);
params[0].buffer = v.ts;
params[0].length = t64_len;
params[0].is_null = is_null;
params[0].num = 10;
params[1].buffer_type = TSDB_DATA_TYPE_BOOL;
// params[1].buffer_length = sizeof(v.b[0]);
params[1].buffer = v.b;
params[1].length = t8_len;
params[1].is_null = is_null;
params[1].num = 10;
params[2].buffer_type = TSDB_DATA_TYPE_TINYINT;
// params[2].buffer_length = sizeof(v.v1[0]);
params[2].buffer = v.v1;
params[2].length = t8_len;
params[2].is_null = is_null;
params[2].num = 10;
params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT;
// params[3].buffer_length = sizeof(v.v2[0]);
params[3].buffer = v.v2;
params[3].length = t16_len;
params[3].is_null = is_null;
params[3].num = 10;
params[4].buffer_type = TSDB_DATA_TYPE_INT;
// params[4].buffer_length = sizeof(v.v4[0]);
params[4].buffer = v.v4;
params[4].length = t32_len;
params[4].is_null = is_null;
params[4].num = 10;
params[5].buffer_type = TSDB_DATA_TYPE_BIGINT;
// params[5].buffer_length = sizeof(v.v8[0]);
params[5].buffer = v.v8;
params[5].length = t64_len;
params[5].is_null = is_null;
params[5].num = 10;
params[6].buffer_type = TSDB_DATA_TYPE_FLOAT;
// params[6].buffer_length = sizeof(v.f4[0]);
params[6].buffer = v.f4;
params[6].length = float_len;
params[6].is_null = is_null;
params[6].num = 10;
params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE;
// params[7].buffer_length = sizeof(v.f8[0]);
params[7].buffer = v.f8;
params[7].length = double_len;
params[7].is_null = is_null;
params[7].num = 10;
/*
params[8].buffer_type = TSDB_DATA_TYPE_BINARY;
//params[8].buffer_length = sizeof(v.bin[0]);
params[8].buffer = v.bin;
params[8].length = bin_len;
params[8].is_null = is_null;
params[8].num = 10;
*/
params[8].buffer_type = TSDB_DATA_TYPE_NCHAR;
// params[8].buffer_length = sizeof(v.blob2[0]);
params[8].buffer = v.blob2;
params[8].length = blob_len2;
params[8].is_null = is_null;
params[8].num = 10;
params[9].buffer_type = TSDB_DATA_TYPE_NCHAR;
// params[9].buffer_length = sizeof(v.blob[0]);
params[9].buffer = v.blob;
params[9].length = blob_len;
params[9].is_null = is_null;
params[9].num = 10;
sql = "insert into ? (ts, b, v1, v2, v4, v8, f4, f8, blob2, blob) values(?,?,?,?,?,?,?,?,?,?)";
start = clock();
code = taos_stmt2_prepare(stmt, sql, 0);
end = clock();
printf("prepare time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
/*
code = taos_stmt_set_tbname(stmt, "m1");
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
*/
int64_t ts = 1591060628000;
for (int i = 0; i < 10; ++i) {
is_null[i] = 0;
v.ts[i] = ts++;
v.b[i] = (int8_t)i % 2;
v.v1[i] = (int8_t)i;
v.v2[i] = (int16_t)(i * 2);
v.v4[i] = (int32_t)(i * 4);
v.v8[i] = (int64_t)(i * 8);
v.f4[i] = (float)(i * 40);
v.f8[i] = (double)(i * 80);
for (int j = 0; j < sizeof(v.bin[0]); ++j) {
v.bin[i][j] = (char)(i + '0');
}
strcpy(v.blob2[i], "一二三四五六七十九八");
strcpy(v.blob[i], "一二三四五六七八九十");
t8_len[i] = sizeof(int8_t);
t16_len[i] = sizeof(int16_t);
t32_len[i] = sizeof(int32_t);
t64_len[i] = sizeof(int64_t);
float_len[i] = sizeof(float);
double_len[i] = sizeof(double);
bin_len[i] = sizeof(v.bin[0]);
blob_len[i] = (int32_t)strlen(v.blob[i]);
blob_len2[i] = (int32_t)strlen(v.blob2[i]);
}
char* tbname = "m1";
TAOS_STMT2_BIND* bind_cols[1] = {&params[0]};
TAOS_STMT2_BINDV bindv = {1, &tbname, NULL, &bind_cols[0]};
start = clock();
// taos_stmt2_bind_param(stmt, "m1", NULL, params, -1);
taos_stmt2_bind_param(stmt, &bindv, -1);
end = clock();
printf("bind time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
// taos_stmt_bind_param_batch(stmt, params);
// taos_stmt_add_batch(stmt);
/*
int param_count = -1;
code = taos_stmt2_param_count(stmt, &param_count);
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_param_count. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
printf("param_count: %d\n", param_count);
*/
TAOS_FIELD_E* fields = NULL;
int field_count = -1;
start = clock();
code = taos_stmt2_get_fields(stmt, TAOS_FIELD_COL, &field_count, NULL);
end = clock();
printf("get fields time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_param_count. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
printf("col field_count: %d\n", field_count);
start = clock();
taos_stmt2_free_fields(stmt, fields);
end = clock();
printf("free time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
/*
code = taos_stmt2_get_fields(stmt, TAOS_FIELD_TAG, &field_count, &fields);
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_param_count. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
printf("tag field_count: %d\n", field_count);
taos_stmt2_free_fields(stmt, fields);
*/
// if (taos_stmt_execute(stmt) != 0) {
start = clock();
// if (taos_stmt2_exec(stmt, NULL, stmtAsyncQueryCb, NULL) != 0) {
if (taos_stmt2_exec(stmt, NULL) != 0) {
printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt2_close(stmt);
return;
}
end = clock();
printf("exec time:%f\n", (double)(end - start) / CLOCKS_PER_SEC);
taos_stmt2_close(stmt);
free(t8_len);
free(t16_len);
free(t32_len);
free(t64_len);
free(float_len);
free(double_len);
free(bin_len);
free(blob_len);
free(blob_len2);
}
int main(int argc, char* argv[]) {
const char* host = "127.0.0.1";
const char* user = "root";
const char* passwd = "taosdata";
taos_options(TSDB_OPTION_TIMEZONE, "GMT-8");
TAOS* taos = taos_connect(host, user, passwd, "", 0);
if (taos == NULL) {
printf("\033[31mfailed to connect to db, reason:%s\033[0m\n", taos_errstr(taos));
exit(1);
}
printf("********* verify stmt query **********\n");
veriry_stmt(taos);
printf("done\n");
taos_close(taos);
taos_cleanup();
}

View File

@ -48,15 +48,15 @@ static char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision);
static int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres); static int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres);
static void shellPrintNChar(const char *str, int32_t length, int32_t width); static void shellPrintNChar(const char *str, int32_t length, int32_t width);
static void shellPrintGeometry(const unsigned char *str, int32_t length, int32_t width); static void shellPrintGeometry(const unsigned char *str, int32_t length, int32_t width);
static void shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo* dump_info); static void shellVerticalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info);
static void shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo* dump_info); static void shellHorizontalPrintResult(TAOS_RES *tres, tsDumpInfo *dump_info);
static int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql); static int64_t shellDumpResult(TAOS_RES *tres, char *fname, int32_t *error_no, bool vertical, const char *sql);
static void shellReadHistory(); static void shellReadHistory();
static void shellWriteHistory(); static void shellWriteHistory();
static void shellPrintError(TAOS_RES *tres, int64_t st); static void shellPrintError(TAOS_RES *tres, int64_t st);
static bool shellIsCommentLine(char *line); static bool shellIsCommentLine(char *line);
static void shellSourceFile(const char *file); static void shellSourceFile(const char *file);
static bool shellGetGrantInfo(char* buf); static bool shellGetGrantInfo(char *buf);
static void shellCleanup(void *arg); static void shellCleanup(void *arg);
static void *shellCancelHandler(void *arg); static void *shellCancelHandler(void *arg);
@ -165,12 +165,12 @@ int32_t shellRunCommand(char *command, bool recordHistory) {
// add help or help; // add help or help;
if (strncasecmp(command, "help", 4) == 0) { if (strncasecmp(command, "help", 4) == 0) {
if(command[4] == ';' || command[4] == ' ' || command[4] == 0) { if (command[4] == ';' || command[4] == ' ' || command[4] == 0) {
showHelp(); showHelp();
return 0; return 0;
} }
} }
if (recordHistory) shellRecordCommandToHistory(command); if (recordHistory) shellRecordCommandToHistory(command);
char quote = 0, *cmd = command; char quote = 0, *cmd = command;
@ -197,19 +197,18 @@ int32_t shellRunCommand(char *command, bool recordHistory) {
return shellRunSingleCommand(cmd); return shellRunSingleCommand(cmd);
} }
char *strendG(const char *pstr) {
char * strendG(const char* pstr) { if (pstr == NULL) {
if(pstr == NULL) {
return NULL; return NULL;
} }
size_t len = strlen(pstr); size_t len = strlen(pstr);
if(len < 4) { if (len < 4) {
return NULL; return NULL;
} }
char * p = (char *)pstr + len - 2; char *p = (char *)pstr + len - 2;
if (strcmp(p, "\\G") == 0 ){ if (strcmp(p, "\\G") == 0) {
return p; return p;
} }
@ -285,10 +284,11 @@ void shellRunSingleCommandImp(char *command) {
et = taosGetTimestampUs(); et = taosGetTimestampUs();
if (error_no == 0) { if (error_no == 0) {
printf("Query OK, %"PRId64 " row(s) in set (%.6fs)\r\n", numOfRows, (et - st) / 1E6); printf("Query OK, %" PRId64 " row(s) in set (%.6fs)\r\n", numOfRows, (et - st) / 1E6);
} else { } else {
terrno = error_no; terrno = error_no;
printf("Query interrupted (%s), %"PRId64 " row(s) in set (%.6fs)\r\n", taos_errstr(NULL), numOfRows, (et - st) / 1E6); printf("Query interrupted (%s), %" PRId64 " row(s) in set (%.6fs)\r\n", taos_errstr(NULL), numOfRows,
(et - st) / 1E6);
} }
taos_free_result(pSql); taos_free_result(pSql);
} else { } else {
@ -366,10 +366,10 @@ void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, i
return; return;
} }
char quotationStr[2] ={'"', 0}; char quotationStr[2] = {'"', 0};
int32_t width; int32_t width;
int n = 0; int n = 0;
#define LENGTH 64 #define LENGTH 64
char buf[LENGTH] = {0}; char buf[LENGTH] = {0};
switch (field->type) { switch (field->type) {
@ -431,8 +431,8 @@ void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, i
case TSDB_DATA_TYPE_NCHAR: case TSDB_DATA_TYPE_NCHAR:
case TSDB_DATA_TYPE_JSON: { case TSDB_DATA_TYPE_JSON: {
int32_t bufIndex = 0; int32_t bufIndex = 0;
char* tmp = (char*)taosMemoryCalloc(length * 2 + 1, 1); char *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
if(tmp == NULL) break; if (tmp == NULL) break;
for (int32_t i = 0; i < length; i++) { for (int32_t i = 0; i < length; i++) {
tmp[bufIndex] = val[i]; tmp[bufIndex] = val[i];
bufIndex++; bufIndex++;
@ -446,19 +446,19 @@ void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, i
taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr); taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
taosMemoryFree(tmp); taosMemoryFree(tmp);
} break; } break;
case TSDB_DATA_TYPE_VARBINARY:{ case TSDB_DATA_TYPE_VARBINARY: {
void* tmp = NULL; void *tmp = NULL;
uint32_t size = 0; uint32_t size = 0;
if(taosAscii2Hex(val, length, &tmp, &size) < 0){ if (taosAscii2Hex(val, length, &tmp, &size) < 0) {
break; break;
} }
taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr); taosFprintfFile(pFile, "%s%s%s", quotationStr, tmp, quotationStr);
taosMemoryFree(tmp); taosMemoryFree(tmp);
break; break;
} }
case TSDB_DATA_TYPE_GEOMETRY:{ case TSDB_DATA_TYPE_GEOMETRY: {
char* tmp = (char*)taosMemoryCalloc(length * 2 + 1, 1); char *tmp = (char *)taosMemoryCalloc(length * 2 + 1, 1);
if(tmp == NULL) break; if (tmp == NULL) break;
shellDumpHexValue(tmp, val, length); shellDumpHexValue(tmp, val, length);
taosFprintfFile(pFile, "%s", buf); taosFprintfFile(pFile, "%s", buf);
taosMemoryFree(tmp); taosMemoryFree(tmp);
@ -635,12 +635,12 @@ void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t
return; return;
} }
int n = 0; int n = 0;
#define LENGTH 64 #define LENGTH 64
char buf[LENGTH] = {0}; char buf[LENGTH] = {0};
switch (field->type) { switch (field->type) {
case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_BOOL:
shellPrintString(((((int32_t)(*((char *)val))) == 1) ? "true" : "false"), width); shellPrintString(((((int32_t)(*((char *)val))) == TSDB_FALSE) ? "false" : "true"), width);
break; break;
case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_TINYINT:
printf("%*d", width, *((int8_t *)val)); printf("%*d", width, *((int8_t *)val));
@ -680,7 +680,7 @@ void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t
break; break;
case TSDB_DATA_TYPE_DOUBLE: case TSDB_DATA_TYPE_DOUBLE:
if (tsEnableScience) { if (tsEnableScience) {
snprintf(buf, LENGTH, "%*.15e", width,GET_DOUBLE_VAL(val)); snprintf(buf, LENGTH, "%*.15e", width, GET_DOUBLE_VAL(val));
printf("%s", buf); printf("%s", buf);
} else { } else {
n = snprintf(buf, LENGTH, "%*.15f", width, GET_DOUBLE_VAL(val)); n = snprintf(buf, LENGTH, "%*.15f", width, GET_DOUBLE_VAL(val));
@ -691,10 +691,10 @@ void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t
} }
} }
break; break;
case TSDB_DATA_TYPE_VARBINARY:{ case TSDB_DATA_TYPE_VARBINARY: {
void* data = NULL; void *data = NULL;
uint32_t size = 0; uint32_t size = 0;
if(taosAscii2Hex(val, length, &data, &size) < 0){ if (taosAscii2Hex(val, length, &data, &size) < 0) {
break; break;
} }
shellPrintNChar(data, size, width); shellPrintNChar(data, size, width);
@ -867,7 +867,7 @@ int32_t shellCalcColWidth(TAOS_FIELD *field, int32_t precision) {
} else { } else {
return TMAX(field->bytes + 2, width); return TMAX(field->bytes + 2, width);
} }
case TSDB_DATA_TYPE_VARBINARY:{ case TSDB_DATA_TYPE_VARBINARY: {
int32_t bytes = field->bytes * 2 + 2; int32_t bytes = field->bytes * 2 + 2;
if (bytes > shell.args.displayWidth) { if (bytes > shell.args.displayWidth) {
return TMAX(shell.args.displayWidth, width); return TMAX(shell.args.displayWidth, width);
@ -1121,8 +1121,9 @@ void shellSourceFile(const char *file) {
char *line = taosMemoryMalloc(TSDB_MAX_ALLOWED_SQL_LEN + 1); char *line = taosMemoryMalloc(TSDB_MAX_ALLOWED_SQL_LEN + 1);
while ((read_len = taosGetsFile(pFile, TSDB_MAX_ALLOWED_SQL_LEN, line)) > 0) { while ((read_len = taosGetsFile(pFile, TSDB_MAX_ALLOWED_SQL_LEN, line)) > 0) {
if ( cmd_len + read_len >= TSDB_MAX_ALLOWED_SQL_LEN) { if (cmd_len + read_len >= TSDB_MAX_ALLOWED_SQL_LEN) {
printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len, read_len); printf("read command line too long over 1M, ignore this line. cmd_len = %d read_len=%d \n", (int32_t)cmd_len,
read_len);
cmd_len = 0; cmd_len = 0;
memset(line, 0, TSDB_MAX_ALLOWED_SQL_LEN + 1); memset(line, 0, TSDB_MAX_ALLOWED_SQL_LEN + 1);
continue; continue;
@ -1156,7 +1157,7 @@ void shellSourceFile(const char *file) {
taosCloseFile(&pFile); taosCloseFile(&pFile);
} }
bool shellGetGrantInfo(char* buf) { bool shellGetGrantInfo(char *buf) {
bool community = true; bool community = true;
char sinfo[256] = {0}; char sinfo[256] = {0};
tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo)); tstrncpy(sinfo, taos_get_server_info(shell.conn), sizeof(sinfo));
@ -1207,8 +1208,7 @@ bool shellGetGrantInfo(char* buf) {
sprintf(buf, "Server is %s, %s and will never expire.\r\n", serverVersion, sinfo); sprintf(buf, "Server is %s, %s and will never expire.\r\n", serverVersion, sinfo);
} else { } else {
community = false; community = false;
sprintf(buf, "Server is %s, %s and will expire at %s.\r\n", serverVersion, sinfo, sprintf(buf, "Server is %s, %s and will expire at %s.\r\n", serverVersion, sinfo, expiretime);
expiretime);
} }
taos_free_result(tres); taos_free_result(tres);
@ -1324,13 +1324,13 @@ int32_t shellExecute() {
shellSetConn(shell.conn, runOnce); shellSetConn(shell.conn, runOnce);
shellReadHistory(); shellReadHistory();
if(shell.args.is_bi_mode) { if (shell.args.is_bi_mode) {
// need set bi mode // need set bi mode
printf("Set BI mode is true.\n"); printf("Set BI mode is true.\n");
#ifndef WEBSOCKET #ifndef WEBSOCKET
taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1); taos_set_conn_mode(shell.conn, TAOS_CONN_MODE_BI, 1);
#endif #endif
} }
if (runOnce) { if (runOnce) {
if (pArgs->commands != NULL) { if (pArgs->commands != NULL) {
@ -1373,22 +1373,22 @@ int32_t shellExecute() {
#ifdef WEBSOCKET #ifdef WEBSOCKET
if (!shell.args.restful && !shell.args.cloud) { if (!shell.args.restful && !shell.args.cloud) {
#endif #endif
char* buf = taosMemoryMalloc(512); char *buf = taosMemoryMalloc(512);
bool community = shellGetGrantInfo(buf); bool community = shellGetGrantInfo(buf);
#ifndef WINDOWS #ifndef WINDOWS
printfIntroduction(community); printfIntroduction(community);
#else #else
#ifndef WEBSOCKET #ifndef WEBSOCKET
if(community) { if (community) {
showAD(false); showAD(false);
} }
#endif
#endif #endif
// printf version #endif
if(!community) { // printf version
printf("%s\n", buf); if (!community) {
} printf("%s\n", buf);
taosMemoryFree(buf); }
taosMemoryFree(buf);
#ifdef WEBSOCKET #ifdef WEBSOCKET
} }
@ -1407,7 +1407,7 @@ taosMemoryFree(buf);
if (community) { if (community) {
showAD(true); showAD(true);
} }
#endif #endif
taosThreadJoin(spid, NULL); taosThreadJoin(spid, NULL);