From 47a77371eea1d85518bbe94b6e04b405c1b5a1f3 Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 4 Oct 2020 14:54:31 +0800 Subject: [PATCH 001/101] prefer named-type rather than void-type to reduce typos --- src/client/inc/tscSubquery.h | 2 +- src/client/inc/tsclient.h | 12 +++--- src/client/src/tscLocal.c | 2 +- src/client/src/tscProfile.c | 4 +- src/client/src/tscServer.c | 2 +- src/client/src/tscSql.c | 23 +++++----- src/client/src/tscSubquery.c | 6 +-- src/inc/taos.h | 20 ++++++--- src/kit/shell/inc/shell.h | 4 +- src/kit/shell/src/shellEngine.c | 6 +-- src/kit/shell/src/shellImport.c | 4 +- src/plugins/http/inc/httpInt.h | 6 +-- src/plugins/http/src/httpGcJson.c | 2 +- src/plugins/http/src/httpRestJson.c | 4 +- src/plugins/http/src/httpSql.c | 2 - tests/comparisonTest/tdengine/tdengineTest.c | 45 ++++++++++++++------ tests/examples/c/demo.c | 20 ++++++--- 17 files changed, 101 insertions(+), 63 deletions(-) diff --git a/src/client/inc/tscSubquery.h b/src/client/inc/tscSubquery.h index 07e0580397..ae7f4a2f0c 100644 --- a/src/client/inc/tscSubquery.h +++ b/src/client/inc/tscSubquery.h @@ -39,7 +39,7 @@ int32_t tscHandleMultivnodeInsert(SSqlObj *pSql); int32_t tscHandleInsertRetry(SSqlObj* pSql); void tscBuildResFromSubqueries(SSqlObj *pSql); -void **doSetResultRowData(SSqlObj *pSql, bool finalResult); +TAOS_ROW doSetResultRowData(SSqlObj *pSql, bool finalResult); #ifdef __cplusplus } diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index 046bea2c27..3db4a97974 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -306,7 +306,7 @@ typedef struct { int32_t numOfGroups; SResRec * pGroupRec; char * data; - void ** tsrow; + TAOS_ROW tsrow; int32_t* length; // length for each field for current row char ** buffer; // Buffer used to put multibytes encoded using unicode (wchar_t) SColumnIndex * pColumnIndex; @@ -439,7 +439,7 @@ void tscFreeSqlObjInCache(void *pSql); void tscCloseTscObj(STscObj *pObj); TAOS *taos_connect_a(char *ip, char *user, char *pass, char *db, uint16_t port, void (*fp)(void *, TAOS_RES *, int), - void *param, void **taos); + void *param, TAOS **taos); void waitForQueryRsp(void *param, TAOS_RES *tres, int code) ; void doAsyncQuery(STscObj *pObj, SSqlObj *pSql, void (*fp)(), void *param, const char *sqlstr, size_t sqlLen); @@ -471,11 +471,11 @@ static FORCE_INLINE void tscGetResultColumnChr(SSqlRes* pRes, SFieldInfo* pField if (type == TSDB_DATA_TYPE_NCHAR || type == TSDB_DATA_TYPE_BINARY) { pData = pInfo->pSqlExpr->param[1].pz; pRes->length[columnIndex] = pInfo->pSqlExpr->param[1].nLen; - pRes->tsrow[columnIndex] = (pInfo->pSqlExpr->param[1].nType == TSDB_DATA_TYPE_NULL) ? NULL : pData; + pRes->tsrow[columnIndex] = (pInfo->pSqlExpr->param[1].nType == TSDB_DATA_TYPE_NULL) ? NULL : (unsigned char*)pData; } else { assert(bytes == tDataTypeDesc[type].nSize); - pRes->tsrow[columnIndex] = isNull(pData, type) ? NULL : &pInfo->pSqlExpr->param[1].i64Key; + pRes->tsrow[columnIndex] = isNull(pData, type) ? NULL : (unsigned char*)&pInfo->pSqlExpr->param[1].i64Key; pRes->length[columnIndex] = bytes; } } else { @@ -483,7 +483,7 @@ static FORCE_INLINE void tscGetResultColumnChr(SSqlRes* pRes, SFieldInfo* pField int32_t realLen = varDataLen(pData); assert(realLen <= bytes - VARSTR_HEADER_SIZE); - pRes->tsrow[columnIndex] = (isNull(pData, type)) ? NULL : ((tstr *)pData)->data; + pRes->tsrow[columnIndex] = (isNull(pData, type)) ? NULL : (unsigned char*)((tstr *)pData)->data; if (realLen < pInfo->pSqlExpr->resBytes - VARSTR_HEADER_SIZE) { // todo refactor *(pData + realLen + VARSTR_HEADER_SIZE) = 0; } @@ -492,7 +492,7 @@ static FORCE_INLINE void tscGetResultColumnChr(SSqlRes* pRes, SFieldInfo* pField } else { assert(bytes == tDataTypeDesc[type].nSize); - pRes->tsrow[columnIndex] = isNull(pData, type) ? NULL : pData; + pRes->tsrow[columnIndex] = isNull(pData, type) ? NULL : (unsigned char*)pData; pRes->length[columnIndex] = bytes; } } diff --git a/src/client/src/tscLocal.c b/src/client/src/tscLocal.c index a15673bdb1..929b9ffe68 100644 --- a/src/client/src/tscLocal.c +++ b/src/client/src/tscLocal.c @@ -296,7 +296,7 @@ static int32_t tscProcessDescribeTable(SSqlObj *pSql) { return tscSetValueToResObj(pSql, rowLen); } static int32_t tscGetNthFieldResult(TAOS_ROW row, TAOS_FIELD* fields, int *lengths, int idx, char *result) { - const char *val = row[idx]; + const char *val = (const char*)row[idx]; if (val == NULL) { sprintf(result, "%s", TSDB_DATA_NULL_STR); return -1; diff --git a/src/client/src/tscProfile.c b/src/client/src/tscProfile.c index f2f997a594..5b3876d3dc 100644 --- a/src/client/src/tscProfile.c +++ b/src/client/src/tscProfile.c @@ -20,8 +20,10 @@ #include "tutil.h" #include "taosmsg.h" +#include "taos.h" + void tscSaveSlowQueryFp(void *handle, void *tmrId); -void *tscSlowQueryConn = NULL; +TAOS *tscSlowQueryConn = NULL; bool tscSlowQueryConnInitialized = false; void tscInitConnCb(void *param, TAOS_RES *result, int code) { diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index d3c0201169..d25f4b4a95 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1392,7 +1392,7 @@ static int tscSetResultPointer(SQueryInfo *pQueryInfo, SSqlRes *pRes) { for (int i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) { int16_t offset = tscFieldInfoGetOffset(pQueryInfo, i); - pRes->tsrow[i] = ((char*) pRes->data + offset * pRes->numOfRows); + pRes->tsrow[i] = (unsigned char*)((char*) pRes->data + offset * pRes->numOfRows); } return 0; diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index ac7081ba70..525333454f 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -49,8 +49,8 @@ static bool validPassword(const char* passwd) { return validImpl(passwd, TSDB_PASSWORD_LEN - 1); } -SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pass, const char *auth, const char *db, - uint16_t port, void (*fp)(void *, TAOS_RES *, int), void *param, void **taos) { +static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pass, const char *auth, const char *db, + uint16_t port, void (*fp)(void *, TAOS_RES *, int), void *param, TAOS **taos) { taos_init(); if (!validUserName(user)) { @@ -172,7 +172,7 @@ static void syncConnCallback(void *param, TAOS_RES *tres, int code) { TAOS *taos_connect_internal(const char *ip, const char *user, const char *pass, const char *auth, const char *db, uint16_t port) { STscObj *pObj = NULL; - SSqlObj *pSql = taosConnectImpl(ip, user, pass, auth, db, port, syncConnCallback, NULL, (void **)&pObj); + SSqlObj *pSql = taosConnectImpl(ip, user, pass, auth, db, port, syncConnCallback, NULL, &pObj); if (pSql != NULL) { pSql->fp = syncConnCallback; pSql->param = pSql; @@ -241,16 +241,19 @@ static void asyncConnCallback(void *param, TAOS_RES *tres, int code) { } TAOS *taos_connect_a(char *ip, char *user, char *pass, char *db, uint16_t port, void (*fp)(void *, TAOS_RES *, int), - void *param, void **taos) { - SSqlObj* pSql = taosConnectImpl(ip, user, pass, NULL, db, port, asyncConnCallback, param, taos); + void *param, TAOS **taos) { + STscObj *pObj = NULL; + SSqlObj* pSql = taosConnectImpl(ip, user, pass, NULL, db, port, asyncConnCallback, param, &pObj); if (pSql == NULL) { return NULL; } - + + if (taos) *taos = pObj; + pSql->fetchFp = fp; pSql->res.code = tscProcessSql(pSql); tscDebug("%p DB async connection is opening", taos); - return taos; + return pObj; } void taos_close(TAOS *taos) { @@ -812,7 +815,7 @@ int taos_validate_sql(TAOS *taos, const char *sql) { if (pSql->sqlstr == NULL) { pRes->code = TSDB_CODE_TSC_OUT_OF_MEMORY; tscError("%p failed to malloc sql string buffer", pSql); - tscDebug("%p Valid SQL result:%d, %s pObj:%p", pSql, pRes->code, taos_errstr(taos), pObj); + tscDebug("%p Valid SQL result:%d, %s pObj:%p", pSql, pRes->code, taos_errstr(pSql), pObj); taosTFree(pSql); return pRes->code; } @@ -835,7 +838,7 @@ int taos_validate_sql(TAOS *taos, const char *sql) { } if (code != TSDB_CODE_SUCCESS) { - tscDebug("%p Valid SQL result:%d, %s pObj:%p", pSql, code, taos_errstr(taos), pObj); + tscDebug("%p Valid SQL result:%d, %s pObj:%p", pSql, code, taos_errstr(pSql), pObj); } taos_free_result(pSql); @@ -979,7 +982,7 @@ int taos_load_table_info(TAOS *taos, const char *tableNameList) { tscDoQuery(pSql); - tscDebug("%p load multi table meta result:%d %s pObj:%p", pSql, pRes->code, taos_errstr(taos), pObj); + tscDebug("%p load multi table meta result:%d %s pObj:%p", pSql, pRes->code, taos_errstr(pSql), pObj); if ((code = pRes->code) != TSDB_CODE_SUCCESS) { tscFreeSqlObj(pSql); } diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index 390c3ca92d..1527a82d68 100644 --- a/src/client/src/tscSubquery.c +++ b/src/client/src/tscSubquery.c @@ -2141,7 +2141,7 @@ static void transferNcharData(SSqlObj *pSql, int32_t columnIndex, TAOS_FIELD *pF int32_t length = taosUcs4ToMbs(pRes->tsrow[columnIndex], pRes->length[columnIndex], pRes->buffer[columnIndex]); if ( length >= 0 ) { - pRes->tsrow[columnIndex] = pRes->buffer[columnIndex]; + pRes->tsrow[columnIndex] = (unsigned char*)pRes->buffer[columnIndex]; pRes->length[columnIndex] = length; } else { tscError("%p charset:%s to %s. val:%s convert failed.", pSql, DEFAULT_UNICODE_ENCODEC, tsCharset, (char*)pRes->tsrow[columnIndex]); @@ -2169,7 +2169,7 @@ static char *getArithemicInputSrc(void *param, const char *name, int32_t colId) return pSupport->data[index] + pSupport->offset * pExpr->resBytes; } -void **doSetResultRowData(SSqlObj *pSql, bool finalResult) { +TAOS_ROW doSetResultRowData(SSqlObj *pSql, bool finalResult) { SSqlCmd *pCmd = &pSql->cmd; SSqlRes *pRes = &pSql->res; @@ -2222,7 +2222,7 @@ void **doSetResultRowData(SSqlObj *pSql, bool finalResult) { tExprTreeCalcTraverse(pRes->pArithSup->pArithExpr->pExpr, 1, pRes->buffer[i], pRes->pArithSup, TSDB_ORDER_ASC, getArithemicInputSrc); - pRes->tsrow[i] = pRes->buffer[i]; + pRes->tsrow[i] = (unsigned char*)pRes->buffer[i]; } } diff --git a/src/inc/taos.h b/src/inc/taos.h index 7e8f174b7c..9ea1d268ea 100644 --- a/src/inc/taos.h +++ b/src/inc/taos.h @@ -22,12 +22,20 @@ extern "C" { #endif -typedef void TAOS; -typedef void** TAOS_ROW; -typedef void TAOS_RES; -typedef void TAOS_SUB; -typedef void TAOS_STREAM; -typedef void TAOS_STMT; +typedef struct STscObj TAOS; +typedef struct STscStmt TAOS_STMT; +typedef struct SSqlObj TAOS_RES; +typedef struct SSqlStream TAOS_STREAM; +typedef struct SSub TAOS_SUB; +typedef unsigned char** TAOS_ROW; + + +// typedef void TAOS; +// typedef void** TAOS_ROW; +// typedef void TAOS_RES; +// typedef void TAOS_SUB; +// typedef void TAOS_STREAM; +// typedef void TAOS_STMT; // Data type definition #define TSDB_DATA_TYPE_NULL 0 // 1 bytes diff --git a/src/kit/shell/inc/shell.h b/src/kit/shell/inc/shell.h index dd62df170a..da2824d806 100644 --- a/src/kit/shell/inc/shell.h +++ b/src/kit/shell/inc/shell.h @@ -59,7 +59,7 @@ typedef struct SShellArguments { extern void shellParseArgument(int argc, char* argv[], SShellArguments* arguments); extern TAOS* shellInit(SShellArguments* args); extern void* shellLoopQuery(void* arg); -extern void taos_error(TAOS* con); +extern void taos_error(TAOS_RES* tres); extern int regex_match(const char* s, const char* reg, int cflags); void shellReadCommand(TAOS* con, char command[]); int32_t shellRunCommand(TAOS* con, char* command); @@ -71,7 +71,7 @@ void source_dir(TAOS* con, SShellArguments* args); void get_history_path(char* history); void cleanup_handler(void* arg); void exitShell(); -int shellDumpResult(TAOS* con, char* fname, int* error_no, bool printMode); +int shellDumpResult(TAOS_RES* con, char* fname, int* error_no, bool printMode); void shellGetGrantInfo(void *con); int isCommentLine(char *line); diff --git a/src/kit/shell/src/shellEngine.c b/src/kit/shell/src/shellEngine.c index d5e826fbaa..4c4d8913bb 100644 --- a/src/kit/shell/src/shellEngine.c +++ b/src/kit/shell/src/shellEngine.c @@ -495,7 +495,7 @@ static int dumpResultToFile(const char* fname, TAOS_RES* tres) { if (i > 0) { fputc(',', fp); } - dumpFieldToFile(fp, row[i], fields +i, length[i], precision); + dumpFieldToFile(fp, (const char*)row[i], fields +i, length[i], precision); } fputc('\n', fp); @@ -621,7 +621,7 @@ static int verticalPrintResult(TAOS_RES* tres) { int padding = (int)(maxColNameLen - strlen(field->name)); printf("%*.s%s: ", padding, " ", field->name); - printField(row[i], field, 0, length[i], precision); + printField((const char*)row[i], field, 0, length[i], precision); putchar('\n'); } @@ -722,7 +722,7 @@ static int horizontalPrintResult(TAOS_RES* tres) { int32_t* length = taos_fetch_lengths(tres); for (int i = 0; i < num_fields; i++) { putchar(' '); - printField(row[i], fields + i, width[i], length[i], precision); + printField((const char*)row[i], fields + i, width[i], length[i], precision); putchar(' '); putchar('|'); } diff --git a/src/kit/shell/src/shellImport.c b/src/kit/shell/src/shellImport.c index ee0a90757b..af61995c61 100644 --- a/src/kit/shell/src/shellImport.c +++ b/src/kit/shell/src/shellImport.c @@ -204,7 +204,7 @@ static void shellSourceFile(TAOS *con, char *fptr) { int32_t code = taos_errno(pSql); if (code != 0) { - fprintf(stderr, "DB error: %s: %s (%d)\n", taos_errstr(con), fname, lineNo); + fprintf(stderr, "DB error: %s: %s (%d)\n", taos_errstr(pSql), fname, lineNo); } /* free local resouce: allocated memory/metric-meta refcnt */ @@ -243,7 +243,7 @@ static void shellRunImportThreads(SShellArguments* args) pThread->totalThreads = args->threadNum; pThread->taos = taos_connect(args->host, args->user, args->password, args->database, tsDnodeShellPort); if (pThread->taos == NULL) { - fprintf(stderr, "ERROR: thread:%d failed connect to TDengine, error:%s\n", pThread->threadIndex, taos_errstr(pThread->taos)); + fprintf(stderr, "ERROR: thread:%d failed connect to TDengine, error:%s\n", pThread->threadIndex, "null taos"/*taos_errstr(pThread->taos)*/); exit(0); } diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h index 36a022159f..affc0e838e 100644 --- a/src/plugins/http/inc/httpInt.h +++ b/src/plugins/http/inc/httpInt.h @@ -122,9 +122,9 @@ typedef struct { } HttpDecodeMethod; typedef struct { - void (*startJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, void *result); + void (*startJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result); void (*stopJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd); - bool (*buildQueryJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, void *result, int numOfRows); + bool (*buildQueryJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows); void (*buildAffectRowJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, int affectRows); void (*initJsonFp)(struct HttpContext *pContext); void (*cleanJsonFp)(struct HttpContext *pContext); @@ -148,7 +148,7 @@ typedef struct HttpContext { char ipstr[22]; char user[TSDB_USER_LEN]; // parsed from auth token or login message char pass[TSDB_PASSWORD_LEN]; - void * taos; + TAOS * taos; void * ppContext; HttpSession *session; z_stream gzipStream; diff --git a/src/plugins/http/src/httpGcJson.c b/src/plugins/http/src/httpGcJson.c index 80e4ae3bc2..2c9eca11de 100644 --- a/src/plugins/http/src/httpGcJson.c +++ b/src/plugins/http/src/httpGcJson.c @@ -217,7 +217,7 @@ bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, break; case TSDB_DATA_TYPE_BINARY: case TSDB_DATA_TYPE_NCHAR: - httpJsonStringForTransMean(jsonBuf, row[i], fields[i].bytes); + httpJsonStringForTransMean(jsonBuf, (char*)row[i], fields[i].bytes); break; case TSDB_DATA_TYPE_TIMESTAMP: if (precision == TSDB_TIME_PRECISION_MILLI) { //ms diff --git a/src/plugins/http/src/httpRestJson.c b/src/plugins/http/src/httpRestJson.c index 954678c24c..a5b156bffc 100644 --- a/src/plugins/http/src/httpRestJson.c +++ b/src/plugins/http/src/httpRestJson.c @@ -131,7 +131,7 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, break; case TSDB_DATA_TYPE_BINARY: case TSDB_DATA_TYPE_NCHAR: - httpJsonStringForTransMean(jsonBuf, row[i], length[i]); + httpJsonStringForTransMean(jsonBuf, (char*)row[i], length[i]); break; case TSDB_DATA_TYPE_TIMESTAMP: if (timestampFormat == REST_TIMESTAMP_FMT_LOCAL_STRING) { @@ -195,4 +195,4 @@ void restStopSqlJson(HttpContext *pContext, HttpSqlCmd *cmd) { httpJsonToken(jsonBuf, JsonObjEnd); httpWriteJsonBufEnd(jsonBuf); -} \ No newline at end of file +} diff --git a/src/plugins/http/src/httpSql.c b/src/plugins/http/src/httpSql.c index 883fa574ff..70d644146c 100644 --- a/src/plugins/http/src/httpSql.c +++ b/src/plugins/http/src/httpSql.c @@ -27,8 +27,6 @@ #include "httpSession.h" #include "httpQueue.h" -void *taos_connect_a(char *ip, char *user, char *pass, char *db, uint16_t port, void (*fp)(void *, TAOS_RES *, int), - void *param, void **taos); void httpProcessMultiSql(HttpContext *pContext); void httpProcessMultiSqlRetrieveCallBack(void *param, TAOS_RES *result, int numOfRows); diff --git a/tests/comparisonTest/tdengine/tdengineTest.c b/tests/comparisonTest/tdengine/tdengineTest.c index 237403f525..1298aa8323 100644 --- a/tests/comparisonTest/tdengine/tdengineTest.c +++ b/tests/comparisonTest/tdengine/tdengineTest.c @@ -108,9 +108,9 @@ void parseArg(int argc, char *argv[]) { } } -void taos_error(TAOS *con) { - printf("TDengine error: %s\n", taos_errstr(con)); - taos_close(con); +static void taos_error(TAOS_RES *tres, TAOS *conn) { + printf("TDengine error: %s\n", tres?taos_errstr(tres):"null result"); + taos_close(conn); exit(1); } @@ -125,13 +125,17 @@ void writeDataImp(void *param) { printf("Thread %d, writing sID %d, eID %d\n", pThread->threadId, pThread->sID, pThread->eID); void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0); - if (taos == NULL) - taos_error(taos); + if (taos == NULL) { + // where to find errstr? + // taos_error(NULL, taos); + printf("TDengine error: %s\n", "failed to connect"); + exit(1); + } TAOS_RES* result = taos_query(taos, "use db"); int32_t code = taos_errno(result); if (code != 0) { - taos_error(taos); + taos_error(result, taos); } taos_free_result(result); @@ -227,12 +231,17 @@ void writeData() { taos_init(); void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0); - if (taos == NULL) taos_error(taos); + if (taos == NULL) { + // where to find errstr? + // taos_error(NULL, taos); + printf("TDengine error: %s\n", "failed to connect"); + exit(1); + } TAOS_RES *result = taos_query(taos, "create database if not exists db"); int32_t code = taos_errno(result); if (code != 0) { - taos_error(taos); + taos_error(result, taos); } taos_free_result(result); @@ -241,7 +250,7 @@ void writeData() { "tags(devid int, devname binary(16), devgroup int)"); code = taos_errno(result); if (code != 0) { - taos_error(taos); + taos_error(result, taos); } taos_free_result(result); @@ -293,8 +302,12 @@ void readDataImp(void *param) printf("open file %s success\n", arguments.sql); void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0); - if (taos == NULL) - taos_error(taos); + if (taos == NULL) { + // where to find errstr? + // taos_error(NULL, taos); + printf("TDengine error: %s\n", "failed to connect"); + exit(1); + } char *line = NULL; size_t len = 0; @@ -313,7 +326,7 @@ void readDataImp(void *param) TAOS_RES *result = taos_query(taos, line); int32_t code = taos_errno(result); if (code != 0) { - taos_error(taos); + taos_error(result, taos); } TAOS_ROW row; @@ -343,8 +356,12 @@ void readData() { printf("---- clients: %d\n", arguments.clients); void *taos = taos_connect("127.0.0.1", "root", "taosdata", NULL, 0); - if (taos == NULL) - taos_error(taos); + if (taos == NULL) { + // where to find errstr? + // taos_error(NULL, taos); + printf("TDengine error: %s\n", "failed to connect"); + exit(1); + } ThreadObj *threads = calloc((size_t)arguments.clients, sizeof(ThreadObj)); diff --git a/tests/examples/c/demo.c b/tests/examples/c/demo.c index d64c0de1ce..8f8a66a325 100644 --- a/tests/examples/c/demo.c +++ b/tests/examples/c/demo.c @@ -38,7 +38,7 @@ int main(int argc, char *argv[]) { taos = taos_connect(argv[1], "root", "taosdata", NULL, 0); if (taos == NULL) { - printf("failed to connect to server, reason:%s\n", taos_errstr(taos)); + printf("failed to connect to server, reason:%s\n", "null taos"/*taos_errstr(taos)*/); exit(1); } printf("success to connect to server\n"); @@ -48,7 +48,7 @@ int main(int argc, char *argv[]) { result = taos_query(taos, "create database demo"); if (result == NULL) { - printf("failed to create database, reason:%s\n", taos_errstr(taos)); + printf("failed to create database, reason:%s\n", "null result"/*taos_errstr(taos)*/); exit(1); } printf("success to create database\n"); @@ -57,7 +57,7 @@ int main(int argc, char *argv[]) { // create table if (taos_query(taos, "create table m1 (ts timestamp, ti tinyint, si smallint, i int, bi bigint, f float, d double, b binary(10))") == 0) { - printf("failed to create table, reason:%s\n", taos_errstr(taos)); + printf("failed to create table, reason:%s\n", taos_errstr(result)); exit(1); } printf("success to create table\n"); @@ -70,9 +70,19 @@ int main(int argc, char *argv[]) { for (i = 0; i < 10; ++i) { sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", 1546300800000 + i * 1000, i, i, i, i*10000000, i*1.0, i*2.0, "hello"); printf("qstr: %s\n", qstr); - if (taos_query(taos, qstr)) { - printf("insert row: %i, reason:%s\n", i, taos_errstr(taos)); + + // note: how do you wanna do if taos_query returns non-NULL + // if (taos_query(taos, qstr)) { + // printf("insert row: %i, reason:%s\n", i, taos_errstr(taos)); + // } + TAOS_RES *result = taos_query(taos, qstr); + if (result) { + printf("insert row: %i\n", i); + } else { + printf("failed to insert row: %i, reason:%s\n", i, "null result"/*taos_errstr(result)*/); + exit(1); } + //sleep(1); } printf("success to insert rows, total %d rows\n", i); From 231cc2963ebad5a0cfa9d2b8518ca90734d0857f Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 4 Oct 2020 20:27:04 +0800 Subject: [PATCH 002/101] remove void types --- src/inc/taos.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/inc/taos.h b/src/inc/taos.h index 9ea1d268ea..deba808d0a 100644 --- a/src/inc/taos.h +++ b/src/inc/taos.h @@ -30,13 +30,6 @@ typedef struct SSub TAOS_SUB; typedef unsigned char** TAOS_ROW; -// typedef void TAOS; -// typedef void** TAOS_ROW; -// typedef void TAOS_RES; -// typedef void TAOS_SUB; -// typedef void TAOS_STREAM; -// typedef void TAOS_STMT; - // Data type definition #define TSDB_DATA_TYPE_NULL 0 // 1 bytes #define TSDB_DATA_TYPE_BOOL 1 // 1 bytes From 52f023c0282bc7127c02912ee9fa5356d3025630 Mon Sep 17 00:00:00 2001 From: freemine Date: Tue, 6 Oct 2020 22:53:15 +0800 Subject: [PATCH 003/101] init odbc --- src/CMakeLists.txt | 2 + src/connector/odbc/CMakeLists.txt | 17 + src/connector/odbc/src/odbc_install.sh | 7 + src/connector/odbc/src/template.dsn | 5 + src/connector/odbc/src/template.ini | 4 + src/connector/odbc/src/todbc.c | 426 ++++++++++++++++++++++++ src/connector/odbc/tests/CMakeLists.txt | 7 + src/connector/odbc/tests/main.c | 20 ++ src/os/inc/osDef.h | 2 +- 9 files changed, 489 insertions(+), 1 deletion(-) create mode 100644 src/connector/odbc/CMakeLists.txt create mode 100644 src/connector/odbc/src/odbc_install.sh create mode 100644 src/connector/odbc/src/template.dsn create mode 100644 src/connector/odbc/src/template.ini create mode 100644 src/connector/odbc/src/todbc.c create mode 100644 src/connector/odbc/tests/CMakeLists.txt create mode 100644 src/connector/odbc/tests/main.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 898b7cb032..f619edd221 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,4 +20,6 @@ ADD_SUBDIRECTORY(tsdb) ADD_SUBDIRECTORY(wal) ADD_SUBDIRECTORY(cq) ADD_SUBDIRECTORY(dnode) +ADD_SUBDIRECTORY(connector/odbc) ADD_SUBDIRECTORY(connector/jdbc) + diff --git a/src/connector/odbc/CMakeLists.txt b/src/connector/odbc/CMakeLists.txt new file mode 100644 index 0000000000..32e5c7ed69 --- /dev/null +++ b/src/connector/odbc/CMakeLists.txt @@ -0,0 +1,17 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +PROJECT(TDengine) + +AUX_SOURCE_DIRECTORY(src SRC) + +# generate dynamic library (*.so) +ADD_LIBRARY(todbc SHARED ${SRC}) +SET_TARGET_PROPERTIES(todbc PROPERTIES CLEAN_DIRECT_OUTPUT 1) +SET_TARGET_PROPERTIES(todbc PROPERTIES VERSION ${TD_VER_NUMBER} SOVERSION 1) +TARGET_LINK_LIBRARIES(todbc taos) + +install(CODE "execute_process(COMMAND sudo odbcinst -i -d -f ${CMAKE_CURRENT_SOURCE_DIR}/src/template.ini + COMMAND odbcinst -i -s -f ${CMAKE_CURRENT_SOURCE_DIR}/src/template.dsn + COMMAND echo odbc install done)") + +ADD_SUBDIRECTORY(tests) + diff --git a/src/connector/odbc/src/odbc_install.sh b/src/connector/odbc/src/odbc_install.sh new file mode 100644 index 0000000000..3ed8078a19 --- /dev/null +++ b/src/connector/odbc/src/odbc_install.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +odbcinst -u -d -n TAOS && +odbcinst -i -d -f "$(dirname "$0")/template.ini" && +odbcinst -i -s -f "$(dirname "$0")/template.dsn" && +echo yes + diff --git a/src/connector/odbc/src/template.dsn b/src/connector/odbc/src/template.dsn new file mode 100644 index 0000000000..3a90205464 --- /dev/null +++ b/src/connector/odbc/src/template.dsn @@ -0,0 +1,5 @@ +[TAOS_DSN] +Description=Connection to TAOS +Driver=TAOS +Server=localhost:1234 + diff --git a/src/connector/odbc/src/template.ini b/src/connector/odbc/src/template.ini new file mode 100644 index 0000000000..6e0e23154a --- /dev/null +++ b/src/connector/odbc/src/template.ini @@ -0,0 +1,4 @@ +[TAOS] +Description = taos odbc driver +Driver = /home/xxh/Documents/TDengine/debug/build/lib/libtodbc.so + diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c new file mode 100644 index 0000000000..fc102cbcd2 --- /dev/null +++ b/src/connector/odbc/src/todbc.c @@ -0,0 +1,426 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "taos.h" + +#include "os.h" + +#include +#include + +#include +#include +#include +#include + +#define D(fmt, ...) \ + fprintf(stderr, \ + "%s[%d]:%s() " fmt "\n", \ + basename((char*)__FILE__), __LINE__, __func__, \ + ##__VA_ARGS__) + +#define DASSERT(statement) \ +do { \ + if (statement) break; \ + D("Assertion failure: %s", #statement); \ + abort(); \ +} while (0) + +#define GET_REF(obj) atomic_load_64(&obj->refcount) +#define INC_REF(obj) atomic_add_fetch_64(&obj->refcount, 1) +#define DEC_REF(obj) atomic_sub_fetch_64(&obj->refcount, 1) + +#define LOCK(obj) pthread_mutex_lock(&obj->lock); +#define UNLOCK(obj) pthread_mutex_unlock(&obj->lock); + + +typedef struct env_s env_t; +typedef struct conn_s conn_t; +typedef struct sql_s sql_t; + + + +struct env_s { + unsigned int destroying:1; +}; + +struct conn_s { + env_t *env; + + TAOS *taos; +}; + +struct sql_s { + conn_t *conn; + + TAOS_RES *rs; + TAOS_ROW row; +}; + +static pthread_once_t init_once = PTHREAD_ONCE_INIT; +static void init_routine(void); + +static int do_field_display_size(TAOS_FIELD *field); +static void do_convert(SQLPOINTER TargetValue, SQLLEN BufferLength, SQLLEN *StrLen_or_Ind, TAOS_FIELD *field, void *row); + +SQLRETURN SQL_API SQLAllocEnv(SQLHENV *EnvironmentHandle) { + pthread_once(&init_once, init_routine); + + env_t *env = (env_t*)calloc(1, sizeof(*env)); + if (!env) return SQL_ERROR; + + *EnvironmentHandle = env; + + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLFreeEnv(SQLHENV EnvironmentHandle) { + env_t *env = (env_t*)EnvironmentHandle; + if (!env) return SQL_ERROR; + + DASSERT(!env->destroying); + + env->destroying = 1; + DASSERT(env->destroying == 1); + + free(env); + + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLAllocConnect(SQLHENV EnvironmentHandle, + SQLHDBC *ConnectionHandle) { + env_t *env = (env_t*)EnvironmentHandle; + if (!env) return SQL_ERROR; + + conn_t *conn = NULL; + do { + conn = (conn_t*)calloc(1, sizeof(*conn)); + if (!conn) break; + + conn->env = env; + *ConnectionHandle = conn; + } while (0); + + return conn ? SQL_SUCCESS : SQL_ERROR; +} + +SQLRETURN SQL_API SQLFreeConnect(SQLHDBC ConnectionHandle) { + conn_t *conn = (conn_t*)ConnectionHandle; + if (!conn) return SQL_ERROR; + + do { + if (conn->taos) { + taos_close(conn->taos); + conn->taos = NULL; + } + conn->env = NULL; + free(conn); + } while (0); + + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLConnect(SQLHDBC ConnectionHandle, + SQLCHAR *ServerName, SQLSMALLINT NameLength1, + SQLCHAR *UserName, SQLSMALLINT NameLength2, + SQLCHAR *Authentication, SQLSMALLINT NameLength3) { + conn_t *conn = (conn_t*)ConnectionHandle; + if (!conn) return SQL_ERROR; + + if (conn->taos) return SQL_ERROR; + + conn->taos = taos_connect("localhost", (const char*)UserName, (const char*)Authentication, NULL, 0); + + return conn->taos ? SQL_SUCCESS : SQL_ERROR; +} + +SQLRETURN SQL_API SQLDisconnect(SQLHDBC ConnectionHandle) { + conn_t *conn = (conn_t*)ConnectionHandle; + if (!conn) return SQL_ERROR; + + if (conn->taos) { + taos_close(conn->taos); + conn->taos = NULL; + } + + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLAllocStmt(SQLHDBC ConnectionHandle, + SQLHSTMT *StatementHandle) { + conn_t *conn = (conn_t*)ConnectionHandle; + if (!conn) return SQL_ERROR; + + sql_t *sql = (sql_t*)calloc(1, sizeof(*sql)); + if (!sql) return SQL_ERROR; + + sql->conn = conn; + *StatementHandle = sql; + + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle, + SQLUSMALLINT Option) { + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + + if (sql->rs) { + taos_free_result(sql->rs); + sql->rs = NULL; + } + sql->conn = NULL; + free(sql); + + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLExecDirect(SQLHSTMT StatementHandle, + SQLCHAR *StatementText, SQLINTEGER TextLength) { + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + if (!sql->conn) return SQL_ERROR; + if (!sql->conn->taos) return SQL_ERROR; + + if (sql->rs) { + taos_free_result(sql->rs); + sql->rs = NULL; + sql->row = NULL; + } + sql->rs = taos_query(sql->conn->taos, (const char*)StatementText); + if (sql->rs) { + sql->row = taos_fetch_row(sql->rs); + } + + return sql->row ? SQL_SUCCESS : SQL_NO_DATA; +} + +SQLRETURN SQL_API SQLNumResultCols(SQLHSTMT StatementHandle, + SQLSMALLINT *ColumnCount) { + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + if (!sql->conn) return SQL_ERROR; + if (!sql->conn->taos) return SQL_ERROR; + if (!sql->rs) return SQL_ERROR; + + int fields = taos_field_count(sql->rs); + if (ColumnCount) { + *ColumnCount = fields; + } + + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLRowCount(SQLHSTMT StatementHandle, + SQLLEN *RowCount) { + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + if (!sql->conn) return SQL_ERROR; + if (!sql->conn->taos) return SQL_ERROR; + if (!sql->rs) return SQL_ERROR; + int rows = taos_affected_rows(sql->rs); + if (RowCount) { + *RowCount = rows; + } + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLColAttribute(SQLHSTMT StatementHandle, + SQLUSMALLINT ColumnNumber, SQLUSMALLINT FieldIdentifier, + SQLPOINTER CharacterAttribute, SQLSMALLINT BufferLength, + SQLSMALLINT *StringLength, SQLLEN *NumericAttribute ) { + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + if (!sql->conn) return SQL_ERROR; + if (!sql->conn->taos) return SQL_ERROR; + if (!sql->rs) return SQL_ERROR; + int nfields = taos_field_count(sql->rs); + TAOS_FIELD *fields = taos_fetch_fields(sql->rs); + + if (nfields==0 || fields==NULL) return SQL_ERROR; + if (ColumnNumber>nfields) return SQL_ERROR; + + TAOS_FIELD *field = fields + ColumnNumber-1; + + switch (FieldIdentifier) { + case SQL_COLUMN_DISPLAY_SIZE: { + *NumericAttribute = do_field_display_size(field); + } break; + case SQL_COLUMN_LABEL: { + strncpy(CharacterAttribute, field->name, field->bytes); + } break; + default: { + return SQL_ERROR; + } break; + } + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLGetData(SQLHSTMT StatementHandle, + SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, + SQLPOINTER TargetValue, SQLLEN BufferLength, + SQLLEN *StrLen_or_Ind) { + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + if (!sql->conn) return SQL_ERROR; + if (!sql->conn->taos) return SQL_ERROR; + if (!sql->rs) return SQL_ERROR; + if (!sql->row) return SQL_ERROR; + int nfields = taos_field_count(sql->rs); + TAOS_FIELD *fields = taos_fetch_fields(sql->rs); + + if (nfields==0 || fields==NULL) return SQL_ERROR; + if (ColumnNumber>nfields) return SQL_ERROR; + + TAOS_FIELD *field = fields + ColumnNumber-1; + + switch (TargetType) { + case SQL_CHAR: { + do_convert(TargetValue, BufferLength, StrLen_or_Ind, field, sql->row[ColumnNumber-1]); + *StrLen_or_Ind = SQL_NTS; + } break; + default: { + return SQL_ERROR; + } break; + } + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLFetch(SQLHSTMT StatementHandle) { + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + if (!sql->conn) return SQL_ERROR; + if (!sql->conn->taos) return SQL_ERROR; + if (!sql->rs) return SQL_ERROR; + sql->row = taos_fetch_row(sql->rs); + return sql->row ? SQL_SUCCESS : SQL_NO_DATA; +} + +SQLRETURN SQL_API SQLPrepare(SQLHSTMT StatementHandle, + SQLCHAR *StatementText, SQLINTEGER TextLength) { + return SQL_ERROR; +} + +SQLRETURN SQL_API SQLExecute(SQLHSTMT StatementHandle) { + return SQL_ERROR; +} + +static void init_routine(void) { + taos_init(); +} + +static int do_field_display_size(TAOS_FIELD *field) { + switch (field->type) { + case TSDB_DATA_TYPE_TINYINT: + return 5; + break; + + case TSDB_DATA_TYPE_SMALLINT: + return 7; + break; + + case TSDB_DATA_TYPE_INT: + return 12; + break; + + case TSDB_DATA_TYPE_BIGINT: + return 22; + break; + + case TSDB_DATA_TYPE_FLOAT: { + return 12; + } break; + + case TSDB_DATA_TYPE_DOUBLE: { + return 12; + } break; + + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + return 3*(field->bytes - VARSTR_HEADER_SIZE) + 2; + } break; + + case TSDB_DATA_TYPE_TIMESTAMP: + return 22; + break; + + case TSDB_DATA_TYPE_BOOL: + return 7; + default: + break; + } + + return 10; +} + +static void do_convert(SQLPOINTER TargetValue, SQLLEN BufferLength, SQLLEN *StrLen_or_Ind, TAOS_FIELD *field, void *row) { + switch (field->type) { + case TSDB_DATA_TYPE_TINYINT: + snprintf((char*)TargetValue, BufferLength, "%d", *((int8_t *)row)); + break; + + case TSDB_DATA_TYPE_SMALLINT: + snprintf((char*)TargetValue, BufferLength, "%d", *((int16_t *)row)); + break; + + case TSDB_DATA_TYPE_INT: + snprintf((char*)TargetValue, BufferLength, "%d", *((int32_t *)row)); + break; + + case TSDB_DATA_TYPE_BIGINT: + snprintf((char*)TargetValue, BufferLength, "%" PRId64, *((int64_t *)row)); + break; + + case TSDB_DATA_TYPE_FLOAT: { + float fv = 0; + fv = GET_FLOAT_VAL(row); + snprintf((char*)TargetValue, BufferLength, "%f", fv); + } break; + + case TSDB_DATA_TYPE_DOUBLE: { + double dv = 0; + dv = GET_DOUBLE_VAL(row); + snprintf((char*)TargetValue, BufferLength, "%lf", dv); + } break; + + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + size_t xlen = 0; + char *p = (char*)TargetValue; + size_t n = BufferLength; + for (xlen = 0; xlen < field->bytes - VARSTR_HEADER_SIZE; xlen++) { + char c = ((char *)row)[xlen]; + if (c == 0) break; + int v = snprintf(p, n, "%c", c); + p += v; + n -= v; + if (n<=0) break; + } + if (n>0) *p = '\0'; + ((char*)TargetValue)[BufferLength-1] = '\0'; + } break; + + case TSDB_DATA_TYPE_TIMESTAMP: + snprintf((char*)TargetValue, BufferLength, "%" PRId64, *((int64_t *)row)); + break; + + case TSDB_DATA_TYPE_BOOL: + snprintf((char*)TargetValue, BufferLength, "%d", *((int8_t *)row)); + default: + break; + } +} + diff --git a/src/connector/odbc/tests/CMakeLists.txt b/src/connector/odbc/tests/CMakeLists.txt new file mode 100644 index 0000000000..ac57a5647f --- /dev/null +++ b/src/connector/odbc/tests/CMakeLists.txt @@ -0,0 +1,7 @@ +PROJECT(TDengine) + +IF (TD_LINUX) + AUX_SOURCE_DIRECTORY(. SRC) + ADD_EXECUTABLE(tcodbc main.c) + TARGET_LINK_LIBRARIES(tcodbc odbc) +ENDIF () diff --git a/src/connector/odbc/tests/main.c b/src/connector/odbc/tests/main.c new file mode 100644 index 0000000000..3924fe9077 --- /dev/null +++ b/src/connector/odbc/tests/main.c @@ -0,0 +1,20 @@ +#include + +#include + + +int main(void) { + SQLRETURN r; + SQLHENV env = {0}; + SQLHDBC conn = {0}; + r = SQLAllocEnv(&env); + if (r!=SQL_SUCCESS) return 1; + do { + r = SQLAllocConnect(env, &conn); + if (r!=SQL_SUCCESS) break; + SQLFreeConnect(conn); + } while (0); + SQLFreeEnv(env); + return r ? 1 : 0; +} + diff --git a/src/os/inc/osDef.h b/src/os/inc/osDef.h index 81c70a58fd..d718bef6da 100644 --- a/src/os/inc/osDef.h +++ b/src/os/inc/osDef.h @@ -27,7 +27,7 @@ extern "C" { #define FD_VALID(x) ((x) > STDERR_FILENO) #define FD_INITIALIZER ((int32_t)-1) -#define WCHAR wchar_t +// #define WCHAR wchar_t #define POINTER_SHIFT(p, b) ((void *)((char *)(p) + (b))) #define POINTER_DISTANCE(p1, p2) ((char *)(p1) - (char *)(p2)) From ec04ef7b79a87e6d0a5f33ea8a3dac1fb5188e1e Mon Sep 17 00:00:00 2001 From: freemine Date: Wed, 7 Oct 2020 08:08:34 +0800 Subject: [PATCH 004/101] delay fetch --- src/connector/odbc/src/todbc.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index fc102cbcd2..fdacb2128f 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -201,11 +201,8 @@ SQLRETURN SQL_API SQLExecDirect(SQLHSTMT StatementHandle, sql->row = NULL; } sql->rs = taos_query(sql->conn->taos, (const char*)StatementText); - if (sql->rs) { - sql->row = taos_fetch_row(sql->rs); - } - return sql->row ? SQL_SUCCESS : SQL_NO_DATA; + return sql->rs ? SQL_SUCCESS : SQL_NO_DATA; } SQLRETURN SQL_API SQLNumResultCols(SQLHSTMT StatementHandle, From 36e5dac0eba732328ec9d8a223cabb0abf162bd9 Mon Sep 17 00:00:00 2001 From: freemine Date: Wed, 7 Oct 2020 08:38:07 +0800 Subject: [PATCH 005/101] add refcount --- src/connector/odbc/src/todbc.c | 57 +++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index fdacb2128f..dea549f5e5 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -53,16 +53,19 @@ typedef struct sql_s sql_t; struct env_s { + uint64_t refcount; unsigned int destroying:1; }; struct conn_s { + uint64_t refcount; env_t *env; TAOS *taos; }; struct sql_s { + uint64_t refcount; conn_t *conn; TAOS_RES *rs; @@ -81,6 +84,8 @@ SQLRETURN SQL_API SQLAllocEnv(SQLHENV *EnvironmentHandle) { env_t *env = (env_t*)calloc(1, sizeof(*env)); if (!env) return SQL_ERROR; + DASSERT(INC_REF(env)>0); + *EnvironmentHandle = env; return SQL_SUCCESS; @@ -90,11 +95,15 @@ SQLRETURN SQL_API SQLFreeEnv(SQLHENV EnvironmentHandle) { env_t *env = (env_t*)EnvironmentHandle; if (!env) return SQL_ERROR; + DASSERT(GET_REF(env)==1); + DASSERT(!env->destroying); env->destroying = 1; DASSERT(env->destroying == 1); + DASSERT(DEC_REF(env)==0); + free(env); return SQL_SUCCESS; @@ -105,6 +114,8 @@ SQLRETURN SQL_API SQLAllocConnect(SQLHENV EnvironmentHandle, env_t *env = (env_t*)EnvironmentHandle; if (!env) return SQL_ERROR; + DASSERT(INC_REF(env)>1); + conn_t *conn = NULL; do { conn = (conn_t*)calloc(1, sizeof(*conn)); @@ -112,20 +123,34 @@ SQLRETURN SQL_API SQLAllocConnect(SQLHENV EnvironmentHandle, conn->env = env; *ConnectionHandle = conn; + + DASSERT(INC_REF(conn)>0); + + return SQL_SUCCESS; } while (0); - return conn ? SQL_SUCCESS : SQL_ERROR; + DASSERT(DEC_REF(env)>0); + + return SQL_ERROR; } SQLRETURN SQL_API SQLFreeConnect(SQLHDBC ConnectionHandle) { conn_t *conn = (conn_t*)ConnectionHandle; if (!conn) return SQL_ERROR; + DASSERT(GET_REF(conn)==1); + + DASSERT(conn->env); + do { if (conn->taos) { taos_close(conn->taos); conn->taos = NULL; } + + DASSERT(DEC_REF(conn->env)>0); + DASSERT(DEC_REF(conn)==0); + conn->env = NULL; free(conn); } while (0); @@ -142,8 +167,9 @@ SQLRETURN SQL_API SQLConnect(SQLHDBC ConnectionHandle, if (conn->taos) return SQL_ERROR; + // TODO: data-race conn->taos = taos_connect("localhost", (const char*)UserName, (const char*)Authentication, NULL, 0); - + return conn->taos ? SQL_SUCCESS : SQL_ERROR; } @@ -164,13 +190,23 @@ SQLRETURN SQL_API SQLAllocStmt(SQLHDBC ConnectionHandle, conn_t *conn = (conn_t*)ConnectionHandle; if (!conn) return SQL_ERROR; - sql_t *sql = (sql_t*)calloc(1, sizeof(*sql)); - if (!sql) return SQL_ERROR; + DASSERT(INC_REF(conn)>1); - sql->conn = conn; - *StatementHandle = sql; + do { + sql_t *sql = (sql_t*)calloc(1, sizeof(*sql)); + if (!sql) break; - return SQL_SUCCESS; + sql->conn = conn; + DASSERT(INC_REF(sql)>0); + + *StatementHandle = sql; + + return SQL_SUCCESS; + } while (0); + + DASSERT(DEC_REF(conn)>0); + + return SQL_ERROR; } SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle, @@ -178,11 +214,18 @@ SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle, sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; + DASSERT(GET_REF(sql)==1); + if (sql->rs) { taos_free_result(sql->rs); sql->rs = NULL; } + + DASSERT(DEC_REF(sql->conn)>0); + DASSERT(DEC_REF(sql)==0); + sql->conn = NULL; + free(sql); return SQL_SUCCESS; From dc2df3a09e6e62e5839036158be842988d67b565 Mon Sep 17 00:00:00 2001 From: freemine Date: Wed, 7 Oct 2020 17:42:54 +0800 Subject: [PATCH 006/101] add Error support --- src/CMakeLists.txt | 2 +- src/connector/odbc/src/todbc.c | 242 +++++++++++++++++++++++++++++++-- 2 files changed, 235 insertions(+), 9 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f619edd221..2ed56d4f81 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,5 +21,5 @@ ADD_SUBDIRECTORY(wal) ADD_SUBDIRECTORY(cq) ADD_SUBDIRECTORY(dnode) ADD_SUBDIRECTORY(connector/odbc) -ADD_SUBDIRECTORY(connector/jdbc) +# ADD_SUBDIRECTORY(connector/jdbc) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index dea549f5e5..4b2c767f51 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -16,6 +16,7 @@ #include "taos.h" #include "os.h" +#include "taoserror.h" #include #include @@ -45,16 +46,73 @@ do { \ #define LOCK(obj) pthread_mutex_lock(&obj->lock); #define UNLOCK(obj) pthread_mutex_unlock(&obj->lock); +#define SET_ERROR(obj, sqlstate, eno, err_fmt, ...) \ +do { \ + obj->err.err_no = eno; \ + const char* estr = tstrerror(eno); \ + if (!estr) estr = "Unknown error"; \ + int n = snprintf(NULL, 0, "[%x]%s: " err_fmt "", eno, estr, ##__VA_ARGS__); \ + if (n<0) break; \ + char *err_str = (char*)realloc(obj->err.err_str, n+1); \ + if (!err_str) break; \ + obj->err.err_str = err_str; \ + snprintf(obj->err.err_str, n+1, "[%x]%s: " err_fmt "", eno, estr, ##__VA_ARGS__); \ + snprintf((char*)obj->err.sql_state, sizeof(obj->err.sql_state), "%s", sqlstate); \ +} while (0) + +#define CLR_ERROR(obj) \ +do { \ + obj->err.err_no = TSDB_CODE_SUCCESS; \ + if (obj->err.err_str) obj->err.err_str[0] = '\0'; \ + obj->err.sql_state[0] = '\0'; \ +} while (0) + +#define FILL_ERROR(obj) \ +do { \ + size_t n = sizeof(obj->err.sql_state); \ + if (Sqlstate) strncpy((char*)Sqlstate, (char*)obj->err.sql_state, n); \ + if (NativeError) *NativeError = obj->err.err_no; \ + snprintf((char*)MessageText, BufferLength, "%s", obj->err.err_str); \ + if (TextLength && obj->err.err_str) *TextLength = strlen(obj->err.err_str); \ +} while (0) + +#define FREE_ERROR(obj) \ +do { \ + obj->err.err_no = TSDB_CODE_SUCCESS; \ + if (obj->err.err_str) { \ + free(obj->err.err_str); \ + obj->err.err_str = NULL; \ + } \ + obj->err.sql_state[0] = '\0'; \ +} while (0) + +#define SDUP(s,n) (s ? (s[n] ? (const char*)strndup((const char*)s,n) : (const char*)s) : strdup("")) +#define SFRE(x,s,n) \ +do { \ + if (x==(const char*)s) break; \ + if (x) { \ + free((char*)x); \ + x = NULL; \ + } \ +} while (0) typedef struct env_s env_t; typedef struct conn_s conn_t; typedef struct sql_s sql_t; +typedef struct taos_error_s taos_error_t; +struct taos_error_s { + char *err_str; + int err_no; + SQLCHAR sql_state[6]; +}; struct env_s { uint64_t refcount; unsigned int destroying:1; + + taos_error_t err; }; struct conn_s { @@ -62,14 +120,19 @@ struct conn_s { env_t *env; TAOS *taos; + + taos_error_t err; }; struct sql_s { uint64_t refcount; conn_t *conn; + TAOS_STMT *stmt; TAOS_RES *rs; TAOS_ROW row; + + taos_error_t err; }; static pthread_once_t init_once = PTHREAD_ONCE_INIT; @@ -88,6 +151,7 @@ SQLRETURN SQL_API SQLAllocEnv(SQLHENV *EnvironmentHandle) { *EnvironmentHandle = env; + CLR_ERROR(env); return SQL_SUCCESS; } @@ -104,6 +168,7 @@ SQLRETURN SQL_API SQLFreeEnv(SQLHENV EnvironmentHandle) { DASSERT(DEC_REF(env)==0); + FREE_ERROR(env); free(env); return SQL_SUCCESS; @@ -119,7 +184,10 @@ SQLRETURN SQL_API SQLAllocConnect(SQLHENV EnvironmentHandle, conn_t *conn = NULL; do { conn = (conn_t*)calloc(1, sizeof(*conn)); - if (!conn) break; + if (!conn) { + SET_ERROR(env, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to alloc connection handle"); + break; + } conn->env = env; *ConnectionHandle = conn; @@ -152,6 +220,7 @@ SQLRETURN SQL_API SQLFreeConnect(SQLHDBC ConnectionHandle) { DASSERT(DEC_REF(conn)==0); conn->env = NULL; + FREE_ERROR(conn); free(conn); } while (0); @@ -161,14 +230,36 @@ SQLRETURN SQL_API SQLFreeConnect(SQLHDBC ConnectionHandle) { SQLRETURN SQL_API SQLConnect(SQLHDBC ConnectionHandle, SQLCHAR *ServerName, SQLSMALLINT NameLength1, SQLCHAR *UserName, SQLSMALLINT NameLength2, - SQLCHAR *Authentication, SQLSMALLINT NameLength3) { + SQLCHAR *Authentication, SQLSMALLINT NameLength3) { conn_t *conn = (conn_t*)ConnectionHandle; if (!conn) return SQL_ERROR; - if (conn->taos) return SQL_ERROR; + if (conn->taos) { + SET_ERROR(conn, "HY000", TSDB_CODE_TSC_APP_ERROR, "connection still in use"); + return SQL_ERROR; + } - // TODO: data-race - conn->taos = taos_connect("localhost", (const char*)UserName, (const char*)Authentication, NULL, 0); + const char *serverName = SDUP(ServerName, NameLength1); + const char *userName = SDUP(UserName, NameLength2); + const char *auth = SDUP(Authentication, NameLength3); + + do { + if (!serverName || !userName || !auth) { + SET_ERROR(conn, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to connect to database"); + break; + } + // TODO: data-race + // TODO: shall receive ip/port from odbc.ini + conn->taos = taos_connect("localhost", userName, auth, NULL, 0); + if (!conn->taos) { + SET_ERROR(conn, "HY000", terrno, "failed to connect to database"); + break; + } + } while (0); + + SFRE(serverName, ServerName, NameLength1); + SFRE(userName, UserName, NameLength2); + SFRE(auth, Authentication, NameLength3); return conn->taos ? SQL_SUCCESS : SQL_ERROR; } @@ -194,7 +285,10 @@ SQLRETURN SQL_API SQLAllocStmt(SQLHDBC ConnectionHandle, do { sql_t *sql = (sql_t*)calloc(1, sizeof(*sql)); - if (!sql) break; + if (!sql) { + SET_ERROR(conn, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to alloc statement handle"); + break; + } sql->conn = conn; DASSERT(INC_REF(sql)>0); @@ -214,6 +308,12 @@ SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle, sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; + if (Option != SQL_DROP) { + D("Option: [%d][%x]", Option, Option); + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "failed to free statement"); + return SQL_ERROR; + } + DASSERT(GET_REF(sql)==1); if (sql->rs) { @@ -221,11 +321,17 @@ SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle, sql->rs = NULL; } + if (sql->stmt) { + taos_stmt_close(sql->stmt); + sql->stmt = NULL; + } + DASSERT(DEC_REF(sql->conn)>0); DASSERT(DEC_REF(sql)==0); sql->conn = NULL; + FREE_ERROR(sql); free(sql); return SQL_SUCCESS; @@ -243,7 +349,27 @@ SQLRETURN SQL_API SQLExecDirect(SQLHSTMT StatementHandle, sql->rs = NULL; sql->row = NULL; } - sql->rs = taos_query(sql->conn->taos, (const char*)StatementText); + + if (sql->stmt) { + taos_stmt_close(sql->stmt); + sql->stmt = NULL; + } + + const char *stxt = SDUP(StatementText, TextLength); + + do { + if (!stxt) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to query"); + break; + } + sql->rs = taos_query(sql->conn->taos, stxt); + if (!sql->rs) { + SET_ERROR(sql, "HY000", terrno, "failed to query"); + break; + } + } while (0); + + SFRE(stxt, StatementText, TextLength); return sql->rs ? SQL_SUCCESS : SQL_NO_DATA; } @@ -351,13 +477,113 @@ SQLRETURN SQL_API SQLFetch(SQLHSTMT StatementHandle) { SQLRETURN SQL_API SQLPrepare(SQLHSTMT StatementHandle, SQLCHAR *StatementText, SQLINTEGER TextLength) { - return SQL_ERROR; + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + if (!sql->conn) return SQL_ERROR; + if (!sql->conn->taos) return SQL_ERROR; + + if (sql->rs) { + taos_free_result(sql->rs); + sql->rs = NULL; + sql->row = NULL; + } + + if (sql->stmt) { + taos_stmt_close(sql->stmt); + sql->stmt = NULL; + } + + do { + sql->stmt = taos_stmt_init(sql->conn->taos); + if (!sql->stmt) { + SET_ERROR(sql, "HY000", terrno, "failed to initialize statement internally"); + break; + } + + int r = taos_stmt_prepare(sql->stmt, (const char *)StatementText, TextLength); + if (r) { + SET_ERROR(sql, "HY000", r, "failed to prepare a statement"); + taos_stmt_close(sql->stmt); + sql->stmt = NULL; + break; + } + } while (0); + + return sql->stmt ? SQL_SUCCESS : SQL_ERROR; } SQLRETURN SQL_API SQLExecute(SQLHSTMT StatementHandle) { + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + if (!sql->conn) return SQL_ERROR; + if (!sql->conn->taos) return SQL_ERROR; + if (!sql->stmt) return SQL_ERROR; + + if (sql->rs) { + taos_free_result(sql->rs); + sql->rs = NULL; + sql->row = NULL; + } + + int r = 0; + + r = taos_stmt_execute(sql->stmt); + if (r) { + SET_ERROR(sql, "HY000", r, "failed to execute statement"); + return SQL_ERROR; + } + + sql->rs = taos_stmt_use_result(sql->stmt); + if (!sql->rs) { + SET_ERROR(sql, "HY000", r, "failed to fetch result"); + return SQL_ERROR; + } + + return sql->rs ? SQL_SUCCESS : SQL_ERROR; +} + +SQLRETURN SQL_API SQLGetDiagField(SQLSMALLINT HandleType, SQLHANDLE Handle, + SQLSMALLINT RecNumber, SQLSMALLINT DiagIdentifier, + SQLPOINTER DiagInfo, SQLSMALLINT BufferLength, + SQLSMALLINT *StringLength) { + // if this function is not exported, isql will never call SQLGetDiagRec return SQL_ERROR; } +SQLRETURN SQL_API SQLGetDiagRec(SQLSMALLINT HandleType, SQLHANDLE Handle, + SQLSMALLINT RecNumber, SQLCHAR *Sqlstate, + SQLINTEGER *NativeError, SQLCHAR *MessageText, + SQLSMALLINT BufferLength, SQLSMALLINT *TextLength) { + if (RecNumber>1) return SQL_NO_DATA; + switch (HandleType) { + case SQL_HANDLE_ENV: { + env_t *env = (env_t*)Handle; + if (!env) break; + FILL_ERROR(env); + return SQL_SUCCESS; + } break; + case SQL_HANDLE_DBC: { + conn_t *conn = (conn_t*)Handle; + if (!conn) break; + FILL_ERROR(conn); + return SQL_SUCCESS; + } break; + case SQL_HANDLE_STMT: { + sql_t *sql = (sql_t*)Handle; + if (!sql) break; + FILL_ERROR(sql); + return SQL_SUCCESS; + } break; + default: { + } break; + } + + return SQL_ERROR; +} + + + + static void init_routine(void) { taos_init(); } From 12eafdd8f31fa807a918d871721cf186afe0fa15 Mon Sep 17 00:00:00 2001 From: freemine Date: Thu, 8 Oct 2020 12:11:20 +0800 Subject: [PATCH 007/101] add prepare/bind/execute support --- src/connector/odbc/src/todbc.c | 608 ++++++++++++++++++++++++++++++-- src/connector/odbc/tests/main.c | 183 +++++++++- 2 files changed, 764 insertions(+), 27 deletions(-) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 4b2c767f51..58659a2346 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -46,18 +46,18 @@ do { \ #define LOCK(obj) pthread_mutex_lock(&obj->lock); #define UNLOCK(obj) pthread_mutex_unlock(&obj->lock); -#define SET_ERROR(obj, sqlstate, eno, err_fmt, ...) \ -do { \ - obj->err.err_no = eno; \ - const char* estr = tstrerror(eno); \ - if (!estr) estr = "Unknown error"; \ - int n = snprintf(NULL, 0, "[%x]%s: " err_fmt "", eno, estr, ##__VA_ARGS__); \ - if (n<0) break; \ - char *err_str = (char*)realloc(obj->err.err_str, n+1); \ - if (!err_str) break; \ - obj->err.err_str = err_str; \ - snprintf(obj->err.err_str, n+1, "[%x]%s: " err_fmt "", eno, estr, ##__VA_ARGS__); \ - snprintf((char*)obj->err.sql_state, sizeof(obj->err.sql_state), "%s", sqlstate); \ +#define SET_ERROR(obj, sqlstate, eno, err_fmt, ...) \ +do { \ + obj->err.err_no = eno; \ + const char* estr = tstrerror(eno); \ + if (!estr) estr = "Unknown error"; \ + int n = snprintf(NULL, 0, "@[%d][%x]%s: " err_fmt "", __LINE__, eno, estr, ##__VA_ARGS__); \ + if (n<0) break; \ + char *err_str = (char*)realloc(obj->err.err_str, n+1); \ + if (!err_str) break; \ + obj->err.err_str = err_str; \ + snprintf(obj->err.err_str, n+1, "@[%d][%x]%s: " err_fmt "", __LINE__, eno, estr, ##__VA_ARGS__); \ + snprintf((char*)obj->err.sql_state, sizeof(obj->err.sql_state), "%s", sqlstate); \ } while (0) #define CLR_ERROR(obj) \ @@ -96,10 +96,29 @@ do { \ } \ } while (0) +#define CHK_RS(r_091c, sql_091c, fmt_091c, ...) \ +do { \ + r_091c = SQL_ERROR; \ + int e = sql_091c->rs ? taos_errno(sql_091c->rs) : terrno; \ + if (e != TSDB_CODE_SUCCESS) { \ + SET_ERROR(sql_091c, "HY000", e, fmt_091c, ##__VA_ARGS__); \ + break; \ + } \ + r_091c = SQL_SUCCESS; \ +} while (0) + typedef struct env_s env_t; typedef struct conn_s conn_t; typedef struct sql_s sql_t; typedef struct taos_error_s taos_error_t; +typedef struct param_bind_s param_bind_t; + +struct param_bind_s { + SQLUSMALLINT ParameterNumber; + SQLPOINTER ParameterValue; + SQLLEN *StrLen_or_Ind; + unsigned int valid; +}; struct taos_error_s { char *err_str; @@ -129,6 +148,9 @@ struct sql_s { conn_t *conn; TAOS_STMT *stmt; + TAOS_BIND *binds; + param_bind_t *params; + int n_params; TAOS_RES *rs; TAOS_ROW row; @@ -309,7 +331,6 @@ SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle, if (!sql) return SQL_ERROR; if (Option != SQL_DROP) { - D("Option: [%d][%x]", Option, Option); SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "failed to free statement"); return SQL_ERROR; } @@ -326,6 +347,17 @@ SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle, sql->stmt = NULL; } + if (sql->binds) { + free(sql->binds); + sql->binds = NULL; + } + if (sql->params) { + free(sql->params); + sql->params = NULL; + } + sql->n_params = 0; + + DASSERT(DEC_REF(sql->conn)>0); DASSERT(DEC_REF(sql)==0); @@ -355,23 +387,32 @@ SQLRETURN SQL_API SQLExecDirect(SQLHSTMT StatementHandle, sql->stmt = NULL; } + if (sql->binds) { + free(sql->binds); + sql->binds = NULL; + } + + if (sql->params) { + free(sql->params); + sql->params = NULL; + } + sql->n_params = 0; + const char *stxt = SDUP(StatementText, TextLength); + SQLRETURN r = SQL_ERROR; do { if (!stxt) { SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to query"); break; } sql->rs = taos_query(sql->conn->taos, stxt); - if (!sql->rs) { - SET_ERROR(sql, "HY000", terrno, "failed to query"); - break; - } + CHK_RS(r, sql, "failed to query"); } while (0); SFRE(stxt, StatementText, TextLength); - return sql->rs ? SQL_SUCCESS : SQL_NO_DATA; + return r; } SQLRETURN SQL_API SQLNumResultCols(SQLHSTMT StatementHandle, @@ -455,8 +496,12 @@ SQLRETURN SQL_API SQLGetData(SQLHSTMT StatementHandle, switch (TargetType) { case SQL_CHAR: { - do_convert(TargetValue, BufferLength, StrLen_or_Ind, field, sql->row[ColumnNumber-1]); - *StrLen_or_Ind = SQL_NTS; + if (sql->row[ColumnNumber-1]) { + do_convert(TargetValue, BufferLength, StrLen_or_Ind, field, sql->row[ColumnNumber-1]); + *StrLen_or_Ind = SQL_NTS; + } else { + *StrLen_or_Ind = SQL_NULL_DATA; + } } break; default: { return SQL_ERROR; @@ -493,6 +538,16 @@ SQLRETURN SQL_API SQLPrepare(SQLHSTMT StatementHandle, sql->stmt = NULL; } + if (sql->binds) { + free(sql->binds); + sql->binds = NULL; + } + if (sql->params) { + free(sql->params); + sql->params = NULL; + } + sql->n_params = 0; + do { sql->stmt = taos_stmt_init(sql->conn->taos); if (!sql->stmt) { @@ -527,19 +582,83 @@ SQLRETURN SQL_API SQLExecute(SQLHSTMT StatementHandle) { int r = 0; + for (int i=0; in_params; ++i) { + param_bind_t *pb = sql->params + i; + if (!pb->valid) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "default parameter [@%d] not supported yet", i+1); + return SQL_ERROR; + } + TAOS_BIND *b = sql->binds + i; + int yes = 1; + int no = 0; + if (pb->StrLen_or_Ind && *pb->StrLen_or_Ind == SQL_NULL_DATA) { + b->is_null = &yes; + } else { + b->is_null = &no; + switch (b->buffer_type) { + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_SMALLINT: + case TSDB_DATA_TYPE_INT: + case TSDB_DATA_TYPE_BIGINT: + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_TIMESTAMP: { + b->length = &b->buffer_length; + b->buffer = pb->ParameterValue; + } break; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + if (!pb->StrLen_or_Ind) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "value [@%d] bad StrLen_or_Ind", i+1); + return SQL_ERROR; + } + size_t n = *pb->StrLen_or_Ind; + if (n == SQL_NTS) { + n = strlen(pb->ParameterValue); + } else if (n < 0 || n > b->buffer_length) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "value [@%d] bad StrLen_or_Ind", i+1); + return SQL_ERROR; + } + + b->buffer_length = n; + b->length = &b->buffer_length; + b->buffer = pb->ParameterValue; + } break; + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "value [@%d] not supported yet", i+1); + return SQL_ERROR; + } break; + } + } + } + + if (sql->n_params > 0) { + r = taos_stmt_bind_param(sql->stmt, sql->binds); + if (r) { + SET_ERROR(sql, "HY000", r, "failed to bind parameters"); + return SQL_ERROR; + } + + r = taos_stmt_add_batch(sql->stmt); + if (r) { + SET_ERROR(sql, "HY000", r, "failed to add batch"); + return SQL_ERROR; + } + } + r = taos_stmt_execute(sql->stmt); if (r) { SET_ERROR(sql, "HY000", r, "failed to execute statement"); return SQL_ERROR; } - sql->rs = taos_stmt_use_result(sql->stmt); - if (!sql->rs) { - SET_ERROR(sql, "HY000", r, "failed to fetch result"); - return SQL_ERROR; - } + SQLRETURN ret = SQL_ERROR; - return sql->rs ? SQL_SUCCESS : SQL_ERROR; + sql->rs = taos_stmt_use_result(sql->stmt); + CHK_RS(ret, sql, "failed to use result"); + + return ret; } SQLRETURN SQL_API SQLGetDiagField(SQLSMALLINT HandleType, SQLHANDLE Handle, @@ -581,6 +700,443 @@ SQLRETURN SQL_API SQLGetDiagRec(SQLSMALLINT HandleType, SQLHANDLE Handle, return SQL_ERROR; } +SQLRETURN SQL_API SQLBindParameter( + SQLHSTMT StatementHandle, + SQLUSMALLINT ParameterNumber, + SQLSMALLINT fParamType, + SQLSMALLINT ValueType, + SQLSMALLINT ParameterType, + SQLULEN LengthPrecision, + SQLSMALLINT ParameterScale, + SQLPOINTER ParameterValue, + SQLLEN cbValueMax, // ignore for now, since only SQL_PARAM_INPUT is supported now + SQLLEN *StrLen_or_Ind) { + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + if (!sql->conn) return SQL_ERROR; + if (!sql->conn->taos) return SQL_ERROR; + if (!sql->stmt) return SQL_ERROR; + + if (fParamType != SQL_PARAM_INPUT) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "non-input parameter [@%d] not supported yet", ParameterNumber); + return SQL_ERROR; + } + switch (ParameterType) { + case SQL_BIT: { // TSDB_DATA_TYPE_BOOL + if (ValueType!=SQL_C_BIT) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); + return SQL_ERROR; + } + // LengthPrecision ignored; + // ParameterScale ignored; + // if (LengthPrecision != sizeof(v.b)) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); + // return SQL_ERROR; + // } + // if (ParameterScale != 0) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); + // return SQL_ERROR; + // } + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + b->buffer_type = TSDB_DATA_TYPE_BOOL; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + pb->valid = 1; + } break; + case SQL_TINYINT: { // TSDB_DATA_TYPE_TINYINT + if (ValueType!=SQL_C_TINYINT) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); + return SQL_ERROR; + } + // LengthPrecision ignored; + // ParameterScale ignored; + // if (LengthPrecision != sizeof(v.v1)) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); + // return SQL_ERROR; + // } + // if (ParameterScale != 0) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); + // return SQL_ERROR; + // } + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + b->buffer_type = TSDB_DATA_TYPE_TINYINT; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + pb->valid = 1; + } break; + case SQL_SMALLINT: { // TSDB_DATA_TYPE_SMALLINT + if (ValueType!=SQL_C_SHORT) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); + return SQL_ERROR; + } + // LengthPrecision ignored; + // ParameterScale ignored; + // if (LengthPrecision != sizeof(v.v2)) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); + // return SQL_ERROR; + // } + // if (ParameterScale != 0) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); + // return SQL_ERROR; + // } + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + b->buffer_type = TSDB_DATA_TYPE_SMALLINT; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + pb->valid = 1; + } break; + case SQL_INTEGER: { // TSDB_DATA_TYPE_INT + if (ValueType!=SQL_C_LONG) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); + return SQL_ERROR; + } + // LengthPrecision ignored; + // ParameterScale ignored; + // if (LengthPrecision != sizeof(v.v4)) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); + // return SQL_ERROR; + // } + // if (ParameterScale != 0) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); + // return SQL_ERROR; + // } + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + b->buffer_type = TSDB_DATA_TYPE_INT; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + pb->valid = 1; + } break; + case SQL_BIGINT: { // TSDB_DATA_TYPE_BIGINT + if (ValueType!=SQL_C_SBIGINT) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); + return SQL_ERROR; + } + // LengthPrecision ignored; + // ParameterScale ignored; + // if (LengthPrecision != sizeof(v.v8)) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); + // return SQL_ERROR; + // } + // if (ParameterScale != 0) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); + // return SQL_ERROR; + // } + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + b->buffer_type = TSDB_DATA_TYPE_BIGINT; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + pb->valid = 1; + } break; + case SQL_FLOAT: { // TSDB_DATA_TYPE_FLOAT + if (ValueType!=SQL_C_FLOAT) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); + return SQL_ERROR; + } + // LengthPrecision ignored; + // ParameterScale ignored; + // if (LengthPrecision != sizeof(v.f4)) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); + // return SQL_ERROR; + // } + // if (ParameterScale != 0) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); + // return SQL_ERROR; + // } + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + b->buffer_type = TSDB_DATA_TYPE_FLOAT; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + pb->valid = 1; + } break; + case SQL_DOUBLE: { // TSDB_DATA_TYPE_DOUBLE + if (ValueType!=SQL_C_DOUBLE) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); + return SQL_ERROR; + } + // LengthPrecision ignored; + // ParameterScale ignored; + // if (LengthPrecision != sizeof(v.f8)) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); + // return SQL_ERROR; + // } + // if (ParameterScale != 0) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); + // return SQL_ERROR; + // } + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + b->buffer_type = TSDB_DATA_TYPE_DOUBLE; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + pb->valid = 1; + } break; + case SQL_TIMESTAMP: { // TSDB_DATA_TYPE_TIMESTAMP + if (ValueType!=SQL_C_SBIGINT) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); + return SQL_ERROR; + } + // LengthPrecision ignored; + // ParameterScale ignored; + // if (LengthPrecision != sizeof(v.v8)) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); + // return SQL_ERROR; + // } + // if (ParameterScale != 0) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); + // return SQL_ERROR; + // } + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + b->buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + pb->valid = 1; + } break; + case SQL_VARBINARY: { // TSDB_DATA_TYPE_BINARY + if (ValueType!=SQL_C_BINARY) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); + return SQL_ERROR; + } + if (LengthPrecision <=0) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); + return SQL_ERROR; + } + // ParameterScale ignored; + // if (ParameterScale != 0) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); + // return SQL_ERROR; + // } + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + b->buffer_type = TSDB_DATA_TYPE_BINARY; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + pb->valid = 1; + } break; + case SQL_VARCHAR: { // TSDB_DATA_TYPE_NCHAR + if (ValueType!=SQL_C_CHAR) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); + return SQL_ERROR; + } + if (LengthPrecision <=0) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); + return SQL_ERROR; + } + // ParameterScale ignored; + // if (ParameterScale != 0) { + // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); + // return SQL_ERROR; + // } + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + b->buffer_type = TSDB_DATA_TYPE_NCHAR; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + pb->valid = 1; + } break; + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "xdoes not support parameter type[%x]", ParameterType); + return SQL_ERROR; + } break; + } + return SQL_SUCCESS; +} diff --git a/src/connector/odbc/tests/main.c b/src/connector/odbc/tests/main.c index 3924fe9077..1680e22620 100644 --- a/src/connector/odbc/tests/main.c +++ b/src/connector/odbc/tests/main.c @@ -1,9 +1,134 @@ #include +#include #include +#include +#include "os.h" -int main(void) { +// static const char *dsn = "TAOS_DSN"; +// static const char *uid = "root"; +// static const char *pwd = "taosdata"; + +typedef struct data_s data_t; +struct data_s { + 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+1]; + char blob[40+1]; // why 80? ref: tests/examples/c/apitest.c +}; + +static const char *pre_stmts[] = { + "create database db", + "use db", + "create table t (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))" +}; + +static const char *pro_stmts[] = { + // "insert into t values ('2019-07-15 00:00:00', 1)", + // "insert into t values ('2019-07-15 01:00:00', 2)", + "select * from t" + // "drop database db" +}; + +static int do_statement(SQLHSTMT stmt, const char *statement) { + SQLRETURN r = 0; + do { + fprintf(stderr, "prepare [%s]\n", statement); + r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement)); + if (r) break; + fprintf(stderr, "execute [%s]\n", statement); + r = SQLExecute(stmt); + if (r) break; + fprintf(stderr, "done\n"); + } while (0); + fprintf(stderr, "r: [%x][%d]\n", r, r); + return r; +} + +static int do_insert(SQLHSTMT stmt, data_t data) { + SQLRETURN r = 0; + SQLLEN lbin; + SQLLEN lblob; + + const char *statement = "insert into t values (?, ?, ?, ?, ?, ?, ?, ?, ?,?)"; + int ignore = 0; + + do { + fprintf(stderr, "prepare [%s]\n", statement); + r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement)); + if (r) break; + + fprintf(stderr, "bind 1 [%s]\n", statement); + r = SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_TIMESTAMP, ignore, ignore, &data.ts, ignore, NULL); + if (r) break; + + fprintf(stderr, "bind 2 [%s]\n", statement); + r = SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_BIT, SQL_BIT, ignore, ignore, &data.b, ignore, NULL); + if (r) break; + + fprintf(stderr, "bind 3 [%s]\n", statement); + r = SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_TINYINT, SQL_TINYINT, ignore, ignore, &data.v1, ignore, NULL); + if (r) break; + + fprintf(stderr, "bind 4 [%s]\n", statement); + r = SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_SMALLINT, ignore, ignore, &data.v2, ignore, NULL); + if (r) break; + + fprintf(stderr, "bind 5 [%s]\n", statement); + r = SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, ignore, ignore, &data.v4, ignore, NULL); + if (r) break; + + fprintf(stderr, "bind 6 [%s]\n", statement); + r = SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, ignore, ignore, &data.v8, ignore, NULL); + if (r) break; + + fprintf(stderr, "bind 7 [%s]\n", statement); + r = SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_FLOAT, ignore, ignore, &data.f4, ignore, NULL); + if (r) break; + + fprintf(stderr, "bind 8 [%s]\n", statement); + SQLLEN l8 = SQL_NULL_DATA; + r = SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, ignore, ignore, &data.f8, ignore, &l8); + if (r) break; + + fprintf(stderr, "bind 9 [%s]\n", statement); + lbin = SQL_NTS; + r = SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_VARBINARY, sizeof(data.bin)-1, ignore, &data.bin, ignore, &lbin); + if (r) break; + + fprintf(stderr, "bind 10 [%s]\n", statement); + lblob = SQL_NTS; + r = SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(data.blob)-1, ignore, &data.blob, ignore, &lblob); + if (r) break; + + fprintf(stderr, "execute [%s]\n", statement); + r = SQLExecute(stmt); + if (r) break; + + // ts += 1; + // v = 2; + // fprintf(stderr, "execute [%s]\n", statement); + // r = SQLExecute(stmt); + // if (r) break; + + fprintf(stderr, "done\n"); + } while (0); + fprintf(stderr, "r: [%x][%d]\n", r, r); + return r; +} + +int main(int argc, char *argv[]) { + if (argc < 4) return 1; + const char *dsn = argv[1]; + const char *uid = argv[2]; + const char *pwd = argv[3]; SQLRETURN r; SQLHENV env = {0}; SQLHDBC conn = {0}; @@ -12,6 +137,62 @@ int main(void) { do { r = SQLAllocConnect(env, &conn); if (r!=SQL_SUCCESS) break; + do { + r = SQLConnect(conn, (SQLCHAR*)dsn, strlen(dsn), + (SQLCHAR*)uid, strlen(uid), + (SQLCHAR*)pwd, strlen(pwd)); + if (r!=SQL_SUCCESS) break; + do { + SQLHSTMT stmt = {0}; + r = SQLAllocHandle(SQL_HANDLE_STMT, conn, &stmt); + if (r!=SQL_SUCCESS) break; + do { + do_statement(stmt, "drop database db"); + for (size_t i=0; i Date: Thu, 8 Oct 2020 12:19:33 +0800 Subject: [PATCH 008/101] remove obsoletes, ignore->ignored for better understanding --- src/connector/odbc/src/todbc.c | 72 --------------------------------- src/connector/odbc/tests/main.c | 22 +++++----- 2 files changed, 11 insertions(+), 83 deletions(-) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 58659a2346..1a14d4b3b1 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -729,14 +729,6 @@ SQLRETURN SQL_API SQLBindParameter( } // LengthPrecision ignored; // ParameterScale ignored; - // if (LengthPrecision != sizeof(v.b)) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); - // return SQL_ERROR; - // } - // if (ParameterScale != 0) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); - // return SQL_ERROR; - // } param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); if (!ar || !binds) { @@ -770,14 +762,6 @@ SQLRETURN SQL_API SQLBindParameter( } // LengthPrecision ignored; // ParameterScale ignored; - // if (LengthPrecision != sizeof(v.v1)) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); - // return SQL_ERROR; - // } - // if (ParameterScale != 0) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); - // return SQL_ERROR; - // } param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); if (!ar || !binds) { @@ -811,14 +795,6 @@ SQLRETURN SQL_API SQLBindParameter( } // LengthPrecision ignored; // ParameterScale ignored; - // if (LengthPrecision != sizeof(v.v2)) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); - // return SQL_ERROR; - // } - // if (ParameterScale != 0) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); - // return SQL_ERROR; - // } param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); if (!ar || !binds) { @@ -852,14 +828,6 @@ SQLRETURN SQL_API SQLBindParameter( } // LengthPrecision ignored; // ParameterScale ignored; - // if (LengthPrecision != sizeof(v.v4)) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); - // return SQL_ERROR; - // } - // if (ParameterScale != 0) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); - // return SQL_ERROR; - // } param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); if (!ar || !binds) { @@ -893,14 +861,6 @@ SQLRETURN SQL_API SQLBindParameter( } // LengthPrecision ignored; // ParameterScale ignored; - // if (LengthPrecision != sizeof(v.v8)) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); - // return SQL_ERROR; - // } - // if (ParameterScale != 0) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); - // return SQL_ERROR; - // } param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); if (!ar || !binds) { @@ -934,14 +894,6 @@ SQLRETURN SQL_API SQLBindParameter( } // LengthPrecision ignored; // ParameterScale ignored; - // if (LengthPrecision != sizeof(v.f4)) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); - // return SQL_ERROR; - // } - // if (ParameterScale != 0) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); - // return SQL_ERROR; - // } param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); if (!ar || !binds) { @@ -975,14 +927,6 @@ SQLRETURN SQL_API SQLBindParameter( } // LengthPrecision ignored; // ParameterScale ignored; - // if (LengthPrecision != sizeof(v.f8)) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); - // return SQL_ERROR; - // } - // if (ParameterScale != 0) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); - // return SQL_ERROR; - // } param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); if (!ar || !binds) { @@ -1016,14 +960,6 @@ SQLRETURN SQL_API SQLBindParameter( } // LengthPrecision ignored; // ParameterScale ignored; - // if (LengthPrecision != sizeof(v.v8)) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); - // return SQL_ERROR; - // } - // if (ParameterScale != 0) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); - // return SQL_ERROR; - // } param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); if (!ar || !binds) { @@ -1060,10 +996,6 @@ SQLRETURN SQL_API SQLBindParameter( return SQL_ERROR; } // ParameterScale ignored; - // if (ParameterScale != 0) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); - // return SQL_ERROR; - // } param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); if (!ar || !binds) { @@ -1100,10 +1032,6 @@ SQLRETURN SQL_API SQLBindParameter( return SQL_ERROR; } // ParameterScale ignored; - // if (ParameterScale != 0) { - // SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] scale not supported yet", ParameterNumber); - // return SQL_ERROR; - // } param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); if (!ar || !binds) { diff --git a/src/connector/odbc/tests/main.c b/src/connector/odbc/tests/main.c index 1680e22620..980194f27a 100644 --- a/src/connector/odbc/tests/main.c +++ b/src/connector/odbc/tests/main.c @@ -58,7 +58,7 @@ static int do_insert(SQLHSTMT stmt, data_t data) { SQLLEN lblob; const char *statement = "insert into t values (?, ?, ?, ?, ?, ?, ?, ?, ?,?)"; - int ignore = 0; + int ignored = 0; do { fprintf(stderr, "prepare [%s]\n", statement); @@ -66,46 +66,46 @@ static int do_insert(SQLHSTMT stmt, data_t data) { if (r) break; fprintf(stderr, "bind 1 [%s]\n", statement); - r = SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_TIMESTAMP, ignore, ignore, &data.ts, ignore, NULL); + r = SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_TIMESTAMP, ignored, ignored, &data.ts, ignored, NULL); if (r) break; fprintf(stderr, "bind 2 [%s]\n", statement); - r = SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_BIT, SQL_BIT, ignore, ignore, &data.b, ignore, NULL); + r = SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_BIT, SQL_BIT, ignored, ignored, &data.b, ignored, NULL); if (r) break; fprintf(stderr, "bind 3 [%s]\n", statement); - r = SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_TINYINT, SQL_TINYINT, ignore, ignore, &data.v1, ignore, NULL); + r = SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_TINYINT, SQL_TINYINT, ignored, ignored, &data.v1, ignored, NULL); if (r) break; fprintf(stderr, "bind 4 [%s]\n", statement); - r = SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_SMALLINT, ignore, ignore, &data.v2, ignore, NULL); + r = SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_SMALLINT, ignored, ignored, &data.v2, ignored, NULL); if (r) break; fprintf(stderr, "bind 5 [%s]\n", statement); - r = SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, ignore, ignore, &data.v4, ignore, NULL); + r = SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, ignored, ignored, &data.v4, ignored, NULL); if (r) break; fprintf(stderr, "bind 6 [%s]\n", statement); - r = SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, ignore, ignore, &data.v8, ignore, NULL); + r = SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, ignored, ignored, &data.v8, ignored, NULL); if (r) break; fprintf(stderr, "bind 7 [%s]\n", statement); - r = SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_FLOAT, ignore, ignore, &data.f4, ignore, NULL); + r = SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_FLOAT, ignored, ignored, &data.f4, ignored, NULL); if (r) break; fprintf(stderr, "bind 8 [%s]\n", statement); SQLLEN l8 = SQL_NULL_DATA; - r = SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, ignore, ignore, &data.f8, ignore, &l8); + r = SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, ignored, ignored, &data.f8, ignored, &l8); if (r) break; fprintf(stderr, "bind 9 [%s]\n", statement); lbin = SQL_NTS; - r = SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_VARBINARY, sizeof(data.bin)-1, ignore, &data.bin, ignore, &lbin); + r = SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_VARBINARY, sizeof(data.bin)-1, ignored, &data.bin, ignored, &lbin); if (r) break; fprintf(stderr, "bind 10 [%s]\n", statement); lblob = SQL_NTS; - r = SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(data.blob)-1, ignore, &data.blob, ignore, &lblob); + r = SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(data.blob)-1, ignored, &data.blob, ignored, &lblob); if (r) break; fprintf(stderr, "execute [%s]\n", statement); From ac16f2031427f2dac7d44a661f420dcc5881019a Mon Sep 17 00:00:00 2001 From: freemine Date: Thu, 8 Oct 2020 15:47:55 +0800 Subject: [PATCH 009/101] 1. adjust CMakeLists.txt to enabling checking unixodbc existence 2. use install.sh to install odbcinst/odbc record in system 3. add packages (unixodbc/unixodbc-dev) to install in .travis.yml --- .travis.yml | 2 ++ src/CMakeLists.txt | 2 +- src/connector/odbc/CMakeLists.txt | 36 +++++++++++++++++++++-------- src/connector/odbc/src/install.sh | 25 ++++++++++++++++++++ src/connector/odbc/src/template.dsn | 5 ---- src/connector/odbc/src/template.ini | 4 ---- 6 files changed, 54 insertions(+), 20 deletions(-) create mode 100755 src/connector/odbc/src/install.sh delete mode 100644 src/connector/odbc/src/template.dsn delete mode 100644 src/connector/odbc/src/template.ini diff --git a/.travis.yml b/.travis.yml index 6e49709c85..3256710434 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,6 +32,8 @@ matrix: - python3-setuptools - valgrind - psmisc + - unixodbc + - unixodbc-dev before_script: - export TZ=Asia/Harbin diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2ed56d4f81..f619edd221 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,5 +21,5 @@ ADD_SUBDIRECTORY(wal) ADD_SUBDIRECTORY(cq) ADD_SUBDIRECTORY(dnode) ADD_SUBDIRECTORY(connector/odbc) -# ADD_SUBDIRECTORY(connector/jdbc) +ADD_SUBDIRECTORY(connector/jdbc) diff --git a/src/connector/odbc/CMakeLists.txt b/src/connector/odbc/CMakeLists.txt index 32e5c7ed69..393a1bd50b 100644 --- a/src/connector/odbc/CMakeLists.txt +++ b/src/connector/odbc/CMakeLists.txt @@ -1,17 +1,33 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(TDengine) -AUX_SOURCE_DIRECTORY(src SRC) +IF (TD_LINUX_64) + find_program(HAVE_ODBCINST NAMES odbcinst) -# generate dynamic library (*.so) -ADD_LIBRARY(todbc SHARED ${SRC}) -SET_TARGET_PROPERTIES(todbc PROPERTIES CLEAN_DIRECT_OUTPUT 1) -SET_TARGET_PROPERTIES(todbc PROPERTIES VERSION ${TD_VER_NUMBER} SOVERSION 1) -TARGET_LINK_LIBRARIES(todbc taos) + IF (HAVE_ODBCINST) + include(CheckSymbolExists) + # shall we revert CMAKE_REQUIRED_LIBRARIES and how? + set(CMAKE_REQUIRED_LIBRARIES odbc) + check_symbol_exists(SQLExecute "sql.h" HAVE_ODBC_DEV) + if(NOT (HAVE_ODBC_DEV)) + unset(HAVE_ODBC_DEV CACHE) + message(WARNING "unixodbc-dev is not installed yet, you may install it under ubuntu by typing: sudo apt install unixodbc-dev") + else () + message(STATUS "unixodbc/unixodbc-dev are installed, and odbc connector will be built") + AUX_SOURCE_DIRECTORY(src SRC) -install(CODE "execute_process(COMMAND sudo odbcinst -i -d -f ${CMAKE_CURRENT_SOURCE_DIR}/src/template.ini - COMMAND odbcinst -i -s -f ${CMAKE_CURRENT_SOURCE_DIR}/src/template.dsn - COMMAND echo odbc install done)") + # generate dynamic library (*.so) + ADD_LIBRARY(todbc SHARED ${SRC}) + SET_TARGET_PROPERTIES(todbc PROPERTIES CLEAN_DIRECT_OUTPUT 1) + SET_TARGET_PROPERTIES(todbc PROPERTIES VERSION ${TD_VER_NUMBER} SOVERSION 1) + TARGET_LINK_LIBRARIES(todbc taos) -ADD_SUBDIRECTORY(tests) + install(CODE "execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/src/install.sh ${CMAKE_BINARY_DIR})") + + ADD_SUBDIRECTORY(tests) + endif() + ELSE () + message(WARNING "unixodbc is not installed yet, you may install it under ubuntu by typing: sudo apt install unixodbc-dev") + ENDIF () +ENDIF () diff --git a/src/connector/odbc/src/install.sh b/src/connector/odbc/src/install.sh new file mode 100755 index 0000000000..b8c04677c7 --- /dev/null +++ b/src/connector/odbc/src/install.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +set -u + +BLD_DIR="$1" + +rm -f "${BLD_DIR}/template.ini" +rm -f "${BLD_DIR}/template.dsn" + +cat > "${BLD_DIR}/template.ini" < "${BLD_DIR}/template.dsn" < Date: Thu, 8 Oct 2020 16:01:05 +0800 Subject: [PATCH 010/101] remove trailing spaces --- src/connector/odbc/src/todbc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 1a14d4b3b1..dda79f209c 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -180,7 +180,7 @@ SQLRETURN SQL_API SQLAllocEnv(SQLHENV *EnvironmentHandle) { SQLRETURN SQL_API SQLFreeEnv(SQLHENV EnvironmentHandle) { env_t *env = (env_t*)EnvironmentHandle; if (!env) return SQL_ERROR; - + DASSERT(GET_REF(env)==1); DASSERT(!env->destroying); @@ -255,7 +255,7 @@ SQLRETURN SQL_API SQLConnect(SQLHDBC ConnectionHandle, SQLCHAR *Authentication, SQLSMALLINT NameLength3) { conn_t *conn = (conn_t*)ConnectionHandle; if (!conn) return SQL_ERROR; - + if (conn->taos) { SET_ERROR(conn, "HY000", TSDB_CODE_TSC_APP_ERROR, "connection still in use"); return SQL_ERROR; From 75786385a2e8a8f7b74d5f261405d5de22fe03be Mon Sep 17 00:00:00 2001 From: freemine Date: Thu, 8 Oct 2020 17:05:07 +0800 Subject: [PATCH 011/101] just add one more blank line to check if travis's happy with that, ref #add odbc connector #3765 --- src/connector/odbc/src/todbc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index dda79f209c..64de488f8b 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -26,6 +26,7 @@ #include #include + #define D(fmt, ...) \ fprintf(stderr, \ "%s[%d]:%s() " fmt "\n", \ From 045175682a1efe89f1401ec4843289a1e4a0194a Mon Sep 17 00:00:00 2001 From: freemine Date: Thu, 8 Oct 2020 22:36:13 +0800 Subject: [PATCH 012/101] 1. typo correction in CMakeLists.txt 2. fprint error message when SQLxxx failed in tcodbc --- src/connector/odbc/CMakeLists.txt | 2 +- src/connector/odbc/src/odbc_install.sh | 7 ------ src/connector/odbc/src/todbc.c | 24 ++++++++++----------- src/connector/odbc/tests/main.c | 30 ++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 20 deletions(-) delete mode 100644 src/connector/odbc/src/odbc_install.sh diff --git a/src/connector/odbc/CMakeLists.txt b/src/connector/odbc/CMakeLists.txt index 393a1bd50b..58e7b6acf1 100644 --- a/src/connector/odbc/CMakeLists.txt +++ b/src/connector/odbc/CMakeLists.txt @@ -27,7 +27,7 @@ IF (TD_LINUX_64) ADD_SUBDIRECTORY(tests) endif() ELSE () - message(WARNING "unixodbc is not installed yet, you may install it under ubuntu by typing: sudo apt install unixodbc-dev") + message(WARNING "unixodbc is not installed yet, you may install it under ubuntu by typing: sudo apt install unixodbc") ENDIF () ENDIF () diff --git a/src/connector/odbc/src/odbc_install.sh b/src/connector/odbc/src/odbc_install.sh deleted file mode 100644 index 3ed8078a19..0000000000 --- a/src/connector/odbc/src/odbc_install.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -odbcinst -u -d -n TAOS && -odbcinst -i -d -f "$(dirname "$0")/template.ini" && -odbcinst -i -s -f "$(dirname "$0")/template.dsn" && -echo yes - diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 64de488f8b..2c4c0f90bf 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -47,18 +47,18 @@ do { \ #define LOCK(obj) pthread_mutex_lock(&obj->lock); #define UNLOCK(obj) pthread_mutex_unlock(&obj->lock); -#define SET_ERROR(obj, sqlstate, eno, err_fmt, ...) \ -do { \ - obj->err.err_no = eno; \ - const char* estr = tstrerror(eno); \ - if (!estr) estr = "Unknown error"; \ - int n = snprintf(NULL, 0, "@[%d][%x]%s: " err_fmt "", __LINE__, eno, estr, ##__VA_ARGS__); \ - if (n<0) break; \ - char *err_str = (char*)realloc(obj->err.err_str, n+1); \ - if (!err_str) break; \ - obj->err.err_str = err_str; \ - snprintf(obj->err.err_str, n+1, "@[%d][%x]%s: " err_fmt "", __LINE__, eno, estr, ##__VA_ARGS__); \ - snprintf((char*)obj->err.sql_state, sizeof(obj->err.sql_state), "%s", sqlstate); \ +#define SET_ERROR(obj, sqlstate, eno, err_fmt, ...) \ +do { \ + obj->err.err_no = eno; \ + const char* estr = tstrerror(eno); \ + if (!estr) estr = "Unknown error"; \ + int n = snprintf(NULL, 0, "%s: @[%d][TSDB:%x]" err_fmt "", estr, __LINE__, eno, ##__VA_ARGS__); \ + if (n<0) break; \ + char *err_str = (char*)realloc(obj->err.err_str, n+1); \ + if (!err_str) break; \ + obj->err.err_str = err_str; \ + snprintf(obj->err.err_str, n+1, "%s: @[%d][TSDB:%x]" err_fmt "", estr, __LINE__, eno, ##__VA_ARGS__); \ + snprintf((char*)obj->err.sql_state, sizeof(obj->err.sql_state), "%s", sqlstate); \ } while (0) #define CLR_ERROR(obj) \ diff --git a/src/connector/odbc/tests/main.c b/src/connector/odbc/tests/main.c index 980194f27a..aa4ce7a05a 100644 --- a/src/connector/odbc/tests/main.c +++ b/src/connector/odbc/tests/main.c @@ -37,14 +37,30 @@ static const char *pro_stmts[] = { // "drop database db" }; +#define CHK_RESULT(r, ht, h) \ +do { \ + if (r==0) break; \ + SQLCHAR ss[10]; \ + SQLINTEGER ne = 0; \ + SQLCHAR es[4096]; \ + SQLSMALLINT n = 0; \ + ss[0] = '\0'; \ + es[0] = '\0'; \ + SQLRETURN ret = SQLGetDiagRec(ht, h, 1, ss, &ne, es, sizeof(es), &n); \ + if (ret) break; \ + fprintf(stderr, "%s%s\n", ss, es); \ +} while (0) + static int do_statement(SQLHSTMT stmt, const char *statement) { SQLRETURN r = 0; do { fprintf(stderr, "prepare [%s]\n", statement); r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement)); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "execute [%s]\n", statement); r = SQLExecute(stmt); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "done\n"); } while (0); @@ -63,53 +79,65 @@ static int do_insert(SQLHSTMT stmt, data_t data) { do { fprintf(stderr, "prepare [%s]\n", statement); r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement)); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "bind 1 [%s]\n", statement); r = SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_TIMESTAMP, ignored, ignored, &data.ts, ignored, NULL); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "bind 2 [%s]\n", statement); r = SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_BIT, SQL_BIT, ignored, ignored, &data.b, ignored, NULL); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "bind 3 [%s]\n", statement); r = SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_TINYINT, SQL_TINYINT, ignored, ignored, &data.v1, ignored, NULL); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "bind 4 [%s]\n", statement); r = SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_SMALLINT, ignored, ignored, &data.v2, ignored, NULL); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "bind 5 [%s]\n", statement); r = SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, ignored, ignored, &data.v4, ignored, NULL); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "bind 6 [%s]\n", statement); r = SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, ignored, ignored, &data.v8, ignored, NULL); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "bind 7 [%s]\n", statement); r = SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_FLOAT, ignored, ignored, &data.f4, ignored, NULL); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "bind 8 [%s]\n", statement); SQLLEN l8 = SQL_NULL_DATA; r = SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, ignored, ignored, &data.f8, ignored, &l8); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "bind 9 [%s]\n", statement); lbin = SQL_NTS; r = SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_VARBINARY, sizeof(data.bin)-1, ignored, &data.bin, ignored, &lbin); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "bind 10 [%s]\n", statement); lblob = SQL_NTS; r = SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(data.blob)-1, ignored, &data.blob, ignored, &lblob); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; fprintf(stderr, "execute [%s]\n", statement); r = SQLExecute(stmt); + CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; // ts += 1; @@ -136,11 +164,13 @@ int main(int argc, char *argv[]) { if (r!=SQL_SUCCESS) return 1; do { r = SQLAllocConnect(env, &conn); + CHK_RESULT(r, SQL_HANDLE_ENV, env); if (r!=SQL_SUCCESS) break; do { r = SQLConnect(conn, (SQLCHAR*)dsn, strlen(dsn), (SQLCHAR*)uid, strlen(uid), (SQLCHAR*)pwd, strlen(pwd)); + CHK_RESULT(r, SQL_HANDLE_DBC, conn); if (r!=SQL_SUCCESS) break; do { SQLHSTMT stmt = {0}; From f1f5c0c4796ec51a57b90a376fca7c429f7311ae Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 10 Oct 2020 22:12:15 +0800 Subject: [PATCH 013/101] 1. add SQLDriverConnect to let ODBC-aware software to communicate with such as pyodbc 2. SQLDescribeCol 3. more DATA-TYPE-convertions in SQLGetData --- src/CMakeLists.txt | 2 +- src/connector/odbc/src/todbc.c | 2278 +++++++++++++++++++++++++------ src/connector/odbc/tests/main.c | 4 +- src/inc/taoserror.h | 12 +- 4 files changed, 1860 insertions(+), 436 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f619edd221..2ed56d4f81 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,5 +21,5 @@ ADD_SUBDIRECTORY(wal) ADD_SUBDIRECTORY(cq) ADD_SUBDIRECTORY(dnode) ADD_SUBDIRECTORY(connector/odbc) -ADD_SUBDIRECTORY(connector/jdbc) +# ADD_SUBDIRECTORY(connector/jdbc) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 2c4c0f90bf..6eaaba762f 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -25,6 +25,7 @@ #include #include #include +#include #define D(fmt, ...) \ @@ -87,6 +88,16 @@ do { \ obj->err.sql_state[0] = '\0'; \ } while (0) +#define SET_UNSUPPORT_ERROR(obj, sqlstate, err_fmt, ...) \ +do { \ + SET_ERROR(obj, sqlstate, TSDB_CODE_COM_OPS_NOT_SUPPORT, err_fmt, ##__VA_ARGS__); \ +} while (0) \ + +#define SET_HANDLE_INVALID(obj, sqlstate, err_fmt, ...) \ +do { \ + SET_ERROR(obj, sqlstate, TSDB_CODE_QRY_INVALID_QHANDLE, err_fmt, ##__VA_ARGS__); \ +} while (0); + #define SDUP(s,n) (s ? (s[n] ? (const char*)strndup((const char*)s,n) : (const char*)s) : strdup("")) #define SFRE(x,s,n) \ do { \ @@ -108,6 +119,25 @@ do { \ r_091c = SQL_SUCCESS; \ } while (0) +#define PROFILING 0 + +#define PROFILE(statement) \ +do { \ + if (!PROFILING) { \ + statement; \ + break; \ + } \ + struct timeval tv0, tv1; \ + gettimeofday(&tv0, NULL); \ + statement; \ + gettimeofday(&tv1, NULL); \ + double delta = difftime(tv1.tv_sec, tv0.tv_sec); \ + delta *= 1000000; \ + delta += (tv1.tv_usec-tv0.tv_usec); \ + delta /= 1000000; \ + D("%s: elapsed: [%.6f]s", #statement, delta); \ +} while (0) + typedef struct env_s env_t; typedef struct conn_s conn_t; typedef struct sql_s sql_t; @@ -116,8 +146,13 @@ typedef struct param_bind_s param_bind_t; struct param_bind_s { SQLUSMALLINT ParameterNumber; + SQLSMALLINT ValueType; + SQLSMALLINT ParameterType; + SQLULEN LengthPrecision; + SQLSMALLINT ParameterScale; SQLPOINTER ParameterValue; - SQLLEN *StrLen_or_Ind; + SQLLEN *StrLen_or_Ind; + unsigned int valid; }; @@ -158,13 +193,23 @@ struct sql_s { taos_error_t err; }; +typedef struct c_target_s c_target_t; +struct c_target_s { + SQLUSMALLINT col; + SQLSMALLINT ct; // c type: SQL_C_XXX + char *ptr; + SQLLEN len; + SQLLEN *soi; +}; + static pthread_once_t init_once = PTHREAD_ONCE_INIT; static void init_routine(void); static int do_field_display_size(TAOS_FIELD *field); static void do_convert(SQLPOINTER TargetValue, SQLLEN BufferLength, SQLLEN *StrLen_or_Ind, TAOS_FIELD *field, void *row); -SQLRETURN SQL_API SQLAllocEnv(SQLHENV *EnvironmentHandle) { +static SQLRETURN doSQLAllocEnv(SQLHENV *EnvironmentHandle) +{ pthread_once(&init_once, init_routine); env_t *env = (env_t*)calloc(1, sizeof(*env)); @@ -178,7 +223,15 @@ SQLRETURN SQL_API SQLAllocEnv(SQLHENV *EnvironmentHandle) { return SQL_SUCCESS; } -SQLRETURN SQL_API SQLFreeEnv(SQLHENV EnvironmentHandle) { +SQLRETURN SQL_API SQLAllocEnv(SQLHENV *EnvironmentHandle) +{ + SQLRETURN r; + r = doSQLAllocEnv(EnvironmentHandle); + return r; +} + +static SQLRETURN doSQLFreeEnv(SQLHENV EnvironmentHandle) +{ env_t *env = (env_t*)EnvironmentHandle; if (!env) return SQL_ERROR; @@ -197,8 +250,16 @@ SQLRETURN SQL_API SQLFreeEnv(SQLHENV EnvironmentHandle) { return SQL_SUCCESS; } -SQLRETURN SQL_API SQLAllocConnect(SQLHENV EnvironmentHandle, - SQLHDBC *ConnectionHandle) { +SQLRETURN SQL_API SQLFreeEnv(SQLHENV EnvironmentHandle) +{ + SQLRETURN r; + r = doSQLFreeEnv(EnvironmentHandle); + return r; +} + +static SQLRETURN doSQLAllocConnect(SQLHENV EnvironmentHandle, + SQLHDBC *ConnectionHandle) +{ env_t *env = (env_t*)EnvironmentHandle; if (!env) return SQL_ERROR; @@ -208,7 +269,7 @@ SQLRETURN SQL_API SQLAllocConnect(SQLHENV EnvironmentHandle, do { conn = (conn_t*)calloc(1, sizeof(*conn)); if (!conn) { - SET_ERROR(env, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to alloc connection handle"); + SET_ERROR(env, "HY000", TSDB_CODE_ODBC_OOM, "failed to alloc connection handle"); break; } @@ -225,7 +286,16 @@ SQLRETURN SQL_API SQLAllocConnect(SQLHENV EnvironmentHandle, return SQL_ERROR; } -SQLRETURN SQL_API SQLFreeConnect(SQLHDBC ConnectionHandle) { +SQLRETURN SQL_API SQLAllocConnect(SQLHENV EnvironmentHandle, + SQLHDBC *ConnectionHandle) +{ + SQLRETURN r; + r = doSQLAllocConnect(EnvironmentHandle, ConnectionHandle); + return r; +} + +static SQLRETURN doSQLFreeConnect(SQLHDBC ConnectionHandle) +{ conn_t *conn = (conn_t*)ConnectionHandle; if (!conn) return SQL_ERROR; @@ -250,10 +320,18 @@ SQLRETURN SQL_API SQLFreeConnect(SQLHDBC ConnectionHandle) { return SQL_SUCCESS; } -SQLRETURN SQL_API SQLConnect(SQLHDBC ConnectionHandle, +SQLRETURN SQL_API SQLFreeConnect(SQLHDBC ConnectionHandle) +{ + SQLRETURN r; + r = doSQLFreeConnect(ConnectionHandle); + return r; +} + +static SQLRETURN doSQLConnect(SQLHDBC ConnectionHandle, SQLCHAR *ServerName, SQLSMALLINT NameLength1, SQLCHAR *UserName, SQLSMALLINT NameLength2, - SQLCHAR *Authentication, SQLSMALLINT NameLength3) { + SQLCHAR *Authentication, SQLSMALLINT NameLength3) +{ conn_t *conn = (conn_t*)ConnectionHandle; if (!conn) return SQL_ERROR; @@ -267,15 +345,16 @@ SQLRETURN SQL_API SQLConnect(SQLHDBC ConnectionHandle, const char *auth = SDUP(Authentication, NameLength3); do { - if (!serverName || !userName || !auth) { - SET_ERROR(conn, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to connect to database"); + if ((ServerName && !serverName) || (UserName && !userName) || (Authentication && !auth)) { + SET_ERROR(conn, "HY000", TSDB_CODE_ODBC_OOM, "failed to allocate resources"); break; } + // TODO: data-race // TODO: shall receive ip/port from odbc.ini conn->taos = taos_connect("localhost", userName, auth, NULL, 0); if (!conn->taos) { - SET_ERROR(conn, "HY000", terrno, "failed to connect to database"); + SET_ERROR(conn, "HY000", terrno, "failed to connect to data source"); break; } } while (0); @@ -287,7 +366,20 @@ SQLRETURN SQL_API SQLConnect(SQLHDBC ConnectionHandle, return conn->taos ? SQL_SUCCESS : SQL_ERROR; } -SQLRETURN SQL_API SQLDisconnect(SQLHDBC ConnectionHandle) { +SQLRETURN SQL_API SQLConnect(SQLHDBC ConnectionHandle, + SQLCHAR *ServerName, SQLSMALLINT NameLength1, + SQLCHAR *UserName, SQLSMALLINT NameLength2, + SQLCHAR *Authentication, SQLSMALLINT NameLength3) +{ + SQLRETURN r; + r = doSQLConnect(ConnectionHandle, ServerName, NameLength1, + UserName, NameLength2, + Authentication, NameLength3); + return r; +} + +static SQLRETURN doSQLDisconnect(SQLHDBC ConnectionHandle) +{ conn_t *conn = (conn_t*)ConnectionHandle; if (!conn) return SQL_ERROR; @@ -299,8 +391,16 @@ SQLRETURN SQL_API SQLDisconnect(SQLHDBC ConnectionHandle) { return SQL_SUCCESS; } -SQLRETURN SQL_API SQLAllocStmt(SQLHDBC ConnectionHandle, - SQLHSTMT *StatementHandle) { +SQLRETURN SQL_API SQLDisconnect(SQLHDBC ConnectionHandle) +{ + SQLRETURN r; + r = doSQLDisconnect(ConnectionHandle); + return r; +} + +static SQLRETURN doSQLAllocStmt(SQLHDBC ConnectionHandle, + SQLHSTMT *StatementHandle) +{ conn_t *conn = (conn_t*)ConnectionHandle; if (!conn) return SQL_ERROR; @@ -309,7 +409,7 @@ SQLRETURN SQL_API SQLAllocStmt(SQLHDBC ConnectionHandle, do { sql_t *sql = (sql_t*)calloc(1, sizeof(*sql)); if (!sql) { - SET_ERROR(conn, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to alloc statement handle"); + SET_ERROR(conn, "HY000", TSDB_CODE_ODBC_OOM, "failed to alloc statement handle"); break; } @@ -326,13 +426,23 @@ SQLRETURN SQL_API SQLAllocStmt(SQLHDBC ConnectionHandle, return SQL_ERROR; } -SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle, - SQLUSMALLINT Option) { +SQLRETURN SQL_API SQLAllocStmt(SQLHDBC ConnectionHandle, + SQLHSTMT *StatementHandle) +{ + SQLRETURN r; + r = doSQLAllocStmt(ConnectionHandle, StatementHandle); + return r; +} + +static SQLRETURN doSQLFreeStmt(SQLHSTMT StatementHandle, + SQLUSMALLINT Option) +{ sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; + if (Option == SQL_CLOSE) return SQL_SUCCESS; if (Option != SQL_DROP) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "failed to free statement"); + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "free statement with Option[%x] not supported yet", Option); return SQL_ERROR; } @@ -370,12 +480,29 @@ SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle, return SQL_SUCCESS; } -SQLRETURN SQL_API SQLExecDirect(SQLHSTMT StatementHandle, - SQLCHAR *StatementText, SQLINTEGER TextLength) { +SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT StatementHandle, + SQLUSMALLINT Option) +{ + SQLRETURN r; + r = doSQLFreeStmt(StatementHandle, Option); + return r; +} + +static SQLRETURN doSQLExecDirect(SQLHSTMT StatementHandle, + SQLCHAR *StatementText, SQLINTEGER TextLength) +{ sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) return SQL_ERROR; - if (!sql->conn->taos) return SQL_ERROR; + + if (!sql->conn) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + return SQL_ERROR; + } + + if (!sql->conn->taos) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); + return SQL_ERROR; + } if (sql->rs) { taos_free_result(sql->rs); @@ -404,7 +531,7 @@ SQLRETURN SQL_API SQLExecDirect(SQLHSTMT StatementHandle, SQLRETURN r = SQL_ERROR; do { if (!stxt) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to query"); + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_OOM, "failed to allocate resources"); break; } sql->rs = taos_query(sql->conn->taos, stxt); @@ -416,13 +543,34 @@ SQLRETURN SQL_API SQLExecDirect(SQLHSTMT StatementHandle, return r; } -SQLRETURN SQL_API SQLNumResultCols(SQLHSTMT StatementHandle, - SQLSMALLINT *ColumnCount) { +SQLRETURN SQL_API SQLExecDirect(SQLHSTMT StatementHandle, + SQLCHAR *StatementText, SQLINTEGER TextLength) +{ + SQLRETURN r; + r = doSQLExecDirect(StatementHandle, StatementText, TextLength); + return r; +} + +static SQLRETURN doSQLNumResultCols(SQLHSTMT StatementHandle, + SQLSMALLINT *ColumnCount) +{ sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) return SQL_ERROR; - if (!sql->conn->taos) return SQL_ERROR; - if (!sql->rs) return SQL_ERROR; + + if (!sql->conn) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + return SQL_ERROR; + } + + if (!sql->conn->taos) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); + return SQL_ERROR; + } + + if (!sql->rs) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + return SQL_ERROR; + } int fields = taos_field_count(sql->rs); if (ColumnCount) { @@ -432,13 +580,35 @@ SQLRETURN SQL_API SQLNumResultCols(SQLHSTMT StatementHandle, return SQL_SUCCESS; } -SQLRETURN SQL_API SQLRowCount(SQLHSTMT StatementHandle, - SQLLEN *RowCount) { +SQLRETURN SQL_API SQLNumResultCols(SQLHSTMT StatementHandle, + SQLSMALLINT *ColumnCount) +{ + SQLRETURN r; + r = doSQLNumResultCols(StatementHandle, ColumnCount); + return r; +} + +static SQLRETURN doSQLRowCount(SQLHSTMT StatementHandle, + SQLLEN *RowCount) +{ sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) return SQL_ERROR; - if (!sql->conn->taos) return SQL_ERROR; - if (!sql->rs) return SQL_ERROR; + + if (!sql->conn) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + return SQL_ERROR; + } + + if (!sql->conn->taos) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); + return SQL_ERROR; + } + + if (!sql->rs) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + return SQL_ERROR; + } + int rows = taos_affected_rows(sql->rs); if (RowCount) { *RowCount = rows; @@ -446,20 +616,60 @@ SQLRETURN SQL_API SQLRowCount(SQLHSTMT StatementHandle, return SQL_SUCCESS; } -SQLRETURN SQL_API SQLColAttribute(SQLHSTMT StatementHandle, - SQLUSMALLINT ColumnNumber, SQLUSMALLINT FieldIdentifier, - SQLPOINTER CharacterAttribute, SQLSMALLINT BufferLength, - SQLSMALLINT *StringLength, SQLLEN *NumericAttribute ) { +SQLRETURN SQL_API SQLRowCount(SQLHSTMT StatementHandle, + SQLLEN *RowCount) +{ + SQLRETURN r; + r = doSQLRowCount(StatementHandle, RowCount); + return r; +} + +static SQLRETURN doSQLColAttribute(SQLHSTMT StatementHandle, + SQLUSMALLINT ColumnNumber, SQLUSMALLINT FieldIdentifier, + SQLPOINTER CharacterAttribute, SQLSMALLINT BufferLength, + SQLSMALLINT *StringLength, SQLLEN *NumericAttribute ) +{ + D("......"); sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) return SQL_ERROR; - if (!sql->conn->taos) return SQL_ERROR; - if (!sql->rs) return SQL_ERROR; + + if (!sql->conn) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + return SQL_ERROR; + } + + if (!sql->conn->taos) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); + return SQL_ERROR; + } + + if (!sql->rs) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + return SQL_ERROR; + } + int nfields = taos_field_count(sql->rs); TAOS_FIELD *fields = taos_fetch_fields(sql->rs); - if (nfields==0 || fields==NULL) return SQL_ERROR; - if (ColumnNumber>nfields) return SQL_ERROR; + if (nfields==0 || fields==NULL) { + SET_ERROR(sql, "HY000", TSDB_CODE_MND_FIELD_NOT_EXIST, "no fields in result set"); + return SQL_ERROR; + } + + if (ColumnNumber<0) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_APP_ERROR, "ColumnNumber[%d] underflow", ColumnNumber); + return SQL_ERROR; + } + + if (ColumnNumber==0) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "ColumnNumber[0] not supported"); + return SQL_ERROR; + } + + if (ColumnNumber>nfields) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_APP_ERROR, "ColumnNumber[%d] overflow", ColumnNumber); + return SQL_ERROR; + } TAOS_FIELD *field = fields + ColumnNumber-1; @@ -468,65 +678,454 @@ SQLRETURN SQL_API SQLColAttribute(SQLHSTMT StatementHandle, *NumericAttribute = do_field_display_size(field); } break; case SQL_COLUMN_LABEL: { - strncpy(CharacterAttribute, field->name, field->bytes); + size_t n = sizeof(field->name); + strncpy(CharacterAttribute, field->name, (n>BufferLength ? BufferLength : n)); + } break; + case SQL_COLUMN_UNSIGNED: { + *NumericAttribute = SQL_FALSE; } break; default: { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, + "ColumnNumber[%d] FieldIdentifier[%d] not supported yet", + ColumnNumber, FieldIdentifier); return SQL_ERROR; } break; } return SQL_SUCCESS; } -SQLRETURN SQL_API SQLGetData(SQLHSTMT StatementHandle, +SQLRETURN SQL_API SQLColAttribute(SQLHSTMT StatementHandle, + SQLUSMALLINT ColumnNumber, SQLUSMALLINT FieldIdentifier, + SQLPOINTER CharacterAttribute, SQLSMALLINT BufferLength, + SQLSMALLINT *StringLength, SQLLEN *NumericAttribute ) +{ + SQLRETURN r; + r = doSQLColAttribute(StatementHandle, ColumnNumber, FieldIdentifier, + CharacterAttribute, BufferLength, + StringLength, NumericAttribute); + return r; +} + +static SQLRETURN conv_tsdb_bool_to_c_bit(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b); +static SQLRETURN conv_tsdb_bool_to_c_tinyint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b); +static SQLRETURN conv_tsdb_bool_to_c_short(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b); +static SQLRETURN conv_tsdb_bool_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b); +static SQLRETURN conv_tsdb_bool_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b); +static SQLRETURN conv_tsdb_bool_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b); +static SQLRETURN conv_tsdb_bool_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b); +static SQLRETURN conv_tsdb_bool_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b); +static SQLRETURN conv_tsdb_bool_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b); +static SQLRETURN conv_tsdb_v1_to_c_tinyint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1); +static SQLRETURN conv_tsdb_v1_to_c_short(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1); +static SQLRETURN conv_tsdb_v1_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1); +static SQLRETURN conv_tsdb_v1_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1); +static SQLRETURN conv_tsdb_v1_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1); +static SQLRETURN conv_tsdb_v1_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1); +static SQLRETURN conv_tsdb_v1_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1); +static SQLRETURN conv_tsdb_v1_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1); +static SQLRETURN conv_tsdb_v2_to_c_short(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2); +static SQLRETURN conv_tsdb_v2_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2); +static SQLRETURN conv_tsdb_v2_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2); +static SQLRETURN conv_tsdb_v2_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2); +static SQLRETURN conv_tsdb_v2_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2); +static SQLRETURN conv_tsdb_v2_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2); +static SQLRETURN conv_tsdb_v2_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2); +static SQLRETURN conv_tsdb_v4_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4); +static SQLRETURN conv_tsdb_v4_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4); +static SQLRETURN conv_tsdb_v4_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4); +static SQLRETURN conv_tsdb_v4_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4); +static SQLRETURN conv_tsdb_v4_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4); +static SQLRETURN conv_tsdb_v4_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4); +static SQLRETURN conv_tsdb_v8_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8); +static SQLRETURN conv_tsdb_v8_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8); +static SQLRETURN conv_tsdb_v8_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8); +static SQLRETURN conv_tsdb_v8_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8); +static SQLRETURN conv_tsdb_v8_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8); +static SQLRETURN conv_tsdb_f4_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, float f4); +static SQLRETURN conv_tsdb_f4_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, float f4); +static SQLRETURN conv_tsdb_f4_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, float f4); +static SQLRETURN conv_tsdb_f4_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, float f4); +static SQLRETURN conv_tsdb_f8_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, double f8); +static SQLRETURN conv_tsdb_f8_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, double f8); +static SQLRETURN conv_tsdb_f8_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, double f8); +static SQLRETURN conv_tsdb_ts_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts); +static SQLRETURN conv_tsdb_ts_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts); +static SQLRETURN conv_tsdb_ts_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts); +static SQLRETURN conv_tsdb_ts_to_c_ts(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts); +static SQLRETURN conv_tsdb_bin_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const unsigned char *bin); +static SQLRETURN conv_tsdb_bin_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const unsigned char *bin); +static SQLRETURN conv_tsdb_str_to_c_bit(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str); +static SQLRETURN conv_tsdb_str_to_c_v1(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str); +static SQLRETURN conv_tsdb_str_to_c_v2(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str); +static SQLRETURN conv_tsdb_str_to_c_v4(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str); +static SQLRETURN conv_tsdb_str_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str); +static SQLRETURN conv_tsdb_str_to_c_f4(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str); +static SQLRETURN conv_tsdb_str_to_c_f8(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str); +static SQLRETURN conv_tsdb_str_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str); +static SQLRETURN conv_tsdb_str_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str); + +static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLPOINTER TargetValue, SQLLEN BufferLength, - SQLLEN *StrLen_or_Ind) { + SQLLEN *StrLen_or_Ind) +{ sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) return SQL_ERROR; - if (!sql->conn->taos) return SQL_ERROR; - if (!sql->rs) return SQL_ERROR; - if (!sql->row) return SQL_ERROR; + + if (!sql->conn) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + return SQL_ERROR; + } + + if (!sql->conn->taos) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); + return SQL_ERROR; + } + + if (!sql->rs) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + return SQL_ERROR; + } + + if (!sql->row) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no rows cached or not ready"); + return SQL_ERROR; + } + + DASSERT(TargetValue); + int nfields = taos_field_count(sql->rs); TAOS_FIELD *fields = taos_fetch_fields(sql->rs); - if (nfields==0 || fields==NULL) return SQL_ERROR; - if (ColumnNumber>nfields) return SQL_ERROR; + if (nfields==0 || fields==NULL) { + SET_ERROR(sql, "HY000", TSDB_CODE_MND_FIELD_NOT_EXIST, "no fields in result set"); + return SQL_ERROR; + } + + if (ColumnNumber<0) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_APP_ERROR, "ColumnNumber[%d] underflow", ColumnNumber); + return SQL_ERROR; + } + + if (ColumnNumber==0) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "ColumnNumber[0] not supported"); + return SQL_ERROR; + } + + if (ColumnNumber>nfields) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_APP_ERROR, "ColumnNumber[%d] overflow", ColumnNumber); + return SQL_ERROR; + } TAOS_FIELD *field = fields + ColumnNumber-1; + void *row = sql->row[ColumnNumber-1]; - switch (TargetType) { - case SQL_CHAR: { - if (sql->row[ColumnNumber-1]) { - do_convert(TargetValue, BufferLength, StrLen_or_Ind, field, sql->row[ColumnNumber-1]); - *StrLen_or_Ind = SQL_NTS; - } else { - *StrLen_or_Ind = SQL_NULL_DATA; + if (!row) { + if (StrLen_or_Ind) { + *StrLen_or_Ind = SQL_NULL_DATA; + } + return SQL_SUCCESS; + } + + c_target_t target = {0}; + target.col = ColumnNumber; + target.ct = TargetType; + target.ptr = TargetValue; + target.len = BufferLength; + target.soi = StrLen_or_Ind; + + SQLRETURN r = SQL_ERROR; + + switch (field->type) { + case TSDB_DATA_TYPE_BOOL: { + int8_t v = *(int8_t*)row; + if (v) v = 1; + switch (target.ct) { + case SQL_C_BIT: return conv_tsdb_bool_to_c_bit(sql, &target, field, v); + case SQL_C_TINYINT: return conv_tsdb_bool_to_c_tinyint(sql, &target, field, v); + case SQL_C_SHORT: return conv_tsdb_bool_to_c_short(sql, &target, field, v); + case SQL_C_LONG: return conv_tsdb_bool_to_c_long(sql, &target, field, v); + case SQL_C_SBIGINT: return conv_tsdb_bool_to_c_sbigint(sql, &target, field, v); + case SQL_C_FLOAT: return conv_tsdb_bool_to_c_float(sql, &target, field, v); + case SQL_C_DOUBLE: return conv_tsdb_bool_to_c_double(sql, &target, field, v); + case SQL_C_CHAR: return conv_tsdb_bool_to_c_char(sql, &target, field, v); + case SQL_C_BINARY: return conv_tsdb_bool_to_c_binary(sql, &target, field, v); + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + return SQL_ERROR; + } + } + } break; + case TSDB_DATA_TYPE_TINYINT: { + int8_t v = *(int8_t*)row; + switch (target.ct) { + case SQL_C_TINYINT: return conv_tsdb_v1_to_c_tinyint(sql, &target, field, v); + case SQL_C_SHORT: return conv_tsdb_v1_to_c_short(sql, &target, field, v); + case SQL_C_LONG: return conv_tsdb_v1_to_c_long(sql, &target, field, v); + case SQL_C_SBIGINT: return conv_tsdb_v1_to_c_sbigint(sql, &target, field, v); + case SQL_C_FLOAT: return conv_tsdb_v1_to_c_float(sql, &target, field, v); + case SQL_C_DOUBLE: return conv_tsdb_v1_to_c_double(sql, &target, field, v); + case SQL_C_CHAR: return conv_tsdb_v1_to_c_char(sql, &target, field, v); + case SQL_C_BINARY: return conv_tsdb_v1_to_c_binary(sql, &target, field, v); + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + return SQL_ERROR; + } + } + } break; + case TSDB_DATA_TYPE_SMALLINT: { + int16_t v = *(int16_t*)row; + switch (target.ct) { + case SQL_C_SHORT: return conv_tsdb_v2_to_c_short(sql, &target, field, v); + case SQL_C_LONG: return conv_tsdb_v2_to_c_long(sql, &target, field, v); + case SQL_C_SBIGINT: return conv_tsdb_v2_to_c_sbigint(sql, &target, field, v); + case SQL_C_FLOAT: return conv_tsdb_v2_to_c_float(sql, &target, field, v); + case SQL_C_DOUBLE: return conv_tsdb_v2_to_c_double(sql, &target, field, v); + case SQL_C_CHAR: return conv_tsdb_v2_to_c_char(sql, &target, field, v); + case SQL_C_BINARY: return conv_tsdb_v2_to_c_binary(sql, &target, field, v); + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + return SQL_ERROR; + } + } + } break; + case TSDB_DATA_TYPE_INT: { + int32_t v = *(int32_t*)row; + switch (target.ct) { + case SQL_C_LONG: return conv_tsdb_v4_to_c_long(sql, &target, field, v); + case SQL_C_SBIGINT: return conv_tsdb_v4_to_c_sbigint(sql, &target, field, v); + case SQL_C_FLOAT: return conv_tsdb_v4_to_c_float(sql, &target, field, v); + case SQL_C_DOUBLE: return conv_tsdb_v4_to_c_double(sql, &target, field, v); + case SQL_C_CHAR: return conv_tsdb_v4_to_c_char(sql, &target, field, v); + case SQL_C_BINARY: return conv_tsdb_v4_to_c_binary(sql, &target, field, v); + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + return SQL_ERROR; + } + } + } break; + case TSDB_DATA_TYPE_BIGINT: { + int64_t v = *(int64_t*)row; + switch (target.ct) { + case SQL_C_SBIGINT: return conv_tsdb_v8_to_c_sbigint(sql, &target, field, v); + case SQL_C_FLOAT: return conv_tsdb_v8_to_c_float(sql, &target, field, v); + case SQL_C_DOUBLE: return conv_tsdb_v8_to_c_double(sql, &target, field, v); + case SQL_C_CHAR: return conv_tsdb_v8_to_c_char(sql, &target, field, v); + case SQL_C_BINARY: return conv_tsdb_v8_to_c_binary(sql, &target, field, v); + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + return SQL_ERROR; + } + } + } break; + case TSDB_DATA_TYPE_FLOAT: { + float v = *(float*)row; + switch (target.ct) { + case SQL_C_FLOAT: return conv_tsdb_f4_to_c_float(sql, &target, field, v); + case SQL_C_DOUBLE: return conv_tsdb_f4_to_c_double(sql, &target, field, v); + case SQL_C_CHAR: return conv_tsdb_f4_to_c_char(sql, &target, field, v); + case SQL_C_BINARY: return conv_tsdb_f4_to_c_binary(sql, &target, field, v); + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + return SQL_ERROR; + } + } + } break; + case TSDB_DATA_TYPE_DOUBLE: { + double v = *(double*)row; + switch (target.ct) { + case SQL_C_DOUBLE: return conv_tsdb_f8_to_c_double(sql, &target, field, v); + case SQL_C_CHAR: return conv_tsdb_f8_to_c_char(sql, &target, field, v); + case SQL_C_BINARY: return conv_tsdb_f8_to_c_binary(sql, &target, field, v); + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + return SQL_ERROR; + } + } + } break; + case TSDB_DATA_TYPE_TIMESTAMP: { + TIMESTAMP_STRUCT ts = {0}; + int64_t v = *(int64_t*)row; + time_t t = v/1000; + struct tm tm = {0}; + localtime_r(&t, &tm); + ts.year = tm.tm_year + 1900; + ts.month = tm.tm_mon + 1; + ts.day = tm.tm_mday; + ts.hour = tm.tm_hour; + ts.minute = tm.tm_min; + ts.second = tm.tm_sec; + ts.fraction = v%1000; + switch (target.ct) { + case SQL_C_SBIGINT: return conv_tsdb_ts_to_c_v8(sql, &target, field, &ts); + case SQL_C_CHAR: return conv_tsdb_ts_to_c_str(sql, &target, field, &ts); + case SQL_C_BINARY: return conv_tsdb_ts_to_c_bin(sql, &target, field, &ts); + case SQL_C_TIMESTAMP: return conv_tsdb_ts_to_c_ts(sql, &target, field, &ts); + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + return SQL_ERROR; + } + } + } break; + case TSDB_DATA_TYPE_BINARY: { + const unsigned char *bin = (const unsigned char *)row; + switch (target.ct) { + case SQL_C_CHAR: return conv_tsdb_bin_to_c_str(sql, &target, field, bin); + case SQL_C_BINARY: return conv_tsdb_bin_to_c_bin(sql, &target, field, bin); + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + return SQL_ERROR; + } + } + } break; + case TSDB_DATA_TYPE_NCHAR: { + const char *str = (const char *)row; + switch (target.ct) { + case SQL_C_BIT: return conv_tsdb_str_to_c_bit(sql, &target, field, str); + case SQL_C_TINYINT: return conv_tsdb_str_to_c_v1(sql, &target, field, str); + case SQL_C_SHORT: return conv_tsdb_str_to_c_v2(sql, &target, field, str); + case SQL_C_LONG: return conv_tsdb_str_to_c_v4(sql, &target, field, str); + case SQL_C_SBIGINT: return conv_tsdb_str_to_c_v8(sql, &target, field, str); + case SQL_C_FLOAT: return conv_tsdb_str_to_c_f4(sql, &target, field, str); + case SQL_C_DOUBLE: return conv_tsdb_str_to_c_f8(sql, &target, field, str); + case SQL_C_CHAR: return conv_tsdb_str_to_c_str(sql, &target, field, str); + case SQL_C_BINARY: return conv_tsdb_str_to_c_bin(sql, &target, field, str); + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + return SQL_ERROR; + } } } break; default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "field [@%d] type [%d] not supported yet", ColumnNumber, field->type); return SQL_ERROR; } break; } + + if (1) return r; + + switch (TargetType) { + case SQL_C_CHAR: { + do_convert(TargetValue, BufferLength, StrLen_or_Ind, field, row); + } break; + case SQL_C_TIMESTAMP: { + TIMESTAMP_STRUCT ts = {0}; + DASSERT(BufferLength == sizeof(ts)); + int64_t v = *(int64_t*)row; + time_t t = v/1000; + struct tm tm = {0}; + localtime_r(&t, &tm); + ts.year = tm.tm_year + 1900; + ts.month = tm.tm_mon + 1; + ts.day = tm.tm_mday; + ts.hour = tm.tm_hour; + ts.minute = tm.tm_min; + ts.second = tm.tm_sec; + ts.fraction = 0; + + memcpy(TargetValue, &ts, sizeof(ts)); + } break; + case SQL_C_LONG: { + int32_t v = *(int32_t*)row; + DASSERT(BufferLength == sizeof(v)); + memcpy(TargetValue, &v, sizeof(v)); + } break; + case SQL_C_SBIGINT: { + int64_t v = *(int64_t*)row; + DASSERT(BufferLength == sizeof(v)); + memcpy(TargetValue, &v, sizeof(v)); + } break; + case SQL_C_FLOAT: { + float v = *(float*)row; + DASSERT(BufferLength == sizeof(v)); + memcpy(TargetValue, &v, sizeof(v)); + } break; + case SQL_C_DOUBLE: { + double v = *(double*)row; + DASSERT(BufferLength == sizeof(v)); + memcpy(TargetValue, &v, sizeof(v)); + } break; + case SQL_C_BINARY: { + if (StrLen_or_Ind) { + if (field->type == TSDB_DATA_TYPE_NCHAR) { + *StrLen_or_Ind = strnlen((const char*)row, field->bytes); + } else { + *StrLen_or_Ind = field->bytes; + } + } + size_t n = field->bytes; + if (n>BufferLength) n = BufferLength; + memcpy(TargetValue, (const char*)row, n); + } break; + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, + "ColumnNumber[%d] TargetType[%d] BufferLength[%ld] not supported yet", + ColumnNumber, TargetType, BufferLength); + return SQL_ERROR; + } break; + } + return SQL_SUCCESS; } -SQLRETURN SQL_API SQLFetch(SQLHSTMT StatementHandle) { +SQLRETURN SQL_API SQLGetData(SQLHSTMT StatementHandle, + SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, + SQLPOINTER TargetValue, SQLLEN BufferLength, + SQLLEN *StrLen_or_Ind) +{ + SQLRETURN r; + r = doSQLGetData(StatementHandle, ColumnNumber, TargetType, + TargetValue, BufferLength, + StrLen_or_Ind); + return r; +} + +static SQLRETURN doSQLFetch(SQLHSTMT StatementHandle) +{ sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) return SQL_ERROR; - if (!sql->conn->taos) return SQL_ERROR; - if (!sql->rs) return SQL_ERROR; + + if (!sql->conn) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + return SQL_ERROR; + } + + if (!sql->conn->taos) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); + return SQL_ERROR; + } + + if (!sql->rs) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + return SQL_ERROR; + } + sql->row = taos_fetch_row(sql->rs); return sql->row ? SQL_SUCCESS : SQL_NO_DATA; } -SQLRETURN SQL_API SQLPrepare(SQLHSTMT StatementHandle, - SQLCHAR *StatementText, SQLINTEGER TextLength) { +SQLRETURN SQL_API SQLFetch(SQLHSTMT StatementHandle) +{ + SQLRETURN r; + r = doSQLFetch(StatementHandle); + return r; +} + +static SQLRETURN doSQLPrepare(SQLHSTMT StatementHandle, + SQLCHAR *StatementText, SQLINTEGER TextLength) +{ sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) return SQL_ERROR; - if (!sql->conn->taos) return SQL_ERROR; + + if (!sql->conn) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + return SQL_ERROR; + } + + if (!sql->conn->taos) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); + return SQL_ERROR; + } if (sql->rs) { taos_free_result(sql->rs); @@ -568,12 +1167,33 @@ SQLRETURN SQL_API SQLPrepare(SQLHSTMT StatementHandle, return sql->stmt ? SQL_SUCCESS : SQL_ERROR; } -SQLRETURN SQL_API SQLExecute(SQLHSTMT StatementHandle) { +SQLRETURN SQL_API SQLPrepare(SQLHSTMT StatementHandle, + SQLCHAR *StatementText, SQLINTEGER TextLength) +{ + SQLRETURN r; + r = doSQLPrepare(StatementHandle, StatementText, TextLength); + return r; +} + +static SQLRETURN doSQLExecute(SQLHSTMT StatementHandle) +{ sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) return SQL_ERROR; - if (!sql->conn->taos) return SQL_ERROR; - if (!sql->stmt) return SQL_ERROR; + + if (!sql->conn) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + return SQL_ERROR; + } + + if (!sql->conn->taos) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); + return SQL_ERROR; + } + + if (!sql->stmt) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_SQL, "no statement cached or not ready"); + return SQL_ERROR; + } if (sql->rs) { taos_free_result(sql->rs); @@ -635,20 +1255,20 @@ SQLRETURN SQL_API SQLExecute(SQLHSTMT StatementHandle) { } if (sql->n_params > 0) { - r = taos_stmt_bind_param(sql->stmt, sql->binds); + PROFILE(r = taos_stmt_bind_param(sql->stmt, sql->binds)); if (r) { SET_ERROR(sql, "HY000", r, "failed to bind parameters"); return SQL_ERROR; } - r = taos_stmt_add_batch(sql->stmt); + PROFILE(r = taos_stmt_add_batch(sql->stmt)); if (r) { SET_ERROR(sql, "HY000", r, "failed to add batch"); return SQL_ERROR; } } - r = taos_stmt_execute(sql->stmt); + PROFILE(r = taos_stmt_execute(sql->stmt)); if (r) { SET_ERROR(sql, "HY000", r, "failed to execute statement"); return SQL_ERROR; @@ -656,25 +1276,48 @@ SQLRETURN SQL_API SQLExecute(SQLHSTMT StatementHandle) { SQLRETURN ret = SQL_ERROR; - sql->rs = taos_stmt_use_result(sql->stmt); + PROFILE(sql->rs = taos_stmt_use_result(sql->stmt)); CHK_RS(ret, sql, "failed to use result"); return ret; } -SQLRETURN SQL_API SQLGetDiagField(SQLSMALLINT HandleType, SQLHANDLE Handle, +SQLRETURN SQL_API SQLExecute(SQLHSTMT StatementHandle) +{ + SQLRETURN r; + PROFILE(r = doSQLExecute(StatementHandle)); + return r; +} + +static SQLRETURN doSQLGetDiagField(SQLSMALLINT HandleType, SQLHANDLE Handle, SQLSMALLINT RecNumber, SQLSMALLINT DiagIdentifier, SQLPOINTER DiagInfo, SQLSMALLINT BufferLength, - SQLSMALLINT *StringLength) { + SQLSMALLINT *StringLength) +{ // if this function is not exported, isql will never call SQLGetDiagRec return SQL_ERROR; } -SQLRETURN SQL_API SQLGetDiagRec(SQLSMALLINT HandleType, SQLHANDLE Handle, +SQLRETURN SQL_API SQLGetDiagField(SQLSMALLINT HandleType, SQLHANDLE Handle, + SQLSMALLINT RecNumber, SQLSMALLINT DiagIdentifier, + SQLPOINTER DiagInfo, SQLSMALLINT BufferLength, + SQLSMALLINT *StringLength) +{ + SQLRETURN r; + r = doSQLGetDiagField(HandleType, Handle, + RecNumber, DiagIdentifier, + DiagInfo, BufferLength, + StringLength); + return r; +} + +static SQLRETURN doSQLGetDiagRec(SQLSMALLINT HandleType, SQLHANDLE Handle, SQLSMALLINT RecNumber, SQLCHAR *Sqlstate, SQLINTEGER *NativeError, SQLCHAR *MessageText, - SQLSMALLINT BufferLength, SQLSMALLINT *TextLength) { + SQLSMALLINT BufferLength, SQLSMALLINT *TextLength) +{ if (RecNumber>1) return SQL_NO_DATA; + switch (HandleType) { case SQL_HANDLE_ENV: { env_t *env = (env_t*)Handle; @@ -698,9 +1341,204 @@ SQLRETURN SQL_API SQLGetDiagRec(SQLSMALLINT HandleType, SQLHANDLE Handle, } break; } + // how to return error? return SQL_ERROR; } +SQLRETURN SQL_API SQLGetDiagRec(SQLSMALLINT HandleType, SQLHANDLE Handle, + SQLSMALLINT RecNumber, SQLCHAR *Sqlstate, + SQLINTEGER *NativeError, SQLCHAR *MessageText, + SQLSMALLINT BufferLength, SQLSMALLINT *TextLength) +{ + SQLRETURN r; + r = doSQLGetDiagRec(HandleType, Handle, + RecNumber, Sqlstate, + NativeError, MessageText, + BufferLength, TextLength); + return r; +} + +static SQLRETURN doSQLBindParameter( + SQLHSTMT StatementHandle, + SQLUSMALLINT ParameterNumber, + SQLSMALLINT fParamType, + SQLSMALLINT ValueType, + SQLSMALLINT ParameterType, + SQLULEN LengthPrecision, + SQLSMALLINT ParameterScale, + SQLPOINTER ParameterValue, + SQLLEN cbValueMax, // ignore for now, since only SQL_PARAM_INPUT is supported now + SQLLEN *StrLen_or_Ind) +{ + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + + if (!sql->conn) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + return SQL_ERROR; + } + + if (!sql->conn->taos) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); + return SQL_ERROR; + } + + if (!sql->stmt) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_SQL, "no statement cached or not ready"); + return SQL_ERROR; + } + + if (fParamType != SQL_PARAM_INPUT) { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "non-input parameter [@%d] not supported yet", ParameterNumber); + return SQL_ERROR; + } + + int buffer_type = 0; + + // ref: https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/converting-data-from-c-to-sql-data-types?view=sql-server-ver15 + switch (ValueType) { + case SQL_C_BIT: { + switch (ParameterType) { + case SQL_BIT: buffer_type = TSDB_DATA_TYPE_BOOL; break; + case SQL_TINYINT: buffer_type = TSDB_DATA_TYPE_TINYINT; break; + case SQL_SMALLINT: buffer_type = TSDB_DATA_TYPE_SMALLINT; break; + case SQL_INTEGER: buffer_type = TSDB_DATA_TYPE_INT; break; + case SQL_BIGINT: buffer_type = TSDB_DATA_TYPE_BIGINT; break; + case SQL_FLOAT: buffer_type = TSDB_DATA_TYPE_FLOAT; break; + case SQL_DOUBLE: buffer_type = TSDB_DATA_TYPE_DOUBLE; break; + case SQL_VARCHAR: buffer_type = TSDB_DATA_TYPE_NCHAR; break; + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, + "parameter[@%d] no conversion from [%d] to [%d]", + ParameterNumber, ValueType, ParameterType); + return SQL_ERROR; + } break; + } + } break; + case SQL_C_TINYINT: + case SQL_C_SHORT: + case SQL_C_LONG: + case SQL_C_SBIGINT: + case SQL_C_FLOAT: + case SQL_C_DOUBLE: + case SQL_C_NUMERIC: { + switch (ParameterType) { + case SQL_BIT: buffer_type = TSDB_DATA_TYPE_BOOL; break; + case SQL_TINYINT: buffer_type = TSDB_DATA_TYPE_TINYINT; break; + case SQL_SMALLINT: buffer_type = TSDB_DATA_TYPE_SMALLINT; break; + case SQL_INTEGER: buffer_type = TSDB_DATA_TYPE_INT; break; + case SQL_BIGINT: buffer_type = TSDB_DATA_TYPE_BIGINT; break; + case SQL_FLOAT: buffer_type = TSDB_DATA_TYPE_FLOAT; break; + case SQL_DOUBLE: buffer_type = TSDB_DATA_TYPE_DOUBLE; break; + case SQL_VARCHAR: buffer_type = TSDB_DATA_TYPE_NCHAR; break; + case SQL_TIMESTAMP: buffer_type = TSDB_DATA_TYPE_TIMESTAMP; break; // extention to taos + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, + "parameter[@%d] no conversion from [%d] to [%d]", + ParameterNumber, ValueType, ParameterType); + return SQL_ERROR; + } break; + } + } break; + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: { + switch (ParameterType) { + case SQL_VARCHAR: buffer_type = TSDB_DATA_TYPE_NCHAR; break; + case SQL_TIMESTAMP: buffer_type = TSDB_DATA_TYPE_TIMESTAMP; break; // extention to taos + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, + "parameter[@%d] no conversion from [%d] to [%d]", + ParameterNumber, ValueType, ParameterType); + return SQL_ERROR; + } break; + } + } break; + case SQL_C_CHAR: { + switch (ParameterType) { + case SQL_BIT: buffer_type = TSDB_DATA_TYPE_BOOL; break; + case SQL_TINYINT: buffer_type = TSDB_DATA_TYPE_TINYINT; break; + case SQL_SMALLINT: buffer_type = TSDB_DATA_TYPE_SMALLINT; break; + case SQL_INTEGER: buffer_type = TSDB_DATA_TYPE_INT; break; + case SQL_BIGINT: buffer_type = TSDB_DATA_TYPE_BIGINT; break; + case SQL_FLOAT: buffer_type = TSDB_DATA_TYPE_FLOAT; break; + case SQL_DOUBLE: buffer_type = TSDB_DATA_TYPE_DOUBLE; break; + case SQL_VARCHAR: buffer_type = TSDB_DATA_TYPE_NCHAR; break; + case SQL_VARBINARY: buffer_type = TSDB_DATA_TYPE_BINARY; break; + case SQL_TIMESTAMP: buffer_type = TSDB_DATA_TYPE_TIMESTAMP; break; // extention to taos + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, + "parameter[@%d] no conversion from [%d] to [%d]", + ParameterNumber, ValueType, ParameterType); + return SQL_ERROR; + } break; + } + } break; + case SQL_C_BINARY: { + switch (ParameterType) { + case SQL_BIT: buffer_type = TSDB_DATA_TYPE_BOOL; break; + case SQL_TINYINT: buffer_type = TSDB_DATA_TYPE_TINYINT; break; + case SQL_SMALLINT: buffer_type = TSDB_DATA_TYPE_SMALLINT; break; + case SQL_INTEGER: buffer_type = TSDB_DATA_TYPE_INT; break; + case SQL_BIGINT: buffer_type = TSDB_DATA_TYPE_BIGINT; break; + case SQL_FLOAT: buffer_type = TSDB_DATA_TYPE_FLOAT; break; + case SQL_DOUBLE: buffer_type = TSDB_DATA_TYPE_DOUBLE; break; + case SQL_VARCHAR: buffer_type = TSDB_DATA_TYPE_NCHAR; break; + case SQL_VARBINARY: buffer_type = TSDB_DATA_TYPE_BINARY; break; + case SQL_TIMESTAMP: buffer_type = TSDB_DATA_TYPE_TIMESTAMP; break; // extention to taos + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, + "parameter[@%d] no conversion from [%d] to [%d]", + ParameterNumber, ValueType, ParameterType); + return SQL_ERROR; + } break; + } + } break; + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, + "parameter[@%d] no conversion from [%d] to [%d]", + ParameterNumber, ValueType, ParameterType); + return SQL_ERROR; + } break; + } + + param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); + TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); + if (!ar || !binds) { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_OOM, "failed to allocate resources for Parameter[%d]", ParameterNumber); + if (ar) sql->params = ar; + if (binds) sql->binds = binds; + return SQL_ERROR; + } + sql->params = ar; + sql->binds = binds; + if (sql->n_paramsn_params = ParameterNumber; + } + + param_bind_t *pb = ar + ParameterNumber - 1; + TAOS_BIND *b = binds + ParameterNumber - 1; + + b->buffer_type = buffer_type; + b->buffer_length = LengthPrecision; + b->buffer = NULL; + b->length = NULL; + b->is_null = NULL; + b->is_unsigned = 0; + b->error = NULL; + + pb->ParameterNumber = ParameterNumber; + pb->ValueType = ValueType; + pb->ParameterType = ParameterType; + pb->LengthPrecision = LengthPrecision; + pb->ParameterScale = ParameterScale; + pb->ParameterValue = ParameterValue; + pb->StrLen_or_Ind = StrLen_or_Ind; + + pb->valid = 1; + return SQL_SUCCESS; +} + SQLRETURN SQL_API SQLBindParameter( SQLHSTMT StatementHandle, SQLUSMALLINT ParameterNumber, @@ -711,362 +1549,252 @@ SQLRETURN SQL_API SQLBindParameter( SQLSMALLINT ParameterScale, SQLPOINTER ParameterValue, SQLLEN cbValueMax, // ignore for now, since only SQL_PARAM_INPUT is supported now - SQLLEN *StrLen_or_Ind) { - sql_t *sql = (sql_t*)StatementHandle; - if (!sql) return SQL_ERROR; - if (!sql->conn) return SQL_ERROR; - if (!sql->conn->taos) return SQL_ERROR; - if (!sql->stmt) return SQL_ERROR; + SQLLEN *StrLen_or_Ind) +{ + SQLRETURN r; + r = doSQLBindParameter(StatementHandle, ParameterNumber, fParamType, ValueType, ParameterType, + LengthPrecision, ParameterScale, ParameterValue, cbValueMax, StrLen_or_Ind); + return r; +} - if (fParamType != SQL_PARAM_INPUT) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "non-input parameter [@%d] not supported yet", ParameterNumber); +static SQLRETURN doSQLDriverConnect( + SQLHDBC hdbc, + SQLHWND hwnd, + SQLCHAR *szConnStrIn, + SQLSMALLINT cbConnStrIn, + SQLCHAR *szConnStrOut, + SQLSMALLINT cbConnStrOutMax, + SQLSMALLINT *pcbConnStrOut, + SQLUSMALLINT fDriverCompletion) +{ + conn_t *conn = (conn_t*)hdbc; + if (!conn) return SQL_ERROR; + + if (fDriverCompletion!=SQL_DRIVER_NOPROMPT) { + SET_ERROR(conn, "HY000", TSDB_CODE_TSC_APP_ERROR, "option[%d] other than SQL_DRIVER_NOPROMPT not supported yet", fDriverCompletion); return SQL_ERROR; } - switch (ParameterType) { - case SQL_BIT: { // TSDB_DATA_TYPE_BOOL - if (ValueType!=SQL_C_BIT) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); - return SQL_ERROR; - } - // LengthPrecision ignored; - // ParameterScale ignored; - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; - return SQL_ERROR; - } - sql->params = ar; - sql->binds = binds; - if (sql->n_paramsn_params = ParameterNumber; - } - param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - b->buffer_type = TSDB_DATA_TYPE_BOOL; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; - pb->ParameterValue = ParameterValue; - pb->StrLen_or_Ind = StrLen_or_Ind; - pb->valid = 1; - } break; - case SQL_TINYINT: { // TSDB_DATA_TYPE_TINYINT - if (ValueType!=SQL_C_TINYINT) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); - return SQL_ERROR; - } - // LengthPrecision ignored; - // ParameterScale ignored; - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; - return SQL_ERROR; - } - sql->params = ar; - sql->binds = binds; - if (sql->n_paramsn_params = ParameterNumber; - } - param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - b->buffer_type = TSDB_DATA_TYPE_TINYINT; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; - pb->ParameterValue = ParameterValue; - pb->StrLen_or_Ind = StrLen_or_Ind; - pb->valid = 1; - } break; - case SQL_SMALLINT: { // TSDB_DATA_TYPE_SMALLINT - if (ValueType!=SQL_C_SHORT) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); - return SQL_ERROR; - } - // LengthPrecision ignored; - // ParameterScale ignored; - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; - return SQL_ERROR; - } - sql->params = ar; - sql->binds = binds; - if (sql->n_paramsn_params = ParameterNumber; - } - param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - b->buffer_type = TSDB_DATA_TYPE_SMALLINT; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; - pb->ParameterValue = ParameterValue; - pb->StrLen_or_Ind = StrLen_or_Ind; - pb->valid = 1; - } break; - case SQL_INTEGER: { // TSDB_DATA_TYPE_INT - if (ValueType!=SQL_C_LONG) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); - return SQL_ERROR; - } - // LengthPrecision ignored; - // ParameterScale ignored; - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; - return SQL_ERROR; - } - sql->params = ar; - sql->binds = binds; - if (sql->n_paramsn_params = ParameterNumber; - } - param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - b->buffer_type = TSDB_DATA_TYPE_INT; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; - pb->ParameterValue = ParameterValue; - pb->StrLen_or_Ind = StrLen_or_Ind; - pb->valid = 1; - } break; - case SQL_BIGINT: { // TSDB_DATA_TYPE_BIGINT - if (ValueType!=SQL_C_SBIGINT) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); - return SQL_ERROR; - } - // LengthPrecision ignored; - // ParameterScale ignored; - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; - return SQL_ERROR; - } - sql->params = ar; - sql->binds = binds; - if (sql->n_paramsn_params = ParameterNumber; - } - param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - b->buffer_type = TSDB_DATA_TYPE_BIGINT; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; - pb->ParameterValue = ParameterValue; - pb->StrLen_or_Ind = StrLen_or_Ind; - pb->valid = 1; - } break; - case SQL_FLOAT: { // TSDB_DATA_TYPE_FLOAT - if (ValueType!=SQL_C_FLOAT) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); - return SQL_ERROR; - } - // LengthPrecision ignored; - // ParameterScale ignored; - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; - return SQL_ERROR; - } - sql->params = ar; - sql->binds = binds; - if (sql->n_paramsn_params = ParameterNumber; - } - param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - b->buffer_type = TSDB_DATA_TYPE_FLOAT; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; - pb->ParameterValue = ParameterValue; - pb->StrLen_or_Ind = StrLen_or_Ind; - pb->valid = 1; - } break; - case SQL_DOUBLE: { // TSDB_DATA_TYPE_DOUBLE - if (ValueType!=SQL_C_DOUBLE) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); - return SQL_ERROR; - } - // LengthPrecision ignored; - // ParameterScale ignored; - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; - return SQL_ERROR; - } - sql->params = ar; - sql->binds = binds; - if (sql->n_paramsn_params = ParameterNumber; - } - param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - b->buffer_type = TSDB_DATA_TYPE_DOUBLE; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; - pb->ParameterValue = ParameterValue; - pb->StrLen_or_Ind = StrLen_or_Ind; - pb->valid = 1; - } break; - case SQL_TIMESTAMP: { // TSDB_DATA_TYPE_TIMESTAMP - if (ValueType!=SQL_C_SBIGINT) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); - return SQL_ERROR; - } - // LengthPrecision ignored; - // ParameterScale ignored; - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; - return SQL_ERROR; - } - sql->params = ar; - sql->binds = binds; - if (sql->n_paramsn_params = ParameterNumber; - } - param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - b->buffer_type = TSDB_DATA_TYPE_TIMESTAMP; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; - pb->ParameterValue = ParameterValue; - pb->StrLen_or_Ind = StrLen_or_Ind; - pb->valid = 1; - } break; - case SQL_VARBINARY: { // TSDB_DATA_TYPE_BINARY - if (ValueType!=SQL_C_BINARY) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); - return SQL_ERROR; - } - if (LengthPrecision <=0) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); - return SQL_ERROR; - } - // ParameterScale ignored; - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; - return SQL_ERROR; - } - sql->params = ar; - sql->binds = binds; - if (sql->n_paramsn_params = ParameterNumber; - } - param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - b->buffer_type = TSDB_DATA_TYPE_BINARY; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; - pb->ParameterValue = ParameterValue; - pb->StrLen_or_Ind = StrLen_or_Ind; - pb->valid = 1; - } break; - case SQL_VARCHAR: { // TSDB_DATA_TYPE_NCHAR - if (ValueType!=SQL_C_CHAR) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching value type", ParameterNumber); - return SQL_ERROR; - } - if (LengthPrecision <=0) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "parameter [@%d] not matching length precision", ParameterNumber); - return SQL_ERROR; - } - // ParameterScale ignored; - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OUT_OF_MEMORY, "failed to bind parameter [@%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; - return SQL_ERROR; - } - sql->params = ar; - sql->binds = binds; - if (sql->n_paramsn_params = ParameterNumber; - } - param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - b->buffer_type = TSDB_DATA_TYPE_NCHAR; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; - pb->ParameterValue = ParameterValue; - pb->StrLen_or_Ind = StrLen_or_Ind; - pb->valid = 1; - } break; - default: { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "xdoes not support parameter type[%x]", ParameterType); - return SQL_ERROR; - } break; + + if (conn->taos) { + SET_ERROR(conn, "HY000", TSDB_CODE_TSC_APP_ERROR, "connection still in use"); + return SQL_ERROR; } + + // DSN=; UID=; PWD= + + const char *connStr = SDUP(szConnStrIn, cbConnStrIn); + + char *serverName = NULL; + char *userName = NULL; + char *auth = NULL; + int bytes = 0; + + do { + if (szConnStrIn && !connStr) { + SET_ERROR(conn, "HY000", TSDB_CODE_ODBC_OOM, "failed to allocate resources"); + break; + } + + int n = sscanf((const char*)connStr, "DSN=%m[^;]; UID=%m[^;]; PWD=%m[^;] %n", &serverName, &userName, &auth, &bytes); + if (n<1) { + SET_ERROR(conn, "HY000", TSDB_CODE_RPC_NETWORK_UNAVAIL, "unrecognized connection string: [%s]", (const char*)szConnStrIn); + break; + } + + D("DSN:[%s];UID:[%s];PWD:[%s]", serverName, userName, auth); + + // TODO: data-race + // TODO: shall receive ip/port from odbc.ini + conn->taos = taos_connect("localhost", userName, auth, NULL, 0); + if (!conn->taos) { + SET_ERROR(conn, "HY000", terrno, "failed to connect to data source"); + break; + } + + if (szConnStrOut) { + snprintf((char*)szConnStrOut, cbConnStrOutMax, "%s", connStr); + } + if (pcbConnStrOut) { + *pcbConnStrOut = cbConnStrIn; + } + + } while (0); + + if (serverName) free(serverName); + if (userName) free(userName); + if (auth) free(auth); + + SFRE(connStr, szConnStrIn, cbConnStrIn); + + return conn->taos ? SQL_SUCCESS : SQL_ERROR; +} + +SQLRETURN SQL_API SQLDriverConnect( + SQLHDBC hdbc, + SQLHWND hwnd, + SQLCHAR *szConnStrIn, + SQLSMALLINT cbConnStrIn, + SQLCHAR *szConnStrOut, + SQLSMALLINT cbConnStrOutMax, + SQLSMALLINT *pcbConnStrOut, + SQLUSMALLINT fDriverCompletion) +{ + SQLRETURN r; + r = doSQLDriverConnect(hdbc, hwnd, szConnStrIn, cbConnStrIn, szConnStrOut, cbConnStrOutMax, pcbConnStrOut, fDriverCompletion); + return r; +} + +static SQLRETURN doSQLSetConnectAttr(SQLHDBC ConnectionHandle, + SQLINTEGER Attribute, SQLPOINTER Value, + SQLINTEGER StringLength) +{ + conn_t *conn = (conn_t*)ConnectionHandle; + if (!conn) return SQL_ERROR; + + if (Attribute != SQL_ATTR_AUTOCOMMIT) { + SET_ERROR(conn, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "Attribute other than SQL_ATTR_AUTOCOMMIT not supported yet"); + return SQL_ERROR; + } + if (Value != (SQLPOINTER)SQL_AUTOCOMMIT_ON) { + SET_ERROR(conn, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "Attribute Value other than SQL_AUTOCOMMIT_ON not supported yet[%p]", Value); + return SQL_ERROR; + } + return SQL_SUCCESS; } +SQLRETURN SQL_API SQLSetConnectAttr(SQLHDBC ConnectionHandle, + SQLINTEGER Attribute, SQLPOINTER Value, + SQLINTEGER StringLength) +{ + SQLRETURN r; + r = doSQLSetConnectAttr(ConnectionHandle, Attribute, Value, StringLength); + return r; +} + +static SQLRETURN doSQLDescribeCol(SQLHSTMT StatementHandle, + SQLUSMALLINT ColumnNumber, SQLCHAR *ColumnName, + SQLSMALLINT BufferLength, SQLSMALLINT *NameLength, + SQLSMALLINT *DataType, SQLULEN *ColumnSize, + SQLSMALLINT *DecimalDigits, SQLSMALLINT *Nullable) +{ + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + + if (!sql->conn) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + return SQL_ERROR; + } + + if (!sql->conn->taos) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); + return SQL_ERROR; + } + + if (!sql->rs) { + SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + return SQL_ERROR; + } + + TAOS_FIELD *fields = taos_fetch_fields(sql->rs); + if (!fields) { + SET_ERROR(sql, "HY000", TSDB_CODE_MND_FIELD_NOT_EXIST, "fields not ready or unavailable"); + return SQL_ERROR; + } + int nfields = taos_field_count(sql->rs); + if (ColumnNumber<1 || ColumnNumber>nfields) { + SET_ERROR(sql, "HY000", TSDB_CODE_MND_FIELD_NOT_EXIST, "ColumnNumber not in valid range"); + return SQL_ERROR; + } + + TAOS_FIELD *field = fields + ColumnNumber - 1; + if (ColumnName) { + size_t n = sizeof(field->name); + if (n>BufferLength) n = BufferLength; + strncpy((char*)ColumnName, field->name, n); + } + if (NameLength) { + *NameLength = strnlen(field->name, sizeof(field->name)); + } + if (DataType) { + switch (field->type) { + case TSDB_DATA_TYPE_BOOL: { + *DataType = SQL_C_TINYINT; + } break; + + case TSDB_DATA_TYPE_TINYINT: { + *DataType = SQL_C_TINYINT; + } break; + + case TSDB_DATA_TYPE_SMALLINT: { + *DataType = SQL_C_SHORT; + } break; + + case TSDB_DATA_TYPE_INT: { + *DataType = SQL_C_LONG; + } break; + + case TSDB_DATA_TYPE_BIGINT: { + *DataType = SQL_BIGINT; + } break; + + case TSDB_DATA_TYPE_FLOAT: { + *DataType = SQL_C_FLOAT; + } break; + + case TSDB_DATA_TYPE_DOUBLE: { + *DataType = SQL_C_DOUBLE; + } break; + + case TSDB_DATA_TYPE_TIMESTAMP: { + *DataType = SQL_C_TIMESTAMP; + } break; + + case TSDB_DATA_TYPE_NCHAR: { + *DataType = SQL_C_CHAR; // unicode ? + } break; + + case TSDB_DATA_TYPE_BINARY: { + *DataType = SQL_C_BINARY; + } break; + + default: + SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "unknown TSDB_DATA_TYPE [%x]", field->type); + return SQL_ERROR; + break; + } + } + if (ColumnSize) { + *ColumnSize = field->bytes; + } + if (DecimalDigits) { + if (field->type == TSDB_DATA_TYPE_TIMESTAMP) { + *DecimalDigits = 3; + } else { + *DecimalDigits = 0; + } + } + if (Nullable) { + *Nullable = SQL_NULLABLE_UNKNOWN; + } + + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLDescribeCol(SQLHSTMT StatementHandle, + SQLUSMALLINT ColumnNumber, SQLCHAR *ColumnName, + SQLSMALLINT BufferLength, SQLSMALLINT *NameLength, + SQLSMALLINT *DataType, SQLULEN *ColumnSize, + SQLSMALLINT *DecimalDigits, SQLSMALLINT *Nullable) +{ + SQLRETURN r; + r = doSQLDescribeCol(StatementHandle, ColumnNumber, ColumnName, + BufferLength, NameLength, + DataType, ColumnSize, + DecimalDigits, Nullable); + return r; +} + static void init_routine(void) { @@ -1096,7 +1824,7 @@ static int do_field_display_size(TAOS_FIELD *field) { } break; case TSDB_DATA_TYPE_DOUBLE: { - return 12; + return 20; } break; case TSDB_DATA_TYPE_BINARY: @@ -1105,7 +1833,7 @@ static int do_field_display_size(TAOS_FIELD *field) { } break; case TSDB_DATA_TYPE_TIMESTAMP: - return 22; + return 26; break; case TSDB_DATA_TYPE_BOOL: @@ -1162,6 +1890,7 @@ static void do_convert(SQLPOINTER TargetValue, SQLLEN BufferLength, SQLLEN *StrL } if (n>0) *p = '\0'; ((char*)TargetValue)[BufferLength-1] = '\0'; + *StrLen_or_Ind = BufferLength - n; } break; case TSDB_DATA_TYPE_TIMESTAMP: @@ -1175,3 +1904,692 @@ static void do_convert(SQLPOINTER TargetValue, SQLLEN BufferLength, SQLLEN *StrL } } +// convertion from TSDB_DATA_TYPE_XXX to SQL_C_XXX +static SQLRETURN conv_tsdb_bool_to_c_bit(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) +{ + int8_t v = b; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_bool_to_c_tinyint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) +{ + int8_t v = b; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_bool_to_c_short(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) +{ + int16_t v = b; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_bool_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) +{ + int32_t v = b; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_bool_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) +{ + int64_t v = b; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_bool_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) +{ + float v = b; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_bool_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) +{ + double v = b; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_bool_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) +{ + DASSERT(target->len>0); + *target->soi = 1; + if (target->ptr) { + target->ptr[0] = '0' + b; + if (target->len>1) { + target->ptr[1] = '\0'; + } + } + + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_bool_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) +{ + DASSERT(target->len>0); + *target->soi = 1; + target->ptr[0] = '0' + b; + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v1_to_c_tinyint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) +{ + int8_t v = v1; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v1_to_c_short(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) +{ + int16_t v = v1; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v1_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) +{ + int32_t v = v1; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v1_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) +{ + int64_t v = v1; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v1_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) +{ + float v = v1; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v1_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) +{ + double v = v1; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v1_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%d", v1); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_TINYINT -> SQL_C_BIT"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_v1_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%d", v1); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_TINYINT -> SQL_C_BIT"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_v2_to_c_short(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) +{ + int16_t v = v2; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v2_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) +{ + int32_t v = v2; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v2_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) +{ + int64_t v = v2; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v2_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) +{ + float v = v2; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v2_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) +{ + double v = v2; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v2_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%d", v2); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_SMALLINT -> SQL_C_CHAR"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_v2_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%d", v2); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_SMALLINT -> SQL_C_CHAR"); + return SQL_SUCCESS_WITH_INFO; +} + + +static SQLRETURN conv_tsdb_v4_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4) +{ + int32_t v = v4; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v4_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4) +{ + int64_t v = v4; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v4_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4) +{ + float v = v4; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v4_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4) +{ + double v = v4; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v4_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%d", v4); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_INTEGER -> SQL_C_CHAR"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_v4_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%d", v4); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_INTEGER -> SQL_C_BINARY"); + return SQL_SUCCESS_WITH_INFO; +} + + +static SQLRETURN conv_tsdb_v8_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8) +{ + int64_t v = v8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v8_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8) +{ + float v = v8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v8_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8) +{ + double v = v8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_v8_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%ld", v8); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_BIGINT -> SQL_C_CHAR"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_v8_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%ld", v8); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_BIGINT -> SQL_C_BINARY"); + return SQL_SUCCESS_WITH_INFO; +} + + +static SQLRETURN conv_tsdb_f4_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, float f4) +{ + float v = f4; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_f4_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, float f4) +{ + double v = f4; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_f4_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, float f4) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%g", f4); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_FLOAT -> SQL_C_CHAR"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_f4_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, float f4) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%g", f4); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_FLOAT -> SQL_C_BINARY"); + return SQL_SUCCESS_WITH_INFO; +} + + +static SQLRETURN conv_tsdb_f8_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, double f8) +{ + double v = f8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_f8_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, double f8) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%.6f", f8); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_DOUBLE -> SQL_C_CHAR"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_f8_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, double f8) +{ + char buf[64]; + int n = snprintf(buf, sizeof(buf), "%g", f8); + DASSERT(nsoi = n; + if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + if (n<=target->len) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_DOUBLE -> SQL_C_BINARY"); + return SQL_SUCCESS_WITH_INFO; +} + + +static SQLRETURN conv_tsdb_ts_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts) +{ + struct tm tm = {0}; + tm.tm_sec = ts->second; + tm.tm_min = ts->minute; + tm.tm_hour = ts->hour; + tm.tm_mday = ts->day; + tm.tm_mon = ts->month - 1; + tm.tm_year = ts->year - 1900; + time_t t = mktime(&tm); + DASSERT(sizeof(t) == sizeof(int64_t)); + int64_t v = (int64_t)t; + v *= 1000; + v += ts->fraction % 1000; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_ts_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts) +{ + struct tm tm = {0}; + tm.tm_sec = ts->second; + tm.tm_min = ts->minute; + tm.tm_hour = ts->hour; + tm.tm_mday = ts->day; + tm.tm_mon = ts->month - 1; + tm.tm_year = ts->year - 1900; + + char buf[64]; + int n = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm); + DASSERT(n < sizeof(buf)); + + *target->soi = n; + + if (target->ptr) { + snprintf(target->ptr, target->len, "%s.%03d", buf, ts->fraction); + } + + if (n <= target->len) { + return SQL_SUCCESS; + } + + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_TIMESTAMP -> SQL_C_CHAR"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_ts_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts) +{ + struct tm tm = {0}; + tm.tm_sec = ts->second; + tm.tm_min = ts->minute; + tm.tm_hour = ts->hour; + tm.tm_mday = ts->day; + tm.tm_mon = ts->month - 1; + tm.tm_year = ts->year - 1900; + + char buf[64]; + int n = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm); + DASSERT(n < sizeof(buf)); + + *target->soi = n; + + if (target->ptr) memcpy(target->ptr, buf, (n>target->len ? target->len : n)); + + if (n <= target->len) { + return SQL_SUCCESS; + } + + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_TIMESTAMP -> SQL_C_BINARY"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_ts_to_c_ts(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts) +{ + if (target->ptr) memcpy(target->ptr, ts, sizeof(*ts)); + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_bin_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const unsigned char *bin) +{ + size_t n = field->bytes; + *target->soi = n; + if (target->ptr) memcpy(target->ptr, bin, (n>target->len ? target->len : n)); + if (n <= target->len) return SQL_SUCCESS; + + SET_ERROR(sql, "01004", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_BINARY -> SQL_C_CHAR"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_bin_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const unsigned char *bin) +{ + size_t n = field->bytes; + *target->soi = n; + if (target->ptr) memcpy(target->ptr, bin, (n>target->len ? target->len : n)); + if (n <= target->len) return SQL_SUCCESS; + + SET_ERROR(sql, "01004", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_BINARY -> SQL_C_CHAR"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_str_to_c_bit(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) +{ + int bytes = 0; + double f8 = 0; + int n = sscanf(str, "%lf%n", &f8, &bytes); + + int8_t v = f8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + + *target->soi = 1; + + if (n!=1 || bytes!=strlen(str)) { + SET_ERROR(sql, "22018", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_NCHAR -> SQL_C_BIT"); + return SQL_SUCCESS_WITH_INFO; + } + + char buf[64]; + snprintf(buf, sizeof(buf), "%d", v); + + if (strcmp(buf, str)==0) { + if (v==0 || v==1) return SQL_SUCCESS; + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_NCHAR -> SQL_C_BIT"); + return SQL_SUCCESS_WITH_INFO; + } + + if (f8>0 || f8<2) { + SET_ERROR(sql, "01S07", TSDB_CODE_ODBC_CONV_TRUNC, "TSDB_DATA_TYPE_NCHAR -> SQL_C_BIT"); + return SQL_SUCCESS_WITH_INFO; + } + + if (f8<0 || f8>2) { + SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_NCHAR -> SQL_C_BIT"); + return SQL_SUCCESS_WITH_INFO; + } + + SET_ERROR(sql, "01S07", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_NCHAR -> SQL_C_BIT"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_str_to_c_v1(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) +{ + int bytes = 0; + double f8 = 0; + int n = sscanf(str, "%lf%n", &f8, &bytes); + + int8_t v = f8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + + *target->soi = 1; + + if (n!=1 || bytes!=strlen(str)) { + SET_ERROR(sql, "22018", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_NCHAR -> SQL_C_TINYINT"); + return SQL_SUCCESS_WITH_INFO; + } + + char buf[64]; + snprintf(buf, sizeof(buf), "%d", v); + + if (strcmp(buf, str)==0) return SQL_SUCCESS; + + if (f8>INT8_MAX || f8 SQL_C_TINYINT"); + return SQL_SUCCESS_WITH_INFO; + } + + SET_ERROR(sql, "01S07", TSDB_CODE_ODBC_CONV_TRUNC, "TSDB_DATA_TYPE_NCHAR -> SQL_C_TINYINT"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_str_to_c_v2(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) +{ + int bytes = 0; + double f8 = 0; + int n = sscanf(str, "%lf%n", &f8, &bytes); + + int16_t v = f8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + + *target->soi = 2; + + if (n!=1 || bytes!=strlen(str)) { + SET_ERROR(sql, "22018", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_NCHAR -> SQL_C_SHORT"); + return SQL_SUCCESS_WITH_INFO; + } + + char buf[64]; + snprintf(buf, sizeof(buf), "%d", v); + + if (strcmp(buf, str)==0) return SQL_SUCCESS; + + if (f8>INT16_MAX || f8 SQL_C_SHORT"); + return SQL_SUCCESS_WITH_INFO; + } + + SET_ERROR(sql, "01S07", TSDB_CODE_ODBC_CONV_TRUNC, "TSDB_DATA_TYPE_NCHAR -> SQL_C_SHORT"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_str_to_c_v4(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) +{ + int bytes = 0; + double f8 = 0; + int n = sscanf(str, "%lf%n", &f8, &bytes); + + int32_t v = f8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + + *target->soi = 4; + + if (n!=1 || bytes!=strlen(str)) { + SET_ERROR(sql, "22018", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_NCHAR -> SQL_C_LONG"); + return SQL_SUCCESS_WITH_INFO; + } + + char buf[64]; + snprintf(buf, sizeof(buf), "%d", v); + + if (strcmp(buf, str)==0) return SQL_SUCCESS; + + if (f8>INT32_MAX || f8 SQL_C_LONG"); + return SQL_SUCCESS_WITH_INFO; + } + + SET_ERROR(sql, "01S07", TSDB_CODE_ODBC_CONV_TRUNC, "TSDB_DATA_TYPE_NCHAR -> SQL_C_LONG"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_str_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) +{ + int bytes = 0; + double f8 = 0; + int n = sscanf(str, "%lf%n", &f8, &bytes); + + int64_t v = f8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + + *target->soi = 8; + + if (n!=1 || bytes!=strlen(str)) { + SET_ERROR(sql, "22018", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_NCHAR -> SQL_C_SBIGINT"); + return SQL_SUCCESS_WITH_INFO; + } + + char buf[64]; + snprintf(buf, sizeof(buf), "%ld", v); + + if (strcmp(buf, str)==0) return SQL_SUCCESS; + + if (f8>INT64_MAX || f8 SQL_C_SBIGINT"); + return SQL_SUCCESS_WITH_INFO; + } + + SET_ERROR(sql, "01S07", TSDB_CODE_ODBC_CONV_TRUNC, "TSDB_DATA_TYPE_NCHAR -> SQL_C_SBIGINT"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_str_to_c_f4(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) +{ + int bytes = 0; + double f8 = 0; + int n = sscanf(str, "%lf%n", &f8, &bytes); + + float v = f8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + + *target->soi = 4; + + if (n!=1 || bytes!=strlen(str)) { + SET_ERROR(sql, "22018", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_NCHAR -> SQL_C_FLOAT"); + return SQL_SUCCESS_WITH_INFO; + } + + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_str_to_c_f8(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) +{ + int bytes = 0; + double f8 = 0; + int n = sscanf(str, "%lf%n", &f8, &bytes); + + float v = f8; + if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + + *target->soi = 8; + + if (n!=1 || bytes!=strlen(str)) { + SET_ERROR(sql, "22018", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_NCHAR -> SQL_C_DOUBLE"); + return SQL_SUCCESS_WITH_INFO; + } + + return SQL_SUCCESS; +} + +static SQLRETURN conv_tsdb_str_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) +{ + size_t n = strlen(str); + *target->soi = n; + if (target->ptr) strncpy(target->ptr, str, (n < target->len ? n+1 : target->len)); + + if (n < target->len) return SQL_SUCCESS; + + SET_ERROR(sql, "01004", TSDB_CODE_ODBC_CONV_TRUNC, "TSDB_DATA_TYPE_NCHAR -> SQL_C_CHAR"); + return SQL_SUCCESS_WITH_INFO; +} + +static SQLRETURN conv_tsdb_str_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) +{ + size_t n = strlen(str); + *target->soi = n; + if (target->ptr) memcpy(target->ptr, str, (n < target->len ? n : target->len)); + + if (n <= target->len) return SQL_SUCCESS; + + SET_ERROR(sql, "01004", TSDB_CODE_ODBC_CONV_TRUNC, "TSDB_DATA_TYPE_NCHAR -> SQL_C_BINARY"); + return SQL_SUCCESS_WITH_INFO; +} + diff --git a/src/connector/odbc/tests/main.c b/src/connector/odbc/tests/main.c index aa4ce7a05a..1d403002a8 100644 --- a/src/connector/odbc/tests/main.c +++ b/src/connector/odbc/tests/main.c @@ -118,8 +118,7 @@ static int do_insert(SQLHSTMT stmt, data_t data) { if (r) break; fprintf(stderr, "bind 8 [%s]\n", statement); - SQLLEN l8 = SQL_NULL_DATA; - r = SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, ignored, ignored, &data.f8, ignored, &l8); + r = SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, ignored, ignored, &data.f8, ignored, NULL); CHK_RESULT(r, SQL_HANDLE_STMT, stmt); if (r) break; @@ -196,6 +195,7 @@ int main(int argc, char *argv[]) { memset(data.blob, 0, sizeof(data.blob)); snprintf(data.bin, sizeof(data.bin), "hello"); snprintf(data.blob, sizeof(data.blob), "world"); + snprintf(data.blob, sizeof(data.blob), "wo人rld"); SQLHSTMT stmt = {0}; r = SQLAllocHandle(SQL_HANDLE_STMT, conn, &stmt); if (r!=SQL_SUCCESS) break; diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index b5d22ea80c..17aa1fbfd5 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -28,7 +28,7 @@ extern "C" { #else #define TAOS_DEFINE_ERROR(name, mod, code, msg) static const int32_t name = (0x80000000 | ((mod)<<16) | (code)); #endif - + #define TAOS_SYSTEM_ERROR(code) (0x80ff0000 | (code)) #define TAOS_SUCCEEDED(err) ((err) >= 0) #define TAOS_FAILED(err) ((err) < 0) @@ -37,7 +37,7 @@ const char* tstrerror(int32_t err); int32_t* taosGetErrno(); #define terrno (*taosGetErrno()) - + #define TSDB_CODE_SUCCESS 0 #ifdef TAOS_ERROR_C @@ -180,7 +180,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_DND_OUT_OF_MEMORY, 0, 0x0401, "Dnode out TAOS_DEFINE_ERROR(TSDB_CODE_DND_NO_WRITE_ACCESS, 0, 0x0402, "No permission for disk files in dnode") TAOS_DEFINE_ERROR(TSDB_CODE_DND_INVALID_MSG_LEN, 0, 0x0403, "Invalid message length") -// vnode +// vnode TAOS_DEFINE_ERROR(TSDB_CODE_VND_ACTION_IN_PROGRESS, 0, 0x0500, "Action in progress") TAOS_DEFINE_ERROR(TSDB_CODE_VND_MSG_NOT_PROCESSED, 0, 0x0501, "Message not processed") TAOS_DEFINE_ERROR(TSDB_CODE_VND_ACTION_NEED_REPROCESSED, 0, 0x0502, "Action need to be reprocessed") @@ -350,6 +350,12 @@ TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_TAG_VALUE_TOO_LONG, 0, 0x11A4, "tag value TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_VALUE_NULL, 0, 0x11A5, "value not find") TAOS_DEFINE_ERROR(TSDB_CODE_HTTP_OP_VALUE_TYPE, 0, 0x11A6, "value type should be boolean, number or string") + +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_OOM, 0, 0x2101, "out of memory") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_CONV_UNDEF, 0, 0x2102, "convertion undefined") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_CONV_TRUNC, 0, 0x2103, "convertion truncated") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_CONV_NOT_SUPPORT, 0, 0x2104, "convertion not supported") + #ifdef TAOS_ERROR_C }; #endif From 77703ac80981c64a201a332c0ba80f0c9d20749c Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 10 Oct 2020 22:17:28 +0800 Subject: [PATCH 014/101] re-enable jdbc --- src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2ed56d4f81..f619edd221 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,5 +21,5 @@ ADD_SUBDIRECTORY(wal) ADD_SUBDIRECTORY(cq) ADD_SUBDIRECTORY(dnode) ADD_SUBDIRECTORY(connector/odbc) -# ADD_SUBDIRECTORY(connector/jdbc) +ADD_SUBDIRECTORY(connector/jdbc) From e88979de90d2def7906a738ebe1aead344b75e98 Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 11 Oct 2020 06:39:36 +0800 Subject: [PATCH 015/101] add python test code with pyodbc --- src/connector/odbc/tests/odbc.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/connector/odbc/tests/odbc.py diff --git a/src/connector/odbc/tests/odbc.py b/src/connector/odbc/tests/odbc.py new file mode 100644 index 0000000000..b12292b238 --- /dev/null +++ b/src/connector/odbc/tests/odbc.py @@ -0,0 +1,12 @@ +import pyodbc +cnxn = pyodbc.connect('DSN=TAOS_DSN;UID=root;PWD=taosdata', autocommit=True) +cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8') +#cnxn.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8') +#cnxn.setencoding(encoding='utf-8') +cursor = cnxn.cursor() +cursor.execute("SELECT * from db.t") +row = cursor.fetchone() +while row: + print(row) + row = cursor.fetchone() + From 8d325f16930045b995403c11b03abbb690d41a7b Mon Sep 17 00:00:00 2001 From: freemine Date: Tue, 13 Oct 2020 15:20:56 +0800 Subject: [PATCH 016/101] 1. support SQLWCHAR 2. more precise error message, according ODBC doc --- src/client/src/tscPrepare.c | 56 ++ src/connector/odbc/src/todbc.c | 1238 ++++++++++++++++----------- src/connector/odbc/src/todbc_util.c | 226 +++++ src/connector/odbc/src/todbc_util.h | 52 ++ src/connector/odbc/tests/main.c | 2 +- src/connector/odbc/tests/odbc.py | 44 + src/inc/taos.h | 17 + src/inc/taoserror.h | 10 + 8 files changed, 1127 insertions(+), 518 deletions(-) create mode 100644 src/connector/odbc/src/todbc_util.c create mode 100644 src/connector/odbc/src/todbc_util.h diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index b3bb4566be..2f42f8ac56 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -637,3 +637,59 @@ TAOS_RES *taos_stmt_use_result(TAOS_STMT* stmt) { pStmt->pSql = NULL; return result; } + +int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) { + STscStmt* pStmt = (STscStmt*)stmt; + + if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { + terrno = TSDB_CODE_TSC_DISCONNECTED; + return TSDB_CODE_TSC_DISCONNECTED; + } + + SSqlObj* pSql = pStmt->pSql; + SSqlCmd *pCmd = &pSql->cmd; + + *nums = pCmd->numOfParams; + + return TSDB_CODE_SUCCESS; +} + +int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) { + STscStmt* pStmt = (STscStmt*)stmt; + + if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { + terrno = TSDB_CODE_TSC_DISCONNECTED; + return TSDB_CODE_TSC_DISCONNECTED; + } + + SSqlObj* pSql = pStmt->pSql; + SSqlCmd *pCmd = &pSql->cmd; + STableDataBlocks* pBlock = taosArrayGetP(pCmd->pDataBlocks, 0); + + assert(pCmd->numOfParams == pBlock->numOfParams); + if (idx < 0 || idx >= pBlock->numOfParams) return -1; + + SParamInfo* param = pBlock->params + idx; + if (type) *type = param->type; + if (bytes) *bytes = param->bytes; + + return TSDB_CODE_SUCCESS; +} + +const char *taos_data_type(int type) { + switch (type) { + case TSDB_DATA_TYPE_NULL: return "TSDB_DATA_TYPE_NULL"; + case TSDB_DATA_TYPE_BOOL: return "TSDB_DATA_TYPE_BOOL"; + case TSDB_DATA_TYPE_TINYINT: return "TSDB_DATA_TYPE_TINYINT"; + case TSDB_DATA_TYPE_SMALLINT: return "TSDB_DATA_TYPE_SMALLINT"; + case TSDB_DATA_TYPE_INT: return "TSDB_DATA_TYPE_INT"; + case TSDB_DATA_TYPE_BIGINT: return "TSDB_DATA_TYPE_BIGINT"; + case TSDB_DATA_TYPE_FLOAT: return "TSDB_DATA_TYPE_FLOAT"; + case TSDB_DATA_TYPE_DOUBLE: return "TSDB_DATA_TYPE_DOUBLE"; + case TSDB_DATA_TYPE_BINARY: return "TSDB_DATA_TYPE_BINARY"; + case TSDB_DATA_TYPE_TIMESTAMP: return "TSDB_DATA_TYPE_TIMESTAMP"; + case TSDB_DATA_TYPE_NCHAR: return "TSDB_DATA_TYPE_NCHAR"; + default: return "UNKNOWN"; + } +} + diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 6eaaba762f..9576a981fb 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -13,34 +13,22 @@ * along with this program. If not, see . */ +// #define _BSD_SOURCE +#define _XOPEN_SOURCE +#define _DEFAULT_SOURCE + #include "taos.h" #include "os.h" #include "taoserror.h" +#include "todbc_util.h" #include #include -#include -#include -#include -#include #include -#define D(fmt, ...) \ - fprintf(stderr, \ - "%s[%d]:%s() " fmt "\n", \ - basename((char*)__FILE__), __LINE__, __func__, \ - ##__VA_ARGS__) - -#define DASSERT(statement) \ -do { \ - if (statement) break; \ - D("Assertion failure: %s", #statement); \ - abort(); \ -} while (0) - #define GET_REF(obj) atomic_load_64(&obj->refcount) #define INC_REF(obj) atomic_add_fetch_64(&obj->refcount, 1) #define DEC_REF(obj) atomic_sub_fetch_64(&obj->refcount, 1) @@ -76,6 +64,7 @@ do { if (NativeError) *NativeError = obj->err.err_no; \ snprintf((char*)MessageText, BufferLength, "%s", obj->err.err_str); \ if (TextLength && obj->err.err_str) *TextLength = strlen(obj->err.err_str); \ + if (TextLength && obj->err.err_str) *TextLength = utf8_chars(obj->err.err_str); \ } while (0) #define FREE_ERROR(obj) \ @@ -90,7 +79,7 @@ do { \ #define SET_UNSUPPORT_ERROR(obj, sqlstate, err_fmt, ...) \ do { \ - SET_ERROR(obj, sqlstate, TSDB_CODE_COM_OPS_NOT_SUPPORT, err_fmt, ##__VA_ARGS__); \ + SET_ERROR(obj, sqlstate, TSDB_CODE_ODBC_NOT_SUPPORT, err_fmt, ##__VA_ARGS__); \ } while (0) \ #define SET_HANDLE_INVALID(obj, sqlstate, err_fmt, ...) \ @@ -108,6 +97,22 @@ do { \ } \ } while (0) +#define CHK_CONN(obj) \ +do { \ + if (!obj->conn) { \ + SET_ERROR(obj, "HY000", TSDB_CODE_ODBC_INVALID_HANDLE, "connection closed or not ready"); \ + return SQL_ERROR; \ + } \ +} while (0); + +#define CHK_CONN_TAOS(obj) \ +do { \ + if (!obj->conn->taos) { \ + SET_ERROR(obj, "HY000", TSDB_CODE_ODBC_INVALID_HANDLE, "connection to data source closed or not ready"); \ + return SQL_ERROR; \ + } \ +} while (0); + #define CHK_RS(r_091c, sql_091c, fmt_091c, ...) \ do { \ r_091c = SQL_ERROR; \ @@ -184,7 +189,6 @@ struct sql_s { conn_t *conn; TAOS_STMT *stmt; - TAOS_BIND *binds; param_bind_t *params; int n_params; TAOS_RES *rs; @@ -205,8 +209,9 @@ struct c_target_s { static pthread_once_t init_once = PTHREAD_ONCE_INIT; static void init_routine(void); + + static int do_field_display_size(TAOS_FIELD *field); -static void do_convert(SQLPOINTER TargetValue, SQLLEN BufferLength, SQLLEN *StrLen_or_Ind, TAOS_FIELD *field, void *row); static SQLRETURN doSQLAllocEnv(SQLHENV *EnvironmentHandle) { @@ -269,7 +274,7 @@ static SQLRETURN doSQLAllocConnect(SQLHENV EnvironmentHandle, do { conn = (conn_t*)calloc(1, sizeof(*conn)); if (!conn) { - SET_ERROR(env, "HY000", TSDB_CODE_ODBC_OOM, "failed to alloc connection handle"); + SET_ERROR(env, "HY001", TSDB_CODE_ODBC_OOM, ""); break; } @@ -336,7 +341,7 @@ static SQLRETURN doSQLConnect(SQLHDBC ConnectionHandle, if (!conn) return SQL_ERROR; if (conn->taos) { - SET_ERROR(conn, "HY000", TSDB_CODE_TSC_APP_ERROR, "connection still in use"); + SET_ERROR(conn, "08002", TSDB_CODE_ODBC_CONNECTION_BUSY, "connection still in use"); return SQL_ERROR; } @@ -346,7 +351,7 @@ static SQLRETURN doSQLConnect(SQLHDBC ConnectionHandle, do { if ((ServerName && !serverName) || (UserName && !userName) || (Authentication && !auth)) { - SET_ERROR(conn, "HY000", TSDB_CODE_ODBC_OOM, "failed to allocate resources"); + SET_ERROR(conn, "HY001", TSDB_CODE_ODBC_OOM, ""); break; } @@ -354,7 +359,7 @@ static SQLRETURN doSQLConnect(SQLHDBC ConnectionHandle, // TODO: shall receive ip/port from odbc.ini conn->taos = taos_connect("localhost", userName, auth, NULL, 0); if (!conn->taos) { - SET_ERROR(conn, "HY000", terrno, "failed to connect to data source"); + SET_ERROR(conn, "08001", terrno, "failed to connect to data source"); break; } } while (0); @@ -409,7 +414,7 @@ static SQLRETURN doSQLAllocStmt(SQLHDBC ConnectionHandle, do { sql_t *sql = (sql_t*)calloc(1, sizeof(*sql)); if (!sql) { - SET_ERROR(conn, "HY000", TSDB_CODE_ODBC_OOM, "failed to alloc statement handle"); + SET_ERROR(conn, "HY001", TSDB_CODE_ODBC_OOM, ""); break; } @@ -442,7 +447,7 @@ static SQLRETURN doSQLFreeStmt(SQLHSTMT StatementHandle, if (Option == SQL_CLOSE) return SQL_SUCCESS; if (Option != SQL_DROP) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "free statement with Option[%x] not supported yet", Option); + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NOT_SUPPORT, "free statement with Option[%x] not supported yet", Option); return SQL_ERROR; } @@ -458,10 +463,6 @@ static SQLRETURN doSQLFreeStmt(SQLHSTMT StatementHandle, sql->stmt = NULL; } - if (sql->binds) { - free(sql->binds); - sql->binds = NULL; - } if (sql->params) { free(sql->params); sql->params = NULL; @@ -494,15 +495,8 @@ static SQLRETURN doSQLExecDirect(SQLHSTMT StatementHandle, sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); - return SQL_ERROR; - } - - if (!sql->conn->taos) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); - return SQL_ERROR; - } + CHK_CONN(sql); + CHK_CONN_TAOS(sql); if (sql->rs) { taos_free_result(sql->rs); @@ -515,11 +509,6 @@ static SQLRETURN doSQLExecDirect(SQLHSTMT StatementHandle, sql->stmt = NULL; } - if (sql->binds) { - free(sql->binds); - sql->binds = NULL; - } - if (sql->params) { free(sql->params); sql->params = NULL; @@ -531,11 +520,11 @@ static SQLRETURN doSQLExecDirect(SQLHSTMT StatementHandle, SQLRETURN r = SQL_ERROR; do { if (!stxt) { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_OOM, "failed to allocate resources"); + SET_ERROR(sql, "HY001", TSDB_CODE_ODBC_OOM, ""); break; } sql->rs = taos_query(sql->conn->taos, stxt); - CHK_RS(r, sql, "failed to query"); + CHK_RS(r, sql, "failed to execute"); } while (0); SFRE(stxt, StatementText, TextLength); @@ -551,24 +540,24 @@ SQLRETURN SQL_API SQLExecDirect(SQLHSTMT StatementHandle, return r; } +SQLRETURN SQL_API SQLExecDirectW(SQLHSTMT hstmt, SQLWCHAR *szSqlStr, SQLINTEGER cbSqlStr) +{ + size_t bytes = 0; + SQLCHAR *utf8 = wchars_to_chars(szSqlStr, cbSqlStr, &bytes); + return SQLExecDirect(hstmt, utf8, bytes); +} + static SQLRETURN doSQLNumResultCols(SQLHSTMT StatementHandle, SQLSMALLINT *ColumnCount) { sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); - return SQL_ERROR; - } - - if (!sql->conn->taos) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); - return SQL_ERROR; - } + CHK_CONN(sql); + CHK_CONN_TAOS(sql); if (!sql->rs) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NO_RESULT, ""); return SQL_ERROR; } @@ -594,18 +583,11 @@ static SQLRETURN doSQLRowCount(SQLHSTMT StatementHandle, sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); - return SQL_ERROR; - } - - if (!sql->conn->taos) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); - return SQL_ERROR; - } + CHK_CONN(sql); + CHK_CONN_TAOS(sql); if (!sql->rs) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NO_RESULT, ""); return SQL_ERROR; } @@ -629,22 +611,14 @@ static SQLRETURN doSQLColAttribute(SQLHSTMT StatementHandle, SQLPOINTER CharacterAttribute, SQLSMALLINT BufferLength, SQLSMALLINT *StringLength, SQLLEN *NumericAttribute ) { - D("......"); sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); - return SQL_ERROR; - } - - if (!sql->conn->taos) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); - return SQL_ERROR; - } + CHK_CONN(sql); + CHK_CONN_TAOS(sql); if (!sql->rs) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NO_RESULT, ""); return SQL_ERROR; } @@ -652,22 +626,12 @@ static SQLRETURN doSQLColAttribute(SQLHSTMT StatementHandle, TAOS_FIELD *fields = taos_fetch_fields(sql->rs); if (nfields==0 || fields==NULL) { - SET_ERROR(sql, "HY000", TSDB_CODE_MND_FIELD_NOT_EXIST, "no fields in result set"); + SET_ERROR(sql, "07005", TSDB_CODE_ODBC_NO_FIELDS, ""); return SQL_ERROR; } - if (ColumnNumber<0) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_APP_ERROR, "ColumnNumber[%d] underflow", ColumnNumber); - return SQL_ERROR; - } - - if (ColumnNumber==0) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "ColumnNumber[0] not supported"); - return SQL_ERROR; - } - - if (ColumnNumber>nfields) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_APP_ERROR, "ColumnNumber[%d] overflow", ColumnNumber); + if (ColumnNumber<=0 || ColumnNumber>nfields) { + SET_ERROR(sql, "07009", TSDB_CODE_ODBC_OUT_OF_RANGE, "invalid column number [%d]", ColumnNumber); return SQL_ERROR; } @@ -685,9 +649,9 @@ static SQLRETURN doSQLColAttribute(SQLHSTMT StatementHandle, *NumericAttribute = SQL_FALSE; } break; default: { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, - "ColumnNumber[%d] FieldIdentifier[%d] not supported yet", - ColumnNumber, FieldIdentifier); + SET_ERROR(sql, "HY091", TSDB_CODE_ODBC_OUT_OF_RANGE, + "FieldIdentifier[%d/0x%x] for Column [%d] not supported yet", + FieldIdentifier, FieldIdentifier, ColumnNumber); return SQL_ERROR; } break; } @@ -772,23 +736,16 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); - return SQL_ERROR; - } - - if (!sql->conn->taos) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); - return SQL_ERROR; - } + CHK_CONN(sql); + CHK_CONN_TAOS(sql); if (!sql->rs) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NO_RESULT, ""); return SQL_ERROR; } if (!sql->row) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no rows cached or not ready"); + SET_ERROR(sql, "24000", TSDB_CODE_ODBC_INVALID_CURSOR, ""); return SQL_ERROR; } @@ -797,23 +754,8 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, int nfields = taos_field_count(sql->rs); TAOS_FIELD *fields = taos_fetch_fields(sql->rs); - if (nfields==0 || fields==NULL) { - SET_ERROR(sql, "HY000", TSDB_CODE_MND_FIELD_NOT_EXIST, "no fields in result set"); - return SQL_ERROR; - } - - if (ColumnNumber<0) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_APP_ERROR, "ColumnNumber[%d] underflow", ColumnNumber); - return SQL_ERROR; - } - - if (ColumnNumber==0) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "ColumnNumber[0] not supported"); - return SQL_ERROR; - } - - if (ColumnNumber>nfields) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_APP_ERROR, "ColumnNumber[%d] overflow", ColumnNumber); + if (ColumnNumber<=0 || ColumnNumber>nfields) { + SET_ERROR(sql, "07009", TSDB_CODE_ODBC_OUT_OF_RANGE, "invalid column number [%d]", ColumnNumber); return SQL_ERROR; } @@ -834,8 +776,6 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, target.len = BufferLength; target.soi = StrLen_or_Ind; - SQLRETURN r = SQL_ERROR; - switch (field->type) { case TSDB_DATA_TYPE_BOOL: { int8_t v = *(int8_t*)row; @@ -851,7 +791,9 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_CHAR: return conv_tsdb_bool_to_c_char(sql, &target, field, v); case SQL_C_BINARY: return conv_tsdb_bool_to_c_binary(sql, &target, field, v); default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, + "no convertion from [%s] to [%s[%d][0x%x]] for col [%d]", + taos_data_type(field->type), sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } } @@ -868,7 +810,9 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_CHAR: return conv_tsdb_v1_to_c_char(sql, &target, field, v); case SQL_C_BINARY: return conv_tsdb_v1_to_c_binary(sql, &target, field, v); default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, + "no convertion from [%s] to [%s[%d][0x%x]] for col [%d]", + taos_data_type(field->type), sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } } @@ -884,7 +828,9 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_CHAR: return conv_tsdb_v2_to_c_char(sql, &target, field, v); case SQL_C_BINARY: return conv_tsdb_v2_to_c_binary(sql, &target, field, v); default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, + "no convertion from [%s] to [%s[%d][0x%x]] for col [%d]", + taos_data_type(field->type), sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } } @@ -899,7 +845,9 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_CHAR: return conv_tsdb_v4_to_c_char(sql, &target, field, v); case SQL_C_BINARY: return conv_tsdb_v4_to_c_binary(sql, &target, field, v); default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, + "no convertion from [%s] to [%s[%d][0x%x]] for col [%d]", + taos_data_type(field->type), sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } } @@ -913,7 +861,9 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_CHAR: return conv_tsdb_v8_to_c_char(sql, &target, field, v); case SQL_C_BINARY: return conv_tsdb_v8_to_c_binary(sql, &target, field, v); default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, + "no convertion from [%s] to [%s[%d][0x%x]] for col [%d]", + taos_data_type(field->type), sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } } @@ -926,7 +876,9 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_CHAR: return conv_tsdb_f4_to_c_char(sql, &target, field, v); case SQL_C_BINARY: return conv_tsdb_f4_to_c_binary(sql, &target, field, v); default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, + "no convertion from [%s] to [%s[%d][0x%x]] for col [%d]", + taos_data_type(field->type), sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } } @@ -938,7 +890,9 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_CHAR: return conv_tsdb_f8_to_c_char(sql, &target, field, v); case SQL_C_BINARY: return conv_tsdb_f8_to_c_binary(sql, &target, field, v); default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, + "no convertion from [%s] to [%s[%d][0x%x]] for col [%d]", + taos_data_type(field->type), sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } } @@ -962,7 +916,9 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_BINARY: return conv_tsdb_ts_to_c_bin(sql, &target, field, &ts); case SQL_C_TIMESTAMP: return conv_tsdb_ts_to_c_ts(sql, &target, field, &ts); default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, + "no convertion from [%s] to [%s[%d][0x%x]] for col [%d]", + taos_data_type(field->type), sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } } @@ -973,7 +929,9 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_CHAR: return conv_tsdb_bin_to_c_str(sql, &target, field, bin); case SQL_C_BINARY: return conv_tsdb_bin_to_c_bin(sql, &target, field, bin); default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, + "no convertion from [%s] to [%s[%d][0x%x]] for col [%d]", + taos_data_type(field->type), sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } } @@ -991,81 +949,21 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_CHAR: return conv_tsdb_str_to_c_str(sql, &target, field, str); case SQL_C_BINARY: return conv_tsdb_str_to_c_bin(sql, &target, field, str); default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "from TSDB_DATA_TYPE [%d] to SQL_C_TYPE [%d] not supported", field->type, target.ct); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, + "no convertion from [%s] to [%s[%d][0x%x]] for col [%d]", + taos_data_type(field->type), sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } } } break; default: { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_CONV_NOT_SUPPORT, "field [@%d] type [%d] not supported yet", ColumnNumber, field->type); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for col [%d]", + taos_data_type(field->type), field->type, field->type, + sql_c_type(target.ct), target.ct, target.ct, ColumnNumber); return SQL_ERROR; } break; } - - if (1) return r; - - switch (TargetType) { - case SQL_C_CHAR: { - do_convert(TargetValue, BufferLength, StrLen_or_Ind, field, row); - } break; - case SQL_C_TIMESTAMP: { - TIMESTAMP_STRUCT ts = {0}; - DASSERT(BufferLength == sizeof(ts)); - int64_t v = *(int64_t*)row; - time_t t = v/1000; - struct tm tm = {0}; - localtime_r(&t, &tm); - ts.year = tm.tm_year + 1900; - ts.month = tm.tm_mon + 1; - ts.day = tm.tm_mday; - ts.hour = tm.tm_hour; - ts.minute = tm.tm_min; - ts.second = tm.tm_sec; - ts.fraction = 0; - - memcpy(TargetValue, &ts, sizeof(ts)); - } break; - case SQL_C_LONG: { - int32_t v = *(int32_t*)row; - DASSERT(BufferLength == sizeof(v)); - memcpy(TargetValue, &v, sizeof(v)); - } break; - case SQL_C_SBIGINT: { - int64_t v = *(int64_t*)row; - DASSERT(BufferLength == sizeof(v)); - memcpy(TargetValue, &v, sizeof(v)); - } break; - case SQL_C_FLOAT: { - float v = *(float*)row; - DASSERT(BufferLength == sizeof(v)); - memcpy(TargetValue, &v, sizeof(v)); - } break; - case SQL_C_DOUBLE: { - double v = *(double*)row; - DASSERT(BufferLength == sizeof(v)); - memcpy(TargetValue, &v, sizeof(v)); - } break; - case SQL_C_BINARY: { - if (StrLen_or_Ind) { - if (field->type == TSDB_DATA_TYPE_NCHAR) { - *StrLen_or_Ind = strnlen((const char*)row, field->bytes); - } else { - *StrLen_or_Ind = field->bytes; - } - } - size_t n = field->bytes; - if (n>BufferLength) n = BufferLength; - memcpy(TargetValue, (const char*)row, n); - } break; - default: { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, - "ColumnNumber[%d] TargetType[%d] BufferLength[%ld] not supported yet", - ColumnNumber, TargetType, BufferLength); - return SQL_ERROR; - } break; - } - - return SQL_SUCCESS; } SQLRETURN SQL_API SQLGetData(SQLHSTMT StatementHandle, @@ -1085,18 +983,11 @@ static SQLRETURN doSQLFetch(SQLHSTMT StatementHandle) sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); - return SQL_ERROR; - } - - if (!sql->conn->taos) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); - return SQL_ERROR; - } + CHK_CONN(sql); + CHK_CONN_TAOS(sql); if (!sql->rs) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NO_RESULT, ""); return SQL_ERROR; } @@ -1117,15 +1008,8 @@ static SQLRETURN doSQLPrepare(SQLHSTMT StatementHandle, sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); - return SQL_ERROR; - } - - if (!sql->conn->taos) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); - return SQL_ERROR; - } + CHK_CONN(sql); + CHK_CONN_TAOS(sql); if (sql->rs) { taos_free_result(sql->rs); @@ -1138,10 +1022,6 @@ static SQLRETURN doSQLPrepare(SQLHSTMT StatementHandle, sql->stmt = NULL; } - if (sql->binds) { - free(sql->binds); - sql->binds = NULL; - } if (sql->params) { free(sql->params); sql->params = NULL; @@ -1151,13 +1031,13 @@ static SQLRETURN doSQLPrepare(SQLHSTMT StatementHandle, do { sql->stmt = taos_stmt_init(sql->conn->taos); if (!sql->stmt) { - SET_ERROR(sql, "HY000", terrno, "failed to initialize statement internally"); + SET_ERROR(sql, "HY001", terrno, "failed to initialize TAOS statement internally"); break; } int r = taos_stmt_prepare(sql->stmt, (const char *)StatementText, TextLength); if (r) { - SET_ERROR(sql, "HY000", r, "failed to prepare a statement"); + SET_ERROR(sql, "HY000", r, "failed to prepare a TAOS statement"); taos_stmt_close(sql->stmt); sql->stmt = NULL; break; @@ -1175,89 +1055,525 @@ SQLRETURN SQL_API SQLPrepare(SQLHSTMT StatementHandle, return r; } -static SQLRETURN doSQLExecute(SQLHSTMT StatementHandle) +static const int yes = 1; +static const int no = 0; + +static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, TAOS_BIND *bind) { - sql_t *sql = (sql_t*)StatementHandle; - if (!sql) return SQL_ERROR; - - if (!sql->conn) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); + if (!param->valid) { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NOT_SUPPORT, "parameter [@%d] not bound yet", idx+1); + return SQL_ERROR; + } + if (param->StrLen_or_Ind && *param->StrLen_or_Ind == SQL_NULL_DATA) { + bind->is_null = (int*)&yes; + return SQL_SUCCESS; + } + bind->is_null = (int*)&no; + int type = 0; + int bytes = 0; + int r = taos_stmt_get_param(sql->stmt, idx, &type, &bytes); + if (r) { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_OUT_OF_RANGE, "parameter [@%d] not valid", idx+1); return SQL_ERROR; } - if (!sql->conn->taos) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); - return SQL_ERROR; - } - - if (!sql->stmt) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_SQL, "no statement cached or not ready"); - return SQL_ERROR; - } - - if (sql->rs) { - taos_free_result(sql->rs); - sql->rs = NULL; - sql->row = NULL; - } - - int r = 0; - - for (int i=0; in_params; ++i) { - param_bind_t *pb = sql->params + i; - if (!pb->valid) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "default parameter [@%d] not supported yet", i+1); - return SQL_ERROR; - } - TAOS_BIND *b = sql->binds + i; - int yes = 1; - int no = 0; - if (pb->StrLen_or_Ind && *pb->StrLen_or_Ind == SQL_NULL_DATA) { - b->is_null = &yes; - } else { - b->is_null = &no; - switch (b->buffer_type) { - case TSDB_DATA_TYPE_BOOL: - case TSDB_DATA_TYPE_TINYINT: - case TSDB_DATA_TYPE_SMALLINT: - case TSDB_DATA_TYPE_INT: - case TSDB_DATA_TYPE_BIGINT: - case TSDB_DATA_TYPE_FLOAT: - case TSDB_DATA_TYPE_DOUBLE: - case TSDB_DATA_TYPE_TIMESTAMP: { - b->length = &b->buffer_length; - b->buffer = pb->ParameterValue; + // ref: https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/converting-data-from-c-to-sql-data-types?view=sql-server-ver15 + switch (type) { + case TSDB_DATA_TYPE_BOOL: { + bind->buffer_type = type; + bind->buffer_length = sizeof(bind->u.b); + bind->buffer = &bind->u.b; + bind->length = &bind->buffer_length; + switch (param->ValueType) { + case SQL_C_LONG: { + bind->u.b = *(int32_t*)param->ParameterValue; } break; - case TSDB_DATA_TYPE_BINARY: - case TSDB_DATA_TYPE_NCHAR: { - if (!pb->StrLen_or_Ind) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "value [@%d] bad StrLen_or_Ind", i+1); - return SQL_ERROR; - } - size_t n = *pb->StrLen_or_Ind; - if (n == SQL_NTS) { - n = strlen(pb->ParameterValue); - } else if (n < 0 || n > b->buffer_length) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "value [@%d] bad StrLen_or_Ind", i+1); - return SQL_ERROR; - } - - b->buffer_length = n; - b->length = &b->buffer_length; - b->buffer = pb->ParameterValue; + case SQL_C_BIT: { + bind->u.b = *(int8_t*)param->ParameterValue; } break; + case SQL_C_CHAR: + case SQL_C_WCHAR: + case SQL_C_SHORT: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_FLOAT: + case SQL_C_DOUBLE: + case SQL_C_TINYINT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_SBIGINT: + case SQL_C_UBIGINT: + case SQL_C_BINARY: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: default: { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "value [@%d] not supported yet", i+1); + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; } - } + } break; + case TSDB_DATA_TYPE_TINYINT: { + bind->buffer_type = type; + bind->buffer_length = sizeof(bind->u.v1); + bind->buffer = &bind->u.v1; + bind->length = &bind->buffer_length; + switch (param->ValueType) { + case SQL_C_TINYINT: { + bind->u.v1 = *(int8_t*)param->ParameterValue; + } break; + case SQL_C_SHORT: { + bind->u.v1 = *(int16_t*)param->ParameterValue; + } break; + case SQL_C_LONG: { + bind->u.v1 = *(int32_t*)param->ParameterValue; + } break; + case SQL_C_SBIGINT: { + bind->u.v1 = *(int64_t*)param->ParameterValue; + } break; + case SQL_C_CHAR: + case SQL_C_WCHAR: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_FLOAT: + case SQL_C_DOUBLE: + case SQL_C_BIT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_UBIGINT: + case SQL_C_BINARY: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } break; + } + } break; + case TSDB_DATA_TYPE_SMALLINT: { + bind->buffer_type = type; + bind->buffer_length = sizeof(bind->u.v2); + bind->buffer = &bind->u.v2; + bind->length = &bind->buffer_length; + switch (param->ValueType) { + case SQL_C_LONG: { + bind->u.v2 = *(int32_t*)param->ParameterValue; + } break; + case SQL_C_SHORT: { + bind->u.v2 = *(int16_t*)param->ParameterValue; + } break; + case SQL_C_CHAR: + case SQL_C_WCHAR: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_FLOAT: + case SQL_C_DOUBLE: + case SQL_C_BIT: + case SQL_C_TINYINT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_SBIGINT: + case SQL_C_UBIGINT: + case SQL_C_BINARY: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } break; + } + } break; + case TSDB_DATA_TYPE_INT: { + bind->buffer_type = type; + bind->buffer_length = sizeof(bind->u.v4); + bind->buffer = &bind->u.v4; + bind->length = &bind->buffer_length; + switch (param->ValueType) { + case SQL_C_LONG: { + bind->u.v4 = *(int32_t*)param->ParameterValue; + } break; + case SQL_C_CHAR: + case SQL_C_WCHAR: + case SQL_C_SHORT: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_FLOAT: + case SQL_C_DOUBLE: + case SQL_C_BIT: + case SQL_C_TINYINT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_SBIGINT: + case SQL_C_UBIGINT: + case SQL_C_BINARY: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } break; + } + } break; + case TSDB_DATA_TYPE_BIGINT: { + bind->buffer_type = type; + bind->buffer_length = sizeof(bind->u.v8); + bind->buffer = &bind->u.v8; + bind->length = &bind->buffer_length; + switch (param->ValueType) { + case SQL_C_SBIGINT: { + bind->u.v8 = *(int64_t*)param->ParameterValue; + } break; + case SQL_C_LONG: { + bind->u.v8 = *(int32_t*)param->ParameterValue; + } break; + case SQL_C_CHAR: + case SQL_C_WCHAR: + case SQL_C_SHORT: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_FLOAT: + case SQL_C_DOUBLE: + case SQL_C_BIT: + case SQL_C_TINYINT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_UBIGINT: + case SQL_C_BINARY: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } break; + } + } break; + case TSDB_DATA_TYPE_FLOAT: { + bind->buffer_type = type; + bind->buffer_length = sizeof(bind->u.f4); + bind->buffer = &bind->u.f4; + bind->length = &bind->buffer_length; + switch (param->ValueType) { + case SQL_C_DOUBLE: { + bind->u.f4 = *(double*)param->ParameterValue; + } break; + case SQL_C_FLOAT: { + bind->u.f4 = *(float*)param->ParameterValue; + } break; + case SQL_C_CHAR: + case SQL_C_WCHAR: + case SQL_C_SHORT: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_LONG: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_BIT: + case SQL_C_TINYINT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_SBIGINT: + case SQL_C_UBIGINT: + case SQL_C_BINARY: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } break; + } + } break; + case TSDB_DATA_TYPE_DOUBLE: { + bind->buffer_type = type; + bind->buffer_length = sizeof(bind->u.f8); + bind->buffer = &bind->u.f8; + bind->length = &bind->buffer_length; + switch (param->ValueType) { + case SQL_C_DOUBLE: { + bind->u.f8 = *(double*)param->ParameterValue; + } break; + case SQL_C_CHAR: + case SQL_C_WCHAR: + case SQL_C_SHORT: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_LONG: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_FLOAT: + case SQL_C_BIT: + case SQL_C_TINYINT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_SBIGINT: + case SQL_C_UBIGINT: + case SQL_C_BINARY: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } break; + } + } break; + case TSDB_DATA_TYPE_BINARY: { + bind->buffer_type = type; + bind->length = &bind->buffer_length; + switch (param->ValueType) { + case SQL_C_WCHAR: { + DASSERT(param->StrLen_or_Ind); + DASSERT(*param->StrLen_or_Ind != SQL_NTS); + size_t bytes = 0; + SQLCHAR *utf8 = wchars_to_chars(param->ParameterValue, *param->StrLen_or_Ind/2, &bytes); + bind->allocated = 1; + bind->u.bin = utf8; + bind->buffer_length = bytes; + bind->buffer = bind->u.bin; + } break; + case SQL_C_BINARY: { + bind->u.bin = (unsigned char*)param->ParameterValue; + if (*param->StrLen_or_Ind == SQL_NTS) { + bind->buffer_length = strlen((const char*)param->ParameterValue); + } else { + bind->buffer_length = *param->StrLen_or_Ind; + } + bind->buffer = bind->u.bin; + } break; + case SQL_C_CHAR: + case SQL_C_SHORT: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_LONG: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_FLOAT: + case SQL_C_DOUBLE: + case SQL_C_BIT: + case SQL_C_TINYINT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_SBIGINT: + case SQL_C_UBIGINT: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } break; + } + } break; + case TSDB_DATA_TYPE_TIMESTAMP: { + bind->buffer_type = type; + bind->buffer_length = sizeof(bind->u.v8); + bind->buffer = &bind->u.v8; + bind->length = &bind->buffer_length; + switch (param->ValueType) { + case SQL_C_WCHAR: { + DASSERT(param->StrLen_or_Ind); + DASSERT(*param->StrLen_or_Ind != SQL_NTS); + size_t bytes = 0; + SQLCHAR *utf8 = wchars_to_chars(param->ParameterValue, *param->StrLen_or_Ind/2, &bytes); + struct tm tm = {0}; + strptime((const char*)utf8, "%Y-%m-%d %H:%M:%S", &tm); + int64_t t = (int64_t)mktime(&tm); + t *= 1000; + bind->u.v8 = t; + free(utf8); + } break; + case SQL_C_SBIGINT: { + int64_t t = *(int64_t*)param->ParameterValue; + bind->u.v8 = t; + } break; + case SQL_C_SHORT: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_LONG: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_FLOAT: + case SQL_C_DOUBLE: + case SQL_C_BIT: + case SQL_C_TINYINT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_UBIGINT: + case SQL_C_BINARY: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } break; + } + } break; + case TSDB_DATA_TYPE_NCHAR: { + bind->buffer_type = type; + bind->length = &bind->buffer_length; + switch (param->ValueType) { + case SQL_C_WCHAR: { + DASSERT(param->StrLen_or_Ind); + DASSERT(*param->StrLen_or_Ind != SQL_NTS); + size_t bytes = 0; + SQLCHAR *utf8 = wchars_to_chars(param->ParameterValue, *param->StrLen_or_Ind/2, &bytes); + bind->allocated = 1; + bind->u.nchar = (char*)utf8; + bind->buffer_length = bytes; + bind->buffer = bind->u.nchar; + } break; + case SQL_C_CHAR: { + bind->u.nchar = (char*)param->ParameterValue; + if (*param->StrLen_or_Ind == SQL_NTS) { + bind->buffer_length = strlen((const char*)param->ParameterValue); + } else { + bind->buffer_length = *param->StrLen_or_Ind; + } + bind->buffer = bind->u.nchar; + } break; + case SQL_C_SHORT: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_LONG: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_FLOAT: + case SQL_C_DOUBLE: + case SQL_C_BIT: + case SQL_C_TINYINT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_SBIGINT: + case SQL_C_UBIGINT: + case SQL_C_BINARY: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } break; + } + } break; + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(param->ValueType), param->ValueType, param->ValueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } break; + } + return SQL_SUCCESS; +} + +static SQLRETURN do_execute(sql_t *sql, TAOS_BIND *binds) +{ + SQLRETURN r = SQL_SUCCESS; + for (int i=0; in_params; ++i) { + r = do_bind_param_value(sql, i, sql->params+i, binds+i); + if (r==SQL_SUCCESS) continue; + return r; } if (sql->n_params > 0) { - PROFILE(r = taos_stmt_bind_param(sql->stmt, sql->binds)); + PROFILE(r = taos_stmt_bind_param(sql->stmt, binds)); if (r) { - SET_ERROR(sql, "HY000", r, "failed to bind parameters"); + SET_ERROR(sql, "HY000", r, "failed to bind parameters[%d in total]", sql->n_params); return SQL_ERROR; } @@ -1274,12 +1590,54 @@ static SQLRETURN doSQLExecute(SQLHSTMT StatementHandle) return SQL_ERROR; } - SQLRETURN ret = SQL_ERROR; - PROFILE(sql->rs = taos_stmt_use_result(sql->stmt)); - CHK_RS(ret, sql, "failed to use result"); + CHK_RS(r, sql, "failed to use result"); - return ret; + return r; +} + +static SQLRETURN doSQLExecute(SQLHSTMT StatementHandle) +{ + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + + CHK_CONN(sql); + CHK_CONN_TAOS(sql); + + if (!sql->stmt) { + SET_ERROR(sql, "HY010", TSDB_CODE_ODBC_STATEMENT_NOT_READY, ""); + return SQL_ERROR; + } + + if (sql->rs) { + taos_free_result(sql->rs); + sql->rs = NULL; + sql->row = NULL; + } + + TAOS_BIND *binds = NULL; + if (sql->n_params>0) { + binds = (TAOS_BIND*)calloc(sql->n_params, sizeof(*binds)); + if (!binds) { + SET_ERROR(sql, "HY001", TSDB_CODE_ODBC_OOM, ""); + return SQL_ERROR; + } + } + + SQLRETURN r = do_execute(sql, binds); + + if (binds) { + for (int i = 0; in_params; ++i) { + TAOS_BIND *bind = binds + i; + if (bind->allocated) { + free(bind->u.nchar); + bind->u.nchar = NULL; + } + } + free(binds); + } + + return r; } SQLRETURN SQL_API SQLExecute(SQLHSTMT StatementHandle) @@ -1373,159 +1731,30 @@ static SQLRETURN doSQLBindParameter( sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); - return SQL_ERROR; - } - - if (!sql->conn->taos) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); - return SQL_ERROR; - } + CHK_CONN(sql); + CHK_CONN_TAOS(sql); if (!sql->stmt) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_SQL, "no statement cached or not ready"); + SET_ERROR(sql, "HY010", TSDB_CODE_ODBC_STATEMENT_NOT_READY, ""); return SQL_ERROR; } if (fParamType != SQL_PARAM_INPUT) { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "non-input parameter [@%d] not supported yet", ParameterNumber); + SET_ERROR(sql, "HY105", TSDB_CODE_ODBC_NOT_SUPPORT, "non-input parameter [@%d] not supported yet", ParameterNumber); return SQL_ERROR; } - int buffer_type = 0; - - // ref: https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/converting-data-from-c-to-sql-data-types?view=sql-server-ver15 - switch (ValueType) { - case SQL_C_BIT: { - switch (ParameterType) { - case SQL_BIT: buffer_type = TSDB_DATA_TYPE_BOOL; break; - case SQL_TINYINT: buffer_type = TSDB_DATA_TYPE_TINYINT; break; - case SQL_SMALLINT: buffer_type = TSDB_DATA_TYPE_SMALLINT; break; - case SQL_INTEGER: buffer_type = TSDB_DATA_TYPE_INT; break; - case SQL_BIGINT: buffer_type = TSDB_DATA_TYPE_BIGINT; break; - case SQL_FLOAT: buffer_type = TSDB_DATA_TYPE_FLOAT; break; - case SQL_DOUBLE: buffer_type = TSDB_DATA_TYPE_DOUBLE; break; - case SQL_VARCHAR: buffer_type = TSDB_DATA_TYPE_NCHAR; break; - default: { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, - "parameter[@%d] no conversion from [%d] to [%d]", - ParameterNumber, ValueType, ParameterType); - return SQL_ERROR; - } break; - } - } break; - case SQL_C_TINYINT: - case SQL_C_SHORT: - case SQL_C_LONG: - case SQL_C_SBIGINT: - case SQL_C_FLOAT: - case SQL_C_DOUBLE: - case SQL_C_NUMERIC: { - switch (ParameterType) { - case SQL_BIT: buffer_type = TSDB_DATA_TYPE_BOOL; break; - case SQL_TINYINT: buffer_type = TSDB_DATA_TYPE_TINYINT; break; - case SQL_SMALLINT: buffer_type = TSDB_DATA_TYPE_SMALLINT; break; - case SQL_INTEGER: buffer_type = TSDB_DATA_TYPE_INT; break; - case SQL_BIGINT: buffer_type = TSDB_DATA_TYPE_BIGINT; break; - case SQL_FLOAT: buffer_type = TSDB_DATA_TYPE_FLOAT; break; - case SQL_DOUBLE: buffer_type = TSDB_DATA_TYPE_DOUBLE; break; - case SQL_VARCHAR: buffer_type = TSDB_DATA_TYPE_NCHAR; break; - case SQL_TIMESTAMP: buffer_type = TSDB_DATA_TYPE_TIMESTAMP; break; // extention to taos - default: { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, - "parameter[@%d] no conversion from [%d] to [%d]", - ParameterNumber, ValueType, ParameterType); - return SQL_ERROR; - } break; - } - } break; - case SQL_C_DATE: - case SQL_C_TIME: - case SQL_C_TIMESTAMP: { - switch (ParameterType) { - case SQL_VARCHAR: buffer_type = TSDB_DATA_TYPE_NCHAR; break; - case SQL_TIMESTAMP: buffer_type = TSDB_DATA_TYPE_TIMESTAMP; break; // extention to taos - default: { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, - "parameter[@%d] no conversion from [%d] to [%d]", - ParameterNumber, ValueType, ParameterType); - return SQL_ERROR; - } break; - } - } break; - case SQL_C_CHAR: { - switch (ParameterType) { - case SQL_BIT: buffer_type = TSDB_DATA_TYPE_BOOL; break; - case SQL_TINYINT: buffer_type = TSDB_DATA_TYPE_TINYINT; break; - case SQL_SMALLINT: buffer_type = TSDB_DATA_TYPE_SMALLINT; break; - case SQL_INTEGER: buffer_type = TSDB_DATA_TYPE_INT; break; - case SQL_BIGINT: buffer_type = TSDB_DATA_TYPE_BIGINT; break; - case SQL_FLOAT: buffer_type = TSDB_DATA_TYPE_FLOAT; break; - case SQL_DOUBLE: buffer_type = TSDB_DATA_TYPE_DOUBLE; break; - case SQL_VARCHAR: buffer_type = TSDB_DATA_TYPE_NCHAR; break; - case SQL_VARBINARY: buffer_type = TSDB_DATA_TYPE_BINARY; break; - case SQL_TIMESTAMP: buffer_type = TSDB_DATA_TYPE_TIMESTAMP; break; // extention to taos - default: { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, - "parameter[@%d] no conversion from [%d] to [%d]", - ParameterNumber, ValueType, ParameterType); - return SQL_ERROR; - } break; - } - } break; - case SQL_C_BINARY: { - switch (ParameterType) { - case SQL_BIT: buffer_type = TSDB_DATA_TYPE_BOOL; break; - case SQL_TINYINT: buffer_type = TSDB_DATA_TYPE_TINYINT; break; - case SQL_SMALLINT: buffer_type = TSDB_DATA_TYPE_SMALLINT; break; - case SQL_INTEGER: buffer_type = TSDB_DATA_TYPE_INT; break; - case SQL_BIGINT: buffer_type = TSDB_DATA_TYPE_BIGINT; break; - case SQL_FLOAT: buffer_type = TSDB_DATA_TYPE_FLOAT; break; - case SQL_DOUBLE: buffer_type = TSDB_DATA_TYPE_DOUBLE; break; - case SQL_VARCHAR: buffer_type = TSDB_DATA_TYPE_NCHAR; break; - case SQL_VARBINARY: buffer_type = TSDB_DATA_TYPE_BINARY; break; - case SQL_TIMESTAMP: buffer_type = TSDB_DATA_TYPE_TIMESTAMP; break; // extention to taos - default: { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, - "parameter[@%d] no conversion from [%d] to [%d]", - ParameterNumber, ValueType, ParameterType); - return SQL_ERROR; - } break; - } - } break; - default: { - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, - "parameter[@%d] no conversion from [%d] to [%d]", - ParameterNumber, ValueType, ParameterType); - return SQL_ERROR; - } break; - } - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - TAOS_BIND *binds = (TAOS_BIND*)(sql->n_params>=ParameterNumber ? sql->binds : realloc(sql->binds, ParameterNumber * sizeof(*binds))); - if (!ar || !binds) { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_OOM, "failed to allocate resources for Parameter[%d]", ParameterNumber); - if (ar) sql->params = ar; - if (binds) sql->binds = binds; + if (!ar) { + SET_ERROR(sql, "HY001", TSDB_CODE_ODBC_OOM, ""); return SQL_ERROR; } sql->params = ar; - sql->binds = binds; if (sql->n_paramsn_params = ParameterNumber; } param_bind_t *pb = ar + ParameterNumber - 1; - TAOS_BIND *b = binds + ParameterNumber - 1; - - b->buffer_type = buffer_type; - b->buffer_length = LengthPrecision; - b->buffer = NULL; - b->length = NULL; - b->is_null = NULL; - b->is_unsigned = 0; - b->error = NULL; pb->ParameterNumber = ParameterNumber; pb->ValueType = ValueType; @@ -1571,12 +1800,12 @@ static SQLRETURN doSQLDriverConnect( if (!conn) return SQL_ERROR; if (fDriverCompletion!=SQL_DRIVER_NOPROMPT) { - SET_ERROR(conn, "HY000", TSDB_CODE_TSC_APP_ERROR, "option[%d] other than SQL_DRIVER_NOPROMPT not supported yet", fDriverCompletion); + SET_ERROR(conn, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, "option[%d] other than SQL_DRIVER_NOPROMPT not supported yet", fDriverCompletion); return SQL_ERROR; } if (conn->taos) { - SET_ERROR(conn, "HY000", TSDB_CODE_TSC_APP_ERROR, "connection still in use"); + SET_ERROR(conn, "08002", TSDB_CODE_ODBC_CONNECTION_BUSY, "connection still in use"); return SQL_ERROR; } @@ -1591,18 +1820,16 @@ static SQLRETURN doSQLDriverConnect( do { if (szConnStrIn && !connStr) { - SET_ERROR(conn, "HY000", TSDB_CODE_ODBC_OOM, "failed to allocate resources"); + SET_ERROR(conn, "HY001", TSDB_CODE_ODBC_OOM, ""); break; } int n = sscanf((const char*)connStr, "DSN=%m[^;]; UID=%m[^;]; PWD=%m[^;] %n", &serverName, &userName, &auth, &bytes); if (n<1) { - SET_ERROR(conn, "HY000", TSDB_CODE_RPC_NETWORK_UNAVAIL, "unrecognized connection string: [%s]", (const char*)szConnStrIn); + SET_ERROR(conn, "HY000", TSDB_CODE_ODBC_BAD_CONNSTR, "unrecognized connection string: [%s]", (const char*)szConnStrIn); break; } - D("DSN:[%s];UID:[%s];PWD:[%s]", serverName, userName, auth); - // TODO: data-race // TODO: shall receive ip/port from odbc.ini conn->taos = taos_connect("localhost", userName, auth, NULL, 0); @@ -1652,11 +1879,11 @@ static SQLRETURN doSQLSetConnectAttr(SQLHDBC ConnectionHandle, if (!conn) return SQL_ERROR; if (Attribute != SQL_ATTR_AUTOCOMMIT) { - SET_ERROR(conn, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "Attribute other than SQL_ATTR_AUTOCOMMIT not supported yet"); + SET_ERROR(conn, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, "Attribute other than SQL_ATTR_AUTOCOMMIT not supported yet"); return SQL_ERROR; } if (Value != (SQLPOINTER)SQL_AUTOCOMMIT_ON) { - SET_ERROR(conn, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "Attribute Value other than SQL_AUTOCOMMIT_ON not supported yet[%p]", Value); + SET_ERROR(conn, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, "Attribute Value other than SQL_AUTOCOMMIT_ON not supported yet[%p]", Value); return SQL_ERROR; } @@ -1681,29 +1908,19 @@ static SQLRETURN doSQLDescribeCol(SQLHSTMT StatementHandle, sql_t *sql = (sql_t*)StatementHandle; if (!sql) return SQL_ERROR; - if (!sql->conn) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection yet"); - return SQL_ERROR; - } - - if (!sql->conn->taos) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_INVALID_CONNECTION, "no connection to data source yet"); - return SQL_ERROR; - } + CHK_CONN(sql); + CHK_CONN_TAOS(sql); if (!sql->rs) { - SET_ERROR(sql, "HY000", TSDB_CODE_TSC_QUERY_CACHE_ERASED, "no result set cached or not ready"); + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NO_RESULT, ""); return SQL_ERROR; } - TAOS_FIELD *fields = taos_fetch_fields(sql->rs); - if (!fields) { - SET_ERROR(sql, "HY000", TSDB_CODE_MND_FIELD_NOT_EXIST, "fields not ready or unavailable"); - return SQL_ERROR; - } int nfields = taos_field_count(sql->rs); - if (ColumnNumber<1 || ColumnNumber>nfields) { - SET_ERROR(sql, "HY000", TSDB_CODE_MND_FIELD_NOT_EXIST, "ColumnNumber not in valid range"); + TAOS_FIELD *fields = taos_fetch_fields(sql->rs); + + if (ColumnNumber<=0 || ColumnNumber>nfields) { + SET_ERROR(sql, "07009", TSDB_CODE_ODBC_OUT_OF_RANGE, "invalid column number [%d]", ColumnNumber); return SQL_ERROR; } @@ -1759,7 +1976,8 @@ static SQLRETURN doSQLDescribeCol(SQLHSTMT StatementHandle, } break; default: - SET_ERROR(sql, "HY000", TSDB_CODE_COM_OPS_NOT_SUPPORT, "unknown TSDB_DATA_TYPE [%x]", field->type); + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NOT_SUPPORT, + "unknown [%s[%d/0x%x]]", taos_data_type(field->type), field->type, field->type); return SQL_ERROR; break; } @@ -1795,9 +2013,52 @@ SQLRETURN SQL_API SQLDescribeCol(SQLHSTMT StatementHandle, return r; } +static SQLRETURN doSQLNumParams(SQLHSTMT hstmt, SQLSMALLINT *pcpar) +{ + sql_t *sql = (sql_t*)hstmt; + if (!sql) return SQL_ERROR; + + CHK_CONN(sql); + CHK_CONN_TAOS(sql); + + if (!sql->stmt) { + SET_ERROR(sql, "HY010", TSDB_CODE_ODBC_STATEMENT_NOT_READY, ""); + return SQL_ERROR; + } + + int params = 0; + int r = taos_stmt_num_params(sql->stmt, ¶ms); + if (r) { + SET_ERROR(sql, "HY000", terrno, "fetch num of statement params failed"); + return SQL_ERROR; + } + + if (pcpar) *pcpar = params; + + return SQL_SUCCESS; +} + +SQLRETURN SQL_API SQLNumParams(SQLHSTMT hstmt, SQLSMALLINT *pcpar) +{ + SQLRETURN r; + r = doSQLNumParams(hstmt, pcpar); + return r; +} + + + + + + + static void init_routine(void) { + if (0) { + string_conv(NULL, NULL, NULL, 0, NULL, 0, NULL, NULL); + utf8_to_ucs4le(NULL, NULL); + ucs4le_to_utf8(NULL, 0, NULL); + } taos_init(); } @@ -1845,65 +2106,6 @@ static int do_field_display_size(TAOS_FIELD *field) { return 10; } -static void do_convert(SQLPOINTER TargetValue, SQLLEN BufferLength, SQLLEN *StrLen_or_Ind, TAOS_FIELD *field, void *row) { - switch (field->type) { - case TSDB_DATA_TYPE_TINYINT: - snprintf((char*)TargetValue, BufferLength, "%d", *((int8_t *)row)); - break; - - case TSDB_DATA_TYPE_SMALLINT: - snprintf((char*)TargetValue, BufferLength, "%d", *((int16_t *)row)); - break; - - case TSDB_DATA_TYPE_INT: - snprintf((char*)TargetValue, BufferLength, "%d", *((int32_t *)row)); - break; - - case TSDB_DATA_TYPE_BIGINT: - snprintf((char*)TargetValue, BufferLength, "%" PRId64, *((int64_t *)row)); - break; - - case TSDB_DATA_TYPE_FLOAT: { - float fv = 0; - fv = GET_FLOAT_VAL(row); - snprintf((char*)TargetValue, BufferLength, "%f", fv); - } break; - - case TSDB_DATA_TYPE_DOUBLE: { - double dv = 0; - dv = GET_DOUBLE_VAL(row); - snprintf((char*)TargetValue, BufferLength, "%lf", dv); - } break; - - case TSDB_DATA_TYPE_BINARY: - case TSDB_DATA_TYPE_NCHAR: { - size_t xlen = 0; - char *p = (char*)TargetValue; - size_t n = BufferLength; - for (xlen = 0; xlen < field->bytes - VARSTR_HEADER_SIZE; xlen++) { - char c = ((char *)row)[xlen]; - if (c == 0) break; - int v = snprintf(p, n, "%c", c); - p += v; - n -= v; - if (n<=0) break; - } - if (n>0) *p = '\0'; - ((char*)TargetValue)[BufferLength-1] = '\0'; - *StrLen_or_Ind = BufferLength - n; - } break; - - case TSDB_DATA_TYPE_TIMESTAMP: - snprintf((char*)TargetValue, BufferLength, "%" PRId64, *((int64_t *)row)); - break; - - case TSDB_DATA_TYPE_BOOL: - snprintf((char*)TargetValue, BufferLength, "%d", *((int8_t *)row)); - default: - break; - } -} - // convertion from TSDB_DATA_TYPE_XXX to SQL_C_XXX static SQLRETURN conv_tsdb_bool_to_c_bit(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) { @@ -2353,6 +2555,7 @@ static SQLRETURN conv_tsdb_ts_to_c_ts(sql_t *sql, c_target_t *target, TAOS_FIELD static SQLRETURN conv_tsdb_bin_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const unsigned char *bin) { size_t n = field->bytes; + n = strlen((const char*)bin); *target->soi = n; if (target->ptr) memcpy(target->ptr, bin, (n>target->len ? target->len : n)); if (n <= target->len) return SQL_SUCCESS; @@ -2364,6 +2567,7 @@ static SQLRETURN conv_tsdb_bin_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIE static SQLRETURN conv_tsdb_bin_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const unsigned char *bin) { size_t n = field->bytes; + n = strlen((const char*)bin); *target->soi = n; if (target->ptr) memcpy(target->ptr, bin, (n>target->len ? target->len : n)); if (n <= target->len) return SQL_SUCCESS; diff --git a/src/connector/odbc/src/todbc_util.c b/src/connector/odbc/src/todbc_util.c new file mode 100644 index 0000000000..9bbe8e6987 --- /dev/null +++ b/src/connector/odbc/src/todbc_util.c @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "todbc_util.h" + +#include "iconv.h" + +#include +#include +#include +#include +#include + +const char* sql_sql_type(int type) { + switch (type) { + case SQL_BIT: return "SQL_BIT"; + case SQL_TINYINT: return "SQL_TINYINT"; + case SQL_SMALLINT: return "SQL_SMALLINT"; + case SQL_INTEGER: return "SQL_INTEGER"; + case SQL_BIGINT: return "SQL_BIGINT"; + case SQL_FLOAT: return "SQL_FLOAT"; + case SQL_DOUBLE: return "SQL_DOUBLE"; + case SQL_DECIMAL: return "SQL_DECIMAL"; + case SQL_NUMERIC: return "SQL_NUMERIC"; + case SQL_REAL: return "SQL_REAL"; + case SQL_CHAR: return "SQL_CHAR"; + case SQL_VARCHAR: return "SQL_VARCHAR"; + case SQL_LONGVARCHAR: return "SQL_LONGVARCHAR"; + case SQL_WCHAR: return "SQL_WCHAR"; + case SQL_WVARCHAR: return "SQL_WVARCHAR"; + case SQL_WLONGVARCHAR: return "SQL_WLONGVARCHAR"; + case SQL_BINARY: return "SQL_BINARY"; + case SQL_VARBINARY: return "SQL_VARBINARY"; + case SQL_LONGVARBINARY: return "SQL_LONGVARBINARY"; + case SQL_DATE: return "SQL_DATE"; + case SQL_TIME: return "SQL_TIME"; + case SQL_TIMESTAMP: return "SQL_TIMESTAMP"; + case SQL_TYPE_DATE: return "SQL_TYPE_DATE"; + case SQL_TYPE_TIME: return "SQL_TYPE_TIME"; + case SQL_TYPE_TIMESTAMP: return "SQL_TYPE_TIMESTAMP"; + case SQL_INTERVAL_MONTH: return "SQL_INTERVAL_MONTH"; + case SQL_INTERVAL_YEAR: return "SQL_INTERVAL_YEAR"; + case SQL_INTERVAL_YEAR_TO_MONTH: return "SQL_INTERVAL_YEAR_TO_MONTH"; + case SQL_INTERVAL_DAY: return "SQL_INTERVAL_DAY"; + case SQL_INTERVAL_HOUR: return "SQL_INTERVAL_HOUR"; + case SQL_INTERVAL_MINUTE: return "SQL_INTERVAL_MINUTE"; + case SQL_INTERVAL_SECOND: return "SQL_INTERVAL_SECOND"; + case SQL_INTERVAL_DAY_TO_HOUR: return "SQL_INTERVAL_DAY_TO_HOUR"; + case SQL_INTERVAL_DAY_TO_MINUTE: return "SQL_INTERVAL_DAY_TO_MINUTE"; + case SQL_INTERVAL_DAY_TO_SECOND: return "SQL_INTERVAL_DAY_TO_SECOND"; + case SQL_INTERVAL_HOUR_TO_MINUTE: return "SQL_INTERVAL_HOUR_TO_MINUTE"; + case SQL_INTERVAL_HOUR_TO_SECOND: return "SQL_INTERVAL_HOUR_TO_SECOND"; + case SQL_INTERVAL_MINUTE_TO_SECOND: return "SQL_INTERVAL_MINUTE_TO_SECOND"; + case SQL_GUID: return "SQL_GUID"; + default: return "UNKNOWN"; + } +} + +const char* sql_c_type(int type) { + switch (type) { + case SQL_C_CHAR: return "SQL_C_CHAR"; + case SQL_C_WCHAR: return "SQL_C_WCHAR"; + case SQL_C_SHORT: return "SQL_C_SHORT"; + case SQL_C_SSHORT: return "SQL_C_SSHORT"; + case SQL_C_USHORT: return "SQL_C_USHORT"; + case SQL_C_LONG: return "SQL_C_LONG"; + case SQL_C_SLONG: return "SQL_C_SLONG"; + case SQL_C_ULONG: return "SQL_C_ULONG"; + case SQL_C_FLOAT: return "SQL_C_FLOAT"; + case SQL_C_DOUBLE: return "SQL_C_DOUBLE"; + case SQL_C_BIT: return "SQL_C_BIT"; + case SQL_C_TINYINT: return "SQL_C_TINYINT"; + case SQL_C_STINYINT: return "SQL_C_STINYINT"; + case SQL_C_UTINYINT: return "SQL_C_UTINYINT"; + case SQL_C_SBIGINT: return "SQL_C_SBIGINT"; + case SQL_C_UBIGINT: return "SQL_C_UBIGINT"; + case SQL_C_BINARY: return "SQL_C_BINARY"; + case SQL_C_DATE: return "SQL_C_DATE"; + case SQL_C_TIME: return "SQL_C_TIME"; + case SQL_C_TIMESTAMP: return "SQL_C_TIMESTAMP"; + case SQL_C_TYPE_DATE: return "SQL_C_TYPE_DATE"; + case SQL_C_TYPE_TIME: return "SQL_C_TYPE_TIME"; + case SQL_C_TYPE_TIMESTAMP: return "SQL_C_TYPE_TIMESTAMP"; + case SQL_C_NUMERIC: return "SQL_C_NUMERIC"; + case SQL_C_GUID: return "SQL_C_GUID"; + default: return "UNKNOWN"; + } +} + +int string_conv(const char *fromcode, const char *tocode, + const unsigned char *src, size_t sbytes, + unsigned char *dst, size_t dbytes, + size_t *consumed, size_t *generated) +{ + if (consumed) *consumed = 0; + if (generated) *generated = 0; + + if (dbytes <= 0) return -1; + dst[0] = '\0'; + + iconv_t conv = iconv_open(tocode, fromcode); + if (!conv) return -1; + + int r = 0; + do { + char *s = (char*)src; + char *d = (char*)dst; + size_t sl = sbytes; + size_t dl = dbytes; + + r = iconv(conv, &s, &sl, &d, &dl); + *d = '\0'; + + if (consumed) *consumed = sbytes - sl; + if (generated) *generated = dbytes - dl; + + } while (0); + + iconv_close(conv); + return r; +} + +int utf8_chars(const char *src) +{ + const char *fromcode = "UTF-8"; + const char *tocode = "UCS-2LE"; + iconv_t conv = iconv_open(tocode, fromcode); + if (!conv) return -1; + + size_t slen = strlen(src); + char buf[4096]; + size_t dlen = sizeof(buf); + char *ps = (char*)src; + char *pd = buf; + iconv(conv, &ps, &slen, &pd, &dlen); + DASSERT(slen==0); + + size_t chars = (sizeof(buf) - dlen) / 2; + iconv_close(conv); + return chars; +} + +unsigned char* utf8_to_ucs4le(const char *utf8, size_t *chars) +{ + const char *tocode = "UCS-4LE"; + const char *fromcode = "UTF-8"; + + iconv_t conv = iconv_open(tocode, fromcode); + if (!conv) return NULL; + + unsigned char *ucs4le = NULL; + + do { + size_t slen = strlen(utf8); + size_t dlen = slen * 4; + + ucs4le = (unsigned char*)malloc(dlen+1); + if (!ucs4le) break; + + char *src = (char*)utf8; + char *dst = (char*)ucs4le; + size_t s = slen; + size_t d = dlen; + iconv(conv, &src, &s, &dst, &d); + dst[0] = '\0'; + + if (chars) *chars = (dlen - d) / 4; + } while (0); + + iconv_close(conv); + return ucs4le; +} + +char* ucs4le_to_utf8(const unsigned char *ucs4le, size_t slen, size_t *chars) +{ + const char *fromcode = "UCS-4LE"; + const char *tocode = "UTF-8"; + + iconv_t conv = iconv_open(tocode, fromcode); + if (!conv) return NULL; + + char *utf8 = NULL; + + do { + size_t dlen = slen; + + utf8 = (char*)malloc(dlen+1); + if (!utf8) break; + + char *dst = utf8; + char *src = (char*)ucs4le; + size_t s = slen; + size_t d = dlen; + iconv(conv, &src, &s, &dst, &d); + dst[0] = '\0'; + + if (chars) *chars = (slen - s) / 4; + } while (0); + + iconv_close(conv); + return utf8; +} + +SQLCHAR* wchars_to_chars(const SQLWCHAR *wchars, size_t chs, size_t *bytes) +{ + size_t dlen = chs * 4; + SQLCHAR *dst = (SQLCHAR*)malloc(dlen + 1); + if (!dst) return NULL; + + string_conv("UCS-2LE", "UTF-8", (const unsigned char*)wchars, chs * sizeof(*wchars), dst, dlen + 1, NULL, bytes); + + return dst; +} + diff --git a/src/connector/odbc/src/todbc_util.h b/src/connector/odbc/src/todbc_util.h new file mode 100644 index 0000000000..0400da59fe --- /dev/null +++ b/src/connector/odbc/src/todbc_util.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef _TODBC_UTIL_H_ +#define _TODBC_UTIL_H_ + +#include +#include +#include +#include +#include + +#define D(fmt, ...) \ + fprintf(stderr, \ + "%s[%d]:%s() " fmt "\n", \ + basename((char*)__FILE__), __LINE__, __func__, \ + ##__VA_ARGS__) + +#define DASSERT(statement) \ +do { \ + if (statement) break; \ + D("Assertion failure: %s", #statement); \ + abort(); \ +} while (0) + +const char* sql_sql_type(int type); +const char* sql_c_type(int type); + + +int string_conv(const char *fromcode, const char *tocode, + const unsigned char *src, size_t sbytes, + unsigned char *dst, size_t dbytes, + size_t *consumed, size_t *generated); +int utf8_chars(const char *src); + +unsigned char* utf8_to_ucs4le(const char *utf8, size_t *chars); +char* ucs4le_to_utf8(const unsigned char *ucs4le, size_t slen, size_t *chars); +SQLCHAR* wchars_to_chars(const SQLWCHAR *wchars, size_t chs, size_t *bytes); + +#endif // _TODBC_UTIL_H_ diff --git a/src/connector/odbc/tests/main.c b/src/connector/odbc/tests/main.c index 1d403002a8..1ac9b71369 100644 --- a/src/connector/odbc/tests/main.c +++ b/src/connector/odbc/tests/main.c @@ -193,7 +193,7 @@ int main(int argc, char *argv[]) { data.f8 = 9999999.999999; memset(data.bin, 0, sizeof(data.bin)); memset(data.blob, 0, sizeof(data.blob)); - snprintf(data.bin, sizeof(data.bin), "hello"); + snprintf(data.bin, sizeof(data.bin), "hel我lo"); snprintf(data.blob, sizeof(data.blob), "world"); snprintf(data.blob, sizeof(data.blob), "wo人rld"); SQLHSTMT stmt = {0}; diff --git a/src/connector/odbc/tests/odbc.py b/src/connector/odbc/tests/odbc.py index b12292b238..c2f2f33d2b 100644 --- a/src/connector/odbc/tests/odbc.py +++ b/src/connector/odbc/tests/odbc.py @@ -9,4 +9,48 @@ row = cursor.fetchone() while row: print(row) row = cursor.fetchone() +cursor.close() + +#cursor = cnxn.cursor() +#cursor.execute(""" +#INSERT INTO db.t values (?,?,?,?,?,?,?,?,?,?) +#""", +#"2020-12-12 00:00:00", +#1, +#27, +#32767, +#147483647, +#223372036854775807, +#23.456, +#899.999999, +#"foo", +#"bar") + +cursor = cnxn.cursor() +cursor.execute("drop database if exists db"); +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("create database db"); +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("create table db.t (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))"); +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("insert into db.t values('2020-10-13 06:44:00', 1, 127, 32767, 32768, 32769, 123.456, 789.987, 'hello', 'world')") +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("insert into db.t values(?,?,?,?,?,?,?,?,?,?)", "2020-10-13 07:06:00", 0, 127, 32767, 32768, 32769, 123.456, 789.987, "hel后lo", "wo哈rld"); +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("SELECT * from db.t") +row = cursor.fetchone() +while row: + print(row) + row = cursor.fetchone() +cursor.close() diff --git a/src/inc/taos.h b/src/inc/taos.h index 7e8f174b7c..e4fba58228 100644 --- a/src/inc/taos.h +++ b/src/inc/taos.h @@ -69,6 +69,8 @@ DLL_EXPORT int taos_options(TSDB_OPTION option, const void *arg, ...); DLL_EXPORT TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port); DLL_EXPORT void taos_close(TAOS *taos); +const char *taos_data_type(int type); + typedef struct TAOS_BIND { int buffer_type; void * buffer; @@ -77,10 +79,25 @@ typedef struct TAOS_BIND { int * is_null; int is_unsigned; // unused int * error; // unused + union { + int64_t ts; + int8_t b; + int8_t v1; + int16_t v2; + int32_t v4; + int64_t v8; + float f4; + double f8; + unsigned char *bin; + char *nchar; + } u; + unsigned int allocated; } TAOS_BIND; TAOS_STMT *taos_stmt_init(TAOS *taos); int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length); +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_bind_param(TAOS_STMT *stmt, TAOS_BIND *bind); int taos_stmt_add_batch(TAOS_STMT *stmt); int taos_stmt_execute(TAOS_STMT *stmt); diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index 17aa1fbfd5..1d8c50280e 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -355,6 +355,16 @@ TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_OOM, 0, 0x2101, "out of mem TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_CONV_UNDEF, 0, 0x2102, "convertion undefined") TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_CONV_TRUNC, 0, 0x2103, "convertion truncated") TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_CONV_NOT_SUPPORT, 0, 0x2104, "convertion not supported") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_OUT_OF_RANGE, 0, 0x2105, "out of range") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_NOT_SUPPORT, 0, 0x2106, "not supported yet") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_INVALID_HANDLE, 0, 0x2107, "invalid handle") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_NO_RESULT, 0, 0x2108, "no result set") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_NO_FIELDS, 0, 0x2109, "no fields returned") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_INVALID_CURSOR, 0, 0x2110, "invalid cursor") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_STATEMENT_NOT_READY, 0, 0x2111, "statement not ready") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_CONNECTION_BUSY, 0, 0x2112, "connection still busy") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_BAD_CONNSTR, 0, 0x2113, "bad connection string") + #ifdef TAOS_ERROR_C }; From 7c04c9b3803a55f7e406bda93e388058b2b39ec7 Mon Sep 17 00:00:00 2001 From: freemine Date: Tue, 13 Oct 2020 22:31:59 +0800 Subject: [PATCH 017/101] 1. bugFix: nchar/binary field bytes 2. report error for parameterised-select --- src/client/src/tscPrepare.c | 49 ++++-- src/connector/odbc/src/todbc.c | 232 ++++++++++++++++------------ src/connector/odbc/src/todbc_util.h | 9 ++ src/connector/odbc/tests/odbc.py | 33 +++- src/inc/taos.h | 1 + src/inc/taoserror.h | 1 + 6 files changed, 203 insertions(+), 122 deletions(-) diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index 2f42f8ac56..5630919ddb 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -638,6 +638,19 @@ TAOS_RES *taos_stmt_use_result(TAOS_STMT* stmt) { return result; } +int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) { + STscStmt* pStmt = (STscStmt*)stmt; + + if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { + terrno = TSDB_CODE_TSC_DISCONNECTED; + return TSDB_CODE_TSC_DISCONNECTED; + } + + if (insert) *insert = pStmt->isInsert; + + return TSDB_CODE_SUCCESS; +} + int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) { STscStmt* pStmt = (STscStmt*)stmt; @@ -646,12 +659,14 @@ int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) { return TSDB_CODE_TSC_DISCONNECTED; } - SSqlObj* pSql = pStmt->pSql; - SSqlCmd *pCmd = &pSql->cmd; - - *nums = pCmd->numOfParams; - - return TSDB_CODE_SUCCESS; + if (pStmt->isInsert) { + SSqlObj* pSql = pStmt->pSql; + SSqlCmd *pCmd = &pSql->cmd; + *nums = pCmd->numOfParams; + return TSDB_CODE_SUCCESS; + } else { + return TSDB_CODE_TSC_APP_ERROR; + } } int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) { @@ -662,18 +677,22 @@ int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) { return TSDB_CODE_TSC_DISCONNECTED; } - SSqlObj* pSql = pStmt->pSql; - SSqlCmd *pCmd = &pSql->cmd; - STableDataBlocks* pBlock = taosArrayGetP(pCmd->pDataBlocks, 0); + if (pStmt->isInsert) { + SSqlObj* pSql = pStmt->pSql; + SSqlCmd *pCmd = &pSql->cmd; + STableDataBlocks* pBlock = taosArrayGetP(pCmd->pDataBlocks, 0); - assert(pCmd->numOfParams == pBlock->numOfParams); - if (idx < 0 || idx >= pBlock->numOfParams) return -1; + assert(pCmd->numOfParams == pBlock->numOfParams); + if (idx < 0 || idx >= pBlock->numOfParams) return -1; - SParamInfo* param = pBlock->params + idx; - if (type) *type = param->type; - if (bytes) *bytes = param->bytes; + SParamInfo* param = pBlock->params + idx; + if (type) *type = param->type; + if (bytes) *bytes = param->bytes; - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; + } else { + return TSDB_CODE_TSC_APP_ERROR; + } } const char *taos_data_type(int type) { diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 9576a981fb..3d30b56662 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -759,6 +759,11 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, return SQL_ERROR; } + if (TargetValue == NULL) { + SET_ERROR(sql, "HY009", TSDB_CODE_ODBC_BAD_ARG, "NULL TargetValue not allowed for col [%d]", ColumnNumber); + return SQL_ERROR; + } + TAOS_FIELD *field = fields + ColumnNumber-1; void *row = sql->row[ColumnNumber-1]; @@ -1563,33 +1568,34 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T static SQLRETURN do_execute(sql_t *sql, TAOS_BIND *binds) { - SQLRETURN r = SQL_SUCCESS; + int tr = TSDB_CODE_SUCCESS; for (int i=0; in_params; ++i) { - r = do_bind_param_value(sql, i, sql->params+i, binds+i); + SQLRETURN r = do_bind_param_value(sql, i, sql->params+i, binds+i); if (r==SQL_SUCCESS) continue; return r; } if (sql->n_params > 0) { - PROFILE(r = taos_stmt_bind_param(sql->stmt, binds)); - if (r) { - SET_ERROR(sql, "HY000", r, "failed to bind parameters[%d in total]", sql->n_params); + PROFILE(tr = taos_stmt_bind_param(sql->stmt, binds)); + if (tr) { + SET_ERROR(sql, "HY000", tr, "failed to bind parameters[%d in total]", sql->n_params); return SQL_ERROR; } - PROFILE(r = taos_stmt_add_batch(sql->stmt)); - if (r) { - SET_ERROR(sql, "HY000", r, "failed to add batch"); - return SQL_ERROR; - } + // PROFILE(r = taos_stmt_add_batch(sql->stmt)); + // if (r) { + // SET_ERROR(sql, "HY000", r, "failed to add batch"); + // return SQL_ERROR; + // } } - PROFILE(r = taos_stmt_execute(sql->stmt)); - if (r) { - SET_ERROR(sql, "HY000", r, "failed to execute statement"); + PROFILE(tr = taos_stmt_execute(sql->stmt)); + if (tr) { + SET_ERROR(sql, "HY000", tr, "failed to execute statement"); return SQL_ERROR; } + SQLRETURN r = SQL_SUCCESS; PROFILE(sql->rs = taos_stmt_use_result(sql->stmt)); CHK_RS(r, sql, "failed to use result"); @@ -1933,6 +1939,10 @@ static SQLRETURN doSQLDescribeCol(SQLHSTMT StatementHandle, if (NameLength) { *NameLength = strnlen(field->name, sizeof(field->name)); } + if (ColumnSize) { + *ColumnSize = field->bytes; + } + if (DataType) { switch (field->type) { case TSDB_DATA_TYPE_BOOL: { @@ -1969,10 +1979,12 @@ static SQLRETURN doSQLDescribeCol(SQLHSTMT StatementHandle, case TSDB_DATA_TYPE_NCHAR: { *DataType = SQL_C_CHAR; // unicode ? + if (ColumnSize) *ColumnSize -= VARSTR_HEADER_SIZE; } break; case TSDB_DATA_TYPE_BINARY: { *DataType = SQL_C_BINARY; + if (ColumnSize) *ColumnSize -= VARSTR_HEADER_SIZE; } break; default: @@ -1982,9 +1994,6 @@ static SQLRETURN doSQLDescribeCol(SQLHSTMT StatementHandle, break; } } - if (ColumnSize) { - *ColumnSize = field->bytes; - } if (DecimalDigits) { if (field->type == TSDB_DATA_TYPE_TIMESTAMP) { *DecimalDigits = 3; @@ -2026,8 +2035,19 @@ static SQLRETURN doSQLNumParams(SQLHSTMT hstmt, SQLSMALLINT *pcpar) return SQL_ERROR; } + int insert = 0; + int r = taos_stmt_is_insert(sql->stmt, &insert); + if (r) { + SET_ERROR(sql, "HY000", terrno, ""); + return SQL_ERROR; + } + if (!insert) { + SET_ERROR(sql, "HY000", terrno, "taos does not provide count of parameters for statement other than insert"); + return SQL_ERROR; + } + int params = 0; - int r = taos_stmt_num_params(sql->stmt, ¶ms); + r = taos_stmt_num_params(sql->stmt, ¶ms); if (r) { SET_ERROR(sql, "HY000", terrno, "fetch num of statement params failed"); return SQL_ERROR; @@ -2110,49 +2130,49 @@ static int do_field_display_size(TAOS_FIELD *field) { static SQLRETURN conv_tsdb_bool_to_c_bit(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) { int8_t v = b; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_bool_to_c_tinyint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) { int8_t v = b; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_bool_to_c_short(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) { int16_t v = b; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_bool_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) { int32_t v = b; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_bool_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) { int64_t v = b; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_bool_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) { float v = b; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_bool_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t b) { double v = b; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } @@ -2160,11 +2180,9 @@ static SQLRETURN conv_tsdb_bool_to_c_char(sql_t *sql, c_target_t *target, TAOS_F { DASSERT(target->len>0); *target->soi = 1; - if (target->ptr) { - target->ptr[0] = '0' + b; - if (target->len>1) { - target->ptr[1] = '\0'; - } + target->ptr[0] = '0' + b; + if (target->len>1) { + target->ptr[1] = '\0'; } return SQL_SUCCESS; @@ -2181,42 +2199,42 @@ static SQLRETURN conv_tsdb_bool_to_c_binary(sql_t *sql, c_target_t *target, TAOS static SQLRETURN conv_tsdb_v1_to_c_tinyint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) { int8_t v = v1; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v1_to_c_short(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) { int16_t v = v1; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v1_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) { int32_t v = v1; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v1_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) { int64_t v = v1; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v1_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) { float v = v1; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v1_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int8_t v1) { double v = v1; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } @@ -2226,7 +2244,7 @@ static SQLRETURN conv_tsdb_v1_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIE int n = snprintf(buf, sizeof(buf), "%d", v1); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_TINYINT -> SQL_C_BIT"); return SQL_SUCCESS_WITH_INFO; @@ -2238,7 +2256,7 @@ static SQLRETURN conv_tsdb_v1_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F int n = snprintf(buf, sizeof(buf), "%d", v1); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + strncpy(target->ptr, buf, (n>target->len ? target->len : n)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_TINYINT -> SQL_C_BIT"); return SQL_SUCCESS_WITH_INFO; @@ -2247,35 +2265,35 @@ static SQLRETURN conv_tsdb_v1_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F static SQLRETURN conv_tsdb_v2_to_c_short(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) { int16_t v = v2; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v2_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) { int32_t v = v2; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v2_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) { int64_t v = v2; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v2_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) { float v = v2; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v2_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int16_t v2) { double v = v2; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } @@ -2285,7 +2303,7 @@ static SQLRETURN conv_tsdb_v2_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIE int n = snprintf(buf, sizeof(buf), "%d", v2); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_SMALLINT -> SQL_C_CHAR"); return SQL_SUCCESS_WITH_INFO; @@ -2297,7 +2315,7 @@ static SQLRETURN conv_tsdb_v2_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F int n = snprintf(buf, sizeof(buf), "%d", v2); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + strncpy(target->ptr, buf, (n>target->len ? target->len : n)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_SMALLINT -> SQL_C_CHAR"); return SQL_SUCCESS_WITH_INFO; @@ -2307,28 +2325,28 @@ static SQLRETURN conv_tsdb_v2_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F static SQLRETURN conv_tsdb_v4_to_c_long(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4) { int32_t v = v4; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v4_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4) { int64_t v = v4; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v4_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4) { float v = v4; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v4_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int32_t v4) { double v = v4; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } @@ -2338,7 +2356,7 @@ static SQLRETURN conv_tsdb_v4_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIE int n = snprintf(buf, sizeof(buf), "%d", v4); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_INTEGER -> SQL_C_CHAR"); return SQL_SUCCESS_WITH_INFO; @@ -2350,7 +2368,7 @@ static SQLRETURN conv_tsdb_v4_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F int n = snprintf(buf, sizeof(buf), "%d", v4); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + strncpy(target->ptr, buf, (n>target->len ? target->len : n)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_INTEGER -> SQL_C_BINARY"); return SQL_SUCCESS_WITH_INFO; @@ -2360,21 +2378,21 @@ static SQLRETURN conv_tsdb_v4_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F static SQLRETURN conv_tsdb_v8_to_c_sbigint(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8) { int64_t v = v8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v8_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8) { float v = v8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_v8_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8) { double v = v8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } @@ -2384,7 +2402,7 @@ static SQLRETURN conv_tsdb_v8_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIE int n = snprintf(buf, sizeof(buf), "%ld", v8); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_BIGINT -> SQL_C_CHAR"); return SQL_SUCCESS_WITH_INFO; @@ -2396,7 +2414,7 @@ static SQLRETURN conv_tsdb_v8_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F int n = snprintf(buf, sizeof(buf), "%ld", v8); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + strncpy(target->ptr, buf, (n>target->len ? target->len : n)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_BIGINT -> SQL_C_BINARY"); return SQL_SUCCESS_WITH_INFO; @@ -2406,14 +2424,14 @@ static SQLRETURN conv_tsdb_v8_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F static SQLRETURN conv_tsdb_f4_to_c_float(sql_t *sql, c_target_t *target, TAOS_FIELD *field, float f4) { float v = f4; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_f4_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, float f4) { double v = f4; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } @@ -2423,7 +2441,7 @@ static SQLRETURN conv_tsdb_f4_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIE int n = snprintf(buf, sizeof(buf), "%g", f4); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_FLOAT -> SQL_C_CHAR"); return SQL_SUCCESS_WITH_INFO; @@ -2435,7 +2453,7 @@ static SQLRETURN conv_tsdb_f4_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F int n = snprintf(buf, sizeof(buf), "%g", f4); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + strncpy(target->ptr, buf, (n>target->len ? target->len : n)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_FLOAT -> SQL_C_BINARY"); return SQL_SUCCESS_WITH_INFO; @@ -2445,7 +2463,7 @@ static SQLRETURN conv_tsdb_f4_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F static SQLRETURN conv_tsdb_f8_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, double f8) { double v = f8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } @@ -2455,7 +2473,7 @@ static SQLRETURN conv_tsdb_f8_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIE int n = snprintf(buf, sizeof(buf), "%.6f", f8); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); + strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_DOUBLE -> SQL_C_CHAR"); return SQL_SUCCESS_WITH_INFO; @@ -2467,7 +2485,7 @@ static SQLRETURN conv_tsdb_f8_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F int n = snprintf(buf, sizeof(buf), "%g", f8); DASSERT(nsoi = n; - if (target->ptr) strncpy(target->ptr, buf, (n>target->len ? target->len : n)); + strncpy(target->ptr, buf, (n>target->len ? target->len : n)); if (n<=target->len) return SQL_SUCCESS; SET_ERROR(sql, "22003", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_DOUBLE -> SQL_C_BINARY"); return SQL_SUCCESS_WITH_INFO; @@ -2488,7 +2506,7 @@ static SQLRETURN conv_tsdb_ts_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIELD int64_t v = (int64_t)t; v *= 1000; v += ts->fraction % 1000; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } @@ -2508,9 +2526,7 @@ static SQLRETURN conv_tsdb_ts_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIEL *target->soi = n; - if (target->ptr) { - snprintf(target->ptr, target->len, "%s.%03d", buf, ts->fraction); - } + snprintf(target->ptr, target->len, "%s.%03d", buf, ts->fraction); if (n <= target->len) { return SQL_SUCCESS; @@ -2536,7 +2552,7 @@ static SQLRETURN conv_tsdb_ts_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIEL *target->soi = n; - if (target->ptr) memcpy(target->ptr, buf, (n>target->len ? target->len : n)); + memcpy(target->ptr, buf, (n>target->len ? target->len : n)); if (n <= target->len) { return SQL_SUCCESS; @@ -2548,31 +2564,55 @@ static SQLRETURN conv_tsdb_ts_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIEL static SQLRETURN conv_tsdb_ts_to_c_ts(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts) { - if (target->ptr) memcpy(target->ptr, ts, sizeof(*ts)); + memcpy(target->ptr, ts, sizeof(*ts)); return SQL_SUCCESS; } static SQLRETURN conv_tsdb_bin_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const unsigned char *bin) { - size_t n = field->bytes; - n = strlen((const char*)bin); - *target->soi = n; - if (target->ptr) memcpy(target->ptr, bin, (n>target->len ? target->len : n)); - if (n <= target->len) return SQL_SUCCESS; + if (target->len<1) { + SET_ERROR(sql, "HY090", TSDB_CODE_ODBC_BAD_ARG, ""); + return SQL_ERROR; + } + size_t field_bytes = field->bytes - VARSTR_HEADER_SIZE; + size_t n = strnlen((const char*)bin, field_bytes); - SET_ERROR(sql, "01004", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_BINARY -> SQL_C_CHAR"); + if (n < target->len) { + memcpy(target->ptr, bin, n); + target->ptr[n] = '\0'; + *target->soi = n; + return SQL_SUCCESS; + } + n = target->len - 1; + *target->soi = n; + if (n > 0) { + memcpy(target->ptr, bin, n-1); + target->ptr[n-1] = '\0'; + } + SET_ERROR(sql, "01004", TSDB_CODE_ODBC_CONV_TRUNC, ""); return SQL_SUCCESS_WITH_INFO; } static SQLRETURN conv_tsdb_bin_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const unsigned char *bin) { - size_t n = field->bytes; - n = strlen((const char*)bin); - *target->soi = n; - if (target->ptr) memcpy(target->ptr, bin, (n>target->len ? target->len : n)); - if (n <= target->len) return SQL_SUCCESS; + if (target->len<1) { + SET_ERROR(sql, "HY090", TSDB_CODE_ODBC_BAD_ARG, ""); + return SQL_ERROR; + } + size_t field_bytes = field->bytes - VARSTR_HEADER_SIZE; + size_t n = strnlen((const char*)bin, field_bytes); - SET_ERROR(sql, "01004", TSDB_CODE_ODBC_CONV_UNDEF, "TSDB_DATA_TYPE_BINARY -> SQL_C_CHAR"); + if (n <= target->len) { + memcpy(target->ptr, bin, n); + if (nlen) target->ptr[n] = '\0'; + *target->soi = n; + return SQL_SUCCESS; + } + + n = target->len; + memcpy(target->ptr, bin, n); + *target->soi = n; + SET_ERROR(sql, "01004", TSDB_CODE_ODBC_CONV_TRUNC, ""); return SQL_SUCCESS_WITH_INFO; } @@ -2583,7 +2623,7 @@ static SQLRETURN conv_tsdb_str_to_c_bit(sql_t *sql, c_target_t *target, TAOS_FIE int n = sscanf(str, "%lf%n", &f8, &bytes); int8_t v = f8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); *target->soi = 1; @@ -2622,7 +2662,7 @@ static SQLRETURN conv_tsdb_str_to_c_v1(sql_t *sql, c_target_t *target, TAOS_FIEL int n = sscanf(str, "%lf%n", &f8, &bytes); int8_t v = f8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); *target->soi = 1; @@ -2652,7 +2692,7 @@ static SQLRETURN conv_tsdb_str_to_c_v2(sql_t *sql, c_target_t *target, TAOS_FIEL int n = sscanf(str, "%lf%n", &f8, &bytes); int16_t v = f8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); *target->soi = 2; @@ -2682,7 +2722,7 @@ static SQLRETURN conv_tsdb_str_to_c_v4(sql_t *sql, c_target_t *target, TAOS_FIEL int n = sscanf(str, "%lf%n", &f8, &bytes); int32_t v = f8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); *target->soi = 4; @@ -2712,7 +2752,7 @@ static SQLRETURN conv_tsdb_str_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIEL int n = sscanf(str, "%lf%n", &f8, &bytes); int64_t v = f8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); *target->soi = 8; @@ -2742,7 +2782,7 @@ static SQLRETURN conv_tsdb_str_to_c_f4(sql_t *sql, c_target_t *target, TAOS_FIEL int n = sscanf(str, "%lf%n", &f8, &bytes); float v = f8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); *target->soi = 4; @@ -2761,7 +2801,7 @@ static SQLRETURN conv_tsdb_str_to_c_f8(sql_t *sql, c_target_t *target, TAOS_FIEL int n = sscanf(str, "%lf%n", &f8, &bytes); float v = f8; - if (target->ptr) memcpy(target->ptr, &v, sizeof(v)); + memcpy(target->ptr, &v, sizeof(v)); *target->soi = 8; @@ -2775,25 +2815,11 @@ static SQLRETURN conv_tsdb_str_to_c_f8(sql_t *sql, c_target_t *target, TAOS_FIEL static SQLRETURN conv_tsdb_str_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) { - size_t n = strlen(str); - *target->soi = n; - if (target->ptr) strncpy(target->ptr, str, (n < target->len ? n+1 : target->len)); - - if (n < target->len) return SQL_SUCCESS; - - SET_ERROR(sql, "01004", TSDB_CODE_ODBC_CONV_TRUNC, "TSDB_DATA_TYPE_NCHAR -> SQL_C_CHAR"); - return SQL_SUCCESS_WITH_INFO; + return conv_tsdb_bin_to_c_str(sql, target, field, (const unsigned char*)str); } static SQLRETURN conv_tsdb_str_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str) { - size_t n = strlen(str); - *target->soi = n; - if (target->ptr) memcpy(target->ptr, str, (n < target->len ? n : target->len)); - - if (n <= target->len) return SQL_SUCCESS; - - SET_ERROR(sql, "01004", TSDB_CODE_ODBC_CONV_TRUNC, "TSDB_DATA_TYPE_NCHAR -> SQL_C_BINARY"); - return SQL_SUCCESS_WITH_INFO; + return conv_tsdb_bin_to_c_bin(sql, target, field, (const unsigned char*)str); } diff --git a/src/connector/odbc/src/todbc_util.h b/src/connector/odbc/src/todbc_util.h index 0400da59fe..a1521ef1cf 100644 --- a/src/connector/odbc/src/todbc_util.h +++ b/src/connector/odbc/src/todbc_util.h @@ -35,6 +35,15 @@ do { \ abort(); \ } while (0) +#define DASSERTX(statement, fmt, ...) \ +do { \ + if (statement) break; \ + D("Assertion failure: %s, " fmt "", #statement, ##__VA_ARGS__); \ + abort(); \ +} while (0) + + + const char* sql_sql_type(int type); const char* sql_c_type(int type); diff --git a/src/connector/odbc/tests/odbc.py b/src/connector/odbc/tests/odbc.py index c2f2f33d2b..b1169679e3 100644 --- a/src/connector/odbc/tests/odbc.py +++ b/src/connector/odbc/tests/odbc.py @@ -3,10 +3,11 @@ cnxn = pyodbc.connect('DSN=TAOS_DSN;UID=root;PWD=taosdata', autocommit=True) cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8') #cnxn.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8') #cnxn.setencoding(encoding='utf-8') + cursor = cnxn.cursor() cursor.execute("SELECT * from db.t") -row = cursor.fetchone() -while row: +row = cursor.fetchone() +while row: print(row) row = cursor.fetchone() cursor.close() @@ -48,8 +49,32 @@ cursor.close() cursor = cnxn.cursor() cursor.execute("SELECT * from db.t") -row = cursor.fetchone() -while row: +row = cursor.fetchone() +while row: + print(row) + row = cursor.fetchone() +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("drop database if exists db"); +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("create database db"); +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("create table db.t (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(4), blob nchar(4))"); +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("insert into db.t values('2020-10-13 06:44:00', 1, 127, 32767, 32768, 32769, 123.456, 789.987, 'hell', 'worl')") +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("SELECT * from db.t") +row = cursor.fetchone() +while row: print(row) row = cursor.fetchone() cursor.close() diff --git a/src/inc/taos.h b/src/inc/taos.h index e4fba58228..2d72b1739d 100644 --- a/src/inc/taos.h +++ b/src/inc/taos.h @@ -96,6 +96,7 @@ typedef struct TAOS_BIND { TAOS_STMT *taos_stmt_init(TAOS *taos); int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length); +int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert); 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_bind_param(TAOS_STMT *stmt, TAOS_BIND *bind); diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index 0e05a0829f..3bf8444d67 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -366,6 +366,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_INVALID_CURSOR, 0, 0x2110, "invalid cu TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_STATEMENT_NOT_READY, 0, 0x2111, "statement not ready") TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_CONNECTION_BUSY, 0, 0x2112, "connection still busy") TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_BAD_CONNSTR, 0, 0x2113, "bad connection string") +TAOS_DEFINE_ERROR(TSDB_CODE_ODBC_BAD_ARG, 0, 0x2114, "bad argument") #ifdef TAOS_ERROR_C From 1c52e6598e723024fe5e782797aa44ae6434929d Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 17 Oct 2020 06:11:21 +0800 Subject: [PATCH 018/101] 1. add batch-insert 2. allow buffer-type conversion within taos --- src/client/src/tscPrepare.c | 417 +++++++++++++- src/connector/odbc/src/todbc.c | 825 +++++++++++++++++++++++++--- src/connector/odbc/src/todbc_util.c | 12 + src/connector/odbc/src/todbc_util.h | 2 + src/connector/odbc/tests/odbc.py | 36 +- 5 files changed, 1189 insertions(+), 103 deletions(-) diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index 5630919ddb..d6dd19db77 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -139,7 +139,7 @@ static int normalStmtBindParam(STscStmt* stmt, TAOS_BIND* bind) { return TSDB_CODE_TSC_INVALID_VALUE; } } - + return TSDB_CODE_SUCCESS; } @@ -213,7 +213,7 @@ static char* normalStmtBuildSql(STscStmt* stmt) { case TSDB_DATA_TYPE_NULL: taosStringBuilderAppendNull(&sb); break; - + case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_SMALLINT: @@ -266,6 +266,387 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { return TSDB_CODE_SUCCESS; } + if (1) { + // allow user bind param data with different type + short size = 0; + union { + int8_t v1; + int16_t v2; + int32_t v4; + int64_t v8; + float f4; + double f8; + unsigned char buf[32*1024]; + } u; + switch (param->type) { + case TSDB_DATA_TYPE_BOOL: { + switch (bind->buffer_type) { + case TSDB_DATA_TYPE_BOOL: { + u.v1 = *(int8_t*)bind->buffer; + if (u.v1==0 || u.v1==1) break; + } break; + case TSDB_DATA_TYPE_TINYINT: { + u.v1 = *(int8_t*)bind->buffer; + if (u.v1==0 || u.v1==1) break; + } break; + case TSDB_DATA_TYPE_SMALLINT: { + u.v1 = *(int16_t*)bind->buffer; + if (u.v1==0 || u.v1==1) break; + } break; + case TSDB_DATA_TYPE_INT: { + u.v1 = *(int32_t*)bind->buffer; + if (u.v1==0 || u.v1==1) break; + } break; + case TSDB_DATA_TYPE_BIGINT: { + u.v1 = *(int64_t*)bind->buffer; + if (u.v1==0 || u.v1==1) break; + } break; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + // "0", "1" convertible + if (strncmp((const char*)bind->buffer, "0", *bind->length)==0) { + u.v1 = 0; + break; + } + if (strncmp((const char*)bind->buffer, "1", *bind->length)==0) { + u.v1 = 1; + break; + } + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_TIMESTAMP: + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } + memcpy(data + param->offset, &u.v1, sizeof(u.v1)); + return TSDB_CODE_SUCCESS; + } break; + case TSDB_DATA_TYPE_TINYINT: { + switch (bind->buffer_type) { + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: { + int8_t v = *(int8_t*)bind->buffer; + u.v1 = v; + if (v >= SCHAR_MIN && v <= SCHAR_MAX) break; + } break; + case TSDB_DATA_TYPE_SMALLINT: { + int16_t v = *(int16_t*)bind->buffer; + u.v1 = v; + if (v >= SCHAR_MIN && v <= SCHAR_MAX) break; + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_INT: { + int32_t v = *(int32_t*)bind->buffer; + u.v1 = v; + if (v >= SCHAR_MIN && v <= SCHAR_MAX) break; + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_BIGINT: { + int64_t v = *(int64_t*)bind->buffer; + u.v1 = v; + if (v >= SCHAR_MIN && v <= SCHAR_MAX) break; + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + int64_t v; + int n,r; + r = sscanf((const char*)bind->buffer, "%ld%n", &v, &n); + if (r==1 && n==strlen((const char*)bind->buffer)) { + u.v1 = v; + if (v >= SCHAR_MIN && v <= SCHAR_MAX) break; + } + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_TIMESTAMP: + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } + memcpy(data + param->offset, &u.v1, sizeof(u.v1)); + return TSDB_CODE_SUCCESS; + } break; + case TSDB_DATA_TYPE_SMALLINT: { + switch (bind->buffer_type) { + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_SMALLINT: { + int v = *(int16_t*)bind->buffer; + u.v2 = v; + } break; + case TSDB_DATA_TYPE_INT: { + int32_t v = *(int32_t*)bind->buffer; + u.v2 = v; + if (v >= SHRT_MIN && v <= SHRT_MAX) break; + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_BIGINT: { + int64_t v = *(int64_t*)bind->buffer; + u.v2 = v; + if (v >= SHRT_MIN && v <= SHRT_MAX) break; + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + int64_t v; + int n,r; + r = sscanf((const char*)bind->buffer, "%ld%n", &v, &n); + if (r==1 && n==strlen((const char*)bind->buffer)) { + u.v2 = v; + if (v >= SHRT_MIN && v <= SHRT_MAX) break; + } + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_TIMESTAMP: + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } + memcpy(data + param->offset, &u.v2, sizeof(u.v2)); + return TSDB_CODE_SUCCESS; + } break; + case TSDB_DATA_TYPE_INT: { + switch (bind->buffer_type) { + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_SMALLINT: + case TSDB_DATA_TYPE_INT: { + u.v4 = *(int32_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_BIGINT: { + int64_t v = *(int64_t*)bind->buffer; + u.v4 = v; + if (v >= INT_MIN && v <= INT_MAX) break; + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + int64_t v; + int n,r; + r = sscanf((const char*)bind->buffer, "%ld%n", &v, &n); + if (r==1 && n==strlen((const char*)bind->buffer)) { + u.v4 = v; + if (v >= INT_MIN && v <= INT_MAX) break; + } + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_TIMESTAMP: + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } + memcpy(data + param->offset, &u.v2, sizeof(u.v2)); + return TSDB_CODE_SUCCESS; + } break; + case TSDB_DATA_TYPE_FLOAT: { + switch (bind->buffer_type) { + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: { + u.f4 = *(int8_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_SMALLINT: { + u.f4 = *(int16_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_INT: { + u.f4 = *(int32_t*)bind->buffer; + // shall we check equality? + } break; + case TSDB_DATA_TYPE_BIGINT: { + u.f4 = *(int64_t*)bind->buffer; + // shall we check equality? + } break; + case TSDB_DATA_TYPE_FLOAT: { + u.f4 = *(float*)bind->buffer; + } break; + case TSDB_DATA_TYPE_DOUBLE: { + u.f4 = *(float*)bind->buffer; + // shall we check equality? + } break; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + float v; + int n,r; + r = sscanf((const char*)bind->buffer, "%f%n", &v, &n); + if (r==1 && n==strlen((const char*)bind->buffer)) { + u.f4 = v; + break; + } + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_TIMESTAMP: + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } + memcpy(data + param->offset, &u.f4, sizeof(u.f4)); + return TSDB_CODE_SUCCESS; + } break; + case TSDB_DATA_TYPE_BIGINT: { + switch (bind->buffer_type) { + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: { + u.v8 = *(int8_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_SMALLINT: { + u.v8 = *(int16_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_INT: { + u.v8 = *(int32_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_BIGINT: { + u.v8 = *(int64_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + int64_t v; + int n,r; + r = sscanf((const char*)bind->buffer, "%ld%n", &v, &n); + if (r==1 && n==strlen((const char*)bind->buffer)) { + u.v8 = v; + break; + } + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_TIMESTAMP: + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } + memcpy(data + param->offset, &u.v8, sizeof(u.v8)); + return TSDB_CODE_SUCCESS; + } break; + case TSDB_DATA_TYPE_DOUBLE: { + switch (bind->buffer_type) { + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: { + u.f8 = *(int8_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_SMALLINT: { + u.f8 = *(int16_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_INT: { + u.f8 = *(int32_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_BIGINT: { + u.f8 = *(int64_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_FLOAT: { + u.f8 = *(float*)bind->buffer; + } break; + case TSDB_DATA_TYPE_DOUBLE: { + u.f8 = *(double*)bind->buffer; + } break; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + double v; + int n,r; + r = sscanf((const char*)bind->buffer, "%lf%n", &v, &n); + if (r==1 && n==strlen((const char*)bind->buffer)) { + u.v8 = v; + break; + } + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_TIMESTAMP: + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } break; + memcpy(data + param->offset, &u.f8, sizeof(u.f8)); + return TSDB_CODE_SUCCESS; + } break; + case TSDB_DATA_TYPE_TIMESTAMP: { + switch (bind->buffer_type) { + case TSDB_DATA_TYPE_TIMESTAMP: { + u.v8 = *(int64_t*)bind->buffer; + } break; + case TSDB_DATA_TYPE_BINARY: + case TSDB_DATA_TYPE_NCHAR: { + // is this the correct way to call taosParseTime? + if (taosParseTime(bind->buffer, &u.v8, *bind->length, 3, tsDaylight) == TSDB_CODE_SUCCESS) { + break; + } + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_SMALLINT: + case TSDB_DATA_TYPE_INT: + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_BIGINT: + case TSDB_DATA_TYPE_DOUBLE: + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } break; + memcpy(data + param->offset, &u.v8, sizeof(u.v8)); + return TSDB_CODE_SUCCESS; + } break; + case TSDB_DATA_TYPE_BINARY: { + switch (bind->buffer_type) { + case TSDB_DATA_TYPE_BINARY: { + if ((*bind->length) > (uintptr_t)param->bytes) { + return TSDB_CODE_TSC_INVALID_VALUE; + } + size = (short)*bind->length; + STR_WITH_SIZE_TO_VARSTR(data + param->offset, bind->buffer, size); + return TSDB_CODE_SUCCESS; + } break; + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_SMALLINT: + case TSDB_DATA_TYPE_INT: + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_BIGINT: + case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_TIMESTAMP: + case TSDB_DATA_TYPE_NCHAR: + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } + } break; + case TSDB_DATA_TYPE_NCHAR: { + switch (bind->buffer_type) { + case TSDB_DATA_TYPE_NCHAR: { + size_t output = 0; + if (!taosMbsToUcs4(bind->buffer, *bind->length, varDataVal(data + param->offset), param->bytes - VARSTR_HEADER_SIZE, &output)) { + return TSDB_CODE_TSC_INVALID_VALUE; + } + varDataSetLen(data + param->offset, output); + return TSDB_CODE_SUCCESS; + } break; + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_SMALLINT: + case TSDB_DATA_TYPE_INT: + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_BIGINT: + case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_TIMESTAMP: + case TSDB_DATA_TYPE_BINARY: + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } + } break; + default: { + return TSDB_CODE_TSC_INVALID_VALUE; + } break; + } + } + if (bind->buffer_type != param->type) { return TSDB_CODE_TSC_INVALID_VALUE; } @@ -299,12 +680,12 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { size = (short)*bind->length; STR_WITH_SIZE_TO_VARSTR(data + param->offset, bind->buffer, size); return TSDB_CODE_SUCCESS; - + case TSDB_DATA_TYPE_NCHAR: { size_t output = 0; if (!taosMbsToUcs4(bind->buffer, *bind->length, varDataVal(data + param->offset), param->bytes - VARSTR_HEADER_SIZE, &output)) { return TSDB_CODE_TSC_INVALID_VALUE; - } + } varDataSetLen(data + param->offset, output); return TSDB_CODE_SUCCESS; } @@ -358,7 +739,7 @@ static int insertStmtBindParam(STscStmt* stmt, TAOS_BIND* bind) { } // actual work of all data blocks is done, update block size and numOfRows. - // note we don't do this block by block during the binding process, because + // note we don't do this block by block during the binding process, because // we cannot recover if something goes wrong. pCmd->batchSize = binded * 2 + 1; @@ -405,7 +786,7 @@ static int insertStmtReset(STscStmt* pStmt) { } } pCmd->batchSize = 0; - + STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0); pTableMetaInfo->vgroupIndex = 0; return TSDB_CODE_SUCCESS; @@ -447,7 +828,7 @@ static int insertStmtExecute(STscStmt* stmt) { pRes->numOfRows = 0; pRes->numOfTotal = 0; pRes->numOfClauseTotal = 0; - + pRes->qhandle = 0; pSql->cmd.insertType = 0; @@ -508,35 +889,35 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { SSqlObj* pSql = pStmt->pSql; size_t sqlLen = strlen(sql); - + SSqlCmd *pCmd = &pSql->cmd; SSqlRes *pRes = &pSql->res; pSql->param = (void*) pSql; pSql->fp = waitForQueryRsp; pSql->cmd.insertType = TSDB_QUERY_TYPE_STMT_INSERT; - + if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, TSDB_DEFAULT_PAYLOAD_SIZE)) { tscError("%p failed to malloc payload buffer", pSql); return TSDB_CODE_TSC_OUT_OF_MEMORY; } - + pSql->sqlstr = realloc(pSql->sqlstr, sqlLen + 1); - + if (pSql->sqlstr == NULL) { tscError("%p failed to malloc sql string buffer", pSql); free(pCmd->payload); return TSDB_CODE_TSC_OUT_OF_MEMORY; } - + pRes->qhandle = 0; pRes->numOfRows = 1; - + strtolower(pSql->sqlstr, sql); tscDebugL("%p SQL: %s", pSql, pSql->sqlstr); - if (tscIsInsertData(pSql->sqlstr)) { + if (tscIsInsertData(pSql->sqlstr)) { pStmt->isInsert = true; - + pSql->cmd.numOfParams = 0; pSql->cmd.batchSize = 0; @@ -548,7 +929,7 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { tsem_wait(&pSql->rspSem); return pSql->res.code; } - + return code; } @@ -665,7 +1046,9 @@ int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) { *nums = pCmd->numOfParams; return TSDB_CODE_SUCCESS; } else { - return TSDB_CODE_TSC_APP_ERROR; + SNormalStmt* normal = &pStmt->normal; + *nums = normal->numParams; + return TSDB_CODE_SUCCESS; } } diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 3d30b56662..45f005a7ed 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -156,7 +156,7 @@ struct param_bind_s { SQLULEN LengthPrecision; SQLSMALLINT ParameterScale; SQLPOINTER ParameterValue; - SQLLEN *StrLen_or_Ind; + SQLLEN *StrLen_or_Ind; unsigned int valid; }; @@ -191,10 +191,17 @@ struct sql_s { TAOS_STMT *stmt; param_bind_t *params; int n_params; + size_t rowlen; + size_t n_rows; + size_t ptr_offset; + TAOS_RES *rs; TAOS_ROW row; taos_error_t err; + unsigned int is_prepared:1; + unsigned int is_insert:1; + unsigned int is_executed:1; }; typedef struct c_target_s c_target_t; @@ -209,6 +216,37 @@ struct c_target_s { static pthread_once_t init_once = PTHREAD_ONCE_INIT; static void init_routine(void); +// conversions + +const char* tsdb_int64_to_bit(int64_t src, int8_t *dst); +const char* tsdb_int64_to_tinyint(int64_t src, int8_t *dst); +const char* tsdb_int64_to_smallint(int64_t src, int16_t *dst); +const char* tsdb_int64_to_int(int64_t src, int32_t *dst); +const char* tsdb_int64_to_bigint(int64_t src, int64_t *dst); +const char* tsdb_int64_to_ts(int64_t src, int64_t *dst); +const char* tsdb_int64_to_float(int64_t src, float *dst); +const char* tsdb_int64_to_double(int64_t src, double *dst); +const char* tsdb_int64_to_char(int64_t src, char *dst, size_t dlen); + +const char* tsdb_double_to_bit(double src, int precision, int8_t *dst); +const char* tsdb_double_to_tinyint(double src, int precision, int8_t *dst); +const char* tsdb_double_to_smallint(double src, int precision, int16_t *dst); +const char* tsdb_double_to_int(double src, int precision, int32_t *dst); +const char* tsdb_double_to_bigint(double src, int precision, int64_t *dst); +const char* tsdb_double_to_ts(double src, int precision, int64_t *dst); +const char* tsdb_double_to_float(double src, int precision, float *dst); +const char* tsdb_double_to_double(double src, int precision, double *dst); +const char* tsdb_double_to_char(double src, int precision, char *dst, size_t dlen); + +const char* tsdb_chars_to_bit(const char *src, int8_t *dst); +const char* tsdb_chars_to_tinyint(const char *src, int8_t *dst); +const char* tsdb_chars_to_smallint(const char *src, int16_t *dst); +const char* tsdb_chars_to_int(const char *src, int32_t *dst); +const char* tsdb_chars_to_bigint(const char *src, int64_t *dst); +const char* tsdb_chars_to_ts(const char *src, int64_t *dst); +const char* tsdb_chars_to_float(const char *src, float *dst); +const char* tsdb_chars_to_double(const char *src, double *dst); +const char* tsdb_chars_to_char(const char *src, char *dst, size_t dlen); static int do_field_display_size(TAOS_FIELD *field); @@ -439,6 +477,40 @@ SQLRETURN SQL_API SQLAllocStmt(SQLHDBC ConnectionHandle, return r; } +static SQLRETURN doSQLAllocHandle(SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE *OutputHandle) +{ + switch (HandleType) { + case SQL_HANDLE_ENV: { + SQLHENV env = {0}; + SQLRETURN r = doSQLAllocEnv(&env); + if (r==SQL_SUCCESS && OutputHandle) *OutputHandle = env; + return r; + } break; + case SQL_HANDLE_DBC: { + SQLHDBC dbc = {0}; + SQLRETURN r = doSQLAllocConnect(InputHandle, &dbc); + if (r==SQL_SUCCESS && OutputHandle) *OutputHandle = dbc; + return r; + } break; + case SQL_HANDLE_STMT: { + SQLHSTMT stmt = {0}; + SQLRETURN r = doSQLAllocStmt(InputHandle, &stmt); + if (r==SQL_SUCCESS && OutputHandle) *OutputHandle = stmt; + return r; + } break; + default: { + return SQL_ERROR; + } break; + } +} + +SQLRETURN SQL_API SQLAllocHandle(SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE *OutputHandle) +{ + SQLRETURN r; + r = doSQLAllocHandle(HandleType, InputHandle, OutputHandle); + return r; +} + static SQLRETURN doSQLFreeStmt(SQLHSTMT StatementHandle, SQLUSMALLINT Option) { @@ -556,6 +628,13 @@ static SQLRETURN doSQLNumResultCols(SQLHSTMT StatementHandle, CHK_CONN(sql); CHK_CONN_TAOS(sql); + if (sql->is_insert) { + if (ColumnCount) { + *ColumnCount = 0; + } + return SQL_SUCCESS; + } + if (!sql->rs) { SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NO_RESULT, ""); return SQL_ERROR; @@ -586,6 +665,11 @@ static SQLRETURN doSQLRowCount(SQLHSTMT StatementHandle, CHK_CONN(sql); CHK_CONN_TAOS(sql); + if (sql->is_insert) { + if (RowCount) *RowCount = 0; + return SQL_SUCCESS; + } + if (!sql->rs) { SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NO_RESULT, ""); return SQL_ERROR; @@ -919,6 +1003,7 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, case SQL_C_SBIGINT: return conv_tsdb_ts_to_c_v8(sql, &target, field, &ts); case SQL_C_CHAR: return conv_tsdb_ts_to_c_str(sql, &target, field, &ts); case SQL_C_BINARY: return conv_tsdb_ts_to_c_bin(sql, &target, field, &ts); + case SQL_C_TYPE_TIMESTAMP: case SQL_C_TIMESTAMP: return conv_tsdb_ts_to_c_ts(sql, &target, field, &ts); default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_NOT_SUPPORT, @@ -1032,6 +1117,7 @@ static SQLRETURN doSQLPrepare(SQLHSTMT StatementHandle, sql->params = NULL; } sql->n_params = 0; + sql->is_insert = 0; do { sql->stmt = taos_stmt_init(sql->conn->taos); @@ -1040,12 +1126,51 @@ static SQLRETURN doSQLPrepare(SQLHSTMT StatementHandle, break; } - int r = taos_stmt_prepare(sql->stmt, (const char *)StatementText, TextLength); - if (r) { - SET_ERROR(sql, "HY000", r, "failed to prepare a TAOS statement"); + int ok = 0; + do { + int r = taos_stmt_prepare(sql->stmt, (const char *)StatementText, TextLength); + if (r) { + SET_ERROR(sql, "HY000", r, "failed to prepare a TAOS statement"); + break; + } + sql->is_prepared = 1; + + int is_insert = 0; + r = taos_stmt_is_insert(sql->stmt, &is_insert); + if (r) { + SET_ERROR(sql, "HY000", r, "failed to determine if a prepared-statement is of insert"); + break; + } + sql->is_insert = is_insert ? 1 : 0; + + int params = 0; + r = taos_stmt_num_params(sql->stmt, ¶ms); + if (r) { + SET_ERROR(sql, "HY000", terrno, "fetch num of statement params failed"); + break; + } + DASSERT(params>=0); + + if (params>0) { + param_bind_t *ar = (param_bind_t*)calloc(1, params * sizeof(*ar)); + if (!ar) { + SET_ERROR(sql, "HY001", TSDB_CODE_ODBC_OOM, ""); + break; + } + sql->params = ar; + } + + sql->n_params = params; + + ok = 1; + } while (0); + + if (!ok) { taos_stmt_close(sql->stmt); sql->stmt = NULL; - break; + sql->is_prepared = 0; + sql->is_insert = 0; + sql->is_executed = 0; } } while (0); @@ -1063,23 +1188,76 @@ SQLRETURN SQL_API SQLPrepare(SQLHSTMT StatementHandle, static const int yes = 1; static const int no = 0; -static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, TAOS_BIND *bind) +static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bind_t *param, TAOS_BIND *bind) { if (!param->valid) { SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NOT_SUPPORT, "parameter [@%d] not bound yet", idx+1); return SQL_ERROR; } - if (param->StrLen_or_Ind && *param->StrLen_or_Ind == SQL_NULL_DATA) { + + SQLPOINTER paramValue = param->ParameterValue; + SQLSMALLINT valueType = param->ValueType; + SQLLEN *soi = param->StrLen_or_Ind; + + size_t offset = idx_row * sql->rowlen + sql->ptr_offset; + + if (paramValue) paramValue += offset; + if (soi) soi = (SQLLEN*)((char*)soi + offset); + + + if (soi && *soi == SQL_NULL_DATA) { bind->is_null = (int*)&yes; return SQL_SUCCESS; } bind->is_null = (int*)&no; int type = 0; int bytes = 0; - int r = taos_stmt_get_param(sql->stmt, idx, &type, &bytes); - if (r) { - SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_OUT_OF_RANGE, "parameter [@%d] not valid", idx+1); - return SQL_ERROR; + if (sql->is_insert) { + int r = taos_stmt_get_param(sql->stmt, idx, &type, &bytes); + if (r) { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_OUT_OF_RANGE, "parameter [@%d] not valid", idx+1); + return SQL_ERROR; + } + } else { + switch (valueType) { + case SQL_C_LONG: { + type = TSDB_DATA_TYPE_INT; + } break; + case SQL_C_WCHAR: { + type = TSDB_DATA_TYPE_NCHAR; + bytes = SQL_NTS; + } break; + case SQL_C_CHAR: + case SQL_C_SHORT: + case SQL_C_SSHORT: + case SQL_C_USHORT: + case SQL_C_SLONG: + case SQL_C_ULONG: + case SQL_C_FLOAT: + case SQL_C_DOUBLE: + case SQL_C_BIT: + case SQL_C_TINYINT: + case SQL_C_STINYINT: + case SQL_C_UTINYINT: + case SQL_C_SBIGINT: + case SQL_C_UBIGINT: + case SQL_C_BINARY: + case SQL_C_DATE: + case SQL_C_TIME: + case SQL_C_TIMESTAMP: + case SQL_C_TYPE_DATE: + case SQL_C_TYPE_TIME: + case SQL_C_TYPE_TIMESTAMP: + case SQL_C_NUMERIC: + case SQL_C_GUID: + default: { + SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, + "no convertion from [%s[%d/0x%x]] for parameter [%d]", + sql_c_type(valueType), valueType, valueType, + idx+1); + return SQL_ERROR; + } break; + } } // ref: https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/converting-data-from-c-to-sql-data-types?view=sql-server-ver15 @@ -1089,12 +1267,12 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T bind->buffer_length = sizeof(bind->u.b); bind->buffer = &bind->u.b; bind->length = &bind->buffer_length; - switch (param->ValueType) { + switch (valueType) { case SQL_C_LONG: { - bind->u.b = *(int32_t*)param->ParameterValue; + bind->u.b = *(int32_t*)paramValue; } break; case SQL_C_BIT: { - bind->u.b = *(int8_t*)param->ParameterValue; + bind->u.b = *(int8_t*)paramValue; } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1122,7 +1300,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1133,18 +1311,18 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T bind->buffer_length = sizeof(bind->u.v1); bind->buffer = &bind->u.v1; bind->length = &bind->buffer_length; - switch (param->ValueType) { + switch (valueType) { case SQL_C_TINYINT: { - bind->u.v1 = *(int8_t*)param->ParameterValue; + bind->u.v1 = *(int8_t*)paramValue; } break; case SQL_C_SHORT: { - bind->u.v1 = *(int16_t*)param->ParameterValue; + bind->u.v1 = *(int16_t*)paramValue; } break; case SQL_C_LONG: { - bind->u.v1 = *(int32_t*)param->ParameterValue; + bind->u.v1 = *(int32_t*)paramValue; } break; case SQL_C_SBIGINT: { - bind->u.v1 = *(int64_t*)param->ParameterValue; + bind->u.v1 = *(int64_t*)paramValue; } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1170,7 +1348,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1181,12 +1359,12 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T bind->buffer_length = sizeof(bind->u.v2); bind->buffer = &bind->u.v2; bind->length = &bind->buffer_length; - switch (param->ValueType) { + switch (valueType) { case SQL_C_LONG: { - bind->u.v2 = *(int32_t*)param->ParameterValue; + bind->u.v2 = *(int32_t*)paramValue; } break; case SQL_C_SHORT: { - bind->u.v2 = *(int16_t*)param->ParameterValue; + bind->u.v2 = *(int16_t*)paramValue; } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1214,7 +1392,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1225,9 +1403,9 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T bind->buffer_length = sizeof(bind->u.v4); bind->buffer = &bind->u.v4; bind->length = &bind->buffer_length; - switch (param->ValueType) { + switch (valueType) { case SQL_C_LONG: { - bind->u.v4 = *(int32_t*)param->ParameterValue; + bind->u.v4 = *(int32_t*)paramValue; } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1256,7 +1434,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1267,12 +1445,12 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T bind->buffer_length = sizeof(bind->u.v8); bind->buffer = &bind->u.v8; bind->length = &bind->buffer_length; - switch (param->ValueType) { + switch (valueType) { case SQL_C_SBIGINT: { - bind->u.v8 = *(int64_t*)param->ParameterValue; + bind->u.v8 = *(int64_t*)paramValue; } break; case SQL_C_LONG: { - bind->u.v8 = *(int32_t*)param->ParameterValue; + bind->u.v8 = *(int32_t*)paramValue; } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1300,7 +1478,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1311,12 +1489,12 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T bind->buffer_length = sizeof(bind->u.f4); bind->buffer = &bind->u.f4; bind->length = &bind->buffer_length; - switch (param->ValueType) { + switch (valueType) { case SQL_C_DOUBLE: { - bind->u.f4 = *(double*)param->ParameterValue; + bind->u.f4 = *(double*)paramValue; } break; case SQL_C_FLOAT: { - bind->u.f4 = *(float*)param->ParameterValue; + bind->u.f4 = *(float*)paramValue; } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1344,7 +1522,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1355,9 +1533,9 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T bind->buffer_length = sizeof(bind->u.f8); bind->buffer = &bind->u.f8; bind->length = &bind->buffer_length; - switch (param->ValueType) { + switch (valueType) { case SQL_C_DOUBLE: { - bind->u.f8 = *(double*)param->ParameterValue; + bind->u.f8 = *(double*)paramValue; } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1386,7 +1564,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1395,23 +1573,23 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T case TSDB_DATA_TYPE_BINARY: { bind->buffer_type = type; bind->length = &bind->buffer_length; - switch (param->ValueType) { + switch (valueType) { case SQL_C_WCHAR: { - DASSERT(param->StrLen_or_Ind); - DASSERT(*param->StrLen_or_Ind != SQL_NTS); + DASSERT(soi); + DASSERT(*soi != SQL_NTS); size_t bytes = 0; - SQLCHAR *utf8 = wchars_to_chars(param->ParameterValue, *param->StrLen_or_Ind/2, &bytes); + SQLCHAR *utf8 = wchars_to_chars(paramValue, *soi/2, &bytes); bind->allocated = 1; bind->u.bin = utf8; bind->buffer_length = bytes; bind->buffer = bind->u.bin; } break; case SQL_C_BINARY: { - bind->u.bin = (unsigned char*)param->ParameterValue; - if (*param->StrLen_or_Ind == SQL_NTS) { - bind->buffer_length = strlen((const char*)param->ParameterValue); + bind->u.bin = (unsigned char*)paramValue; + if (*soi == SQL_NTS) { + bind->buffer_length = strlen((const char*)paramValue); } else { - bind->buffer_length = *param->StrLen_or_Ind; + bind->buffer_length = *soi; } bind->buffer = bind->u.bin; } break; @@ -1441,7 +1619,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1452,12 +1630,12 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T bind->buffer_length = sizeof(bind->u.v8); bind->buffer = &bind->u.v8; bind->length = &bind->buffer_length; - switch (param->ValueType) { + switch (valueType) { case SQL_C_WCHAR: { - DASSERT(param->StrLen_or_Ind); - DASSERT(*param->StrLen_or_Ind != SQL_NTS); + DASSERT(soi); + DASSERT(*soi != SQL_NTS); size_t bytes = 0; - SQLCHAR *utf8 = wchars_to_chars(param->ParameterValue, *param->StrLen_or_Ind/2, &bytes); + SQLCHAR *utf8 = wchars_to_chars(paramValue, *soi/2, &bytes); struct tm tm = {0}; strptime((const char*)utf8, "%Y-%m-%d %H:%M:%S", &tm); int64_t t = (int64_t)mktime(&tm); @@ -1466,7 +1644,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T free(utf8); } break; case SQL_C_SBIGINT: { - int64_t t = *(int64_t*)param->ParameterValue; + int64_t t = *(int64_t*)paramValue; bind->u.v8 = t; } break; case SQL_C_SHORT: @@ -1494,7 +1672,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1503,23 +1681,23 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T case TSDB_DATA_TYPE_NCHAR: { bind->buffer_type = type; bind->length = &bind->buffer_length; - switch (param->ValueType) { + switch (valueType) { case SQL_C_WCHAR: { - DASSERT(param->StrLen_or_Ind); - DASSERT(*param->StrLen_or_Ind != SQL_NTS); + DASSERT(soi); + DASSERT(*soi != SQL_NTS); size_t bytes = 0; - SQLCHAR *utf8 = wchars_to_chars(param->ParameterValue, *param->StrLen_or_Ind/2, &bytes); + SQLCHAR *utf8 = wchars_to_chars(paramValue, *soi/2, &bytes); bind->allocated = 1; bind->u.nchar = (char*)utf8; bind->buffer_length = bytes; bind->buffer = bind->u.nchar; } break; case SQL_C_CHAR: { - bind->u.nchar = (char*)param->ParameterValue; - if (*param->StrLen_or_Ind == SQL_NTS) { - bind->buffer_length = strlen((const char*)param->ParameterValue); + bind->u.nchar = (char*)paramValue; + if (*soi == SQL_NTS) { + bind->buffer_length = strlen((const char*)paramValue); } else { - bind->buffer_length = *param->StrLen_or_Ind; + bind->buffer_length = *soi; } bind->buffer = bind->u.nchar; } break; @@ -1549,7 +1727,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1558,7 +1736,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T default: { SET_ERROR(sql, "HYC00", TSDB_CODE_ODBC_OUT_OF_RANGE, "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", - sql_c_type(param->ValueType), param->ValueType, param->ValueType, + sql_c_type(valueType), valueType, valueType, taos_data_type(type), type, type, idx+1); return SQL_ERROR; } break; @@ -1566,27 +1744,61 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx, param_bind_t *param, T return SQL_SUCCESS; } -static SQLRETURN do_execute(sql_t *sql, TAOS_BIND *binds) +static SQLRETURN do_bind_batch(sql_t *sql, int idx_row, TAOS_BIND *binds) { - int tr = TSDB_CODE_SUCCESS; - for (int i=0; in_params; ++i) { - SQLRETURN r = do_bind_param_value(sql, i, sql->params+i, binds+i); + for (int j=0; jn_params; ++j) { + SQLRETURN r = do_bind_param_value(sql, idx_row, j, sql->params+j, binds+j); if (r==SQL_SUCCESS) continue; return r; } - if (sql->n_params > 0) { + int tr = 0; PROFILE(tr = taos_stmt_bind_param(sql->stmt, binds)); if (tr) { SET_ERROR(sql, "HY000", tr, "failed to bind parameters[%d in total]", sql->n_params); return SQL_ERROR; } - // PROFILE(r = taos_stmt_add_batch(sql->stmt)); - // if (r) { - // SET_ERROR(sql, "HY000", r, "failed to add batch"); - // return SQL_ERROR; - // } + if (sql->is_insert) { + int r = 0; + PROFILE(r = taos_stmt_add_batch(sql->stmt)); + if (r) { + SET_ERROR(sql, "HY000", r, "failed to add batch"); + return SQL_ERROR; + } + } + } + return SQL_SUCCESS; +} + +static SQLRETURN do_execute(sql_t *sql, TAOS_BIND *x_binds) +{ + int tr = TSDB_CODE_SUCCESS; + if (sql->n_rows==0) sql->n_rows = 1; + for (int i=0; in_rows; ++i) { + TAOS_BIND *binds = NULL; + if (sql->n_params>0) { + binds = (TAOS_BIND*)calloc(sql->n_params, sizeof(*binds)); + if (!binds) { + SET_ERROR(sql, "HY001", TSDB_CODE_ODBC_OOM, ""); + return SQL_ERROR; + } + } + + SQLRETURN r = do_bind_batch(sql, i, binds); + + if (binds) { + for (int i = 0; in_params; ++i) { + TAOS_BIND *bind = binds + i; + if (bind->allocated) { + free(bind->u.nchar); + bind->u.nchar = NULL; + } + } + free(binds); + } + + if (r) return r; } PROFILE(tr = taos_stmt_execute(sql->stmt)); @@ -1595,6 +1807,9 @@ static SQLRETURN do_execute(sql_t *sql, TAOS_BIND *binds) return SQL_ERROR; } + sql->is_executed = 1; + if (sql->is_insert) return SQL_SUCCESS; + SQLRETURN r = SQL_SUCCESS; PROFILE(sql->rs = taos_stmt_use_result(sql->stmt)); CHK_RS(r, sql, "failed to use result"); @@ -1745,22 +1960,37 @@ static SQLRETURN doSQLBindParameter( return SQL_ERROR; } + if (ParameterNumber<=0 || ParameterNumber>sql->n_params) { + SET_ERROR(sql, "07009", TSDB_CODE_ODBC_BAD_ARG, + "parameter [@%d] invalid", ParameterNumber); + return SQL_ERROR; + } + if (fParamType != SQL_PARAM_INPUT) { SET_ERROR(sql, "HY105", TSDB_CODE_ODBC_NOT_SUPPORT, "non-input parameter [@%d] not supported yet", ParameterNumber); return SQL_ERROR; } - param_bind_t *ar = (param_bind_t*)(sql->n_params>=ParameterNumber ? sql->params : realloc(sql->params, ParameterNumber * sizeof(*ar))); - if (!ar) { - SET_ERROR(sql, "HY001", TSDB_CODE_ODBC_OOM, ""); + if (ValueType == SQL_C_DEFAULT) { + SET_ERROR(sql, "HY003", TSDB_CODE_ODBC_NOT_SUPPORT, "default value for parameter [@%d] not supported yet", ParameterNumber); return SQL_ERROR; } - sql->params = ar; - if (sql->n_paramsn_params = ParameterNumber; + + if (!is_valid_sql_c_type(ValueType)) { + SET_ERROR(sql, "HY003", TSDB_CODE_ODBC_NOT_SUPPORT, + "SQL_C_TYPE [%s/%d/0x%x] for parameter [@%d] unknown", + sql_c_type(ValueType), ValueType, ValueType, ParameterNumber); + return SQL_ERROR; } - param_bind_t *pb = ar + ParameterNumber - 1; + if (!is_valid_sql_sql_type(ParameterType)) { + SET_ERROR(sql, "HY004", TSDB_CODE_ODBC_NOT_SUPPORT, + "SQL_TYPE [%s/%d/0x%x] for parameter [@%d] unknown", + sql_c_type(ParameterType), ParameterType, ParameterType, ParameterNumber); + return SQL_ERROR; + } + + param_bind_t *pb = sql->params + ParameterNumber - 1; pb->ParameterNumber = ParameterNumber; pb->ValueType = ValueType; @@ -2041,10 +2271,10 @@ static SQLRETURN doSQLNumParams(SQLHSTMT hstmt, SQLSMALLINT *pcpar) SET_ERROR(sql, "HY000", terrno, ""); return SQL_ERROR; } - if (!insert) { - SET_ERROR(sql, "HY000", terrno, "taos does not provide count of parameters for statement other than insert"); - return SQL_ERROR; - } + // if (!insert) { + // SET_ERROR(sql, "HY000", terrno, "taos does not provide count of parameters for statement other than insert"); + // return SQL_ERROR; + // } int params = 0; r = taos_stmt_num_params(sql->stmt, ¶ms); @@ -2065,6 +2295,67 @@ SQLRETURN SQL_API SQLNumParams(SQLHSTMT hstmt, SQLSMALLINT *pcpar) return r; } +static SQLRETURN doSQLSetStmtAttr(SQLHSTMT StatementHandle, + SQLINTEGER Attribute, SQLPOINTER Value, + SQLINTEGER StringLength) +{ + sql_t *sql = (sql_t*)StatementHandle; + if (!sql) return SQL_ERROR; + + CHK_CONN(sql); + CHK_CONN_TAOS(sql); + + if (!sql->stmt) { + SET_ERROR(sql, "HY010", TSDB_CODE_ODBC_STATEMENT_NOT_READY, ""); + return SQL_ERROR; + } + + if (sql->is_executed) { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NOT_SUPPORT, "change attr after executing statement not supported yet"); + return SQL_ERROR; + } + + switch (Attribute) { + case SQL_ATTR_PARAM_BIND_TYPE: { + SQLULEN val = (SQLULEN)Value; + if (val==SQL_BIND_BY_COLUMN) { + sql->rowlen = 0; + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NOT_SUPPORT, "SQL_ATTR_PARAM_BIND_TYPE/SQL_BIND_BY_COLUMN"); + return SQL_ERROR; + } + sql->rowlen = val; + return SQL_SUCCESS; + } break; + case SQL_ATTR_PARAMSET_SIZE: { + SQLULEN val = (SQLULEN)Value; + DASSERT(val>0); + sql->n_rows = val; + return SQL_SUCCESS; + } break; + case SQL_ATTR_PARAM_BIND_OFFSET_PTR: { + if (Value) { + SQLULEN val = *(SQLULEN*)Value; + sql->ptr_offset = val; + } else { + sql->ptr_offset = 0; + } + return SQL_SUCCESS; + } break; + default: { + SET_ERROR(sql, "HY000", TSDB_CODE_ODBC_NOT_SUPPORT, "Attribute:%d", Attribute); + } break; + } + return SQL_ERROR; +} + +SQLRETURN SQL_API SQLSetStmtAttr(SQLHSTMT StatementHandle, + SQLINTEGER Attribute, SQLPOINTER Value, + SQLINTEGER StringLength) +{ + SQLRETURN r; + r = doSQLSetStmtAttr(StatementHandle, Attribute, Value, StringLength); + return r; +} @@ -2823,3 +3114,367 @@ static SQLRETURN conv_tsdb_str_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIE return conv_tsdb_bin_to_c_bin(sql, target, field, (const unsigned char*)str); } + + + +const char* tsdb_int64_to_bit(int64_t src, int8_t *dst) +{ + *dst = src; + if (src==0 || src==1) return NULL; + return "22003"; +} + +const char* tsdb_int64_to_tinyint(int64_t src, int8_t *dst) +{ + *dst = src; + if (src>=SCHAR_MIN || src<=SCHAR_MAX) return SQL_SUCCESS; + return "22003"; +} + +const char* tsdb_int64_to_smallint(int64_t src, int16_t *dst) +{ + *dst = src; + if (src>=SHRT_MIN || src<=SHRT_MAX) return SQL_SUCCESS; + return "22003"; +} + +const char* tsdb_int64_to_int(int64_t src, int32_t *dst) +{ + *dst = src; + if (src>=LONG_MIN || src<=LONG_MAX) return SQL_SUCCESS; + return "22003"; +} + +const char* tsdb_int64_to_bigint(int64_t src, int64_t *dst) +{ + *dst = src; + return NULL; +} + +const char* tsdb_int64_to_ts(int64_t src, int64_t *dst) +{ + *dst = src; + + char buf[4096]; + int n = snprintf(buf, sizeof(buf), "%ld", src); + DASSERT(n>=0); + DASSERT(n=2) return "22003"; + + char buf[4096]; + int n = snprintf(buf, sizeof(buf), "%.*g", precision, src); + DASSERT(n>=0); + DASSERT(nSCHAR_MAX) return "22003"; + + char buf[4096]; + int n = snprintf(buf, sizeof(buf), "%.*g", precision, src); + DASSERT(n>=0); + DASSERT(nSHRT_MAX) return "22003"; + + char buf[4096]; + int n = snprintf(buf, sizeof(buf), "%.*g", precision, src); + DASSERT(n>=0); + DASSERT(nLONG_MAX) return "22003"; + + char buf[4096]; + int n = snprintf(buf, sizeof(buf), "%.*g", precision, src); + DASSERT(n>=0); + DASSERT(nLLONG_MAX) return "22003"; + + char buf[4096]; + int n = snprintf(buf, sizeof(buf), "%.*g", precision, src); + DASSERT(n>=0); + DASSERT(n=0); + DASSERT(n=0); + DASSERT(n2>=0); + DASSERT(n1=0); + if (n>=dlen) return "22001"; + + return NULL; +} + +const char* tsdb_chars_to_bit(const char *src, int8_t *dst) +{ + int bytes = 0; + int64_t v = 0; + int n = sscanf(src, "%ld%n", &v, &bytes); + if (n!=1) return "22018"; + + if (bytes!=strlen(src)) { + if (src[bytes-1]=='.') { + if (v==0 || v==1) return "22001"; + + return "22003"; + } + return "22018"; + } + + if (v==0 || v==1) return NULL; + + return "22003"; +} + +const char* tsdb_chars_to_tinyint(const char *src, int8_t *dst) +{ + int bytes = 0; + int64_t v = 0; + int n = sscanf(src, "%ld%n", &v, &bytes); + if (n!=1) return "22018"; + + + if (bytes!=strlen(src)) { + if (src[bytes-1]=='.') { + if (vSCHAR_MAX) return "22001"; + + return "22003"; + } + return "22018"; + } + + if (vSCHAR_MAX) return "22001"; + + return NULL; +} + +const char* tsdb_chars_to_smallint(const char *src, int16_t *dst) +{ + int bytes = 0; + int64_t v = 0; + int n = sscanf(src, "%ld%n", &v, &bytes); + if (n!=1) return "22018"; + + + if (bytes!=strlen(src)) { + if (src[bytes-1]=='.') { + if (vSHRT_MAX) return "22001"; + + return "22003"; + } + return "22018"; + } + + if (vSHRT_MAX) return "22001"; + + return NULL; +} + +const char* tsdb_chars_to_int(const char *src, int32_t *dst) +{ + int bytes = 0; + int64_t v = 0; + int n = sscanf(src, "%ld%n", &v, &bytes); + if (n!=1) return "22018"; + + + if (bytes!=strlen(src)) { + if (src[bytes-1]=='.') { + if (vLONG_MAX) return "22001"; + + return "22003"; + } + return "22018"; + } + + if (vLONG_MAX) return "22001"; + + return NULL; +} + +const char* tsdb_chars_to_bigint(const char *src, int64_t *dst) +{ + int bytes = 0; + int64_t v = 0; + int n = sscanf(src, "%ld%n", &v, &bytes); + if (n!=1) return "22018"; + + + if (bytes!=strlen(src)) { + if (src[bytes-1]=='.') { + if (vLLONG_MAX) return "22001"; + + return "22003"; + } + return "22018"; + } + + if (vLLONG_MAX) return "22001"; + + return NULL; +} + +const char* tsdb_chars_to_ts(const char *src, int64_t *dst) +{ + int bytes = 0; + int64_t v = 0; + int n = sscanf(src, "%ld%n", &v, &bytes); + if (n!=1) return "22018"; + + + if (bytes!=strlen(src)) { + if (src[bytes-1]=='.') { + if (vLLONG_MAX) return "22001"; + + return "22003"; + } + return "22018"; + } + + if (vLLONG_MAX) return "22001"; + + return NULL; +} + +const char* tsdb_chars_to_float(const char *src, float *dst) +{ + int bytes = 0; + int n = sscanf(src, "%f%n", dst, &bytes); + if (n!=1) return "22018"; + + if (bytes!=strlen(src)) return "22018"; + + return NULL; +} + +const char* tsdb_chars_to_double(const char *src, double *dst) +{ + int bytes = 0; + int n = sscanf(src, "%lf%n", dst, &bytes); + if (n!=1) return "22018"; + + if (bytes!=strlen(src)) return "22018"; + + return NULL; +} + +const char* tsdb_chars_to_char(const char *src, char *dst, size_t dlen) +{ + int n = snprintf(dst, dlen, "%s", src); + if (n>=dlen) return "22001"; + + return NULL; +} + diff --git a/src/connector/odbc/src/todbc_util.c b/src/connector/odbc/src/todbc_util.c index 9bbe8e6987..b6b45d8120 100644 --- a/src/connector/odbc/src/todbc_util.c +++ b/src/connector/odbc/src/todbc_util.c @@ -99,6 +99,18 @@ const char* sql_c_type(int type) { } } +int is_valid_sql_c_type(int type) { + const char *ctype = sql_c_type(type); + if (strcmp(ctype, "UNKNOWN")==0) return 0; + return 1; +} + +int is_valid_sql_sql_type(int type) { + const char *sqltype = sql_sql_type(type); + if (strcmp(sqltype, "UNKNOWN")==0) return 0; + return 1; +} + int string_conv(const char *fromcode, const char *tocode, const unsigned char *src, size_t sbytes, unsigned char *dst, size_t dbytes, diff --git a/src/connector/odbc/src/todbc_util.h b/src/connector/odbc/src/todbc_util.h index a1521ef1cf..43264975b4 100644 --- a/src/connector/odbc/src/todbc_util.h +++ b/src/connector/odbc/src/todbc_util.h @@ -47,6 +47,8 @@ do { \ const char* sql_sql_type(int type); const char* sql_c_type(int type); +int is_valid_sql_c_type(int type); +int is_valid_sql_sql_type(int type); int string_conv(const char *fromcode, const char *tocode, const unsigned char *src, size_t sbytes, diff --git a/src/connector/odbc/tests/odbc.py b/src/connector/odbc/tests/odbc.py index b1169679e3..61ad4f4379 100644 --- a/src/connector/odbc/tests/odbc.py +++ b/src/connector/odbc/tests/odbc.py @@ -43,10 +43,13 @@ cursor = cnxn.cursor() cursor.execute("insert into db.t values('2020-10-13 06:44:00', 1, 127, 32767, 32768, 32769, 123.456, 789.987, 'hello', 'world')") cursor.close() +print("hhhhhhhhhhhhhhhhhhhhhh") + cursor = cnxn.cursor() -cursor.execute("insert into db.t values(?,?,?,?,?,?,?,?,?,?)", "2020-10-13 07:06:00", 0, 127, 32767, 32768, 32769, 123.456, 789.987, "hel后lo", "wo哈rld"); +cursor.execute("insert into db.t values(?,?,?,?,?,?,?,?,?,?)", "2020-10-13 07:06:00", 0, 229, 32767, 32768, 32769, 123.456, 789.987, "hel后lo", "wo哈rld"); cursor.close() +print("hhhhhhhhhhhhhhhhhhhhhh") cursor = cnxn.cursor() cursor.execute("SELECT * from db.t") row = cursor.fetchone() @@ -79,3 +82,34 @@ while row: row = cursor.fetchone() cursor.close() +cursor = cnxn.cursor() +cursor.execute("create table db.v (ts timestamp, v1 tinyint)") +cursor.close() + +params = [ ('A', 1), ('B', 2), ('C', 3) ] +params = [ ('A', 1), ('B', 2), ('C', 3) ] +params = [ ('2020-10-16 00:00:00', 1), + ('2020-10-16 00:00:01', 4), + ('2020-10-16 00:00:02', 5), + ('2020-10-16 00:00:03', 6) ] +cursor = cnxn.cursor() +cursor.fast_executemany = True +cursor.executemany("insert into db.v values (?, ?)", params) +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("SELECT * from db.v") +row = cursor.fetchone() +while row: + print(row) + row = cursor.fetchone() +cursor.close() + +cursor = cnxn.cursor() +cursor.execute("SELECT * from db.v where v1 > ?", 4) +row = cursor.fetchone() +while row: + print(row) + row = cursor.fetchone() +cursor.close() + From 2436485e9b30102f66af6502fa022edfd21cff9f Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 17 Oct 2020 06:25:17 +0800 Subject: [PATCH 019/101] test case: with user-provided type other than table field type --- src/connector/odbc/tests/odbc.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/connector/odbc/tests/odbc.py b/src/connector/odbc/tests/odbc.py index 61ad4f4379..02ac975776 100644 --- a/src/connector/odbc/tests/odbc.py +++ b/src/connector/odbc/tests/odbc.py @@ -113,3 +113,11 @@ while row: row = cursor.fetchone() cursor.close() +cursor = cnxn.cursor() +cursor.execute("SELECT * from db.v where v1 > ?", '5') +row = cursor.fetchone() +while row: + print(row) + row = cursor.fetchone() +cursor.close() + From a6c918bf4208302ae7efd5515c1dbd75ce4e5855 Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 17 Oct 2020 06:50:28 +0800 Subject: [PATCH 020/101] 1. msvc: C4244, conversion-loss-of-data 2. msvc: C4477, "%ld" => "%" PRId64 "" --- src/client/src/tscPrepare.c | 41 +++++++++++++++++----------------- src/connector/odbc/src/todbc.c | 22 +++++++++--------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index d6dd19db77..fe609da9a5 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -290,15 +290,15 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { if (u.v1==0 || u.v1==1) break; } break; case TSDB_DATA_TYPE_SMALLINT: { - u.v1 = *(int16_t*)bind->buffer; + u.v1 = (int8_t)*(int16_t*)bind->buffer; if (u.v1==0 || u.v1==1) break; } break; case TSDB_DATA_TYPE_INT: { - u.v1 = *(int32_t*)bind->buffer; + u.v1 = (int8_t)*(int32_t*)bind->buffer; if (u.v1==0 || u.v1==1) break; } break; case TSDB_DATA_TYPE_BIGINT: { - u.v1 = *(int64_t*)bind->buffer; + u.v1 = (int8_t)*(int64_t*)bind->buffer; if (u.v1==0 || u.v1==1) break; } break; case TSDB_DATA_TYPE_BINARY: @@ -334,19 +334,19 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { } break; case TSDB_DATA_TYPE_SMALLINT: { int16_t v = *(int16_t*)bind->buffer; - u.v1 = v; + u.v1 = (int8_t)v; if (v >= SCHAR_MIN && v <= SCHAR_MAX) break; return TSDB_CODE_TSC_INVALID_VALUE; } break; case TSDB_DATA_TYPE_INT: { int32_t v = *(int32_t*)bind->buffer; - u.v1 = v; + u.v1 = (int8_t)v; if (v >= SCHAR_MIN && v <= SCHAR_MAX) break; return TSDB_CODE_TSC_INVALID_VALUE; } break; case TSDB_DATA_TYPE_BIGINT: { int64_t v = *(int64_t*)bind->buffer; - u.v1 = v; + u.v1 = (int8_t)v; if (v >= SCHAR_MIN && v <= SCHAR_MAX) break; return TSDB_CODE_TSC_INVALID_VALUE; } break; @@ -354,9 +354,9 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { case TSDB_DATA_TYPE_NCHAR: { int64_t v; int n,r; - r = sscanf((const char*)bind->buffer, "%ld%n", &v, &n); + r = sscanf((const char*)bind->buffer, "%" PRId64 "%n", &v, &n); if (r==1 && n==strlen((const char*)bind->buffer)) { - u.v1 = v; + u.v1 = (int8_t)v; if (v >= SCHAR_MIN && v <= SCHAR_MAX) break; } return TSDB_CODE_TSC_INVALID_VALUE; @@ -377,17 +377,17 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_SMALLINT: { int v = *(int16_t*)bind->buffer; - u.v2 = v; + u.v2 = (int16_t)v; } break; case TSDB_DATA_TYPE_INT: { int32_t v = *(int32_t*)bind->buffer; - u.v2 = v; + u.v2 = (int16_t)v; if (v >= SHRT_MIN && v <= SHRT_MAX) break; return TSDB_CODE_TSC_INVALID_VALUE; } break; case TSDB_DATA_TYPE_BIGINT: { int64_t v = *(int64_t*)bind->buffer; - u.v2 = v; + u.v2 = (int16_t)v; if (v >= SHRT_MIN && v <= SHRT_MAX) break; return TSDB_CODE_TSC_INVALID_VALUE; } break; @@ -395,9 +395,9 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { case TSDB_DATA_TYPE_NCHAR: { int64_t v; int n,r; - r = sscanf((const char*)bind->buffer, "%ld%n", &v, &n); + r = sscanf((const char*)bind->buffer, "%" PRId64 "%n", &v, &n); if (r==1 && n==strlen((const char*)bind->buffer)) { - u.v2 = v; + u.v2 = (int16_t)v; if (v >= SHRT_MIN && v <= SHRT_MAX) break; } return TSDB_CODE_TSC_INVALID_VALUE; @@ -430,7 +430,7 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { case TSDB_DATA_TYPE_NCHAR: { int64_t v; int n,r; - r = sscanf((const char*)bind->buffer, "%ld%n", &v, &n); + r = sscanf((const char*)bind->buffer, "%" PRId64 "%n", &v, &n); if (r==1 && n==strlen((const char*)bind->buffer)) { u.v4 = v; if (v >= INT_MIN && v <= INT_MAX) break; @@ -457,11 +457,11 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { u.f4 = *(int16_t*)bind->buffer; } break; case TSDB_DATA_TYPE_INT: { - u.f4 = *(int32_t*)bind->buffer; + u.f4 = (float)*(int32_t*)bind->buffer; // shall we check equality? } break; case TSDB_DATA_TYPE_BIGINT: { - u.f4 = *(int64_t*)bind->buffer; + u.f4 = (float)*(int64_t*)bind->buffer; // shall we check equality? } break; case TSDB_DATA_TYPE_FLOAT: { @@ -509,7 +509,7 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { case TSDB_DATA_TYPE_NCHAR: { int64_t v; int n,r; - r = sscanf((const char*)bind->buffer, "%ld%n", &v, &n); + r = sscanf((const char*)bind->buffer, "%" PRId64 "%n", &v, &n); if (r==1 && n==strlen((const char*)bind->buffer)) { u.v8 = v; break; @@ -539,7 +539,7 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { u.f8 = *(int32_t*)bind->buffer; } break; case TSDB_DATA_TYPE_BIGINT: { - u.f8 = *(int64_t*)bind->buffer; + u.f8 = (double)*(int64_t*)bind->buffer; } break; case TSDB_DATA_TYPE_FLOAT: { u.f8 = *(float*)bind->buffer; @@ -553,7 +553,7 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { int n,r; r = sscanf((const char*)bind->buffer, "%lf%n", &v, &n); if (r==1 && n==strlen((const char*)bind->buffer)) { - u.v8 = v; + u.f8 = v; break; } return TSDB_CODE_TSC_INVALID_VALUE; @@ -574,7 +574,8 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { case TSDB_DATA_TYPE_BINARY: case TSDB_DATA_TYPE_NCHAR: { // is this the correct way to call taosParseTime? - if (taosParseTime(bind->buffer, &u.v8, *bind->length, 3, tsDaylight) == TSDB_CODE_SUCCESS) { + int32_t len = (int32_t)*bind->length; + if (taosParseTime(bind->buffer, &u.v8, len, 3, tsDaylight) == TSDB_CODE_SUCCESS) { break; } return TSDB_CODE_TSC_INVALID_VALUE; diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 45f005a7ed..518c98b41f 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -2690,7 +2690,7 @@ static SQLRETURN conv_tsdb_v8_to_c_double(sql_t *sql, c_target_t *target, TAOS_F static SQLRETURN conv_tsdb_v8_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8) { char buf[64]; - int n = snprintf(buf, sizeof(buf), "%ld", v8); + int n = snprintf(buf, sizeof(buf), "%" PRId64 "", v8); DASSERT(nsoi = n; strncpy(target->ptr, buf, (n>=target->len ? target->len : n+1)); @@ -2702,7 +2702,7 @@ static SQLRETURN conv_tsdb_v8_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIE static SQLRETURN conv_tsdb_v8_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, int64_t v8) { char buf[64]; - int n = snprintf(buf, sizeof(buf), "%ld", v8); + int n = snprintf(buf, sizeof(buf), "%" PRId64 "", v8); DASSERT(nsoi = n; strncpy(target->ptr, buf, (n>target->len ? target->len : n)); @@ -3053,7 +3053,7 @@ static SQLRETURN conv_tsdb_str_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIEL } char buf[64]; - snprintf(buf, sizeof(buf), "%ld", v); + snprintf(buf, sizeof(buf), "%" PRId64 "", v); if (strcmp(buf, str)==0) return SQL_SUCCESS; @@ -3156,7 +3156,7 @@ const char* tsdb_int64_to_ts(int64_t src, int64_t *dst) *dst = src; char buf[4096]; - int n = snprintf(buf, sizeof(buf), "%ld", src); + int n = snprintf(buf, sizeof(buf), "%" PRId64 "", src); DASSERT(n>=0); DASSERT(n Date: Sat, 17 Oct 2020 07:17:45 +0800 Subject: [PATCH 021/101] msvc: C4244 --- src/client/src/tscPrepare.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index fe609da9a5..a579eacdea 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -422,7 +422,7 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { } break; case TSDB_DATA_TYPE_BIGINT: { int64_t v = *(int64_t*)bind->buffer; - u.v4 = v; + u.v4 = (int32_t)v; if (v >= INT_MIN && v <= INT_MAX) break; return TSDB_CODE_TSC_INVALID_VALUE; } break; From 273b8e1732ebfbf2ef1e4f124e6864a67ecaaf0a Mon Sep 17 00:00:00 2001 From: freemine Date: Sat, 17 Oct 2020 07:37:21 +0800 Subject: [PATCH 022/101] msvc: C4244 --- src/client/src/tscPrepare.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index a579eacdea..1739e4348c 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -432,7 +432,7 @@ static int doBindParam(char* data, SParamInfo* param, TAOS_BIND* bind) { int n,r; r = sscanf((const char*)bind->buffer, "%" PRId64 "%n", &v, &n); if (r==1 && n==strlen((const char*)bind->buffer)) { - u.v4 = v; + u.v4 = (int32_t)v; if (v >= INT_MIN && v <= INT_MAX) break; } return TSDB_CODE_TSC_INVALID_VALUE; From a87717bb7d712ddd39a6d3cd37ac48002ac77524 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sat, 17 Oct 2020 16:35:12 +0800 Subject: [PATCH 023/101] [td-225] --- src/client/src/tscAsync.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/src/tscAsync.c b/src/client/src/tscAsync.c index 639de294e6..003eddc8d2 100644 --- a/src/client/src/tscAsync.c +++ b/src/client/src/tscAsync.c @@ -405,11 +405,11 @@ void tscTableMetaCallBack(void *param, TAOS_RES *res, int code) { SSqlRes *pRes = &pSql->res; pRes->code = code; + const char* msg = (pCmd->command == TSDB_SQL_STABLEVGROUP)? "vgroup-list":"table-meta"; if (code != TSDB_CODE_SUCCESS) { - tscError("%p get tableMeta failed, code:%s", pSql, tstrerror(code)); + tscError("%p get %s failed, code:%s", pSql, msg, tstrerror(code)); goto _error; } else { - const char* msg = (pCmd->command == TSDB_SQL_STABLEVGROUP)? "vgroup-list":"table-meta"; tscDebug("%p get %s successfully", pSql, msg); } From d069a35683b015f4e65601943fee4e6a3b0ec4c9 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sat, 17 Oct 2020 22:38:07 +0800 Subject: [PATCH 024/101] [td-225] add more info in log. --- src/vnode/src/vnodeRead.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/vnode/src/vnodeRead.c b/src/vnode/src/vnodeRead.c index baaeae2a81..f6e773c96b 100644 --- a/src/vnode/src/vnodeRead.c +++ b/src/vnode/src/vnodeRead.c @@ -67,12 +67,12 @@ int32_t vnodeProcessRead(void *param, SReadMsg *pReadMsg) { return (*vnodeProcessReadMsgFp[msgType])(pVnode, pReadMsg); } -static void vnodePutItemIntoReadQueue(SVnodeObj *pVnode, void **qhandle) { +static void vnodePutItemIntoReadQueue(SVnodeObj *pVnode, void **qhandle, void *ahandle) { SReadMsg *pRead = (SReadMsg *)taosAllocateQitem(sizeof(SReadMsg)); pRead->rpcMsg.msgType = TSDB_MSG_TYPE_QUERY; pRead->pCont = qhandle; pRead->contLen = 0; - pRead->rpcMsg.handle = NULL; + pRead->rpcMsg.ahandle = ahandle; atomic_add_fetch_32(&pVnode->refCount, 1); @@ -80,14 +80,23 @@ static void vnodePutItemIntoReadQueue(SVnodeObj *pVnode, void **qhandle) { taosWriteQitem(pVnode->rqueue, TAOS_QTYPE_QUERY, pRead); } -static int32_t vnodeDumpQueryResult(SRspRet *pRet, void *pVnode, void **handle, bool *freeHandle) { +/** + * + * @param pRet response message object + * @param pVnode the vnode object + * @param handle qhandle for executing query + * @param freeHandle free qhandle or not + * @param ahandle sqlObj address at client side + * @return + */ +static int32_t vnodeDumpQueryResult(SRspRet *pRet, void *pVnode, void **handle, bool *freeHandle, void *ahandle) { bool continueExec = false; int32_t code = TSDB_CODE_SUCCESS; if ((code = qDumpRetrieveResult(*handle, (SRetrieveTableRsp **)&pRet->rsp, &pRet->len, &continueExec)) == TSDB_CODE_SUCCESS) { if (continueExec) { *freeHandle = false; - vnodePutItemIntoReadQueue(pVnode, handle); + vnodePutItemIntoReadQueue(pVnode, handle, ahandle); pRet->qhandle = *handle; } else { *freeHandle = true; @@ -189,7 +198,7 @@ static int32_t vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { if (handle != NULL) { vDebug("vgId:%d, QInfo:%p, dnode query msg disposed, create qhandle and returns to app", vgId, *handle); - vnodePutItemIntoReadQueue(pVnode, handle); + vnodePutItemIntoReadQueue(pVnode, handle, pReadMsg->rpcMsg.ahandle); } } else { assert(pCont != NULL); @@ -210,7 +219,7 @@ static int32_t vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { pReadMsg->rpcMsg.handle); // set the real rsp error code - pReadMsg->rpcMsg.code = vnodeDumpQueryResult(&pReadMsg->rspRet, pVnode, qhandle, &freehandle); + pReadMsg->rpcMsg.code = vnodeDumpQueryResult(&pReadMsg->rspRet, pVnode, qhandle, &freehandle, pReadMsg->rpcMsg.ahandle); // NOTE: set return code to be TSDB_CODE_QRY_HAS_RSP to notify dnode to return msg to client code = TSDB_CODE_QRY_HAS_RSP; @@ -287,7 +296,8 @@ static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { return TSDB_CODE_QRY_NOT_READY; } - code = vnodeDumpQueryResult(pRet, pVnode, handle, &freeHandle); + // ahandle is the sqlObj pointer + code = vnodeDumpQueryResult(pRet, pVnode, handle, &freeHandle, pReadMsg->rpcMsg.ahandle); } // If qhandle is not added into vread queue, the query should be completed already or paused with error. From 95adf3ea611be0c62e4b50131d4ed9432eec2ef5 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sat, 17 Oct 2020 23:47:40 +0800 Subject: [PATCH 025/101] [td-225] --- src/query/src/qExecutor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index f742616b05..6072b83121 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -6692,12 +6692,12 @@ static bool doBuildResCheck(SQInfo* pQInfo) { pQInfo->dataReady = QUERY_RESULT_READY; buildRes = (pQInfo->rspContext != NULL); - pthread_mutex_unlock(&pQInfo->lock); - - // clear qhandle owner + // clear qhandle owner, it must be in the secure area. other thread may run ahead before current, after it is + // put into task to be executed. assert(pQInfo->owner == taosGetPthreadId()); pQInfo->owner = 0; + pthread_mutex_unlock(&pQInfo->lock); return buildRes; } From adcbb0fe4017456364ece39c0b634abed235181a Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 18 Oct 2020 00:23:19 +0800 Subject: [PATCH 026/101] [td-225] --- src/vnode/src/vnodeRead.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/vnode/src/vnodeRead.c b/src/vnode/src/vnodeRead.c index f6e773c96b..79e69b97a1 100644 --- a/src/vnode/src/vnodeRead.c +++ b/src/vnode/src/vnodeRead.c @@ -224,6 +224,9 @@ static int32_t vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { // NOTE: set return code to be TSDB_CODE_QRY_HAS_RSP to notify dnode to return msg to client code = TSDB_CODE_QRY_HAS_RSP; } else { + void* h1 = qGetResultRetrieveMsg(*qhandle); + assert(h1 == NULL); + freehandle = qQueryCompleted(*qhandle); } From 5adcf14d6e3f5f6c9cc37639789480c133d6ead9 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 18 Oct 2020 00:45:49 +0800 Subject: [PATCH 027/101] [td-225] --- src/query/src/qExecutor.c | 5 +++++ src/vnode/src/vnodeRead.c | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 6072b83121..1070f65284 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -6227,6 +6227,7 @@ static SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGrou // NOTE: pTableCheckInfo need to update the query time range and the lastKey info pQInfo->arrTableIdInfo = taosArrayInit(tableIndex, sizeof(STableIdInfo)); pQInfo->dataReady = QUERY_RESULT_NOT_READY; + pQInfo->rspContext = NULL; pthread_mutex_init(&pQInfo->lock, NULL); pQuery->pos = -1; @@ -6761,6 +6762,7 @@ int32_t qRetrieveQueryResultInfo(qinfo_t qinfo, bool* buildRes, void* pRspContex SQInfo *pQInfo = (SQInfo *)qinfo; if (pQInfo == NULL || !isValidQInfo(pQInfo)) { + qError("QInfo:%p invalid qhandle", pQInfo); return TSDB_CODE_QRY_INVALID_QHANDLE; } @@ -6773,6 +6775,8 @@ int32_t qRetrieveQueryResultInfo(qinfo_t qinfo, bool* buildRes, void* pRspContex int32_t code = TSDB_CODE_SUCCESS; pthread_mutex_lock(&pQInfo->lock); + assert(pQInfo->rspContext == NULL); + if (pQInfo->dataReady == QUERY_RESULT_READY) { *buildRes = true; qDebug("QInfo:%p retrieve result info, rowsize:%d, rows:%"PRId64", code:%d", pQInfo, pQuery->rowSize, pQuery->rec.rows, @@ -6781,6 +6785,7 @@ int32_t qRetrieveQueryResultInfo(qinfo_t qinfo, bool* buildRes, void* pRspContex *buildRes = false; qDebug("QInfo:%p retrieve req set query return result after paused", pQInfo); pQInfo->rspContext = pRspContext; + assert(pQInfo->rspContext != NULL); } code = pQInfo->code; diff --git a/src/vnode/src/vnodeRead.c b/src/vnode/src/vnodeRead.c index 79e69b97a1..a610a8b0df 100644 --- a/src/vnode/src/vnodeRead.c +++ b/src/vnode/src/vnodeRead.c @@ -295,6 +295,8 @@ static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { freeHandle = true; } else { // result is not ready, return immediately if (!buildRes) { + assert(pReadMsg->rpcMsg.handle != NULL); + qReleaseQInfo(pVnode->qMgmt, (void **)&handle, false); return TSDB_CODE_QRY_NOT_READY; } From 5df7a811604b04a2406455247430e22bdd2537aa Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 18 Oct 2020 01:14:01 +0800 Subject: [PATCH 028/101] [td-225] --- src/rpc/src/rpcMain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/src/rpcMain.c b/src/rpc/src/rpcMain.c index 63231bb1fa..6e9088d9fb 100644 --- a/src/rpc/src/rpcMain.c +++ b/src/rpc/src/rpcMain.c @@ -412,7 +412,7 @@ void rpcSendResponse(const SRpcMsg *pRsp) { rpcLockConn(pConn); if ( pConn->inType == 0 || pConn->user[0] == 0 ) { - tDebug("%s, connection is already released, rsp wont be sent", pConn->info); + tError("%s, connection is already released, rsp wont be sent", pConn->info); rpcUnlockConn(pConn); rpcFreeCont(pMsg->pCont); rpcDecRef(pRpc); From 134d1f64343b93d29ede1afca148c6e746afd9b9 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 18 Oct 2020 02:03:10 +0800 Subject: [PATCH 029/101] [td-225] --- src/dnode/src/dnodeVRead.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dnode/src/dnodeVRead.c b/src/dnode/src/dnodeVRead.c index 1a3d0ebc27..cdb03de555 100644 --- a/src/dnode/src/dnodeVRead.c +++ b/src/dnode/src/dnodeVRead.c @@ -213,7 +213,8 @@ static void *dnodeProcessReadQueue(void *param) { } else { if (code == TSDB_CODE_QRY_HAS_RSP) { dnodeSendRpcReadRsp(pVnode, pReadMsg, pReadMsg->rpcMsg.code); - } else { // code == TSDB_CODE_NOT_READY, do not return msg to client + } else { // code == TSDB_CODE_QRY_NOT_READY, do not return msg to client + assert(pReadMsg->rpcMsg.handle != NULL); dnodeDispatchNonRspMsg(pVnode, pReadMsg, code); } } From a0a274e8ba6a795661153312c4b8a5fe41909f0b Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 18 Oct 2020 02:16:36 +0800 Subject: [PATCH 030/101] [td-225] --- src/dnode/src/dnodeVRead.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dnode/src/dnodeVRead.c b/src/dnode/src/dnodeVRead.c index cdb03de555..9dac65c765 100644 --- a/src/dnode/src/dnodeVRead.c +++ b/src/dnode/src/dnodeVRead.c @@ -214,7 +214,7 @@ static void *dnodeProcessReadQueue(void *param) { if (code == TSDB_CODE_QRY_HAS_RSP) { dnodeSendRpcReadRsp(pVnode, pReadMsg, pReadMsg->rpcMsg.code); } else { // code == TSDB_CODE_QRY_NOT_READY, do not return msg to client - assert(pReadMsg->rpcMsg.handle != NULL); + assert(pReadMsg->rpcMsg.handle == NULL); dnodeDispatchNonRspMsg(pVnode, pReadMsg, code); } } From 1e8461e66c9095a2975818a9924e19809f1e8730 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 18 Oct 2020 02:24:29 +0800 Subject: [PATCH 031/101] [td-225] --- src/dnode/src/dnodeVRead.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dnode/src/dnodeVRead.c b/src/dnode/src/dnodeVRead.c index 9dac65c765..f6909e54d4 100644 --- a/src/dnode/src/dnodeVRead.c +++ b/src/dnode/src/dnodeVRead.c @@ -214,7 +214,7 @@ static void *dnodeProcessReadQueue(void *param) { if (code == TSDB_CODE_QRY_HAS_RSP) { dnodeSendRpcReadRsp(pVnode, pReadMsg, pReadMsg->rpcMsg.code); } else { // code == TSDB_CODE_QRY_NOT_READY, do not return msg to client - assert(pReadMsg->rpcMsg.handle == NULL); + assert(pReadMsg->rpcMsg.handle == NULL || (pReadMsg->rpcMsg.handle != NULL && pReadMsg->rpcMsg.msgType == 5)); dnodeDispatchNonRspMsg(pVnode, pReadMsg, code); } } From b0ff31e1a276c131960205364beff44d91fe70a4 Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 18 Oct 2020 14:18:15 +0800 Subject: [PATCH 032/101] remove obsolete code --- src/CMakeLists.txt | 2 +- src/connector/odbc/src/todbc.c | 24 ++---------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f619edd221..b2c6e70f68 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,5 +21,5 @@ ADD_SUBDIRECTORY(wal) ADD_SUBDIRECTORY(cq) ADD_SUBDIRECTORY(dnode) ADD_SUBDIRECTORY(connector/odbc) -ADD_SUBDIRECTORY(connector/jdbc) +#ADD_SUBDIRECTORY(connector/jdbc) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 518c98b41f..46d10fd980 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -1771,7 +1771,7 @@ static SQLRETURN do_bind_batch(sql_t *sql, int idx_row, TAOS_BIND *binds) return SQL_SUCCESS; } -static SQLRETURN do_execute(sql_t *sql, TAOS_BIND *x_binds) +static SQLRETURN do_execute(sql_t *sql) { int tr = TSDB_CODE_SUCCESS; if (sql->n_rows==0) sql->n_rows = 1; @@ -1836,27 +1836,7 @@ static SQLRETURN doSQLExecute(SQLHSTMT StatementHandle) sql->row = NULL; } - TAOS_BIND *binds = NULL; - if (sql->n_params>0) { - binds = (TAOS_BIND*)calloc(sql->n_params, sizeof(*binds)); - if (!binds) { - SET_ERROR(sql, "HY001", TSDB_CODE_ODBC_OOM, ""); - return SQL_ERROR; - } - } - - SQLRETURN r = do_execute(sql, binds); - - if (binds) { - for (int i = 0; in_params; ++i) { - TAOS_BIND *bind = binds + i; - if (bind->allocated) { - free(bind->u.nchar); - bind->u.nchar = NULL; - } - } - free(binds); - } + SQLRETURN r = do_execute(sql); return r; } From b3165d4193819e7aaf719cd7048addb27a3fd37d Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 18 Oct 2020 15:38:18 +0800 Subject: [PATCH 033/101] [td-225] --- src/query/inc/qExecutor.h | 1 + src/query/src/qExecutor.c | 13 ++++++++++++- src/vnode/src/vnodeRead.c | 11 ++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/query/inc/qExecutor.h b/src/query/inc/qExecutor.h index 5d570821cb..fbf699297a 100644 --- a/src/query/inc/qExecutor.h +++ b/src/query/inc/qExecutor.h @@ -206,6 +206,7 @@ typedef struct SQInfo { void* pBuf; // allocated buffer for STableQueryInfo, sizeof(STableQueryInfo)*numOfTables; pthread_mutex_t lock; // used to synchronize the rsp/query threads + tsem_t ready; int32_t dataReady; // denote if query result is ready or not void* rspContext; // response context } SQInfo; diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 1070f65284..b9a5295ed2 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -6699,6 +6699,8 @@ static bool doBuildResCheck(SQInfo* pQInfo) { pQInfo->owner = 0; pthread_mutex_unlock(&pQInfo->lock); + + tsem_post(&pQInfo->ready); return buildRes; } @@ -6767,13 +6769,16 @@ int32_t qRetrieveQueryResultInfo(qinfo_t qinfo, bool* buildRes, void* pRspContex } *buildRes = false; - SQuery *pQuery = pQInfo->runtimeEnv.pQuery; if (IS_QUERY_KILLED(pQInfo)) { qDebug("QInfo:%p query is killed, code:%d", pQInfo, pQInfo->code); return pQInfo->code; } int32_t code = TSDB_CODE_SUCCESS; + +#if 0 + SQuery *pQuery = pQInfo->runtimeEnv.pQuery; + pthread_mutex_lock(&pQInfo->lock); assert(pQInfo->rspContext == NULL); @@ -6790,6 +6795,12 @@ int32_t qRetrieveQueryResultInfo(qinfo_t qinfo, bool* buildRes, void* pRspContex code = pQInfo->code; pthread_mutex_unlock(&pQInfo->lock); +#else + tsem_wait(&pQInfo->ready); + *buildRes = true; + code = pQInfo->code; +#endif + return code; } diff --git a/src/vnode/src/vnodeRead.c b/src/vnode/src/vnodeRead.c index a610a8b0df..ede5921dc1 100644 --- a/src/vnode/src/vnodeRead.c +++ b/src/vnode/src/vnodeRead.c @@ -14,7 +14,8 @@ */ #define _DEFAULT_SOURCE -//#include +#define _NON_BLOCKING_RETRIEVE 0 + #include "os.h" #include "tglobal.h" @@ -206,6 +207,8 @@ static int32_t vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { vDebug("vgId:%d, QInfo:%p, dnode continues to exec query", pVnode->vgId, *qhandle); + +#if _NON_BLOCKING_RETRIEVE bool freehandle = false; bool buildRes = qTableQuery(*qhandle); // do execute query @@ -235,6 +238,9 @@ static int32_t vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { if (freehandle || (!buildRes)) { qReleaseQInfo(pVnode->qMgmt, (void **)&qhandle, freehandle); } +#else + qTableQuery(*qhandle); // do execute query +#endif } return code; @@ -294,12 +300,15 @@ static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { memset(pRet->rsp, 0, sizeof(SRetrieveTableRsp)); freeHandle = true; } else { // result is not ready, return immediately + assert(buildRes == true); +#if _NON_BLOCKING_RETRIEVE if (!buildRes) { assert(pReadMsg->rpcMsg.handle != NULL); qReleaseQInfo(pVnode->qMgmt, (void **)&handle, false); return TSDB_CODE_QRY_NOT_READY; } +#endif // ahandle is the sqlObj pointer code = vnodeDumpQueryResult(pRet, pVnode, handle, &freeHandle, pReadMsg->rpcMsg.ahandle); From 4d7a69d1905400b826ec053ae4c8f9d20909ce65 Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 18 Oct 2020 20:22:47 +0800 Subject: [PATCH 034/101] 1. support fraction in timestamp 2. trying to use tsdb_xxx_to_yyy --- src/connector/odbc/src/todbc.c | 124 ++++++++++++++++++------------- src/connector/odbc/tests/odbc.py | 7 +- 2 files changed, 74 insertions(+), 57 deletions(-) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index 46d10fd980..953999c434 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -143,6 +143,19 @@ do { \ D("%s: elapsed: [%.6f]s", #statement, delta); \ } while (0) + +#define CHK_CONV(statement) \ +do { \ + const char *sqlstate = statement; \ + if (sqlstate) { \ + SET_ERROR(sql, sqlstate, TSDB_CODE_ODBC_OUT_OF_RANGE, \ + "no convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d]", \ + sql_c_type(valueType), valueType, valueType, \ + taos_data_type(type), type, type, idx+1); \ + return SQL_ERROR; \ + } \ +} while (0) + typedef struct env_s env_t; typedef struct conn_s conn_t; typedef struct sql_s sql_t; @@ -541,7 +554,6 @@ static SQLRETURN doSQLFreeStmt(SQLHSTMT StatementHandle, } sql->n_params = 0; - DASSERT(DEC_REF(sql->conn)>0); DASSERT(DEC_REF(sql)==0); @@ -796,10 +808,10 @@ static SQLRETURN conv_tsdb_f4_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F static SQLRETURN conv_tsdb_f8_to_c_double(sql_t *sql, c_target_t *target, TAOS_FIELD *field, double f8); static SQLRETURN conv_tsdb_f8_to_c_char(sql_t *sql, c_target_t *target, TAOS_FIELD *field, double f8); static SQLRETURN conv_tsdb_f8_to_c_binary(sql_t *sql, c_target_t *target, TAOS_FIELD *field, double f8); -static SQLRETURN conv_tsdb_ts_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts); -static SQLRETURN conv_tsdb_ts_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts); -static SQLRETURN conv_tsdb_ts_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts); -static SQLRETURN conv_tsdb_ts_to_c_ts(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts); +static SQLRETURN conv_tsdb_ts_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIELD *field, SQL_TIMESTAMP_STRUCT *ts); +static SQLRETURN conv_tsdb_ts_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, SQL_TIMESTAMP_STRUCT *ts); +static SQLRETURN conv_tsdb_ts_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, SQL_TIMESTAMP_STRUCT *ts); +static SQLRETURN conv_tsdb_ts_to_c_ts(sql_t *sql, c_target_t *target, TAOS_FIELD *field, SQL_TIMESTAMP_STRUCT *ts); static SQLRETURN conv_tsdb_bin_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const unsigned char *bin); static SQLRETURN conv_tsdb_bin_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const unsigned char *bin); static SQLRETURN conv_tsdb_str_to_c_bit(sql_t *sql, c_target_t *target, TAOS_FIELD *field, const char *str); @@ -987,7 +999,7 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, } } break; case TSDB_DATA_TYPE_TIMESTAMP: { - TIMESTAMP_STRUCT ts = {0}; + SQL_TIMESTAMP_STRUCT ts = {0}; int64_t v = *(int64_t*)row; time_t t = v/1000; struct tm tm = {0}; @@ -998,7 +1010,7 @@ static SQLRETURN doSQLGetData(SQLHSTMT StatementHandle, ts.hour = tm.tm_hour; ts.minute = tm.tm_min; ts.second = tm.tm_sec; - ts.fraction = v%1000; + ts.fraction = v%1000 * 1000000; switch (target.ct) { case SQL_C_SBIGINT: return conv_tsdb_ts_to_c_v8(sql, &target, field, &ts); case SQL_C_CHAR: return conv_tsdb_ts_to_c_str(sql, &target, field, &ts); @@ -1269,10 +1281,10 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin bind->length = &bind->buffer_length; switch (valueType) { case SQL_C_LONG: { - bind->u.b = *(int32_t*)paramValue; + CHK_CONV(tsdb_int64_to_bit(*(int32_t*)paramValue, &bind->u.b)); } break; case SQL_C_BIT: { - bind->u.b = *(int8_t*)paramValue; + CHK_CONV(tsdb_int64_to_bit(*(int8_t*)paramValue, &bind->u.b)); } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1313,16 +1325,16 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin bind->length = &bind->buffer_length; switch (valueType) { case SQL_C_TINYINT: { - bind->u.v1 = *(int8_t*)paramValue; + CHK_CONV(tsdb_int64_to_tinyint(*(int8_t*)paramValue, &bind->u.v1)); } break; case SQL_C_SHORT: { - bind->u.v1 = *(int16_t*)paramValue; + CHK_CONV(tsdb_int64_to_tinyint(*(int16_t*)paramValue, &bind->u.v1)); } break; case SQL_C_LONG: { - bind->u.v1 = *(int32_t*)paramValue; + CHK_CONV(tsdb_int64_to_tinyint(*(int32_t*)paramValue, &bind->u.v1)); } break; case SQL_C_SBIGINT: { - bind->u.v1 = *(int64_t*)paramValue; + CHK_CONV(tsdb_int64_to_tinyint(*(int64_t*)paramValue, &bind->u.v1)); } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1361,10 +1373,10 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin bind->length = &bind->buffer_length; switch (valueType) { case SQL_C_LONG: { - bind->u.v2 = *(int32_t*)paramValue; + CHK_CONV(tsdb_int64_to_smallint(*(int32_t*)paramValue, &bind->u.v2)); } break; case SQL_C_SHORT: { - bind->u.v2 = *(int16_t*)paramValue; + CHK_CONV(tsdb_int64_to_smallint(*(int16_t*)paramValue, &bind->u.v2)); } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1405,7 +1417,7 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin bind->length = &bind->buffer_length; switch (valueType) { case SQL_C_LONG: { - bind->u.v4 = *(int32_t*)paramValue; + CHK_CONV(tsdb_int64_to_int(*(int32_t*)paramValue, &bind->u.v4)); } break; case SQL_C_CHAR: case SQL_C_WCHAR: @@ -1635,13 +1647,20 @@ static SQLRETURN do_bind_param_value(sql_t *sql, int idx_row, int idx, param_bin DASSERT(soi); DASSERT(*soi != SQL_NTS); size_t bytes = 0; + int r = 0; + int64_t t = 0; SQLCHAR *utf8 = wchars_to_chars(paramValue, *soi/2, &bytes); - struct tm tm = {0}; - strptime((const char*)utf8, "%Y-%m-%d %H:%M:%S", &tm); - int64_t t = (int64_t)mktime(&tm); - t *= 1000; + // why cast utf8 to 'char*' ? + r = taosParseTime((char*)utf8, &t, strlen((const char*)utf8), TSDB_TIME_PRECISION_MILLI, 0); bind->u.v8 = t; free(utf8); + if (r) { + SET_ERROR(sql, "22007", TSDB_CODE_ODBC_OUT_OF_RANGE, + "convertion from [%s[%d/0x%x]] to [%s[%d/0x%x]] for parameter [%d] failed", + sql_c_type(valueType), valueType, valueType, + taos_data_type(type), type, type, idx+1); + return SQL_ERROR; + } } break; case SQL_C_SBIGINT: { int64_t t = *(int64_t*)paramValue; @@ -2152,23 +2171,24 @@ static SQLRETURN doSQLDescribeCol(SQLHSTMT StatementHandle, if (ColumnSize) { *ColumnSize = field->bytes; } + if (DecimalDigits) *DecimalDigits = 0; if (DataType) { switch (field->type) { case TSDB_DATA_TYPE_BOOL: { - *DataType = SQL_C_TINYINT; + *DataType = SQL_TINYINT; } break; case TSDB_DATA_TYPE_TINYINT: { - *DataType = SQL_C_TINYINT; + *DataType = SQL_TINYINT; } break; case TSDB_DATA_TYPE_SMALLINT: { - *DataType = SQL_C_SHORT; + *DataType = SQL_SMALLINT; } break; case TSDB_DATA_TYPE_INT: { - *DataType = SQL_C_LONG; + *DataType = SQL_INTEGER; } break; case TSDB_DATA_TYPE_BIGINT: { @@ -2176,24 +2196,29 @@ static SQLRETURN doSQLDescribeCol(SQLHSTMT StatementHandle, } break; case TSDB_DATA_TYPE_FLOAT: { - *DataType = SQL_C_FLOAT; + *DataType = SQL_FLOAT; } break; case TSDB_DATA_TYPE_DOUBLE: { - *DataType = SQL_C_DOUBLE; + *DataType = SQL_DOUBLE; } break; case TSDB_DATA_TYPE_TIMESTAMP: { - *DataType = SQL_C_TIMESTAMP; + // *DataType = SQL_TIMESTAMP; + // *ColumnSize = 30; + // *DecimalDigits = 3; + *DataType = SQL_TIMESTAMP; + *ColumnSize = sizeof(SQL_TIMESTAMP_STRUCT); + *DecimalDigits = 0; } break; case TSDB_DATA_TYPE_NCHAR: { - *DataType = SQL_C_CHAR; // unicode ? + *DataType = SQL_CHAR; // unicode ? if (ColumnSize) *ColumnSize -= VARSTR_HEADER_SIZE; } break; case TSDB_DATA_TYPE_BINARY: { - *DataType = SQL_C_BINARY; + *DataType = SQL_BINARY; if (ColumnSize) *ColumnSize -= VARSTR_HEADER_SIZE; } break; @@ -2204,13 +2229,6 @@ static SQLRETURN doSQLDescribeCol(SQLHSTMT StatementHandle, break; } } - if (DecimalDigits) { - if (field->type == TSDB_DATA_TYPE_TIMESTAMP) { - *DecimalDigits = 3; - } else { - *DecimalDigits = 0; - } - } if (Nullable) { *Nullable = SQL_NULLABLE_UNKNOWN; } @@ -2340,10 +2358,6 @@ SQLRETURN SQL_API SQLSetStmtAttr(SQLHSTMT StatementHandle, - - - - static void init_routine(void) { if (0) { string_conv(NULL, NULL, NULL, 0, NULL, 0, NULL, NULL); @@ -2763,7 +2777,7 @@ static SQLRETURN conv_tsdb_f8_to_c_binary(sql_t *sql, c_target_t *target, TAOS_F } -static SQLRETURN conv_tsdb_ts_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts) +static SQLRETURN conv_tsdb_ts_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIELD *field, SQL_TIMESTAMP_STRUCT *ts) { struct tm tm = {0}; tm.tm_sec = ts->second; @@ -2776,12 +2790,12 @@ static SQLRETURN conv_tsdb_ts_to_c_v8(sql_t *sql, c_target_t *target, TAOS_FIELD DASSERT(sizeof(t) == sizeof(int64_t)); int64_t v = (int64_t)t; v *= 1000; - v += ts->fraction % 1000; + v += ts->fraction / 1000000; memcpy(target->ptr, &v, sizeof(v)); return SQL_SUCCESS; } -static SQLRETURN conv_tsdb_ts_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts) +static SQLRETURN conv_tsdb_ts_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIELD *field, SQL_TIMESTAMP_STRUCT *ts) { struct tm tm = {0}; tm.tm_sec = ts->second; @@ -2797,7 +2811,10 @@ static SQLRETURN conv_tsdb_ts_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIEL *target->soi = n; - snprintf(target->ptr, target->len, "%s.%03d", buf, ts->fraction); + unsigned int fraction = ts->fraction; + fraction /= 1000000; + snprintf(target->ptr, target->len, "%s.%03d", buf, fraction); + if (target->soi) *target->soi = strlen((const char*)target->ptr); if (n <= target->len) { return SQL_SUCCESS; @@ -2807,7 +2824,7 @@ static SQLRETURN conv_tsdb_ts_to_c_str(sql_t *sql, c_target_t *target, TAOS_FIEL return SQL_SUCCESS_WITH_INFO; } -static SQLRETURN conv_tsdb_ts_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts) +static SQLRETURN conv_tsdb_ts_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIELD *field, SQL_TIMESTAMP_STRUCT *ts) { struct tm tm = {0}; tm.tm_sec = ts->second; @@ -2821,9 +2838,10 @@ static SQLRETURN conv_tsdb_ts_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIEL int n = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm); DASSERT(n < sizeof(buf)); - *target->soi = n; - - memcpy(target->ptr, buf, (n>target->len ? target->len : n)); + unsigned int fraction = ts->fraction; + fraction /= 1000000; + snprintf(target->ptr, target->len, "%s.%03d", buf, fraction); + if (target->soi) *target->soi = strlen((const char*)target->ptr); if (n <= target->len) { return SQL_SUCCESS; @@ -2833,9 +2851,11 @@ static SQLRETURN conv_tsdb_ts_to_c_bin(sql_t *sql, c_target_t *target, TAOS_FIEL return SQL_SUCCESS_WITH_INFO; } -static SQLRETURN conv_tsdb_ts_to_c_ts(sql_t *sql, c_target_t *target, TAOS_FIELD *field, TIMESTAMP_STRUCT *ts) +static SQLRETURN conv_tsdb_ts_to_c_ts(sql_t *sql, c_target_t *target, TAOS_FIELD *field, SQL_TIMESTAMP_STRUCT *ts) { + DASSERT(target->len == sizeof(*ts)); memcpy(target->ptr, ts, sizeof(*ts)); + *target->soi = target->len; return SQL_SUCCESS; } @@ -3107,21 +3127,21 @@ const char* tsdb_int64_to_bit(int64_t src, int8_t *dst) const char* tsdb_int64_to_tinyint(int64_t src, int8_t *dst) { *dst = src; - if (src>=SCHAR_MIN || src<=SCHAR_MAX) return SQL_SUCCESS; + if (src>=SCHAR_MIN && src<=SCHAR_MAX) return NULL; return "22003"; } const char* tsdb_int64_to_smallint(int64_t src, int16_t *dst) { *dst = src; - if (src>=SHRT_MIN || src<=SHRT_MAX) return SQL_SUCCESS; + if (src>=SHRT_MIN && src<=SHRT_MAX) return NULL; return "22003"; } const char* tsdb_int64_to_int(int64_t src, int32_t *dst) { *dst = src; - if (src>=LONG_MIN || src<=LONG_MAX) return SQL_SUCCESS; + if (src>=LONG_MIN && src<=LONG_MAX) return NULL; return "22003"; } diff --git a/src/connector/odbc/tests/odbc.py b/src/connector/odbc/tests/odbc.py index 02ac975776..d2de8f39c6 100644 --- a/src/connector/odbc/tests/odbc.py +++ b/src/connector/odbc/tests/odbc.py @@ -43,13 +43,10 @@ cursor = cnxn.cursor() cursor.execute("insert into db.t values('2020-10-13 06:44:00', 1, 127, 32767, 32768, 32769, 123.456, 789.987, 'hello', 'world')") cursor.close() -print("hhhhhhhhhhhhhhhhhhhhhh") - cursor = cnxn.cursor() -cursor.execute("insert into db.t values(?,?,?,?,?,?,?,?,?,?)", "2020-10-13 07:06:00", 0, 229, 32767, 32768, 32769, 123.456, 789.987, "hel后lo", "wo哈rld"); +cursor.execute("insert into db.t values(?,?,?,?,?,?,?,?,?,?)", "2020-10-13 07:06:00", 0, 127, 32767, 32768, 32769, 123.456, 789.987, "hel后lo", "wo哈rld"); cursor.close() -print("hhhhhhhhhhhhhhhhhhhhhh") cursor = cnxn.cursor() cursor.execute("SELECT * from db.t") row = cursor.fetchone() @@ -91,7 +88,7 @@ params = [ ('A', 1), ('B', 2), ('C', 3) ] params = [ ('2020-10-16 00:00:00', 1), ('2020-10-16 00:00:01', 4), ('2020-10-16 00:00:02', 5), - ('2020-10-16 00:00:03', 6) ] + ('2020-10-16 00:00:03.009', 6) ] cursor = cnxn.cursor() cursor.fast_executemany = True cursor.executemany("insert into db.v values (?, ?)", params) From 196106ade51a6078c5217995fbab8d031bf22dbc Mon Sep 17 00:00:00 2001 From: freemine Date: Sun, 18 Oct 2020 20:26:14 +0800 Subject: [PATCH 035/101] reenable jdbc --- src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b2c6e70f68..f619edd221 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,5 +21,5 @@ ADD_SUBDIRECTORY(wal) ADD_SUBDIRECTORY(cq) ADD_SUBDIRECTORY(dnode) ADD_SUBDIRECTORY(connector/odbc) -#ADD_SUBDIRECTORY(connector/jdbc) +ADD_SUBDIRECTORY(connector/jdbc) From 40c7208cc2ab92d7c45a999764730d49902f97a5 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 19 Oct 2020 09:53:20 +0800 Subject: [PATCH 036/101] [td-225] --- src/query/src/qExecutor.c | 1 + src/vnode/src/vnodeRead.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index b9a5295ed2..f6c2ce29d6 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -6229,6 +6229,7 @@ static SQInfo *createQInfoImpl(SQueryTableMsg *pQueryMsg, SSqlGroupbyExpr *pGrou pQInfo->dataReady = QUERY_RESULT_NOT_READY; pQInfo->rspContext = NULL; pthread_mutex_init(&pQInfo->lock, NULL); + tsem_init(&pQInfo->ready, 0, 0); pQuery->pos = -1; pQuery->window = pQueryMsg->window; diff --git a/src/vnode/src/vnodeRead.c b/src/vnode/src/vnodeRead.c index ede5921dc1..9c3fc1c7da 100644 --- a/src/vnode/src/vnodeRead.c +++ b/src/vnode/src/vnodeRead.c @@ -272,6 +272,11 @@ static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { if (pRetrieve->free == 1) { vWarn("vgId:%d, QInfo:%p, retrieve msg received to kill query and free qhandle", pVnode->vgId, *handle); qKillQuery(*handle); +#if !(_NON_BLOCKING_RETRIEVE) + void** p = handle; + qReleaseQInfo(pVnode->qMgmt, (void **)&p, false); +#endif + qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); vnodeBuildNoResultQueryRsp(pRet); @@ -285,6 +290,12 @@ static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { pReadMsg->rpcMsg.handle); code = TSDB_CODE_RPC_NETWORK_UNAVAIL; qKillQuery(*handle); + +#if !(_NON_BLOCKING_RETRIEVE) + void** p = handle; + qReleaseQInfo(pVnode->qMgmt, (void **)&p, false); +#endif + qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); return code; } @@ -314,6 +325,11 @@ static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { code = vnodeDumpQueryResult(pRet, pVnode, handle, &freeHandle, pReadMsg->rpcMsg.ahandle); } +#if !(_NON_BLOCKING_RETRIEVE) + void** p = handle; + qReleaseQInfo(pVnode->qMgmt, (void **)&p, false); +#endif + // If qhandle is not added into vread queue, the query should be completed already or paused with error. // Here free qhandle immediately if (freeHandle) { From 3abaaabeee3b081b0ce8253465e90e0a9a3933d0 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 19 Oct 2020 11:33:49 +0800 Subject: [PATCH 037/101] [td-225] --- src/vnode/src/vnodeRead.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/vnode/src/vnodeRead.c b/src/vnode/src/vnodeRead.c index 9c3fc1c7da..a86d83cf2d 100644 --- a/src/vnode/src/vnodeRead.c +++ b/src/vnode/src/vnodeRead.c @@ -240,6 +240,7 @@ static int32_t vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { } #else qTableQuery(*qhandle); // do execute query + qReleaseQInfo(pVnode->qMgmt, (void **)&qhandle, false); #endif } @@ -272,11 +273,6 @@ static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { if (pRetrieve->free == 1) { vWarn("vgId:%d, QInfo:%p, retrieve msg received to kill query and free qhandle", pVnode->vgId, *handle); qKillQuery(*handle); -#if !(_NON_BLOCKING_RETRIEVE) - void** p = handle; - qReleaseQInfo(pVnode->qMgmt, (void **)&p, false); -#endif - qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); vnodeBuildNoResultQueryRsp(pRet); @@ -290,12 +286,6 @@ static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { pReadMsg->rpcMsg.handle); code = TSDB_CODE_RPC_NETWORK_UNAVAIL; qKillQuery(*handle); - -#if !(_NON_BLOCKING_RETRIEVE) - void** p = handle; - qReleaseQInfo(pVnode->qMgmt, (void **)&p, false); -#endif - qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); return code; } @@ -325,11 +315,6 @@ static int32_t vnodeProcessFetchMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { code = vnodeDumpQueryResult(pRet, pVnode, handle, &freeHandle, pReadMsg->rpcMsg.ahandle); } -#if !(_NON_BLOCKING_RETRIEVE) - void** p = handle; - qReleaseQInfo(pVnode->qMgmt, (void **)&p, false); -#endif - // If qhandle is not added into vread queue, the query should be completed already or paused with error. // Here free qhandle immediately if (freeHandle) { From fa7b5f047cbacd1eda23bb55964bfa79c87cb65c Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Mon, 19 Oct 2020 16:15:54 +0800 Subject: [PATCH 038/101] add python connectionerror --- src/connector/python/linux/python3/taos/cinterface.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/connector/python/linux/python3/taos/cinterface.py b/src/connector/python/linux/python3/taos/cinterface.py index c9d0551af5..7b8e9b0897 100644 --- a/src/connector/python/linux/python3/taos/cinterface.py +++ b/src/connector/python/linux/python3/taos/cinterface.py @@ -225,6 +225,7 @@ class CTaosInterface(object): if connection.value == None: print('connect to TDengine failed') + raise ConnectionError("connect to TDengine failed") # sys.exit(1) #else: # print('connect to TDengine success') From fb0456ca4cd88f5a08a97acd3242ada5d89080b0 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 19 Oct 2020 16:26:28 +0800 Subject: [PATCH 039/101] [td-1747] reduce tableMeta size --- src/client/inc/tsclient.h | 8 ++++---- src/client/src/tscSchemaUtil.c | 15 +++++++++++++-- src/client/src/tscServer.c | 18 ++++++++++++------ src/inc/taosmsg.h | 27 ++++++++++++++++++++++----- src/mnode/src/mnodeTable.c | 24 ++++++++++++------------ 5 files changed, 63 insertions(+), 29 deletions(-) diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index 8621f9d28b..f0d3e26a49 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -90,10 +90,10 @@ typedef struct STableComInfo { } STableComInfo; typedef struct SCMCorVgroupInfo { - int32_t version; - int8_t inUse; - int8_t numOfEps; - SEpAddr epAddr[TSDB_MAX_REPLICA]; + int32_t version; + int8_t inUse; + int8_t numOfEps; + SEpAddr1 epAddr[TSDB_MAX_REPLICA]; } SCMCorVgroupInfo; typedef struct STableMeta { diff --git a/src/client/src/tscSchemaUtil.c b/src/client/src/tscSchemaUtil.c index 1e841c68fd..fb9130b6c5 100644 --- a/src/client/src/tscSchemaUtil.c +++ b/src/client/src/tscSchemaUtil.c @@ -145,10 +145,11 @@ static void tscInitCorVgroupInfo(SCMCorVgroupInfo *corVgroupInfo, SCMVgroupInfo corVgroupInfo->inUse = 0; corVgroupInfo->numOfEps = vgroupInfo->numOfEps; for (int32_t i = 0; i < corVgroupInfo->numOfEps; i++) { - strncpy(corVgroupInfo->epAddr[i].fqdn, vgroupInfo->epAddr[i].fqdn, TSDB_FQDN_LEN); + corVgroupInfo->epAddr[i].fqdn = strdup(vgroupInfo->epAddr[i].fqdn); corVgroupInfo->epAddr[i].port = vgroupInfo->epAddr[i].port; } } + STableMeta* tscCreateTableMetaFromMsg(STableMetaMsg* pTableMetaMsg, size_t* size) { assert(pTableMetaMsg != NULL); @@ -164,7 +165,17 @@ STableMeta* tscCreateTableMetaFromMsg(STableMetaMsg* pTableMetaMsg, size_t* size pTableMeta->id.tid = pTableMetaMsg->sid; pTableMeta->id.uid = pTableMetaMsg->uid; - pTableMeta->vgroupInfo = pTableMetaMsg->vgroup; + + SCMVgroupInfo* pVgroupInfo = &pTableMeta->vgroupInfo; + pVgroupInfo->numOfEps = pTableMetaMsg->vgroup.numOfEps; + pVgroupInfo->vgId = pTableMetaMsg->vgroup.vgId; + + for(int32_t i = 0; i < pVgroupInfo->numOfEps; ++i) { + SEpAddrMsg* pEpMsg = &pTableMetaMsg->vgroup.epAddr[i]; + + pVgroupInfo->epAddr[i].fqdn = strndup(pEpMsg->fqdn, tListLen(pEpMsg->fqdn)); + pVgroupInfo->epAddr[i].port = pEpMsg->port; + } tscInitCorVgroupInfo(&pTableMeta->corVgroupInfo, &pTableMeta->vgroupInfo); diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 494a8a9c30..bfd913dcf5 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1839,22 +1839,28 @@ int tscProcessSTableVgroupRsp(SSqlObj *pSql) { SSqlCmd* pCmd = &parent->cmd; for(int32_t i = 0; i < pStableVgroup->numOfTables; ++i) { STableMetaInfo *pInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, i); - SVgroupsInfo * pVgroupInfo = (SVgroupsInfo *)pMsg; - pVgroupInfo->numOfVgroups = htonl(pVgroupInfo->numOfVgroups); - size_t size = sizeof(SCMVgroupInfo) * pVgroupInfo->numOfVgroups + sizeof(SVgroupsInfo); + SVgroupsMsg * pVgroupMsg = (SVgroupsMsg *) pMsg; + pVgroupMsg->numOfVgroups = htonl(pVgroupMsg->numOfVgroups); + + size_t size = sizeof(SCMVgroupInfo) * pVgroupMsg->numOfVgroups + sizeof(SVgroupsInfo); pInfo->vgroupList = calloc(1, size); assert(pInfo->vgroupList != NULL); - memcpy(pInfo->vgroupList, pVgroupInfo, size); + pInfo->vgroupList->numOfVgroups = pVgroupMsg->numOfVgroups; for (int32_t j = 0; j < pInfo->vgroupList->numOfVgroups; ++j) { //just init, no need to lock SCMVgroupInfo *pVgroups = &pInfo->vgroupList->vgroups[j]; - pVgroups->vgId = htonl(pVgroups->vgId); + + SCMVgroupMsg *vgroupMsg = &pVgroupMsg->vgroups[j]; + pVgroups->vgId = htonl(vgroupMsg->vgId); + pVgroups->numOfEps = vgroupMsg->numOfEps; + assert(pVgroups->numOfEps >= 1); for (int32_t k = 0; k < pVgroups->numOfEps; ++k) { - pVgroups->epAddr[k].port = htons(pVgroups->epAddr[k].port); + pVgroups->epAddr[k].port = htons(vgroupMsg->epAddr[k].port); + pVgroups->epAddr[k].fqdn = strndup(vgroupMsg->epAddr[k].fqdn, tListLen(vgroupMsg->epAddr[k].fqdn)); } } diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index be66c76d71..635e2aba80 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -182,10 +182,16 @@ extern char *taosMsg[]; #pragma pack(push, 1) +// null-terminated string instead of char array to avoid too many memory consumption in case of more than 1M tableMeta typedef struct { char fqdn[TSDB_FQDN_LEN]; uint16_t port; -} SEpAddr; +} SEpAddrMsg; + +typedef struct { + char* fqdn; + uint16_t port; +} SEpAddr1; typedef struct { int32_t numOfVnodes; @@ -662,16 +668,27 @@ typedef struct SCMSTableVgroupMsg { } SCMSTableVgroupMsg, SCMSTableVgroupRspMsg; typedef struct { - int32_t vgId; - int8_t numOfEps; - SEpAddr epAddr[TSDB_MAX_REPLICA]; + int32_t vgId; + int8_t numOfEps; + SEpAddr1 epAddr[TSDB_MAX_REPLICA]; } SCMVgroupInfo; +typedef struct { + int32_t vgId; + int8_t numOfEps; + SEpAddrMsg epAddr[TSDB_MAX_REPLICA]; +} SCMVgroupMsg; + typedef struct { int32_t numOfVgroups; SCMVgroupInfo vgroups[]; } SVgroupsInfo; +typedef struct { + int32_t numOfVgroups; + SCMVgroupMsg vgroups[]; +} SVgroupsMsg; + typedef struct STableMetaMsg { int32_t contLen; char tableId[TSDB_TABLE_FNAME_LEN]; // table id @@ -684,7 +701,7 @@ typedef struct STableMetaMsg { int16_t tversion; int32_t sid; uint64_t uid; - SCMVgroupInfo vgroup; + SCMVgroupMsg vgroup; SSchema schema[]; } STableMetaMsg; diff --git a/src/mnode/src/mnodeTable.c b/src/mnode/src/mnodeTable.c index 1a1ff878a5..9965047a99 100644 --- a/src/mnode/src/mnodeTable.c +++ b/src/mnode/src/mnodeTable.c @@ -1472,12 +1472,12 @@ static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) { int32_t numOfTable = htonl(pInfo->numOfTables); // reserve space - int32_t contLen = sizeof(SCMSTableVgroupRspMsg) + 32 * sizeof(SCMVgroupInfo) + sizeof(SVgroupsInfo); + int32_t contLen = sizeof(SCMSTableVgroupRspMsg) + 32 * sizeof(SCMVgroupInfo) + sizeof(SVgroupsMsg); for (int32_t i = 0; i < numOfTable; ++i) { char *stableName = (char*)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_FNAME_LEN) * i; SSuperTableObj *pTable = mnodeGetSuperTable(stableName); if (pTable != NULL && pTable->vgHash != NULL) { - contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SCMVgroupInfo) + sizeof(SVgroupsInfo)); + contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SCMVgroupInfo) + sizeof(SVgroupsMsg)); } mnodeDecTableRef(pTable); } @@ -1506,12 +1506,12 @@ static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) { // even this super table has no corresponding table, still return pRsp->numOfTables++; - SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg; - pVgroupInfo->numOfVgroups = 0; + SVgroupsMsg *pVgroupMsg = (SVgroupsMsg *)msg; + pVgroupMsg->numOfVgroups = 0; - msg += sizeof(SVgroupsInfo); + msg += sizeof(SVgroupsMsg); } else { - SVgroupsInfo *pVgroupInfo = (SVgroupsInfo *)msg; + SVgroupsMsg *pVgroupMsg = (SVgroupsMsg *)msg; SHashMutableIterator *pIter = taosHashCreateIter(pTable->vgHash); int32_t vgSize = 0; @@ -1520,15 +1520,15 @@ static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) { SVgObj * pVgroup = mnodeGetVgroup(*pVgId); if (pVgroup == NULL) continue; - pVgroupInfo->vgroups[vgSize].vgId = htonl(pVgroup->vgId); + pVgroupMsg->vgroups[vgSize].vgId = htonl(pVgroup->vgId); for (int32_t vn = 0; vn < pVgroup->numOfVnodes; ++vn) { SDnodeObj *pDnode = pVgroup->vnodeGid[vn].pDnode; if (pDnode == NULL) break; - tstrncpy(pVgroupInfo->vgroups[vgSize].epAddr[vn].fqdn, pDnode->dnodeFqdn, TSDB_FQDN_LEN); - pVgroupInfo->vgroups[vgSize].epAddr[vn].port = htons(pDnode->dnodePort); + tstrncpy(pVgroupMsg->vgroups[vgSize].epAddr[vn].fqdn, pDnode->dnodeFqdn, TSDB_FQDN_LEN); + pVgroupMsg->vgroups[vgSize].epAddr[vn].port = htons(pDnode->dnodePort); - pVgroupInfo->vgroups[vgSize].numOfEps++; + pVgroupMsg->vgroups[vgSize].numOfEps++; } vgSize++; @@ -1538,10 +1538,10 @@ static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) { taosHashDestroyIter(pIter); mnodeDecTableRef(pTable); - pVgroupInfo->numOfVgroups = htonl(vgSize); + pVgroupMsg->numOfVgroups = htonl(vgSize); // one table is done, try the next table - msg += sizeof(SVgroupsInfo) + vgSize * sizeof(SCMVgroupInfo); + msg += sizeof(SVgroupsMsg) + vgSize * sizeof(SCMVgroupMsg); pRsp->numOfTables++; } } From cc74bdd8f8fe0da0dd6440b5b123fe2a9a024f21 Mon Sep 17 00:00:00 2001 From: Hui Li Date: Mon, 19 Oct 2020 16:39:46 +0800 Subject: [PATCH 040/101] [TD-1639] --- tests/gotest/batchtest.bat | 17 +++++++++ tests/gotest/batchtest.sh | 19 ++++++++-- tests/gotest/case001/case001.bat | 9 +++++ tests/gotest/case001/case001.go | 35 +++++++++++++++++- tests/gotest/case001/case001.sh | 63 ++++---------------------------- 5 files changed, 83 insertions(+), 60 deletions(-) create mode 100644 tests/gotest/batchtest.bat create mode 100644 tests/gotest/case001/case001.bat diff --git a/tests/gotest/batchtest.bat b/tests/gotest/batchtest.bat new file mode 100644 index 0000000000..abe9a58f31 --- /dev/null +++ b/tests/gotest/batchtest.bat @@ -0,0 +1,17 @@ +@echo off +echo ==== start Go connector test cases test ==== +cd /d %~dp0 + +set severIp=%1 +set serverPort=%2 +if "%severIp%"=="" (set severIp=127.0.0.1) +if "%serverPort%"=="" (set serverPort=6030) + +cd case001 +case001.bat %severIp% %serverPort% + +rem cd case002 +rem case002.bat + +:: cd case002 +:: case002.bat diff --git a/tests/gotest/batchtest.sh b/tests/gotest/batchtest.sh index a027dd0d7c..e8ed9ecbed 100644 --- a/tests/gotest/batchtest.sh +++ b/tests/gotest/batchtest.sh @@ -1,5 +1,18 @@ #!/bin/bash -bash ./case001/case001.sh -#bash ./case002/case002.sh -#bash ./case003/case003.sh +echo "==== start Go connector test cases test ====" + +severIp=$1 +serverPort=$2 + +if [ ! -n "$severIp" ]; then + severIp=127.0.0.1 +fi + +if [ ! -n "$serverPort" ]; then + serverPort=6030 +fi + +bash ./case001/case001.sh $severIp $serverPort +#bash ./case002/case002.sh $severIp $serverPort +#bash ./case003/case003.sh $severIp $serverPort diff --git a/tests/gotest/case001/case001.bat b/tests/gotest/case001/case001.bat new file mode 100644 index 0000000000..ebec576e72 --- /dev/null +++ b/tests/gotest/case001/case001.bat @@ -0,0 +1,9 @@ +@echo off +echo ==== start run cases001.go + +del go.* +go mod init demotest +go build +demotest.exe -h %1 -p %2 +cd .. + diff --git a/tests/gotest/case001/case001.go b/tests/gotest/case001/case001.go index 1d5ede6d21..fb94f566dd 100644 --- a/tests/gotest/case001/case001.go +++ b/tests/gotest/case001/case001.go @@ -16,20 +16,53 @@ package main import ( "database/sql" + "flag" "fmt" _ "github.com/taosdata/driver-go/taosSql" "log" + "strconv" "time" ) +type config struct { + hostName string + serverPort int + user string + password string +} + +var configPara config +var url string + +func init() { + flag.StringVar(&configPara.hostName, "h", "127.0.0.1","The host to connect to TDengine server.") + flag.IntVar(&configPara.serverPort, "p", 6030, "The TCP/IP port number to use for the connection to TDengine server.") + flag.StringVar(&configPara.user, "u", "root", "The TDengine user name to use when connecting to the server.") + flag.StringVar(&configPara.password, "P", "taosdata", "The password to use when connecting to the server.") + + flag.Parse() +} + +func printAllArgs() { + fmt.Printf("\n============= args parse result: =============\n") + fmt.Printf("hostName: %v\n", configPara.hostName) + fmt.Printf("serverPort: %v\n", configPara.serverPort) + fmt.Printf("usr: %v\n", configPara.user) + fmt.Printf("password: %v\n", configPara.password) + fmt.Printf("================================================\n") +} + func main() { + printAllArgs() taosDriverName := "taosSql" demodb := "demodb" demot := "demot" fmt.Printf("\n======== start demo test ========\n") + + url = "root:taosdata@/tcp(" + configPara.hostName + ":" + strconv.Itoa(configPara.serverPort) + ")/" // open connect to taos server - db, err := sql.Open(taosDriverName, "root:taosdata@/tcp(192.168.1.217:7100)/") + db, err := sql.Open(taosDriverName, url) if err != nil { log.Fatalf("Open database error: %s\n", err) } diff --git a/tests/gotest/case001/case001.sh b/tests/gotest/case001/case001.sh index 5a9034c4d1..831e9f83ac 100644 --- a/tests/gotest/case001/case001.sh +++ b/tests/gotest/case001/case001.sh @@ -1,10 +1,6 @@ #!/bin/bash -################################################## -# -# Do go test -# -################################################## +echo "==== start run cases001.go" set +e #set -x @@ -12,59 +8,14 @@ set +e script_dir="$(dirname $(readlink -f $0))" #echo "pwd: $script_dir, para0: $0" -execName=$0 -execName=`echo ${execName##*/}` -goName=`echo ${execName%.*}` - -###### step 1: start one taosd -scriptDir=$script_dir/../../script/sh -bash $scriptDir/stop_dnodes.sh -bash $scriptDir/deploy.sh -n dnode1 -i 1 -bash $scriptDir/cfg.sh -n dnode1 -c walLevel -v 0 -bash $scriptDir/exec.sh -n dnode1 -s start - -###### step 2: set config item -TAOS_CFG=/etc/taos/taos.cfg -HOSTNAME=`hostname -f` - -if [ ! -f ${TAOS_CFG} ]; then - touch -f $TAOS_CFG -fi - -echo " " > $TAOS_CFG -echo "firstEp ${HOSTNAME}:7100" >> $TAOS_CFG -echo "secondEp ${HOSTNAME}:7200" >> $TAOS_CFG -echo "serverPort 7100" >> $TAOS_CFG -#echo "dataDir $DATA_DIR" >> $TAOS_CFG -#echo "logDir $LOG_DIR" >> $TAOS_CFG -#echo "scriptDir ${CODE_DIR}/../script" >> $TAOS_CFG -echo "numOfLogLines 100000000" >> $TAOS_CFG -echo "dDebugFlag 135" >> $TAOS_CFG -echo "mDebugFlag 135" >> $TAOS_CFG -echo "sdbDebugFlag 135" >> $TAOS_CFG -echo "rpcDebugFlag 135" >> $TAOS_CFG -echo "tmrDebugFlag 131" >> $TAOS_CFG -echo "cDebugFlag 135" >> $TAOS_CFG -echo "httpDebugFlag 135" >> $TAOS_CFG -echo "monitorDebugFlag 135" >> $TAOS_CFG -echo "udebugFlag 135" >> $TAOS_CFG -echo "tablemetakeeptimer 5" >> $TAOS_CFG -echo "wal 0" >> $TAOS_CFG -echo "asyncLog 0" >> $TAOS_CFG -echo "locale en_US.UTF-8" >> $TAOS_CFG -echo "enableCoreFile 1" >> $TAOS_CFG -echo " " >> $TAOS_CFG - -ulimit -n 600000 -ulimit -c unlimited -# -##sudo sysctl -w kernel.core_pattern=$TOP_DIR/core.%p.%e -# +#execName=$0 +#execName=`echo ${execName##*/}` +#goName=`echo ${execName%.*}` ###### step 3: start build cd $script_dir rm -f go.* -go mod init $goName +go mod init demotest go build -sleep 1s -sudo ./$goName +sleep 1s +./demotest -h $1 -p $2 From de2b0de6ee4a57ec6bef316b559a0be9b560a709 Mon Sep 17 00:00:00 2001 From: Hui Li Date: Mon, 19 Oct 2020 16:46:41 +0800 Subject: [PATCH 041/101] [NONE] --- src/connector/python/linux/python3/taos/cinterface.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/connector/python/linux/python3/taos/cinterface.py b/src/connector/python/linux/python3/taos/cinterface.py index 7b8e9b0897..409b3ba02b 100644 --- a/src/connector/python/linux/python3/taos/cinterface.py +++ b/src/connector/python/linux/python3/taos/cinterface.py @@ -225,7 +225,6 @@ class CTaosInterface(object): if connection.value == None: print('connect to TDengine failed') - raise ConnectionError("connect to TDengine failed") # sys.exit(1) #else: # print('connect to TDengine success') @@ -415,4 +414,4 @@ if __name__ == '__main__': print(data) cinter.freeResult(result) - cinter.close(conn) \ No newline at end of file + cinter.close(conn) From 7c6609e6c23dbca3156fa0a6b1e7fc3534ae1e80 Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Mon, 19 Oct 2020 16:47:05 +0800 Subject: [PATCH 042/101] [TD-1750] add python2 connectionerror --- src/connector/python/linux/python2/taos/cinterface.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/connector/python/linux/python2/taos/cinterface.py b/src/connector/python/linux/python2/taos/cinterface.py index c26e5c0967..32859f6b34 100644 --- a/src/connector/python/linux/python2/taos/cinterface.py +++ b/src/connector/python/linux/python2/taos/cinterface.py @@ -225,6 +225,7 @@ class CTaosInterface(object): if connection.value == None: print('connect to TDengine failed') + raise ConnectionError("connect to TDengine failed") # sys.exit(1) else: print('connect to TDengine success') From bfd137e73bd87f87dec266aca4dbb5dfa058b6a2 Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Mon, 19 Oct 2020 16:50:22 +0800 Subject: [PATCH 043/101] [TD-1744] PythonChecker --- .../PYTHONConnectorChecker/PythonChecker.py | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tests/examples/python/PYTHONConnectorChecker/PythonChecker.py diff --git a/tests/examples/python/PYTHONConnectorChecker/PythonChecker.py b/tests/examples/python/PYTHONConnectorChecker/PythonChecker.py new file mode 100644 index 0000000000..d8f23d8654 --- /dev/null +++ b/tests/examples/python/PYTHONConnectorChecker/PythonChecker.py @@ -0,0 +1,114 @@ +import taos +import time +import sys +import getopt +class ConnectorChecker: + def init(self): + self.host = "127.0.0.1" + self.dbName = "test" + self.tbName = "weather" + self.user = "root" + self.password = "taosdata" + + + def sethdt(self,FQDN,dbname,tbname): + if(FQDN): + self.host=FQDN + if(dbname): + self.dbname=dbname + if(tbname): + self.tbName + def printSql(self,sql,elapsed): + print("[ "+"OK"+" ]"+" time cost: %s ms, execute statement ====> %s" + %(elapsed,sql)) + def executeQuery(self,sql): + try: + start=time.time() + execute = self.cl.execute(sql) + elapsed = (time.time()-start)*1000 + self.printSql(sql,elapsed) + data = self.cl.fetchall() + numOfRows = self.cl.rowcount + numOfCols = len(self.cl.description) + for irow in range(numOfRows): + print("Row%d: ts=%s, temperature=%d, humidity=%f" %(irow, data[irow][0], data[irow][1],data[irow][2])) + except Exception as e: + print("Failure sql: %s,exception: %s" %sql,str(e)) + def execute(self,sql): + try: + start=time.time() + execute = self.cl.execute(sql) + elapsed = (time.time()-start)*1000 + self.printSql(sql,elapsed) + + except Exception as e: + print("Failure sql: %s,exception: %s" % + sql,str(e)) + def close(self): + self.cl.close() + self.conn.close() + def createDatabase(self): + sql="create database if not exists %s" % self.dbName + self.execute(sql) + def useDatabase(self): + sql="use %s" % self.dbName + self.execute(sql) + def createTable(self): + sql="create table if not exists %s.%s (ts timestamp, temperature float, humidity int)"%(self.dbName,self.tbName) + self.execute(sql) + def checkDropTable(self): + sql="drop table if exists " + self.dbName + "." + self.tbName + "" + self.execute(sql) + def checkInsert(self): + sql="insert into test.weather (ts, temperature, humidity) values(now, 20.5, 34)" + self.execute(sql) + def checkSelect(self): + sql = "select * from test.weather" + self.executeQuery(sql) + def srun(self): + print(self.host) + try: + self.conn = taos.connect(host=self.host,user=self.user,password=self.password) + #self.conn = taos.connect(self.host,self.user,self.password) + except Exception as e: + print("connection failed: %s"%self.host) + exit(1) + print("[ OK ] Connection established.") + self.cl = self.conn.cursor() + +def main(argv): + FQDN='' + dbname='' + tbname='' + try: + opts, args = getopt.getopt(argv,"h:d:t:",["FQDN=","ifile=","ofile="]) + except getopt.GetoptError: + print ('PYTHONConnectorChecker.py -h ') + sys.exit(2) + for opt, arg in opts: + if opt in ("-h", "--FQDN"): + FQDN=arg + elif opt in ("-d", "--dbname"): + dbname = arg + elif opt in ("-t", "--tbname"): + tbname = arg + + checker = ConnectorChecker() + checker.init() + checker.sethdt(FQDN,dbname,tbname) + checker.srun() + checker.createDatabase() + checker.useDatabase() + checker.checkDropTable() + checker.createTable() + checker.checkInsert() + checker.checkSelect() + checker.checkDropTable() + checker.close() + + + +if __name__ == "__main__": + main(sys.argv[1:]) + + From 69d8861faf09d21825ca792723760f93b5d24544 Mon Sep 17 00:00:00 2001 From: Hui Li Date: Mon, 19 Oct 2020 16:56:33 +0800 Subject: [PATCH 044/101] [TD-1750] --- src/connector/python/linux/python3/taos/cinterface.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/connector/python/linux/python3/taos/cinterface.py b/src/connector/python/linux/python3/taos/cinterface.py index 409b3ba02b..609154a3a4 100644 --- a/src/connector/python/linux/python3/taos/cinterface.py +++ b/src/connector/python/linux/python3/taos/cinterface.py @@ -225,6 +225,7 @@ class CTaosInterface(object): if connection.value == None: print('connect to TDengine failed') + raise ConnectionError("connect to TDengine failed") # sys.exit(1) #else: # print('connect to TDengine success') From 2cac39d66599050ea46016db23f451617d623b15 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 19 Oct 2020 18:20:33 +0800 Subject: [PATCH 045/101] [td-225] reduce tableMeta size --- src/client/src/tscServer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index bfd913dcf5..8a50299d51 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1856,7 +1856,7 @@ int tscProcessSTableVgroupRsp(SSqlObj *pSql) { pVgroups->vgId = htonl(vgroupMsg->vgId); pVgroups->numOfEps = vgroupMsg->numOfEps; - assert(pVgroups->numOfEps >= 1); + assert(pVgroups->numOfEps >= 1 && pVgroups->vgId >= 1); for (int32_t k = 0; k < pVgroups->numOfEps; ++k) { pVgroups->epAddr[k].port = htons(vgroupMsg->epAddr[k].port); From 0147efd19073dc84c2461a90b24f186e02b84c52 Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Mon, 19 Oct 2020 18:47:16 +0800 Subject: [PATCH 046/101] [TD-1677] add query test case --- .../example/JDBCConnectorChecker.java | 1 + tests/pytest/concurrent_inquiry.py | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JDBCConnectorChecker.java b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JDBCConnectorChecker.java index 74e586d7fd..a19f0a0093 100644 --- a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JDBCConnectorChecker.java +++ b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JDBCConnectorChecker.java @@ -120,6 +120,7 @@ public class JDBCConnectorChecker { printSql(sql, execute, (end - start)); } catch (SQLException e) { e.printStackTrace(); + } } diff --git a/tests/pytest/concurrent_inquiry.py b/tests/pytest/concurrent_inquiry.py index faefc8a1c2..2460900891 100644 --- a/tests/pytest/concurrent_inquiry.py +++ b/tests/pytest/concurrent_inquiry.py @@ -27,6 +27,7 @@ query_sql = [ "select count(*) from test.meters where t7 like 'fi%';", "select count(*) from test.meters where t7 like '_econd';", "select count(*) from test.meters interval(1n) order by ts desc;", +"select max(c0) from test.meters group by tbname", "select first(*) from test.meters;", "select last(*) from test.meters;", "select last_row(*) from test.meters;", @@ -56,6 +57,12 @@ query_sql = [ "select stddev(c6) from test.t1;", "select sum(c6) from test.meters;", "select top(c6, 2) from test.meters;", +#all vnode +"select count(*) from test.meters where t5 >2500 and t5<7500", +"select max(c0),avg(c1) from test.meters where t5 >2500 and t5<7500", +"select sum(c5),avg(c1) from test.meters where t5 >2500 and t5<7500", +"select max(c0),min(c6) from test.meters where t5 >2500 and t5<7500", +"select min(c0),avg(c6) from test.meters where t5 >2500 and t5<7500", # second supertable "select count(*) from test.meters1 where c1 > 50;", "select count(*) from test.meters1 where c2 >= 50 and c2 < 100;", @@ -65,6 +72,7 @@ query_sql = [ "select count(*) from test.meters1 where t7 like 'fi%';", "select count(*) from test.meters1 where t7 like '_econd';", "select count(*) from test.meters1 interval(1n) order by ts desc;", +"select max(c0) from test.meters1 group by tbname", "select first(*) from test.meters1;", "select last(*) from test.meters1;", "select last_row(*) from test.meters1;", @@ -93,7 +101,19 @@ query_sql = [ "select spread(c6) from test.m1 ;", "select stddev(c6) from test.m1;", "select sum(c6) from test.meters1;", -"select top(c6, 2) from test.meters1;" +"select top(c6, 2) from test.meters1;", +"select count(*) from test.meters1 where t5 >2500 and t5<7500", +#all vnode +"select count(*) from test.meters1 where t5 >2500 and t5<7500", +"select max(c0),avg(c1) from test.meters1 where t5 >2500 and t5<7500", +"select sum(c5),avg(c1) from test.meters1 where t5 >2500 and t5<7500", +"select max(c0),min(c6) from test.meters1 where t5 >2500 and t5<7500", +"select min(c0),avg(c6) from test.meters1 where t5 >2500 and t5<7500", +#join +"select * from meters,meters1 where meters.ts = meters1.ts and meters.t5 = meters1.t5", +"select * from meters,meters1 where meters.ts = meters1.ts and meters.t7 = meters1.t7", +"select * from meters,meters1 where meters.ts = meters1.ts and meters.t8 = meters1.t8", +"select meters.ts,meters1.c2 from meters,meters1 where meters.ts = meters1.ts and meters.t8 = meters1.t8" ] class ConcurrentInquiry: @@ -112,6 +132,7 @@ class ConcurrentInquiry: password, ) cl = conn.cursor() + cl.execute("use test;") print("Thread %d: starting" % threadID) From 3b3b7d257394dfe5f78b5195caecd54186d1e61c Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Mon, 19 Oct 2020 18:51:42 +0800 Subject: [PATCH 047/101] [TD-1677] add query test case --- tests/pytest/concurrent_inquiry.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/pytest/concurrent_inquiry.py b/tests/pytest/concurrent_inquiry.py index faefc8a1c2..2460900891 100644 --- a/tests/pytest/concurrent_inquiry.py +++ b/tests/pytest/concurrent_inquiry.py @@ -27,6 +27,7 @@ query_sql = [ "select count(*) from test.meters where t7 like 'fi%';", "select count(*) from test.meters where t7 like '_econd';", "select count(*) from test.meters interval(1n) order by ts desc;", +"select max(c0) from test.meters group by tbname", "select first(*) from test.meters;", "select last(*) from test.meters;", "select last_row(*) from test.meters;", @@ -56,6 +57,12 @@ query_sql = [ "select stddev(c6) from test.t1;", "select sum(c6) from test.meters;", "select top(c6, 2) from test.meters;", +#all vnode +"select count(*) from test.meters where t5 >2500 and t5<7500", +"select max(c0),avg(c1) from test.meters where t5 >2500 and t5<7500", +"select sum(c5),avg(c1) from test.meters where t5 >2500 and t5<7500", +"select max(c0),min(c6) from test.meters where t5 >2500 and t5<7500", +"select min(c0),avg(c6) from test.meters where t5 >2500 and t5<7500", # second supertable "select count(*) from test.meters1 where c1 > 50;", "select count(*) from test.meters1 where c2 >= 50 and c2 < 100;", @@ -65,6 +72,7 @@ query_sql = [ "select count(*) from test.meters1 where t7 like 'fi%';", "select count(*) from test.meters1 where t7 like '_econd';", "select count(*) from test.meters1 interval(1n) order by ts desc;", +"select max(c0) from test.meters1 group by tbname", "select first(*) from test.meters1;", "select last(*) from test.meters1;", "select last_row(*) from test.meters1;", @@ -93,7 +101,19 @@ query_sql = [ "select spread(c6) from test.m1 ;", "select stddev(c6) from test.m1;", "select sum(c6) from test.meters1;", -"select top(c6, 2) from test.meters1;" +"select top(c6, 2) from test.meters1;", +"select count(*) from test.meters1 where t5 >2500 and t5<7500", +#all vnode +"select count(*) from test.meters1 where t5 >2500 and t5<7500", +"select max(c0),avg(c1) from test.meters1 where t5 >2500 and t5<7500", +"select sum(c5),avg(c1) from test.meters1 where t5 >2500 and t5<7500", +"select max(c0),min(c6) from test.meters1 where t5 >2500 and t5<7500", +"select min(c0),avg(c6) from test.meters1 where t5 >2500 and t5<7500", +#join +"select * from meters,meters1 where meters.ts = meters1.ts and meters.t5 = meters1.t5", +"select * from meters,meters1 where meters.ts = meters1.ts and meters.t7 = meters1.t7", +"select * from meters,meters1 where meters.ts = meters1.ts and meters.t8 = meters1.t8", +"select meters.ts,meters1.c2 from meters,meters1 where meters.ts = meters1.ts and meters.t8 = meters1.t8" ] class ConcurrentInquiry: @@ -112,6 +132,7 @@ class ConcurrentInquiry: password, ) cl = conn.cursor() + cl.execute("use test;") print("Thread %d: starting" % threadID) From 2381b9b59085ceb079b29fb417400d96b93e7d29 Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Mon, 19 Oct 2020 18:54:07 +0800 Subject: [PATCH 048/101] [TD-1745] check C# connector --- tests/examples/C#/C#checker/C#checker.cs | 377 ++++++++++++++++++ tests/examples/C#/C#checker/TDengineDriver.cs | 154 +++++++ 2 files changed, 531 insertions(+) create mode 100644 tests/examples/C#/C#checker/C#checker.cs create mode 100644 tests/examples/C#/C#checker/TDengineDriver.cs diff --git a/tests/examples/C#/C#checker/C#checker.cs b/tests/examples/C#/C#checker/C#checker.cs new file mode 100644 index 0000000000..24b7060b14 --- /dev/null +++ b/tests/examples/C#/C#checker/C#checker.cs @@ -0,0 +1,377 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +using System; +using System.Text; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Collections; + +namespace TDengineDriver +{ + class TDengineTest + { + //connect parameters + private string host; + private string configDir; + private string user; + private string password; + private short port = 0; + + //sql parameters + private string dbName; + private string tbName; + + + private bool isInsertData; + private bool isQueryData; + + private long tableCount; + private long totalRows; + private long batchRows; + private long beginTimestamp = 1551369600000L; + + private IntPtr conn = IntPtr.Zero; + private long rowsInserted = 0; + + static void Main(string[] args) + { + TDengineTest tester = new TDengineTest(); + tester.ReadArgument(args); + + + tester.InitTDengine(); + tester.ConnectTDengine(); + tester.createDatabase(); + tester.useDatabase(); + tester.checkDropTable(); + tester.createTable(); + tester.checkInsert(); + tester.checkSelect(); + tester.checkDropTable(); + + tester.CloseConnection(); + + + + } + + public long GetArgumentAsLong(String[] argv, String argName, int minVal, int maxVal, int defaultValue) + { + int argc = argv.Length; + for (int i = 0; i < argc; ++i) + { + if (argName != argv[i]) + { + continue; + } + if (i < argc - 1) + { + String tmp = argv[i + 1]; + if (tmp[0] == '-') + { + Console.WriteLine("option {0:G} requires an argument", tmp); + ExitProgram(); + } + + long tmpVal = Convert.ToInt64(tmp); + if (tmpVal < minVal || tmpVal > maxVal) + { + Console.WriteLine("option {0:G} should in range [{1:G}, {2:G}]", argName, minVal, maxVal); + ExitProgram(); + } + + return tmpVal; + } + } + + return defaultValue; + } + + public String GetArgumentAsString(String[] argv, String argName, String defaultValue) + { + int argc = argv.Length; + for (int i = 0; i < argc; ++i) + { + if (argName != argv[i]) + { + continue; + } + if (i < argc - 1) + { + String tmp = argv[i + 1]; + if (tmp[0] == '-') + { + Console.WriteLine("option {0:G} requires an argument", tmp); + ExitProgram(); + } + return tmp; + } + } + + return defaultValue; + } + + public void PrintHelp(String[] argv) + { + for (int i = 0; i < argv.Length; ++i) + { + if ("--help" == argv[i]) + { + String indent = " "; + Console.WriteLine("taosTest is simple example to operate TDengine use C# Language.\n"); + Console.WriteLine("{0:G}{1:G}", indent, "-h"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "TDEngine server IP address to connect"); + Console.WriteLine("{0:G}{1:G}", indent, "-u"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "The TDEngine user name to use when connecting to the server, default is root"); + Console.WriteLine("{0:G}{1:G}", indent, "-p"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "The TDEngine user name to use when connecting to the server, default is taosdata"); + Console.WriteLine("{0:G}{1:G}", indent, "-d"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Database used to create table or import data, default is db"); + Console.WriteLine("{0:G}{1:G}", indent, "-s"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Super Tables used to create table, default is mt"); + Console.WriteLine("{0:G}{1:G}", indent, "-t"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Table prefixs, default is t"); + Console.WriteLine("{0:G}{1:G}", indent, "-w"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Whether to insert data"); + Console.WriteLine("{0:G}{1:G}", indent, "-r"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Whether to query data"); + Console.WriteLine("{0:G}{1:G}", indent, "-n"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "How many Tables to create, default is 10"); + Console.WriteLine("{0:G}{1:G}", indent, "-b"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "How many rows per insert batch, default is 10"); + Console.WriteLine("{0:G}{1:G}", indent, "-i"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "How many rows to insert, default is 100"); + Console.WriteLine("{0:G}{1:G}", indent, "-c"); + Console.WriteLine("{0:G}{1:G}{2:G}", indent, indent, "Configuration directory"); + + ExitProgram(); + } + } + } + + public void ReadArgument(String[] argv) + { + PrintHelp(argv); + host = this.GetArgumentAsString(argv, "-h", "127.0.0.1"); + user = this.GetArgumentAsString(argv, "-u", "root"); + password = this.GetArgumentAsString(argv, "-p", "taosdata"); + dbName = this.GetArgumentAsString(argv, "-db", "test"); + tbName = this.GetArgumentAsString(argv, "-s", "weather"); + + isInsertData = this.GetArgumentAsLong(argv, "-w", 0, 1, 1) != 0; + isQueryData = this.GetArgumentAsLong(argv, "-r", 0, 1, 1) != 0; + tableCount = this.GetArgumentAsLong(argv, "-n", 1, 10000, 10); + batchRows = this.GetArgumentAsLong(argv, "-b", 1, 1000, 500); + totalRows = this.GetArgumentAsLong(argv, "-i", 1, 10000000, 10000); + configDir = this.GetArgumentAsString(argv, "-c", "C:/TDengine/cfg"); + } + + public void InitTDengine() + { + TDengine.Options((int)TDengineInitOption.TDDB_OPTION_CONFIGDIR, this.configDir); + TDengine.Options((int)TDengineInitOption.TDDB_OPTION_SHELL_ACTIVITY_TIMER, "60"); + TDengine.Init(); + Console.WriteLine("get connection starting..."); + } + + public void ConnectTDengine() + { + string db = ""; + this.conn = TDengine.Connect(this.host, this.user, this.password, db, this.port); + if (this.conn == IntPtr.Zero) + { + Console.WriteLine("connection failed: " + this.host); + ExitProgram(); + } + else + { + Console.WriteLine("[ OK ] Connection established."); + } + } + public void createDatabase() + { + StringBuilder sql = new StringBuilder(); + sql.Append("create database if not exists ").Append(this.dbName); + execute(sql.ToString()); + } + public void useDatabase() + { + StringBuilder sql = new StringBuilder(); + sql.Append("use ").Append(this.dbName); + execute(sql.ToString()); + } + public void checkSelect() + { + StringBuilder sql = new StringBuilder(); + sql.Append("select * from test.weather"); + execute(sql.ToString()); + } + public void createTable() + { + StringBuilder sql = new StringBuilder(); + sql.Append("create table if not exists ").Append(this.dbName).Append(".").Append(this.tbName).Append("(ts timestamp, temperature float, humidity int)"); + execute(sql.ToString()); + } + public void checkInsert() + { + StringBuilder sql = new StringBuilder(); + sql.Append("insert into test.weather (ts, temperature, humidity) values(now, 20.5, 34)"); + execute(sql.ToString()); + } + public void checkDropTable() + { + StringBuilder sql = new StringBuilder(); + sql.Append("drop table if exists ").Append(this.dbName).Append(".").Append(this.tbName).Append(""); + execute(sql.ToString()); + } + public void execute(string sql) + { + DateTime dt1 = DateTime.Now; + IntPtr res = TDengine.Query(this.conn, sql.ToString()); + DateTime dt2 = DateTime.Now; + TimeSpan span = dt2 - dt1; + + if (res != IntPtr.Zero) + { + Console.WriteLine("[OK] time cost: " + span.ToString() + "ms, execute statement ====> " + sql.ToString()); + } + else + { + Console.WriteLine(sql.ToString() + " failure, reason: " + TDengine.Error(res)); + ExitProgram(); + } + TDengine.FreeResult(res); + } + + public void ExecuteQuery(string sql) + { + + DateTime dt1 = DateTime.Now; + long queryRows = 0; + IntPtr res = TDengine.Query(conn, sql); + if (res == IntPtr.Zero) + { + Console.WriteLine(sql + " failure, reason: " + TDengine.Error(res)); + ExitProgram(); + } + DateTime dt2 = DateTime.Now; + TimeSpan span = dt2 - dt1; + Console.WriteLine("[OK] time cost: " + span.ToString() + "ms, execute statement ====> " + sql.ToString()); + int fieldCount = TDengine.FieldCount(res); + + List metas = TDengine.FetchFields(res); + for (int j = 0; j < metas.Count; j++) + { + TDengineMeta meta = (TDengineMeta)metas[j]; + } + + IntPtr rowdata; + StringBuilder builder = new StringBuilder(); + while ((rowdata = TDengine.FetchRows(res)) != IntPtr.Zero) + { + queryRows++; + for (int fields = 0; fields < fieldCount; ++fields) + { + TDengineMeta meta = metas[fields]; + int offset = IntPtr.Size * fields; + IntPtr data = Marshal.ReadIntPtr(rowdata, offset); + + builder.Append("---"); + + if (data == IntPtr.Zero) + { + builder.Append("NULL"); + continue; + } + + switch ((TDengineDataType)meta.type) + { + case TDengineDataType.TSDB_DATA_TYPE_BOOL: + bool v1 = Marshal.ReadByte(data) == 0 ? false : true; + builder.Append(v1); + break; + case TDengineDataType.TSDB_DATA_TYPE_TINYINT: + byte v2 = Marshal.ReadByte(data); + builder.Append(v2); + break; + case TDengineDataType.TSDB_DATA_TYPE_SMALLINT: + short v3 = Marshal.ReadInt16(data); + builder.Append(v3); + break; + case TDengineDataType.TSDB_DATA_TYPE_INT: + int v4 = Marshal.ReadInt32(data); + builder.Append(v4); + break; + case TDengineDataType.TSDB_DATA_TYPE_BIGINT: + long v5 = Marshal.ReadInt64(data); + builder.Append(v5); + break; + case TDengineDataType.TSDB_DATA_TYPE_FLOAT: + float v6 = (float)Marshal.PtrToStructure(data, typeof(float)); + builder.Append(v6); + break; + case TDengineDataType.TSDB_DATA_TYPE_DOUBLE: + double v7 = (double)Marshal.PtrToStructure(data, typeof(double)); + builder.Append(v7); + break; + case TDengineDataType.TSDB_DATA_TYPE_BINARY: + string v8 = Marshal.PtrToStringAnsi(data); + builder.Append(v8); + break; + case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP: + long v9 = Marshal.ReadInt64(data); + builder.Append(v9); + break; + case TDengineDataType.TSDB_DATA_TYPE_NCHAR: + string v10 = Marshal.PtrToStringAnsi(data); + builder.Append(v10); + break; + } + } + builder.Append("---"); + + if (queryRows <= 10) + { + Console.WriteLine(builder.ToString()); + } + builder.Clear(); + } + + if (TDengine.ErrorNo(res) != 0) + { + Console.Write("Query is not complete, Error {0:G}", TDengine.ErrorNo(res), TDengine.Error(res)); + } + + TDengine.FreeResult(res); + + } + + public void CloseConnection() + { + if (this.conn != IntPtr.Zero) + { + TDengine.Close(this.conn); + Console.WriteLine("connection closed."); + } + } + + static void ExitProgram() + { + TDengine.Cleanup(); + System.Environment.Exit(0); + } + } +} diff --git a/tests/examples/C#/C#checker/TDengineDriver.cs b/tests/examples/C#/C#checker/TDengineDriver.cs new file mode 100644 index 0000000000..b6f143e181 --- /dev/null +++ b/tests/examples/C#/C#checker/TDengineDriver.cs @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace TDengineDriver +{ + enum TDengineDataType { + TSDB_DATA_TYPE_NULL = 0, // 1 bytes + TSDB_DATA_TYPE_BOOL = 1, // 1 bytes + TSDB_DATA_TYPE_TINYINT = 2, // 1 bytes + TSDB_DATA_TYPE_SMALLINT = 3, // 2 bytes + TSDB_DATA_TYPE_INT = 4, // 4 bytes + TSDB_DATA_TYPE_BIGINT = 5, // 8 bytes + TSDB_DATA_TYPE_FLOAT = 6, // 4 bytes + TSDB_DATA_TYPE_DOUBLE = 7, // 8 bytes + TSDB_DATA_TYPE_BINARY = 8, // string + TSDB_DATA_TYPE_TIMESTAMP = 9,// 8 bytes + TSDB_DATA_TYPE_NCHAR = 10 // unicode string + } + + enum TDengineInitOption + { + TSDB_OPTION_LOCALE = 0, + TSDB_OPTION_CHARSET = 1, + TSDB_OPTION_TIMEZONE = 2, + TDDB_OPTION_CONFIGDIR = 3, + TDDB_OPTION_SHELL_ACTIVITY_TIMER = 4 + } + + class TDengineMeta + { + public string name; + public short size; + public byte type; + public string TypeName() + { + switch ((TDengineDataType)type) + { + case TDengineDataType.TSDB_DATA_TYPE_BOOL: + return "BOOLEAN"; + case TDengineDataType.TSDB_DATA_TYPE_TINYINT: + return "BYTE"; + case TDengineDataType.TSDB_DATA_TYPE_SMALLINT: + return "SHORT"; + case TDengineDataType.TSDB_DATA_TYPE_INT: + return "INT"; + case TDengineDataType.TSDB_DATA_TYPE_BIGINT: + return "LONG"; + case TDengineDataType.TSDB_DATA_TYPE_FLOAT: + return "FLOAT"; + case TDengineDataType.TSDB_DATA_TYPE_DOUBLE: + return "DOUBLE"; + case TDengineDataType.TSDB_DATA_TYPE_BINARY: + return "STRING"; + case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP: + return "TIMESTAMP"; + case TDengineDataType.TSDB_DATA_TYPE_NCHAR: + return "NCHAR"; + default: + return "undefine"; + } + } + } + + class TDengine + { + public const int TSDB_CODE_SUCCESS = 0; + + [DllImport("taos.dll", EntryPoint = "taos_init", CallingConvention = CallingConvention.Cdecl)] + static extern public void Init(); + + [DllImport("taos.dll", EntryPoint = "taos_cleanup", CallingConvention = CallingConvention.Cdecl)] + static extern public void Cleanup(); + + [DllImport("taos.dll", EntryPoint = "taos_options", CallingConvention = CallingConvention.Cdecl)] + static extern public void Options(int option, string value); + + [DllImport("taos.dll", EntryPoint = "taos_connect", CallingConvention = CallingConvention.Cdecl)] + static extern public IntPtr Connect(string ip, string user, string password, string db, short port); + + [DllImport("taos.dll", EntryPoint = "taos_errstr", CallingConvention = CallingConvention.Cdecl)] + static extern private IntPtr taos_errstr(IntPtr res); + static public string Error(IntPtr res) + { + IntPtr errPtr = taos_errstr(res); + return Marshal.PtrToStringAnsi(errPtr); + } + + [DllImport("taos.dll", EntryPoint = "taos_errno", CallingConvention = CallingConvention.Cdecl)] + static extern public int ErrorNo(IntPtr res); + + [DllImport("taos.dll", EntryPoint = "taos_query", CallingConvention = CallingConvention.Cdecl)] + static extern public IntPtr Query(IntPtr conn, string sqlstr); + + [DllImport("taos.dll", EntryPoint = "taos_affected_rows", CallingConvention = CallingConvention.Cdecl)] + static extern public int AffectRows(IntPtr res); + + [DllImport("taos.dll", EntryPoint = "taos_field_count", CallingConvention = CallingConvention.Cdecl)] + static extern public int FieldCount(IntPtr res); + + [DllImport("taos.dll", EntryPoint = "taos_fetch_fields", CallingConvention = CallingConvention.Cdecl)] + static extern private IntPtr taos_fetch_fields(IntPtr res); + static public List FetchFields(IntPtr res) + { + const int fieldSize = 68; + + List metas = new List(); + if (res == IntPtr.Zero) + { + return metas; + } + + int fieldCount = FieldCount(res); + IntPtr fieldsPtr = taos_fetch_fields(res); + + for (int i = 0; i < fieldCount; ++i) + { + int offset = i * fieldSize; + + TDengineMeta meta = new TDengineMeta(); + meta.name = Marshal.PtrToStringAnsi(fieldsPtr + offset); + meta.type = Marshal.ReadByte(fieldsPtr + offset + 65); + meta.size = Marshal.ReadInt16(fieldsPtr + offset + 66); + metas.Add(meta); + } + + return metas; + } + + [DllImport("taos.dll", EntryPoint = "taos_fetch_row", CallingConvention = CallingConvention.Cdecl)] + static extern public IntPtr FetchRows(IntPtr res); + + [DllImport("taos.dll", EntryPoint = "taos_free_result", CallingConvention = CallingConvention.Cdecl)] + static extern public IntPtr FreeResult(IntPtr res); + + [DllImport("taos.dll", EntryPoint = "taos_close", CallingConvention = CallingConvention.Cdecl)] + static extern public int Close(IntPtr taos); + } +} \ No newline at end of file From dc23dd496336b0cae7fc24f54f3332eafb1900b2 Mon Sep 17 00:00:00 2001 From: zyyang Date: Mon, 19 Oct 2020 20:25:23 +0800 Subject: [PATCH 049/101] update jdbcTaosDemo --- tests/examples/JDBC/JDBCDemo/pom.xml | 21 +- tests/examples/JDBC/JDBCDemo/readme.md | 14 +- .../src/main/java/TSDBSyncSample.java | 205 ------------------ ...ConnectorChecker.java => JdbcChecker.java} | 4 +- .../example/jdbcTaosdemo/JdbcTaosdemo.java | 84 ++++++- .../domain/JdbcTaosdemoConfig.java | 6 +- .../jdbcTaosdemo/task/InsertTableTask.java | 29 ++- .../jdbcTaosdemo/utils/SqlSpeller.java | 44 ++++ .../jdbcTaosdemo/utils/TimeStampUtil.java | 16 +- .../jdbcTaosdemo/utils/TimeStampUtilTest.java | 52 +++++ 10 files changed, 226 insertions(+), 249 deletions(-) delete mode 100644 tests/examples/JDBC/JDBCDemo/src/main/java/TSDBSyncSample.java rename tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/{JDBCConnectorChecker.java => JdbcChecker.java} (98%) create mode 100644 tests/examples/JDBC/JDBCDemo/src/test/java/com/taosdata/example/jdbcTaosdemo/utils/TimeStampUtilTest.java diff --git a/tests/examples/JDBC/JDBCDemo/pom.xml b/tests/examples/JDBC/JDBCDemo/pom.xml index 2113074674..15db449810 100644 --- a/tests/examples/JDBC/JDBCDemo/pom.xml +++ b/tests/examples/JDBC/JDBCDemo/pom.xml @@ -5,18 +5,11 @@ 4.0.0 com.taosdata.jdbc - jdbcdemo - 1.0-SNAPSHOT + jdbcChecker + SNAPSHOT jar - - - org.apache.maven.plugins - maven-plugins - 30 - - org.apache.maven.plugins maven-assembly-plugin @@ -30,7 +23,7 @@ - TSDBSyncSample + com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo @@ -63,12 +56,18 @@ com.taosdata.jdbc taos-jdbcdriver - 2.0.4 + 2.0.8 log4j log4j 1.2.17 + + junit + junit + 4.12 + test + diff --git a/tests/examples/JDBC/JDBCDemo/readme.md b/tests/examples/JDBC/JDBCDemo/readme.md index a91624a9e4..9b8790adad 100644 --- a/tests/examples/JDBC/JDBCDemo/readme.md +++ b/tests/examples/JDBC/JDBCDemo/readme.md @@ -2,12 +2,14 @@ TDengine's JDBC demo project is organized in a Maven way so that users can easily compile, package and run the project. If you don't have Maven on your server, you may install it using
sudo apt-get install maven
-## Compile and Install JDBC Driver -TDengine's JDBC driver jar is not yet published to maven center repo, so we need to manually compile it and install it to the local Maven repository. This can be easily done with Maven. Go to source directory of the JDBC driver ``TDengine/src/connector/jdbc`` and execute -
mvn clean package install
+## Install TDengine Client +Make sure you have already installed a tdengine client on your current develop environment. +Download the tdengine package on our website: ``https://www.taosdata.com/cn/all-downloads/`` and install the client. ## Compile the Demo Code and Run It To compile the demo project, go to the source directory ``TDengine/tests/examples/JDBC/JDBCDemo`` and execute -
mvn clean assembly:single package
-The ``pom.xml`` is configured to package all the dependencies into one executable jar file. To run it, go to ``examples/JDBC/JDBCDemo/target`` and execute -
java -jar jdbcdemo-1.0-SNAPSHOT-jar-with-dependencies.jar
+
mvn clean package assembly:single
+The ``pom.xml`` is configured to package all the dependencies into one executable jar file. + +To run it, go to ``examples/JDBC/JDBCDemo/target`` and execute +
java -jar jdbcChecker-SNAPSHOT-jar-with-dependencies.jar -host localhost
\ No newline at end of file diff --git a/tests/examples/JDBC/JDBCDemo/src/main/java/TSDBSyncSample.java b/tests/examples/JDBC/JDBCDemo/src/main/java/TSDBSyncSample.java deleted file mode 100644 index c093b604da..0000000000 --- a/tests/examples/JDBC/JDBCDemo/src/main/java/TSDBSyncSample.java +++ /dev/null @@ -1,205 +0,0 @@ -import java.sql.*; - -public class TSDBSyncSample { - private static final String JDBC_PROTOCAL = "jdbc:TAOS://"; - private static final String TSDB_DRIVER = "com.taosdata.jdbc.TSDBDriver"; - - private String host = "127.0.0.1"; - private String user = "root"; - private String password = "taosdata"; - private int port = 0; - private String jdbcUrl = ""; - - private String databaseName = "db"; - private String metricsName = "mt"; - private String tablePrefix = "t"; - - private int tablesCount = 1; - private int loopCount = 2; - private int batchSize = 10; - private long beginTimestamp = 1519833600000L; - - private long rowsInserted = 0; - - static { - try { - Class.forName(TSDB_DRIVER); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * @param args - */ - public static void main(String[] args) { - TSDBSyncSample tester = new TSDBSyncSample(); - tester.doReadArgument(args); - - System.out.println("---------------------------------------------------------------"); - System.out.println("Start testing..."); - System.out.println("---------------------------------------------------------------"); - - tester.doMakeJdbcUrl(); - tester.doCreateDbAndTable(); - tester.doExecuteInsert(); - tester.doExecuteQuery(); - - System.out.println("\n---------------------------------------------------------------"); - System.out.println("Stop testing..."); - System.out.println("---------------------------------------------------------------"); - } - - private void doReadArgument(String[] args) { - System.out.println("Arguments format: host tables loop batchs"); - if (args.length >= 1) { - this.host = args[0]; - } - - if (args.length >= 2) { - this.tablesCount = Integer.parseInt(args[1]); - } - - if (args.length >= 3) { - this.loopCount = Integer.parseInt(args[2]); - } - - if (args.length >= 4) { - this.batchSize = Integer.parseInt(args[3]); - } - } - - private void doMakeJdbcUrl() { - // jdbc:TSDB://127.0.0.1:0/dbname?user=root&password=taosdata - System.out.println("\nJDBC URL to use:"); - this.jdbcUrl = String.format("%s%s:%d/%s?user=%s&password=%s", JDBC_PROTOCAL, this.host, this.port, "", - this.user, this.password); - System.out.println(this.jdbcUrl); - } - - private void doCreateDbAndTable() { - System.out.println("\n---------------------------------------------------------------"); - System.out.println("Start creating databases and tables..."); - String sql = ""; - try (Connection conn = DriverManager.getConnection(jdbcUrl); - Statement stmt = conn.createStatement()){ - - sql = "create database if not exists " + this.databaseName; - stmt.executeUpdate(sql); - System.out.printf("Successfully executed: %s\n", sql); - - sql = "use " + this.databaseName; - stmt.executeUpdate(sql); - System.out.printf("Successfully executed: %s\n", sql); - - sql = "create table if not exists " + this.metricsName + " (ts timestamp, v1 int) tags(t1 int)"; - stmt.executeUpdate(sql); - System.out.printf("Successfully executed: %s\n", sql); - - for (int i = 0; i < this.tablesCount; i++) { - sql = String.format("create table if not exists %s%d using %s tags(%d)", this.tablePrefix, i, - this.metricsName, i); - stmt.executeUpdate(sql); - System.out.printf("Successfully executed: %s\n", sql); - } - } catch (SQLException e) { - e.printStackTrace(); - System.out.printf("Failed to execute SQL: %s\n", sql); - System.exit(4); - } catch (Exception e) { - e.printStackTrace(); - System.exit(4); - } - System.out.println("Successfully created databases and tables"); - } - - public void doExecuteInsert() { - System.out.println("\n---------------------------------------------------------------"); - System.out.println("Start inserting data..."); - int start = (int) System.currentTimeMillis(); - StringBuilder sql = new StringBuilder(""); - try (Connection conn = DriverManager.getConnection(jdbcUrl); - Statement stmt = conn.createStatement()){ - stmt.executeUpdate("use " + databaseName); - for (int loop = 0; loop < this.loopCount; loop++) { - for (int table = 0; table < this.tablesCount; ++table) { - sql = new StringBuilder("insert into "); - sql.append(this.tablePrefix).append(table).append(" values"); - for (int batch = 0; batch < this.batchSize; ++batch) { - int rows = loop * this.batchSize + batch; - sql.append("(").append(this.beginTimestamp + rows).append(",").append(rows).append(")"); - } - int affectRows = stmt.executeUpdate(sql.toString()); - this.rowsInserted += affectRows; - } - } - } catch (SQLException e) { - e.printStackTrace(); - System.out.printf("Failed to execute SQL: %s\n", sql.toString()); - System.exit(4); - } catch (Exception e) { - e.printStackTrace(); - System.exit(4); - } - int end = (int) System.currentTimeMillis(); - System.out.println("Inserting completed!"); - System.out.printf("Total %d rows inserted, %d rows failed, time spend %d seconds.\n", this.rowsInserted, - this.loopCount * this.batchSize - this.rowsInserted, (end - start) / 1000); - } - - public void doExecuteQuery() { - System.out.println("\n---------------------------------------------------------------"); - System.out.println("Starting querying data..."); - ResultSet resSet = null; - StringBuilder sql = new StringBuilder(""); - StringBuilder resRow = new StringBuilder(""); - try (Connection conn = DriverManager.getConnection(jdbcUrl); - Statement stmt = conn.createStatement()){ - stmt.executeUpdate("use " + databaseName); - for (int i = 0; i < this.tablesCount; ++i) { - sql = new StringBuilder("select * from ").append(this.tablePrefix).append(i); - - resSet = stmt.executeQuery(sql.toString()); - if (resSet == null) { - System.out.println(sql + " failed"); - System.exit(4); - } - - ResultSetMetaData metaData = resSet.getMetaData(); - System.out.println("Retrieve metadata of " + tablePrefix + i); - for (int column = 1; column <= metaData.getColumnCount(); ++column) { - System.out.printf("Column%d: name = %s, type = %d, type name = %s, display size = %d\n", column, metaData.getColumnName(column), metaData.getColumnType(column), - metaData.getColumnTypeName(column), metaData.getColumnDisplaySize(column)); - } - int rows = 0; - System.out.println("Retrieve data of " + tablePrefix + i); - while (resSet.next()) { - resRow = new StringBuilder(); - for (int col = 1; col <= metaData.getColumnCount(); col++) { - resRow.append(metaData.getColumnName(col)).append("=").append(resSet.getObject(col)) - .append(" "); - } - System.out.println(resRow.toString()); - rows++; - } - - try { - if (resSet != null) - resSet.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - System.out.printf("Successfully executed query: %s;\nTotal rows returned: %d\n", sql.toString(), rows); - } - } catch (SQLException e) { - e.printStackTrace(); - System.out.printf("Failed to execute query: %s\n", sql.toString()); - System.exit(4); - } catch (Exception e) { - e.printStackTrace(); - System.exit(4); - } - System.out.println("Query completed!"); - } - -} diff --git a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JDBCConnectorChecker.java b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcChecker.java similarity index 98% rename from tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JDBCConnectorChecker.java rename to tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcChecker.java index 74e586d7fd..e5283c24e9 100644 --- a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JDBCConnectorChecker.java +++ b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcChecker.java @@ -5,7 +5,7 @@ import com.taosdata.jdbc.TSDBDriver; import java.sql.*; import java.util.Properties; -public class JDBCConnectorChecker { +public class JdbcChecker { private static String host; private static String dbName = "test"; private static String tbName = "weather"; @@ -157,7 +157,7 @@ public class JDBCConnectorChecker { return; } - JDBCConnectorChecker checker = new JDBCConnectorChecker(); + JdbcChecker checker = new JdbcChecker(); checker.init(); checker.createDatabase(); checker.useDatabase(); diff --git a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/JdbcTaosdemo.java b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/JdbcTaosdemo.java index c30d85a084..259985ec9f 100644 --- a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/JdbcTaosdemo.java +++ b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/JdbcTaosdemo.java @@ -25,6 +25,7 @@ public class JdbcTaosdemo { } public static void main(String[] args) { + // parse config from args JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args); boolean isHelp = Arrays.asList(args).contains("--help"); @@ -38,27 +39,51 @@ public class JdbcTaosdemo { } JdbcTaosdemo taosdemo = new JdbcTaosdemo(config); + // establish connection taosdemo.init(); + // drop database taosdemo.dropDatabase(); + // create database taosdemo.createDatabase(); + // use db taosdemo.useDatabase(); + // create super table taosdemo.createSuperTable(); + // create sub tables taosdemo.createTableMultiThreads(); boolean infinite = Arrays.asList(args).contains("--infinite"); if (infinite) { - logger.info("!!! Infinite Insert Mode Started. !!!!"); + logger.info("!!! Infinite Insert Mode Started. !!!"); taosdemo.insertInfinite(); } else { + // insert into table taosdemo.insertMultiThreads(); - // single table select + // select from sub table taosdemo.selectFromTableLimit(); taosdemo.selectCountFromTable(); taosdemo.selectAvgMinMaxFromTable(); - // super table select + // select last from + taosdemo.selectLastFromTable(); + // select from super table taosdemo.selectFromSuperTableLimit(); taosdemo.selectCountFromSuperTable(); taosdemo.selectAvgMinMaxFromSuperTable(); + //select avg ,max from stb where tag + taosdemo.selectAvgMinMaxFromSuperTableWhereTag(); + //select last from stb where location = '' + taosdemo.selectLastFromSuperTableWhere(); + // select group by + taosdemo.selectGroupBy(); + // select like + taosdemo.selectLike(); + // select where ts >= ts<= + taosdemo.selectLastOneHour(); + taosdemo.selectLastOneDay(); + taosdemo.selectLastOneWeek(); + taosdemo.selectLastOneMonth(); + taosdemo.selectLastOneYear(); + // drop super table if (config.isDeleteTable()) taosdemo.dropSuperTable(); @@ -196,6 +221,11 @@ public class JdbcTaosdemo { executeQuery(sql); } + private void selectLastFromTable() { + String sql = SqlSpeller.selectLastFromTableSQL(config.getDbName(), config.getTbPrefix(), 1); + executeQuery(sql); + } + private void selectFromSuperTableLimit() { String sql = SqlSpeller.selectFromSuperTableLimitSQL(config.getDbName(), config.getStbName(), 10, 0); executeQuery(sql); @@ -211,6 +241,52 @@ public class JdbcTaosdemo { executeQuery(sql); } + private void selectAvgMinMaxFromSuperTableWhereTag() { + String sql = SqlSpeller.selectAvgMinMaxFromSuperTableWhere("current", config.getDbName(), config.getStbName()); + executeQuery(sql); + } + + private void selectLastFromSuperTableWhere() { + String sql = SqlSpeller.selectLastFromSuperTableWhere("current", config.getDbName(), config.getStbName()); + executeQuery(sql); + } + + private void selectGroupBy() { + String sql = SqlSpeller.selectGroupBy("current", config.getDbName(), config.getStbName()); + executeQuery(sql); + } + + private void selectLike() { + String sql = SqlSpeller.selectLike(config.getDbName(), config.getStbName()); + executeQuery(sql); + } + + private void selectLastOneHour() { + String sql = SqlSpeller.selectLastOneHour(config.getDbName(), config.getStbName()); + executeQuery(sql); + } + + private void selectLastOneDay() { + String sql = SqlSpeller.selectLastOneDay(config.getDbName(), config.getStbName()); + executeQuery(sql); + } + + private void selectLastOneWeek() { + String sql = SqlSpeller.selectLastOneWeek(config.getDbName(), config.getStbName()); + executeQuery(sql); + } + + private void selectLastOneMonth() { + String sql = SqlSpeller.selectLastOneMonth(config.getDbName(), config.getStbName()); + executeQuery(sql); + } + + private void selectLastOneYear() { + String sql = SqlSpeller.selectLastOneYear(config.getDbName(), config.getStbName()); + executeQuery(sql); + } + + private void close() { try { if (connection != null) { @@ -241,6 +317,7 @@ public class JdbcTaosdemo { long end = System.currentTimeMillis(); printSql(sql, execute, (end - start)); } catch (SQLException e) { + logger.error("ERROR execute SQL ===> " + sql); logger.error(e.getMessage()); e.printStackTrace(); } @@ -258,6 +335,7 @@ public class JdbcTaosdemo { printSql(sql, true, (end - start)); printResult(resultSet); } catch (SQLException e) { + logger.error("ERROR execute SQL ===> " + sql); logger.error(e.getMessage()); e.printStackTrace(); } diff --git a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/domain/JdbcTaosdemoConfig.java b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/domain/JdbcTaosdemoConfig.java index 3cca9a3d7a..82613037db 100644 --- a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/domain/JdbcTaosdemoConfig.java +++ b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/domain/JdbcTaosdemoConfig.java @@ -14,9 +14,9 @@ public final class JdbcTaosdemoConfig { //Destination database. Default is 'test' private String dbName = "test"; //keep - private int keep = 365 * 20; + private int keep = 3650; //days - private int days = 30; + private int days = 10; //Super table Name. Default is 'meters' private String stbName = "meters"; @@ -35,7 +35,7 @@ public final class JdbcTaosdemoConfig { private boolean deleteTable = false; public static void printHelp() { - System.out.println("Usage: java -jar JDBCConnectorChecker.jar [OPTION...]"); + System.out.println("Usage: java -jar JdbcTaosDemo.jar [OPTION...]"); System.out.println("-h host The host to connect to TDengine. you must input one"); System.out.println("-p port The TCP/IP port number to use for the connection. Default is 6030"); System.out.println("-u user The TDengine user name to use when connecting to the server. Default is 'root'"); diff --git a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/task/InsertTableTask.java b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/task/InsertTableTask.java index d6d6ebbff1..a35628bb58 100644 --- a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/task/InsertTableTask.java +++ b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/task/InsertTableTask.java @@ -3,44 +3,49 @@ package com.taosdata.example.jdbcTaosdemo.task; import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig; import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory; import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller; -import com.taosdata.example.jdbcTaosdemo.utils.TimeStampUtil; import org.apache.log4j.Logger; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; -import java.util.concurrent.atomic.AtomicLong; +import java.time.Duration; +import java.time.Instant; +import java.time.temporal.ChronoUnit; public class InsertTableTask implements Runnable { private static final Logger logger = Logger.getLogger(InsertTableTask.class); - private static AtomicLong beginTimestamp = new AtomicLong(TimeStampUtil.datetimeToLong("2005-01-01 00:00:00.000")); private final JdbcTaosdemoConfig config; - private final int startIndex; + private final int startTbIndex; private final int tableNumber; - private final int recordsNumber; + private final int recordsNumberPerTable; - public InsertTableTask(JdbcTaosdemoConfig config, int startIndex, int tableNumber, int recordsNumber) { + public InsertTableTask(JdbcTaosdemoConfig config, int startTbIndex, int tableNumber, int recordsNumberPerTable) { this.config = config; - this.startIndex = startIndex; + this.startTbIndex = startTbIndex; this.tableNumber = tableNumber; - this.recordsNumber = recordsNumber; + this.recordsNumberPerTable = recordsNumberPerTable; } @Override public void run() { try { Connection connection = ConnectionFactory.build(config); + int keep = config.getKeep(); + Instant end = Instant.now(); + Instant start = end.minus(Duration.ofDays(keep - 1)); + long timeGap = ChronoUnit.MILLIS.between(start, end) / (recordsNumberPerTable - 1); + // iterate insert - for (int j = 0; j < recordsNumber; j++) { - long ts = beginTimestamp.getAndIncrement(); + for (int j = 0; j < recordsNumberPerTable; j++) { + long ts = start.toEpochMilli() + (j * timeGap); // insert data into echo table - for (int i = startIndex; i < startIndex + tableNumber; i++) { + for (int i = startTbIndex; i < startTbIndex + tableNumber; i++) { String sql = SqlSpeller.insertOneRowSQL(config.getDbName(), config.getTbPrefix(), i + 1, ts); + logger.info(Thread.currentThread().getName() + ">>> " + sql); Statement statement = connection.createStatement(); statement.execute(sql); statement.close(); - logger.info(Thread.currentThread().getName() + ">>> " + sql); } } connection.close(); diff --git a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java index 7af97f3b1b..c20e12c048 100644 --- a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java +++ b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java @@ -78,5 +78,49 @@ public class SqlSpeller { return "select avg(" + field + "),min(" + field + "),max(" + field + ") from " + dbName + "." + stbName + ""; } + public static String selectLastFromTableSQL(String dbName, String tbPrefix, int tbIndex) { + return "select last(*) from " + dbName + "." + tbPrefix + "" + tbIndex; + } + //select avg ,max from stb where tag + public static String selectAvgMinMaxFromSuperTableWhere(String field, String dbName, String stbName) { + return "select avg(" + field + "),min(" + field + "),max(" + field + ") from " + dbName + "." + stbName + " where location = '" + locations[random.nextInt(locations.length)] + "'"; + } + + //select last from stb where + public static String selectLastFromSuperTableWhere(String field, String dbName, String stbName) { + return "select last(" + field + ") from " + dbName + "." + stbName + " where location = '" + locations[random.nextInt(locations.length)] + "'"; + } + + public static String selectGroupBy(String field, String dbName, String stbName) { + return "select avg(" + field + ") from " + dbName + "." + stbName + " group by location"; + } + + public static String selectLike(String dbName, String stbName) { + return "select * from " + dbName + "." + stbName + " where location like 'S%'"; + } + + public static String selectLastOneHour(String dbName, String stbName) { + return "select * from " + dbName + "." + stbName + " where ts >= now - 1h"; + } + + public static String selectLastOneDay(String dbName, String stbName) { + return "select * from " + dbName + "." + stbName + " where ts >= now - 1d"; + } + + public static String selectLastOneWeek(String dbName, String stbName) { + return "select * from " + dbName + "." + stbName + " where ts >= now - 1w"; + } + + public static String selectLastOneMonth(String dbName, String stbName) { + return "select * from " + dbName + "." + stbName + " where ts >= now - 1M"; + } + + public static String selectLastOneYear(String dbName, String stbName) { + return "select * from " + dbName + "." + stbName + " where ts >= now - 1y"; + } + + // select group by + // select like + // select ts >= ts<= } \ No newline at end of file diff --git a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/TimeStampUtil.java b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/TimeStampUtil.java index d00471f581..0a345afdd1 100644 --- a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/TimeStampUtil.java +++ b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/TimeStampUtil.java @@ -1,8 +1,10 @@ package com.taosdata.example.jdbcTaosdemo.utils; -import java.sql.Date; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.Duration; +import java.time.Instant; +import java.util.Date; public class TimeStampUtil { private static final String datetimeFormat = "yyyy-MM-dd HH:mm:ss.SSS"; @@ -21,14 +23,14 @@ public class TimeStampUtil { return sdf.format(new Date(time)); } - public static void main(String[] args) { - final String startTime = "2005-01-01 00:00:00.000"; + public static void main(String[] args) throws ParseException { + +// Instant now = Instant.now(); +// System.out.println(now); +// Instant years20Ago = now.minus(Duration.ofDays(365)); +// System.out.println(years20Ago); - long start = TimeStampUtil.datetimeToLong(startTime); - System.out.println(start); - String datetime = TimeStampUtil.longToDatetime(1519833600000L); - System.out.println(datetime); } diff --git a/tests/examples/JDBC/JDBCDemo/src/test/java/com/taosdata/example/jdbcTaosdemo/utils/TimeStampUtilTest.java b/tests/examples/JDBC/JDBCDemo/src/test/java/com/taosdata/example/jdbcTaosdemo/utils/TimeStampUtilTest.java new file mode 100644 index 0000000000..f370b2ef6e --- /dev/null +++ b/tests/examples/JDBC/JDBCDemo/src/test/java/com/taosdata/example/jdbcTaosdemo/utils/TimeStampUtilTest.java @@ -0,0 +1,52 @@ +package com.taosdata.example.jdbcTaosdemo.utils; + +import org.junit.Test; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.Duration; +import java.time.Instant; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Date; + +import static org.junit.Assert.*; + +public class TimeStampUtilTest { + + @Test + public void datetimeToLong() { + final String startTime = "2005-01-01 00:00:00.000"; + long start = TimeStampUtil.datetimeToLong(startTime); + assertEquals(1104508800000l, start); + } + + @Test + public void longToDatetime() { + String datetime = TimeStampUtil.longToDatetime(1510000000000L); + assertEquals("2017-11-07 04:26:40.000", datetime); + } + + @Test + public void getStartDateTime() { + int keep = 365; + + Instant end = Instant.now(); + System.out.println(end.toString()); + System.out.println(end.toEpochMilli()); + + Instant start = end.minus(Duration.ofDays(keep)); + System.out.println(start.toString()); + System.out.println(start.toEpochMilli()); + + int numberOfRecordsPerTable = 10; + long timeGap = ChronoUnit.MILLIS.between(start, end) / (numberOfRecordsPerTable - 1); + System.out.println(timeGap); + + System.out.println("==========================="); + for (int i = 0; i < numberOfRecordsPerTable; i++) { + long ts = start.toEpochMilli() + (i * timeGap); + System.out.println(i + " : " + ts); + } + } +} \ No newline at end of file From aa55054e368aee22563a47c143e035108cb46248 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 19 Oct 2020 23:19:44 +0800 Subject: [PATCH 050/101] [td-1747] --- src/client/inc/tscUtil.h | 5 +- src/client/inc/tsclient.h | 1 + src/client/src/tscServer.c | 14 +++--- src/client/src/tscStream.c | 6 +-- src/client/src/tscSubquery.c | 8 +-- src/client/src/tscSystem.c | 8 +-- src/client/src/tscUtil.c | 95 +++++++++++++++++++++++++++++++----- src/mnode/src/mnodeTable.c | 4 +- 8 files changed, 111 insertions(+), 30 deletions(-) diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index 76a9bbac10..1bd86714c5 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -231,10 +231,11 @@ int tscGetTableMeta(SSqlObj* pSql, STableMetaInfo* pTableMetaInfo); int tscGetMeterMetaEx(SSqlObj* pSql, STableMetaInfo* pTableMetaInfo, bool createIfNotExists); void tscResetForNextRetrieve(SSqlRes* pRes); - -void tscAddTimestampColumn(SQueryInfo* pQueryInfo, int16_t functionId, int16_t tableIndex); void tscDoQuery(SSqlObj* pSql); +SVgroupsInfo* tscVgroupInfoClone(SVgroupsInfo *pInfo); +void* tscVgroupInfoClear(SVgroupsInfo *pInfo); +void tscSCMVgroupInfoCopy(SCMVgroupInfo* dst, const SCMVgroupInfo* src); /** * The create object function must be successful expect for the out of memory issue. * diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index f0d3e26a49..5d9abf852e 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -443,6 +443,7 @@ void tscPartiallyFreeSqlObj(SSqlObj *pSql); */ void tscFreeSqlObj(SSqlObj *pSql); void tscFreeRegisteredSqlObj(void *pSql); +void tscFreeTableMetaHelper(void *pTableMeta); void tscCloseTscObj(STscObj *pObj); diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 8a50299d51..ff798a9f99 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -124,9 +124,11 @@ static void tscUpdateVgroupInfo(SSqlObj *pObj, SRpcEpSet *pEpSet) { pVgroupInfo->inUse = pEpSet->inUse; pVgroupInfo->numOfEps = pEpSet->numOfEps; for (int32_t i = 0; i < pVgroupInfo->numOfEps; i++) { - tstrncpy(pVgroupInfo->epAddr[i].fqdn, pEpSet->fqdn[i], TSDB_FQDN_LEN); + taosTFree(pVgroupInfo->epAddr[i].fqdn); + pVgroupInfo->epAddr[i].fqdn = strndup(pEpSet->fqdn[i], tListLen(pEpSet->fqdn[i])); pVgroupInfo->epAddr[i].port = pEpSet->port[i]; } + tscDebug("after: EndPoint in use: %d", pVgroupInfo->inUse); taosCorEndWrite(&pVgroupInfo->version); } @@ -1852,15 +1854,15 @@ int tscProcessSTableVgroupRsp(SSqlObj *pSql) { //just init, no need to lock SCMVgroupInfo *pVgroups = &pInfo->vgroupList->vgroups[j]; - SCMVgroupMsg *vgroupMsg = &pVgroupMsg->vgroups[j]; - pVgroups->vgId = htonl(vgroupMsg->vgId); - pVgroups->numOfEps = vgroupMsg->numOfEps; + SCMVgroupMsg *vmsg = &pVgroupMsg->vgroups[j]; + pVgroups->vgId = htonl(vmsg->vgId); + pVgroups->numOfEps = vmsg->numOfEps; assert(pVgroups->numOfEps >= 1 && pVgroups->vgId >= 1); for (int32_t k = 0; k < pVgroups->numOfEps; ++k) { - pVgroups->epAddr[k].port = htons(vgroupMsg->epAddr[k].port); - pVgroups->epAddr[k].fqdn = strndup(vgroupMsg->epAddr[k].fqdn, tListLen(vgroupMsg->epAddr[k].fqdn)); + pVgroups->epAddr[k].port = htons(vmsg->epAddr[k].port); + pVgroups->epAddr[k].fqdn = strndup(vmsg->epAddr[k].fqdn, tListLen(vmsg->epAddr[k].fqdn)); } } diff --git a/src/client/src/tscStream.c b/src/client/src/tscStream.c index 61614b56fb..0f67911bbe 100644 --- a/src/client/src/tscStream.c +++ b/src/client/src/tscStream.c @@ -168,8 +168,8 @@ static void tscProcessStreamQueryCallback(void *param, TAOS_RES *tres, int numOf STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(&pStream->pSql->cmd, 0, 0); taosCacheRelease(tscMetaCache, (void**)&(pTableMetaInfo->pTableMeta), true); - taosTFree(pTableMetaInfo->vgroupList); - + pTableMetaInfo->vgroupList = tscVgroupInfoClear(pTableMetaInfo->vgroupList); + tscSetRetryTimer(pStream, pStream->pSql, retryDelay); return; } @@ -275,7 +275,7 @@ static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOf tscFreeSqlResult(pSql); taosTFree(pSql->pSubs); pSql->subState.numOfSub = 0; - taosTFree(pTableMetaInfo->vgroupList); + pTableMetaInfo->vgroupList = tscVgroupInfoClear(pTableMetaInfo->vgroupList); tscSetNextLaunchTimer(pStream, pSql); } } diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index 49759bc4d3..7a2a242df5 100644 --- a/src/client/src/tscSubquery.c +++ b/src/client/src/tscSubquery.c @@ -449,7 +449,7 @@ void tscBuildVgroupTableInfo(SSqlObj* pSql, STableMetaInfo* pTableMetaInfo, SArr SVgroupTableInfo info = {{0}}; for (int32_t m = 0; m < pvg->numOfVgroups; ++m) { if (tt->vgId == pvg->vgroups[m].vgId) { - info.vgInfo = pvg->vgroups[m]; + tscSCMVgroupInfoCopy(&info.vgInfo, &pvg->vgroups[m]); break; } } @@ -1645,9 +1645,9 @@ static void tscAllDataRetrievedFromDnode(SRetrieveSupport *trsupport, SSqlObj* p // data in from current vnode is stored in cache and disk uint32_t numOfRowsFromSubquery = (uint32_t)(trsupport->pExtMemBuffer[idx]->numOfTotalElems + trsupport->localBuffer->num); - tscDebug("%p sub:%p all data retrieved from ep:%s, vgId:%d, numOfRows:%d, orderOfSub:%d", pParentSql, pSql, - pTableMetaInfo->vgroupList->vgroups[0].epAddr[0].fqdn, pTableMetaInfo->vgroupList->vgroups[0].vgId, - numOfRowsFromSubquery, idx); + SVgroupsInfo* vgroupsInfo = pTableMetaInfo->vgroupList; + tscDebug("%p sub:%p all data retrieved from ep:%s, vgId:%d, numOfRows:%d, orderOfSub:%d", pParentSql, pSql, + vgroupsInfo->vgroups[0].epAddr[0].fqdn, vgroupsInfo->vgroups[0].vgId, numOfRowsFromSubquery, idx); tColModelCompact(pDesc->pColumnModel, trsupport->localBuffer, pDesc->pColumnModel->capacity); diff --git a/src/client/src/tscSystem.c b/src/client/src/tscSystem.c index 4c5dbb079f..47c2d35a75 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -77,6 +77,7 @@ int32_t tscInitRpc(const char *user, const char *secretEncrypt, void **pDnodeCon return 0; } + void taos_init_imp(void) { char temp[128]; @@ -124,8 +125,9 @@ void taos_init_imp(void) { double factor = (tscEmbedded == 0)? 2.0:4.0; tscNumOfThreads = (int)(tsNumOfCores * tsNumOfThreadsPerCore / factor); - - if (tscNumOfThreads < 2) tscNumOfThreads = 2; + if (tscNumOfThreads < 2) { + tscNumOfThreads = 2; + } tscQhandle = taosInitScheduler(queueSize, tscNumOfThreads, "tsc"); if (NULL == tscQhandle) { @@ -140,7 +142,7 @@ void taos_init_imp(void) { int64_t refreshTime = 10; // 10 seconds by default if (tscMetaCache == NULL) { - tscMetaCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, refreshTime, false, NULL, "tableMeta"); + tscMetaCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, refreshTime, false, tscFreeTableMetaHelper, "tableMeta"); tscObjCache = taosCacheInit(TSDB_CACHE_PTR_KEY, refreshTime / 2, false, tscFreeRegisteredSqlObj, "sqlObj"); } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 0235f037bd..9e861d3f28 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -115,7 +115,7 @@ bool tscIsTwoStageSTableQuery(SQueryInfo* pQueryInfo, int32_t tableIndex) { // for select query super table, the super table vgroup list can not be null in any cases. if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { - assert(pTableMetaInfo->vgroupList != NULL); +// assert(pTableMetaInfo->vgroupList != NULL); // if retrieve vgroupInfo failed, the value may be null } if ((pQueryInfo->type & TSDB_QUERY_TYPE_FREE_RESOURCE) == TSDB_QUERY_TYPE_FREE_RESOURCE) { @@ -408,6 +408,24 @@ void tscFreeRegisteredSqlObj(void *pSql) { } } +void tscFreeTableMetaHelper(void *pTableMeta) { + STableMeta* p = (STableMeta*) pTableMeta; + + int32_t numOfEps = p->vgroupInfo.numOfEps; + assert(numOfEps >= 0 && numOfEps <= TSDB_MAX_REPLICA); + + for(int32_t i = 0; i < numOfEps; ++i) { + taosTFree(p->vgroupInfo.epAddr[i].fqdn); + } + + int32_t numOfEps1 = p->corVgroupInfo.numOfEps; + assert(numOfEps1 >= 0 && numOfEps1 <= TSDB_MAX_REPLICA); + + for(int32_t i = 0; i < numOfEps1; ++i) { + taosTFree(p->corVgroupInfo.epAddr[i].fqdn); + } +} + void tscFreeSqlObj(SSqlObj* pSql) { if (pSql == NULL || pSql->signature != pSql) { return; @@ -1682,8 +1700,14 @@ void tscClearSubqueryInfo(SSqlCmd* pCmd) { void tscFreeVgroupTableInfo(SArray* pVgroupTables) { if (pVgroupTables != NULL) { - for (size_t i = 0; i < taosArrayGetSize(pVgroupTables); i++) { + size_t num = taosArrayGetSize(pVgroupTables); + for (size_t i = 0; i < num; i++) { SVgroupTableInfo* pInfo = taosArrayGet(pVgroupTables, i); + + for(int32_t j = 0; j < pInfo->vgInfo.numOfEps; ++j) { + taosTFree(pInfo->vgInfo.epAddr[j].fqdn); + } + taosArrayDestroy(pInfo->itemList); } taosArrayDestroy(pVgroupTables); @@ -1695,6 +1719,7 @@ void clearAllTableMetaInfo(SQueryInfo* pQueryInfo, const char* address, bool rem for(int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, i); + tscFreeVgroupTableInfo(pTableMetaInfo->pVgroupTables); tscClearTableMetaInfo(pTableMetaInfo, removeFromCache); free(pTableMetaInfo); @@ -1727,13 +1752,7 @@ STableMetaInfo* tscAddTableMetaInfo(SQueryInfo* pQueryInfo, const char* name, ST pTableMetaInfo->pTableMeta = pTableMeta; if (vgroupList != NULL) { - size_t size = sizeof(SVgroupsInfo) + sizeof(SCMVgroupInfo) * vgroupList->numOfVgroups; - pTableMetaInfo->vgroupList = malloc(size); - if (pTableMetaInfo->vgroupList == NULL) { - return NULL; - } - - memcpy(pTableMetaInfo->vgroupList, vgroupList, size); + pTableMetaInfo->vgroupList = tscVgroupInfoClone(vgroupList); } pTableMetaInfo->tagColList = taosArrayInit(4, POINTER_BYTES); @@ -1762,8 +1781,7 @@ void tscClearTableMetaInfo(STableMetaInfo* pTableMetaInfo, bool removeFromCache) taosCacheRelease(tscMetaCache, (void**)&(pTableMetaInfo->pTableMeta), removeFromCache); } - taosTFree(pTableMetaInfo->vgroupList); - + pTableMetaInfo->vgroupList = tscVgroupInfoClear(pTableMetaInfo->vgroupList); tscColumnListDestroy(pTableMetaInfo->tagColList); pTableMetaInfo->tagColList = NULL; } @@ -2403,3 +2421,58 @@ void tscClearSqlOwner(SSqlObj* pSql) { assert(taosCheckPthreadValid(pSql->owner)); atomic_store_64(&pSql->owner, 0); } + +SVgroupsInfo* tscVgroupInfoClone(SVgroupsInfo *vgroupList) { + if (vgroupList == NULL) { + return NULL; + } + + size_t size = sizeof(SVgroupsInfo) + sizeof(SCMVgroupInfo) * vgroupList->numOfVgroups; + SVgroupsInfo* pNew = calloc(1, size); + if (pNew == NULL) { + return NULL; + } + + pNew->numOfVgroups = vgroupList->numOfVgroups; + + for(int32_t i = 0; i < vgroupList->numOfVgroups; ++i) { + SCMVgroupInfo* pNewVInfo = &pNew->vgroups[i]; + + SCMVgroupInfo* pvInfo = &vgroupList->vgroups[i]; + pNewVInfo->vgId = pvInfo->vgId; + pNewVInfo->numOfEps = pvInfo->numOfEps; + + for(int32_t j = 0; j < pvInfo->numOfEps; ++j) { + pNewVInfo->epAddr[j].fqdn = strdup(pvInfo->epAddr[j].fqdn); + pNewVInfo->epAddr[j].port = pvInfo->epAddr[j].port; + } + } + + return pNew; +} + +void* tscVgroupInfoClear(SVgroupsInfo *vgroupList) { + if (vgroupList == NULL) { + return NULL; + } + + for(int32_t i = 0; i < vgroupList->numOfVgroups; ++i) { + SCMVgroupInfo* pVgroupInfo = &vgroupList->vgroups[i]; + + for(int32_t j = 0; j < pVgroupInfo->numOfEps; ++j) { + taosTFree(pVgroupInfo->epAddr[j].fqdn); + } + } + + taosTFree(vgroupList); + return NULL; +} + +void tscSCMVgroupInfoCopy(SCMVgroupInfo* dst, const SCMVgroupInfo* src) { + dst->vgId = src->vgId; + dst->numOfEps = src->numOfEps; + for(int32_t i = 0; i < dst->numOfEps; ++i) { + dst->epAddr[i].port = src->epAddr[i].port; + dst->epAddr[i].fqdn = strdup(src->epAddr[i].fqdn); + } +} diff --git a/src/mnode/src/mnodeTable.c b/src/mnode/src/mnodeTable.c index 9965047a99..143153be49 100644 --- a/src/mnode/src/mnodeTable.c +++ b/src/mnode/src/mnodeTable.c @@ -1472,7 +1472,7 @@ static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) { int32_t numOfTable = htonl(pInfo->numOfTables); // reserve space - int32_t contLen = sizeof(SCMSTableVgroupRspMsg) + 32 * sizeof(SCMVgroupInfo) + sizeof(SVgroupsMsg); + int32_t contLen = sizeof(SCMSTableVgroupRspMsg) + 32 * sizeof(SCMVgroupMsg) + sizeof(SVgroupsMsg); for (int32_t i = 0; i < numOfTable; ++i) { char *stableName = (char*)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_FNAME_LEN) * i; SSuperTableObj *pTable = mnodeGetSuperTable(stableName); @@ -1521,6 +1521,8 @@ static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) { if (pVgroup == NULL) continue; pVgroupMsg->vgroups[vgSize].vgId = htonl(pVgroup->vgId); + pVgroupMsg->vgroups[vgSize].numOfEps = 0; + for (int32_t vn = 0; vn < pVgroup->numOfVnodes; ++vn) { SDnodeObj *pDnode = pVgroup->vnodeGid[vn].pDnode; if (pDnode == NULL) break; From 67ffdd17bef41ef50157bd986aaaa216d6f96b4e Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 19 Oct 2020 23:32:04 +0800 Subject: [PATCH 051/101] [td-1747] --- src/mnode/src/mnodeTable.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mnode/src/mnodeTable.c b/src/mnode/src/mnodeTable.c index 143153be49..b72addaa97 100644 --- a/src/mnode/src/mnodeTable.c +++ b/src/mnode/src/mnodeTable.c @@ -1477,7 +1477,7 @@ static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) { char *stableName = (char*)pInfo + sizeof(SCMSTableVgroupMsg) + (TSDB_TABLE_FNAME_LEN) * i; SSuperTableObj *pTable = mnodeGetSuperTable(stableName); if (pTable != NULL && pTable->vgHash != NULL) { - contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SCMVgroupInfo) + sizeof(SVgroupsMsg)); + contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SCMVgroupMsg) + sizeof(SVgroupsMsg)); } mnodeDecTableRef(pTable); } From 10a574abd5e6dd447d59791bb2b6c6c583f72b85 Mon Sep 17 00:00:00 2001 From: Ping Xiao Date: Tue, 20 Oct 2020 09:48:00 +0800 Subject: [PATCH 052/101] [TD-1743] add test case for remove db and STable --- tests/pytest/query/removeDBAndSTable.py | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/pytest/query/removeDBAndSTable.py diff --git a/tests/pytest/query/removeDBAndSTable.py b/tests/pytest/query/removeDBAndSTable.py new file mode 100644 index 0000000000..4616c7e378 --- /dev/null +++ b/tests/pytest/query/removeDBAndSTable.py @@ -0,0 +1,70 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import taos +from util.log import tdLog +from util.cases import tdCases +from util.sql import tdSql +from util.dnodes import tdDnodes + + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + def run(self): + tdSql.prepare() + + print("==============step1") + tdSql.execute("create database db_vplu"); + tdSql.execute("use db_vplu") + tdSql.execute("CREATE table if not exists st (ts timestamp, speed int) tags(id int)") + tdSql.execute("CREATE table if not exists st_vplu (ts timestamp, speed int) tags(id int)") + + print("==============step2") + + tdSql.execute("drop table st") + + tdSql.query("show stables") + tdSql.checkRows(1) + tdSql.checkData(0, 0, "st_vplu") + + tdDnodes.stopAll() + tdDnodes.start(1) + + tdSql.execute("use db_vplu") + tdSql.query("show stables") + tdSql.checkRows(1) + tdSql.checkData(0, 0, "st_vplu") + + tdSql.execute("drop database db") + tdSql.query("show databases") + tdSql.checkRows(1) + tdSql.checkData(0, 0, "db_vplu") + + tdDnodes.stopAll() + tdDnodes.start(1) + + tdSql.query("show databases") + tdSql.checkRows(1) + tdSql.checkData(0, 0, "db_vplu") + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) From badbbe70ddf508cbee035102b04b38e99f661def Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Tue, 20 Oct 2020 09:59:58 +0800 Subject: [PATCH 053/101] [TD-1497] python connector check --- tests/examples/python/PYTHONConnectorChecker/PythonChecker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/examples/python/PYTHONConnectorChecker/PythonChecker.py b/tests/examples/python/PYTHONConnectorChecker/PythonChecker.py index d8f23d8654..d74f021ffc 100644 --- a/tests/examples/python/PYTHONConnectorChecker/PythonChecker.py +++ b/tests/examples/python/PYTHONConnectorChecker/PythonChecker.py @@ -45,6 +45,7 @@ class ConnectorChecker: print("Failure sql: %s,exception: %s" % sql,str(e)) def close(self): + print("connetion closed.") self.cl.close() self.conn.close() def createDatabase(self): @@ -66,7 +67,6 @@ class ConnectorChecker: sql = "select * from test.weather" self.executeQuery(sql) def srun(self): - print(self.host) try: self.conn = taos.connect(host=self.host,user=self.user,password=self.password) #self.conn = taos.connect(self.host,self.user,self.password) From f2a24e9a880df3591852497a05e3f861415f199c Mon Sep 17 00:00:00 2001 From: zyyang <69311263+zyyang-taosdata@users.noreply.github.com> Date: Tue, 20 Oct 2020 10:07:42 +0800 Subject: [PATCH 054/101] Update cluster-ch.md --- documentation20/webdocs/markdowndocs/cluster-ch.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/documentation20/webdocs/markdowndocs/cluster-ch.md b/documentation20/webdocs/markdowndocs/cluster-ch.md index 0e7a26eb3a..10579df79a 100644 --- a/documentation20/webdocs/markdowndocs/cluster-ch.md +++ b/documentation20/webdocs/markdowndocs/cluster-ch.md @@ -215,4 +215,6 @@ SHOW MNODES; 如果副本数为偶数,当一个vnode group里一半或超过一半的vnode不工作时,是无法从中选出master的。同理,一半或超过一半的mnode不工作时,是无法选出mnode的master的,因为存在“split brain”问题。为解决这个问题,TDengine引入了arbitrator的概念。Arbitrator模拟一个vnode或mnode在工作,但只简单的负责网络连接,不处理任何数据插入或访问。只要包含arbitrator在内,超过半数的vnode或mnode工作,那么该vnode group或mnode组就可以正常的提供数据插入或查询服务。比如对于副本数为2的情形,如果一个节点A离线,但另外一个节点B正常,而且能连接到arbitrator, 那么节点B就能正常工作。 -TDengine安装包里带有一个执行程序tarbitrator, 找任何一台Linux服务器运行它即可。该程序对系统资源几乎没有要求,只需要保证有网络连接即可。该应用的命令行参数`-p`可以指定其对外服务的端口号,缺省是6042。配置每个taosd实例时,可以在配置文件taos.cfg里将参数arbitrator设置为arbitrator的End Point。如果该参数配置了,当副本数为偶数数,系统将自动连接配置的arbitrator。 +下载最新arbitrator及之前版本的安装包,请点击[安装包下载](https://www.taosdata.com/cn/all-downloads/),在TDengine Arbitrator Linux一节中,选择适合的版本下载并安装。 + +TDengine Arbitrator安装包里带有一个执行程序tarbitrator, 找任何一台Linux服务器运行它即可。该程序对系统资源几乎没有要求,只需要保证有网络连接即可。该应用的命令行参数`-p`可以指定其对外服务的端口号,缺省是6042。配置每个taosd实例时,可以在配置文件taos.cfg里将参数arbitrator设置为arbitrator的End Point。如果该参数配置了,当副本数为偶数数,系统将自动连接配置的arbitrator。 From c9a7931bea731b0bc473ce04a2b6a59733fcadbb Mon Sep 17 00:00:00 2001 From: zyyang Date: Tue, 20 Oct 2020 10:19:02 +0800 Subject: [PATCH 055/101] update sqlSpeller --- src/connector/jdbc/pom.xml | 2 +- .../src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java | 1 - .../com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/connector/jdbc/pom.xml b/src/connector/jdbc/pom.xml index 99409fe277..3b62f66d2e 100755 --- a/src/connector/jdbc/pom.xml +++ b/src/connector/jdbc/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.taosdata.jdbc taos-jdbcdriver - 2.0.0 + 2.0.8 jar JDBCDriver https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java index e5515c24b7..c1d9d2af8e 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDatabaseMetaData.java @@ -587,7 +587,6 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData { public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException { - /** add by zyyang **********/ Statement stmt = null; if (null != conn && !conn.isClosed()) { diff --git a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java index c20e12c048..b4a79e9eba 100644 --- a/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java +++ b/tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java @@ -113,7 +113,7 @@ public class SqlSpeller { } public static String selectLastOneMonth(String dbName, String stbName) { - return "select * from " + dbName + "." + stbName + " where ts >= now - 1M"; + return "select * from " + dbName + "." + stbName + " where ts >= now - 1n"; } public static String selectLastOneYear(String dbName, String stbName) { From 6ebd50e3cfdf5bc31a9e8f081bcad877b66100cb Mon Sep 17 00:00:00 2001 From: zyyang Date: Tue, 20 Oct 2020 10:36:55 +0800 Subject: [PATCH 056/101] COPY the correct taos-jdbcdriver to target dir --- src/connector/jdbc/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connector/jdbc/CMakeLists.txt b/src/connector/jdbc/CMakeLists.txt index 7f823b97b2..2796423e4d 100644 --- a/src/connector/jdbc/CMakeLists.txt +++ b/src/connector/jdbc/CMakeLists.txt @@ -8,7 +8,7 @@ IF (TD_MVN_INSTALLED) ADD_CUSTOM_COMMAND(OUTPUT ${JDBC_CMD_NAME} POST_BUILD COMMAND mvn -Dmaven.test.skip=true install -f ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/target/taos-jdbcdriver-2.0.0-dist.jar ${LIBRARY_OUTPUT_PATH} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/target/taos-jdbcdriver-2.*-dist.jar ${LIBRARY_OUTPUT_PATH} COMMAND mvn -Dmaven.test.skip=true clean -f ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml COMMENT "build jdbc driver") ADD_CUSTOM_TARGET(${JDBC_TARGET_NAME} ALL WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} DEPENDS ${JDBC_CMD_NAME}) From 05325532ecd3913d8c589a03440f9eddf2cddc44 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 20 Oct 2020 11:01:00 +0800 Subject: [PATCH 057/101] [td-1747] --- src/client/src/tscSQLParser.c | 20 ------------- src/client/src/tscSchemaUtil.c | 2 +- src/client/src/tscServer.c | 14 +++++---- src/client/src/tscSubquery.c | 9 ------ src/dnode/src/dnodeShell.c | 14 ++++----- src/inc/dnode.h | 2 +- src/inc/taosmsg.h | 8 ++--- src/mnode/inc/mnodeDef.h | 2 +- src/mnode/src/mnodeTable.c | 53 +++++++++++++++++----------------- src/mnode/src/mnodeVgroup.c | 10 +++---- src/tsdb/src/tsdbMeta.c | 2 +- src/vnode/src/vnodeWrite.c | 2 +- 12 files changed, 56 insertions(+), 82 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 055d44ff1e..667a3dbe5a 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -5259,26 +5259,6 @@ int32_t parseCreateDBOptions(SSqlCmd* pCmd, SCreateDBInfo* pCreateDbSql) { return TSDB_CODE_SUCCESS; } -//void tscAddTimestampColumn(SQueryInfo* pQueryInfo, int16_t functionId, int16_t tableIndex) { -// // the first column not timestamp column, add it -// SSqlExpr* pExpr = NULL; -// if (tscSqlExprNumOfExprs(pQueryInfo) > 0) { -// pExpr = tscSqlExprGet(pQueryInfo, 0); -// } -// -// if (pExpr == NULL || pExpr->colInfo.colId != PRIMARYKEY_TIMESTAMP_COL_INDEX || pExpr->functionId != functionId) { -// SColumnIndex index = {tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX}; -// -// pExpr = tscSqlExprInsert(pQueryInfo, 0, functionId, &index, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, TSDB_KEYSIZE, false); -// pExpr->colInfo.flag = TSDB_COL_NORMAL; -// -// // NOTE: tag column does not add to source column list -// SColumnList ids = getColumnList(1, tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX); -// -// insertResultField(pQueryInfo, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, "ts", pExpr); -// } -//} - void addGroupInfoForSubquery(SSqlObj* pParentObj, SSqlObj* pSql, int32_t subClauseIndex, int32_t tableIndex) { SQueryInfo* pParentQueryInfo = tscGetQueryInfoDetail(&pParentObj->cmd, subClauseIndex); diff --git a/src/client/src/tscSchemaUtil.c b/src/client/src/tscSchemaUtil.c index fb9130b6c5..dfd707344c 100644 --- a/src/client/src/tscSchemaUtil.c +++ b/src/client/src/tscSchemaUtil.c @@ -163,7 +163,7 @@ STableMeta* tscCreateTableMetaFromMsg(STableMetaMsg* pTableMetaMsg, size_t* size .numOfColumns = pTableMetaMsg->numOfColumns, }; - pTableMeta->id.tid = pTableMetaMsg->sid; + pTableMeta->id.tid = pTableMetaMsg->tid; pTableMeta->id.uid = pTableMetaMsg->uid; SCMVgroupInfo* pVgroupInfo = &pTableMeta->vgroupInfo; diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index ff798a9f99..8d63eb9775 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1650,7 +1650,7 @@ int tscBuildHeartBeatMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int tscProcessTableMetaRsp(SSqlObj *pSql) { STableMetaMsg *pMetaMsg = (STableMetaMsg *)pSql->res.pRsp; - pMetaMsg->sid = htonl(pMetaMsg->sid); + pMetaMsg->tid = htonl(pMetaMsg->tid); pMetaMsg->sversion = htons(pMetaMsg->sversion); pMetaMsg->tversion = htons(pMetaMsg->tversion); pMetaMsg->vgroup.vgId = htonl(pMetaMsg->vgroup.vgId); @@ -1660,9 +1660,9 @@ int tscProcessTableMetaRsp(SSqlObj *pSql) { pMetaMsg->numOfColumns = htons(pMetaMsg->numOfColumns); if ((pMetaMsg->tableType != TSDB_SUPER_TABLE) && - (pMetaMsg->sid <= 0 || pMetaMsg->vgroup.vgId < 2 || pMetaMsg->vgroup.numOfEps <= 0)) { + (pMetaMsg->tid <= 0 || pMetaMsg->vgroup.vgId < 2 || pMetaMsg->vgroup.numOfEps <= 0)) { tscError("invalid value in table numOfEps:%d, vgId:%d tid:%d, name:%s", pMetaMsg->vgroup.numOfEps, pMetaMsg->vgroup.vgId, - pMetaMsg->sid, pMetaMsg->tableId); + pMetaMsg->tid, pMetaMsg->tableId); return TSDB_CODE_TSC_INVALID_VALUE; } @@ -1845,8 +1845,10 @@ int tscProcessSTableVgroupRsp(SSqlObj *pSql) { SVgroupsMsg * pVgroupMsg = (SVgroupsMsg *) pMsg; pVgroupMsg->numOfVgroups = htonl(pVgroupMsg->numOfVgroups); - size_t size = sizeof(SCMVgroupInfo) * pVgroupMsg->numOfVgroups + sizeof(SVgroupsInfo); - pInfo->vgroupList = calloc(1, size); + size_t size = sizeof(SCMVgroupMsg) * pVgroupMsg->numOfVgroups + sizeof(SVgroupsMsg); + + size_t vgroupsz = sizeof(SCMVgroupInfo) * pVgroupMsg->numOfVgroups + sizeof(SVgroupsInfo); + pInfo->vgroupList = calloc(1, vgroupsz); assert(pInfo->vgroupList != NULL); pInfo->vgroupList->numOfVgroups = pVgroupMsg->numOfVgroups; @@ -1898,7 +1900,7 @@ int tscProcessShowRsp(SSqlObj *pSql) { pMetaMsg->numOfColumns = ntohs(pMetaMsg->numOfColumns); pSchema = pMetaMsg->schema; - pMetaMsg->sid = ntohs(pMetaMsg->sid); + pMetaMsg->tid = ntohs(pMetaMsg->tid); for (int i = 0; i < pMetaMsg->numOfColumns; ++i) { pSchema->bytes = htons(pSchema->bytes); pSchema++; diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index 7a2a242df5..4184d05ddc 100644 --- a/src/client/src/tscSubquery.c +++ b/src/client/src/tscSubquery.c @@ -1142,7 +1142,6 @@ static void tscRetrieveDataRes(void *param, TAOS_RES *tres, int code); static SSqlObj *tscCreateSTableSubquery(SSqlObj *pSql, SRetrieveSupport *trsupport, SSqlObj *prevSqlObj); -// TODO int32_t tscCreateJoinSubquery(SSqlObj *pSql, int16_t tableIndex, SJoinSupporter *pSupporter) { SSqlCmd * pCmd = &pSql->cmd; SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex); @@ -1298,14 +1297,6 @@ void tscHandleMasterJoinQuery(SSqlObj* pSql) { assert((pQueryInfo->type & TSDB_QUERY_TYPE_SUBQUERY) == 0); int32_t code = TSDB_CODE_SUCCESS; - - // todo add test -// SSubqueryState *pState = calloc(1, sizeof(SSubqueryState)); -// if (pState == NULL) { -// code = TSDB_CODE_TSC_OUT_OF_MEMORY; -// goto _error; -// } - pSql->subState.numOfSub = pQueryInfo->numOfTables; bool hasEmptySub = false; diff --git a/src/dnode/src/dnodeShell.c b/src/dnode/src/dnodeShell.c index a5c5d4759b..4c6c2100e0 100644 --- a/src/dnode/src/dnodeShell.c +++ b/src/dnode/src/dnodeShell.c @@ -173,15 +173,15 @@ static int dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, char return rpcRsp.code; } -void *dnodeSendCfgTableToRecv(int32_t vgId, int32_t sid) { - dDebug("vgId:%d, sid:%d send config table msg to mnode", vgId, sid); +void *dnodeSendCfgTableToRecv(int32_t vgId, int32_t tid) { + dDebug("vgId:%d, tid:%d send config table msg to mnode", vgId, tid); int32_t contLen = sizeof(SDMConfigTableMsg); SDMConfigTableMsg *pMsg = rpcMallocCont(contLen); pMsg->dnodeId = htonl(dnodeGetDnodeId()); pMsg->vgId = htonl(vgId); - pMsg->sid = htonl(sid); + pMsg->tid = htonl(tid); SRpcMsg rpcMsg = {0}; rpcMsg.pCont = pMsg; @@ -194,18 +194,18 @@ void *dnodeSendCfgTableToRecv(int32_t vgId, int32_t sid) { if (rpcRsp.code != 0) { rpcFreeCont(rpcRsp.pCont); - dError("vgId:%d, sid:%d failed to config table from mnode", vgId, sid); + dError("vgId:%d, tid:%d failed to config table from mnode", vgId, tid); return NULL; } else { - dInfo("vgId:%d, sid:%d config table msg is received", vgId, sid); + dInfo("vgId:%d, tid:%d config table msg is received", vgId, tid); // delete this after debug finished SMDCreateTableMsg *pTable = rpcRsp.pCont; int16_t numOfColumns = htons(pTable->numOfColumns); int16_t numOfTags = htons(pTable->numOfTags); - int32_t sid = htonl(pTable->sid); + int32_t tableId = htonl(pTable->tid); uint64_t uid = htobe64(pTable->uid); - dInfo("table:%s, numOfColumns:%d numOfTags:%d sid:%d uid:%" PRIu64, pTable->tableId, numOfColumns, numOfTags, sid, uid); + dInfo("table:%s, numOfColumns:%d numOfTags:%d tid:%d uid:%" PRIu64, pTable->tableId, numOfColumns, numOfTags, tableId, uid); return rpcRsp.pCont; } diff --git a/src/inc/dnode.h b/src/inc/dnode.h index 83d2a4ad9c..e84545be17 100644 --- a/src/inc/dnode.h +++ b/src/inc/dnode.h @@ -49,7 +49,7 @@ void dnodeAddClientRspHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)); void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg); void dnodeSendMsgToMnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp); void dnodeSendMsgToDnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp, SRpcEpSet *epSet); -void *dnodeSendCfgTableToRecv(int32_t vgId, int32_t sid); +void *dnodeSendCfgTableToRecv(int32_t vgId, int32_t tid); void *dnodeAllocateVnodeWqueue(void *pVnode); void dnodeFreeVnodeWqueue(void *queue); diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index 635e2aba80..2c2d686099 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -251,7 +251,7 @@ typedef struct { int8_t tableType; int16_t numOfColumns; int16_t numOfTags; - int32_t sid; + int32_t tid; int32_t sversion; int32_t tversion; int32_t tagDataLen; @@ -360,7 +360,7 @@ typedef struct { typedef struct { int32_t contLen; int32_t vgId; - int32_t sid; + int32_t tid; uint64_t uid; char tableId[TSDB_TABLE_FNAME_LEN]; } SMDDropTableMsg; @@ -699,7 +699,7 @@ typedef struct STableMetaMsg { int16_t numOfColumns; int16_t sversion; int16_t tversion; - int32_t sid; + int32_t tid; uint64_t uid; SCMVgroupMsg vgroup; SSchema schema[]; @@ -747,7 +747,7 @@ typedef struct { typedef struct { int32_t dnodeId; int32_t vgId; - int32_t sid; + int32_t tid; } SDMConfigTableMsg; typedef struct { diff --git a/src/mnode/inc/mnodeDef.h b/src/mnode/inc/mnodeDef.h index 8a2947dd18..4bc840f026 100644 --- a/src/mnode/inc/mnodeDef.h +++ b/src/mnode/inc/mnodeDef.h @@ -115,7 +115,7 @@ typedef struct { uint64_t suid; int64_t createdTime; int32_t numOfColumns; //used by normal table - int32_t sid; + int32_t tid; int32_t vgId; int32_t sqlLen; int8_t updateEnd[4]; diff --git a/src/mnode/src/mnodeTable.c b/src/mnode/src/mnodeTable.c index b72addaa97..0a81b47c9b 100644 --- a/src/mnode/src/mnodeTable.c +++ b/src/mnode/src/mnodeTable.c @@ -303,7 +303,7 @@ static int32_t mnodeChildTableActionRestored() { SVgObj *pVgroup = mnodeGetVgroup(pTable->vgId); if (pVgroup == NULL) { - mError("ctable:%s, failed to get vgId:%d sid:%d, discard it", pTable->info.tableId, pTable->vgId, pTable->sid); + mError("ctable:%s, failed to get vgId:%d tid:%d, discard it", pTable->info.tableId, pTable->vgId, pTable->tid); pTable->vgId = 0; SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb}; sdbDeleteRow(&desc); @@ -314,7 +314,7 @@ static int32_t mnodeChildTableActionRestored() { if (strcmp(pVgroup->dbName, pDb->name) != 0) { mError("ctable:%s, db:%s not match with vgId:%d db:%s sid:%d, discard it", - pTable->info.tableId, pDb->name, pTable->vgId, pVgroup->dbName, pTable->sid); + pTable->info.tableId, pDb->name, pTable->vgId, pVgroup->dbName, pTable->tid); pTable->vgId = 0; SSdbOper desc = {.type = SDB_OPER_LOCAL, .pObj = pTable, .table = tsChildTableSdb}; sdbDeleteRow(&desc); @@ -771,8 +771,8 @@ static int32_t mnodeProcessDropTableMsg(SMnodeMsg *pMsg) { return mnodeProcessDropSuperTableMsg(pMsg); } else { SChildTableObj *pCTable = (SChildTableObj *)pMsg->pTable; - mInfo("app:%p:%p, table:%s, start to drop ctable, vgId:%d sid:%d uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg, - pDrop->tableId, pCTable->vgId, pCTable->sid, pCTable->uid); + mInfo("app:%p:%p, table:%s, start to drop ctable, vgId:%d tid:%d uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg, + pDrop->tableId, pCTable->vgId, pCTable->tid, pCTable->uid); return mnodeProcessDropChildTableMsg(pMsg); } } @@ -1479,6 +1479,7 @@ static int32_t mnodeProcessSuperTableVgroupMsg(SMnodeMsg *pMsg) { if (pTable != NULL && pTable->vgHash != NULL) { contLen += (taosHashGetSize(pTable->vgHash) * sizeof(SCMVgroupMsg) + sizeof(SVgroupsMsg)); } + mnodeDecTableRef(pTable); } @@ -1593,7 +1594,7 @@ static void *mnodeBuildCreateChildTableMsg(SCMCreateTableMsg *pMsg, SChildTableO pCreate->vgId = htonl(pTable->vgId); pCreate->tableType = pTable->info.type; pCreate->createdTime = htobe64(pTable->createdTime); - pCreate->sid = htonl(pTable->sid); + pCreate->tid = htonl(pTable->tid); pCreate->sqlDataLen = htonl(pTable->sqlLen); pCreate->uid = htobe64(pTable->uid); @@ -1642,7 +1643,7 @@ static int32_t mnodeDoCreateChildTableFp(SMnodeMsg *pMsg) { assert(pTable); mDebug("app:%p:%p, table:%s, created in mnode, vgId:%d sid:%d, uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg, - pTable->info.tableId, pTable->vgId, pTable->sid, pTable->uid); + pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid); SCMCreateTableMsg *pCreate = pMsg->rpcMsg.pCont; SMDCreateTableMsg *pMDCreate = mnodeBuildCreateChildTableMsg(pCreate, pTable); @@ -1684,7 +1685,7 @@ static int32_t mnodeDoCreateChildTableCb(SMnodeMsg *pMsg, int32_t code) { return TSDB_CODE_MND_ACTION_IN_PROGRESS; } else { mError("app:%p:%p, table:%s, failed to create table sid:%d, uid:%" PRIu64 ", reason:%s", pMsg->rpcMsg.ahandle, pMsg, - pTable->info.tableId, pTable->sid, pTable->uid, tstrerror(code)); + pTable->info.tableId, pTable->tid, pTable->uid, tstrerror(code)); SSdbOper desc = {.type = SDB_OPER_GLOBAL, .pObj = pTable, .table = tsChildTableSdb}; sdbDeleteRow(&desc); return code; @@ -1708,7 +1709,7 @@ static int32_t mnodeDoCreateChildTable(SMnodeMsg *pMsg, int32_t tid) { pTable->info.tableId = strdup(pCreate->tableId); pTable->createdTime = taosGetTimestampMs(); - pTable->sid = tid; + pTable->tid = tid; pTable->vgId = pVgroup->vgId; if (pTable->info.type == TSDB_CHILD_TABLE) { @@ -1722,7 +1723,7 @@ static int32_t mnodeDoCreateChildTable(SMnodeMsg *pMsg, int32_t tid) { } pTable->suid = pMsg->pSTable->uid; - pTable->uid = (((uint64_t)pTable->vgId) << 48) + ((((uint64_t)pTable->sid) & ((1ul << 24) - 1ul)) << 24) + + pTable->uid = (((uint64_t)pTable->vgId) << 48) + ((((uint64_t)pTable->tid) & ((1ul << 24) - 1ul)) << 24) + ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul)); pTable->superTable = pMsg->pSTable; } else { @@ -1730,7 +1731,7 @@ static int32_t mnodeDoCreateChildTable(SMnodeMsg *pMsg, int32_t tid) { int64_t us = taosGetTimestampUs(); pTable->uid = (us << 24) + ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul)); } else { - pTable->uid = (((uint64_t)pTable->vgId) << 48) + ((((uint64_t)pTable->sid) & ((1ul << 24) - 1ul)) << 24) + + pTable->uid = (((uint64_t)pTable->vgId) << 48) + ((((uint64_t)pTable->tid) & ((1ul << 24) - 1ul)) << 24) + ((sdbGetVersion() & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul)); } @@ -1787,7 +1788,7 @@ static int32_t mnodeDoCreateChildTable(SMnodeMsg *pMsg, int32_t tid) { tstrerror(code)); } else { mDebug("app:%p:%p, table:%s, allocated in vgroup, vgId:%d sid:%d uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg, - pTable->info.tableId, pVgroup->vgId, pTable->sid, pTable->uid); + pTable->info.tableId, pVgroup->vgId, pTable->tid, pTable->uid); } return code; @@ -1805,8 +1806,8 @@ static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg) { if (pMsg->retry == 0) { if (pMsg->pTable == NULL) { SVgObj *pVgroup = NULL; - int32_t sid = 0; - code = mnodeGetAvailableVgroup(pMsg, &pVgroup, &sid); + int32_t tid = 0; + code = mnodeGetAvailableVgroup(pMsg, &pVgroup, &tid); if (code != TSDB_CODE_SUCCESS) { mDebug("app:%p:%p, table:%s, failed to get available vgroup, reason:%s", pMsg->rpcMsg.ahandle, pMsg, pCreate->tableId, tstrerror(code)); @@ -1820,7 +1821,7 @@ static int32_t mnodeProcessCreateChildTableMsg(SMnodeMsg *pMsg) { pMsg->pVgroup = pVgroup; mnodeIncVgroupRef(pVgroup); - return mnodeDoCreateChildTable(pMsg, sid); + return mnodeDoCreateChildTable(pMsg, tid); } } else { if (pMsg->pTable == NULL) pMsg->pTable = mnodeGetTable(pCreate->tableId); @@ -1850,13 +1851,13 @@ static int32_t mnodeSendDropChildTableMsg(SMnodeMsg *pMsg, bool needReturn) { tstrncpy(pDrop->tableId, pTable->info.tableId, TSDB_TABLE_FNAME_LEN); pDrop->vgId = htonl(pTable->vgId); pDrop->contLen = htonl(sizeof(SMDDropTableMsg)); - pDrop->sid = htonl(pTable->sid); + pDrop->tid = htonl(pTable->tid); pDrop->uid = htobe64(pTable->uid); SRpcEpSet epSet = mnodeGetEpSetFromVgroup(pMsg->pVgroup); mInfo("app:%p:%p, ctable:%s, send drop ctable msg, vgId:%d sid:%d uid:%" PRIu64, pMsg->rpcMsg.ahandle, pMsg, - pDrop->tableId, pTable->vgId, pTable->sid, pTable->uid); + pDrop->tableId, pTable->vgId, pTable->tid, pTable->uid); SRpcMsg rpcMsg = { .ahandle = pMsg, @@ -2095,7 +2096,7 @@ static int32_t mnodeDoGetChildTableMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta) { SChildTableObj *pTable = (SChildTableObj *)pMsg->pTable; pMeta->uid = htobe64(pTable->uid); - pMeta->sid = htonl(pTable->sid); + pMeta->tid = htonl(pTable->tid); pMeta->precision = pDb->cfg.precision; pMeta->tableType = pTable->info.type; tstrncpy(pMeta->tableId, pTable->info.tableId, TSDB_TABLE_FNAME_LEN); @@ -2135,7 +2136,7 @@ static int32_t mnodeDoGetChildTableMeta(SMnodeMsg *pMsg, STableMetaMsg *pMeta) { pMeta->vgroup.vgId = htonl(pMsg->pVgroup->vgId); mDebug("app:%p:%p, table:%s, uid:%" PRIu64 " table meta is retrieved, vgId:%d sid:%d", pMsg->rpcMsg.ahandle, pMsg, - pTable->info.tableId, pTable->uid, pTable->vgId, pTable->sid); + pTable->info.tableId, pTable->uid, pTable->vgId, pTable->tid); return TSDB_CODE_SUCCESS; } @@ -2283,11 +2284,11 @@ static void mnodeDropAllChildTablesInStable(SSuperTableObj *pStable) { } #if 0 -static SChildTableObj* mnodeGetTableByPos(int32_t vnode, int32_t sid) { +static SChildTableObj* mnodeGetTableByPos(int32_t vnode, int32_t tid) { SVgObj *pVgroup = mnodeGetVgroup(vnode); if (pVgroup == NULL) return NULL; - SChildTableObj *pTable = pVgroup->tableList[sid - 1]; + SChildTableObj *pTable = pVgroup->tableList[tid - 1]; mnodeIncTableRef((STableObj *)pTable); mnodeDecVgroupRef(pVgroup); @@ -2335,12 +2336,12 @@ static void mnodeProcessDropChildTableRsp(SRpcMsg *rpcMsg) { assert(pTable); mInfo("app:%p:%p, table:%s, drop table rsp received, vgId:%d sid:%d uid:%" PRIu64 ", thandle:%p result:%s", - mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->sid, pTable->uid, + mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid, mnodeMsg->rpcMsg.handle, tstrerror(rpcMsg->code)); if (rpcMsg->code != TSDB_CODE_SUCCESS) { mError("app:%p:%p, table:%s, failed to drop in dnode, vgId:%d sid:%d uid:%" PRIu64 ", reason:%s", - mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->sid, pTable->uid, + mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid, tstrerror(rpcMsg->code)); dnodeSendRpcMnodeWriteRsp(mnodeMsg, rpcMsg->code); return; @@ -2378,7 +2379,7 @@ static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) { // If the table is deleted by another thread during creation, stop creating and send drop msg to vnode if (sdbCheckRowDeleted(tsChildTableSdb, pTable)) { mDebug("app:%p:%p, table:%s, create table rsp received, but a deleting opertion incoming, vgId:%d sid:%d uid:%" PRIu64, - mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->sid, pTable->uid); + mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid); // if the vgroup is already dropped from hash, it can't be accquired by pTable->vgId // so the refCount of vgroup can not be decreased @@ -2413,13 +2414,13 @@ static void mnodeProcessCreateChildTableRsp(SRpcMsg *rpcMsg) { if (mnodeMsg->retry++ < 10) { mDebug("app:%p:%p, table:%s, create table rsp received, need retry, times:%d vgId:%d sid:%d uid:%" PRIu64 " result:%s thandle:%p", - mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, mnodeMsg->retry, pTable->vgId, pTable->sid, + mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, mnodeMsg->retry, pTable->vgId, pTable->tid, pTable->uid, tstrerror(rpcMsg->code), mnodeMsg->rpcMsg.handle); dnodeDelayReprocessMnodeWriteMsg(mnodeMsg); } else { mError("app:%p:%p, table:%s, failed to create in dnode, vgId:%d sid:%d uid:%" PRIu64 ", result:%s thandle:%p", - mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->sid, pTable->uid, + mnodeMsg->rpcMsg.ahandle, mnodeMsg, pTable->info.tableId, pTable->vgId, pTable->tid, pTable->uid, tstrerror(rpcMsg->code), mnodeMsg->rpcMsg.handle); SSdbOper oper = {.type = SDB_OPER_GLOBAL, .table = tsChildTableSdb, .pObj = pTable}; @@ -2674,7 +2675,7 @@ static int32_t mnodeRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows // tid pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - *(int32_t*) pWrite = pTable->sid; + *(int32_t*) pWrite = pTable->tid; cols++; //vgid diff --git a/src/mnode/src/mnodeVgroup.c b/src/mnode/src/mnodeVgroup.c index 28e4d17920..5084f1276a 100644 --- a/src/mnode/src/mnodeVgroup.c +++ b/src/mnode/src/mnodeVgroup.c @@ -793,12 +793,12 @@ static int32_t mnodeRetrieveVgroups(SShowObj *pShow, char *data, int32_t rows, v void mnodeAddTableIntoVgroup(SVgObj *pVgroup, SChildTableObj *pTable) { int32_t idPoolSize = taosIdPoolMaxSize(pVgroup->idPool); - if (pTable->sid > idPoolSize) { + if (pTable->tid > idPoolSize) { mnodeAllocVgroupIdPool(pVgroup); } - if (pTable->sid >= 1) { - taosIdPoolMarkStatus(pVgroup->idPool, pTable->sid); + if (pTable->tid >= 1) { + taosIdPoolMarkStatus(pVgroup->idPool, pTable->tid); pVgroup->numOfTables++; // The create vgroup message may be received later than the create table message // and the writing order in sdb is therefore uncertain @@ -808,8 +808,8 @@ void mnodeAddTableIntoVgroup(SVgObj *pVgroup, SChildTableObj *pTable) { } void mnodeRemoveTableFromVgroup(SVgObj *pVgroup, SChildTableObj *pTable) { - if (pTable->sid >= 1) { - taosFreeId(pVgroup->idPool, pTable->sid); + if (pTable->tid >= 1) { + taosFreeId(pVgroup->idPool, pTable->tid); pVgroup->numOfTables--; // The create vgroup message may be received later than the create table message // and the writing order in sdb is therefore uncertain diff --git a/src/tsdb/src/tsdbMeta.c b/src/tsdb/src/tsdbMeta.c index 564d7f5db5..f3bd91f038 100644 --- a/src/tsdb/src/tsdbMeta.c +++ b/src/tsdb/src/tsdbMeta.c @@ -239,7 +239,7 @@ STableCfg *tsdbCreateTableCfgFromMsg(SMDCreateTableMsg *pMsg) { return NULL; } - if (tsdbInitTableCfg(pCfg, pMsg->tableType, htobe64(pMsg->uid), htonl(pMsg->sid)) < 0) goto _err; + if (tsdbInitTableCfg(pCfg, pMsg->tableType, htobe64(pMsg->uid), htonl(pMsg->tid)) < 0) goto _err; if (tdInitTSchemaBuilder(&schemaBuilder, htonl(pMsg->sversion)) < 0) { terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; goto _err; diff --git a/src/vnode/src/vnodeWrite.c b/src/vnode/src/vnodeWrite.c index 70b08b9669..084e72a734 100644 --- a/src/vnode/src/vnodeWrite.c +++ b/src/vnode/src/vnodeWrite.c @@ -149,7 +149,7 @@ static int32_t vnodeProcessDropTableMsg(SVnodeObj *pVnode, void *pCont, SRspRet int32_t code = TSDB_CODE_SUCCESS; vDebug("vgId:%d, table:%s, start to drop", pVnode->vgId, pTable->tableId); - STableId tableId = {.uid = htobe64(pTable->uid), .tid = htonl(pTable->sid)}; + STableId tableId = {.uid = htobe64(pTable->uid), .tid = htonl(pTable->tid)}; if (tsdbDropTable(pVnode->tsdb, tableId) < 0) code = terrno; From f55c46c8e98156e29466e830cd65abb9f9f513c0 Mon Sep 17 00:00:00 2001 From: zyyang Date: Tue, 20 Oct 2020 11:15:16 +0800 Subject: [PATCH 058/101] package JdbcChecker --- tests/examples/JDBC/JDBCDemo/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/examples/JDBC/JDBCDemo/pom.xml b/tests/examples/JDBC/JDBCDemo/pom.xml index 15db449810..1db956189e 100644 --- a/tests/examples/JDBC/JDBCDemo/pom.xml +++ b/tests/examples/JDBC/JDBCDemo/pom.xml @@ -23,7 +23,7 @@ - com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo + com.taosdata.example.JdbcChecker From 959c3405a05f74dfa43d026db66f9084eef6da4a Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 20 Oct 2020 11:41:35 +0800 Subject: [PATCH 059/101] [td-1756] add error check --- src/client/src/tscSQLParser.c | 2 +- src/query/inc/qSqlparser.h | 2 -- src/query/src/qParserImpl.c | 27 +++++---------------------- 3 files changed, 6 insertions(+), 25 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 667a3dbe5a..aaf0450da3 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -190,7 +190,7 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { SSqlRes* pRes = &pSql->res; int32_t code = TSDB_CODE_SUCCESS; - if (!pInfo->valid) { + if (!pInfo->valid || errno == TSDB_CODE_TSC_SQL_SYNTAX_ERROR) { return tscSQLSyntaxErrMsg(tscGetErrorMsgPayload(pCmd), NULL, pInfo->pzErrMsg); } diff --git a/src/query/inc/qSqlparser.h b/src/query/inc/qSqlparser.h index d6664577a3..bc8f9a5e23 100644 --- a/src/query/inc/qSqlparser.h +++ b/src/query/inc/qSqlparser.h @@ -276,8 +276,6 @@ tSQLExpr *tSQLExprNodeClone(tSQLExpr *pExpr); SAlterTableSQL *tAlterTableSQLElems(SStrToken *pMeterName, tFieldList *pCols, tVariantList *pVals, int32_t type); -tSQLExprListList *tSQLListListAppend(tSQLExprListList *pList, tSQLExprList *pExprList); - void destroyAllSelectClause(SSubclauseInfo *pSql); void doDestroyQuerySql(SQuerySQL *pSql); diff --git a/src/query/src/qParserImpl.c b/src/query/src/qParserImpl.c index 33237a58c2..c42a643ab5 100644 --- a/src/query/src/qParserImpl.c +++ b/src/query/src/qParserImpl.c @@ -130,13 +130,15 @@ tSQLExpr *tSQLExprIdValueCreate(SStrToken *pToken, int32_t optrType) { tVariantCreate(&pSQLExpr->val, pToken); pSQLExpr->nSQLOptr = optrType; } else if (optrType == TK_NOW) { - // default use microsecond + // use microsecond by default pSQLExpr->val.i64Key = taosGetTimestamp(TSDB_TIME_PRECISION_MICRO); pSQLExpr->val.nType = TSDB_DATA_TYPE_BIGINT; pSQLExpr->nSQLOptr = TK_TIMESTAMP; // TK_TIMESTAMP used to denote the time value is in microsecond } else if (optrType == TK_VARIABLE) { int32_t ret = parseAbsoluteDuration(pToken->z, pToken->n, &pSQLExpr->val.i64Key); - UNUSED(ret); + if (ret != TSDB_CODE_SUCCESS) { + errno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; + } pSQLExpr->val.nType = TSDB_DATA_TYPE_BIGINT; pSQLExpr->nSQLOptr = TK_TIMESTAMP; @@ -148,6 +150,7 @@ tSQLExpr *tSQLExprIdValueCreate(SStrToken *pToken, int32_t optrType) { pSQLExpr->nSQLOptr = optrType; } + return pSQLExpr; } @@ -532,26 +535,6 @@ SQuerySQL *tSetQuerySQLElems(SStrToken *pSelectToken, tSQLExprList *pSelection, return pQuery; } -tSQLExprListList *tSQLListListAppend(tSQLExprListList *pList, tSQLExprList *pExprList) { - if (pList == NULL) pList = calloc(1, sizeof(tSQLExprListList)); - - if (pList->nAlloc <= pList->nList) { // - pList->nAlloc = (pList->nAlloc << 1) + 4; - pList->a = realloc(pList->a, pList->nAlloc * sizeof(pList->a[0])); - if (pList->a == 0) { - pList->nList = pList->nAlloc = 0; - return pList; - } - } - assert(pList->a != 0); - - if (pExprList) { - pList->a[pList->nList++] = pExprList; - } - - return pList; -} - void doDestroyQuerySql(SQuerySQL *pQuerySql) { if (pQuerySql == NULL) { return; From d030cd2338db0a724c06fef1ae60781f18f19a4e Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 20 Oct 2020 11:50:29 +0800 Subject: [PATCH 060/101] [td-1756] add test cases. --- tests/script/general/parser/constCol.sim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/script/general/parser/constCol.sim b/tests/script/general/parser/constCol.sim index 13b4455779..8ffb263ecc 100644 --- a/tests/script/general/parser/constCol.sim +++ b/tests/script/general/parser/constCol.sim @@ -352,5 +352,7 @@ print ======================udc with normal column group by sql_error select from t1 sql_error select abc from t1 sql_error select abc as tu from t1 +sql_error select * from t1 where ts>now-1y +sql_error select * from t1 where ts>now-1n system sh/exec.sh -n dnode1 -s stop -x SIGINT From ef419e4c4b1b007124236c74dc3afa5321d13aeb Mon Sep 17 00:00:00 2001 From: zyyang Date: Tue, 20 Oct 2020 12:38:37 +0800 Subject: [PATCH 061/101] update taos-jdbcdriver version --- src/connector/jdbc/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connector/jdbc/CMakeLists.txt b/src/connector/jdbc/CMakeLists.txt index 2796423e4d..c565853ab0 100644 --- a/src/connector/jdbc/CMakeLists.txt +++ b/src/connector/jdbc/CMakeLists.txt @@ -8,7 +8,7 @@ IF (TD_MVN_INSTALLED) ADD_CUSTOM_COMMAND(OUTPUT ${JDBC_CMD_NAME} POST_BUILD COMMAND mvn -Dmaven.test.skip=true install -f ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/target/taos-jdbcdriver-2.*-dist.jar ${LIBRARY_OUTPUT_PATH} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/target/taos-jdbcdriver-2.0.8-dist.jar ${LIBRARY_OUTPUT_PATH} COMMAND mvn -Dmaven.test.skip=true clean -f ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml COMMENT "build jdbc driver") ADD_CUSTOM_TARGET(${JDBC_TARGET_NAME} ALL WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} DEPENDS ${JDBC_CMD_NAME}) From 84757a24123e9b1c9b1b8f7b27ab2a2ab0f195e7 Mon Sep 17 00:00:00 2001 From: zyyang Date: Tue, 20 Oct 2020 13:00:41 +0800 Subject: [PATCH 062/101] update jdbc driver version --- cmake/install.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/install.inc b/cmake/install.inc index c7fbd6df79..dfca758b93 100755 --- a/cmake/install.inc +++ b/cmake/install.inc @@ -31,7 +31,7 @@ ELSEIF (TD_WINDOWS) #INSTALL(TARGETS taos RUNTIME DESTINATION driver) #INSTALL(TARGETS shell RUNTIME DESTINATION .) IF (TD_MVN_INSTALLED) - INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos-jdbcdriver-2.0.0-dist.jar DESTINATION connector/jdbc) + INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos-jdbcdriver-2.0.8-dist.jar DESTINATION connector/jdbc) ENDIF () ELSEIF (TD_DARWIN) SET(TD_MAKE_INSTALL_SH "${TD_COMMUNITY_DIR}/packaging/tools/make_install.sh") From 38de20a7ede4a1b009b67b3f5b82a2bdb8849b45 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 20 Oct 2020 13:42:51 +0800 Subject: [PATCH 063/101] [td-1756] add test cases. --- tests/script/general/parser/constCol.sim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/script/general/parser/constCol.sim b/tests/script/general/parser/constCol.sim index 8ffb263ecc..7232851ae9 100644 --- a/tests/script/general/parser/constCol.sim +++ b/tests/script/general/parser/constCol.sim @@ -352,6 +352,8 @@ print ======================udc with normal column group by sql_error select from t1 sql_error select abc from t1 sql_error select abc as tu from t1 + +print ========================> td-1756 sql_error select * from t1 where ts>now-1y sql_error select * from t1 where ts>now-1n From 7c4276cb0502e6e8ec7c5137094826321f97a646 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 20 Oct 2020 13:44:05 +0800 Subject: [PATCH 064/101] [td-1752] --- src/query/src/qAst.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/query/src/qAst.c b/src/query/src/qAst.c index 63411aaf3f..893105e44a 100644 --- a/src/query/src/qAst.c +++ b/src/query/src/qAst.c @@ -646,9 +646,7 @@ static bool filterItem(tExprNode *pExpr, const void *pItem, SExprTraverseSupp *p } // handle the leaf node - assert(pLeft->nodeType == TSQL_NODE_COL && pRight->nodeType == TSQL_NODE_VALUE); param->setupInfoFn(pExpr, param->pExtInfo); - return param->nodeFilterFn(pItem, pExpr->_node.info); } @@ -769,6 +767,7 @@ void tExprTreeTraverse(tExprNode *pExpr, SSkipList *pSkipList, SArray *result, S assert(taosArrayGetSize(result) == 0); tSQLBinaryTraverseOnSkipList(pExpr, result, pSkipList, param); } + return; } From 2ed28bf703dd3d620e8c986a9f3ec26d8ac8c5ab Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 20 Oct 2020 13:49:35 +0800 Subject: [PATCH 065/101] [td-1752] add test cases. --- tests/script/general/parser/constCol.sim | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/script/general/parser/constCol.sim b/tests/script/general/parser/constCol.sim index 7232851ae9..69f4111700 100644 --- a/tests/script/general/parser/constCol.sim +++ b/tests/script/general/parser/constCol.sim @@ -357,4 +357,32 @@ print ========================> td-1756 sql_error select * from t1 where ts>now-1y sql_error select * from t1 where ts>now-1n +print ========================> td-1752 +sql select * from st2 where t2 < 200 and t2 is not null; +if $rows != 1 then + return -1 +endi + +if $data00 != @19-12-09 16:27:35.000@ then + return -1 +endi + +if $data01 != 2 then + return -1 +endi + +if $data02 != 1 then + return -1 +endi + +sql select * from st2 where t2 > 200 or t2 is null; +if $rows != 0 then + return -1 +endi + +sql select * from st2 where t2 < 200 and t2 is null; +if $rows != 0 then + return -1 +endi + system sh/exec.sh -n dnode1 -s stop -x SIGINT From 43d322c5d0a5df277ddb302ede8c3577f1bbd93c Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Tue, 20 Oct 2020 13:59:10 +0800 Subject: [PATCH 066/101] balance jenkins load --- tests/script/jenkins/basic_1.txt | 5 ++++- tests/script/jenkins/basic_2.txt | 7 +++++++ tests/script/jenkins/basic_3.txt | 9 +-------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/script/jenkins/basic_1.txt b/tests/script/jenkins/basic_1.txt index aba2ec945f..765e713916 100644 --- a/tests/script/jenkins/basic_1.txt +++ b/tests/script/jenkins/basic_1.txt @@ -199,4 +199,7 @@ ./test.sh -f unique/dnode/vnode_clean.sim ./test.sh -f unique/http/admin.sim -./test.sh -f unique/http/opentsdb.sim \ No newline at end of file +./test.sh -f unique/http/opentsdb.sim + +./test.sh -f unique/import/replica2.sim +./test.sh -f unique/import/replica3.sim diff --git a/tests/script/jenkins/basic_2.txt b/tests/script/jenkins/basic_2.txt index 166c732df7..014313fafe 100644 --- a/tests/script/jenkins/basic_2.txt +++ b/tests/script/jenkins/basic_2.txt @@ -81,3 +81,10 @@ cd ../../../debug; make ./test.sh -f unique/db/replica_reduce32.sim ./test.sh -f unique/db/replica_reduce31.sim ./test.sh -f unique/db/replica_part.sim + +./test.sh -f unique/vnode/many.sim +./test.sh -f unique/vnode/replica2_basic2.sim +./test.sh -f unique/vnode/replica2_repeat.sim +./test.sh -f unique/vnode/replica3_basic.sim +./test.sh -f unique/vnode/replica3_repeat.sim +./test.sh -f unique/vnode/replica3_vgroup.sim \ No newline at end of file diff --git a/tests/script/jenkins/basic_3.txt b/tests/script/jenkins/basic_3.txt index de5d64b984..83b10a371c 100644 --- a/tests/script/jenkins/basic_3.txt +++ b/tests/script/jenkins/basic_3.txt @@ -1,5 +1,3 @@ -./test.sh -f unique/import/replica2.sim -./test.sh -f unique/import/replica3.sim ./test.sh -f unique/stable/balance_replica1.sim ./test.sh -f unique/stable/dnode2_stop.sim @@ -21,12 +19,7 @@ ./test.sh -f unique/mnode/mgmt34.sim ./test.sh -f unique/mnode/mgmtr2.sim -./test.sh -f unique/vnode/many.sim -./test.sh -f unique/vnode/replica2_basic2.sim -./test.sh -f unique/vnode/replica2_repeat.sim -./test.sh -f unique/vnode/replica3_basic.sim -./test.sh -f unique/vnode/replica3_repeat.sim -./test.sh -f unique/vnode/replica3_vgroup.sim + ./test.sh -f general/parser/stream_on_sys.sim ./test.sh -f general/stream/metrics_del.sim From e910cae1ec5d61914f58ba0fb8fd2935a4d079f8 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 20 Oct 2020 14:06:03 +0800 Subject: [PATCH 067/101] [td-1756] --- src/query/src/qParserImpl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query/src/qParserImpl.c b/src/query/src/qParserImpl.c index c42a643ab5..7e8128f200 100644 --- a/src/query/src/qParserImpl.c +++ b/src/query/src/qParserImpl.c @@ -137,7 +137,7 @@ tSQLExpr *tSQLExprIdValueCreate(SStrToken *pToken, int32_t optrType) { } else if (optrType == TK_VARIABLE) { int32_t ret = parseAbsoluteDuration(pToken->z, pToken->n, &pSQLExpr->val.i64Key); if (ret != TSDB_CODE_SUCCESS) { - errno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; + terrno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; } pSQLExpr->val.nType = TSDB_DATA_TYPE_BIGINT; From beb6599256dd42a6dc5e285e05785f551f340771 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 20 Oct 2020 14:07:28 +0800 Subject: [PATCH 068/101] [td-1756] --- src/client/src/tscSQLParser.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index aaf0450da3..7a8be085eb 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -190,7 +190,8 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { SSqlRes* pRes = &pSql->res; int32_t code = TSDB_CODE_SUCCESS; - if (!pInfo->valid || errno == TSDB_CODE_TSC_SQL_SYNTAX_ERROR) { + if (!pInfo->valid || terrno == TSDB_CODE_TSC_SQL_SYNTAX_ERROR) { + terrno = TSDB_CODE_SUCCESS; // clear the error number return tscSQLSyntaxErrMsg(tscGetErrorMsgPayload(pCmd), NULL, pInfo->pzErrMsg); } From 9f653edacd1a0a374133876cdd79a070fbc3ad14 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 20 Oct 2020 14:08:53 +0800 Subject: [PATCH 069/101] [td-1752] add test cases. --- tests/script/general/parser/constCol.sim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/script/general/parser/constCol.sim b/tests/script/general/parser/constCol.sim index 69f4111700..7ae496f1ac 100644 --- a/tests/script/general/parser/constCol.sim +++ b/tests/script/general/parser/constCol.sim @@ -358,7 +358,7 @@ sql_error select * from t1 where ts>now-1y sql_error select * from t1 where ts>now-1n print ========================> td-1752 -sql select * from st2 where t2 < 200 and t2 is not null; +sql select * from db.st2 where t2 < 200 and t2 is not null; if $rows != 1 then return -1 endi @@ -375,7 +375,7 @@ if $data02 != 1 then return -1 endi -sql select * from st2 where t2 > 200 or t2 is null; +sql select * from db.st2 where t2 > 200 or t2 is null; if $rows != 0 then return -1 endi From e491a8908c4d7ed99076eea5f67d7f257427e4d0 Mon Sep 17 00:00:00 2001 From: Bomin Zhang Date: Tue, 20 Oct 2020 15:00:12 +0800 Subject: [PATCH 070/101] fix TD-1471 --- src/client/src/tscSql.c | 7 +- .../python/linux/python2/taos/cursor.py | 6 +- .../python/linux/python3/taos/cursor.py | 6 +- .../python/windows/python2/taos/cursor.py | 3 + .../python/windows/python3/taos/cursor.py | 3 + tests/pytest/fulltest.sh | 1 + tests/pytest/query/bug1471.py | 73 +++++++++++++++++++ 7 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 tests/pytest/query/bug1471.py diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index be91255b9c..02a2c1e052 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -479,8 +479,11 @@ TAOS_ROW taos_fetch_row(TAOS_RES *res) { SSqlCmd *pCmd = &pSql->cmd; SSqlRes *pRes = &pSql->res; - if (pRes->qhandle == 0 || - pRes->code == TSDB_CODE_TSC_QUERY_CANCELLED || + if (pRes->qhandle == 0) { + pRes->code = TSDB_CODE_TSC_INVALID_QHANDLE; + return NULL; + } + if (pRes->code == TSDB_CODE_TSC_QUERY_CANCELLED || pCmd->command == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pCmd->command == TSDB_SQL_INSERT) { return NULL; diff --git a/src/connector/python/linux/python2/taos/cursor.py b/src/connector/python/linux/python2/taos/cursor.py index 8c268d8afb..37c02d330e 100644 --- a/src/connector/python/linux/python2/taos/cursor.py +++ b/src/connector/python/linux/python2/taos/cursor.py @@ -192,8 +192,10 @@ class TDengineCursor(object): buffer = [[] for i in range(len(self._fields))] self._rowcount = 0 while True: - block, num_of_fields = CTaosInterface.fetchBlock( - self._result, self._fields) + block, num_of_fields = CTaosInterface.fetchBlock(self._result, self._fields) + errno = CTaosInterface.libtaos.taos_errno(self._result) + if errno != 0: + raise ProgrammingError(CTaosInterface.errStr(self._result), errno) if num_of_fields == 0: break self._rowcount += num_of_fields diff --git a/src/connector/python/linux/python3/taos/cursor.py b/src/connector/python/linux/python3/taos/cursor.py index 3f0f315d33..ec7a85ee1a 100644 --- a/src/connector/python/linux/python3/taos/cursor.py +++ b/src/connector/python/linux/python3/taos/cursor.py @@ -207,8 +207,10 @@ class TDengineCursor(object): buffer = [[] for i in range(len(self._fields))] self._rowcount = 0 while True: - block, num_of_fields = CTaosInterface.fetchBlock( - self._result, self._fields) + block, num_of_fields = CTaosInterface.fetchBlock(self._result, self._fields) + errno = CTaosInterface.libtaos.taos_errno(self._result) + if errno != 0: + raise ProgrammingError(CTaosInterface.errStr(self._result), errno) if num_of_fields == 0: break self._rowcount += num_of_fields diff --git a/src/connector/python/windows/python2/taos/cursor.py b/src/connector/python/windows/python2/taos/cursor.py index 7eee3bfc8f..8714fe77cb 100644 --- a/src/connector/python/windows/python2/taos/cursor.py +++ b/src/connector/python/windows/python2/taos/cursor.py @@ -142,6 +142,9 @@ class TDengineCursor(object): self._rowcount = 0 while True: block, num_of_fields = CTaosInterface.fetchBlock(self._result, self._fields) + errno = CTaosInterface.libtaos.taos_errno(self._result) + if errno != 0: + raise ProgrammingError(CTaosInterface.errStr(self._result), errno) if num_of_fields == 0: break self._rowcount += num_of_fields for i in range(len(self._fields)): diff --git a/src/connector/python/windows/python3/taos/cursor.py b/src/connector/python/windows/python3/taos/cursor.py index 5f5aa4e1d7..c2c442b06e 100644 --- a/src/connector/python/windows/python3/taos/cursor.py +++ b/src/connector/python/windows/python3/taos/cursor.py @@ -142,6 +142,9 @@ class TDengineCursor(object): self._rowcount = 0 while True: block, num_of_fields = CTaosInterface.fetchBlock(self._result, self._fields) + errno = CTaosInterface.libtaos.taos_errno(self._result) + if errno != 0: + raise ProgrammingError(CTaosInterface.errStr(self._result), errno) if num_of_fields == 0: break self._rowcount += num_of_fields for i in range(len(self._fields)): diff --git a/tests/pytest/fulltest.sh b/tests/pytest/fulltest.sh index fd973b8b76..4b27c42d38 100755 --- a/tests/pytest/fulltest.sh +++ b/tests/pytest/fulltest.sh @@ -149,6 +149,7 @@ python3 ./test.py -f query/queryNullValueTest.py python3 ./test.py -f query/queryInsertValue.py python3 ./test.py -f query/queryConnection.py python3 ./test.py -f query/natualInterval.py +python3 ./test.py -f query/bug1471.py #stream python3 ./test.py -f stream/metric_1.py diff --git a/tests/pytest/query/bug1471.py b/tests/pytest/query/bug1471.py new file mode 100644 index 0000000000..f1cb0bdcdf --- /dev/null +++ b/tests/pytest/query/bug1471.py @@ -0,0 +1,73 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import taos +from util.log import * +from util.cases import * +from util.sql import * +import time +import threading + + +class myThread(threading.Thread): + def __init__(self, conn): + threading.Thread.__init__(self) + self.event = threading.Event() + self.conn = taos.connect(conn._host, port=conn._port, config=conn._config) + + def run(self): + cur = self.conn.cursor() + self.event.wait() + cur.execute("drop database db") + cur.close() + self.conn.close() + + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor()) + + def run(self): + for i in range(50): + print("round", i) + thread = myThread(tdSql.cursor._connection) + thread.start() + + tdSql.execute('reset query cache') + tdSql.execute('drop database if exists db') + tdSql.execute('create database db') + tdSql.execute('use db') + tdSql.execute("create table car (ts timestamp, s int)") + tdSql.execute("insert into car values('2020-10-19 17:00:00', 123)") + + thread.event.set() + try: + tdSql.query("select s from car where ts = '2020-10-19 17:00:00'") + except Exception as e: + pass + else: + tdSql.checkData(0, 0, 123) + + thread.join() + time.sleep(0.2) + + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) From 546cdfcdad889866bc92c58fa63b828483172cb8 Mon Sep 17 00:00:00 2001 From: wangyazhou1313 Date: Tue, 20 Oct 2020 15:18:59 +0800 Subject: [PATCH 071/101] [TD-1584] add some testcases for testing before 1970s --- tests/pytest/insert/before_1970.py | 80 ++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/pytest/insert/before_1970.py diff --git a/tests/pytest/insert/before_1970.py b/tests/pytest/insert/before_1970.py new file mode 100644 index 0000000000..cb17b657aa --- /dev/null +++ b/tests/pytest/insert/before_1970.py @@ -0,0 +1,80 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import taos +from util.log import tdLog +from util.cases import tdCases +from util.sql import tdSql +from util.dnodes import tdDnodes + + +class TDTestCase: + """ + add test data before 1970s + """ + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + def run(self): + tdSql.prepare() + + print("==============step1") + tdSql.execute("create database if not exists demo keep 36500;"); + print("==============create db demo keep 365000 days") + tdSql.execute("use demo;") + tdSql.execute("CREATE table if not exists test (ts timestamp, f1 int);") + print("==============create table test") + + print("==============step2") + #TODO : should add more testcases + tdSql.execute("insert into test values('1930-12-12 01:19:20.345', 1);") + tdSql.execute("insert into test values('1969-12-30 23:59:59.999', 2);") + tdSql.execute("insert into test values(-3600, 3);") + tdSql.execute("insert into test values('2020-10-20 14:02:53.770', 4);") + print("==============insert data") + + # tdSql.query("select * from test;") + # + # tdSql.checkRows(3) + # + # tdSql.checkData(0,0,'1969-12-12 01:19:20.345000') + # tdSql.checkData(1,0,'1970-01-01 07:00:00.000000') + # tdSql.checkData(2,0,'2020-10-20 14:02:53.770000') + print("==============step3") + tdDnodes.stopAll() + tdDnodes.start(1) + print("==============restart taosd") + + + print("==============step4") + tdSql.execute("use demo;") + tdSql.query("select * from test;") + # print(tdSql.queryResult) + tdSql.checkRows(4) + tdSql.checkData(0,0,'1930-12-12 01:19:20.345000') + tdSql.checkData(1,0,'1969-12-30 23:59:59.999000') + tdSql.checkData(2,0,'1970-01-01 07:00:00.000000') + tdSql.checkData(3,0,'2020-10-20 14:02:53.770000') + print("==============check data") + + + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) From 305fe5aff8038fa9e30545eb5f96a615bee7bfcc Mon Sep 17 00:00:00 2001 From: zyyang Date: Tue, 20 Oct 2020 15:21:16 +0800 Subject: [PATCH 072/101] [feature]: add a nodejs checker for nodejs examples --- tests/examples/nodejs/nodejsChecker.js | 60 ++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/examples/nodejs/nodejsChecker.js diff --git a/tests/examples/nodejs/nodejsChecker.js b/tests/examples/nodejs/nodejsChecker.js new file mode 100644 index 0000000000..c77944f752 --- /dev/null +++ b/tests/examples/nodejs/nodejsChecker.js @@ -0,0 +1,60 @@ +const taos = require('td2.0-connector'); + + +var host = null; +var port = 6030; +for(var i = 2; i < global.process.argv.length; i++){ + var key = global.process.argv[i].split("=")[0]; + var value = global.process.argv[i].split("=")[1]; + + if("host" == key){ + host = value; + } + if("port" == key){ + port = value; + } +} + +if(host == null){ + console.log("Usage: node nodejsChecker.js host= port="); + process.exit(0); +} + +// establish connection +var conn = taos.connect({host:host, user:"root", password:"taosdata",port:port}); +var cursor = conn.cursor(); +// create database +executeSql("create database if not exists test", 0); +// use db +executeSql("use test", 0); +// drop table +executeSql("drop table if exists test.weather", 0); +// create table +executeSql("create table if not exists test.weather(ts timestamp, temperature float, humidity int)", 0); +// insert +executeSql("insert into test.weather (ts, temperature, humidity) values(now, 20.5, 34)", 1); +// select +executeQuery("select * from test.weather"); +// close connection +conn.close(); + +function executeQuery(sql){ + var start = new Date().getTime(); + var promise = cursor.query(sql, true); + var end = new Date().getTime(); + printSql(sql, promise != null,(end - start)); + promise.then(function(result){ + result.pretty(); + }); +} + +function executeSql(sql, affectRows){ + var start = new Date().getTime(); + var promise = cursor.execute(sql); + var end = new Date().getTime(); + printSql(sql, promise == affectRows, (end - start)); +} + +function printSql(sql, succeed, cost){ + console.log("[ "+(succeed ? "OK" : "ERROR!")+" ] time cost: " + cost + " ms, execute statement ====> " + sql); +} From a17b11c65e9486a82c5b1f18881d437d74ff049b Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Tue, 20 Oct 2020 15:23:52 +0800 Subject: [PATCH 073/101] Determine whether the file has changed --- Jenkinsfile | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 4410d81be6..75ae83eeb8 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,6 +4,22 @@ pipeline { WK = '/var/lib/jenkins/workspace/TDinternal' WKC= '/var/lib/jenkins/workspace/TDinternal/community' } + stages{ + stage('pre build'){ + agent{label 'master'} + steps{ + sh ''' + ${WKC} + td=`git diff develop remotes/origin/develop` + if [ ! $td ];then + echo "no changes,skip build" + exit 0 + fi + echo "check OK!" + ''' + } + } + } stages { stage('Parallel test stage') { parallel { From b3120b1b4b04192bf4bdc8edf76b34916d3e2184 Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Tue, 20 Oct 2020 15:54:41 +0800 Subject: [PATCH 074/101] Determine whether the file has changed --- Jenkinsfile | 57 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 75ae83eeb8..8a380c46df 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,29 +4,35 @@ pipeline { WK = '/var/lib/jenkins/workspace/TDinternal' WKC= '/var/lib/jenkins/workspace/TDinternal/community' } - stages{ - stage('pre build'){ - agent{label 'master'} - steps{ - sh ''' - ${WKC} - td=`git diff develop remotes/origin/develop` - if [ ! $td ];then - echo "no changes,skip build" - exit 0 - fi - echo "check OK!" - ''' - } - } - } + stages { + stage('pre build'){ + agent{label 'master'} + steps{ + sh ''' + cd ${WKC} + td=`git diff develop remotes/origin/develop` + if [ ! $td ];then + echo "no changes,skip build" + exit 0 + fi + echo "check OK!" + ''' + } + } stage('Parallel test stage') { parallel { stage('pytest') { agent{label 'master'} steps { sh ''' + cd ${WKC} + td=`git diff develop remotes/origin/develop` + if [ ! $td ];then + echo "no changes,skip build" + exit 0 + fi + date cd ${WKC} git checkout develop @@ -52,6 +58,12 @@ pipeline { agent{label '184'} steps { sh ''' + cd ${WKC} + td=`git diff develop remotes/origin/develop` + if [ ! $td ];then + echo "no changes,skip build" + exit 0 + fi date cd ${WKC} git checkout develop @@ -78,7 +90,12 @@ pipeline { agent{label "185"} steps { sh ''' - + cd ${WKC} + td=`git diff develop remotes/origin/develop` + if [ ! $td ];then + echo "no changes,skip build" + exit 0 + fi cd ${WKC} git checkout develop git pull @@ -107,6 +124,12 @@ pipeline { agent{label "186"} steps { sh ''' + cd ${WKC} + td=`git diff develop remotes/origin/develop` + if [ ! $td ];then + echo "no changes,skip build" + exit 0 + fi date cd ${WKC} git checkout develop From dd3e1a9e50d72cf254eff331d49b47d6225db2eb Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Tue, 20 Oct 2020 17:12:17 +0800 Subject: [PATCH 075/101] modify Jenkinsfile add branch --- Jenkinsfile | 43 ++++++++++--------------------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 8a380c46df..a603dec841 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -8,14 +8,11 @@ pipeline { stages { stage('pre build'){ agent{label 'master'} + when{ changeset "develop"} steps{ sh ''' - cd ${WKC} - td=`git diff develop remotes/origin/develop` - if [ ! $td ];then - echo "no changes,skip build" - exit 0 - fi + + echo "check OK!" ''' } @@ -23,16 +20,10 @@ pipeline { stage('Parallel test stage') { parallel { stage('pytest') { + when{ changeset "develop"} agent{label 'master'} steps { sh ''' - cd ${WKC} - td=`git diff develop remotes/origin/develop` - if [ ! $td ];then - echo "no changes,skip build" - exit 0 - fi - date cd ${WKC} git checkout develop @@ -55,19 +46,14 @@ pipeline { } } stage('test_b1') { + when{ changeset "develop"} agent{label '184'} steps { sh ''' - cd ${WKC} - td=`git diff develop remotes/origin/develop` - if [ ! $td ];then - echo "no changes,skip build" - exit 0 - fi - date cd ${WKC} git checkout develop git pull + git submodule update cd ${WK} git checkout develop @@ -88,17 +74,13 @@ pipeline { stage('test_crash_gen') { agent{label "185"} + when{ changeset "develop"} steps { sh ''' - cd ${WKC} - td=`git diff develop remotes/origin/develop` - if [ ! $td ];then - echo "no changes,skip build" - exit 0 - fi cd ${WKC} git checkout develop git pull + git submodule update cd ${WK} git checkout develop @@ -122,18 +104,13 @@ pipeline { stage('test_valgrind') { agent{label "186"} + when{ changeset "develop"} steps { sh ''' - cd ${WKC} - td=`git diff develop remotes/origin/develop` - if [ ! $td ];then - echo "no changes,skip build" - exit 0 - fi - date cd ${WKC} git checkout develop git pull + git submodule update cd ${WK} git checkout develop From 9673d5d3bd2577920972279d0f0b16bd4fe88846 Mon Sep 17 00:00:00 2001 From: wangyazhou1313 Date: Tue, 20 Oct 2020 17:26:13 +0800 Subject: [PATCH 076/101] [TD-1636] remove last 2 bytes of file 'wal0' --- tests/pytest/table/alter_wal0.py | 75 ++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/pytest/table/alter_wal0.py diff --git a/tests/pytest/table/alter_wal0.py b/tests/pytest/table/alter_wal0.py new file mode 100644 index 0000000000..15ad69998f --- /dev/null +++ b/tests/pytest/table/alter_wal0.py @@ -0,0 +1,75 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import taos +from util.log import tdLog +from util.cases import tdCases +from util.sql import tdSql +from util.dnodes import tdDnodes + + +class TDTestCase: + """ + remove last tow bytes of file 'wal0',then restart taosd and create new tables. + """ + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + def run(self): + tdSql.prepare() + + print("==============step1") + tdSql.execute("create database if not exists demo;"); + tdSql.execute("use demo;") + tdSql.execute("create table if not exists meters(ts timestamp, f1 int) tags(t1 int);"); + for i in range(1,11): + tdSql.execute("CREATE table if not exists test{num} using meters tags({num});".format(num=i)) + print("==============insert 10 tables") + + tdSql.query('show tables;') + tdSql.checkRows(10) + + print("==============step2") + tdDnodes.stopAll() + filename = '/var/lib/taos/mnode/wal/wal0' + + with open(filename, 'rb') as f1: + temp = f1.read() + + with open(filename, 'wb') as f2: + f2.write(temp[:-2]) + + tdDnodes.start(1) + print("==============remove last tow bytes of file 'wal0' and restart taosd") + + print("==============step3") + tdSql.execute("use demo;") + tdSql.query('show tables;') + tdSql.checkRows(10) + for i in range(11,21): + tdSql.execute("CREATE table if not exists test{num} using meters tags({num});".format(num=i)) + + tdSql.query('show tables;') + tdSql.checkRows(20) + print("==============check table numbers and create 10 tables") + + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) From 696aebfbe3b51b2a7ece65c157b70ce40ecb38c3 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 20 Oct 2020 09:53:28 +0000 Subject: [PATCH 077/101] Bump junit from 4.12 to 4.13.1 in /tests/examples/JDBC/JDBCDemo Bumps [junit](https://github.com/junit-team/junit4) from 4.12 to 4.13.1. - [Release notes](https://github.com/junit-team/junit4/releases) - [Changelog](https://github.com/junit-team/junit4/blob/main/doc/ReleaseNotes4.12.md) - [Commits](https://github.com/junit-team/junit4/compare/r4.12...r4.13.1) Signed-off-by: dependabot[bot] --- tests/examples/JDBC/JDBCDemo/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/examples/JDBC/JDBCDemo/pom.xml b/tests/examples/JDBC/JDBCDemo/pom.xml index 1db956189e..121a3b5cd6 100644 --- a/tests/examples/JDBC/JDBCDemo/pom.xml +++ b/tests/examples/JDBC/JDBCDemo/pom.xml @@ -66,7 +66,7 @@ junit junit - 4.12 + 4.13.1 test From 9fdd251873e87e106c23d74b0295db8ef5edfffd Mon Sep 17 00:00:00 2001 From: songtianyi Date: Tue, 20 Oct 2020 18:59:46 +0800 Subject: [PATCH 078/101] fix: docs and tests typos, taos_fetch_subfields --> taos_fetch_fields, taos_subfields_count --> taos_field_count --- .../tdenginedocs-cn/advanced-features/index.html | 4 ++-- documentation/tdenginedocs-cn/connector/index.html | 6 +++--- documentation/tdenginedocs-en/connector/index.html | 6 +++--- tests/examples/rust/src/bindings.rs | 6 ------ tests/examples/rust/src/subscriber.rs | 10 +++++----- 5 files changed, 13 insertions(+), 19 deletions(-) diff --git a/documentation/tdenginedocs-cn/advanced-features/index.html b/documentation/tdenginedocs-cn/advanced-features/index.html index 9a1b908cd5..b4953b4dd4 100644 --- a/documentation/tdenginedocs-cn/advanced-features/index.html +++ b/documentation/tdenginedocs-cn/advanced-features/index.html @@ -51,8 +51,8 @@ INTERVAL(1M)
  • mseconds:查询数据库更新的时间间隔,单位为毫秒。一般设置为1000毫秒。返回值为指向TDengine_SUB 结构的指针,如果返回为空,表示失败。

  • TAOS_ROW taos_consume(TAOS_SUB *tsub)

    该函数用来获取订阅的结果,用户应用程序将其置于一个无限循环语句。如果数据库有新记录到达,该API将返回该最新的记录。如果没有新的记录,该API将阻塞。如果返回值为空,说明系统出错。参数说明:

    • tsub:taos_subscribe的结构体指针。

  • void taos_unsubscribe(TAOS_SUB *tsub)

    取消订阅。应用程序退出时,务必调用该函数以避免资源泄露。

  • -
  • int taos_num_subfields(TAOS_SUB *tsub)

    获取返回的一行记录中数据包含多少列。

  • -
  • TAOS_FIELD *taos_fetch_subfields(TAOS_SUB *tsub)

    获取每列数据的属性(数据类型、名字、长度),与taos_num_subfileds配合使用,可解析返回的每行数据。

  • +
  • int taos_num_fields(TAOS_SUB *tsub)

    获取返回的一行记录中数据包含多少列。

  • +
  • TAOS_FIELD *taos_fetch_fields(TAOS_SUB *tsub)

    获取每列数据的属性(数据类型、名字、长度),与taos_num_subfileds配合使用,可解析返回的每行数据。

  • 示例代码:请看安装包中的的示范程序

    缓存 (Cache)

    TDengine采用时间驱动缓存管理策略(First-In-First-Out,FIFO),又称为写驱动的缓存管理机制。这种策略有别于读驱动的数据缓存模式(Least-Recent-Use,LRU),直接将最近写入的数据保存在系统的缓存中。当缓存达到临界值的时候,将最早的数据批量写入磁盘。一般意义上来说,对于物联网数据的使用,用户最为关心最近产生的数据,即当前状态。TDengine充分利用了这一特性,将最近到达的(当前状态)数据保存在缓存中。

    diff --git a/documentation/tdenginedocs-cn/connector/index.html b/documentation/tdenginedocs-cn/connector/index.html index 3167c1521f..34ea19813f 100644 --- a/documentation/tdenginedocs-cn/connector/index.html +++ b/documentation/tdenginedocs-cn/connector/index.html @@ -64,9 +64,9 @@

    该API用来获取最新消息,应用程序一般会将其置于一个无限循环语句中。其中参数tsub是taos_subscribe的返回值。如果数据库有新的记录,该API将返回,返回参数是一行记录。如果没有新的记录,该API将阻塞。如果返回值为空,说明系统出错,需要检查系统是否还在正常运行。

  • void taos_unsubscribe(TAOS_SUB *tsub)

    该API用于取消订阅,参数tsub是taos_subscribe的返回值。应用程序退出时,需要调用该API,否则有资源泄露。

  • -
  • int taos_num_subfields(TAOS_SUB *tsub)

    +
  • int taos_num_fields(TAOS_SUB *tsub)

    该API用来获取返回的一排数据中数据的列数

  • -
  • TAOS_FIELD *taos_fetch_subfields(TAOS_RES *res)

    +
  • TAOS_FIELD *taos_fetch_fields(TAOS_RES *res)

    该API用来获取每列数据的属性(数据类型、名字、字节数),与taos_num_subfileds配合使用,可用来解析返回的一排数据。

  • Java Connector

    @@ -259,4 +259,4 @@ conn.close() _ "taosSql" )

    taosSql驱动包内采用cgo模式,调用了TDengine的C/C++同步接口,与TDengine进行交互,因此,在数据库操作执行完成之前,客户端应用将处于阻塞状态。单个数据库连接,在同一时刻只能有一个线程调用API。客户应用可以建立多个连接,进行多线程的数据写入或查询处理。

    -

    更多使用的细节,请参考下载目录中的示例源码。

    回去 \ No newline at end of file +

    更多使用的细节,请参考下载目录中的示例源码。

    回去 diff --git a/documentation/tdenginedocs-en/connector/index.html b/documentation/tdenginedocs-en/connector/index.html index 0f9e6b4717..ea1f75ae00 100644 --- a/documentation/tdenginedocs-en/connector/index.html +++ b/documentation/tdenginedocs-en/connector/index.html @@ -72,9 +72,9 @@ The API is used to start a subscription session by given a handle. The parameter The API used to get the new data from a TDengine server. It should be put in an infinite loop. The parameter tsub is the handle returned by taos_subscribe. If new data are updated, the API will return a row of the result. Otherwise, the API is blocked until new data arrives. If NULL pointer is returned, it means an error occurs.

  • void taos_unsubscribe(TAOS_SUB *tsub) Stop a subscription session by the handle returned by taos_subscribe.

  • -
  • int taos_num_subfields(TAOS_SUB *tsub) +

  • int taos_num_fields(TAOS_SUB *tsub) The API used to get the number of fields in a row.

  • -
  • TAOS_FIELD *taos_fetch_subfields(TAOS_RES *res) +

  • TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) The API used to get the description of each column.

  • Java Connector

    @@ -351,4 +351,4 @@ promise2.then(function(result) { })

    Example

    An example of using the NodeJS connector to create a table with weather data and create and execute queries can be found here (The preferred method for using the connector)

    -

    An example of using the NodeJS connector to achieve the same things but without all the object wrappers that wrap around the data returned to achieve higher functionality can be found here

    Back \ No newline at end of file +

    An example of using the NodeJS connector to achieve the same things but without all the object wrappers that wrap around the data returned to achieve higher functionality can be found here

    Back diff --git a/tests/examples/rust/src/bindings.rs b/tests/examples/rust/src/bindings.rs index b93e833ba0..fc13647130 100644 --- a/tests/examples/rust/src/bindings.rs +++ b/tests/examples/rust/src/bindings.rs @@ -308,12 +308,6 @@ extern "C" { extern "C" { pub fn taos_unsubscribe(tsub: *mut ::std::os::raw::c_void); } -extern "C" { - pub fn taos_subfields_count(tsub: *mut ::std::os::raw::c_void) -> ::std::os::raw::c_int; -} -extern "C" { - pub fn taos_fetch_subfields(tsub: *mut ::std::os::raw::c_void) -> *mut TAOS_FIELD; -} extern "C" { pub fn taos_open_stream( taos: *mut ::std::os::raw::c_void, diff --git a/tests/examples/rust/src/subscriber.rs b/tests/examples/rust/src/subscriber.rs index b6812d7b6e..78c6f5cd8d 100644 --- a/tests/examples/rust/src/subscriber.rs +++ b/tests/examples/rust/src/subscriber.rs @@ -37,16 +37,16 @@ impl Subscriber { println!("subscribed to {} user:{}, db:{}, tb:{}, time:{}, mseconds:{}", host, username, db, table, time, mseconds); - let mut fields = taos_fetch_subfields(tsub); + let mut fields = taos_fetch_fields(tsub); if fields.is_null() { taos_unsubscribe(tsub); - return Err("fetch subfields error") + return Err("fetch fields error") } - let fcount = taos_subfields_count(tsub); + let fcount = taos_field_count(tsub); if fcount == 0 { taos_unsubscribe(tsub); - return Err("subfields count is 0") + return Err("fields count is 0") } Ok(Subscriber{tsub, fields, fcount}) @@ -74,4 +74,4 @@ impl Drop for Subscriber { fn drop(&mut self) { unsafe {taos_unsubscribe(self.tsub);} } -} \ No newline at end of file +} From 74c1842e389ef33b28f48bf8da1452b43fbc0d6f Mon Sep 17 00:00:00 2001 From: Jeff Tao Date: Wed, 21 Oct 2020 08:05:48 +0800 Subject: [PATCH 079/101] Update cluster-ch.md --- documentation20/webdocs/markdowndocs/cluster-ch.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/documentation20/webdocs/markdowndocs/cluster-ch.md b/documentation20/webdocs/markdowndocs/cluster-ch.md index 10579df79a..10c28c284c 100644 --- a/documentation20/webdocs/markdowndocs/cluster-ch.md +++ b/documentation20/webdocs/markdowndocs/cluster-ch.md @@ -213,8 +213,6 @@ SHOW MNODES; ## Arbitrator的使用 -如果副本数为偶数,当一个vnode group里一半或超过一半的vnode不工作时,是无法从中选出master的。同理,一半或超过一半的mnode不工作时,是无法选出mnode的master的,因为存在“split brain”问题。为解决这个问题,TDengine引入了arbitrator的概念。Arbitrator模拟一个vnode或mnode在工作,但只简单的负责网络连接,不处理任何数据插入或访问。只要包含arbitrator在内,超过半数的vnode或mnode工作,那么该vnode group或mnode组就可以正常的提供数据插入或查询服务。比如对于副本数为2的情形,如果一个节点A离线,但另外一个节点B正常,而且能连接到arbitrator, 那么节点B就能正常工作。 +如果副本数为偶数,当一个vnode group里一半vnode不工作时,是无法从中选出master的。同理,一半mnode不工作时,是无法选出mnode的master的,因为存在“split brain”问题。为解决这个问题,TDengine引入了arbitrator的概念。Arbitrator模拟一个vnode或mnode在工作,但只简单的负责网络连接,不处理任何数据插入或访问。只要包含arbitrator在内,超过半数的vnode或mnode工作,那么该vnode group或mnode组就可以正常的提供数据插入或查询服务。比如对于副本数为2的情形,如果一个节点A离线,但另外一个节点B正常,而且能连接到arbitrator, 那么节点B就能正常工作。 -下载最新arbitrator及之前版本的安装包,请点击[安装包下载](https://www.taosdata.com/cn/all-downloads/),在TDengine Arbitrator Linux一节中,选择适合的版本下载并安装。 - -TDengine Arbitrator安装包里带有一个执行程序tarbitrator, 找任何一台Linux服务器运行它即可。该程序对系统资源几乎没有要求,只需要保证有网络连接即可。该应用的命令行参数`-p`可以指定其对外服务的端口号,缺省是6042。配置每个taosd实例时,可以在配置文件taos.cfg里将参数arbitrator设置为arbitrator的End Point。如果该参数配置了,当副本数为偶数数,系统将自动连接配置的arbitrator。 +TDengine提供一个执行程序tarbitrator, 找任何一台Linux服务器运行它即可。请点击[安装包下载](https://www.taosdata.com/cn/all-downloads/),在TDengine Arbitrator Linux一节中,选择适合的版本下载并安装。该程序对系统资源几乎没有要求,只需要保证有网络连接即可。该应用的命令行参数`-p`可以指定其对外服务的端口号,缺省是6042。配置每个taosd实例时,可以在配置文件taos.cfg里将参数arbitrator设置为arbitrator的End Point。如果该参数配置了,当副本数为偶数数,系统将自动连接配置的arbitrator。如果副本数为奇数,即使配置了arbitrator, 系统也不会去建立连接。 From eea4bf2129511663177296f50ff7352d1ec6639e Mon Sep 17 00:00:00 2001 From: Xiaowei Su <46439638+Shawshank-Smile@users.noreply.github.com> Date: Wed, 21 Oct 2020 09:23:28 +0800 Subject: [PATCH 080/101] Update taosd-ch.md --- documentation20/webdocs/markdowndocs/taosd-ch.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation20/webdocs/markdowndocs/taosd-ch.md b/documentation20/webdocs/markdowndocs/taosd-ch.md index e90bc2233f..08be0c163e 100644 --- a/documentation20/webdocs/markdowndocs/taosd-ch.md +++ b/documentation20/webdocs/markdowndocs/taosd-ch.md @@ -61,7 +61,7 @@ vnode与其子模块是通过API直接调用,而不是通过消息队列传递 mnode是整个系统的大脑,负责整个系统的资源调度,负责meta data的管理与存储。 -一个运行的系统里,只有一个mnode,但它有多个副本(由系统配置参数numOfMpeers控制)。这些副本分布在不同的dnode里,目的是保证系统的高可靠运行。副本之间的数据复制是采用同步而非异步的方式,以确保数据的一致性,确保数据不会丢失。这些副本会自动选举一个Master,其他副本是slave。所有数据更新类的操作,都只能在master上进行,而查询类的可以在slave节点上进行。代码实现上,同步模块与vnode共享,但mnode被分配一个特殊的vgroup ID: 1,而且quorum大于1。整个集群系统是由多个dnode组成的,运行的mnode的副本数不可能超过dnode的个数,但不会超过配置的副本数。如果某个mnode副本宕机一段时间,只要超过半数的mnode副本仍在运行,运行的mnode会自动根据整个系统的资源情况,在其他dnode里再启动一个mnode, 以保证运行的副本数。 +一个运行的系统里,只有一个mnode,但它有多个副本(由系统配置参数numOfMnodes控制)。这些副本分布在不同的dnode里,目的是保证系统的高可靠运行。副本之间的数据复制是采用同步而非异步的方式,以确保数据的一致性,确保数据不会丢失。这些副本会自动选举一个Master,其他副本是slave。所有数据更新类的操作,都只能在master上进行,而查询类的可以在slave节点上进行。代码实现上,同步模块与vnode共享,但mnode被分配一个特殊的vgroup ID: 1,而且quorum大于1。整个集群系统是由多个dnode组成的,运行的mnode的副本数不可能超过dnode的个数,但不会超过配置的副本数。如果某个mnode副本宕机一段时间,只要超过半数的mnode副本仍在运行,运行的mnode会自动根据整个系统的资源情况,在其他dnode里再启动一个mnode, 以保证运行的副本数。 各个dnode通过信息交换,保存有mnode各个副本的End Point列表,并向其中的master节点定时(间隔由系统配置参数statusInterval控制)发送status消息,消息体里包含该dnode的CPU、内存、剩余存储空间、vnode个数,以及各个vnode的状态(存储空间、原始数据大小、记录条数、角色等)。这样mnode就了解整个系统的资源情况,如果用户创建新的表,就可以决定需要在哪个dnode创建;如果增加或删除dnode, 或者监测到某dnode数据过热、或离线太长,就可以决定需要挪动那些vnode,以实现负载均衡。 From 69cc4e49c6463539bcabae7859658f7d2df249ba Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Wed, 21 Oct 2020 09:39:16 +0800 Subject: [PATCH 081/101] remove start condition --- Jenkinsfile | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index a603dec841..6d563d132b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,21 +6,9 @@ pipeline { } stages { - stage('pre build'){ - agent{label 'master'} - when{ changeset "develop"} - steps{ - sh ''' - - - echo "check OK!" - ''' - } - } stage('Parallel test stage') { parallel { stage('pytest') { - when{ changeset "develop"} agent{label 'master'} steps { sh ''' @@ -46,7 +34,6 @@ pipeline { } } stage('test_b1') { - when{ changeset "develop"} agent{label '184'} steps { sh ''' @@ -74,7 +61,6 @@ pipeline { stage('test_crash_gen') { agent{label "185"} - when{ changeset "develop"} steps { sh ''' cd ${WKC} @@ -104,7 +90,7 @@ pipeline { stage('test_valgrind') { agent{label "186"} - when{ changeset "develop"} + steps { sh ''' cd ${WKC} From 015f1b82c5b5f78be0b69d3f81543f5cb31473fc Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 21 Oct 2020 10:07:35 +0800 Subject: [PATCH 082/101] [td-1752] add test cases. --- .../parser/col_arithmetic_operation.sim | 18 ++++++++++++++++++ tests/script/general/parser/union.sim | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/script/general/parser/col_arithmetic_operation.sim b/tests/script/general/parser/col_arithmetic_operation.sim index 132ecf4342..e1557227d4 100644 --- a/tests/script/general/parser/col_arithmetic_operation.sim +++ b/tests/script/general/parser/col_arithmetic_operation.sim @@ -91,4 +91,22 @@ endi sql_error select max(c2*2) from $tb sql_error select max(c1-c2) from $tb +print =====================> td-1764 +sql select sum(c1)/count(*), sum(c1) as b, count(*) as b from $stb interval(1y) +if $rows != 1 then + return -1 +endi + +if $data00 != @18-01-01 00:00:00.000@ then + return -1 +endi + +if $data01 != 2.250000000 then + return -1 +endi + +if $data02 != 225000 then + return -1 +endi + system sh/exec.sh -n dnode1 -s stop -x SIGINT \ No newline at end of file diff --git a/tests/script/general/parser/union.sim b/tests/script/general/parser/union.sim index 9e178537a2..4af482bde0 100644 --- a/tests/script/general/parser/union.sim +++ b/tests/script/general/parser/union.sim @@ -251,7 +251,7 @@ if $rows != 15 then endi # first subclause are empty -sql select count(*) as c from union_tb0 where ts>now+10y union all select sum(c1) as c from union_tb1; +sql select count(*) as c from union_tb0 where ts > now + 3650d union all select sum(c1) as c from union_tb1; if $rows != 1 then return -1 endi @@ -346,7 +346,7 @@ if $data91 != 99 then return -1 endi -#1111111111111111111111111111111111111111111111111 +#================================================================================================= # two aggregated functions for normal tables sql select sum(c1) as a from union_tb0 limit 1 union all select sum(c3) as a from union_tb1 limit 2; if $rows != 2 then From 8e188274f085dc22984a0fda49e0c0f674a13aa4 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 21 Oct 2020 10:08:37 +0800 Subject: [PATCH 083/101] [td-1764] --- src/client/inc/tscUtil.h | 7 +-- src/client/inc/tsclient.h | 13 +++-- src/client/src/tscAsync.c | 4 +- src/client/src/tscLocal.c | 12 ++-- src/client/src/tscSQLParser.c | 37 ++++++------ src/client/src/tscServer.c | 2 +- src/client/src/tscSql.c | 22 ++++++- src/client/src/tscSubquery.c | 6 +- src/client/src/tscUtil.c | 104 +++++++++------------------------- src/inc/taosmsg.h | 1 + 10 files changed, 88 insertions(+), 120 deletions(-) diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index 1bd86714c5..3df493349e 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -149,14 +149,13 @@ int tscAllocPayload(SSqlCmd* pCmd, int size); TAOS_FIELD tscCreateField(int8_t type, const char* name, int16_t bytes); -SFieldSupInfo* tscFieldInfoAppend(SFieldInfo* pFieldInfo, TAOS_FIELD* pField); -SFieldSupInfo* tscFieldInfoInsert(SFieldInfo* pFieldInfo, int32_t index, TAOS_FIELD* field); +SInternalField* tscFieldInfoAppend(SFieldInfo* pFieldInfo, TAOS_FIELD* pField); +SInternalField* tscFieldInfoInsert(SFieldInfo* pFieldInfo, int32_t index, TAOS_FIELD* field); -SFieldSupInfo* tscFieldInfoGetSupp(SFieldInfo* pFieldInfo, int32_t index); +SInternalField* tscFieldInfoGetInternalField(SFieldInfo* pFieldInfo, int32_t index); TAOS_FIELD* tscFieldInfoGetField(SFieldInfo* pFieldInfo, int32_t index); void tscFieldInfoUpdateOffset(SQueryInfo* pQueryInfo); -void tscFieldInfoCopy(SFieldInfo* dst, const SFieldInfo* src); void tscFieldInfoUpdateOffsetForInterResult(SQueryInfo* pQueryInfo); int16_t tscFieldInfoGetOffset(SQueryInfo* pQueryInfo, int32_t index); diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index 5d9abf852e..a3a58a4e0f 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -142,16 +142,17 @@ typedef struct SColumnIndex { int16_t columnIndex; } SColumnIndex; -typedef struct SFieldSupInfo { +typedef struct SInternalField { + TAOS_FIELD field; bool visible; SExprInfo *pArithExprInfo; SSqlExpr *pSqlExpr; -} SFieldSupInfo; +} SInternalField; typedef struct SFieldInfo { - int16_t numOfOutput; // number of column in result - SArray *pFields; // SArray - SArray *pSupportInfo; // SArray + int16_t numOfOutput; // number of column in result + TAOS_FIELD* final; + SArray *internalField; // SArray } SFieldInfo; typedef struct SColumn { @@ -469,7 +470,7 @@ int32_t tscSQLSyntaxErrMsg(char* msg, const char* additionalInfo, const char* s int32_t tscToSQLCmd(SSqlObj *pSql, struct SSqlInfo *pInfo); static FORCE_INLINE void tscGetResultColumnChr(SSqlRes* pRes, SFieldInfo* pFieldInfo, int32_t columnIndex) { - SFieldSupInfo* pInfo = (SFieldSupInfo*) TARRAY_GET_ELEM(pFieldInfo->pSupportInfo, columnIndex); + SInternalField* pInfo = (SInternalField*) TARRAY_GET_ELEM(pFieldInfo->internalField, columnIndex); assert(pInfo->pSqlExpr != NULL); int32_t type = pInfo->pSqlExpr->resType; diff --git a/src/client/src/tscAsync.c b/src/client/src/tscAsync.c index 003eddc8d2..c996bb2a76 100644 --- a/src/client/src/tscAsync.c +++ b/src/client/src/tscAsync.c @@ -327,7 +327,7 @@ void tscAsyncFetchSingleRowProxy(void *param, TAOS_RES *tres, int numOfRows) { } for (int i = 0; i < pCmd->numOfCols; ++i){ - SFieldSupInfo* pSup = taosArrayGet(pQueryInfo->fieldsInfo.pSupportInfo, i); + SInternalField* pSup = taosArrayGet(pQueryInfo->fieldsInfo.internalField, i); if (pSup->pSqlExpr != NULL) { // pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pQueryInfo, i) + pSup->pSqlExpr->resBytes * pRes->row; } else { @@ -348,7 +348,7 @@ void tscProcessFetchRow(SSchedMsg *pMsg) { SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex); for (int i = 0; i < pCmd->numOfCols; ++i) { - SFieldSupInfo* pSup = taosArrayGet(pQueryInfo->fieldsInfo.pSupportInfo, i); + SInternalField* pSup = taosArrayGet(pQueryInfo->fieldsInfo.internalField, i); if (pSup->pSqlExpr != NULL) { tscGetResultColumnChr(pRes, &pQueryInfo->fieldsInfo, i); diff --git a/src/client/src/tscLocal.c b/src/client/src/tscLocal.c index a15673bdb1..ddab61fb45 100644 --- a/src/client/src/tscLocal.c +++ b/src/client/src/tscLocal.c @@ -239,7 +239,7 @@ static int32_t tscBuildTableSchemaResultFields(SSqlObj *pSql, int32_t numOfCols, TAOS_FIELD f = {.type = TSDB_DATA_TYPE_BINARY, .bytes = (TSDB_COL_NAME_LEN - 1) + VARSTR_HEADER_SIZE}; tstrncpy(f.name, "Field", sizeof(f.name)); - SFieldSupInfo* pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); + SInternalField* pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); pInfo->pSqlExpr = tscSqlExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_BINARY, (TSDB_COL_NAME_LEN - 1) + VARSTR_HEADER_SIZE, (TSDB_COL_NAME_LEN - 1), false); @@ -485,7 +485,7 @@ static int32_t tscSCreateBuildResultFields(SSqlObj *pSql, BuildType type, const tstrncpy(f.name, "Database", sizeof(f.name)); } - SFieldSupInfo* pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); + SInternalField* pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); pInfo->pSqlExpr = tscSqlExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_BINARY, f.bytes, f.bytes - VARSTR_HEADER_SIZE, false); @@ -922,19 +922,17 @@ void tscSetLocalQueryResult(SSqlObj *pSql, const char *val, const char *columnNa pQueryInfo->order.order = TSDB_ORDER_ASC; tscFieldInfoClear(&pQueryInfo->fieldsInfo); - pQueryInfo->fieldsInfo.pFields = taosArrayInit(1, sizeof(TAOS_FIELD)); - pQueryInfo->fieldsInfo.pSupportInfo = taosArrayInit(1, sizeof(SFieldSupInfo)); + pQueryInfo->fieldsInfo.internalField = taosArrayInit(1, sizeof(SInternalField)); TAOS_FIELD f = tscCreateField((int8_t)type, columnName, (int16_t)valueLength); tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f); tscInitResObjForLocalQuery(pSql, 1, (int32_t)valueLength); - TAOS_FIELD *pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 0); - SFieldSupInfo* pInfo = tscFieldInfoGetSupp(&pQueryInfo->fieldsInfo, 0); + SInternalField* pInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, 0); pInfo->pSqlExpr = taosArrayGetP(pQueryInfo->exprList, 0); - memcpy(pRes->data, val, pField->bytes); + memcpy(pRes->data, val, pInfo->field.bytes); } int tscProcessLocalCmd(SSqlObj *pSql) { diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 7a8be085eb..c8e80790ef 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -122,7 +122,7 @@ static int32_t doCheckForCreateTable(SSqlObj* pSql, int32_t subClauseIndex, SSql static int32_t doCheckForCreateFromStable(SSqlObj* pSql, SSqlInfo* pInfo); static int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo); static int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index); -static int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSQLExpr* pSqlExpr, SArray* pExprInfo, SQueryInfo* pQueryInfo, SArray* pCols); +static int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSQLExpr* pSqlExpr, SQueryInfo* pQueryInfo, SArray* pCols, int64_t *uid); /* * Used during parsing query sql. Since the query sql usually small in length, error position @@ -1250,7 +1250,7 @@ static int32_t handleArithmeticExpr(SSqlCmd* pCmd, int32_t clauseIndex, int32_t tExprNode* pNode = NULL; SArray* colList = taosArrayInit(10, sizeof(SColIndex)); - int32_t ret = exprTreeFromSqlExpr(pCmd, &pNode, pItem->pNode, pQueryInfo->exprList, pQueryInfo, colList); + int32_t ret = exprTreeFromSqlExpr(pCmd, &pNode, pItem->pNode, pQueryInfo, colList, NULL); if (ret != TSDB_CODE_SUCCESS) { taosArrayDestroy(colList); tExprTreeDestroy(&pNode, NULL); @@ -1306,17 +1306,17 @@ static int32_t handleArithmeticExpr(SSqlCmd* pCmd, int32_t clauseIndex, int32_t insertResultField(pQueryInfo, exprIndex, &columnList, sizeof(double), TSDB_DATA_TYPE_DOUBLE, aliasName, NULL); int32_t slot = tscNumOfFields(pQueryInfo) - 1; - SFieldSupInfo* pInfo = tscFieldInfoGetSupp(&pQueryInfo->fieldsInfo, slot); + SInternalField* pInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, slot); if (pInfo->pSqlExpr == NULL) { SExprInfo* pArithExprInfo = calloc(1, sizeof(SExprInfo)); // arithmetic expression always return result in the format of double float - pArithExprInfo->bytes = sizeof(double); + pArithExprInfo->bytes = sizeof(double); pArithExprInfo->interBytes = sizeof(double); - pArithExprInfo->type = TSDB_DATA_TYPE_DOUBLE; + pArithExprInfo->type = TSDB_DATA_TYPE_DOUBLE; - int32_t ret = exprTreeFromSqlExpr(pCmd, &pArithExprInfo->pExpr, pItem->pNode, pQueryInfo->exprList, pQueryInfo, NULL); + int32_t ret = exprTreeFromSqlExpr(pCmd, &pArithExprInfo->pExpr, pItem->pNode, pQueryInfo, NULL, &pArithExprInfo->uid); if (ret != TSDB_CODE_SUCCESS) { tExprTreeDestroy(&pArithExprInfo->pExpr, NULL); return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), "invalid expression in select clause"); @@ -1373,7 +1373,7 @@ static void addPrimaryTsColIntoResult(SQueryInfo* pQueryInfo) { int32_t numOfCols = (int32_t)tscSqlExprNumOfExprs(pQueryInfo); tscAddSpecialColumnForSelect(pQueryInfo, numOfCols, TSDB_FUNC_PRJ, &index, pSchema, TSDB_COL_NORMAL); - SFieldSupInfo* pSupInfo = tscFieldInfoGetSupp(&pQueryInfo->fieldsInfo, numOfCols); + SInternalField* pSupInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, numOfCols); pSupInfo->visible = false; pQueryInfo->type |= TSDB_QUERY_TYPE_PROJECTION_QUERY; @@ -1470,7 +1470,7 @@ int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SColumnLi } TAOS_FIELD f = tscCreateField(type, fieldName, bytes); - SFieldSupInfo* pInfo = tscFieldInfoInsert(&pQueryInfo->fieldsInfo, outputIndex, &f); + SInternalField* pInfo = tscFieldInfoInsert(&pQueryInfo->fieldsInfo, outputIndex, &f); pInfo->pSqlExpr = pSqlExpr; return TSDB_CODE_SUCCESS; @@ -4063,7 +4063,7 @@ static int32_t getTagQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SCondE tExprNode* p = NULL; SArray* colList = taosArrayInit(10, sizeof(SColIndex)); - ret = exprTreeFromSqlExpr(pCmd, &p, p1, NULL, pQueryInfo, colList); + ret = exprTreeFromSqlExpr(pCmd, &p, p1, pQueryInfo, colList, NULL); SBufferWriter bw = tbufInitWriter(NULL, false); TRY(0) { @@ -5316,7 +5316,7 @@ void doAddGroupColumnForSubquery(SQueryInfo* pQueryInfo, int32_t tagIndex) { tscAddSpecialColumnForSelect(pQueryInfo, (int32_t)size, TSDB_FUNC_PRJ, &colIndex, pSchema, TSDB_COL_NORMAL); - SFieldSupInfo* pInfo = tscFieldInfoGetSupp(&pQueryInfo->fieldsInfo, (int32_t)size); + SInternalField* pInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, (int32_t)size); doLimitOutputNormalColOfGroupby(pInfo->pSqlExpr); pInfo->visible = false; } @@ -6361,19 +6361,19 @@ int32_t doCheckForQuery(SSqlObj* pSql, SQuerySQL* pQuerySql, int32_t index) { return TSDB_CODE_SUCCESS; // Does not build query message here } -int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSQLExpr* pSqlExpr, SArray* pExprInfo, SQueryInfo* pQueryInfo, SArray* pCols) { +int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSQLExpr* pSqlExpr, SQueryInfo* pQueryInfo, SArray* pCols, int64_t *uid) { tExprNode* pLeft = NULL; tExprNode* pRight= NULL; if (pSqlExpr->pLeft != NULL) { - int32_t ret = exprTreeFromSqlExpr(pCmd, &pLeft, pSqlExpr->pLeft, pExprInfo, pQueryInfo, pCols); + int32_t ret = exprTreeFromSqlExpr(pCmd, &pLeft, pSqlExpr->pLeft, pQueryInfo, pCols, uid); if (ret != TSDB_CODE_SUCCESS) { return ret; } } if (pSqlExpr->pRight != NULL) { - int32_t ret = exprTreeFromSqlExpr(pCmd, &pRight, pSqlExpr->pRight, pExprInfo, pQueryInfo, pCols); + int32_t ret = exprTreeFromSqlExpr(pCmd, &pRight, pSqlExpr->pRight, pQueryInfo, pCols, uid); if (ret != TSDB_CODE_SUCCESS) { return ret; } @@ -6400,14 +6400,19 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSQLExpr* pS strncpy((*pExpr)->pSchema->name, pSqlExpr->operand.z, pSqlExpr->operand.n); // set the input column data byte and type. - size_t size = taosArrayGetSize(pExprInfo); + size_t size = taosArrayGetSize(pQueryInfo->exprList); for (int32_t i = 0; i < size; ++i) { - SSqlExpr* p1 = taosArrayGetP(pExprInfo, i); + SSqlExpr* p1 = taosArrayGetP(pQueryInfo->exprList, i); if (strcmp((*pExpr)->pSchema->name, p1->aliasName) == 0) { - (*pExpr)->pSchema->type = (uint8_t)p1->resType; + (*pExpr)->pSchema->type = (uint8_t)p1->resType; (*pExpr)->pSchema->bytes = p1->resBytes; + + if (uid != NULL) { + *uid = p1->uid; + } + break; } } diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 8d63eb9775..966eee6b01 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1934,7 +1934,7 @@ int tscProcessShowRsp(SSqlObj *pSql) { tscColumnListInsert(pQueryInfo->colList, &index); TAOS_FIELD f = tscCreateField(pSchema->type, pSchema->name, pSchema->bytes); - SFieldSupInfo* pInfo = tscFieldInfoAppend(pFieldInfo, &f); + SInternalField* pInfo = tscFieldInfoAppend(pFieldInfo, &f); pInfo->pSqlExpr = tscSqlExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, pTableSchema[i].type, pTableSchema[i].bytes, pTableSchema[i].bytes, false); diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index be91255b9c..e99297ef16 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -370,7 +370,7 @@ int taos_num_fields(TAOS_RES *res) { size_t numOfCols = tscNumOfFields(pQueryInfo); for(int32_t i = 0; i < numOfCols; ++i) { - SFieldSupInfo* pInfo = taosArrayGet(pQueryInfo->fieldsInfo.pSupportInfo, i); + SInternalField* pInfo = taosArrayGet(pQueryInfo->fieldsInfo.internalField, i); if (pInfo->visible) { num++; } @@ -406,8 +406,24 @@ TAOS_FIELD *taos_fetch_fields(TAOS_RES *res) { if (numOfCols == 0) { return NULL; } - - return pQueryInfo->fieldsInfo.pFields->pData; + + SFieldInfo *pFieldInfo = &pQueryInfo->fieldsInfo; + + if (pFieldInfo->final == NULL) { + TAOS_FIELD* f = calloc(pFieldInfo->numOfOutput, sizeof(TAOS_FIELD)); + + int32_t j = 0; + for(int32_t i = 0; i < pFieldInfo->numOfOutput; ++i) { + SInternalField* pField = tscFieldInfoGetInternalField(pFieldInfo, i); + if (pField->visible) { + f[j++] = pField->field; + } + } + + pFieldInfo->final = f; + } + + return pFieldInfo->final; } int taos_retrieve(TAOS_RES *res) { diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index 4184d05ddc..90490f07d8 100644 --- a/src/client/src/tscSubquery.c +++ b/src/client/src/tscSubquery.c @@ -2013,7 +2013,7 @@ int32_t tscHandleMultivnodeInsert(SSqlObj *pSql) { static char* getResultBlockPosition(SSqlCmd* pCmd, SSqlRes* pRes, int32_t columnIndex, int16_t* bytes) { SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex); - SFieldSupInfo* pInfo = (SFieldSupInfo*) TARRAY_GET_ELEM(pQueryInfo->fieldsInfo.pSupportInfo, columnIndex); + SInternalField* pInfo = (SInternalField*) TARRAY_GET_ELEM(pQueryInfo->fieldsInfo.internalField, columnIndex); assert(pInfo->pSqlExpr != NULL); *bytes = pInfo->pSqlExpr->resBytes; @@ -2163,7 +2163,7 @@ void **doSetResultRowData(SSqlObj *pSql, bool finalResult) { size_t size = tscNumOfFields(pQueryInfo); for (int i = 0; i < size; ++i) { - SFieldSupInfo* pSup = TARRAY_GET_ELEM(pQueryInfo->fieldsInfo.pSupportInfo, i); + SInternalField* pSup = TARRAY_GET_ELEM(pQueryInfo->fieldsInfo.internalField, i); if (pSup->pSqlExpr != NULL) { tscGetResultColumnChr(pRes, &pQueryInfo->fieldsInfo, i); } @@ -2173,7 +2173,7 @@ void **doSetResultRowData(SSqlObj *pSql, bool finalResult) { continue; } - TAOS_FIELD *pField = TARRAY_GET_ELEM(pQueryInfo->fieldsInfo.pFields, i); + TAOS_FIELD *pField = TARRAY_GET_ELEM(pQueryInfo->fieldsInfo.internalField, i); if (pRes->tsrow[i] != NULL && pField->type == TSDB_DATA_TYPE_NCHAR) { transferNcharData(pSql, i, pField); } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 9e861d3f28..d35fb21c57 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -847,35 +847,30 @@ TAOS_FIELD tscCreateField(int8_t type, const char* name, int16_t bytes) { return f; } -SFieldSupInfo* tscFieldInfoAppend(SFieldInfo* pFieldInfo, TAOS_FIELD* pField) { +SInternalField* tscFieldInfoAppend(SFieldInfo* pFieldInfo, TAOS_FIELD* pField) { assert(pFieldInfo != NULL); - taosArrayPush(pFieldInfo->pFields, pField); pFieldInfo->numOfOutput++; - struct SFieldSupInfo info = { + struct SInternalField info = { .pSqlExpr = NULL, .pArithExprInfo = NULL, .visible = true, }; - - return taosArrayPush(pFieldInfo->pSupportInfo, &info); + + info.field = *pField; + return taosArrayPush(pFieldInfo->internalField, &info); } -SFieldSupInfo* tscFieldInfoGetSupp(SFieldInfo* pFieldInfo, int32_t index) { - return TARRAY_GET_ELEM(pFieldInfo->pSupportInfo, index); -} - -SFieldSupInfo* tscFieldInfoInsert(SFieldInfo* pFieldInfo, int32_t index, TAOS_FIELD* field) { - taosArrayInsert(pFieldInfo->pFields, index, field); +SInternalField* tscFieldInfoInsert(SFieldInfo* pFieldInfo, int32_t index, TAOS_FIELD* field) { pFieldInfo->numOfOutput++; - - struct SFieldSupInfo info = { + struct SInternalField info = { .pSqlExpr = NULL, .pArithExprInfo = NULL, .visible = true, }; - - return taosArrayInsert(pFieldInfo->pSupportInfo, index, &info); + + info.field = *field; + return taosArrayInsert(pFieldInfo->internalField, index, &info); } void tscFieldInfoUpdateOffset(SQueryInfo* pQueryInfo) { @@ -909,29 +904,18 @@ void tscFieldInfoUpdateOffsetForInterResult(SQueryInfo* pQueryInfo) { } } -void tscFieldInfoCopy(SFieldInfo* dst, const SFieldInfo* src) { - dst->numOfOutput = src->numOfOutput; - - if (dst->pFields == NULL) { - dst->pFields = taosArrayClone(src->pFields); - } else { - taosArrayCopy(dst->pFields, src->pFields); - } - - if (dst->pSupportInfo == NULL) { - dst->pSupportInfo = taosArrayClone(src->pSupportInfo); - } else { - taosArrayCopy(dst->pSupportInfo, src->pSupportInfo); - } +SInternalField* tscFieldInfoGetInternalField(SFieldInfo* pFieldInfo, int32_t index) { + assert(index < pFieldInfo->numOfOutput); + return TARRAY_GET_ELEM(pFieldInfo->internalField, index); } TAOS_FIELD* tscFieldInfoGetField(SFieldInfo* pFieldInfo, int32_t index) { assert(index < pFieldInfo->numOfOutput); - return TARRAY_GET_ELEM(pFieldInfo->pFields, index); + return &((SInternalField*)TARRAY_GET_ELEM(pFieldInfo->internalField, index))->field; } int16_t tscFieldInfoGetOffset(SQueryInfo* pQueryInfo, int32_t index) { - SFieldSupInfo* pInfo = tscFieldInfoGetSupp(&pQueryInfo->fieldsInfo, index); + SInternalField* pInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, index); assert(pInfo != NULL && pInfo->pSqlExpr != NULL); return pInfo->pSqlExpr->offset; @@ -978,10 +962,8 @@ void tscFieldInfoClear(SFieldInfo* pFieldInfo) { return; } - taosArrayDestroy(pFieldInfo->pFields); - for(int32_t i = 0; i < pFieldInfo->numOfOutput; ++i) { - SFieldSupInfo* pInfo = taosArrayGet(pFieldInfo->pSupportInfo, i); + SInternalField* pInfo = taosArrayGet(pFieldInfo->internalField, i); if (pInfo->pArithExprInfo != NULL) { tExprTreeDestroy(&pInfo->pArithExprInfo->pExpr, NULL); @@ -989,7 +971,7 @@ void tscFieldInfoClear(SFieldInfo* pFieldInfo) { } } - taosArrayDestroy(pFieldInfo->pSupportInfo); + taosArrayDestroy(pFieldInfo->internalField); memset(pFieldInfo, 0, sizeof(SFieldInfo)); } @@ -1633,11 +1615,8 @@ STableMetaInfo* tscGetTableMetaInfoByUid(SQueryInfo* pQueryInfo, uint64_t uid, i } void tscInitQueryInfo(SQueryInfo* pQueryInfo) { - assert(pQueryInfo->fieldsInfo.pFields == NULL); - pQueryInfo->fieldsInfo.pFields = taosArrayInit(4, sizeof(TAOS_FIELD)); - - assert(pQueryInfo->fieldsInfo.pSupportInfo == NULL); - pQueryInfo->fieldsInfo.pSupportInfo = taosArrayInit(4, sizeof(SFieldSupInfo)); + assert(pQueryInfo->fieldsInfo.internalField == NULL); + pQueryInfo->fieldsInfo.internalField = taosArrayInit(4, sizeof(SInternalField)); assert(pQueryInfo->exprList == NULL); pQueryInfo->exprList = taosArrayInit(4, POINTER_BYTES); @@ -1849,54 +1828,23 @@ SSqlObj* createSimpleSubObj(SSqlObj* pSql, void (*fp)(), void* param, int32_t cm return pNew; } -// current sql function is not direct output result, so create a dummy output field -static void doSetNewFieldInfo(SQueryInfo* pNewQueryInfo, SSqlExpr* pExpr) { - TAOS_FIELD f = {.type = (uint8_t)pExpr->resType, .bytes = pExpr->resBytes}; - tstrncpy(f.name, pExpr->aliasName, sizeof(f.name)); - - SFieldSupInfo* pInfo1 = tscFieldInfoAppend(&pNewQueryInfo->fieldsInfo, &f); - - pInfo1->pSqlExpr = pExpr; - pInfo1->visible = false; -} - static void doSetSqlExprAndResultFieldInfo(SQueryInfo* pQueryInfo, SQueryInfo* pNewQueryInfo, int64_t uid) { int32_t numOfOutput = (int32_t)tscSqlExprNumOfExprs(pNewQueryInfo); if (numOfOutput == 0) { return; } - size_t numOfExprs = tscSqlExprNumOfExprs(pQueryInfo); - SFieldInfo* pFieldInfo = &pQueryInfo->fieldsInfo; - // set the field info in pNewQueryInfo object + size_t numOfExprs = tscSqlExprNumOfExprs(pNewQueryInfo); for (int32_t i = 0; i < numOfExprs; ++i) { - SSqlExpr* pExpr = tscSqlExprGet(pQueryInfo, i); + SSqlExpr* pExpr = tscSqlExprGet(pNewQueryInfo, i); - if (pExpr->uid == uid) { - if (i < pFieldInfo->numOfOutput) { - SFieldSupInfo* pInfo = tscFieldInfoGetSupp(pFieldInfo, i); - - if (pInfo->pSqlExpr != NULL) { - TAOS_FIELD* p = tscFieldInfoGetField(pFieldInfo, i); - assert(strcmp(p->name, pExpr->aliasName) == 0); - - SFieldSupInfo* pInfo1 = tscFieldInfoAppend(&pNewQueryInfo->fieldsInfo, p); - *pInfo1 = *pInfo; - } else { - assert(pInfo->pArithExprInfo != NULL); - doSetNewFieldInfo(pNewQueryInfo, pExpr); - } - } else { // it is a arithmetic column, does not have actual field for sqlExpr, so build it - doSetNewFieldInfo(pNewQueryInfo, pExpr); - } - } + TAOS_FIELD f = tscCreateField(pExpr->resType, pExpr->aliasName, pExpr->resBytes); + SInternalField* pInfo1 = tscFieldInfoAppend(&pNewQueryInfo->fieldsInfo, &f); + pInfo1->pSqlExpr = pExpr; } - // make sure the the sqlExpr for each fields is correct - numOfExprs = tscSqlExprNumOfExprs(pNewQueryInfo); - - // update the pSqlExpr pointer in SFieldSupInfo according the field name + // update the pSqlExpr pointer in SInternalField according the field name // make sure the pSqlExpr point to the correct SqlExpr in pNewQueryInfo, not SqlExpr in pQueryInfo for (int32_t f = 0; f < pNewQueryInfo->fieldsInfo.numOfOutput; ++f) { TAOS_FIELD* field = tscFieldInfoGetField(&pNewQueryInfo->fieldsInfo, f); @@ -1906,7 +1854,7 @@ static void doSetSqlExprAndResultFieldInfo(SQueryInfo* pQueryInfo, SQueryInfo* p SSqlExpr* pExpr1 = tscSqlExprGet(pNewQueryInfo, k1); if (strcmp(field->name, pExpr1->aliasName) == 0) { // establish link according to the result field name - SFieldSupInfo* pInfo = tscFieldInfoGetSupp(&pNewQueryInfo->fieldsInfo, f); + SInternalField* pInfo = tscFieldInfoGetInternalField(&pNewQueryInfo->fieldsInfo, f); pInfo->pSqlExpr = pExpr1; matched = true; diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index 2c2d686099..600347c44f 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -407,6 +407,7 @@ typedef struct SExprInfo { int16_t bytes; int16_t type; int32_t interBytes; + int64_t uid; } SExprInfo; typedef struct SColumnFilterInfo { From 820bf89a2e9eaf2f5cdc088fe3a9c683b322d071 Mon Sep 17 00:00:00 2001 From: Bomin Zhang Date: Wed, 21 Oct 2020 10:42:09 +0800 Subject: [PATCH 084/101] TD-1471: use new error code when qhandle is null --- src/client/src/tscSql.c | 7 ++----- src/query/src/qExecutor.c | 3 +++ src/vnode/src/vnodeRead.c | 4 +++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index 02a2c1e052..be91255b9c 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -479,11 +479,8 @@ TAOS_ROW taos_fetch_row(TAOS_RES *res) { SSqlCmd *pCmd = &pSql->cmd; SSqlRes *pRes = &pSql->res; - if (pRes->qhandle == 0) { - pRes->code = TSDB_CODE_TSC_INVALID_QHANDLE; - return NULL; - } - if (pRes->code == TSDB_CODE_TSC_QUERY_CANCELLED || + if (pRes->qhandle == 0 || + pRes->code == TSDB_CODE_TSC_QUERY_CANCELLED || pCmd->command == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pCmd->command == TSDB_SQL_INSERT) { return NULL; diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index f742616b05..869e2dd7ed 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -7101,6 +7101,7 @@ void qCleanupQueryMgmt(void* pQMgmt) { void** qRegisterQInfo(void* pMgmt, uint64_t qInfo) { if (pMgmt == NULL) { + terrno = TSDB_CODE_VND_INVALID_VGROUP_ID; return NULL; } @@ -7109,6 +7110,7 @@ void** qRegisterQInfo(void* pMgmt, uint64_t qInfo) { SQueryMgmt *pQueryMgmt = pMgmt; if (pQueryMgmt->qinfoPool == NULL) { qError("QInfo:%p failed to add qhandle into qMgmt, since qMgmt is closed", (void *)qInfo); + terrno = TSDB_CODE_VND_INVALID_VGROUP_ID; return NULL; } @@ -7116,6 +7118,7 @@ void** qRegisterQInfo(void* pMgmt, uint64_t qInfo) { if (pQueryMgmt->closed) { // pthread_mutex_unlock(&pQueryMgmt->lock); qError("QInfo:%p failed to add qhandle into cache, since qMgmt is colsing", (void *)qInfo); + terrno = TSDB_CODE_VND_INVALID_VGROUP_ID; return NULL; } else { TSDB_CACHE_PTR_TYPE handleVal = (TSDB_CACHE_PTR_TYPE) qInfo; diff --git a/src/vnode/src/vnodeRead.c b/src/vnode/src/vnodeRead.c index baaeae2a81..969a29359f 100644 --- a/src/vnode/src/vnodeRead.c +++ b/src/vnode/src/vnodeRead.c @@ -166,10 +166,12 @@ static int32_t vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { if (code == TSDB_CODE_SUCCESS) { handle = qRegisterQInfo(pVnode->qMgmt, (uint64_t)pQInfo); if (handle == NULL) { // failed to register qhandle, todo add error test case + pRsp->code = terrno; + terrno = 0; vError("vgId:%d QInfo:%p register qhandle failed, return to app, code:%s", pVnode->vgId, (void *)pQInfo, tstrerror(pRsp->code)); - pRsp->code = TSDB_CODE_QRY_INVALID_QHANDLE; qDestroyQueryInfo(pQInfo); // destroy it directly + return pRsp->code; } else { assert(*handle == pQInfo); pRsp->qhandle = htobe64((uint64_t)pQInfo); From 6b985a6e8677070ccd9bf7d3c0c10d9cd7b756d2 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 21 Oct 2020 10:51:46 +0800 Subject: [PATCH 085/101] [td-1764] fix memory leak. --- src/client/src/tscLocalMerge.c | 3 ++- src/client/src/tscUtil.c | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/client/src/tscLocalMerge.c b/src/client/src/tscLocalMerge.c index d800d62c8a..44ccb2471a 100644 --- a/src/client/src/tscLocalMerge.c +++ b/src/client/src/tscLocalMerge.c @@ -510,7 +510,8 @@ void tscDestroyLocalReducer(SSqlObj *pSql) { taosTFree(pLocalReducer->pResultBuf); if (pLocalReducer->pResInfo != NULL) { - for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) { + size_t num = tscSqlExprNumOfExprs(pQueryInfo); + for (int32_t i = 0; i < num; ++i) { taosTFree(pLocalReducer->pResInfo[i].interResultBuf); } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index d35fb21c57..1a8ed2e725 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -972,6 +972,8 @@ void tscFieldInfoClear(SFieldInfo* pFieldInfo) { } taosArrayDestroy(pFieldInfo->internalField); + taosTFree(pFieldInfo->final); + memset(pFieldInfo, 0, sizeof(SFieldInfo)); } @@ -1834,7 +1836,7 @@ static void doSetSqlExprAndResultFieldInfo(SQueryInfo* pQueryInfo, SQueryInfo* p return; } - // set the field info in pNewQueryInfo object + // set the field info in pNewQueryInfo object according to sqlExpr information size_t numOfExprs = tscSqlExprNumOfExprs(pNewQueryInfo); for (int32_t i = 0; i < numOfExprs; ++i) { SSqlExpr* pExpr = tscSqlExprGet(pNewQueryInfo, i); From 6137a2b377f8c027aeb946bacee3f1eb9d4b4ac9 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 21 Oct 2020 03:07:16 +0000 Subject: [PATCH 086/101] TD-1746 --- src/balance/src/balance.c | 4 +- src/client/src/tscSql.c | 41 ++++++++++++++- src/client/src/tscUtil.c | 18 +++++-- src/dnode/src/dnodeVRead.c | 1 - src/inc/vnode.h | 2 + src/mnode/src/mnodeProfile.c | 5 +- src/vnode/src/vnodeMain.c | 96 ++++++++++++++++++++++-------------- src/vnode/src/vnodeWrite.c | 4 +- 8 files changed, 123 insertions(+), 48 deletions(-) diff --git a/src/balance/src/balance.c b/src/balance/src/balance.c index 0fa4b3f346..0e9bb85b25 100644 --- a/src/balance/src/balance.c +++ b/src/balance/src/balance.c @@ -216,8 +216,8 @@ static bool balanceCheckVgroupReady(SVgObj *pVgroup, SVnodeGid *pRmVnode) { SVnodeGid *pVnode = pVgroup->vnodeGid + i; if (pVnode == pRmVnode) continue; - mTrace("vgId:%d, change vgroup status, dnode:%d status:%d", pVgroup->vgId, pVnode->pDnode->dnodeId, - pVnode->pDnode->status); + mTrace("vgId:%d, check vgroup status, dnode:%d status:%d, vnode role:%s", pVgroup->vgId, pVnode->pDnode->dnodeId, + pVnode->pDnode->status, syncRole[pVnode->role]); if (pVnode->pDnode->status == TAOS_DN_STATUS_DROPPING) continue; if (pVnode->pDnode->status == TAOS_DN_STATUS_OFFLINE) continue; diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index be91255b9c..1ccf9c5ec4 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -588,7 +588,20 @@ static UNUSED_FUNC bool tscKillQueryInDnode(SSqlObj* pSql) { return true; } - SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + + if (pQueryInfo != 0) { + STableMetaInfo *pTableMetaInfo1 = tscGetMetaInfo(pQueryInfo, 0); + if (pTableMetaInfo1 != NULL) { + // for select query super table, the super table vgroup list can not be null in any cases. + if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo1)) { + if (pTableMetaInfo1->pVgroupTables == 0) { + tscError("error !!!%p, vgroupTable is null", pSql); + } + } + } + } + if ((pQueryInfo == NULL) || tscIsTwoStageSTableQuery(pQueryInfo, 0)) { return true; } @@ -702,6 +715,19 @@ static void tscKillSTableQuery(SSqlObj *pSql) { SSqlCmd* pCmd = &pSql->cmd; SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex); + + if (pQueryInfo != 0) { + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); + if (pTableMetaInfo != NULL) { + // for select query super table, the super table vgroup list can not be null in any cases. + if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { + if (pTableMetaInfo->pVgroupTables == 0) { + tscError("error !!!%p, vgroupTable is null", pSql); + } + } + } + } + if (!tscIsTwoStageSTableQuery(pQueryInfo, 0)) { return; } @@ -750,6 +776,19 @@ void taos_stop_query(TAOS_RES *res) { pSql->res.code = TSDB_CODE_TSC_QUERY_CANCELLED; SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex); + + if (pQueryInfo != 0) { + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); + if (pTableMetaInfo != NULL) { + // for select query super table, the super table vgroup list can not be null in any cases. + if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { + if (pTableMetaInfo->pVgroupTables == 0) { + tscError("error !!!%p, vgroupTable is null", pSql); + } + } + } + } + if (tscIsTwoStageSTableQuery(pQueryInfo, 0)) { assert(pSql->pRpcCtx == NULL); tscKillSTableQuery(pSql); diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 0235f037bd..baa733ef49 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -114,9 +114,9 @@ bool tscIsTwoStageSTableQuery(SQueryInfo* pQueryInfo, int32_t tableIndex) { } // for select query super table, the super table vgroup list can not be null in any cases. - if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { - assert(pTableMetaInfo->vgroupList != NULL); - } + // if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { + // assert(pTableMetaInfo->vgroupList != NULL); + // } if ((pQueryInfo->type & TSDB_QUERY_TYPE_FREE_RESOURCE) == TSDB_QUERY_TYPE_FREE_RESOURCE) { return false; @@ -2098,6 +2098,18 @@ void tscDoQuery(SSqlObj* pSql) { } else { SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex); uint16_t type = pQueryInfo->type; + + if (pQueryInfo != 0) { + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); + if (pTableMetaInfo != NULL) { + // for select query super table, the super table vgroup list can not be null in any cases. + if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { + if (pTableMetaInfo->pVgroupTables == 0) { + tscError("error !!!%p, vgroupTable is null", pSql); + } + } + } + } if (pSql->fp == (void(*)())tscHandleMultivnodeInsert) { // multi-vnodes insertion tscHandleMultivnodeInsert(pSql); diff --git a/src/dnode/src/dnodeVRead.c b/src/dnode/src/dnodeVRead.c index 1a3d0ebc27..654e35cad5 100644 --- a/src/dnode/src/dnodeVRead.c +++ b/src/dnode/src/dnodeVRead.c @@ -189,7 +189,6 @@ void dnodeSendRpcReadRsp(void *pVnode, SReadMsg *pRead, int32_t code) { void dnodeDispatchNonRspMsg(void *pVnode, SReadMsg *pRead, int32_t code) { rpcFreeCont(pRead->rpcMsg.pCont); vnodeRelease(pVnode); - return; } static void *dnodeProcessReadQueue(void *param) { diff --git a/src/inc/vnode.h b/src/inc/vnode.h index 15ddb6afee..ed6c232fdb 100644 --- a/src/inc/vnode.h +++ b/src/inc/vnode.h @@ -41,6 +41,8 @@ typedef struct { SRpcMsg rpcMsg; } SReadMsg; +extern char *vnodeStatus[]; + int32_t vnodeCreate(SMDCreateVnodeMsg *pVnodeCfg); int32_t vnodeDrop(int32_t vgId); int32_t vnodeOpen(int32_t vgId, char *rootDir); diff --git a/src/mnode/src/mnodeProfile.c b/src/mnode/src/mnodeProfile.c index fc76e3dce3..f8f99e22c6 100644 --- a/src/mnode/src/mnodeProfile.c +++ b/src/mnode/src/mnodeProfile.c @@ -111,7 +111,6 @@ void mnodeReleaseConn(SConnObj *pConn) { } SConnObj *mnodeAccquireConn(int32_t connId, char *user, uint32_t ip, uint16_t port) { - uint64_t expireTime = CONN_KEEP_TIME * 1000 + (uint64_t)taosGetTimestampMs(); SConnObj *pConn = taosCacheAcquireByKey(tsMnodeConnCache, &connId, sizeof(int32_t)); if (pConn == NULL) { mDebug("connId:%d, is already destroyed, user:%s ip:%s:%u", connId, user, taosIpStr(ip), port); @@ -126,7 +125,7 @@ SConnObj *mnodeAccquireConn(int32_t connId, char *user, uint32_t ip, uint16_t po } // mDebug("connId:%d, is incoming, user:%s ip:%s:%u", connId, pConn->user, taosIpStr(pConn->ip), pConn->port); - pConn->lastAccess = expireTime; + pConn->lastAccess = CONN_KEEP_TIME * 1000 + (uint64_t)taosGetTimestampMs(); return pConn; } @@ -626,7 +625,7 @@ static int32_t mnodeProcessKillConnectionMsg(SMnodeMsg *pMsg) { SCMKillConnMsg *pKill = pMsg->rpcMsg.pCont; int32_t connId = atoi(pKill->queryId); - SConnObj * pConn = taosCacheAcquireByKey(tsMnodeConnCache, &connId, sizeof(int32_t)); + SConnObj *pConn = taosCacheAcquireByKey(tsMnodeConnCache, &connId, sizeof(int32_t)); if (pConn == NULL) { mError("connId:%s, failed to kill, conn not exist", pKill->queryId); return TSDB_CODE_MND_INVALID_CONN_ID; diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index a9bcf948b6..e4e5399a86 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -56,6 +56,14 @@ int syncGetNodesRole(tsync_h shandle, SNodesRole * cfg) { return 0; } void syncConfirmForward(tsync_h shandle, uint64_t version, int32_t code) {} #endif +char* vnodeStatus[] = { + "init", + "ready", + "closing", + "updating", + "reset" +}; + int32_t vnodeInitResources() { int code = syncInit(); if (code != 0) return code; @@ -74,6 +82,7 @@ int32_t vnodeInitResources() { void vnodeCleanupResources() { if (tsDnodeVnodesHash != NULL) { + vDebug("vnode list is cleanup"); taosHashCleanup(tsDnodeVnodesHash); tsDnodeVnodesHash = NULL; } @@ -84,9 +93,10 @@ void vnodeCleanupResources() { int32_t vnodeCreate(SMDCreateVnodeMsg *pVnodeCfg) { int32_t code; - SVnodeObj *pTemp = (SVnodeObj *)taosHashGet(tsDnodeVnodesHash, (const char *)&pVnodeCfg->cfg.vgId, sizeof(int32_t)); - if (pTemp != NULL) { - vInfo("vgId:%d, vnode already exist, pVnode:%p", pVnodeCfg->cfg.vgId, pTemp); + SVnodeObj *pVnode = vnodeAcquire(pVnodeCfg->cfg.vgId); + if (pVnode != NULL) { + vDebug("vgId:%d, vnode already exist, refCount:%d pVnode:%p", pVnodeCfg->cfg.vgId, pVnode->refCount, pVnode); + vnodeRelease(pVnode); return TSDB_CODE_SUCCESS; } @@ -143,22 +153,24 @@ int32_t vnodeCreate(SMDCreateVnodeMsg *pVnodeCfg) { return TSDB_CODE_VND_INIT_FAILED; } - vInfo("vgId:%d, vnode is created, walLevel:%d fsyncPeriod:%d", pVnodeCfg->cfg.vgId, pVnodeCfg->cfg.walLevel, pVnodeCfg->cfg.fsyncPeriod); + vInfo("vgId:%d, vnode dir is created, walLevel:%d fsyncPeriod:%d", pVnodeCfg->cfg.vgId, pVnodeCfg->cfg.walLevel, + pVnodeCfg->cfg.fsyncPeriod); code = vnodeOpen(pVnodeCfg->cfg.vgId, rootDir); return code; } int32_t vnodeDrop(int32_t vgId) { - SVnodeObj **ppVnode = (SVnodeObj **)taosHashGet(tsDnodeVnodesHash, (const char *)&vgId, sizeof(int32_t)); - if (ppVnode == NULL || *ppVnode == NULL) { - vDebug("vgId:%d, failed to drop, vgId not find", vgId); + SVnodeObj *pVnode = vnodeAcquire(vgId); + if (pVnode == NULL) { + vDebug("vgId:%d, failed to drop, vnode not find", vgId); return TSDB_CODE_VND_INVALID_VGROUP_ID; } - SVnodeObj *pVnode = *ppVnode; - vTrace("vgId:%d, vnode will be dropped, refCount:%d", pVnode->vgId, pVnode->refCount); + vInfo("vgId:%d, vnode will be dropped, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); pVnode->dropped = 1; + + vnodeRelease(pVnode); vnodeCleanUp(pVnode); return TSDB_CODE_SUCCESS; @@ -340,11 +352,11 @@ int32_t vnodeOpen(int32_t vnode, char *rootDir) { } int32_t vnodeClose(int32_t vgId) { - SVnodeObj **ppVnode = (SVnodeObj **)taosHashGet(tsDnodeVnodesHash, (const char *)&vgId, sizeof(int32_t)); - if (ppVnode == NULL || *ppVnode == NULL) return 0; + SVnodeObj *pVnode = vnodeAcquire(vgId); + if (pVnode == NULL) return 0; - SVnodeObj *pVnode = *ppVnode; - vDebug("vgId:%d, vnode will be closed", pVnode->vgId); + vDebug("vgId:%d, vnode will be closed, pVnode:%p", pVnode->vgId, pVnode); + vnodeRelease(pVnode); vnodeCleanUp(pVnode); return 0; @@ -355,21 +367,27 @@ void vnodeRelease(void *pVnodeRaw) { int32_t vgId = pVnode->vgId; int32_t refCount = atomic_sub_fetch_32(&pVnode->refCount, 1); + vTrace("vgId:%d, release vnode, refCount:%d pVnode:%p", vgId, refCount, pVnode); assert(refCount >= 0); if (refCount > 0) { - vDebug("vgId:%d, release vnode, refCount:%d", vgId, refCount); - if (pVnode->status == TAOS_VN_STATUS_RESET && refCount == 2) + if (pVnode->status == TAOS_VN_STATUS_RESET && refCount == 2) { tsem_post(&pVnode->sem); + } return; } - qCleanupQueryMgmt(pVnode->qMgmt); - pVnode->qMgmt = NULL; + vDebug("vgId:%d, vnode will be destroyed, refCount:%d pVnode:%p", vgId, refCount, pVnode); - if (pVnode->tsdb) + if (pVnode->qMgmt) { + qCleanupQueryMgmt(pVnode->qMgmt); + pVnode->qMgmt = NULL; + } + + if (pVnode->tsdb) { tsdbCloseRepo(pVnode->tsdb, 1); - pVnode->tsdb = NULL; + pVnode->tsdb = NULL; + } // stop continuous query if (pVnode->cq) { @@ -378,18 +396,21 @@ void vnodeRelease(void *pVnodeRaw) { cqClose(cq); } - if (pVnode->wal) + if (pVnode->wal) { walClose(pVnode->wal); - pVnode->wal = NULL; + pVnode->wal = NULL; + } - if (pVnode->wqueue) + if (pVnode->wqueue) { dnodeFreeVnodeWqueue(pVnode->wqueue); - pVnode->wqueue = NULL; + pVnode->wqueue = NULL; + } - if (pVnode->rqueue) + if (pVnode->rqueue) { dnodeFreeVnodeRqueue(pVnode->rqueue); - pVnode->rqueue = NULL; - + pVnode->rqueue = NULL; + } + taosTFree(pVnode->rootDir); if (pVnode->dropped) { @@ -413,20 +434,20 @@ void vnodeRelease(void *pVnodeRaw) { free(pVnode); int32_t count = taosHashGetSize(tsDnodeVnodesHash); - vDebug("vgId:%d, vnode is released, vnodes:%d", vgId, count); + vDebug("vgId:%d, vnode is destroyed, vnodes:%d", vgId, count); } void *vnodeAcquire(int32_t vgId) { SVnodeObj **ppVnode = (SVnodeObj **)taosHashGet(tsDnodeVnodesHash, (const char *)&vgId, sizeof(int32_t)); if (ppVnode == NULL || *ppVnode == NULL) { terrno = TSDB_CODE_VND_INVALID_VGROUP_ID; - vInfo("vgId:%d, not exist", vgId); + vDebug("vgId:%d, not exist", vgId); return NULL; } SVnodeObj *pVnode = *ppVnode; atomic_add_fetch_32(&pVnode->refCount, 1); - vDebug("vgId:%d, get vnode, refCount:%d", pVnode->vgId, pVnode->refCount); + vTrace("vgId:%d, get vnode, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); return pVnode; } @@ -528,7 +549,7 @@ void vnodeSetAccess(SDMVgroupAccess *pAccess, int32_t numOfVnodes) { if (pVnode != NULL) { pVnode->accessState = pAccess[i].accessState; if (pVnode->accessState != TSDB_VN_ALL_ACCCESS) { - vDebug("vgId:%d, access state is set to %d", pAccess[i].vgId, pVnode->accessState) + vDebug("vgId:%d, access state is set to %d", pAccess[i].vgId, pVnode->accessState); } vnodeRelease(pVnode); } @@ -538,11 +559,12 @@ void vnodeSetAccess(SDMVgroupAccess *pAccess, int32_t numOfVnodes) { static void vnodeCleanUp(SVnodeObj *pVnode) { // remove from hash, so new messages wont be consumed taosHashRemove(tsDnodeVnodesHash, (const char *)&pVnode->vgId, sizeof(int32_t)); - int i = 0; if (pVnode->status != TAOS_VN_STATUS_INIT) { // it may be in updateing or reset state, then it shall wait - while (atomic_val_compare_exchange_8(&pVnode->status, TAOS_VN_STATUS_READY, TAOS_VN_STATUS_CLOSING) != TAOS_VN_STATUS_READY) { + int i = 0; + while (atomic_val_compare_exchange_8(&pVnode->status, TAOS_VN_STATUS_READY, TAOS_VN_STATUS_CLOSING) != + TAOS_VN_STATUS_READY) { if (++i % 1000 == 0) { sched_yield(); } @@ -556,7 +578,7 @@ static void vnodeCleanUp(SVnodeObj *pVnode) { syncStop(sync); } - vTrace("vgId:%d, vnode will cleanup, refCount:%d", pVnode->vgId, pVnode->refCount); + vDebug("vgId:%d, vnode will cleanup, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); // release local resources only after cutting off outside connections qQueryMgmtNotifyClosed(pVnode->qMgmt); @@ -613,17 +635,19 @@ static int vnodeResetTsdb(SVnodeObj *pVnode) char rootDir[128] = "\0"; sprintf(rootDir, "%s/tsdb", pVnode->rootDir); - if (atomic_val_compare_exchange_8(&pVnode->status, TAOS_VN_STATUS_READY, TAOS_VN_STATUS_RESET) != TAOS_VN_STATUS_READY) + if (atomic_val_compare_exchange_8(&pVnode->status, TAOS_VN_STATUS_READY, TAOS_VN_STATUS_RESET) != TAOS_VN_STATUS_READY) { return -1; + } void *tsdb = pVnode->tsdb; pVnode->tsdb = NULL; // acquire vnode - int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1); + int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1); - if (refCount > 2) + if (refCount > 2) { tsem_wait(&pVnode->sem); + } // close tsdb, then open tsdb tsdbCloseRepo(tsdb, 0); diff --git a/src/vnode/src/vnodeWrite.c b/src/vnode/src/vnodeWrite.c index c4924f312f..0966192c94 100644 --- a/src/vnode/src/vnodeWrite.c +++ b/src/vnode/src/vnodeWrite.c @@ -202,7 +202,7 @@ int vnodeWriteCqMsgToQueue(void *param, void *data, int type) { memcpy(pWal, pHead, size); atomic_add_fetch_32(&pVnode->refCount, 1); - vDebug("CQ: vgId:%d, get vnode wqueue, refCount:%d", pVnode->vgId, pVnode->refCount); + vTrace("CQ: vgId:%d, get vnode wqueue, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); taosWriteQitem(pVnode->wqueue, type, pSync); @@ -219,7 +219,7 @@ int vnodeWriteToQueue(void *param, void *data, int type) { memcpy(pWal, pHead, size); atomic_add_fetch_32(&pVnode->refCount, 1); - vDebug("vgId:%d, get vnode wqueue, refCount:%d", pVnode->vgId, pVnode->refCount); + vTrace("vgId:%d, get vnode wqueue, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); taosWriteQitem(pVnode->wqueue, type, pWal); From 438aef99f7c080ffcd4665b5f6e62c8932579a60 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 21 Oct 2020 03:07:22 +0000 Subject: [PATCH 087/101] TD-1661 --- src/vnode/src/vnodeRead.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/vnode/src/vnodeRead.c b/src/vnode/src/vnodeRead.c index baaeae2a81..2a39e2e76f 100644 --- a/src/vnode/src/vnodeRead.c +++ b/src/vnode/src/vnodeRead.c @@ -38,8 +38,7 @@ void vnodeInitReadFp(void) { vnodeProcessReadMsgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessFetchMsg; } -int32_t vnodeProcessRead(void *param, SReadMsg *pReadMsg) { - SVnodeObj *pVnode = (SVnodeObj *)param; +static int32_t vnodeProcessReadImp(SVnodeObj *pVnode, SReadMsg *pReadMsg) { int msgType = pReadMsg->rpcMsg.msgType; if (vnodeProcessReadMsgFp[msgType] == NULL) { @@ -48,16 +47,23 @@ int32_t vnodeProcessRead(void *param, SReadMsg *pReadMsg) { } if (pVnode->status != TAOS_VN_STATUS_READY) { - vDebug("vgId:%d, msgType:%s not processed, vnode status is %d", pVnode->vgId, taosMsg[msgType], pVnode->status); + vDebug("vgId:%d, msgType:%s not processed, vnode status is %s", pVnode->vgId, taosMsg[msgType], + vnodeStatus[pVnode->status]); return TSDB_CODE_APP_NOT_READY; } // tsdb may be in reset state - if (pVnode->tsdb == NULL) return TSDB_CODE_APP_NOT_READY; - if (pVnode->status == TAOS_VN_STATUS_CLOSING) return TSDB_CODE_APP_NOT_READY; + if (pVnode->tsdb == NULL) { + vDebug("vgId:%d, msgType:%s not processed, tsdb is null", pVnode->vgId, taosMsg[msgType]); + return TSDB_CODE_APP_NOT_READY; + } + + if (pVnode->status == TAOS_VN_STATUS_CLOSING) { + vDebug("vgId:%d, msgType:%s not processed, vstatus is %s", pVnode->vgId, taosMsg[msgType], + vnodeStatus[pVnode->status]); + return TSDB_CODE_APP_NOT_READY; + } - // TODO: Later, let slave to support query - // if (pVnode->syncCfg.replica > 1 && pVnode->role != TAOS_SYNC_ROLE_MASTER) { if (pVnode->role != TAOS_SYNC_ROLE_SLAVE && pVnode->role != TAOS_SYNC_ROLE_MASTER) { vDebug("vgId:%d, msgType:%s not processed, replica:%d role:%s", pVnode->vgId, taosMsg[msgType], pVnode->syncCfg.replica, syncRole[pVnode->role]); @@ -67,6 +73,25 @@ int32_t vnodeProcessRead(void *param, SReadMsg *pReadMsg) { return (*vnodeProcessReadMsgFp[msgType])(pVnode, pReadMsg); } +int32_t vnodeProcessRead(void *param, SReadMsg *pRead) { + SVnodeObj *pVnode = (SVnodeObj *)param; + int32_t code = vnodeProcessReadImp(pVnode, pRead); + + if (code == TSDB_CODE_APP_NOT_READY && pRead->rpcMsg.msgType == TSDB_MSG_TYPE_QUERY) { + // After the fetch request enters the vnode queue + // If the vnode cannot provide services, the following operations are still required + // Or, there will be a deadlock + void **qhandle = (void **)pRead->pCont; + vError("QInfo:%p msg:%p will be killed for vstatus is %s", *qhandle, pRead, vnodeStatus[pVnode->status]); + + // qKillQuery(*qhandle); + // qReleaseQInfo(pVnode->qMgmt, (void **)&qhandle, true); + return TSDB_CODE_APP_NOT_READY; + } else { + return code; + } +} + static void vnodePutItemIntoReadQueue(SVnodeObj *pVnode, void **qhandle) { SReadMsg *pRead = (SReadMsg *)taosAllocateQitem(sizeof(SReadMsg)); pRead->rpcMsg.msgType = TSDB_MSG_TYPE_QUERY; From 374987e039c1c914c43fbeb5b7163d7e5144c9d0 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 21 Oct 2020 03:29:14 +0000 Subject: [PATCH 088/101] TD-1746 --- src/vnode/src/vnodeMain.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index e4e5399a86..bc728e86d9 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -437,19 +437,28 @@ void vnodeRelease(void *pVnodeRaw) { vDebug("vgId:%d, vnode is destroyed, vnodes:%d", vgId, count); } +static void vnodeIncRef(void *ptNode) { + assert(ptNode != NULL); + + SVnodeObj **ppVnode = (SVnodeObj **)ptNode; + assert(ppVnode); + assert(*ppVnode); + + SVnodeObj *pVnode = *ppVnode; + atomic_add_fetch_32(&pVnode->refCount, 1); + vTrace("vgId:%d, get vnode, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); +} + void *vnodeAcquire(int32_t vgId) { - SVnodeObj **ppVnode = (SVnodeObj **)taosHashGet(tsDnodeVnodesHash, (const char *)&vgId, sizeof(int32_t)); + SVnodeObj **ppVnode = taosHashGetCB(tsDnodeVnodesHash, &vgId, sizeof(int32_t), vnodeIncRef, NULL, sizeof(void *)); + if (ppVnode == NULL || *ppVnode == NULL) { terrno = TSDB_CODE_VND_INVALID_VGROUP_ID; vDebug("vgId:%d, not exist", vgId); return NULL; } - SVnodeObj *pVnode = *ppVnode; - atomic_add_fetch_32(&pVnode->refCount, 1); - vTrace("vgId:%d, get vnode, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); - - return pVnode; + return *ppVnode; } void *vnodeAcquireRqueue(int32_t vgId) { From 196b03f1bdf595f323affd87128ad0ad2e9eaad2 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 21 Oct 2020 03:37:24 +0000 Subject: [PATCH 089/101] remove debug codes add by hjliao --- src/client/src/tscSql.c | 36 ------------------------------------ src/client/src/tscUtil.c | 12 ------------ 2 files changed, 48 deletions(-) diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index 1ccf9c5ec4..7594a73c96 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -590,18 +590,6 @@ static UNUSED_FUNC bool tscKillQueryInDnode(SSqlObj* pSql) { SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); - if (pQueryInfo != 0) { - STableMetaInfo *pTableMetaInfo1 = tscGetMetaInfo(pQueryInfo, 0); - if (pTableMetaInfo1 != NULL) { - // for select query super table, the super table vgroup list can not be null in any cases. - if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo1)) { - if (pTableMetaInfo1->pVgroupTables == 0) { - tscError("error !!!%p, vgroupTable is null", pSql); - } - } - } - } - if ((pQueryInfo == NULL) || tscIsTwoStageSTableQuery(pQueryInfo, 0)) { return true; } @@ -716,18 +704,6 @@ static void tscKillSTableQuery(SSqlObj *pSql) { SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex); - if (pQueryInfo != 0) { - STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - if (pTableMetaInfo != NULL) { - // for select query super table, the super table vgroup list can not be null in any cases. - if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { - if (pTableMetaInfo->pVgroupTables == 0) { - tscError("error !!!%p, vgroupTable is null", pSql); - } - } - } - } - if (!tscIsTwoStageSTableQuery(pQueryInfo, 0)) { return; } @@ -777,18 +753,6 @@ void taos_stop_query(TAOS_RES *res) { SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex); - if (pQueryInfo != 0) { - STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - if (pTableMetaInfo != NULL) { - // for select query super table, the super table vgroup list can not be null in any cases. - if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { - if (pTableMetaInfo->pVgroupTables == 0) { - tscError("error !!!%p, vgroupTable is null", pSql); - } - } - } - } - if (tscIsTwoStageSTableQuery(pQueryInfo, 0)) { assert(pSql->pRpcCtx == NULL); tscKillSTableQuery(pSql); diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index baa733ef49..a38be04ae0 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -2098,18 +2098,6 @@ void tscDoQuery(SSqlObj* pSql) { } else { SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, pCmd->clauseIndex); uint16_t type = pQueryInfo->type; - - if (pQueryInfo != 0) { - STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - if (pTableMetaInfo != NULL) { - // for select query super table, the super table vgroup list can not be null in any cases. - if (pQueryInfo->command == TSDB_SQL_SELECT && UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { - if (pTableMetaInfo->pVgroupTables == 0) { - tscError("error !!!%p, vgroupTable is null", pSql); - } - } - } - } if (pSql->fp == (void(*)())tscHandleMultivnodeInsert) { // multi-vnodes insertion tscHandleMultivnodeInsert(pSql); From 2c7a9137b548e7424d94e63fadb2e639383717ce Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Wed, 21 Oct 2020 13:45:44 +0800 Subject: [PATCH 090/101] modify test case --- Jenkinsfile | 16 +++ tests/pytest/concurrent_inquiry.py | 154 +++++++++++++++-------------- 2 files changed, 94 insertions(+), 76 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 6d563d132b..08b6121f12 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -118,6 +118,22 @@ pipeline { date''' } } + stage('connector'){ + agent{label "release"} + steps{ + sh''' + cd ${WORKSPACE} + git checkout develop + cd tests/gotest + bash batchtest.sh + cd ${WORKSPACE}/tests/examples/JDBC/JDBCDemo/ + mvn clean package assembly:single >/dev/null + java -jar target/jdbcChecker-SNAPSHOT-jar-with-dependencies.jar -host 127.0.0.1 + cd ${WORKSPACE}/tests/examples/python/PYTHONConnectorChecker + python3 PythonChecker.py + ''' + } + } } } diff --git a/tests/pytest/concurrent_inquiry.py b/tests/pytest/concurrent_inquiry.py index 2460900891..39a4cb48fd 100644 --- a/tests/pytest/concurrent_inquiry.py +++ b/tests/pytest/concurrent_inquiry.py @@ -12,108 +12,105 @@ # -*- coding: utf-8 -*- import threading import taos - +import sys import json import time import random # query sql query_sql = [ # first supertable -"select count(*) from test.meters where c1 > 50;", -"select count(*) from test.meters where c2 >= 50 and c2 < 100;", -"select count(*) from test.meters where c3 != 5;", +"select count(*) from test.meters ;", "select count(*) from test.meters where t3 > 2;", "select count(*) from test.meters where ts <> '2020-05-13 10:00:00.002';", -"select count(*) from test.meters where t7 like 'fi%';", -"select count(*) from test.meters where t7 like '_econd';", +"select count(*) from test.meters where t7 like 'taos_1%';", +"select count(*) from test.meters where t7 like '_____2';", +"select count(*) from test.meters where t8 like '%思%';", "select count(*) from test.meters interval(1n) order by ts desc;", -"select max(c0) from test.meters group by tbname", -"select first(*) from test.meters;", -"select last(*) from test.meters;", +#"select max(c0) from test.meters group by tbname", +"select first(ts) from test.meters where t5 >5000 and t5<5100;", +"select last(ts) from test.meters where t5 >5000 and t5<5100;", "select last_row(*) from test.meters;", "select twa(c1) from test.t1 where ts > 1500000001000 and ts < 1500000101000" , -"select avg(c1) from test.meters;", +"select avg(c1) from test.meters where t5 >5000 and t5<5100;", "select bottom(c1, 2) from test.t1;", "select diff(c1) from test.t1;", "select leastsquares(c1, 1, 1) from test.t1 ;", -"select max(c1) from test.meters;", -"select min(c1) from test.meters;", -"select c1 + c2 * c3 + c1 / c5 + c4 + c2 from test.t1;", +"select max(c1) from test.meters where t5 >5000 and t5<5100;", +"select min(c1) from test.meters where t5 >5000 and t5<5100;", +"select c1 + c2 + c1 / c5 + c4 + c2 from test.t1;", "select percentile(c1, 50) from test.t1;", "select spread(c1) from test.t1 ;", "select stddev(c1) from test.t1;", -"select sum(c1) from test.meters;", -"select top(c1, 2) from test.meters;" -"select twa(c6) from test.t1 where ts > 1500000001000 and ts < 1500000101000" , -"select avg(c6) from test.meters;", -"select bottom(c6, 2) from test.t1;", -"select diff(c6) from test.t1;", -"select leastsquares(c6, 1, 1) from test.t1 ;", -"select max(c6) from test.meters;", -"select min(c6) from test.meters;", -"select c6 + c2 * c3 + c6 / c5 + c4 + c2 from test.t1;", -"select percentile(c6, 50) from test.t1;", -"select spread(c6) from test.t1 ;", -"select stddev(c6) from test.t1;", -"select sum(c6) from test.meters;", -"select top(c6, 2) from test.meters;", +"select sum(c1) from test.meters where t5 >5000 and t5<5100;", +"select top(c1, 2) from test.meters where t5 >5000 and t5<5100;" +"select twa(c4) from test.t1 where ts > 1500000001000 and ts < 1500000101000" , +"select avg(c4) from test.meters where t5 >5000 and t5<5100;", +"select bottom(c4, 2) from test.t1 where t5 >5000 and t5<5100;", +"select diff(c4) from test.t1 where t5 >5000 and t5<5100;", +"select leastsquares(c4, 1, 1) from test.t1 ;", +"select max(c4) from test.meters where t5 >5000 and t5<5100;", +"select min(c4) from test.meters where t5 >5000 and t5<5100;", +"select c5 + c2 + c4 / c5 + c4 + c2 from test.t1 ;", +"select percentile(c5, 50) from test.t1;", +"select spread(c5) from test.t1 ;", +"select stddev(c5) from test.t1 where t5 >5000 and t5<5100;", +"select sum(c5) from test.meters where t5 >5000 and t5<5100;", +"select top(c5, 2) from test.meters where t5 >5000 and t5<5100;", #all vnode -"select count(*) from test.meters where t5 >2500 and t5<7500", -"select max(c0),avg(c1) from test.meters where t5 >2500 and t5<7500", -"select sum(c5),avg(c1) from test.meters where t5 >2500 and t5<7500", -"select max(c0),min(c6) from test.meters where t5 >2500 and t5<7500", -"select min(c0),avg(c6) from test.meters where t5 >2500 and t5<7500", +"select count(*) from test.meters where t5 >5000 and t5<5100", +"select max(c0),avg(c1) from test.meters where t5 >5000 and t5<5100", +"select sum(c5),avg(c1) from test.meters where t5 >5000 and t5<5100", +"select max(c0),min(c5) from test.meters where t5 >5000 and t5<5100", +"select min(c0),avg(c5) from test.meters where t5 >5000 and t5<5100", # second supertable -"select count(*) from test.meters1 where c1 > 50;", -"select count(*) from test.meters1 where c2 >= 50 and c2 < 100;", -"select count(*) from test.meters1 where c3 != 5;", "select count(*) from test.meters1 where t3 > 2;", "select count(*) from test.meters1 where ts <> '2020-05-13 10:00:00.002';", -"select count(*) from test.meters1 where t7 like 'fi%';", -"select count(*) from test.meters1 where t7 like '_econd';", +"select count(*) from test.meters where t7 like 'taos_1%';", +"select count(*) from test.meters where t7 like '_____2';", +"select count(*) from test.meters where t8 like '%思%';", "select count(*) from test.meters1 interval(1n) order by ts desc;", -"select max(c0) from test.meters1 group by tbname", -"select first(*) from test.meters1;", -"select last(*) from test.meters1;", -"select last_row(*) from test.meters1;", +#"select max(c0) from test.meters1 group by tbname", +"select first(ts) from test.meters1 where t5 >5000 and t5<5100;", +"select last(ts) from test.meters1 where t5 >5000 and t5<5100;", +"select last_row(*) from test.meters1 ;", "select twa(c1) from test.m1 where ts > 1500000001000 and ts < 1500000101000" , -"select avg(c1) from test.meters1;", -"select bottom(c1, 2) from test.m1;", -"select diff(c1) from test.m1;", +"select avg(c1) from test.meters1 where t5 >5000 and t5<5100;", +"select bottom(c1, 2) from test.m1 where t5 >5000 and t5<5100;", +"select diff(c1) from test.m1 ;", "select leastsquares(c1, 1, 1) from test.m1 ;", -"select max(c1) from test.meters1;", -"select min(c1) from test.meters1;", -"select c1 + c2 * c3 + c1 / c5 + c3 + c2 from test.m1;", +"select max(c1) from test.meters1 where t5 >5000 and t5<5100;", +"select min(c1) from test.meters1 where t5 >5000 and t5<5100;", +"select c1 + c2 + c1 / c0 + c2 from test.m1 ;", "select percentile(c1, 50) from test.m1;", "select spread(c1) from test.m1 ;", "select stddev(c1) from test.m1;", -"select sum(c1) from test.meters1;", -"select top(c1, 2) from test.meters1;", -"select twa(c6) from test.m1 where ts > 1500000001000 and ts < 1500000101000" , -"select avg(c6) from test.meters1;", -"select bottom(c6, 2) from test.m1;", -"select diff(c6) from test.m1;", -"select leastsquares(c6, 1, 1) from test.m1 ;", -"select max(c6) from test.meters1;", -"select min(c6) from test.meters1;", -"select c6 + c2 * c3 + c6 / c5 + c3 + c2 from test.m1;", -"select percentile(c6, 50) from test.m1;", -"select spread(c6) from test.m1 ;", -"select stddev(c6) from test.m1;", -"select sum(c6) from test.meters1;", -"select top(c6, 2) from test.meters1;", -"select count(*) from test.meters1 where t5 >2500 and t5<7500", +"select sum(c1) from test.meters1 where t5 >5000 and t5<5100;", +"select top(c1, 2) from test.meters1 where t5 >5000 and t5<5100;", +"select twa(c5) from test.m1 where ts > 1500000001000 and ts < 1500000101000" , +"select avg(c5) from test.meters1 where t5 >5000 and t5<5100;", +"select bottom(c5, 2) from test.m1;", +"select diff(c5) from test.m1;", +"select leastsquares(c5, 1, 1) from test.m1 ;", +"select max(c5) from test.meters1 where t5 >5000 and t5<5100;", +"select min(c5) from test.meters1 where t5 >5000 and t5<5100;", +"select c5 + c2 + c4 / c5 + c0 from test.m1;", +"select percentile(c4, 50) from test.m1;", +"select spread(c4) from test.m1 ;", +"select stddev(c4) from test.m1;", +"select sum(c4) from test.meters1 where t5 >5100 and t5<5300;", +"select top(c4, 2) from test.meters1 where t5 >5100 and t5<5300;", +"select count(*) from test.meters1 where t5 >5100 and t5<5300", #all vnode -"select count(*) from test.meters1 where t5 >2500 and t5<7500", -"select max(c0),avg(c1) from test.meters1 where t5 >2500 and t5<7500", -"select sum(c5),avg(c1) from test.meters1 where t5 >2500 and t5<7500", -"select max(c0),min(c6) from test.meters1 where t5 >2500 and t5<7500", -"select min(c0),avg(c6) from test.meters1 where t5 >2500 and t5<7500", +"select count(*) from test.meters1 where t5 >5100 and t5<5300", +"select max(c0),avg(c1) from test.meters1 where t5 >5000 and t5<5100", +"select sum(c5),avg(c1) from test.meters1 where t5 >5000 and t5<5100", +"select max(c0),min(c5) from test.meters1 where t5 >5000 and t5<5100", +"select min(c0),avg(c5) from test.meters1 where t5 >5000 and t5<5100", #join -"select * from meters,meters1 where meters.ts = meters1.ts and meters.t5 = meters1.t5", -"select * from meters,meters1 where meters.ts = meters1.ts and meters.t7 = meters1.t7", -"select * from meters,meters1 where meters.ts = meters1.ts and meters.t8 = meters1.t8", -"select meters.ts,meters1.c2 from meters,meters1 where meters.ts = meters1.ts and meters.t8 = meters1.t8" +# "select * from meters,meters1 where meters.ts = meters1.ts and meters.t5 = meters1.t5", +# "select * from meters,meters1 where meters.ts = meters1.ts and meters.t7 = meters1.t7", +# "select * from meters,meters1 where meters.ts = meters1.ts and meters.t8 = meters1.t8", +# "select meters.ts,meters1.c2 from meters,meters1 where meters.ts = meters1.ts and meters.t8 = meters1.t8" ] class ConcurrentInquiry: @@ -121,7 +118,8 @@ class ConcurrentInquiry: self.numOfTherads = 50 self.ts=1500000001000 - + def SetThreadsNum(self,num): + self.numOfTherads=num def query_thread(self,threadID): host = "10.211.55.14" user = "root" @@ -142,12 +140,16 @@ class ConcurrentInquiry: for i in ran_query_sql: print("Thread %d : %s"% (threadID,i)) try: + start = time.time() cl.execute(i) cl.fetchall + end = time.time() + print("time cost :",end-start) except Exception as e: print( "Failure thread%d, sql: %s,exception: %s" % (threadID, str(i),str(e))) + exit(-1) print("Thread %d: finishing" % threadID) @@ -155,9 +157,9 @@ class ConcurrentInquiry: def run(self): - + threads = [] - for i in range(50): + for i in range(self.numOfTherads): thread = threading.Thread(target=self.query_thread, args=(i,)) threads.append(thread) thread.start() From a170da720130b053c806b4ebd412d4dc99ca55ff Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 21 Oct 2020 13:49:31 +0800 Subject: [PATCH 091/101] [td-1664] --- src/client/src/tscSQLParser.c | 18 +++++++++- src/query/src/qSyntaxtreefunction.c | 3 ++ .../parser/col_arithmetic_operation.sim | 35 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index c8e80790ef..b28dd9cf1c 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -3385,10 +3385,26 @@ static int32_t validateSQLExpr(SSqlCmd* pCmd, tSQLExpr* pExpr, SQueryInfo* pQuer tSQLExprItem item = {.pNode = pExpr, .aliasName = NULL}; - // sql function in selection clause, append sql function info in pSqlCmd structure sequentially + // sql function list in selection clause. + // Append the sqlExpr into exprList of pQueryInfo structure sequentially if (addExprAndResultField(pCmd, pQueryInfo, outputIndex, &item, false) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_SQL; } + + // It is invalid in case of more than one sqlExpr, such as first(ts, k) - last(ts, k) + int32_t inc = (int32_t) tscSqlExprNumOfExprs(pQueryInfo) - outputIndex; + if (inc > 1) { + return TSDB_CODE_TSC_INVALID_SQL; + } + + // Not supported data type in arithmetic expression + for(int32_t i = 0; i < inc; ++i) { + SSqlExpr* p1 = tscSqlExprGet(pQueryInfo, i + outputIndex); + int16_t t = p1->resType; + if (t == TSDB_DATA_TYPE_BINARY || t == TSDB_DATA_TYPE_NCHAR || t == TSDB_DATA_TYPE_BOOL || t == TSDB_DATA_TYPE_TIMESTAMP) { + return TSDB_CODE_TSC_INVALID_SQL; + } + } } return TSDB_CODE_SUCCESS; diff --git a/src/query/src/qSyntaxtreefunction.c b/src/query/src/qSyntaxtreefunction.c index 2104edfd91..7f7fca2c1e 100644 --- a/src/query/src/qSyntaxtreefunction.c +++ b/src/query/src/qSyntaxtreefunction.c @@ -1247,7 +1247,10 @@ _bi_consumer_fn_t tGetBiConsumerFn(int32_t leftType, int32_t rightType, int32_t case TSDB_BINARY_OP_REMAINDER: return rem_function_arraylist[leftType][rightType]; default: + assert(0); return NULL; } + + assert(0); return NULL; } diff --git a/tests/script/general/parser/col_arithmetic_operation.sim b/tests/script/general/parser/col_arithmetic_operation.sim index e1557227d4..4977e906d7 100644 --- a/tests/script/general/parser/col_arithmetic_operation.sim +++ b/tests/script/general/parser/col_arithmetic_operation.sim @@ -109,4 +109,39 @@ if $data02 != 225000 then return -1 endi +sql select first(speed) - last(speed), first(speed) as b, last(speed) as b, min(speed) - max(speed), spread(speed) from stb interval(1y)", NULL); +if $rows != 1 then + return -1 +endi + +if $data00 != @20-01-01 00:00:00.000@ then + return -1 +endi + +if $data01 != -20.000000000 then + return -1 +endi + +if $data02 != 100.000000000 then + return -1 +endi + +if $data03 != 120.000000000 then + return -1 +endi + +if $data04 != -20.000000000 then + return -1 +endi + +if $data05 != 20.000000000 then + return -1 +endi + +sql_error select first(speed, mileage) - last(speed, mileage) from stb interval(1y) +sql_error select first(ts) - last(ts) from stb interval(1y) +sql_error select top(speed, 2) - last(speed) from stb; +sql_error select stddev(speed) - last(speed) from stb; +sql_error select diff(speed) - last(speed) from stb; + system sh/exec.sh -n dnode1 -s stop -x SIGINT \ No newline at end of file From 84d24478e62f952b8a26e5c7e6a66680b1e09d50 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 21 Oct 2020 14:21:11 +0800 Subject: [PATCH 092/101] [td-1664] --- .../parser/col_arithmetic_operation.sim | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/tests/script/general/parser/col_arithmetic_operation.sim b/tests/script/general/parser/col_arithmetic_operation.sim index 4977e906d7..3208df95e4 100644 --- a/tests/script/general/parser/col_arithmetic_operation.sim +++ b/tests/script/general/parser/col_arithmetic_operation.sim @@ -109,39 +109,42 @@ if $data02 != 225000 then return -1 endi -sql select first(speed) - last(speed), first(speed) as b, last(speed) as b, min(speed) - max(speed), spread(speed) from stb interval(1y)", NULL); +sql select first(c1) - last(c1), first(c1) as b, last(c1) as b, min(c1) - max(c1), spread(c1) from ca_stb0 interval(1y) if $rows != 1 then return -1 endi -if $data00 != @20-01-01 00:00:00.000@ then +if $data00 != @18-01-01 00:00:00.000@ then return -1 endi -if $data01 != -20.000000000 then +if $data01 != -9.000000000 then return -1 endi -if $data02 != 100.000000000 then +if $data02 != 0 then return -1 endi -if $data03 != 120.000000000 then +if $data03 != 9 then return -1 endi -if $data04 != -20.000000000 then +if $data04 != -9.000000000 then return -1 endi -if $data05 != 20.000000000 then +if $data05 != 9.000000000 then return -1 endi -sql_error select first(speed, mileage) - last(speed, mileage) from stb interval(1y) +sql_error select first(c1, c2) - last(c1, c2) from stb interval(1y) sql_error select first(ts) - last(ts) from stb interval(1y) -sql_error select top(speed, 2) - last(speed) from stb; -sql_error select stddev(speed) - last(speed) from stb; -sql_error select diff(speed) - last(speed) from stb; +sql_error select top(c1, 2) - last(c1) from stb; +sql_error select stddev(c1) - last(c1) from stb; +sql_error select diff(c1) - last(c1) from stb; +sql_error select first(c7) - last(c7) from stb; +sql_error select first(c8) - last(c8) from stb; +sql_error select first(c9) - last(c9) from stb; system sh/exec.sh -n dnode1 -s stop -x SIGINT \ No newline at end of file From 478fd09f288c15e883edc9e2325a148acd9ba0db Mon Sep 17 00:00:00 2001 From: Bomin Zhang Date: Wed, 21 Oct 2020 14:59:49 +0800 Subject: [PATCH 093/101] fix TD-1758 --- src/client/src/tscUtil.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index a38be04ae0..e129657acc 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -686,16 +686,14 @@ static int32_t getRowExpandSize(STableMeta* pTableMeta) { int32_t tscMergeTableDataBlocks(SSqlObj* pSql, SArray* pTableDataBlockList) { SSqlCmd* pCmd = &pSql->cmd; - // the maximum expanded size in byte when a row-wise data is converted to SDataRow format - STableDataBlocks* pOneTableBlock = taosArrayGetP(pTableDataBlockList, 0); - int32_t expandSize = getRowExpandSize(pOneTableBlock->pTableMeta); - void* pVnodeDataBlockHashList = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, false); SArray* pVnodeDataBlockList = taosArrayInit(8, POINTER_BYTES); size_t total = taosArrayGetSize(pTableDataBlockList); for (int32_t i = 0; i < total; ++i) { - pOneTableBlock = taosArrayGetP(pTableDataBlockList, i); + // the maximum expanded size in byte when a row-wise data is converted to SDataRow format + STableDataBlocks* pOneTableBlock = taosArrayGetP(pTableDataBlockList, i); + int32_t expandSize = getRowExpandSize(pOneTableBlock->pTableMeta); STableDataBlocks* dataBuf = NULL; int32_t ret = From c3a31c681d158964363139c33b5e61936aab071f Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 21 Oct 2020 15:11:57 +0800 Subject: [PATCH 094/101] [td-1765] --- src/client/src/tscSQLParser.c | 2 +- tests/script/general/parser/tags_dynamically_specifiy.sim | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index b28dd9cf1c..e2573f7e19 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -4750,7 +4750,7 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { // validate the length of binary if ((pTagsSchema->type == TSDB_DATA_TYPE_BINARY || pTagsSchema->type == TSDB_DATA_TYPE_NCHAR) && - (pVarList->a[1].pVar.nLen + VARSTR_HEADER_SIZE) > pTagsSchema->bytes) { + varDataTLen(pAlterSQL->tagData.data) > pTagsSchema->bytes) { return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg14); } diff --git a/tests/script/general/parser/tags_dynamically_specifiy.sim b/tests/script/general/parser/tags_dynamically_specifiy.sim index 0a5d5c9716..07bf4d8dd1 100644 --- a/tests/script/general/parser/tags_dynamically_specifiy.sim +++ b/tests/script/general/parser/tags_dynamically_specifiy.sim @@ -96,5 +96,10 @@ if $rows != 14 then return -1 endi +print ===============================> td-1765 +sql create table m1(ts timestamp, k int) tags(a binary(4), b nchar(4)); +sql create table tm0 using m1 tags('abcd', 'abcd'); +sql_error alter table tm0 set tag b = 'abcd1'; +sql_error alter table tm0 set tag a = 'abcd1'; system sh/exec.sh -n dnode1 -s stop -x SIGINT \ No newline at end of file From 19cced6f0ec45ae8e31c368ceb3f40f58859f865 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 21 Oct 2020 15:29:44 +0800 Subject: [PATCH 095/101] TD-1768 --- src/inc/vnode.h | 2 ++ src/vnode/src/vnodeMain.c | 16 +++++---- src/vnode/src/vnodeRead.c | 71 +++++++++++++++++++------------------- src/vnode/src/vnodeWrite.c | 31 ++++++++++++----- 4 files changed, 69 insertions(+), 51 deletions(-) diff --git a/src/inc/vnode.h b/src/inc/vnode.h index ed6c232fdb..fdce4d6279 100644 --- a/src/inc/vnode.h +++ b/src/inc/vnode.h @@ -56,6 +56,7 @@ void vnodeRelease(void *pVnode); // dec refCount void* vnodeGetWal(void *pVnode); int32_t vnodeProcessWrite(void *pVnode, int qtype, void *pHead, void *item); +int32_t vnodeCheckWrite(void *pVnode); int32_t vnodeGetVnodeList(int32_t vnodeList[], int32_t *numOfVnodes); void vnodeBuildStatusMsg(void *param); void vnodeConfirmForward(void *param, uint64_t version, int32_t code); @@ -65,6 +66,7 @@ int32_t vnodeInitResources(); void vnodeCleanupResources(); int32_t vnodeProcessRead(void *pVnode, SReadMsg *pReadMsg); +int32_t vnodeCheckRead(void *pVnode); #ifdef __cplusplus } diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index bc728e86d9..e529f27f55 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -465,9 +465,10 @@ void *vnodeAcquireRqueue(int32_t vgId) { SVnodeObj *pVnode = vnodeAcquire(vgId); if (pVnode == NULL) return NULL; - if (pVnode->status == TAOS_VN_STATUS_RESET) { - terrno = TSDB_CODE_APP_NOT_READY; - vInfo("vgId:%d, status is in reset", vgId); + int32_t code = vnodeCheckRead(pVnode); + if (code != TSDB_CODE_SUCCESS) { + terrno = code; + vInfo("vgId:%d, can not provide read service, status is %s", vgId, vnodeStatus[pVnode->status]); vnodeRelease(pVnode); return NULL; } @@ -479,13 +480,14 @@ void *vnodeAcquireWqueue(int32_t vgId) { SVnodeObj *pVnode = vnodeAcquire(vgId); if (pVnode == NULL) return NULL; - if (pVnode->status == TAOS_VN_STATUS_RESET) { - terrno = TSDB_CODE_APP_NOT_READY; - vInfo("vgId:%d, status is in reset", vgId); + int32_t code = vnodeCheckWrite(pVnode); + if (code != TSDB_CODE_SUCCESS) { + terrno = code; + vInfo("vgId:%d, can not provide write service, status is %s", vgId, vnodeStatus[pVnode->status]); vnodeRelease(pVnode); return NULL; } - + return pVnode->wqueue; } diff --git a/src/vnode/src/vnodeRead.c b/src/vnode/src/vnodeRead.c index 2a39e2e76f..0ad85bbf91 100644 --- a/src/vnode/src/vnodeRead.c +++ b/src/vnode/src/vnodeRead.c @@ -38,7 +38,13 @@ void vnodeInitReadFp(void) { vnodeProcessReadMsgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessFetchMsg; } -static int32_t vnodeProcessReadImp(SVnodeObj *pVnode, SReadMsg *pReadMsg) { +// +// After the fetch request enters the vnode queue, if the vnode cannot provide services, the process function are +// still required, or there will be a deadlock, so we don’t do any check here, but put the check codes before the +// request enters the queue +// +int32_t vnodeProcessRead(void *param, SReadMsg *pReadMsg) { + SVnodeObj *pVnode = (SVnodeObj *)param; int msgType = pReadMsg->rpcMsg.msgType; if (vnodeProcessReadMsgFp[msgType] == NULL) { @@ -46,53 +52,36 @@ static int32_t vnodeProcessReadImp(SVnodeObj *pVnode, SReadMsg *pReadMsg) { return TSDB_CODE_VND_MSG_NOT_PROCESSED; } + return (*vnodeProcessReadMsgFp[msgType])(pVnode, pReadMsg); +} + +int32_t vnodeCheckRead(void *param) { + SVnodeObj *pVnode = param; if (pVnode->status != TAOS_VN_STATUS_READY) { - vDebug("vgId:%d, msgType:%s not processed, vnode status is %s", pVnode->vgId, taosMsg[msgType], - vnodeStatus[pVnode->status]); + vDebug("vgId:%d, vnode status is %s, recCount:%d pVnode:%p", pVnode->vgId, vnodeStatus[pVnode->status], + pVnode->refCount, pVnode); return TSDB_CODE_APP_NOT_READY; } // tsdb may be in reset state if (pVnode->tsdb == NULL) { - vDebug("vgId:%d, msgType:%s not processed, tsdb is null", pVnode->vgId, taosMsg[msgType]); - return TSDB_CODE_APP_NOT_READY; - } - - if (pVnode->status == TAOS_VN_STATUS_CLOSING) { - vDebug("vgId:%d, msgType:%s not processed, vstatus is %s", pVnode->vgId, taosMsg[msgType], - vnodeStatus[pVnode->status]); + vDebug("vgId:%d, tsdb is null, recCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); return TSDB_CODE_APP_NOT_READY; } if (pVnode->role != TAOS_SYNC_ROLE_SLAVE && pVnode->role != TAOS_SYNC_ROLE_MASTER) { - vDebug("vgId:%d, msgType:%s not processed, replica:%d role:%s", pVnode->vgId, taosMsg[msgType], - pVnode->syncCfg.replica, syncRole[pVnode->role]); + vDebug("vgId:%d, replica:%d role:%s, recCount:%d pVnode:%p", pVnode->vgId, pVnode->syncCfg.replica, + syncRole[pVnode->role], pVnode->refCount, pVnode); return TSDB_CODE_APP_NOT_READY; } - return (*vnodeProcessReadMsgFp[msgType])(pVnode, pReadMsg); + return TSDB_CODE_SUCCESS; } -int32_t vnodeProcessRead(void *param, SReadMsg *pRead) { - SVnodeObj *pVnode = (SVnodeObj *)param; - int32_t code = vnodeProcessReadImp(pVnode, pRead); +static int32_t vnodePutItemIntoReadQueue(SVnodeObj *pVnode, void **qhandle) { + int32_t code = vnodeCheckRead(pVnode); + if (code != TSDB_CODE_SUCCESS) return code; - if (code == TSDB_CODE_APP_NOT_READY && pRead->rpcMsg.msgType == TSDB_MSG_TYPE_QUERY) { - // After the fetch request enters the vnode queue - // If the vnode cannot provide services, the following operations are still required - // Or, there will be a deadlock - void **qhandle = (void **)pRead->pCont; - vError("QInfo:%p msg:%p will be killed for vstatus is %s", *qhandle, pRead, vnodeStatus[pVnode->status]); - - // qKillQuery(*qhandle); - // qReleaseQInfo(pVnode->qMgmt, (void **)&qhandle, true); - return TSDB_CODE_APP_NOT_READY; - } else { - return code; - } -} - -static void vnodePutItemIntoReadQueue(SVnodeObj *pVnode, void **qhandle) { SReadMsg *pRead = (SReadMsg *)taosAllocateQitem(sizeof(SReadMsg)); pRead->rpcMsg.msgType = TSDB_MSG_TYPE_QUERY; pRead->pCont = qhandle; @@ -103,6 +92,8 @@ static void vnodePutItemIntoReadQueue(SVnodeObj *pVnode, void **qhandle) { vDebug("QInfo:%p add to vread queue for exec query, msg:%p", *qhandle, pRead); taosWriteQitem(pVnode->rqueue, TAOS_QTYPE_QUERY, pRead); + + return TSDB_CODE_SUCCESS; } static int32_t vnodeDumpQueryResult(SRspRet *pRet, void *pVnode, void **handle, bool *freeHandle) { @@ -112,8 +103,13 @@ static int32_t vnodeDumpQueryResult(SRspRet *pRet, void *pVnode, void **handle, if ((code = qDumpRetrieveResult(*handle, (SRetrieveTableRsp **)&pRet->rsp, &pRet->len, &continueExec)) == TSDB_CODE_SUCCESS) { if (continueExec) { *freeHandle = false; - vnodePutItemIntoReadQueue(pVnode, handle); - pRet->qhandle = *handle; + code = vnodePutItemIntoReadQueue(pVnode, handle); + if (code != TSDB_CODE_SUCCESS) { + *freeHandle = true; + return code; + } else { + pRet->qhandle = *handle; + } } else { *freeHandle = true; vDebug("QInfo:%p exec completed, free handle:%d", *handle, *freeHandle); @@ -214,7 +210,12 @@ static int32_t vnodeProcessQueryMsg(SVnodeObj *pVnode, SReadMsg *pReadMsg) { if (handle != NULL) { vDebug("vgId:%d, QInfo:%p, dnode query msg disposed, create qhandle and returns to app", vgId, *handle); - vnodePutItemIntoReadQueue(pVnode, handle); + code = vnodePutItemIntoReadQueue(pVnode, handle); + if (code != TSDB_CODE_SUCCESS) { + pRsp->code = code; + qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); + return pRsp->code; + } } } else { assert(pCont != NULL); diff --git a/src/vnode/src/vnodeWrite.c b/src/vnode/src/vnodeWrite.c index 0966192c94..f541c7717a 100644 --- a/src/vnode/src/vnodeWrite.c +++ b/src/vnode/src/vnodeWrite.c @@ -56,15 +56,6 @@ int32_t vnodeProcessWrite(void *param1, int qtype, void *param2, void *item) { return TSDB_CODE_VND_MSG_NOT_PROCESSED; } - if (!(pVnode->accessState & TSDB_VN_WRITE_ACCCESS)) { - vDebug("vgId:%d, msgType:%s not processed, no write auth", pVnode->vgId, taosMsg[pHead->msgType]); - return TSDB_CODE_VND_NO_WRITE_AUTH; - } - - // tsdb may be in reset state - if (pVnode->tsdb == NULL) return TSDB_CODE_APP_NOT_READY; - if (pVnode->status == TAOS_VN_STATUS_CLOSING) return TSDB_CODE_APP_NOT_READY; - if (pHead->version == 0) { // from client or CQ if (pVnode->status != TAOS_VN_STATUS_READY) { vDebug("vgId:%d, msgType:%s not processed, vnode status is %d", pVnode->vgId, taosMsg[pHead->msgType], @@ -105,6 +96,28 @@ int32_t vnodeProcessWrite(void *param1, int qtype, void *param2, void *item) { return syncCode; } +int32_t vnodeCheckWrite(void *param) { + SVnodeObj *pVnode = param; + if (!(pVnode->accessState & TSDB_VN_WRITE_ACCCESS)) { + vDebug("vgId:%d, no write auth, recCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); + return TSDB_CODE_VND_NO_WRITE_AUTH; + } + + // tsdb may be in reset state + if (pVnode->tsdb == NULL) { + vDebug("vgId:%d, tsdb is null, recCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); + return TSDB_CODE_APP_NOT_READY; + } + + if (pVnode->status == TAOS_VN_STATUS_CLOSING) { + vDebug("vgId:%d, vnode status is %s, recCount:%d pVnode:%p", pVnode->vgId, vnodeStatus[pVnode->status], + pVnode->refCount, pVnode); + return TSDB_CODE_APP_NOT_READY; + } + + return TSDB_CODE_SUCCESS; +} + void vnodeConfirmForward(void *param, uint64_t version, int32_t code) { SVnodeObj *pVnode = (SVnodeObj *)param; syncConfirmForward(pVnode->sync, version, code); From 9072e302e1e20715b464bb2c134b7a63d6ff399f Mon Sep 17 00:00:00 2001 From: Ping Xiao Date: Wed, 21 Oct 2020 15:46:34 +0800 Subject: [PATCH 096/101] [TD-1758] add test case --- tests/pytest/fulltest.sh | 1 + tests/pytest/insert/insertIntoTwoTables.py | 53 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 tests/pytest/insert/insertIntoTwoTables.py diff --git a/tests/pytest/fulltest.sh b/tests/pytest/fulltest.sh index fd973b8b76..86147a4b2b 100755 --- a/tests/pytest/fulltest.sh +++ b/tests/pytest/fulltest.sh @@ -18,6 +18,7 @@ python3 ./test.py -f insert/multi.py python3 ./test.py -f insert/randomNullCommit.py python3 insert/retentionpolicy.py python3 ./test.py -f insert/alterTableAndInsert.py +python3 ./test.py -f insert/insertIntoTwoTables.py python3 ./test.py -f table/column_name.py python3 ./test.py -f table/column_num.py diff --git a/tests/pytest/insert/insertIntoTwoTables.py b/tests/pytest/insert/insertIntoTwoTables.py new file mode 100644 index 0000000000..dc985fbe12 --- /dev/null +++ b/tests/pytest/insert/insertIntoTwoTables.py @@ -0,0 +1,53 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import taos +from util.log import tdLog +from util.cases import tdCases +from util.sql import tdSql + + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + def run(self): + tdSql.prepare() + + ### test case for TD-1758 ### + print("==============step1") + tdSql.execute( + "create table t0(ts timestamp, c int)") + tdSql.execute( + 'create table t1(ts timestamp, c binary(1))') + tdSql.execute( + "insert into t0 values(now,1) t1 values(now,'0')(now+1a,'1')(now+2a,'2')(now+3a,'3')(now+4a,'4')") + + print("==============step2") + + tdSql.query("select * from t0") + tdSql.checkRows(1) + tdSql.checkData(0, 0, 1) + + tdSql.query("select * from t1") + tdSql.checkRows(5) + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) From fccb4851dee72cc0a70a41d0c837c098a88dfc35 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 21 Oct 2020 15:48:16 +0800 Subject: [PATCH 097/101] [td-225] --- src/query/src/qPercentile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query/src/qPercentile.c b/src/query/src/qPercentile.c index 3a8be781d5..ab9ffb7bcb 100644 --- a/src/query/src/qPercentile.c +++ b/src/query/src/qPercentile.c @@ -365,7 +365,7 @@ void tMemBucketDestroy(tMemBucket *pBucket) { taosTFree(pBucket); } -void tMemBucketUpdateBoundingBox(MinMaxEntry *r, char *data, int32_t dataType) { +void tMemBucketUpdateBoundingBox(MinMaxEntry *r, const char *data, int32_t dataType) { switch (dataType) { case TSDB_DATA_TYPE_INT: { int32_t val = *(int32_t *)data; From b8e1f3d3b4b2ce1f2f359128c683684b0efb02d6 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 21 Oct 2020 16:07:10 +0800 Subject: [PATCH 098/101] [td-225] fix compiler error. --- src/client/src/tscUtil.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index e53b58daa2..7e1c378584 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -1841,7 +1841,7 @@ static void doSetSqlExprAndResultFieldInfo(SQueryInfo* pQueryInfo, SQueryInfo* p for (int32_t i = 0; i < numOfExprs; ++i) { SSqlExpr* pExpr = tscSqlExprGet(pNewQueryInfo, i); - TAOS_FIELD f = tscCreateField(pExpr->resType, pExpr->aliasName, pExpr->resBytes); + TAOS_FIELD f = tscCreateField((int8_t) pExpr->resType, pExpr->aliasName, pExpr->resBytes); SInternalField* pInfo1 = tscFieldInfoAppend(&pNewQueryInfo->fieldsInfo, &f); pInfo1->pSqlExpr = pExpr; } From c7d95710888cd71b8ff43003df4e2c84d87b1dea Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 21 Oct 2020 08:29:03 +0000 Subject: [PATCH 099/101] #3922 --- src/sync/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sync/CMakeLists.txt b/src/sync/CMakeLists.txt index efdf2bd185..60271c771c 100644 --- a/src/sync/CMakeLists.txt +++ b/src/sync/CMakeLists.txt @@ -5,14 +5,14 @@ INCLUDE_DIRECTORIES(inc) AUX_SOURCE_DIRECTORY(src SRC) IF (TD_LINUX) - LIST(REMOVE_ITEM SRC ./src/tarbitrator.c) + LIST(REMOVE_ITEM SRC src/tarbitrator.c) ADD_LIBRARY(sync ${SRC}) TARGET_LINK_LIBRARIES(sync tutil pthread common) - LIST(APPEND BIN_SRC ./src/tarbitrator.c) - LIST(APPEND BIN_SRC ./src/taosTcpPool.c) + LIST(APPEND BIN_SRC src/tarbitrator.c) + LIST(APPEND BIN_SRC src/taosTcpPool.c) ADD_EXECUTABLE(tarbitrator ${BIN_SRC}) TARGET_LINK_LIBRARIES(tarbitrator sync common osdetail tutil) - ADD_SUBDIRECTORY(test) + #ADD_SUBDIRECTORY(test) ENDIF () From 9101e5059153a63f5ee14c25a54d0486c4504c34 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 21 Oct 2020 09:02:25 +0000 Subject: [PATCH 100/101] fix compile errors --- src/client/src/tscSql.c | 6 +++--- src/inc/taos.h | 13 ++++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index 9799c9129b..7c856e6cc3 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -174,7 +174,7 @@ static void syncConnCallback(void *param, TAOS_RES *tres, int code) { TAOS *taos_connect_internal(const char *ip, const char *user, const char *pass, const char *auth, const char *db, uint16_t port) { STscObj *pObj = NULL; - SSqlObj *pSql = taosConnectImpl(ip, user, pass, auth, db, port, syncConnCallback, NULL, &pObj); + SSqlObj *pSql = taosConnectImpl(ip, user, pass, auth, db, port, syncConnCallback, NULL, (void **)&pObj); if (pSql != NULL) { pSql->fp = syncConnCallback; pSql->param = pSql; @@ -245,11 +245,11 @@ static void asyncConnCallback(void *param, TAOS_RES *tres, int code) { TAOS *taos_connect_a(char *ip, char *user, char *pass, char *db, uint16_t port, void (*fp)(void *, TAOS_RES *, int), void *param, TAOS **taos) { STscObj *pObj = NULL; - SSqlObj* pSql = taosConnectImpl(ip, user, pass, NULL, db, port, asyncConnCallback, param, &pObj); + SSqlObj *pSql = taosConnectImpl(ip, user, pass, NULL, db, port, asyncConnCallback, param, (void **)&pObj); if (pSql == NULL) { return NULL; } - + if (taos) *taos = pObj; pSql->fetchFp = fp; diff --git a/src/inc/taos.h b/src/inc/taos.h index 66b5f930bc..3153137347 100644 --- a/src/inc/taos.h +++ b/src/inc/taos.h @@ -22,13 +22,12 @@ extern "C" { #endif -typedef struct STscObj TAOS; -typedef struct STscStmt TAOS_STMT; -typedef struct SSqlObj TAOS_RES; -typedef struct SSqlStream TAOS_STREAM; -typedef struct SSub TAOS_SUB; -typedef unsigned char** TAOS_ROW; - +typedef void TAOS; +typedef void TAOS_STMT; +typedef void TAOS_RES; +typedef void TAOS_STREAM; +typedef void TAOS_SUB; +typedef void **TAOS_ROW; // Data type definition #define TSDB_DATA_TYPE_NULL 0 // 1 bytes From 2b343e33b3e0bdeb7269baba413e35b2f26f4c70 Mon Sep 17 00:00:00 2001 From: wangyazhou1313 Date: Thu, 22 Oct 2020 09:29:38 +0800 Subject: [PATCH 101/101] edit fulltest.sh add testcase table/alter_wal0.py & edit architectur-ch.md --- documentation20/webdocs/markdowndocs/architecture-ch.md | 2 +- tests/pytest/fulltest.sh | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/documentation20/webdocs/markdowndocs/architecture-ch.md b/documentation20/webdocs/markdowndocs/architecture-ch.md index ba45bc4796..d4705ccb05 100644 --- a/documentation20/webdocs/markdowndocs/architecture-ch.md +++ b/documentation20/webdocs/markdowndocs/architecture-ch.md @@ -228,7 +228,7 @@ TDengine采用数据驱动的方式让缓存中的数据写入硬盘进行持久 为充分利用时序数据特点,TDengine将一个vnode保存在持久化存储的数据切分成多个文件,每个文件只保存固定天数的数据,这个天数由系统配置参数days决定。切分成多个文件后,给定查询的起止日期,无需任何索引,就可以立即定位需要打开哪些数据文件,大大加快读取速度。 -对于采集的数据,一般有保留时长,这个时长由系统配置参数keep决定。超过这个设置天数的数据文件,将被系统将自动删除,释放存储空间。 +对于采集的数据,一般有保留时长,这个时长由系统配置参数keep决定。超过这个设置天数的数据文件,将被系统自动删除,释放存储空间。 给定days与keep两个参数,一个vnode总的数据文件数为:keep/days。总的数据文件个数不宜过大,也不宜过小。10到100以内合适。基于这个原则,可以设置合理的days。 目前的版本,参数keep可以修改,但对于参数days,一但设置后,不可修改。 diff --git a/tests/pytest/fulltest.sh b/tests/pytest/fulltest.sh index fd973b8b76..2a5909f271 100755 --- a/tests/pytest/fulltest.sh +++ b/tests/pytest/fulltest.sh @@ -19,6 +19,7 @@ python3 ./test.py -f insert/randomNullCommit.py python3 insert/retentionpolicy.py python3 ./test.py -f insert/alterTableAndInsert.py +python3 ./test.py -f table/alter_wal0.py python3 ./test.py -f table/column_name.py python3 ./test.py -f table/column_num.py python3 ./test.py -f table/db_table.py