提交 414bce81 编写于 作者: Z zhaoyanggh

Merge branch 'master' of https://github.com/taosdata/TDengine into fix/TS-165

...@@ -265,12 +265,12 @@ pipeline { ...@@ -265,12 +265,12 @@ pipeline {
} }
} }
timeout(time: 60, unit: 'MINUTES'){ timeout(time: 60, unit: 'MINUTES'){
sh ''' // sh '''
cd ${WKC}/tests/pytest // cd ${WKC}/tests/pytest
rm -rf /var/lib/taos/* // rm -rf /var/lib/taos/*
rm -rf /var/log/taos/* // rm -rf /var/log/taos/*
./handle_crash_gen_val_log.sh // ./handle_crash_gen_val_log.sh
''' // '''
sh ''' sh '''
cd ${WKC}/tests/pytest cd ${WKC}/tests/pytest
rm -rf /var/lib/taos/* rm -rf /var/lib/taos/*
......
...@@ -1779,6 +1779,7 @@ static void parseFileSendDataBlock(void *param, TAOS_RES *tres, int32_t numOfRow ...@@ -1779,6 +1779,7 @@ static void parseFileSendDataBlock(void *param, TAOS_RES *tres, int32_t numOfRow
} }
_error: _error:
pParentSql->res.code = code;
tfree(tokenBuf); tfree(tokenBuf);
tfree(line); tfree(line);
taos_free_result(pSql); taos_free_result(pSql);
......
...@@ -86,6 +86,10 @@ typedef struct STscStmt { ...@@ -86,6 +86,10 @@ typedef struct STscStmt {
return _code; \ return _code; \
} while (0) } while (0)
#define STMT_CHECK if (pStmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { \
STMT_RET(TSDB_CODE_TSC_DISCONNECTED); \
}
static int32_t invalidOperationMsg(char* dstBuffer, const char* errMsg) { static int32_t invalidOperationMsg(char* dstBuffer, const char* errMsg) {
return tscInvalidOperationMsg(dstBuffer, errMsg, NULL); return tscInvalidOperationMsg(dstBuffer, errMsg, NULL);
} }
...@@ -155,6 +159,22 @@ static int normalStmtBindParam(STscStmt* stmt, TAOS_BIND* bind) { ...@@ -155,6 +159,22 @@ static int normalStmtBindParam(STscStmt* stmt, TAOS_BIND* bind) {
var->i64 = *(int64_t*)tb->buffer; var->i64 = *(int64_t*)tb->buffer;
break; break;
case TSDB_DATA_TYPE_UTINYINT:
var->u64 = *(uint8_t*)tb->buffer;
break;
case TSDB_DATA_TYPE_USMALLINT:
var->u64 = *(uint16_t*)tb->buffer;
break;
case TSDB_DATA_TYPE_UINT:
var->u64 = *(uint32_t*)tb->buffer;
break;
case TSDB_DATA_TYPE_UBIGINT:
var->u64 = *(uint64_t*)tb->buffer;
break;
case TSDB_DATA_TYPE_FLOAT: case TSDB_DATA_TYPE_FLOAT:
var->dKey = GET_FLOAT_VAL(tb->buffer); var->dKey = GET_FLOAT_VAL(tb->buffer);
break; break;
...@@ -261,9 +281,17 @@ static char* normalStmtBuildSql(STscStmt* stmt) { ...@@ -261,9 +281,17 @@ static char* normalStmtBuildSql(STscStmt* stmt) {
case TSDB_DATA_TYPE_SMALLINT: case TSDB_DATA_TYPE_SMALLINT:
case TSDB_DATA_TYPE_INT: case TSDB_DATA_TYPE_INT:
case TSDB_DATA_TYPE_BIGINT: case TSDB_DATA_TYPE_BIGINT:
case TSDB_DATA_TYPE_TIMESTAMP:
taosStringBuilderAppendInteger(&sb, var->i64); taosStringBuilderAppendInteger(&sb, var->i64);
break; break;
case TSDB_DATA_TYPE_UTINYINT:
case TSDB_DATA_TYPE_USMALLINT:
case TSDB_DATA_TYPE_UINT:
case TSDB_DATA_TYPE_UBIGINT:
taosStringBuilderAppendUnsignedInteger(&sb, var->u64);
break;
case TSDB_DATA_TYPE_FLOAT: case TSDB_DATA_TYPE_FLOAT:
case TSDB_DATA_TYPE_DOUBLE: case TSDB_DATA_TYPE_DOUBLE:
taosStringBuilderAppendDouble(&sb, var->dKey); taosStringBuilderAppendDouble(&sb, var->dKey);
...@@ -1501,9 +1529,7 @@ TAOS_STMT* taos_stmt_init(TAOS* taos) { ...@@ -1501,9 +1529,7 @@ TAOS_STMT* taos_stmt_init(TAOS* taos) {
int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { STMT_CHECK
STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
}
if (sql == NULL) { if (sql == NULL) {
tscError("sql is NULL"); tscError("sql is NULL");
...@@ -1580,9 +1606,7 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags ...@@ -1580,9 +1606,7 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
int32_t code = 0; int32_t code = 0;
if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { STMT_CHECK
STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
}
SSqlObj* pSql = pStmt->pSql; SSqlObj* pSql = pStmt->pSql;
SSqlCmd* pCmd = &pSql->cmd; SSqlCmd* pCmd = &pSql->cmd;
...@@ -1742,6 +1766,7 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags ...@@ -1742,6 +1766,7 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags
int taos_stmt_set_sub_tbname(TAOS_STMT* stmt, const char* name) { int taos_stmt_set_sub_tbname(TAOS_STMT* stmt, const char* name) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
STMT_CHECK
pStmt->mtb.subSet = true; pStmt->mtb.subSet = true;
return taos_stmt_set_tbname_tags(stmt, name, NULL); return taos_stmt_set_tbname_tags(stmt, name, NULL);
} }
...@@ -1750,6 +1775,7 @@ int taos_stmt_set_sub_tbname(TAOS_STMT* stmt, const char* name) { ...@@ -1750,6 +1775,7 @@ int taos_stmt_set_sub_tbname(TAOS_STMT* stmt, const char* name) {
int taos_stmt_set_tbname(TAOS_STMT* stmt, const char* name) { int taos_stmt_set_tbname(TAOS_STMT* stmt, const char* name) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
STMT_CHECK
pStmt->mtb.subSet = false; pStmt->mtb.subSet = false;
return taos_stmt_set_tbname_tags(stmt, name, NULL); return taos_stmt_set_tbname_tags(stmt, name, NULL);
} }
...@@ -1757,6 +1783,7 @@ int taos_stmt_set_tbname(TAOS_STMT* stmt, const char* name) { ...@@ -1757,6 +1783,7 @@ int taos_stmt_set_tbname(TAOS_STMT* stmt, const char* name) {
int taos_stmt_close(TAOS_STMT* stmt) { int taos_stmt_close(TAOS_STMT* stmt) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
STMT_CHECK
if (!pStmt->isInsert) { if (!pStmt->isInsert) {
SNormalStmt* normal = &pStmt->normal; SNormalStmt* normal = &pStmt->normal;
if (normal->params != NULL) { if (normal->params != NULL) {
...@@ -1785,16 +1812,14 @@ int taos_stmt_close(TAOS_STMT* stmt) { ...@@ -1785,16 +1812,14 @@ int taos_stmt_close(TAOS_STMT* stmt) {
} }
} }
taos_free_result(pStmt->pSql); tscFreeSqlObj(pStmt->pSql);
tfree(pStmt); tfree(pStmt);
STMT_RET(TSDB_CODE_SUCCESS); STMT_RET(TSDB_CODE_SUCCESS);
} }
int taos_stmt_bind_param(TAOS_STMT* stmt, TAOS_BIND* bind) { int taos_stmt_bind_param(TAOS_STMT* stmt, TAOS_BIND* bind) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { STMT_CHECK
STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
}
if (pStmt->isInsert) { if (pStmt->isInsert) {
if (pStmt->multiTbInsert) { if (pStmt->multiTbInsert) {
...@@ -1823,9 +1848,7 @@ int taos_stmt_bind_param(TAOS_STMT* stmt, TAOS_BIND* bind) { ...@@ -1823,9 +1848,7 @@ int taos_stmt_bind_param(TAOS_STMT* stmt, TAOS_BIND* bind) {
int taos_stmt_bind_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind) { int taos_stmt_bind_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { STMT_CHECK
STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
}
if (bind == NULL || bind->num <= 0 || bind->num > INT16_MAX) { if (bind == NULL || bind->num <= 0 || bind->num > INT16_MAX) {
tscError("0x%"PRIx64" invalid parameter", pStmt->pSql->self); tscError("0x%"PRIx64" invalid parameter", pStmt->pSql->self);
...@@ -1856,9 +1879,7 @@ int taos_stmt_bind_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind) { ...@@ -1856,9 +1879,7 @@ int taos_stmt_bind_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind) {
int taos_stmt_bind_single_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int colIdx) { int taos_stmt_bind_single_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int colIdx) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { STMT_CHECK
STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
}
if (bind == NULL || bind->num <= 0 || bind->num > INT16_MAX || colIdx < 0) { if (bind == NULL || bind->num <= 0 || bind->num > INT16_MAX || colIdx < 0) {
tscError("0x%"PRIx64" invalid parameter", pStmt->pSql->self); tscError("0x%"PRIx64" invalid parameter", pStmt->pSql->self);
...@@ -1891,9 +1912,7 @@ int taos_stmt_bind_single_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, in ...@@ -1891,9 +1912,7 @@ int taos_stmt_bind_single_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, in
int taos_stmt_add_batch(TAOS_STMT* stmt) { int taos_stmt_add_batch(TAOS_STMT* stmt) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { STMT_CHECK
STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
}
if (pStmt->isInsert) { if (pStmt->isInsert) {
if (pStmt->last != STMT_BIND && pStmt->last != STMT_BIND_COL) { if (pStmt->last != STMT_BIND && pStmt->last != STMT_BIND_COL) {
...@@ -1920,9 +1939,7 @@ int taos_stmt_reset(TAOS_STMT* stmt) { ...@@ -1920,9 +1939,7 @@ int taos_stmt_reset(TAOS_STMT* stmt) {
int taos_stmt_execute(TAOS_STMT* stmt) { int taos_stmt_execute(TAOS_STMT* stmt) {
int ret = 0; int ret = 0;
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { STMT_CHECK
STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
}
if (pStmt->isInsert) { if (pStmt->isInsert) {
if (pStmt->last != STMT_ADD_BATCH) { if (pStmt->last != STMT_ADD_BATCH) {
...@@ -1968,18 +1985,14 @@ TAOS_RES *taos_stmt_use_result(TAOS_STMT* stmt) { ...@@ -1968,18 +1985,14 @@ TAOS_RES *taos_stmt_use_result(TAOS_STMT* stmt) {
tscError("result has been used already."); tscError("result has been used already.");
return NULL; return NULL;
} }
TAOS_RES* result = pStmt->pSql; TAOS_RES* result = pStmt->pSql;
pStmt->pSql = NULL;
return result; return result;
} }
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) { int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { STMT_CHECK
STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
}
if (insert) *insert = pStmt->isInsert; if (insert) *insert = pStmt->isInsert;
...@@ -1989,9 +2002,7 @@ int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) { ...@@ -1989,9 +2002,7 @@ int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) { int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { STMT_CHECK
STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
}
if (pStmt->isInsert) { if (pStmt->isInsert) {
SSqlObj* pSql = pStmt->pSql; SSqlObj* pSql = pStmt->pSql;
...@@ -2008,9 +2019,7 @@ int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) { ...@@ -2008,9 +2019,7 @@ int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) { int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
STscStmt* pStmt = (STscStmt*)stmt; STscStmt* pStmt = (STscStmt*)stmt;
if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { STMT_CHECK
STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
}
if (pStmt->isInsert) { if (pStmt->isInsert) {
SSqlCmd* pCmd = &pStmt->pSql->cmd; SSqlCmd* pCmd = &pStmt->pSql->cmd;
......
...@@ -2609,6 +2609,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col ...@@ -2609,6 +2609,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col
SColumnIndex indexTS = {.tableIndex = index.tableIndex, .columnIndex = 0}; SColumnIndex indexTS = {.tableIndex = index.tableIndex, .columnIndex = 0};
SExprInfo* pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &indexTS, TSDB_DATA_TYPE_TIMESTAMP, SExprInfo* pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &indexTS, TSDB_DATA_TYPE_TIMESTAMP,
TSDB_KEYSIZE, getNewResColId(pCmd), TSDB_KEYSIZE, false); TSDB_KEYSIZE, getNewResColId(pCmd), TSDB_KEYSIZE, false);
tstrncpy(pExpr->base.aliasName, aAggs[TSDB_FUNC_TS_DUMMY].name, sizeof(pExpr->base.aliasName));
SColumnList ids = createColumnList(1, 0, 0); SColumnList ids = createColumnList(1, 0, 0);
insertResultField(pQueryInfo, colIndex, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS_DUMMY].name, pExpr); insertResultField(pQueryInfo, colIndex, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS_DUMMY].name, pExpr);
...@@ -8449,6 +8450,7 @@ static int32_t doValidateSubquery(SSqlNode* pSqlNode, int32_t index, SSqlObj* pS ...@@ -8449,6 +8450,7 @@ static int32_t doValidateSubquery(SSqlNode* pSqlNode, int32_t index, SSqlObj* pS
pSub->udfCopy = true; pSub->udfCopy = true;
pSub->pDownstream = pQueryInfo; pSub->pDownstream = pQueryInfo;
taosArrayPush(pQueryInfo->pUpstream, &pSub);
int32_t code = validateSqlNode(pSql, p, pSub); int32_t code = validateSqlNode(pSql, p, pSub);
if (code != TSDB_CODE_SUCCESS) { if (code != TSDB_CODE_SUCCESS) {
return code; return code;
...@@ -8472,8 +8474,6 @@ static int32_t doValidateSubquery(SSqlNode* pSqlNode, int32_t index, SSqlObj* pS ...@@ -8472,8 +8474,6 @@ static int32_t doValidateSubquery(SSqlNode* pSqlNode, int32_t index, SSqlObj* pS
tstrncpy(pTableMetaInfo1->aliasName, subInfo->aliasName.z, subInfo->aliasName.n + 1); tstrncpy(pTableMetaInfo1->aliasName, subInfo->aliasName.z, subInfo->aliasName.n + 1);
} }
taosArrayPush(pQueryInfo->pUpstream, &pSub);
// NOTE: order mix up in subquery not support yet. // NOTE: order mix up in subquery not support yet.
pQueryInfo->order = pSub->order; pQueryInfo->order = pSub->order;
......
...@@ -60,6 +60,21 @@ int32_t converToStr(char *str, int type, void *buf, int32_t bufSize, int32_t *le ...@@ -60,6 +60,21 @@ int32_t converToStr(char *str, int type, void *buf, int32_t bufSize, int32_t *le
case TSDB_DATA_TYPE_TIMESTAMP: case TSDB_DATA_TYPE_TIMESTAMP:
n = sprintf(str, "%" PRId64, *(int64_t*)buf); n = sprintf(str, "%" PRId64, *(int64_t*)buf);
break; break;
case TSDB_DATA_TYPE_UTINYINT:
n = sprintf(str, "%d", *(uint8_t*)buf);
break;
case TSDB_DATA_TYPE_USMALLINT:
n = sprintf(str, "%d", *(uint16_t*)buf);
break;
case TSDB_DATA_TYPE_UINT:
n = sprintf(str, "%d", *(uint32_t*)buf);
break;
case TSDB_DATA_TYPE_UBIGINT:
n = sprintf(str, "%" PRId64, *(uint64_t*)buf);
break;
case TSDB_DATA_TYPE_FLOAT: case TSDB_DATA_TYPE_FLOAT:
n = sprintf(str, "%f", GET_FLOAT_VAL(buf)); n = sprintf(str, "%f", GET_FLOAT_VAL(buf));
......
...@@ -1217,10 +1217,10 @@ static void doInitGlobalConfig(void) { ...@@ -1217,10 +1217,10 @@ static void doInitGlobalConfig(void) {
cfg.unitType = TAOS_CFG_UTYPE_NONE; cfg.unitType = TAOS_CFG_UTYPE_NONE;
taosInitConfigOption(cfg); taosInitConfigOption(cfg);
cfg.option = "topicBianryLen"; cfg.option = "topicBinaryLen";
cfg.ptr = &tsTopicBianryLen; cfg.ptr = &tsTopicBianryLen;
cfg.valType = TAOS_CFG_VTYPE_INT32; cfg.valType = TAOS_CFG_VTYPE_INT32;
cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG; cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_SHOW;
cfg.minValue = 16; cfg.minValue = 16;
cfg.maxValue = 16000; cfg.maxValue = 16000;
cfg.ptrLength = 0; cfg.ptrLength = 0;
......
...@@ -232,9 +232,9 @@ static struct argp_option options[] = { ...@@ -232,9 +232,9 @@ static struct argp_option options[] = {
{"inpath", 'i', "INPATH", 0, "Input file path.", 1}, {"inpath", 'i', "INPATH", 0, "Input file path.", 1},
{"resultFile", 'r', "RESULTFILE", 0, "DumpOut/In Result file path and name.", 1}, {"resultFile", 'r', "RESULTFILE", 0, "DumpOut/In Result file path and name.", 1},
#ifdef _TD_POWER_ #ifdef _TD_POWER_
{"config", 'c', "CONFIG_DIR", 0, "Configure directory. Default is /etc/power/taos.cfg.", 1}, {"config-dir", 'c', "CONFIG_DIR", 0, "Configure directory. Default is /etc/power/taos.cfg.", 1},
#else #else
{"config", 'c', "CONFIG_DIR", 0, "Configure directory. Default is /etc/taos/taos.cfg.", 1}, {"config-dir", 'c', "CONFIG_DIR", 0, "Configure directory. Default is /etc/taos/taos.cfg.", 1},
#endif #endif
{"encode", 'e', "ENCODE", 0, "Input file encoding.", 1}, {"encode", 'e', "ENCODE", 0, "Input file encoding.", 1},
// dump unit options // dump unit options
...@@ -370,6 +370,15 @@ struct arguments g_args = { ...@@ -370,6 +370,15 @@ struct arguments g_args = {
false // performance_print false // performance_print
}; };
static void errorPrintReqArg3(char *program, char *wrong_arg)
{
fprintf(stderr,
"%s: option '%s' requires an argument\n",
program, wrong_arg);
fprintf(stderr,
"Try `taosdump --help' or `taosdump --usage' for more information.\n");
}
/* Parse a single option. */ /* Parse a single option. */
static error_t parse_opt(int key, char *arg, struct argp_state *state) { static error_t parse_opt(int key, char *arg, struct argp_state *state) {
/* Get the input argument from argp_parse, which we /* Get the input argument from argp_parse, which we
...@@ -430,9 +439,13 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { ...@@ -430,9 +439,13 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
g_args.resultFile = arg; g_args.resultFile = arg;
break; break;
case 'c': case 'c':
if (0 == strlen(arg)) {
errorPrintReqArg3("taosdump", "-c or --config-dir");
exit(EXIT_FAILURE);
}
if (wordexp(arg, &full_path, 0) != 0) { if (wordexp(arg, &full_path, 0) != 0) {
errorPrint("Invalid path %s\n", arg); errorPrint("Invalid path %s\n", arg);
return -1; exit(EXIT_FAILURE);
} }
tstrncpy(configDir, full_path.we_wordv[0], MAX_FILE_NAME_LEN); tstrncpy(configDir, full_path.we_wordv[0], MAX_FILE_NAME_LEN);
wordfree(&full_path); wordfree(&full_path);
......
...@@ -4032,11 +4032,23 @@ static void irate_function(SQLFunctionCtx *pCtx) { ...@@ -4032,11 +4032,23 @@ static void irate_function(SQLFunctionCtx *pCtx) {
double v = 0; double v = 0;
GET_TYPED_DATA(v, double, pCtx->inputType, pData); GET_TYPED_DATA(v, double, pCtx->inputType, pData);
if ((INT64_MIN == pRateInfo->lastKey) || primaryKey[i] > pRateInfo->lastKey) { if (INT64_MIN == pRateInfo->lastKey) {
pRateInfo->lastValue = v; pRateInfo->lastValue = v;
pRateInfo->lastKey = primaryKey[i]; pRateInfo->lastKey = primaryKey[i];
continue; continue;
} }
if (primaryKey[i] > pRateInfo->lastKey) {
if ((INT64_MIN == pRateInfo->firstKey) || pRateInfo->lastKey > pRateInfo->firstKey) {
pRateInfo->firstValue = pRateInfo->lastValue;
pRateInfo->firstKey = pRateInfo->lastKey;
}
pRateInfo->lastValue = v;
pRateInfo->lastKey = primaryKey[i];
continue;
}
if ((INT64_MIN == pRateInfo->firstKey) || primaryKey[i] > pRateInfo->firstKey) { if ((INT64_MIN == pRateInfo->firstKey) || primaryKey[i] > pRateInfo->firstKey) {
pRateInfo->firstValue = v; pRateInfo->firstValue = v;
......
...@@ -67,10 +67,18 @@ static int32_t setBoundingBox(MinMaxEntry* range, int16_t type, double minval, d ...@@ -67,10 +67,18 @@ static int32_t setBoundingBox(MinMaxEntry* range, int16_t type, double minval, d
if (IS_SIGNED_NUMERIC_TYPE(type)) { if (IS_SIGNED_NUMERIC_TYPE(type)) {
range->i64MinVal = (int64_t) minval; range->i64MinVal = (int64_t) minval;
range->i64MaxVal = (int64_t) maxval; if (maxval > INT64_MAX || (int64_t)maxval == INT64_MIN) {
range->i64MaxVal = INT64_MAX;
} else {
range->i64MaxVal = (int64_t) maxval;
}
} else if (IS_UNSIGNED_NUMERIC_TYPE(type)){ } else if (IS_UNSIGNED_NUMERIC_TYPE(type)){
range->u64MinVal = (uint64_t) minval; range->u64MinVal = (uint64_t) minval;
range->u64MaxVal = (uint64_t) maxval; if ((uint64_t)maxval > UINT64_MAX) {
range->u64MaxVal = UINT64_MAX;
} else {
range->u64MaxVal = (uint64_t) maxval;
}
} else { } else {
range->dMinVal = minval; range->dMinVal = minval;
range->dMaxVal = maxval; range->dMaxVal = maxval;
...@@ -127,8 +135,8 @@ int32_t tBucketIntHash(tMemBucket *pBucket, const void *value) { ...@@ -127,8 +135,8 @@ int32_t tBucketIntHash(tMemBucket *pBucket, const void *value) {
index = (delta % pBucket->numOfSlots); index = (delta % pBucket->numOfSlots);
} else { } else {
double slotSpan = (double)span / pBucket->numOfSlots; double slotSpan = (double)span / pBucket->numOfSlots;
index = (int32_t)((v - pBucket->range.i64MinVal) / slotSpan); index = (int32_t)(((double)v - pBucket->range.i64MinVal) / slotSpan);
if (v == pBucket->range.i64MaxVal) { if (index == pBucket->numOfSlots) {
index -= 1; index -= 1;
} }
} }
......
...@@ -43,6 +43,7 @@ void taosStringBuilderAppendStringLen(SStringBuilder* sb, const char* str, size_ ...@@ -43,6 +43,7 @@ void taosStringBuilderAppendStringLen(SStringBuilder* sb, const char* str, size_
void taosStringBuilderAppendString(SStringBuilder* sb, const char* str); void taosStringBuilderAppendString(SStringBuilder* sb, const char* str);
void taosStringBuilderAppendNull(SStringBuilder* sb); void taosStringBuilderAppendNull(SStringBuilder* sb);
void taosStringBuilderAppendInteger(SStringBuilder* sb, int64_t v); void taosStringBuilderAppendInteger(SStringBuilder* sb, int64_t v);
void taosStringBuilderAppendUnsignedInteger(SStringBuilder* sb, uint64_t v);
void taosStringBuilderAppendDouble(SStringBuilder* sb, double v); void taosStringBuilderAppendDouble(SStringBuilder* sb, double v);
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -73,6 +73,12 @@ void taosStringBuilderAppendInteger(SStringBuilder* sb, int64_t v) { ...@@ -73,6 +73,12 @@ void taosStringBuilderAppendInteger(SStringBuilder* sb, int64_t v) {
taosStringBuilderAppendStringLen(sb, buf, MIN(len, sizeof(buf))); taosStringBuilderAppendStringLen(sb, buf, MIN(len, sizeof(buf)));
} }
void taosStringBuilderAppendUnsignedInteger(SStringBuilder* sb, uint64_t v) {
char buf[64];
size_t len = snprintf(buf, sizeof(buf), "%" PRId64, v);
taosStringBuilderAppendStringLen(sb, buf, MIN(len, sizeof(buf)));
}
void taosStringBuilderAppendDouble(SStringBuilder* sb, double v) { void taosStringBuilderAppendDouble(SStringBuilder* sb, double v) {
char buf[512]; char buf[512];
size_t len = snprintf(buf, sizeof(buf), "%.9lf", v); size_t len = snprintf(buf, sizeof(buf), "%.9lf", v);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include <unistd.h> #include <unistd.h>
static void prepare_data(TAOS* taos) { static void prepare_data(TAOS* taos) {
TAOS_RES *result; TAOS_RES* result;
result = taos_query(taos, "drop database if exists test;"); result = taos_query(taos, "drop database if exists test;");
taos_free_result(result); taos_free_result(result);
usleep(100000); usleep(100000);
...@@ -41,24 +41,25 @@ static void prepare_data(TAOS* taos) { ...@@ -41,24 +41,25 @@ static void prepare_data(TAOS* taos) {
result = taos_query(taos, "create table t9 using meters tags(9);"); result = taos_query(taos, "create table t9 using meters tags(9);");
taos_free_result(result); taos_free_result(result);
result = taos_query(taos, "insert into t0 values('2020-01-01 00:00:00.000', 0)" result = taos_query(taos,
" ('2020-01-01 00:01:00.000', 0)" "insert into t0 values('2020-01-01 00:00:00.000', 0)"
" ('2020-01-01 00:02:00.000', 0)" " ('2020-01-01 00:01:00.000', 0)"
" t1 values('2020-01-01 00:00:00.000', 0)" " ('2020-01-01 00:02:00.000', 0)"
" ('2020-01-01 00:01:00.000', 0)" " t1 values('2020-01-01 00:00:00.000', 0)"
" ('2020-01-01 00:02:00.000', 0)" " ('2020-01-01 00:01:00.000', 0)"
" ('2020-01-01 00:03:00.000', 0)" " ('2020-01-01 00:02:00.000', 0)"
" t2 values('2020-01-01 00:00:00.000', 0)" " ('2020-01-01 00:03:00.000', 0)"
" ('2020-01-01 00:01:00.000', 0)" " t2 values('2020-01-01 00:00:00.000', 0)"
" ('2020-01-01 00:01:01.000', 0)" " ('2020-01-01 00:01:00.000', 0)"
" ('2020-01-01 00:01:02.000', 0)" " ('2020-01-01 00:01:01.000', 0)"
" t3 values('2020-01-01 00:01:02.000', 0)" " ('2020-01-01 00:01:02.000', 0)"
" t4 values('2020-01-01 00:01:02.000', 0)" " t3 values('2020-01-01 00:01:02.000', 0)"
" t5 values('2020-01-01 00:01:02.000', 0)" " t4 values('2020-01-01 00:01:02.000', 0)"
" t6 values('2020-01-01 00:01:02.000', 0)" " t5 values('2020-01-01 00:01:02.000', 0)"
" t7 values('2020-01-01 00:01:02.000', 0)" " t6 values('2020-01-01 00:01:02.000', 0)"
" t8 values('2020-01-01 00:01:02.000', 0)" " t7 values('2020-01-01 00:01:02.000', 0)"
" t9 values('2020-01-01 00:01:02.000', 0)"); " t8 values('2020-01-01 00:01:02.000', 0)"
" t9 values('2020-01-01 00:01:02.000', 0)");
int affected = taos_affected_rows(result); int affected = taos_affected_rows(result);
if (affected != 18) { if (affected != 18) {
printf("\033[31m%d rows affected by last insert statement, but it should be 18\033[0m\n", affected); printf("\033[31m%d rows affected by last insert statement, but it should be 18\033[0m\n", affected);
...@@ -77,7 +78,7 @@ static int print_result(TAOS_RES* res, int blockFetch) { ...@@ -77,7 +78,7 @@ static int print_result(TAOS_RES* res, int blockFetch) {
if (blockFetch) { if (blockFetch) {
int rows = 0; int rows = 0;
while ((rows = taos_fetch_block(res, &row))) { while ((rows = taos_fetch_block(res, &row))) {
//for (int i = 0; i < rows; i++) { // for (int i = 0; i < rows; i++) {
// char temp[256]; // char temp[256];
// taos_print_row(temp, row + i, fields, num_fields); // taos_print_row(temp, row + i, fields, num_fields);
// puts(temp); // puts(temp);
...@@ -126,9 +127,9 @@ static void verify_query(TAOS* taos) { ...@@ -126,9 +127,9 @@ static void verify_query(TAOS* taos) {
TAOS_RES* res = taos_query(taos, "select * from meters"); TAOS_RES* res = taos_query(taos, "select * from meters");
check_row_count(__LINE__, res, 18); check_row_count(__LINE__, res, 18);
printf("result precision is: %d\n", taos_result_precision(res)); printf("result precision is: %d\n", taos_result_precision(res));
int c = taos_field_count(res); int c = taos_field_count(res);
printf("field count is: %d\n", c); printf("field count is: %d\n", c);
int* lengths = taos_fetch_lengths(res); int* lengths = taos_fetch_lengths(res);
for (int i = 0; i < c; i++) { for (int i = 0; i < c; i++) {
printf("length of column %d is %d\n", i, lengths[i]); printf("length of column %d is %d\n", i, lengths[i]);
...@@ -149,7 +150,7 @@ static void verify_query(TAOS* taos) { ...@@ -149,7 +150,7 @@ static void verify_query(TAOS* taos) {
taos_free_result(res); taos_free_result(res);
} }
void subscribe_callback(TAOS_SUB* tsub, TAOS_RES *res, void* param, int code) { void subscribe_callback(TAOS_SUB* tsub, TAOS_RES* res, void* param, int code) {
int rows = print_result(res, *(int*)param); int rows = print_result(res, *(int*)param);
printf("%d rows consumed in subscribe_callback\n", rows); printf("%d rows consumed in subscribe_callback\n", rows);
} }
...@@ -164,7 +165,7 @@ static void verify_subscribe(TAOS* taos) { ...@@ -164,7 +165,7 @@ static void verify_subscribe(TAOS* taos) {
res = taos_consume(tsub); res = taos_consume(tsub);
check_row_count(__LINE__, res, 0); check_row_count(__LINE__, res, 0);
TAOS_RES *result; TAOS_RES* result;
result = taos_query(taos, "insert into t0 values('2020-01-01 00:02:00.001', 0);"); result = taos_query(taos, "insert into t0 values('2020-01-01 00:02:00.001', 0);");
taos_free_result(result); taos_free_result(result);
result = taos_query(taos, "insert into t8 values('2020-01-01 00:01:03.000', 0);"); result = taos_query(taos, "insert into t8 values('2020-01-01 00:01:03.000', 0);");
...@@ -230,666 +231,7 @@ static void verify_subscribe(TAOS* taos) { ...@@ -230,666 +231,7 @@ static void verify_subscribe(TAOS* taos) {
taos_unsubscribe(tsub, 0); taos_unsubscribe(tsub, 0);
} }
void verify_prepare(TAOS* taos) { void retrieve_callback(void* param, TAOS_RES* tres, int numOfRows) {
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))";
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;
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];
} v = {0};
TAOS_STMT* stmt = taos_stmt_init(taos);
TAOS_BIND params[10];
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(v.ts);
params[0].buffer = &v.ts;
params[0].length = &params[0].buffer_length;
params[0].is_null = NULL;
params[1].buffer_type = TSDB_DATA_TYPE_BOOL;
params[1].buffer_length = sizeof(v.b);
params[1].buffer = &v.b;
params[1].length = &params[1].buffer_length;
params[1].is_null = NULL;
params[2].buffer_type = TSDB_DATA_TYPE_TINYINT;
params[2].buffer_length = sizeof(v.v1);
params[2].buffer = &v.v1;
params[2].length = &params[2].buffer_length;
params[2].is_null = NULL;
params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT;
params[3].buffer_length = sizeof(v.v2);
params[3].buffer = &v.v2;
params[3].length = &params[3].buffer_length;
params[3].is_null = NULL;
params[4].buffer_type = TSDB_DATA_TYPE_INT;
params[4].buffer_length = sizeof(v.v4);
params[4].buffer = &v.v4;
params[4].length = &params[4].buffer_length;
params[4].is_null = NULL;
params[5].buffer_type = TSDB_DATA_TYPE_BIGINT;
params[5].buffer_length = sizeof(v.v8);
params[5].buffer = &v.v8;
params[5].length = &params[5].buffer_length;
params[5].is_null = NULL;
params[6].buffer_type = TSDB_DATA_TYPE_FLOAT;
params[6].buffer_length = sizeof(v.f4);
params[6].buffer = &v.f4;
params[6].length = &params[6].buffer_length;
params[6].is_null = NULL;
params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE;
params[7].buffer_length = sizeof(v.f8);
params[7].buffer = &v.f8;
params[7].length = &params[7].buffer_length;
params[7].is_null = NULL;
params[8].buffer_type = TSDB_DATA_TYPE_BINARY;
params[8].buffer_length = sizeof(v.bin);
params[8].buffer = v.bin;
params[8].length = &params[8].buffer_length;
params[8].is_null = NULL;
strcpy(v.blob, "一二三四五六七八九十");
params[9].buffer_type = TSDB_DATA_TYPE_NCHAR;
params[9].buffer_length = strlen(v.blob);
params[9].buffer = v.blob;
params[9].length = &params[9].buffer_length;
params[9].is_null = NULL;
int is_null = 1;
sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?)";
code = taos_stmt_prepare(stmt, sql, 0);
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;
}
v.ts = 1591060628000;
for (int i = 0; i < 10; ++i) {
v.ts += 1;
for (int j = 1; j < 10; ++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);
}
if (taos_stmt_execute(stmt) != 0) {
printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
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("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
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);
}
taos_free_result(result);
taos_stmt_close(stmt);
}
void verify_prepare2(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))";
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];
} 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);
TAOS_STMT* stmt = taos_stmt_init(taos);
TAOS_MULTI_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[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, bin, blob) values(?,?,?,?,?,?,?,?,?,?)";
code = taos_stmt_prepare(stmt, sql, 0);
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) {
v.ts[i] = ts++;
is_null[i] = 0;
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.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]);
}
taos_stmt_bind_param_batch(stmt, params);
taos_stmt_add_batch(stmt);
if (taos_stmt_execute(stmt) != 0) {
printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
taos_stmt_close(stmt);
// query the records
stmt = taos_stmt_init(taos);
taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0);
TAOS_BIND qparams[2];
int8_t v1 = 5;
int16_t v2 = 15;
qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT;
qparams[0].buffer_length = sizeof(v1);
qparams[0].buffer = &v1;
qparams[0].length = &qparams[0].buffer_length;
qparams[0].is_null = NULL;
qparams[1].buffer_type = TSDB_DATA_TYPE_SMALLINT;
qparams[1].buffer_length = sizeof(v2);
qparams[1].buffer = &v2;
qparams[1].length = &qparams[1].buffer_length;
qparams[1].is_null = NULL;
taos_stmt_bind_param(stmt, qparams);
if (taos_stmt_execute(stmt) != 0) {
printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
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);
}
taos_free_result(result);
taos_stmt_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);
}
void verify_prepare3(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 stable st1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10)) tags (id1 int, id2 binary(40))";
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);
TAOS_BIND tags[2];
int32_t id1 = 1;
char id2[40] = "abcdefghijklmnopqrstuvwxyz0123456789";
uintptr_t id2_len = strlen(id2);
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[1].buffer_type = TSDB_DATA_TYPE_BINARY;
tags[1].buffer_length = sizeof(id2);
tags[1].buffer = id2;
tags[1].length = &id2_len;
tags[1].is_null = NULL;
// 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];
} 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);
TAOS_STMT* stmt = taos_stmt_init(taos);
TAOS_MULTI_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[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 ? using st1 tags(?,?) values(?,?,?,?,?,?,?,?,?,?)";
code = taos_stmt_prepare(stmt, sql, 0);
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_tags(stmt, "m1", tags);
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) {
v.ts[i] = ts++;
is_null[i] = 0;
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.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]);
}
taos_stmt_bind_param_batch(stmt, params);
taos_stmt_add_batch(stmt);
if (taos_stmt_execute(stmt) != 0) {
printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
taos_stmt_close(stmt);
// query the records
stmt = taos_stmt_init(taos);
taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0);
TAOS_BIND qparams[2];
int8_t v1 = 5;
int16_t v2 = 15;
qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT;
qparams[0].buffer_length = sizeof(v1);
qparams[0].buffer = &v1;
qparams[0].length = &qparams[0].buffer_length;
qparams[0].is_null = NULL;
qparams[1].buffer_type = TSDB_DATA_TYPE_SMALLINT;
qparams[1].buffer_length = sizeof(v2);
qparams[1].buffer = &v2;
qparams[1].length = &qparams[1].buffer_length;
qparams[1].is_null = NULL;
taos_stmt_bind_param(stmt, qparams);
if (taos_stmt_execute(stmt) != 0) {
printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
return;
}
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);
}
taos_free_result(result);
taos_stmt_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);
}
void retrieve_callback(void *param, TAOS_RES *tres, int numOfRows)
{
if (numOfRows > 0) { if (numOfRows > 0) {
printf("%d rows async retrieved\n", numOfRows); printf("%d rows async retrieved\n", numOfRows);
taos_fetch_rows_a(tres, retrieve_callback, param); taos_fetch_rows_a(tres, retrieve_callback, param);
...@@ -903,8 +245,7 @@ void retrieve_callback(void *param, TAOS_RES *tres, int numOfRows) ...@@ -903,8 +245,7 @@ void retrieve_callback(void *param, TAOS_RES *tres, int numOfRows)
} }
} }
void select_callback(void *param, TAOS_RES *tres, int code) void select_callback(void* param, TAOS_RES* tres, int code) {
{
if (code == 0 && tres) { if (code == 0 && tres) {
taos_fetch_rows_a(tres, retrieve_callback, param); taos_fetch_rows_a(tres, retrieve_callback, param);
} else { } else {
...@@ -918,11 +259,11 @@ void verify_async(TAOS* taos) { ...@@ -918,11 +259,11 @@ void verify_async(TAOS* taos) {
usleep(1000000); usleep(1000000);
} }
void stream_callback(void *param, TAOS_RES *res, TAOS_ROW row) { void stream_callback(void* param, TAOS_RES* res, TAOS_ROW row) {
if (res == NULL || row == NULL) { if (res == NULL || row == NULL) {
return; return;
} }
int num_fields = taos_num_fields(res); int num_fields = taos_num_fields(res);
TAOS_FIELD* fields = taos_fetch_fields(res); TAOS_FIELD* fields = taos_fetch_fields(res);
...@@ -934,14 +275,9 @@ void stream_callback(void *param, TAOS_RES *res, TAOS_ROW row) { ...@@ -934,14 +275,9 @@ void stream_callback(void *param, TAOS_RES *res, TAOS_ROW row) {
void verify_stream(TAOS* taos) { void verify_stream(TAOS* taos) {
prepare_data(taos); prepare_data(taos);
TAOS_STREAM* strm = taos_open_stream( TAOS_STREAM* strm =
taos, taos_open_stream(taos, "select count(*) from meters interval(1m)", stream_callback, 0, NULL, NULL);
"select count(*) from meters interval(1m)", printf("waiting for stream data\n");
stream_callback,
0,
NULL,
NULL);
printf("waiting for stream data\n");
usleep(100000); usleep(100000);
TAOS_RES* result = taos_query(taos, "insert into t0 values(now, 0)(now+5s,1)(now+10s, 2);"); TAOS_RES* result = taos_query(taos, "insert into t0 values(now, 0)(now+5s,1)(now+10s, 2);");
taos_free_result(result); taos_free_result(result);
...@@ -950,7 +286,7 @@ void verify_stream(TAOS* taos) { ...@@ -950,7 +286,7 @@ void verify_stream(TAOS* taos) {
} }
int32_t verify_schema_less(TAOS* taos) { int32_t verify_schema_less(TAOS* taos) {
TAOS_RES *result; TAOS_RES* result;
result = taos_query(taos, "drop database if exists test;"); result = taos_query(taos, "drop database if exists test;");
taos_free_result(result); taos_free_result(result);
usleep(100000); usleep(100000);
...@@ -972,49 +308,51 @@ int32_t verify_schema_less(TAOS* taos) { ...@@ -972,49 +308,51 @@ int32_t verify_schema_less(TAOS* taos) {
"st,t1=4i64,t2=5f64,t3=\"t4\" c1=3i64,c3=L\"passitagain\",c2=true,c4=5f64 1626006833642000000ns", "st,t1=4i64,t2=5f64,t3=\"t4\" c1=3i64,c3=L\"passitagain\",c2=true,c4=5f64 1626006833642000000ns",
"ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false 1626056811843316532ns", "ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false 1626056811843316532ns",
"ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false,c5=32i8,c6=64i16,c7=32i32,c8=88.88f32 1626056812843316532ns", "ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false,c5=32i8,c6=64i16,c7=32i32,c8=88.88f32 1626056812843316532ns",
"st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 1626006933640000000ns", "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 "
"stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 1626006933640000000ns", "1626006933640000000ns",
"stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933641000000ns" "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 "
}; "1626006933640000000ns",
"stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 "
"1626006933641000000ns"};
code = taos_insert_lines(taos, lines , sizeof(lines)/sizeof(char*)); code = taos_insert_lines(taos, lines, sizeof(lines) / sizeof(char*));
char* lines2[] = { char* lines2[] = {
"stg,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", "stg,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns",
"stg,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns" "stg,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns"};
};
code = taos_insert_lines(taos, &lines2[0], 1); code = taos_insert_lines(taos, &lines2[0], 1);
code = taos_insert_lines(taos, &lines2[1], 1); code = taos_insert_lines(taos, &lines2[1], 1);
char* lines3[] = { char* lines3[] = {
"sth,t1=4i64,t2=5f64,t4=5f64,ID=\"childtable\" c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933641ms", "sth,t1=4i64,t2=5f64,t4=5f64,ID=\"childtable\" c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 "
"sth,t1=4i64,t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933654ms" "1626006933641ms",
}; "sth,t1=4i64,t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933654ms"};
code = taos_insert_lines(taos, lines3, 2); code = taos_insert_lines(taos, lines3, 2);
char* lines4[] = { char* lines4[] = {"st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns",
"st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns"};
"dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns"
};
code = taos_insert_lines(taos, lines4, 2); code = taos_insert_lines(taos, lines4, 2);
char* lines5[] = { char* lines5[] = {
"zqlbgs,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=11.12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\" c0=f,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22.123456789f64,c7=\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64 1626006833639000000ns", "zqlbgs,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=11."
"zqlbgs,t9=f,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t11=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=11.12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\",t10=L\"ncharTagValue\" c10=f,c0=f,c1=127i8,c12=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22.123456789f64,c7=\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64,c11=L\"ncharColValue\" 1626006833639000000ns" "12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\" "
}; "c0=f,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22.123456789f64,c7="
"\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64 1626006833639000000ns",
"zqlbgs,t9=f,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t11=127i8,t2=32767i16,t3=2147483647i32,t4="
"9223372036854775807i64,t5=11.12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\",t10="
"L\"ncharTagValue\" "
"c10=f,c0=f,c1=127i8,c12=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22."
"123456789f64,c7=\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64,c11=L\"ncharColValue\" 1626006833639000000ns"};
code = taos_insert_lines(taos, &lines5[0], 1); code = taos_insert_lines(taos, &lines5[0], 1);
code = taos_insert_lines(taos, &lines5[1], 1); code = taos_insert_lines(taos, &lines5[1], 1);
char* lines6[] = {"st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns",
char* lines6[] = { "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns"};
"st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns",
"dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns"
};
code = taos_insert_lines(taos, lines6, 2); code = taos_insert_lines(taos, lines6, 2);
return (code); return (code);
} }
int main(int argc, char *argv[]) { int main(int argc, char* argv[]) {
const char* host = "127.0.0.1"; const char* host = "127.0.0.1";
const char* user = "root"; const char* user = "root";
const char* passwd = "taosdata"; const char* passwd = "taosdata";
...@@ -1034,7 +372,6 @@ int main(int argc, char *argv[]) { ...@@ -1034,7 +372,6 @@ int main(int argc, char *argv[]) {
printf("************ verify schema-less *************\n"); printf("************ verify schema-less *************\n");
verify_schema_less(taos); verify_schema_less(taos);
printf("************ verify query *************\n"); printf("************ verify query *************\n");
verify_query(taos); verify_query(taos);
...@@ -1044,16 +381,8 @@ int main(int argc, char *argv[]) { ...@@ -1044,16 +381,8 @@ int main(int argc, char *argv[]) {
printf("*********** verify subscribe ************\n"); printf("*********** verify subscribe ************\n");
verify_subscribe(taos); verify_subscribe(taos);
printf("************ verify prepare *************\n");
verify_prepare(taos);
printf("************ verify prepare2 *************\n");
verify_prepare2(taos);
printf("************ verify prepare3 *************\n");
verify_prepare3(taos);
printf("************ verify stream *************\n"); printf("************ verify stream *************\n");
verify_stream(taos); // verify_stream(taos);
printf("done\n"); printf("done\n");
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
......
...@@ -20,9 +20,9 @@ ...@@ -20,9 +20,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#include <taos.h> #include <taos.h>
...@@ -33,14 +33,14 @@ int tablesSelectProcessed = 0; ...@@ -33,14 +33,14 @@ int tablesSelectProcessed = 0;
int64_t st, et; int64_t st, et;
typedef struct { typedef struct {
int id; int id;
TAOS *taos; TAOS * taos;
char name[16]; char name[16];
time_t timeStamp; time_t timeStamp;
int value; int value;
int rowsInserted; int rowsInserted;
int rowsTried; int rowsTried;
int rowsRetrieved; int rowsRetrieved;
} STable; } STable;
void taos_insert_call_back(void *param, TAOS_RES *tres, int code); void taos_insert_call_back(void *param, TAOS_RES *tres, int code);
...@@ -48,7 +48,7 @@ void taos_select_call_back(void *param, TAOS_RES *tres, int code); ...@@ -48,7 +48,7 @@ void taos_select_call_back(void *param, TAOS_RES *tres, int code);
void taos_error(TAOS *taos); void taos_error(TAOS *taos);
static void queryDB(TAOS *taos, char *command) { static void queryDB(TAOS *taos, char *command) {
int i; int i;
TAOS_RES *pSql = NULL; TAOS_RES *pSql = NULL;
int32_t code = -1; int32_t code = -1;
...@@ -57,12 +57,12 @@ static void queryDB(TAOS *taos, char *command) { ...@@ -57,12 +57,12 @@ static void queryDB(TAOS *taos, char *command) {
taos_free_result(pSql); taos_free_result(pSql);
pSql = NULL; pSql = NULL;
} }
pSql = taos_query(taos, command); pSql = taos_query(taos, command);
code = taos_errno(pSql); code = taos_errno(pSql);
if (0 == code) { if (0 == code) {
break; break;
} }
} }
if (code != 0) { if (code != 0) {
...@@ -76,15 +76,14 @@ static void queryDB(TAOS *taos, char *command) { ...@@ -76,15 +76,14 @@ static void queryDB(TAOS *taos, char *command) {
taos_free_result(pSql); taos_free_result(pSql);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{ TAOS * taos;
TAOS *taos; struct timeval systemTime;
struct timeval systemTime; int i;
int i; char sql[1024] = {0};
char sql[1024] = { 0 }; char prefix[20] = {0};
char prefix[20] = { 0 }; char db[128] = {0};
char db[128] = { 0 }; STable * tableList;
STable *tableList;
if (argc != 5) { if (argc != 5) {
printf("usage: %s server-ip dbname rowsPerTable numOfTables\n", argv[0]); printf("usage: %s server-ip dbname rowsPerTable numOfTables\n", argv[0]);
...@@ -101,8 +100,7 @@ int main(int argc, char *argv[]) ...@@ -101,8 +100,7 @@ int main(int argc, char *argv[])
memset(tableList, 0, size); memset(tableList, 0, size);
taos = taos_connect(argv[1], "root", "taosdata", NULL, 0); taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
if (taos == NULL) if (taos == NULL) taos_error(taos);
taos_error(taos);
printf("success to connect to server\n"); printf("success to connect to server\n");
...@@ -122,7 +120,7 @@ int main(int argc, char *argv[]) ...@@ -122,7 +120,7 @@ int main(int argc, char *argv[])
sprintf(tableList[i].name, "%s%d", prefix, i); sprintf(tableList[i].name, "%s%d", prefix, i);
sprintf(sql, "create table %s%d (ts timestamp, volume bigint)", prefix, i); sprintf(sql, "create table %s%d (ts timestamp, volume bigint)", prefix, i);
queryDB(taos, sql); queryDB(taos, sql);
} }
gettimeofday(&systemTime, NULL); gettimeofday(&systemTime, NULL);
for (i = 0; i < numOfTables; ++i) for (i = 0; i < numOfTables; ++i)
...@@ -138,7 +136,7 @@ int main(int argc, char *argv[]) ...@@ -138,7 +136,7 @@ int main(int argc, char *argv[])
tablesInsertProcessed = 0; tablesInsertProcessed = 0;
tablesSelectProcessed = 0; tablesSelectProcessed = 0;
for (i = 0; i<numOfTables; ++i) { for (i = 0; i < numOfTables; ++i) {
// insert records in asynchronous API // insert records in asynchronous API
sprintf(sql, "insert into %s values(%ld, 0)", tableList[i].name, 1546300800000 + i); sprintf(sql, "insert into %s values(%ld, 0)", tableList[i].name, 1546300800000 + i);
taos_query_a(taos, sql, taos_insert_call_back, (void *)(tableList + i)); taos_query_a(taos, sql, taos_insert_call_back, (void *)(tableList + i));
...@@ -147,12 +145,12 @@ int main(int argc, char *argv[]) ...@@ -147,12 +145,12 @@ int main(int argc, char *argv[])
printf("once insert finished, presse any key to query\n"); printf("once insert finished, presse any key to query\n");
getchar(); getchar();
while(1) { while (1) {
if (tablesInsertProcessed < numOfTables) { if (tablesInsertProcessed < numOfTables) {
printf("wait for process finished\n"); printf("wait for process finished\n");
sleep(1); sleep(1);
continue; continue;
} }
break; break;
} }
...@@ -161,9 +159,8 @@ int main(int argc, char *argv[]) ...@@ -161,9 +159,8 @@ int main(int argc, char *argv[])
gettimeofday(&systemTime, NULL); gettimeofday(&systemTime, NULL);
st = systemTime.tv_sec * 1000000 + systemTime.tv_usec; st = systemTime.tv_sec * 1000000 + systemTime.tv_usec;
for (i = 0; i < numOfTables; ++i) { for (i = 0; i < numOfTables; ++i) {
// select records in asynchronous API // select records in asynchronous API
sprintf(sql, "select * from %s", tableList[i].name); sprintf(sql, "select * from %s", tableList[i].name);
taos_query_a(taos, sql, taos_select_call_back, (void *)(tableList + i)); taos_query_a(taos, sql, taos_select_call_back, (void *)(tableList + i));
} }
...@@ -171,17 +168,17 @@ int main(int argc, char *argv[]) ...@@ -171,17 +168,17 @@ int main(int argc, char *argv[])
printf("\nonce finished, press any key to exit\n"); printf("\nonce finished, press any key to exit\n");
getchar(); getchar();
while(1) { while (1) {
if (tablesSelectProcessed < numOfTables) { if (tablesSelectProcessed < numOfTables) {
printf("wait for process finished\n"); printf("wait for process finished\n");
sleep(1); sleep(1);
continue; continue;
} }
break; break;
} }
for (i = 0; i<numOfTables; ++i) { for (i = 0; i < numOfTables; ++i) {
printf("%s inserted:%d retrieved:%d\n", tableList[i].name, tableList[i].rowsInserted, tableList[i].rowsRetrieved); printf("%s inserted:%d retrieved:%d\n", tableList[i].name, tableList[i].rowsInserted, tableList[i].rowsRetrieved);
} }
...@@ -193,60 +190,54 @@ int main(int argc, char *argv[]) ...@@ -193,60 +190,54 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
void taos_error(TAOS *con) void taos_error(TAOS *con) {
{
fprintf(stderr, "TDengine error: %s\n", taos_errstr(con)); fprintf(stderr, "TDengine error: %s\n", taos_errstr(con));
taos_close(con); taos_close(con);
taos_cleanup(); taos_cleanup();
exit(1); exit(1);
} }
void taos_insert_call_back(void *param, TAOS_RES *tres, int code) void taos_insert_call_back(void *param, TAOS_RES *tres, int code) {
{ STable * pTable = (STable *)param;
STable *pTable = (STable *)param; struct timeval systemTime;
struct timeval systemTime; char sql[128];
char sql[128];
pTable->rowsTried++; pTable->rowsTried++;
if (code < 0) { if (code < 0) {
printf("%s insert failed, code:%d, rows:%d\n", pTable->name, code, pTable->rowsTried); printf("%s insert failed, code:%d, rows:%d\n", pTable->name, code, pTable->rowsTried);
} } else if (code == 0) {
else if (code == 0) {
printf("%s not inserted\n", pTable->name); printf("%s not inserted\n", pTable->name);
} } else {
else {
pTable->rowsInserted++; pTable->rowsInserted++;
} }
if (pTable->rowsTried < points) { if (pTable->rowsTried < points) {
// for this demo, insert another record // for this demo, insert another record
sprintf(sql, "insert into %s values(%ld, %d)", pTable->name, 1546300800000+pTable->rowsTried*1000, pTable->rowsTried); sprintf(sql, "insert into %s values(%ld, %d)", pTable->name, 1546300800000 + pTable->rowsTried * 1000,
pTable->rowsTried);
taos_query_a(pTable->taos, sql, taos_insert_call_back, (void *)pTable); taos_query_a(pTable->taos, sql, taos_insert_call_back, (void *)pTable);
} } else {
else {
printf("%d rows data are inserted into %s\n", points, pTable->name); printf("%d rows data are inserted into %s\n", points, pTable->name);
tablesInsertProcessed++; tablesInsertProcessed++;
if (tablesInsertProcessed >= numOfTables) { if (tablesInsertProcessed >= numOfTables) {
gettimeofday(&systemTime, NULL); gettimeofday(&systemTime, NULL);
et = systemTime.tv_sec * 1000000 + systemTime.tv_usec; et = systemTime.tv_sec * 1000000 + systemTime.tv_usec;
printf("%lld mseconds to insert %d data points\n", (et - st) / 1000, points*numOfTables); printf("%lld mseconds to insert %d data points\n", (et - st) / 1000, points * numOfTables);
} }
} }
taos_free_result(tres); taos_free_result(tres);
} }
void taos_retrieve_call_back(void *param, TAOS_RES *tres, int numOfRows) void taos_retrieve_call_back(void *param, TAOS_RES *tres, int numOfRows) {
{ STable * pTable = (STable *)param;
STable *pTable = (STable *)param;
struct timeval systemTime; struct timeval systemTime;
if (numOfRows > 0) { if (numOfRows > 0) {
for (int i = 0; i < numOfRows; ++i) {
for (int i = 0; i<numOfRows; ++i) {
// synchronous API to retrieve a row from batch of records // synchronous API to retrieve a row from batch of records
/*TAOS_ROW row = */(void)taos_fetch_row(tres); /*TAOS_ROW row = */ (void)taos_fetch_row(tres);
// process row // process row
} }
...@@ -255,12 +246,10 @@ void taos_retrieve_call_back(void *param, TAOS_RES *tres, int numOfRows) ...@@ -255,12 +246,10 @@ void taos_retrieve_call_back(void *param, TAOS_RES *tres, int numOfRows)
// retrieve next batch of rows // retrieve next batch of rows
taos_fetch_rows_a(tres, taos_retrieve_call_back, pTable); taos_fetch_rows_a(tres, taos_retrieve_call_back, pTable);
} } else {
else { if (numOfRows < 0) printf("%s retrieve failed, code:%d\n", pTable->name, numOfRows);
if (numOfRows < 0)
printf("%s retrieve failed, code:%d\n", pTable->name, numOfRows);
//taos_free_result(tres); // taos_free_result(tres);
printf("%d rows data retrieved from %s\n", pTable->rowsRetrieved, pTable->name); printf("%d rows data retrieved from %s\n", pTable->rowsRetrieved, pTable->name);
tablesSelectProcessed++; tablesSelectProcessed++;
...@@ -272,19 +261,15 @@ void taos_retrieve_call_back(void *param, TAOS_RES *tres, int numOfRows) ...@@ -272,19 +261,15 @@ void taos_retrieve_call_back(void *param, TAOS_RES *tres, int numOfRows)
taos_free_result(tres); taos_free_result(tres);
} }
} }
void taos_select_call_back(void *param, TAOS_RES *tres, int code) void taos_select_call_back(void *param, TAOS_RES *tres, int code) {
{
STable *pTable = (STable *)param; STable *pTable = (STable *)param;
if (code == 0 && tres) { if (code == 0 && tres) {
// asynchronous API to fetch a batch of records // asynchronous API to fetch a batch of records
taos_fetch_rows_a(tres, taos_retrieve_call_back, pTable); taos_fetch_rows_a(tres, taos_retrieve_call_back, pTable);
} } else {
else {
printf("%s select failed, code:%d\n", pTable->name, code); printf("%s select failed, code:%d\n", pTable->name, code);
taos_free_result(tres); taos_free_result(tres);
taos_cleanup(); taos_cleanup();
......
...@@ -16,14 +16,14 @@ ...@@ -16,14 +16,14 @@
// TAOS standard API example. The same syntax as MySQL, but only a subset // TAOS standard API example. The same syntax as MySQL, but only a subset
// to compile: gcc -o demo demo.c -ltaos // to compile: gcc -o demo demo.c -ltaos
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <inttypes.h>
#include <taos.h> // TAOS header file #include <taos.h> // TAOS header file
static void queryDB(TAOS *taos, char *command) { static void queryDB(TAOS *taos, char *command) {
int i; int i;
TAOS_RES *pSql = NULL; TAOS_RES *pSql = NULL;
int32_t code = -1; int32_t code = -1;
...@@ -32,12 +32,12 @@ static void queryDB(TAOS *taos, char *command) { ...@@ -32,12 +32,12 @@ static void queryDB(TAOS *taos, char *command) {
taos_free_result(pSql); taos_free_result(pSql);
pSql = NULL; pSql = NULL;
} }
pSql = taos_query(taos, command); pSql = taos_query(taos, command);
code = taos_errno(pSql); code = taos_errno(pSql);
if (0 == code) { if (0 == code) {
break; break;
} }
} }
if (code != 0) { if (code != 0) {
...@@ -53,7 +53,7 @@ static void queryDB(TAOS *taos, char *command) { ...@@ -53,7 +53,7 @@ static void queryDB(TAOS *taos, char *command) {
void Test(TAOS *taos, char *qstr, int i); void Test(TAOS *taos, char *qstr, int i);
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char qstr[1024]; char qstr[1024];
// connect to server // connect to server
if (argc < 2) { if (argc < 2) {
...@@ -63,7 +63,7 @@ int main(int argc, char *argv[]) { ...@@ -63,7 +63,7 @@ int main(int argc, char *argv[]) {
TAOS *taos = taos_connect(argv[1], "root", "taosdata", NULL, 0); TAOS *taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
if (taos == NULL) { if (taos == NULL) {
printf("failed to connect to server, reason:%s\n", "null taos"/*taos_errstr(taos)*/); printf("failed to connect to server, reason:%s\n", "null taos" /*taos_errstr(taos)*/);
exit(1); exit(1);
} }
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
...@@ -72,28 +72,30 @@ int main(int argc, char *argv[]) { ...@@ -72,28 +72,30 @@ int main(int argc, char *argv[]) {
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
} }
void Test(TAOS *taos, char *qstr, int index) { void Test(TAOS *taos, char *qstr, int index) {
printf("==================test at %d\n================================", index); printf("==================test at %d\n================================", index);
queryDB(taos, "drop database if exists demo"); queryDB(taos, "drop database if exists demo");
queryDB(taos, "create database demo"); queryDB(taos, "create database demo");
TAOS_RES *result; TAOS_RES *result;
queryDB(taos, "use demo"); queryDB(taos, "use demo");
queryDB(taos, "create table m1 (ts timestamp, ti tinyint, si smallint, i int, bi bigint, f float, d double, b binary(10))"); queryDB(taos,
"create table m1 (ts timestamp, ti tinyint, si smallint, i int, bi bigint, f float, d double, b binary(10))");
printf("success to create table\n"); printf("success to create table\n");
int i = 0; int i = 0;
for (i = 0; i < 10; ++i) { for (i = 0; i < 10; ++i) {
sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", (uint64_t)(1546300800000 + i * 1000), i, i, i, i*10000000, i*1.0, i*2.0, "hello"); sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')",
(uint64_t)(1546300800000 + i * 1000), i, i, i, i * 10000000, i * 1.0, i * 2.0, "hello");
printf("qstr: %s\n", qstr); printf("qstr: %s\n", qstr);
// note: how do you wanna do if taos_query returns non-NULL // note: how do you wanna do if taos_query returns non-NULL
// if (taos_query(taos, qstr)) { // if (taos_query(taos, qstr)) {
// printf("insert row: %i, reason:%s\n", i, taos_errstr(taos)); // printf("insert row: %i, reason:%s\n", i, taos_errstr(taos));
// } // }
TAOS_RES *result1 = taos_query(taos, qstr); TAOS_RES *result1 = taos_query(taos, qstr);
if (result1 == NULL || taos_errno(result1) != 0) { if (result1 == NULL || taos_errno(result1) != 0) {
printf("failed to insert row, reason:%s\n", taos_errstr(result1)); printf("failed to insert row, reason:%s\n", taos_errstr(result1));
taos_free_result(result1); taos_free_result(result1);
exit(1); exit(1);
} else { } else {
...@@ -107,7 +109,7 @@ void Test(TAOS *taos, char *qstr, int index) { ...@@ -107,7 +109,7 @@ void Test(TAOS *taos, char *qstr, int index) {
sprintf(qstr, "SELECT * FROM m1"); sprintf(qstr, "SELECT * FROM m1");
result = taos_query(taos, qstr); result = taos_query(taos, qstr);
if (result == NULL || taos_errno(result) != 0) { if (result == NULL || taos_errno(result) != 0) {
printf("failed to select, reason:%s\n", taos_errstr(result)); printf("failed to select, reason:%s\n", taos_errstr(result));
taos_free_result(result); taos_free_result(result);
exit(1); exit(1);
} }
...@@ -130,4 +132,3 @@ void Test(TAOS *taos, char *qstr, int index) { ...@@ -130,4 +132,3 @@ void Test(TAOS *taos, char *qstr, int index) {
taos_free_result(result); taos_free_result(result);
printf("====demo end====\n\n"); printf("====demo end====\n\n");
} }
...@@ -21,103 +21,101 @@ ...@@ -21,103 +21,101 @@
#ifdef __APPLE__ #ifdef __APPLE__
#include "osEok.h" #include "osEok.h"
#else // __APPLE__ #else // __APPLE__
#include <sys/epoll.h> #include <sys/epoll.h>
#endif // __APPLE__ #endif // __APPLE__
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <fcntl.h>
#include <arpa/inet.h>
#include <libgen.h> #include <libgen.h>
#include <locale.h> #include <locale.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) #define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__)
#define A(statement, fmt, ...) do { \ #define A(statement, fmt, ...) \
if (statement) break; \ do { \
fprintf(stderr, "%s[%d]%s(): assert [%s] failed: %d[%s]: " fmt "\n", \ if (statement) break; \
basename(__FILE__), __LINE__, __func__, \ fprintf(stderr, "%s[%d]%s(): assert [%s] failed: %d[%s]: " fmt "\n", basename(__FILE__), __LINE__, __func__, \
#statement, errno, strerror(errno), \ #statement, errno, strerror(errno), ##__VA_ARGS__); \
##__VA_ARGS__); \ abort(); \
abort(); \ } while (0)
} while (0)
#define E(fmt, ...) do { \ #define E(fmt, ...) \
fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", \ do { \
basename(__FILE__), __LINE__, __func__, \ fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", basename(__FILE__), __LINE__, __func__, errno, strerror(errno), \
errno, strerror(errno), \ ##__VA_ARGS__); \
##__VA_ARGS__); \ } while (0)
} while (0)
#include "os.h" #include "os.h"
typedef struct ep_s ep_t; typedef struct ep_s ep_t;
struct ep_s { struct ep_s {
int ep; int ep;
pthread_mutex_t lock; pthread_mutex_t lock;
int sv[2]; // 0 for read, 1 for write; int sv[2]; // 0 for read, 1 for write;
pthread_t thread; pthread_t thread;
volatile unsigned int stopping:1; volatile unsigned int stopping : 1;
volatile unsigned int waiting:1; volatile unsigned int waiting : 1;
volatile unsigned int wakenup:1; volatile unsigned int wakenup : 1;
}; };
static int ep_dummy = 0; static int ep_dummy = 0;
static ep_t* ep_create(void); static ep_t *ep_create(void);
static void ep_destroy(ep_t *ep); static void ep_destroy(ep_t *ep);
static void* routine(void* arg); static void *routine(void *arg);
static int open_listen(unsigned short port); static int open_listen(unsigned short port);
typedef struct fde_s fde_t; typedef struct fde_s fde_t;
struct fde_s { struct fde_s {
int skt; int skt;
void (*on_event)(ep_t *ep, struct epoll_event *events, fde_t *client); void (*on_event)(ep_t *ep, struct epoll_event *events, fde_t *client);
}; };
static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client); static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client);
static void null_event(ep_t *ep, struct epoll_event *ev, fde_t *client); static void null_event(ep_t *ep, struct epoll_event *ev, fde_t *client);
#define usage(arg0, fmt, ...) do { \ #define usage(arg0, fmt, ...) \
if (fmt[0]) { \ do { \
fprintf(stderr, "" fmt "\n", ##__VA_ARGS__); \ if (fmt[0]) { \
} \ fprintf(stderr, "" fmt "\n", ##__VA_ARGS__); \
fprintf(stderr, "usage:\n"); \ } \
fprintf(stderr, " %s -l <port> : specify listenning port\n", arg0); \ fprintf(stderr, "usage:\n"); \
} while (0) fprintf(stderr, " %s -l <port> : specify listenning port\n", arg0); \
} while (0)
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char *prg = basename(argv[0]); char *prg = basename(argv[0]);
if (argc==1) { if (argc == 1) {
usage(prg, ""); usage(prg, "");
return 0; return 0;
} }
ep_t* ep = ep_create(); ep_t *ep = ep_create();
A(ep, "failed"); A(ep, "failed");
for (int i=1; i<argc; ++i) { for (int i = 1; i < argc; ++i) {
const char *arg = argv[i]; const char *arg = argv[i];
if (0==strcmp(arg, "-l")) { if (0 == strcmp(arg, "-l")) {
++i; ++i;
if (i>=argc) { if (i >= argc) {
usage(prg, "expecting <port> after -l, but got nothing"); usage(prg, "expecting <port> after -l, but got nothing");
return 1; // confirmed potential leakage return 1; // confirmed potential leakage
} }
arg = argv[i]; arg = argv[i];
int port = atoi(arg); int port = atoi(arg);
int skt = open_listen(port); int skt = open_listen(port);
if (skt==-1) continue; if (skt == -1) continue;
fde_t *client = (fde_t*)calloc(1, sizeof(*client)); fde_t *client = (fde_t *)calloc(1, sizeof(*client));
if (!client) { if (!client) {
E("out of memory"); E("out of memory");
close(skt); close(skt);
...@@ -126,32 +124,32 @@ int main(int argc, char *argv[]) { ...@@ -126,32 +124,32 @@ int main(int argc, char *argv[]) {
client->skt = skt; client->skt = skt;
client->on_event = listen_event; client->on_event = listen_event;
struct epoll_event ev = {0}; struct epoll_event ev = {0};
ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP;
ev.data.ptr = client; ev.data.ptr = client;
A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ev), ""); A(0 == epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ev), "");
continue; continue;
} }
usage(prg, "unknown argument: [%s]", arg); usage(prg, "unknown argument: [%s]", arg);
return 1; return 1;
} }
char *line = NULL; char * line = NULL;
size_t linecap = 0; size_t linecap = 0;
ssize_t linelen; ssize_t linelen;
while ((linelen = getline(&line, &linecap, stdin)) > 0) { while ((linelen = getline(&line, &linecap, stdin)) > 0) {
line[strlen(line)-1] = '\0'; line[strlen(line) - 1] = '\0';
if (0==strcmp(line, "exit")) break; if (0 == strcmp(line, "exit")) break;
if (0==strcmp(line, "quit")) break; if (0 == strcmp(line, "quit")) break;
if (line==strstr(line, "close")) { if (line == strstr(line, "close")) {
int fd = 0; int fd = 0;
sscanf(line, "close %d", &fd); sscanf(line, "close %d", &fd);
if (fd<=2) { if (fd <= 2) {
fprintf(stderr, "fd [%d] invalid\n", fd); fprintf(stderr, "fd [%d] invalid\n", fd);
continue; continue;
} }
A(0==epoll_ctl(ep->ep, EPOLL_CTL_DEL, fd, NULL), ""); A(0 == epoll_ctl(ep->ep, EPOLL_CTL_DEL, fd, NULL), "");
continue; continue;
} }
if (strlen(line)==0) continue; if (strlen(line) == 0) continue;
fprintf(stderr, "unknown cmd:[%s]\n", line); fprintf(stderr, "unknown cmd:[%s]\n", line);
} }
ep_destroy(ep); ep_destroy(ep);
...@@ -159,69 +157,69 @@ int main(int argc, char *argv[]) { ...@@ -159,69 +157,69 @@ int main(int argc, char *argv[]) {
return 0; return 0;
} }
ep_t* ep_create(void) { ep_t *ep_create(void) {
ep_t *ep = (ep_t*)calloc(1, sizeof(*ep)); ep_t *ep = (ep_t *)calloc(1, sizeof(*ep));
A(ep, "out of memory"); A(ep, "out of memory");
A(-1!=(ep->ep = epoll_create(1)), ""); A(-1 != (ep->ep = epoll_create(1)), "");
ep->sv[0] = -1; ep->sv[0] = -1;
ep->sv[1] = -1; ep->sv[1] = -1;
A(0==socketpair(AF_LOCAL, SOCK_STREAM, 0, ep->sv), ""); A(0 == socketpair(AF_LOCAL, SOCK_STREAM, 0, ep->sv), "");
A(0==pthread_mutex_init(&ep->lock, NULL), ""); A(0 == pthread_mutex_init(&ep->lock, NULL), "");
A(0==pthread_mutex_lock(&ep->lock), ""); A(0 == pthread_mutex_lock(&ep->lock), "");
struct epoll_event ev = {0}; struct epoll_event ev = {0};
ev.events = EPOLLIN; ev.events = EPOLLIN;
ev.data.ptr = &ep_dummy; ev.data.ptr = &ep_dummy;
A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, ep->sv[0], &ev), ""); A(0 == epoll_ctl(ep->ep, EPOLL_CTL_ADD, ep->sv[0], &ev), "");
A(0==pthread_create(&ep->thread, NULL, routine, ep), ""); A(0 == pthread_create(&ep->thread, NULL, routine, ep), "");
A(0==pthread_mutex_unlock(&ep->lock), ""); A(0 == pthread_mutex_unlock(&ep->lock), "");
return ep; return ep;
} }
static void ep_destroy(ep_t *ep) { static void ep_destroy(ep_t *ep) {
A(ep, "invalid argument"); A(ep, "invalid argument");
ep->stopping = 1; ep->stopping = 1;
A(1==send(ep->sv[1], "1", 1, 0), ""); A(1 == send(ep->sv[1], "1", 1, 0), "");
A(0==pthread_join(ep->thread, NULL), ""); A(0 == pthread_join(ep->thread, NULL), "");
A(0==pthread_mutex_destroy(&ep->lock), ""); A(0 == pthread_mutex_destroy(&ep->lock), "");
A(0==close(ep->sv[0]), ""); A(0 == close(ep->sv[0]), "");
A(0==close(ep->sv[1]), ""); A(0 == close(ep->sv[1]), "");
A(0==close(ep->ep), ""); A(0 == close(ep->ep), "");
free(ep); free(ep);
} }
static void* routine(void* arg) { static void *routine(void *arg) {
A(arg, "invalid argument"); A(arg, "invalid argument");
ep_t *ep = (ep_t*)arg; ep_t *ep = (ep_t *)arg;
while (!ep->stopping) { while (!ep->stopping) {
struct epoll_event evs[10]; struct epoll_event evs[10];
memset(evs, 0, sizeof(evs)); memset(evs, 0, sizeof(evs));
A(0==pthread_mutex_lock(&ep->lock), ""); A(0 == pthread_mutex_lock(&ep->lock), "");
A(ep->waiting==0, "internal logic error"); A(ep->waiting == 0, "internal logic error");
ep->waiting = 1; ep->waiting = 1;
A(0==pthread_mutex_unlock(&ep->lock), ""); A(0 == pthread_mutex_unlock(&ep->lock), "");
int r = epoll_wait(ep->ep, evs, sizeof(evs)/sizeof(evs[0]), -1); int r = epoll_wait(ep->ep, evs, sizeof(evs) / sizeof(evs[0]), -1);
A(r>0, "indefinite epoll_wait shall not timeout:[%d]", r); A(r > 0, "indefinite epoll_wait shall not timeout:[%d]", r);
A(0==pthread_mutex_lock(&ep->lock), ""); A(0 == pthread_mutex_lock(&ep->lock), "");
A(ep->waiting==1, "internal logic error"); A(ep->waiting == 1, "internal logic error");
ep->waiting = 0; ep->waiting = 0;
A(0==pthread_mutex_unlock(&ep->lock), ""); A(0 == pthread_mutex_unlock(&ep->lock), "");
for (int i=0; i<r; ++i) { for (int i = 0; i < r; ++i) {
struct epoll_event *ev = evs + i; struct epoll_event *ev = evs + i;
if (ev->data.ptr == &ep_dummy) { if (ev->data.ptr == &ep_dummy) {
char c = '\0'; char c = '\0';
A(1==recv(ep->sv[0], &c, 1, 0), "internal logic error"); A(1 == recv(ep->sv[0], &c, 1, 0), "internal logic error");
A(0==pthread_mutex_lock(&ep->lock), ""); A(0 == pthread_mutex_lock(&ep->lock), "");
ep->wakenup = 0; ep->wakenup = 0;
A(0==pthread_mutex_unlock(&ep->lock), ""); A(0 == pthread_mutex_unlock(&ep->lock), "");
continue; continue;
} }
A(ev->data.ptr, "internal logic error"); A(ev->data.ptr, "internal logic error");
fde_t *client = (fde_t*)ev->data.ptr; fde_t *client = (fde_t *)ev->data.ptr;
client->on_event(ep, ev, client); client->on_event(ep, ev, client);
continue; continue;
} }
...@@ -232,7 +230,7 @@ static void* routine(void* arg) { ...@@ -232,7 +230,7 @@ static void* routine(void* arg) {
static int open_listen(unsigned short port) { static int open_listen(unsigned short port) {
int r = 0; int r = 0;
int skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (skt==-1) { if (skt == -1) {
E("socket() failed"); E("socket() failed");
return -1; return -1;
} }
...@@ -241,7 +239,7 @@ static int open_listen(unsigned short port) { ...@@ -241,7 +239,7 @@ static int open_listen(unsigned short port) {
si.sin_family = AF_INET; si.sin_family = AF_INET;
si.sin_addr.s_addr = inet_addr("0.0.0.0"); si.sin_addr.s_addr = inet_addr("0.0.0.0");
si.sin_port = htons(port); si.sin_port = htons(port);
r = bind(skt, (struct sockaddr*)&si, sizeof(si)); r = bind(skt, (struct sockaddr *)&si, sizeof(si));
if (r) { if (r) {
E("bind(%u) failed", port); E("bind(%u) failed", port);
break; break;
...@@ -257,7 +255,7 @@ static int open_listen(unsigned short port) { ...@@ -257,7 +255,7 @@ static int open_listen(unsigned short port) {
if (r) { if (r) {
E("getsockname() failed"); E("getsockname() failed");
} }
A(len==sizeof(si), "internal logic error"); A(len == sizeof(si), "internal logic error");
D("listenning at: %d", ntohs(si.sin_port)); D("listenning at: %d", ntohs(si.sin_port));
return skt; return skt;
} while (0); } while (0);
...@@ -268,10 +266,10 @@ static int open_listen(unsigned short port) { ...@@ -268,10 +266,10 @@ static int open_listen(unsigned short port) {
static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client) { static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client) {
A(ev->events & EPOLLIN, "internal logic error"); A(ev->events & EPOLLIN, "internal logic error");
struct sockaddr_in si = {0}; struct sockaddr_in si = {0};
socklen_t silen = sizeof(si); socklen_t silen = sizeof(si);
int skt = accept(client->skt, (struct sockaddr*)&si, &silen); int skt = accept(client->skt, (struct sockaddr *)&si, &silen);
A(skt!=-1, "internal logic error"); A(skt != -1, "internal logic error");
fde_t *server = (fde_t*)calloc(1, sizeof(*server)); fde_t *server = (fde_t *)calloc(1, sizeof(*server));
if (!server) { if (!server) {
close(skt); close(skt);
return; return;
...@@ -279,26 +277,25 @@ static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client) { ...@@ -279,26 +277,25 @@ static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client) {
server->skt = skt; server->skt = skt;
server->on_event = null_event; server->on_event = null_event;
struct epoll_event ee = {0}; struct epoll_event ee = {0};
ee.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; ee.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP;
ee.data.ptr = server; ee.data.ptr = server;
A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ee), ""); A(0 == epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ee), "");
} }
static void null_event(ep_t *ep, struct epoll_event *ev, fde_t *client) { static void null_event(ep_t *ep, struct epoll_event *ev, fde_t *client) {
if (ev->events & EPOLLIN) { if (ev->events & EPOLLIN) {
char buf[8192]; char buf[8192];
int n = recv(client->skt, buf, sizeof(buf), 0); int n = recv(client->skt, buf, sizeof(buf), 0);
A(n>=0 && n<=sizeof(buf), "internal logic error:[%d]", n); A(n >= 0 && n <= sizeof(buf), "internal logic error:[%d]", n);
A(n==fwrite(buf, 1, n, stdout), "internal logic error"); A(n == fwrite(buf, 1, n, stdout), "internal logic error");
} }
if (ev->events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { if (ev->events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) {
A(0==pthread_mutex_lock(&ep->lock), ""); A(0 == pthread_mutex_lock(&ep->lock), "");
A(0==epoll_ctl(ep->ep, EPOLL_CTL_DEL, client->skt, NULL), ""); A(0 == epoll_ctl(ep->ep, EPOLL_CTL_DEL, client->skt, NULL), "");
A(0==pthread_mutex_unlock(&ep->lock), ""); A(0 == pthread_mutex_unlock(&ep->lock), "");
close(client->skt); close(client->skt);
client->skt = -1; client->skt = -1;
client->on_event = NULL; client->on_event = NULL;
free(client); free(client);
} }
} }
...@@ -7,6 +7,7 @@ LFLAGS = '-Wl,-rpath,/usr/local/taos/driver/' -ltaos -lpthread -lm -lrt ...@@ -7,6 +7,7 @@ LFLAGS = '-Wl,-rpath,/usr/local/taos/driver/' -ltaos -lpthread -lm -lrt
CFLAGS = -O3 -g -Wall -Wno-deprecated -fPIC -Wno-unused-result -Wconversion \ CFLAGS = -O3 -g -Wall -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-unused-function -D_M_X64 -I/usr/local/taos/include -std=gnu99
-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment
all: $(TARGET) all: $(TARGET)
...@@ -19,10 +20,9 @@ exe: ...@@ -19,10 +20,9 @@ exe:
gcc $(CFLAGS) ./apitest.c -o $(ROOT)apitest $(LFLAGS) gcc $(CFLAGS) ./apitest.c -o $(ROOT)apitest $(LFLAGS)
clean: clean:
rm $(ROOT)asyncdemo rm -f $(ROOT)asyncdemo
rm $(ROOT)demo rm -f $(ROOT)demo
rm $(ROOT)prepare rm -f $(ROOT)prepare
rm $(ROOT)batchprepare rm -f $(ROOT)stream
rm $(ROOT)stream rm -f $(ROOT)subscribe
rm $(ROOT)subscribe rm -f $(ROOT)apitest
rm $(ROOT)apitest
// TAOS standard API example. The same syntax as MySQL, but only a subet // TAOS standard API example. The same syntax as MySQL, but only a subet
// to compile: gcc -o prepare prepare.c -ltaos // to compile: gcc -o prepare prepare.c -ltaos
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "taos.h" #include <taos.h>
#include <unistd.h>
void taosMsleep(int mseconds); void taosMsleep(int mseconds);
int main(int argc, char *argv[]) void verify_prepare(TAOS* taos) {
{ TAOS_RES* result = taos_query(taos, "drop database if exists test;");
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", 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); taos_free_result(result);
result = taos_query(taos, "create database demo"); usleep(100000);
code = taos_errno(result); result = taos_query(taos, "create database test;");
int code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("failed to create database, reason:%s\n", taos_errstr(result)); printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result));
taos_free_result(result); taos_free_result(result);
exit(1); exit(EXIT_FAILURE);
} }
taos_free_result(result);
result = taos_query(taos, "use demo");
taos_free_result(result); taos_free_result(result);
usleep(100000);
taos_select_db(taos, "test");
// create table // 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, bin "
"binary(40), blob nchar(10), u1 tinyint unsigned, u2 smallint unsigned, u4 int unsigned, u8 bigint unsigned)";
result = taos_query(taos, sql); result = taos_query(taos, sql);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("failed to create table, reason:%s\n", taos_errstr(result)); printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result));
taos_free_result(result); taos_free_result(result);
exit(1); exit(EXIT_FAILURE);
} }
taos_free_result(result); taos_free_result(result);
// sleep for one second to make sure table is created on data node
// taosMsleep(1000);
// insert 10 records // insert 10 records
struct { struct {
int64_t ts; int64_t ts;
int8_t b; int8_t b;
int8_t v1; int8_t v1;
int16_t v2; int16_t v2;
int32_t v4; int32_t v4;
int64_t v8; int64_t v8;
float f4; float f4;
double f8; double f8;
char bin[40]; char bin[40];
char blob[80]; char blob[80];
uint8_t u1;
uint16_t u2;
uint32_t u4;
uint64_t u8;
} v = {0}; } v = {0};
stmt = taos_stmt_init(taos); TAOS_STMT* stmt = taos_stmt_init(taos);
TAOS_BIND params[10]; TAOS_BIND params[14];
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(v.ts); params[0].buffer_length = sizeof(v.ts);
params[0].buffer = &v.ts; params[0].buffer = &v.ts;
...@@ -134,12 +122,38 @@ int main(int argc, char *argv[]) ...@@ -134,12 +122,38 @@ int main(int argc, char *argv[])
params[9].length = &params[9].buffer_length; params[9].length = &params[9].buffer_length;
params[9].is_null = NULL; params[9].is_null = NULL;
params[10].buffer_type = TSDB_DATA_TYPE_UTINYINT;
params[10].buffer_length = sizeof(v.u1);
params[10].buffer = &v.u1;
params[10].length = &params[10].buffer_length;
params[10].is_null = NULL;
params[11].buffer_type = TSDB_DATA_TYPE_USMALLINT;
params[11].buffer_length = sizeof(v.u2);
params[11].buffer = &v.u2;
params[11].length = &params[11].buffer_length;
params[11].is_null = NULL;
params[12].buffer_type = TSDB_DATA_TYPE_UINT;
params[12].buffer_length = sizeof(v.u4);
params[12].buffer = &v.u4;
params[12].length = &params[12].buffer_length;
params[12].is_null = NULL;
params[13].buffer_type = TSDB_DATA_TYPE_UBIGINT;
params[13].buffer_length = sizeof(v.u8);
params[13].buffer = &v.u8;
params[13].length = &params[13].buffer_length;
params[13].is_null = NULL;
int is_null = 1; int is_null = 1;
sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?)"; sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
code = taos_stmt_prepare(stmt, sql, 0); code = taos_stmt_prepare(stmt, sql, 0);
if (code != 0){ if (code != 0) {
printf("failed to execute taos_stmt_prepare. code:0x%x\n", code); printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
exit(EXIT_FAILURE);
} }
v.ts = 1591060628000; v.ts = 1591060628000;
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
...@@ -154,16 +168,21 @@ int main(int argc, char *argv[]) ...@@ -154,16 +168,21 @@ int main(int argc, char *argv[])
v.v8 = (int64_t)(i * 8); v.v8 = (int64_t)(i * 8);
v.f4 = (float)(i * 40); v.f4 = (float)(i * 40);
v.f8 = (double)(i * 80); v.f8 = (double)(i * 80);
for (int j = 0; j < sizeof(v.bin) - 1; ++j) { for (int j = 0; j < sizeof(v.bin); ++j) {
v.bin[j] = (char)(i + '0'); v.bin[j] = (char)(i + '0');
} }
v.u1 = (uint8_t)i;
v.u2 = (uint16_t)(i * 2);
v.u4 = (uint32_t)(i * 4);
v.u8 = (uint64_t)(i * 8);
taos_stmt_bind_param(stmt, params); taos_stmt_bind_param(stmt, params);
taos_stmt_add_batch(stmt); taos_stmt_add_batch(stmt);
} }
if (taos_stmt_execute(stmt) != 0) { if (taos_stmt_execute(stmt) != 0) {
printf("failed to execute insert statement.\n"); printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
exit(1); taos_stmt_close(stmt);
exit(EXIT_FAILURE);
} }
taos_stmt_close(stmt); taos_stmt_close(stmt);
...@@ -174,8 +193,239 @@ int main(int argc, char *argv[]) ...@@ -174,8 +193,239 @@ int main(int argc, char *argv[])
v.v2 = 15; v.v2 = 15;
taos_stmt_bind_param(stmt, params + 2); taos_stmt_bind_param(stmt, params + 2);
if (taos_stmt_execute(stmt) != 0) { if (taos_stmt_execute(stmt) != 0) {
printf("failed to execute select statement.\n"); printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
exit(1); taos_stmt_close(stmt);
exit(EXIT_FAILURE);
}
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);
}
taos_free_result(result);
taos_stmt_close(stmt);
}
void verify_prepare2(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);
exit(EXIT_FAILURE);
}
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), u1 tinyint unsigned, u2 smallint unsigned, u4 int unsigned, u8 bigint unsigned)";
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);
exit(EXIT_FAILURE);
}
taos_free_result(result);
// 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];
uint8_t u1;
uint16_t u2;
uint32_t u4;
uint64_t u8;
} v = {0};
TAOS_STMT* stmt = taos_stmt_init(taos);
TAOS_BIND params[14];
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(v.ts);
params[0].buffer = &v.ts;
params[0].length = &params[0].buffer_length;
params[0].is_null = NULL;
params[1].buffer_type = TSDB_DATA_TYPE_BOOL;
params[1].buffer_length = sizeof(v.b);
params[1].buffer = &v.b;
params[1].length = &params[1].buffer_length;
params[1].is_null = NULL;
params[2].buffer_type = TSDB_DATA_TYPE_TINYINT;
params[2].buffer_length = sizeof(v.v1);
params[2].buffer = &v.v1;
params[2].length = &params[2].buffer_length;
params[2].is_null = NULL;
params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT;
params[3].buffer_length = sizeof(v.v2);
params[3].buffer = &v.v2;
params[3].length = &params[3].buffer_length;
params[3].is_null = NULL;
params[4].buffer_type = TSDB_DATA_TYPE_INT;
params[4].buffer_length = sizeof(v.v4);
params[4].buffer = &v.v4;
params[4].length = &params[4].buffer_length;
params[4].is_null = NULL;
params[5].buffer_type = TSDB_DATA_TYPE_BIGINT;
params[5].buffer_length = sizeof(v.v8);
params[5].buffer = &v.v8;
params[5].length = &params[5].buffer_length;
params[5].is_null = NULL;
params[6].buffer_type = TSDB_DATA_TYPE_FLOAT;
params[6].buffer_length = sizeof(v.f4);
params[6].buffer = &v.f4;
params[6].length = &params[6].buffer_length;
params[6].is_null = NULL;
params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE;
params[7].buffer_length = sizeof(v.f8);
params[7].buffer = &v.f8;
params[7].length = &params[7].buffer_length;
params[7].is_null = NULL;
params[8].buffer_type = TSDB_DATA_TYPE_BINARY;
params[8].buffer_length = sizeof(v.bin);
params[8].buffer = v.bin;
params[8].length = &params[8].buffer_length;
params[8].is_null = NULL;
strcpy(v.blob, "一二三四五六七八九十");
params[9].buffer_type = TSDB_DATA_TYPE_NCHAR;
params[9].buffer_length = strlen(v.blob);
params[9].buffer = v.blob;
params[9].length = &params[9].buffer_length;
params[9].is_null = NULL;
params[10].buffer_type = TSDB_DATA_TYPE_UTINYINT;
params[10].buffer_length = sizeof(v.u1);
params[10].buffer = &v.u1;
params[10].length = &params[10].buffer_length;
params[10].is_null = NULL;
params[11].buffer_type = TSDB_DATA_TYPE_USMALLINT;
params[11].buffer_length = sizeof(v.u2);
params[11].buffer = &v.u2;
params[11].length = &params[11].buffer_length;
params[11].is_null = NULL;
params[12].buffer_type = TSDB_DATA_TYPE_UINT;
params[12].buffer_length = sizeof(v.u4);
params[12].buffer = &v.u4;
params[12].length = &params[12].buffer_length;
params[12].is_null = NULL;
params[13].buffer_type = TSDB_DATA_TYPE_UBIGINT;
params[13].buffer_length = sizeof(v.u8);
params[13].buffer = &v.u8;
params[13].length = &params[13].buffer_length;
params[13].is_null = NULL;
sql = "insert into ? values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
code = taos_stmt_prepare(stmt, sql, 0);
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(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);
exit(EXIT_FAILURE);
}
int is_null = 1;
v.ts = 1591060628000;
for (int i = 0; i < 10; ++i) {
v.ts += 1;
for (int j = 1; j < 10; ++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');
}
v.u1 = (uint8_t)i;
v.u2 = (uint16_t)(i * 2);
v.u4 = (uint32_t)(i * 4);
v.u8 = (uint64_t)(i * 8);
taos_stmt_bind_param(stmt, params);
taos_stmt_add_batch(stmt);
}
if (taos_stmt_execute(stmt) != 0) {
printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
exit(EXIT_FAILURE);
}
taos_stmt_close(stmt);
// query the records
stmt = taos_stmt_init(taos);
taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0);
TAOS_BIND qparams[2];
int8_t v1 = 5;
int16_t v2 = 15;
qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT;
qparams[0].buffer_length = sizeof(v1);
qparams[0].buffer = &v1;
qparams[0].length = &qparams[0].buffer_length;
qparams[0].is_null = NULL;
qparams[1].buffer_type = TSDB_DATA_TYPE_SMALLINT;
qparams[1].buffer_length = sizeof(v2);
qparams[1].buffer = &v2;
qparams[1].length = &qparams[1].buffer_length;
qparams[1].is_null = NULL;
taos_stmt_bind_param(stmt, qparams);
if (taos_stmt_execute(stmt) != 0) {
printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
exit(EXIT_FAILURE);
} }
result = taos_stmt_use_result(stmt); result = taos_stmt_use_result(stmt);
...@@ -183,7 +433,7 @@ int main(int argc, char *argv[]) ...@@ -183,7 +433,7 @@ int main(int argc, char *argv[])
TAOS_ROW row; TAOS_ROW row;
int rows = 0; int rows = 0;
int num_fields = taos_num_fields(result); int num_fields = taos_num_fields(result);
TAOS_FIELD *fields = taos_fetch_fields(result); TAOS_FIELD* fields = taos_fetch_fields(result);
// fetch the records row by row // fetch the records row by row
while ((row = taos_fetch_row(result))) { while ((row = taos_fetch_row(result))) {
...@@ -192,15 +442,427 @@ int main(int argc, char *argv[]) ...@@ -192,15 +442,427 @@ int main(int argc, char *argv[])
taos_print_row(temp, row, fields, num_fields); taos_print_row(temp, row, fields, num_fields);
printf("%s\n", temp); printf("%s\n", temp);
} }
if (rows == 2) {
printf("two rows are fetched as expectation\n"); taos_free_result(result);
} else { taos_stmt_close(stmt);
printf("expect two rows, but %d rows are fetched\n", rows); }
void verify_prepare3(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);
exit(EXIT_FAILURE);
}
taos_free_result(result);
usleep(100000);
taos_select_db(taos, "test");
// create table
const char* sql =
"create stable st1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin "
"binary(40), blob nchar(10), u1 tinyint unsigned, u2 smallint unsigned, u4 int unsigned, u8 bigint unsigned) "
"tags "
"(b_tag bool, v1_tag tinyint, v2_tag smallint, v4_tag int, v8_tag bigint, f4_tag float, f8_tag double, bin_tag "
"binary(40), blob_tag nchar(10), u1_tag tinyint unsigned, u2_tag smallint unsigned, u4_tag int unsigned, u8_tag "
"bigint "
"unsigned)";
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);
exit(EXIT_FAILURE);
}
taos_free_result(result);
TAOS_BIND tags[13];
struct {
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];
uint8_t u1;
uint16_t u2;
uint32_t u4;
uint64_t u8;
} id = {0};
id.b = (int8_t)1;
id.v1 = (int8_t)1;
id.v2 = (int16_t)2;
id.v4 = (int32_t)4;
id.v8 = (int64_t)8;
id.f4 = (float)40;
id.f8 = (double)80;
for (int j = 0; j < sizeof(id.bin); ++j) {
id.bin[j] = (char)('1' + '0');
}
strcpy(id.blob, "一二三四五六七八九十");
id.u1 = (uint8_t)1;
id.u2 = (uint16_t)2;
id.u4 = (uint32_t)4;
id.u8 = (uint64_t)8;
tags[0].buffer_type = TSDB_DATA_TYPE_BOOL;
tags[0].buffer_length = sizeof(id.b);
tags[0].buffer = &id.b;
tags[0].length = &tags[0].buffer_length;
tags[0].is_null = NULL;
tags[1].buffer_type = TSDB_DATA_TYPE_TINYINT;
tags[1].buffer_length = sizeof(id.v1);
tags[1].buffer = &id.v1;
tags[1].length = &tags[1].buffer_length;
tags[1].is_null = NULL;
tags[2].buffer_type = TSDB_DATA_TYPE_SMALLINT;
tags[2].buffer_length = sizeof(id.v2);
tags[2].buffer = &id.v2;
tags[2].length = &tags[2].buffer_length;
tags[2].is_null = NULL;
tags[3].buffer_type = TSDB_DATA_TYPE_INT;
tags[3].buffer_length = sizeof(id.v4);
tags[3].buffer = &id.v4;
tags[3].length = &tags[3].buffer_length;
tags[3].is_null = NULL;
tags[4].buffer_type = TSDB_DATA_TYPE_BIGINT;
tags[4].buffer_length = sizeof(id.v8);
tags[4].buffer = &id.v8;
tags[4].length = &tags[4].buffer_length;
tags[4].is_null = NULL;
tags[5].buffer_type = TSDB_DATA_TYPE_FLOAT;
tags[5].buffer_length = sizeof(id.f4);
tags[5].buffer = &id.f4;
tags[5].length = &tags[5].buffer_length;
tags[5].is_null = NULL;
tags[6].buffer_type = TSDB_DATA_TYPE_DOUBLE;
tags[6].buffer_length = sizeof(id.f8);
tags[6].buffer = &id.f8;
tags[6].length = &tags[6].buffer_length;
tags[6].is_null = NULL;
tags[7].buffer_type = TSDB_DATA_TYPE_BINARY;
tags[7].buffer_length = sizeof(id.bin);
tags[7].buffer = &id.bin;
tags[7].length = &tags[7].buffer_length;
tags[7].is_null = NULL;
tags[8].buffer_type = TSDB_DATA_TYPE_NCHAR;
tags[8].buffer_length = strlen(id.blob);
tags[8].buffer = &id.blob;
tags[8].length = &tags[8].buffer_length;
tags[8].is_null = NULL;
tags[9].buffer_type = TSDB_DATA_TYPE_UTINYINT;
tags[9].buffer_length = sizeof(id.u1);
tags[9].buffer = &id.u1;
tags[9].length = &tags[9].buffer_length;
tags[9].is_null = NULL;
tags[10].buffer_type = TSDB_DATA_TYPE_USMALLINT;
tags[10].buffer_length = sizeof(id.u2);
tags[10].buffer = &id.u2;
tags[10].length = &tags[10].buffer_length;
tags[10].is_null = NULL;
tags[11].buffer_type = TSDB_DATA_TYPE_UINT;
tags[11].buffer_length = sizeof(id.u4);
tags[11].buffer = &id.u4;
tags[11].length = &tags[11].buffer_length;
tags[11].is_null = NULL;
tags[12].buffer_type = TSDB_DATA_TYPE_UBIGINT;
tags[12].buffer_length = sizeof(id.u8);
tags[12].buffer = &id.u8;
tags[12].length = &tags[12].buffer_length;
tags[12].is_null = NULL;
// 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];
uint8_t u1[10];
uint16_t u2[10];
uint32_t u4[10];
uint64_t u8[10];
} 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* u8_len = malloc(sizeof(int32_t) * 10);
int32_t* u16_len = malloc(sizeof(int32_t) * 10);
int32_t* u32_len = malloc(sizeof(int32_t) * 10);
int32_t* u64_len = malloc(sizeof(int32_t) * 10);
TAOS_STMT* stmt = taos_stmt_init(taos);
TAOS_MULTI_BIND params[14];
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[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;
params[10].buffer_type = TSDB_DATA_TYPE_UTINYINT;
params[10].buffer_length = sizeof(v.u1[0]);
params[10].buffer = v.u1;
params[10].length = u8_len;
params[10].is_null = is_null;
params[10].num = 10;
params[11].buffer_type = TSDB_DATA_TYPE_USMALLINT;
params[11].buffer_length = sizeof(v.u2[0]);
params[11].buffer = v.u2;
params[11].length = u16_len;
params[11].is_null = is_null;
params[11].num = 10;
params[12].buffer_type = TSDB_DATA_TYPE_UINT;
params[12].buffer_length = sizeof(v.u4[0]);
params[12].buffer = v.u4;
params[12].length = u32_len;
params[12].is_null = is_null;
params[12].num = 10;
params[13].buffer_type = TSDB_DATA_TYPE_UBIGINT;
params[13].buffer_length = sizeof(v.u8[0]);
params[13].buffer = v.u8;
params[13].length = u64_len;
params[13].is_null = is_null;
params[13].num = 10;
sql = "insert into ? using st1 tags(?,?,?,?,?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
code = taos_stmt_prepare(stmt, sql, 0);
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);
if (code != 0) {
printf("\033[31mfailed to execute taos_stmt_set_tbname_tags. error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
exit(EXIT_FAILURE);
}
int64_t ts = 1591060628000;
for (int i = 0; i < 10; ++i) {
v.ts[i] = ts++;
is_null[i] = 0;
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.blob[i], "一二三四五六七八九十");
v.u1[i] = (uint8_t)i;
v.u2[i] = (uint16_t)(i * 2);
v.u4[i] = (uint32_t)(i * 4);
v.u8[i] = (uint64_t)(i * 8);
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]);
u8_len[i] = sizeof(uint8_t);
u16_len[i] = sizeof(uint16_t);
u32_len[i] = sizeof(uint32_t);
u64_len[i] = sizeof(uint64_t);
}
taos_stmt_bind_param_batch(stmt, params);
taos_stmt_add_batch(stmt);
if (taos_stmt_execute(stmt) != 0) {
printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
exit(EXIT_FAILURE);
}
taos_stmt_close(stmt);
// query the records
stmt = taos_stmt_init(taos);
taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0);
TAOS_BIND qparams[2];
int8_t v1 = 5;
int16_t v2 = 15;
qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT;
qparams[0].buffer_length = sizeof(v1);
qparams[0].buffer = &v1;
qparams[0].length = &qparams[0].buffer_length;
qparams[0].is_null = NULL;
qparams[1].buffer_type = TSDB_DATA_TYPE_SMALLINT;
qparams[1].buffer_length = sizeof(v2);
qparams[1].buffer = &v2;
qparams[1].length = &qparams[1].buffer_length;
qparams[1].is_null = NULL;
taos_stmt_bind_param(stmt, qparams);
if (taos_stmt_execute(stmt) != 0) {
printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
exit(EXIT_FAILURE);
}
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);
} }
taos_free_result(result); taos_free_result(result);
taos_stmt_close(stmt); taos_stmt_close(stmt);
return 0; free(t8_len);
free(t16_len);
free(t32_len);
free(t64_len);
free(float_len);
free(double_len);
free(bin_len);
free(blob_len);
} }
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);
}
char* info = taos_get_server_info(taos);
printf("server info: %s\n", info);
info = taos_get_client_info(taos);
printf("client info: %s\n", info);
printf("************ verify prepare *************\n");
verify_prepare(taos);
printf("************ verify prepare2 *************\n");
verify_prepare2(taos);
printf("************ verify prepare3 *************\n");
verify_prepare3(taos);
printf("************ verify prepare4 *************\n");
exit(EXIT_SUCCESS);
}
#include "os.h"
#include "taos.h" #include "taos.h"
#include "taoserror.h" #include "taoserror.h"
#include "os.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -12,15 +12,12 @@ int numSuperTables = 8; ...@@ -12,15 +12,12 @@ int numSuperTables = 8;
int numChildTables = 4; int numChildTables = 4;
int numRowsPerChildTable = 2048; int numRowsPerChildTable = 2048;
void shuffle(char**lines, size_t n) void shuffle(char** lines, size_t n) {
{ if (n > 1) {
if (n > 1)
{
size_t i; size_t i;
for (i = 0; i < n - 1; i++) for (i = 0; i < n - 1; i++) {
{
size_t j = i + rand() / (RAND_MAX / (n - i) + 1); size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
char* t = lines[j]; char* t = lines[j];
lines[j] = lines[i]; lines[j] = lines[i];
lines[i] = t; lines[i] = t;
} }
...@@ -34,7 +31,7 @@ static int64_t getTimeInUs() { ...@@ -34,7 +31,7 @@ static int64_t getTimeInUs() {
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
TAOS_RES *result; TAOS_RES* result;
const char* host = "127.0.0.1"; const char* host = "127.0.0.1";
const char* user = "root"; const char* user = "root";
const char* passwd = "taosdata"; const char* passwd = "taosdata";
...@@ -59,12 +56,16 @@ int main(int argc, char* argv[]) { ...@@ -59,12 +56,16 @@ int main(int argc, char* argv[]) {
(void)taos_select_db(taos, "db"); (void)taos_select_db(taos, "db");
time_t ct = time(0); time_t ct = time(0);
int64_t ts = ct * 1000; int64_t ts = ct * 1000;
char* lineFormat = "sta%d,t0=true,t1=127i8,t2=32767i16,t3=%di32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" %lldms"; char* lineFormat =
"sta%d,t0=true,t1=127i8,t2=32767i16,t3=%di32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11="
"\"binaryTagValue\",t12=L\"ncharTagValue\" "
"c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,"
"c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" %lldms";
char** lines = calloc(numSuperTables * numChildTables * numRowsPerChildTable, sizeof(char*)); char** lines = calloc(numSuperTables * numChildTables * numRowsPerChildTable, sizeof(char*));
int l = 0; int l = 0;
for (int i = 0; i < numSuperTables; ++i) { for (int i = 0; i < numSuperTables; ++i) {
for (int j = 0; j < numChildTables; ++j) { for (int j = 0; j < numChildTables; ++j) {
for (int k = 0; k < numRowsPerChildTable; ++k) { for (int k = 0; k < numRowsPerChildTable; ++k) {
...@@ -78,121 +79,142 @@ int main(int argc, char* argv[]) { ...@@ -78,121 +79,142 @@ int main(int argc, char* argv[]) {
shuffle(lines, numSuperTables * numChildTables * numRowsPerChildTable); shuffle(lines, numSuperTables * numChildTables * numRowsPerChildTable);
printf("%s\n", "begin taos_insert_lines"); printf("%s\n", "begin taos_insert_lines");
int64_t begin = getTimeInUs(); int64_t begin = getTimeInUs();
int32_t code = taos_insert_lines(taos, lines, numSuperTables * numChildTables * numRowsPerChildTable); int32_t code = taos_insert_lines(taos, lines, numSuperTables * numChildTables * numRowsPerChildTable);
int64_t end = getTimeInUs(); int64_t end = getTimeInUs();
printf("code: %d, %s. time used: %"PRId64"\n", code, tstrerror(code), end-begin); printf("code: %d, %s. time used: %" PRId64 "\n", code, tstrerror(code), end - begin);
char* lines_000_0[] = { char* lines_000_0[] = {
"sta1,id=sta1_1,t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=255u8,t6=32770u16,t7=2147483699u32,t8=9223372036854775899u64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639000us" "sta1,id=sta1_1,t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=255u8,t6=32770u16,t7="
}; "2147483699u32,t8=9223372036854775899u64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12="
"L\"ncharTagValue\" "
code = taos_insert_lines(taos, lines_000_0 , sizeof(lines_000_0)/sizeof(char*)); "c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,"
"c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" "
"1626006833639000us"};
code = taos_insert_lines(taos, lines_000_0, sizeof(lines_000_0) / sizeof(char*));
if (0 == code) { if (0 == code) {
printf("taos_insert_lines() lines_000_0 should return error\n"); printf("taos_insert_lines() lines_000_0 should return error\n");
return -1; return -1;
} }
char* lines_000_1[] = { char* lines_000_1[] = {
"sta2,id=\"sta2_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=255u8,t6=32770u16,t7=2147483699u32,t8=9223372036854775899u64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639001" "sta2,id=\"sta2_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=255u8,t6=32770u16,"
}; "t7=2147483699u32,t8=9223372036854775899u64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12="
"L\"ncharTagValue\" "
code = taos_insert_lines(taos, lines_000_1 , sizeof(lines_000_1)/sizeof(char*)); "c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,"
"c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" "
"1626006833639001"};
code = taos_insert_lines(taos, lines_000_1, sizeof(lines_000_1) / sizeof(char*));
if (0 == code) { if (0 == code) {
printf("taos_insert_lines() lines_000_1 should return error\n"); printf("taos_insert_lines() lines_000_1 should return error\n");
return -1; return -1;
} }
char* lines_000_2[] = { char* lines_000_2[] = {
"sta3,id=\"sta3_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 0" "sta3,id=\"sta3_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10="
}; "22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" "
"c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,"
"c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 0"};
code = taos_insert_lines(taos, lines_000_2 , sizeof(lines_000_2)/sizeof(char*)); code = taos_insert_lines(taos, lines_000_2, sizeof(lines_000_2) / sizeof(char*));
if (0 != code) { if (0 != code) {
printf("taos_insert_lines() lines_000_2 return code:%d (%s)\n", code, (char*)tstrerror(code)); printf("taos_insert_lines() lines_000_2 return code:%d (%s)\n", code, (char*)tstrerror(code));
return -1; return -1;
} }
char* lines_001_0[] = { char* lines_001_0[] = {
"sta4,t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639000us", "sta4,t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,"
"t11=\"binaryTagValue\",t12=L\"ncharTagValue\" "
"c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11="
"\"binaryValue\",c12=L\"ncharValue\" 1626006833639000us",
}; };
code = taos_insert_lines(taos, lines_001_0 , sizeof(lines_001_0)/sizeof(char*)); code = taos_insert_lines(taos, lines_001_0, sizeof(lines_001_0) / sizeof(char*));
if (0 != code) { if (0 != code) {
printf("taos_insert_lines() lines_001_0 return code:%d (%s)\n", code, (char*)tstrerror(code)); printf("taos_insert_lines() lines_001_0 return code:%d (%s)\n", code, (char*)tstrerror(code));
return -1; return -1;
} }
char* lines_001_1[] = { char* lines_001_1[] = {
"sta5,id=\"sta5_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639001" "sta5,id=\"sta5_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10="
}; "22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" "
"c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11="
"\"binaryValue\",c12=L\"ncharValue\" 1626006833639001"};
code = taos_insert_lines(taos, lines_001_1 , sizeof(lines_001_1)/sizeof(char*)); code = taos_insert_lines(taos, lines_001_1, sizeof(lines_001_1) / sizeof(char*));
if (0 != code) { if (0 != code) {
printf("taos_insert_lines() lines_001_1 return code:%d (%s)\n", code, (char*)tstrerror(code)); printf("taos_insert_lines() lines_001_1 return code:%d (%s)\n", code, (char*)tstrerror(code));
return -1; return -1;
} }
char* lines_001_2[] = { char* lines_001_2[] = {
"sta6,id=\"sta6_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 0" "sta6,id=\"sta6_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10="
}; "22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" "
"c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11="
"\"binaryValue\",c12=L\"ncharValue\" 0"};
code = taos_insert_lines(taos, lines_001_2 , sizeof(lines_001_2)/sizeof(char*)); code = taos_insert_lines(taos, lines_001_2, sizeof(lines_001_2) / sizeof(char*));
if (0 != code) { if (0 != code) {
printf("taos_insert_lines() lines_001_2 return code:%d (%s)\n", code, (char*)tstrerror(code)); printf("taos_insert_lines() lines_001_2 return code:%d (%s)\n", code, (char*)tstrerror(code));
return -1; return -1;
} }
char* lines_002[] = { char* lines_002[] = {
"stb,id=\"stb_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639000000ns", "stb,id=\"stb_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,"
"stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639019us", "t11=\"binaryTagValue\",t12=L\"ncharTagValue\" "
"stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833640ms", "c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11="
"stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006834s" "\"binaryValue\",c12=L\"ncharValue\" 1626006833639000000ns",
}; "stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,"
"t11=\"binaryTagValue\",t12=L\"ncharTagValue\" "
code = taos_insert_lines(taos, lines_002 , sizeof(lines_002)/sizeof(char*)); "c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11="
"\"binaryValue\",c12=L\"ncharValue\" 1626006833639019us",
"stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,"
"t11=\"binaryTagValue\",t12=L\"ncharTagValue\" "
"c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11="
"\"binaryValue\",c12=L\"ncharValue\" 1626006833640ms",
"stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,"
"t11=\"binaryTagValue\",t12=L\"ncharTagValue\" "
"c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11="
"\"binaryValue\",c12=L\"ncharValue\" 1626006834s"};
code = taos_insert_lines(taos, lines_002, sizeof(lines_002) / sizeof(char*));
if (0 != code) { if (0 != code) {
printf("taos_insert_lines() lines_002 return code:%d (%s)\n", code, (char*)tstrerror(code)); printf("taos_insert_lines() lines_002 return code:%d (%s)\n", code, (char*)tstrerror(code));
return -1; return -1;
} }
//Duplicate key check; // Duplicate key check;
char* lines_003_1[] = { char* lines_003_1[] = {"std,id=\"std_3_1\",t1=4i64,Id=\"std\",t2=true c1=true 1626006834s"};
"std,id=\"std_3_1\",t1=4i64,Id=\"std\",t2=true c1=true 1626006834s"
};
code = taos_insert_lines(taos, lines_003_1 , sizeof(lines_003_1)/sizeof(char*)); code = taos_insert_lines(taos, lines_003_1, sizeof(lines_003_1) / sizeof(char*));
if (0 == code) { if (0 == code) {
printf("taos_insert_lines() lines_003_1 return code:%d (%s)\n", code, (char*)tstrerror(code)); printf("taos_insert_lines() lines_003_1 return code:%d (%s)\n", code, (char*)tstrerror(code));
return -1; return -1;
} }
char* lines_003_2[] = { char* lines_003_2[] = {"std,id=\"std_3_2\",tag1=4i64,Tag2=true,tAg3=2,TaG2=\"dup!\" c1=true 1626006834s"};
"std,id=\"std_3_2\",tag1=4i64,Tag2=true,tAg3=2,TaG2=\"dup!\" c1=true 1626006834s"
};
code = taos_insert_lines(taos, lines_003_2 , sizeof(lines_003_2)/sizeof(char*)); code = taos_insert_lines(taos, lines_003_2, sizeof(lines_003_2) / sizeof(char*));
if (0 == code) { if (0 == code) {
printf("taos_insert_lines() lines_003_2 return code:%d (%s)\n", code, (char*)tstrerror(code)); printf("taos_insert_lines() lines_003_2 return code:%d (%s)\n", code, (char*)tstrerror(code));
return -1; return -1;
} }
char* lines_003_3[] = { char* lines_003_3[] = {"std,id=\"std_3_3\",tag1=4i64 field1=true,Field2=2,FIElD1=\"dup!\",fIeLd4=true 1626006834s"};
"std,id=\"std_3_3\",tag1=4i64 field1=true,Field2=2,FIElD1=\"dup!\",fIeLd4=true 1626006834s"
};
code = taos_insert_lines(taos, lines_003_3 , sizeof(lines_003_3)/sizeof(char*)); code = taos_insert_lines(taos, lines_003_3, sizeof(lines_003_3) / sizeof(char*));
if (0 == code) { if (0 == code) {
printf("taos_insert_lines() lines_003_3 return code:%d (%s)\n", code, (char*)tstrerror(code)); printf("taos_insert_lines() lines_003_3 return code:%d (%s)\n", code, (char*)tstrerror(code));
return -1; return -1;
} }
char* lines_003_4[] = { char* lines_003_4[] = {
"std,id=\"std_3_4\",tag1=4i64,dupkey=4i16,tag2=T field1=true,dUpkEy=1e3f32,field2=\"1234\" 1626006834s" "std,id=\"std_3_4\",tag1=4i64,dupkey=4i16,tag2=T field1=true,dUpkEy=1e3f32,field2=\"1234\" 1626006834s"};
};
code = taos_insert_lines(taos, lines_003_4 , sizeof(lines_003_4)/sizeof(char*)); code = taos_insert_lines(taos, lines_003_4, sizeof(lines_003_4) / sizeof(char*));
if (0 == code) { if (0 == code) {
printf("taos_insert_lines() lines_003_4 return code:%d (%s)\n", code, (char*)tstrerror(code)); printf("taos_insert_lines() lines_003_4 return code:%d (%s)\n", code, (char*)tstrerror(code));
return -1; return -1;
......
...@@ -13,24 +13,23 @@ ...@@ -13,24 +13,23 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <taos.h> // include TDengine header file #include <taos.h> // include TDengine header file
#include <unistd.h>
typedef struct { typedef struct {
char server_ip[64]; char server_ip[64];
char db_name[64]; char db_name[64];
char tbl_name[64]; char tbl_name[64];
} param; } param;
int g_thread_exit_flag = 0; int g_thread_exit_flag = 0;
void* insert_rows(void *sarg); void *insert_rows(void *sarg);
void streamCallBack(void *param, TAOS_RES *res, TAOS_ROW row) void streamCallBack(void *param, TAOS_RES *res, TAOS_ROW row) {
{
// in this simple demo, it just print out the result // in this simple demo, it just print out the result
char temp[128]; char temp[128];
...@@ -42,85 +41,81 @@ void streamCallBack(void *param, TAOS_RES *res, TAOS_ROW row) ...@@ -42,85 +41,81 @@ void streamCallBack(void *param, TAOS_RES *res, TAOS_ROW row)
printf("\n%s\n", temp); printf("\n%s\n", temp);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{ TAOS *taos;
TAOS *taos; char db_name[64];
char db_name[64]; char tbl_name[64];
char tbl_name[64]; char sql[1024] = {0};
char sql[1024] = { 0 };
if (argc != 4) { if (argc != 4) {
printf("usage: %s server-ip dbname tblname\n", argv[0]); printf("usage: %s server-ip dbname tblname\n", argv[0]);
exit(0); exit(0);
} }
strcpy(db_name, argv[2]); strcpy(db_name, argv[2]);
strcpy(tbl_name, argv[3]); strcpy(tbl_name, argv[3]);
// create pthread to insert into row per second for stream calc // create pthread to insert into row per second for stream calc
param *t_param = (param *)malloc(sizeof(param)); param *t_param = (param *)malloc(sizeof(param));
if (NULL == t_param) if (NULL == t_param) {
{
printf("failed to malloc\n"); printf("failed to malloc\n");
exit(1); exit(1);
} }
memset(t_param, 0, sizeof(param)); memset(t_param, 0, sizeof(param));
strcpy(t_param->server_ip, argv[1]); strcpy(t_param->server_ip, argv[1]);
strcpy(t_param->db_name, db_name); strcpy(t_param->db_name, db_name);
strcpy(t_param->tbl_name, tbl_name); strcpy(t_param->tbl_name, tbl_name);
pthread_t pid; pthread_t pid;
pthread_create(&pid, NULL, (void * (*)(void *))insert_rows, t_param); pthread_create(&pid, NULL, (void *(*)(void *))insert_rows, t_param);
sleep(3); // waiting for database is created. sleep(3); // waiting for database is created.
// open connection to database // open connection to database
taos = taos_connect(argv[1], "root", "taosdata", db_name, 0); taos = taos_connect(argv[1], "root", "taosdata", db_name, 0);
if (taos == NULL) { if (taos == NULL) {
printf("failed to connet to server:%s\n", argv[1]); printf("failed to connet to server:%s\n", argv[1]);
free(t_param); free(t_param);
exit(1); exit(1);
} }
// starting stream calc, // starting stream calc,
printf("please input stream SQL:[e.g., select count(*) from tblname interval(5s) sliding(2s);]\n"); printf("please input stream SQL:[e.g., select count(*) from tblname interval(5s) sliding(2s);]\n");
fgets(sql, sizeof(sql), stdin); fgets(sql, sizeof(sql), stdin);
if (sql[0] == 0) { if (sql[0] == 0) {
printf("input NULL stream SQL, so exit!\n"); printf("input NULL stream SQL, so exit!\n");
free(t_param); free(t_param);
exit(1); exit(1);
} }
// param is set to NULL in this demo, it shall be set to the pointer to app context // param is set to NULL in this demo, it shall be set to the pointer to app context
TAOS_STREAM *pStream = taos_open_stream(taos, sql, streamCallBack, 0, NULL, NULL); TAOS_STREAM *pStream = taos_open_stream(taos, sql, streamCallBack, 0, NULL, NULL);
if (NULL == pStream) { if (NULL == pStream) {
printf("failed to create stream\n"); printf("failed to create stream\n");
free(t_param); free(t_param);
exit(1); exit(1);
} }
printf("presss any key to exit\n"); printf("presss any key to exit\n");
getchar(); getchar();
taos_close_stream(pStream); taos_close_stream(pStream);
g_thread_exit_flag = 1; g_thread_exit_flag = 1;
pthread_join(pid, NULL); pthread_join(pid, NULL);
taos_close(taos); taos_close(taos);
free(t_param); free(t_param);
return 0; return 0;
} }
void *insert_rows(void *sarg) {
TAOS * taos;
char command[1024] = {0};
param *winfo = (param *)sarg;
void* insert_rows(void *sarg) if (NULL == winfo) {
{ printf("para is null!\n");
TAOS *taos;
char command[1024] = { 0 };
param *winfo = (param * )sarg;
if (NULL == winfo){
printf("para is null!\n");
exit(1); exit(1);
} }
...@@ -129,7 +124,7 @@ void* insert_rows(void *sarg) ...@@ -129,7 +124,7 @@ void* insert_rows(void *sarg)
printf("failed to connet to server:%s\n", winfo->server_ip); printf("failed to connet to server:%s\n", winfo->server_ip);
exit(1); exit(1);
} }
// drop database // drop database
sprintf(command, "drop database %s;", winfo->db_name); sprintf(command, "drop database %s;", winfo->db_name);
if (taos_query(taos, command) != 0) { if (taos_query(taos, command) != 0) {
...@@ -160,19 +155,18 @@ void* insert_rows(void *sarg) ...@@ -160,19 +155,18 @@ void* insert_rows(void *sarg)
// insert data // insert data
int64_t begin = (int64_t)time(NULL); int64_t begin = (int64_t)time(NULL);
int index = 0; int index = 0;
while (1) { while (1) {
if (g_thread_exit_flag) break; if (g_thread_exit_flag) break;
index++; index++;
sprintf(command, "insert into %s values (%ld, %d)", winfo->tbl_name, (begin + index) * 1000, index); sprintf(command, "insert into %s values (%ld, %d)", winfo->tbl_name, (begin + index) * 1000, index);
if (taos_query(taos, command)) { if (taos_query(taos, command)) {
printf("failed to insert row [%s], reason:%s\n", command, taos_errstr(taos)); printf("failed to insert row [%s], reason:%s\n", command, taos_errstr(taos));
} }
sleep(1); sleep(1);
} }
taos_close(taos); taos_close(taos);
return 0; return 0;
} }
...@@ -14,10 +14,10 @@ void print_result(TAOS_RES* res, int blockFetch) { ...@@ -14,10 +14,10 @@ void print_result(TAOS_RES* res, int blockFetch) {
int num_fields = taos_num_fields(res); int num_fields = taos_num_fields(res);
TAOS_FIELD* fields = taos_fetch_fields(res); TAOS_FIELD* fields = taos_fetch_fields(res);
int nRows = 0; int nRows = 0;
if (blockFetch) { if (blockFetch) {
nRows = taos_fetch_block(res, &row); nRows = taos_fetch_block(res, &row);
//for (int i = 0; i < nRows; i++) { // for (int i = 0; i < nRows; i++) {
// taos_print_row(buf, row + i, fields, num_fields); // taos_print_row(buf, row + i, fields, num_fields);
// puts(buf); // puts(buf);
//} //}
...@@ -34,15 +34,11 @@ void print_result(TAOS_RES* res, int blockFetch) { ...@@ -34,15 +34,11 @@ void print_result(TAOS_RES* res, int blockFetch) {
printf("%d rows consumed.\n", nRows); printf("%d rows consumed.\n", nRows);
} }
void subscribe_callback(TAOS_SUB* tsub, TAOS_RES* res, void* param, int code) { print_result(res, *(int*)param); }
void subscribe_callback(TAOS_SUB* tsub, TAOS_RES *res, void* param, int code) {
print_result(res, *(int*)param);
}
void check_row_count(int line, TAOS_RES* res, int expected) { void check_row_count(int line, TAOS_RES* res, int expected) {
int actual = 0; int actual = 0;
TAOS_ROW row; TAOS_ROW row;
while ((row = taos_fetch_row(res))) { while ((row = taos_fetch_row(res))) {
actual++; actual++;
} }
...@@ -53,16 +49,14 @@ void check_row_count(int line, TAOS_RES* res, int expected) { ...@@ -53,16 +49,14 @@ void check_row_count(int line, TAOS_RES* res, int expected) {
} }
} }
void do_query(TAOS* taos, const char* sql) { void do_query(TAOS* taos, const char* sql) {
TAOS_RES* res = taos_query(taos, sql); TAOS_RES* res = taos_query(taos, sql);
taos_free_result(res); taos_free_result(res);
} }
void run_test(TAOS* taos) { void run_test(TAOS* taos) {
do_query(taos, "drop database if exists test;"); do_query(taos, "drop database if exists test;");
usleep(100000); usleep(100000);
do_query(taos, "create database test;"); do_query(taos, "create database test;");
usleep(100000); usleep(100000);
...@@ -161,14 +155,13 @@ void run_test(TAOS* taos) { ...@@ -161,14 +155,13 @@ void run_test(TAOS* taos) {
taos_unsubscribe(tsub, 0); taos_unsubscribe(tsub, 0);
} }
int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {
const char* host = "127.0.0.1"; const char* host = "127.0.0.1";
const char* user = "root"; const char* user = "root";
const char* passwd = "taosdata"; const char* passwd = "taosdata";
const char* sql = "select * from meters;"; const char* sql = "select * from meters;";
const char* topic = "test-multiple"; const char* topic = "test-multiple";
int async = 1, restart = 0, keep = 1, test = 0, blockFetch = 0; int async = 1, restart = 0, keep = 1, test = 0, blockFetch = 0;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (strncmp(argv[i], "-h=", 3) == 0) { if (strncmp(argv[i], "-h=", 3) == 0) {
...@@ -240,20 +233,21 @@ int main(int argc, char *argv[]) { ...@@ -240,20 +233,21 @@ int main(int argc, char *argv[]) {
if (tsub == NULL) { if (tsub == NULL) {
printf("failed to create subscription.\n"); printf("failed to create subscription.\n");
exit(0); exit(0);
} }
if (async) { if (async) {
getchar(); getchar();
} else while(1) { } else
TAOS_RES* res = taos_consume(tsub); while (1) {
if (res == NULL) { TAOS_RES* res = taos_consume(tsub);
printf("failed to consume data."); if (res == NULL) {
break; printf("failed to consume data.");
} else { break;
print_result(res, blockFetch); } else {
getchar(); print_result(res, blockFetch);
getchar();
}
} }
}
printf("total rows consumed: %d\n", nTotalRows); printf("total rows consumed: %d\n", nTotalRows);
taos_unsubscribe(tsub, keep); taos_unsubscribe(tsub, keep);
......
...@@ -7,7 +7,7 @@ LFLAGS = '-Wl,-rpath,/usr/local/taos/driver/' -ltaos -lpthread -lm -lrt ...@@ -7,7 +7,7 @@ LFLAGS = '-Wl,-rpath,/usr/local/taos/driver/' -ltaos -lpthread -lm -lrt
CFLAGS = -O0 -g -Wall -Wno-deprecated -fPIC -Wno-unused-result -Wconversion \ CFLAGS = -O0 -g -Wall -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-unused-function -D_M_X64 -I/usr/local/taos/include -std=gnu99 \
-fsanitize=address -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment
all: $(TARGET) all: $(TARGET)
...@@ -15,10 +15,10 @@ exe: ...@@ -15,10 +15,10 @@ exe:
gcc $(CFLAGS) ./batchprepare.c -o $(ROOT)batchprepare $(LFLAGS) gcc $(CFLAGS) ./batchprepare.c -o $(ROOT)batchprepare $(LFLAGS)
gcc $(CFLAGS) ./stmtBatchTest.c -o $(ROOT)stmtBatchTest $(LFLAGS) gcc $(CFLAGS) ./stmtBatchTest.c -o $(ROOT)stmtBatchTest $(LFLAGS)
gcc $(CFLAGS) ./stmtTest.c -o $(ROOT)stmtTest $(LFLAGS) gcc $(CFLAGS) ./stmtTest.c -o $(ROOT)stmtTest $(LFLAGS)
gcc $(CFLAGS) ./stmt_function.c -o $(ROOT)stmt_function $(LFLAGS) gcc $(CFLAGS) ./stmt.c -o $(ROOT)stmt $(LFLAGS)
clean: clean:
rm $(ROOT)batchprepare rm $(ROOT)batchprepare
rm $(ROOT)stmtBatchTest rm $(ROOT)stmtBatchTest
rm $(ROOT)stmtTest rm $(ROOT)stmtTest
rm $(ROOT)stmt_function rm $(ROOT)stmt
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include "taos.h"
void execute_simple_sql(void *taos, char *sql) {
TAOS_RES *result = taos_query(taos, sql);
if (result == NULL || taos_errno(result) != 0) {
printf("failed to %s, Reason: %s\n", sql, taos_errstr(result));
taos_free_result(result);
exit(EXIT_FAILURE);
}
taos_free_result(result);
}
void print_result(TAOS_RES *res) {
if (res == NULL) {
exit(EXIT_FAILURE);
}
TAOS_ROW row = NULL;
int num_fields = taos_num_fields(res);
TAOS_FIELD *fields = taos_fetch_fields(res);
while ((row = taos_fetch_row(res))) {
char temp[256] = {0};
taos_print_row(temp, row, fields, num_fields);
printf("get result: %s\n", temp);
}
}
void taos_stmt_init_test() {
printf("start taos_stmt_init test \n");
void * taos = NULL;
TAOS_STMT *stmt = NULL;
stmt = taos_stmt_init(taos);
assert(stmt == NULL);
// ASM ERROR
assert(taos_stmt_close(stmt) != 0);
taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
assert(taos_stmt_close(stmt) == 0);
printf("finish taos_stmt_init test\n");
}
void taos_stmt_preprare_test() {
printf("start taos_stmt_prepare test\n");
char * stmt_sql = calloc(1, 1048576);
TAOS_STMT *stmt = NULL;
assert(taos_stmt_prepare(stmt, stmt_sql, 0) != 0);
void *taos = NULL;
taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos,
"create table super(ts timestamp, c1 int, c2 bigint, c3 float, c4 double, c5 binary(8), c6 "
"smallint, c7 tinyint, c8 bool, c9 nchar(8), c10 timestamp) tags (t1 int, t2 bigint, t3 float, t4 "
"double, t5 binary(8), t6 smallint, t7 tinyint, t8 bool, t9 nchar(8))");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
// below will make client dead lock
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_close(stmt) == 0);
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
sprintf(stmt_sql, "select from ?");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_close(stmt) == 0);
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
sprintf(stmt_sql, "insert into ? values (?,?,?,?,?,?,?,?,?,?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_close(stmt) == 0);
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
sprintf(stmt_sql, "insert into super values (?,?,?,?,?,?,?,?,?,?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) != 0);
assert(taos_stmt_close(stmt) == 0);
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
sprintf(stmt_sql, "insert into ? values (?,?,?,?,?,?,?,?,1,?,?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_close(stmt) == 0);
free(stmt_sql);
printf("finish taos_stmt_prepare test\n");
}
void taos_stmt_set_tbname_test() {
printf("start taos_stmt_set_tbname test\n");
TAOS_STMT *stmt = NULL;
char * name = calloc(1, 200);
// ASM ERROR
assert(taos_stmt_set_tbname(stmt, name) != 0);
void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create table super(ts timestamp, c1 int)");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
assert(taos_stmt_set_tbname(stmt, name) != 0);
char *stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
sprintf(name, "super");
assert(stmt != NULL);
assert(taos_stmt_set_tbname(stmt, name) == 0);
free(name);
free(stmt_sql);
taos_stmt_close(stmt);
printf("finish taos_stmt_set_tbname test\n");
}
void taos_stmt_set_tbname_tags_test() {
printf("start taos_stmt_set_tbname_tags test\n");
TAOS_STMT *stmt = NULL;
char * name = calloc(1, 20);
TAOS_BIND *tags = calloc(1, sizeof(TAOS_BIND));
// ASM ERROR
assert(taos_stmt_set_tbname_tags(stmt, name, tags) != 0);
void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create stable super(ts timestamp, c1 int) tags (id int)");
execute_simple_sql(taos, "create table tb using super tags (1)");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
char *stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? using super tags (?) values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_set_tbname_tags(stmt, name, tags) != 0);
sprintf(name, "tb");
assert(taos_stmt_set_tbname_tags(stmt, name, tags) != 0);
int t = 1;
tags->buffer_length = TSDB_DATA_TYPE_INT;
tags->buffer_length = sizeof(uint32_t);
tags->buffer = &t;
tags->length = &tags->buffer_length;
tags->is_null = NULL;
assert(taos_stmt_set_tbname_tags(stmt, name, tags) == 0);
free(stmt_sql);
free(name);
free(tags);
taos_stmt_close(stmt);
printf("finish taos_stmt_set_tbname_tags test\n");
}
void taos_stmt_set_sub_tbname_test() {
printf("start taos_stmt_set_sub_tbname test\n");
TAOS_STMT *stmt = NULL;
char * name = calloc(1, 200);
// ASM ERROR
assert(taos_stmt_set_sub_tbname(stmt, name) != 0);
void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create stable super(ts timestamp, c1 int) tags (id int)");
execute_simple_sql(taos, "create table tb using super tags (1)");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
char *stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_set_sub_tbname(stmt, name) != 0);
sprintf(name, "tb");
assert(taos_stmt_set_sub_tbname(stmt, name) == 0);
assert(taos_load_table_info(taos, "super, tb") == 0);
assert(taos_stmt_set_sub_tbname(stmt, name) == 0);
free(name);
free(stmt_sql);
assert(taos_stmt_close(stmt) == 0);
printf("finish taos_stmt_set_sub_tbname test\n");
}
void taos_stmt_bind_param_test() {
printf("start taos_stmt_bind_param test\n");
TAOS_STMT *stmt = NULL;
TAOS_BIND *binds = NULL;
assert(taos_stmt_bind_param(stmt, binds) != 0);
void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create table super(ts timestamp, c1 int)");
stmt = taos_stmt_init(taos);
char *stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_bind_param(stmt, binds) != 0);
free(binds);
TAOS_BIND *params = calloc(2, sizeof(TAOS_BIND));
int64_t ts = (int64_t)1591060628000;
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(uint64_t);
params[0].buffer = &ts;
params[0].length = &params[0].buffer_length;
params[0].is_null = NULL;
int32_t i = (int32_t)21474;
params[1].buffer_type = TSDB_DATA_TYPE_INT;
params[1].buffer_length = sizeof(int32_t);
params[1].buffer = &i;
params[1].length = &params[1].buffer_length;
params[1].is_null = NULL;
assert(taos_stmt_bind_param(stmt, params) != 0);
assert(taos_stmt_set_tbname(stmt, "super") == 0);
assert(taos_stmt_bind_param(stmt, params) == 0);
free(params);
free(stmt_sql);
taos_stmt_close(stmt);
printf("finish taos_stmt_bind_param test\n");
}
void taos_stmt_bind_single_param_batch_test() {
printf("start taos_stmt_bind_single_param_batch test\n");
TAOS_STMT * stmt = NULL;
TAOS_MULTI_BIND *bind = NULL;
assert(taos_stmt_bind_single_param_batch(stmt, bind, 0) != 0);
printf("finish taos_stmt_bind_single_param_batch test\n");
}
void taos_stmt_bind_param_batch_test() {
printf("start taos_stmt_bind_param_batch test\n");
TAOS_STMT * stmt = NULL;
TAOS_MULTI_BIND *bind = NULL;
assert(taos_stmt_bind_param_batch(stmt, bind) != 0);
printf("finish taos_stmt_bind_param_batch test\n");
}
void taos_stmt_add_batch_test() {
printf("start taos_stmt_add_batch test\n");
TAOS_STMT *stmt = NULL;
assert(taos_stmt_add_batch(stmt) != 0);
void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create table super(ts timestamp, c1 int)");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
char *stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_add_batch(stmt) != 0);
TAOS_BIND *params = calloc(2, sizeof(TAOS_BIND));
int64_t ts = (int64_t)1591060628000;
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(uint64_t);
params[0].buffer = &ts;
params[0].length = &params[0].buffer_length;
params[0].is_null = NULL;
int32_t i = (int32_t)21474;
params[1].buffer_type = TSDB_DATA_TYPE_INT;
params[1].buffer_length = sizeof(int32_t);
params[1].buffer = &i;
params[1].length = &params[1].buffer_length;
params[1].is_null = NULL;
assert(taos_stmt_set_tbname(stmt, "super") == 0);
assert(taos_stmt_bind_param(stmt, params) == 0);
assert(taos_stmt_add_batch(stmt) == 0);
free(params);
free(stmt_sql);
assert(taos_stmt_close(stmt) == 0);
printf("finish taos_stmt_add_batch test\n");
}
void taos_stmt_execute_test() {
printf("start taos_stmt_execute test\n");
TAOS_STMT *stmt = NULL;
assert(taos_stmt_execute(stmt) != 0);
void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create table super(ts timestamp, c1 int)");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
assert(taos_stmt_execute(stmt) != 0);
char *stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_execute(stmt) != 0);
TAOS_BIND *params = calloc(2, sizeof(TAOS_BIND));
int64_t ts = (int64_t)1591060628000;
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(uint64_t);
params[0].buffer = &ts;
params[0].length = &params[0].buffer_length;
params[0].is_null = NULL;
int32_t i = (int32_t)21474;
params[1].buffer_type = TSDB_DATA_TYPE_INT;
params[1].buffer_length = sizeof(int32_t);
params[1].buffer = &i;
params[1].length = &params[1].buffer_length;
params[1].is_null = NULL;
assert(taos_stmt_set_tbname(stmt, "super") == 0);
assert(taos_stmt_execute(stmt) != 0);
assert(taos_stmt_bind_param(stmt, params) == 0);
assert(taos_stmt_execute(stmt) != 0);
assert(taos_stmt_add_batch(stmt) == 0);
assert(taos_stmt_execute(stmt) == 0);
free(params);
free(stmt_sql);
assert(taos_stmt_close(stmt) == 0);
printf("finish taos_stmt_execute test\n");
}
void taos_stmt_use_result_query(void *taos, char *col, int type) {
TAOS_STMT *stmt = taos_stmt_init(taos);
assert(stmt != NULL);
char *stmt_sql = calloc(1, 1024);
struct {
int64_t long_value;
int64_t ts_value;
uint64_t ulong_value;
int32_t int_value;
uint32_t uint_value;
int16_t small_value;
uint16_t usmall_value;
int8_t tiny_value;
uint8_t utiny_value;
float float_value;
double double_value;
char binary_value[10];
char nchar_value[32];
} v = {0};
v.ts_value = (int64_t)1591060628000;
v.long_value = (int64_t)1;
v.int_value = (int32_t)1;
v.small_value = (int16_t)1;
v.tiny_value = (int8_t)1;
v.ulong_value = (uint64_t)1;
v.uint_value = (uint32_t)1;
v.usmall_value = (uint16_t)1;
v.utiny_value = (uint8_t)1;
v.float_value = (float)1;
v.double_value = (double)1;
strcpy(v.binary_value, "abcdefgh");
strcpy(v.nchar_value, "一二三四五六七八");
uintptr_t nchar_value_len = strlen(v.nchar_value);
sprintf(stmt_sql, "select * from stmt_test.t1 where %s = ?", col);
printf("stmt_sql: %s\n", stmt_sql);
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
TAOS_BIND *params = calloc(1, sizeof(TAOS_BIND));
params->buffer_type = type;
params->is_null = NULL;
switch (type) {
case TSDB_DATA_TYPE_TIMESTAMP:
params->buffer_length = sizeof(v.ts_value);
params->buffer = &v.ts_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_INT:
params->buffer_length = sizeof(v.int_value);
params->buffer = &v.int_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_BIGINT:
params->buffer_length = sizeof(v.long_value);
params->buffer = &v.long_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_FLOAT:
params->buffer_length = sizeof(v.float_value);
params->buffer = &v.float_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_DOUBLE:
params->buffer_length = sizeof(v.double_value);
params->buffer = &v.double_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_BINARY:
params->buffer_length = sizeof(v.binary_value);
params->buffer = &v.binary_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_SMALLINT:
params->buffer_length = sizeof(v.small_value);
params->buffer = &v.small_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_TINYINT:
params->buffer_length = sizeof(v.tiny_value);
params->buffer = &v.tiny_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_BOOL:
params->buffer_length = sizeof(v.tiny_value);
params->buffer = &v.tiny_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_NCHAR:
params->buffer_length = sizeof(v.nchar_value);
params->buffer = &v.nchar_value;
params->length = &nchar_value_len;
break;
case TSDB_DATA_TYPE_UINT:
params->buffer_length = sizeof(v.uint_value);
params->buffer = &v.uint_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_UBIGINT:
params->buffer_length = sizeof(v.ulong_value);
params->buffer = &v.ulong_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_USMALLINT:
params->buffer_length = sizeof(v.usmall_value);
params->buffer = &v.usmall_value;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_UTINYINT:
params->buffer_length = sizeof(v.utiny_value);
params->buffer = &v.utiny_value;
params->length = &params->buffer_length;
break;
default:
printf("Cannnot find type: %d\n", type);
break;
}
assert(taos_stmt_bind_param(stmt, params) == 0);
assert(taos_stmt_execute(stmt) == 0);
TAOS_RES *result = taos_stmt_use_result(stmt);
assert(result != NULL);
print_result(result);
assert(taos_stmt_close(stmt) == 0);
free(params);
free(stmt_sql);
}
void taos_stmt_use_result_test() {
printf("start taos_stmt_use_result test\n");
void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0);
if (taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(
taos,
"create table super(ts timestamp, c1 int, c2 bigint, c3 float, c4 double, c5 binary(8), c6 smallint, c7 tinyint, "
"c8 bool, c9 nchar(8), c10 timestamp, c11 int unsigned, c12 bigint unsigned, c13 smallint unsigned, c14 tinyint "
"unsigned) tags (t1 int, t2 bigint, t3 float, t4 double, t5 binary(8), t6 smallint, t7 tinyint, t8 bool, t9 "
"nchar(8), t10 int unsigned, t11 bigint unsigned, t12 smallint unsigned, t13 tinyint unsigned)");
execute_simple_sql(taos,
"create table t1 using super tags (1, 1, 1, 1, 'abcdefgh',1,1,1,'一二三四五六七八', 1, 1, 1, 1)");
execute_simple_sql(
taos, "insert into t1 values (1591060628000, 1, 1, 1, 1, 'abcdefgh',1,1,1,'一二三四五六七八', now, 1, 1, 1, 1)");
execute_simple_sql(
taos, "insert into t1 values (1591060628001, 1, 1, 1, 1, 'abcdefgh',1,1,1,'一二三四五六七八', now, 1, 1, 1, 1)");
taos_stmt_use_result_query(taos, "ts", TSDB_DATA_TYPE_TIMESTAMP);
taos_stmt_use_result_query(taos, "c1", TSDB_DATA_TYPE_INT);
taos_stmt_use_result_query(taos, "c2", TSDB_DATA_TYPE_BIGINT);
taos_stmt_use_result_query(taos, "c3", TSDB_DATA_TYPE_FLOAT);
taos_stmt_use_result_query(taos, "c4", TSDB_DATA_TYPE_DOUBLE);
taos_stmt_use_result_query(taos, "c5", TSDB_DATA_TYPE_BINARY);
taos_stmt_use_result_query(taos, "c6", TSDB_DATA_TYPE_SMALLINT);
taos_stmt_use_result_query(taos, "c7", TSDB_DATA_TYPE_TINYINT);
taos_stmt_use_result_query(taos, "c8", TSDB_DATA_TYPE_BOOL);
taos_stmt_use_result_query(taos, "c9", TSDB_DATA_TYPE_NCHAR);
taos_stmt_use_result_query(taos, "c10", TSDB_DATA_TYPE_TIMESTAMP);
taos_stmt_use_result_query(taos, "c11", TSDB_DATA_TYPE_UINT);
taos_stmt_use_result_query(taos, "c12", TSDB_DATA_TYPE_UBIGINT);
taos_stmt_use_result_query(taos, "c13", TSDB_DATA_TYPE_USMALLINT);
taos_stmt_use_result_query(taos, "c14", TSDB_DATA_TYPE_UTINYINT);
printf("finish taos_stmt_use_result test\n");
}
void taos_stmt_close_test() {
printf("start taos_stmt_close test\n");
// ASM ERROR
TAOS_STMT *stmt = NULL;
assert(taos_stmt_close(stmt) != 0);
printf("finish taos_stmt_close test\n");
}
void test_api_reliability() {
// ASM catch memory leak
taos_stmt_init_test();
taos_stmt_preprare_test();
taos_stmt_set_tbname_test();
taos_stmt_set_tbname_tags_test();
taos_stmt_set_sub_tbname_test();
taos_stmt_bind_param_test();
taos_stmt_bind_single_param_batch_test();
taos_stmt_bind_param_batch_test();
taos_stmt_add_batch_test();
taos_stmt_execute_test();
taos_stmt_close_test();
}
void test_query() { taos_stmt_use_result_test(); }
int main(int argc, char *argv[]) {
test_api_reliability();
test_query();
return 0;
}
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "taos.h"
#include <sys/time.h>
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
void execute_simple_sql(void *taos, char *sql) {
TAOS_RES *result = taos_query(taos, sql);
if ( result == NULL || taos_errno(result) != 0) {
printf( "failed to %s, Reason: %s\n" , sql, taos_errstr(result));
taos_free_result(result);
exit(EXIT_FAILURE);
}
taos_free_result(result);
}
void print_result(TAOS_RES* res) {
if (res == NULL) {
exit(EXIT_FAILURE);
}
TAOS_ROW row = NULL;
int num_fields = taos_num_fields(res);
TAOS_FIELD* fields = taos_fetch_fields(res);
while ((row = taos_fetch_row(res))) {
char temp[256] = {0};
taos_print_row(temp, row, fields, num_fields);
printf("get result: %s\n", temp);
}
}
void taos_stmt_init_test() {
printf("start taos_stmt_init test \n");
void *taos = NULL;
TAOS_STMT *stmt = NULL;
stmt = taos_stmt_init(taos);
assert(stmt == NULL);
// ASM ERROR
// assert(taos_stmt_close(stmt) != 0);
taos = taos_connect("127.0.0.1","root","taosdata",NULL,0);
if(taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
assert(taos_stmt_close(stmt) == 0);
printf("finish taos_stmt_init test\n");
}
void taos_stmt_preprare_test() {
printf("start taos_stmt_prepare test\n");
char *stmt_sql = calloc(1, 1048576);
TAOS_STMT *stmt = NULL;
assert(taos_stmt_prepare(stmt, stmt_sql, 0) != 0);
void *taos = NULL;
taos = taos_connect("127.0.0.1","root","taosdata",NULL,0);
if(taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create table super(ts timestamp, c1 int, c2 bigint, c3 float, c4 double, c5 binary(8), c6 smallint, c7 tinyint, c8 bool, c9 nchar(8), c10 timestamp) tags (t1 int, t2 bigint, t3 float, t4 double, t5 binary(8), t6 smallint, t7 tinyint, t8 bool, t9 nchar(8))");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
// below will make client dead lock
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
// assert(taos_stmt_close(stmt) == 0);
// stmt = taos_stmt_init(taos);
assert(stmt != NULL);
sprintf(stmt_sql, "select from ?");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) != 0);
assert(taos_stmt_close(stmt) == 0);
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
sprintf(stmt_sql, "insert into ? values (?,?,?,?,?,?,?,?,?,?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_close(stmt) == 0);
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
sprintf(stmt_sql, "insert into super values (?,?,?,?,?,?,?,?,?,?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) != 0);
assert(taos_stmt_close(stmt) == 0);
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
sprintf(stmt_sql, "insert into ? values (?,?,?,?,?,?,?,?,1,?,?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_close(stmt) == 0);
free(stmt_sql);
printf("finish taos_stmt_prepare test\n");
}
void taos_stmt_set_tbname_test() {
printf("start taos_stmt_set_tbname test\n");
TAOS_STMT *stmt = NULL;
char *name = calloc(1, 200);
// ASM ERROR
// assert(taos_stmt_set_tbname(stmt, name) != 0);
void *taos = taos_connect("127.0.0.1","root","taosdata",NULL,0);
if(taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create table super(ts timestamp, c1 int)");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
assert(taos_stmt_set_tbname(stmt, name) != 0);
char* stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
sprintf(name, "super");
assert(stmt != NULL);
assert(taos_stmt_set_tbname(stmt, name) == 0);
free(name);
free(stmt_sql);
taos_stmt_close(stmt);
printf("finish taos_stmt_set_tbname test\n");
}
void taos_stmt_set_tbname_tags_test() {
printf("start taos_stmt_set_tbname_tags test\n");
TAOS_STMT *stmt = NULL;
char *name = calloc(1,20);
TAOS_BIND *tags = calloc(1, sizeof(TAOS_BIND));
// ASM ERROR
// assert(taos_stmt_set_tbname_tags(stmt, name, tags) != 0);
void *taos = taos_connect("127.0.0.1","root","taosdata",NULL,0);
if(taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create stable super(ts timestamp, c1 int) tags (id int)");
execute_simple_sql(taos, "create table tb using super tags (1)");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
char* stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? using super tags (?) values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_set_tbname_tags(stmt, name, tags) != 0);
sprintf(name, "tb");
assert(taos_stmt_set_tbname_tags(stmt, name, tags) != 0);
int t = 1;
tags->buffer_length = TSDB_DATA_TYPE_INT;
tags->buffer_length = sizeof(uint32_t);
tags->buffer = &t;
tags->length = &tags->buffer_length;
tags->is_null = NULL;
assert(taos_stmt_set_tbname_tags(stmt, name, tags) == 0);
free(stmt_sql);
free(name);
free(tags);
taos_stmt_close(stmt);
printf("finish taos_stmt_set_tbname_tags test\n");
}
void taos_stmt_set_sub_tbname_test() {
printf("start taos_stmt_set_sub_tbname test\n");
TAOS_STMT *stmt = NULL;
char *name = calloc(1, 200);
// ASM ERROR
// assert(taos_stmt_set_sub_tbname(stmt, name) != 0);
void *taos = taos_connect("127.0.0.1","root","taosdata",NULL,0);
if(taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create stable super(ts timestamp, c1 int) tags (id int)");
execute_simple_sql(taos, "create table tb using super tags (1)");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
char* stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_set_sub_tbname(stmt, name) != 0);
sprintf(name, "tb");
assert(taos_stmt_set_sub_tbname(stmt, name) == 0);
// assert(taos_load_table_info(taos, "super, tb") == 0);
// assert(taos_stmt_set_sub_tbname(stmt, name) == 0);
free(name);
free(stmt_sql);
assert(taos_stmt_close(stmt) == 0);
printf("finish taos_stmt_set_sub_tbname test\n");
}
void taos_stmt_bind_param_test() {
printf("start taos_stmt_bind_param test\n");
TAOS_STMT *stmt = NULL;
TAOS_BIND *binds = NULL;
assert(taos_stmt_bind_param(stmt, binds) != 0);
void *taos = taos_connect("127.0.0.1","root","taosdata",NULL,0);
if(taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create table super(ts timestamp, c1 int)");
stmt = taos_stmt_init(taos);
char* stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_bind_param(stmt, binds) != 0);
free(binds);
TAOS_BIND *params = calloc(2, sizeof(TAOS_BIND));
int64_t ts = (int64_t)1591060628000;
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(uint64_t);
params[0].buffer = &ts;
params[0].length = &params[0].buffer_length;
params[0].is_null = NULL;
int32_t i = (int32_t)21474;
params[1].buffer_type = TSDB_DATA_TYPE_INT;
params[1].buffer_length = sizeof(int32_t);
params[1].buffer = &i;
params[1].length = &params[1].buffer_length;
params[1].is_null = NULL;
assert(taos_stmt_bind_param(stmt, params) != 0);
assert(taos_stmt_set_tbname(stmt, "super") == 0);
assert(taos_stmt_bind_param(stmt, params) == 0);
free(params);
free(stmt_sql);
taos_stmt_close(stmt);
printf("finish taos_stmt_bind_param test\n");
}
void taos_stmt_bind_single_param_batch_test() {
printf("start taos_stmt_bind_single_param_batch test\n");
TAOS_STMT *stmt = NULL;
TAOS_MULTI_BIND *bind = NULL;
assert(taos_stmt_bind_single_param_batch(stmt, bind, 0) != 0);
printf("finish taos_stmt_bind_single_param_batch test\n");
}
void taos_stmt_bind_param_batch_test() {
printf("start taos_stmt_bind_param_batch test\n");
TAOS_STMT *stmt = NULL;
TAOS_MULTI_BIND *bind = NULL;
assert(taos_stmt_bind_param_batch(stmt, bind) != 0);
printf("finish taos_stmt_bind_param_batch test\n");
}
void taos_stmt_add_batch_test() {
printf("start taos_stmt_add_batch test\n");
TAOS_STMT *stmt = NULL;
assert(taos_stmt_add_batch(stmt) != 0);
void *taos = taos_connect("127.0.0.1","root","taosdata",NULL,0);
if(taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create table super(ts timestamp, c1 int)");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
char* stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_add_batch(stmt) != 0);
TAOS_BIND *params = calloc(2, sizeof(TAOS_BIND));
int64_t ts = (int64_t)1591060628000;
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(uint64_t);
params[0].buffer = &ts;
params[0].length = &params[0].buffer_length;
params[0].is_null = NULL;
int32_t i = (int32_t)21474;
params[1].buffer_type = TSDB_DATA_TYPE_INT;
params[1].buffer_length = sizeof(int32_t);
params[1].buffer = &i;
params[1].length = &params[1].buffer_length;
params[1].is_null = NULL;
assert(taos_stmt_set_tbname(stmt, "super") == 0);
assert(taos_stmt_bind_param(stmt, params) == 0);
assert(taos_stmt_add_batch(stmt) == 0);
free(params);
free(stmt_sql);
assert(taos_stmt_close(stmt) == 0);
printf("finish taos_stmt_add_batch test\n");
}
void taos_stmt_execute_test() {
printf("start taos_stmt_execute test\n");
TAOS_STMT *stmt = NULL;
assert(taos_stmt_execute(stmt) != 0);
void *taos = taos_connect("127.0.0.1","root","taosdata",NULL,0);
if(taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create table super(ts timestamp, c1 int)");
stmt = taos_stmt_init(taos);
assert(stmt != NULL);
assert(taos_stmt_execute(stmt) != 0);
char* stmt_sql = calloc(1, 1000);
sprintf(stmt_sql, "insert into ? values (?,?)");
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
assert(taos_stmt_execute(stmt) != 0);
TAOS_BIND *params = calloc(2, sizeof(TAOS_BIND));
int64_t ts = (int64_t)1591060628000;
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(uint64_t);
params[0].buffer = &ts;
params[0].length = &params[0].buffer_length;
params[0].is_null = NULL;
int32_t i = (int32_t)21474;
params[1].buffer_type = TSDB_DATA_TYPE_INT;
params[1].buffer_length = sizeof(int32_t);
params[1].buffer = &i;
params[1].length = &params[1].buffer_length;
params[1].is_null = NULL;
assert(taos_stmt_set_tbname(stmt, "super") == 0);
assert(taos_stmt_execute(stmt) != 0);
assert(taos_stmt_bind_param(stmt, params) == 0);
assert(taos_stmt_execute(stmt) != 0);
assert(taos_stmt_add_batch(stmt) == 0);
assert(taos_stmt_execute(stmt) == 0);
free(params);
free(stmt_sql);
assert(taos_stmt_close(stmt) == 0);
printf("finish taos_stmt_execute test\n");
}
void taos_stmt_use_result_query(void *taos, char *col, int type) {
TAOS_STMT *stmt = taos_stmt_init(taos);
assert(stmt != NULL);
char *stmt_sql = calloc(1, 1024);
struct {
int64_t c1;
int32_t c2;
int64_t c3;
float c4;
double c5;
char c6[8];
int16_t c7;
int8_t c8;
int8_t c9;
char c10[32];
} v = {0};
v.c1 = (int64_t)1591060628000;
v.c2 = (int32_t)1;
v.c3 = (int64_t)1;
v.c4 = (float)1;
v.c5 = (double)1;
strcpy(v.c6, "abcdefgh");
v.c7 = 1;
v.c8 = 1;
v.c9 = 1;
strcpy(v.c10, "一二三四五六七八");
uintptr_t c10len=strlen(v.c10);
sprintf(stmt_sql, "select * from stmt_test.t1 where %s = ?", col);
printf("stmt_sql: %s\n", stmt_sql);
assert(taos_stmt_prepare(stmt, stmt_sql, 0) == 0);
TAOS_BIND *params = calloc(1, sizeof(TAOS_BIND));
params->buffer_type = type;
params->is_null = NULL;
switch(type){
case TSDB_DATA_TYPE_TIMESTAMP:
params->buffer_length = sizeof(v.c1);
params->buffer = &v.c1;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_INT:
params->buffer_length = sizeof(v.c2);
params->buffer = &v.c2;
params->length = &params->buffer_length;
case TSDB_DATA_TYPE_BIGINT:
params->buffer_length = sizeof(v.c3);
params->buffer = &v.c3;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_FLOAT:
params->buffer_length = sizeof(v.c4);
params->buffer = &v.c4;
params->length = &params->buffer_length;
case TSDB_DATA_TYPE_DOUBLE:
params->buffer_length = sizeof(v.c5);
params->buffer = &v.c5;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_BINARY:
params->buffer_length = sizeof(v.c6);
params->buffer = &v.c6;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_SMALLINT:
params->buffer_length = sizeof(v.c7);
params->buffer = &v.c7;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_TINYINT:
params->buffer_length = sizeof(v.c8);
params->buffer = &v.c8;
params->length = &params->buffer_length;
case TSDB_DATA_TYPE_BOOL:
params->buffer_length = sizeof(v.c9);
params->buffer = &v.c9;
params->length = &params->buffer_length;
break;
case TSDB_DATA_TYPE_NCHAR:
params->buffer_length = sizeof(v.c10);
params->buffer = &v.c10;
params->length = &c10len;
break;
default:
printf("Cannnot find type: %d\n", type);
break;
}
assert(taos_stmt_bind_param(stmt, params) == 0);
assert(taos_stmt_execute(stmt) == 0);
TAOS_RES* result = taos_stmt_use_result(stmt);
assert(result != NULL);
print_result(result);
assert(taos_stmt_close(stmt) == 0);
free(params);
free(stmt_sql);
taos_free_result(result);
}
void taos_stmt_use_result_test() {
printf("start taos_stmt_use_result test\n");
void *taos = taos_connect("127.0.0.1","root","taosdata",NULL,0);
if(taos == NULL) {
printf("Cannot connect to tdengine server\n");
exit(EXIT_FAILURE);
}
execute_simple_sql(taos, "drop database if exists stmt_test");
execute_simple_sql(taos, "create database stmt_test");
execute_simple_sql(taos, "use stmt_test");
execute_simple_sql(taos, "create table super(ts timestamp, c1 int, c2 bigint, c3 float, c4 double, c5 binary(8), c6 smallint, c7 tinyint, c8 bool, c9 nchar(8), c10 timestamp) tags (t1 int, t2 bigint, t3 float, t4 double, t5 binary(8), t6 smallint, t7 tinyint, t8 bool, t9 nchar(8))");
execute_simple_sql(taos, "create table t1 using super tags (1, 1, 1, 1, 'abcdefgh',1,1,1,'一二三四五六七八')");
execute_simple_sql(taos, "insert into t1 values (1591060628000, 1, 1, 1, 1, 'abcdefgh',1,1,1,'一二三四五六七八', now)");
execute_simple_sql(taos, "insert into t1 values (1591060628001, 1, 1, 1, 1, 'abcdefgh',1,1,1,'一二三四五六七八', now)");
taos_stmt_use_result_query(taos, "c1", TSDB_DATA_TYPE_INT);
taos_stmt_use_result_query(taos, "c2", TSDB_DATA_TYPE_BIGINT);
taos_stmt_use_result_query(taos, "c3", TSDB_DATA_TYPE_FLOAT);
taos_stmt_use_result_query(taos, "c4", TSDB_DATA_TYPE_DOUBLE);
taos_stmt_use_result_query(taos, "c5", TSDB_DATA_TYPE_BINARY);
taos_stmt_use_result_query(taos, "c6", TSDB_DATA_TYPE_SMALLINT);
taos_stmt_use_result_query(taos, "c7", TSDB_DATA_TYPE_TINYINT);
taos_stmt_use_result_query(taos, "c8", TSDB_DATA_TYPE_BOOL);
taos_stmt_use_result_query(taos, "c9", TSDB_DATA_TYPE_NCHAR);
printf("finish taos_stmt_use_result test\n");
}
void taos_stmt_close_test() {
printf("start taos_stmt_close test\n");
// ASM ERROR
// TAOS_STMT *stmt = NULL;
// assert(taos_stmt_close(stmt) != 0);
printf("finish taos_stmt_close test\n");
}
void test_api_reliability() {
// ASM catch memory leak
taos_stmt_init_test();
taos_stmt_preprare_test();
taos_stmt_set_tbname_test();
taos_stmt_set_tbname_tags_test();
taos_stmt_set_sub_tbname_test();
taos_stmt_bind_param_test();
taos_stmt_bind_single_param_batch_test();
taos_stmt_bind_param_batch_test();
taos_stmt_add_batch_test();
taos_stmt_execute_test();
taos_stmt_close_test();
}
void test_query() {
taos_stmt_use_result_test();
}
int main(int argc, char *argv[]) {
test_api_reliability();
test_query();
return 0;
}
\ No newline at end of file
...@@ -941,6 +941,17 @@ if $data32 != 0.000144445 then ...@@ -941,6 +941,17 @@ if $data32 != 0.000144445 then
return -1 return -1
endi endi
sql insert into t1 values('2015-09-18 00:30:00', 3.0);
sql select irate(k) from t1
if $rows != 1 then
return -1
endi
if $data00 != 0.000000354 then
return -1
endi
print ===========================> derivative print ===========================> derivative
sql drop table t1 sql drop table t1
sql drop table tx; sql drop table tx;
......
...@@ -497,7 +497,7 @@ if [ "$2" != "sim" ] && [ "$2" != "python" ] && [ "$2" != "jdbc" ] && [ "$2" != ...@@ -497,7 +497,7 @@ if [ "$2" != "sim" ] && [ "$2" != "python" ] && [ "$2" != "jdbc" ] && [ "$2" !=
totalExamplePass=`expr $totalExamplePass + 1` totalExamplePass=`expr $totalExamplePass + 1`
fi fi
./prepare 127.0.0.1 > /dev/null 2>&1 ./prepare > /dev/null 2>&1
if [ $? != "0" ]; then if [ $? != "0" ]; then
echo "prepare failed" echo "prepare failed"
totalExampleFailed=`expr $totalExampleFailed + 1` totalExampleFailed=`expr $totalExampleFailed + 1`
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册