diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index 2490e3d7569dfb45b5c94bb9ba2fd67b6860c3f0..57a4cb29c19b23cabe85b390459081ccb29115dd 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -458,6 +458,7 @@ bool tscResultsetFetchCompleted(TAOS_RES *result); char *tscGetErrorMsgPayload(SSqlCmd *pCmd); int32_t tscInvalidSQLErrMsg(char *msg, const char *additionalInfo, const char *sql); +int32_t tscSQLSyntaxErrMsg(char* msg, const char* additionalInfo, const char* sql); int32_t tscToSQLCmd(SSqlObj *pSql, struct SSqlInfo *pInfo); diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index 47bfe0fcdcc0e0331e36e146627feea18bd31b2c..f214e91f457f541f8517fef3084611e092155cc9 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -180,7 +180,7 @@ int32_t tsParseOneColumnData(SSchema *pSchema, SStrToken *pToken, char *payload, } else if (strncasecmp(TSDB_DATA_NULL_STR_L, pToken->z, pToken->n) == 0) { *(uint8_t *)payload = TSDB_DATA_BOOL_NULL; } else { - return tscInvalidSQLErrMsg(msg, "invalid bool data", pToken->z); + return tscSQLSyntaxErrMsg(msg, "invalid bool data", pToken->z); } } else if (pToken->type == TK_INTEGER) { iv = strtoll(pToken->z, NULL, 10); @@ -439,8 +439,8 @@ int tsParseOneRowData(char **str, STableDataBlocks *pDataBlocks, SSchema schema[ int16_t type = sToken.type; if ((type != TK_NOW && type != TK_INTEGER && type != TK_STRING && type != TK_FLOAT && type != TK_BOOL && type != TK_NULL && type != TK_HEX && type != TK_OCT && type != TK_BIN) || (sToken.n == 0) || (type == TK_RP)) { - tscInvalidSQLErrMsg(error, "invalid data or symbol", sToken.z); - *code = TSDB_CODE_TSC_INVALID_SQL; + tscSQLSyntaxErrMsg(error, "invalid data or symbol", sToken.z); + *code = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; return -1; } @@ -472,7 +472,7 @@ int tsParseOneRowData(char **str, STableDataBlocks *pDataBlocks, SSchema schema[ bool isPrimaryKey = (colIndex == PRIMARYKEY_TIMESTAMP_COL_INDEX); int32_t ret = tsParseOneColumnData(pSchema, &sToken, start, error, str, isPrimaryKey, timePrec); if (ret != TSDB_CODE_SUCCESS) { - *code = TSDB_CODE_TSC_INVALID_SQL; + *code = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; return -1; // NOTE: here 0 mean error! } @@ -568,8 +568,8 @@ int tsParseValues(char **str, STableDataBlocks *pDataBlock, STableMeta *pTableMe sToken = tStrGetToken(*str, &index, false, 0, NULL); *str += index; if (sToken.n == 0 || sToken.type != TK_RP) { - tscInvalidSQLErrMsg(error, ") expected", *str); - *code = TSDB_CODE_TSC_INVALID_SQL; + tscSQLSyntaxErrMsg(error, ") expected", *str); + *code = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; return -1; } @@ -578,7 +578,7 @@ int tsParseValues(char **str, STableDataBlocks *pDataBlock, STableMeta *pTableMe if (numOfRows <= 0) { strcpy(error, "no any data points"); - *code = TSDB_CODE_TSC_INVALID_SQL; + *code = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; return -1; } else { return numOfRows; @@ -943,7 +943,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql) { sToken = tStrGetToken(sql, &index, false, 0, NULL); sql += index; if (sToken.n == 0 || sToken.type != TK_RP) { - return tscInvalidSQLErrMsg(pCmd->payload, ") expected", sToken.z); + return tscSQLSyntaxErrMsg(pCmd->payload, ") expected", sToken.z); } pCmd->payloadLen = sizeof(pTag->name) + sizeof(pTag->dataLen) + pTag->dataLen; diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 3b1b87eddea146206b8d1cf2cf97cd3f0bd60b16..f27ce73f7f11a539fe25998b1f34c1feec6bfeca 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) { int32_t code = TSDB_CODE_SUCCESS; if (!pInfo->valid) { - return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), pInfo->pzErrMsg); + return tscSQLSyntaxErrMsg(tscGetErrorMsgPayload(pCmd), NULL, pInfo->pzErrMsg); } SQueryInfo* pQueryInfo = tscGetQueryInfoDetailSafely(pCmd, pCmd->clauseIndex); diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index f63923e046f889cf2f63c93fce37dacf0c858d09..1af53d3645cc9aba13d078f8c9796ff208ace4e8 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -597,11 +597,12 @@ int taos_errno(TAOS_RES *tres) { } /* - * In case of invalid sql error, additional information is attached to explain + * In case of invalid sql/sql syntax error, additional information is attached to explain * why the sql is invalid */ static bool hasAdditionalErrorInfo(int32_t code, SSqlCmd *pCmd) { - if (code != TSDB_CODE_TSC_INVALID_SQL) { + if (code != TSDB_CODE_TSC_INVALID_SQL + && code != TSDB_CODE_TSC_SQL_SYNTAX_ERROR) { return false; } @@ -609,9 +610,11 @@ static bool hasAdditionalErrorInfo(int32_t code, SSqlCmd *pCmd) { char *z = NULL; if (len > 0) { - z = strstr(pCmd->payload, "invalid SQL"); + z = strstr(pCmd->payload, "invalid SQL"); + if (z == NULL) { + z = strstr(pCmd->payload, "syntax error"); + } } - return z != NULL; } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 49f7c91397d793c6795a599078c1ea7b7e490de7..cb810ac69829fbd4fe60ce9af8098e44a81585b9 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -2029,10 +2029,37 @@ bool tscIsUpdateQuery(SSqlObj* pSql) { return ((pCmd->command >= TSDB_SQL_INSERT && pCmd->command <= TSDB_SQL_DROP_DNODE) || TSDB_SQL_USE_DB == pCmd->command); } +int32_t tscSQLSyntaxErrMsg(char* msg, const char* additionalInfo, const char* sql) { + const char* msgFormat1 = "syntax error near \'%s\'"; + const char* msgFormat2 = "syntax error near \'%s\' (%s)"; + const char* msgFormat3 = "%s"; + + const char* prefix = "syntax error"; + const int32_t BACKWARD_CHAR_STEP = 0; + + if (sql == NULL) { + assert(additionalInfo != NULL); + sprintf(msg, msgFormat1, additionalInfo); + return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; + } + + char buf[64] = {0}; // only extract part of sql string + strncpy(buf, (sql - BACKWARD_CHAR_STEP), tListLen(buf) - 1); + + if (additionalInfo != NULL) { + sprintf(msg, msgFormat2, buf, additionalInfo); + } else { + const char* msgFormat = (0 == strncmp(sql, prefix, strlen(prefix))) ? msgFormat3 : msgFormat1; + sprintf(msg, msgFormat, buf); + } + + return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; + +} int32_t tscInvalidSQLErrMsg(char* msg, const char* additionalInfo, const char* sql) { const char* msgFormat1 = "invalid SQL: %s"; - const char* msgFormat2 = "invalid SQL: syntax error near \"%s\" (%s)"; - const char* msgFormat3 = "invalid SQL: syntax error near \"%s\""; + const char* msgFormat2 = "invalid SQL: \'%s\' (%s)"; + const char* msgFormat3 = "invalid SQL: \'%s\'"; const int32_t BACKWARD_CHAR_STEP = 0; @@ -2258,4 +2285,4 @@ bool tscSetSqlOwner(SSqlObj* pSql) { void tscClearSqlOwner(SSqlObj* pSql) { assert(taosCheckPthreadValid(pSql->owner)); atomic_store_64(&pSql->owner, 0); -} \ No newline at end of file +} diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index 951c511022e5df1bf7e385cde526b177a12812ad..d8e5c8f1d748f09c67bd6827b9b9441a01b155c5 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -98,6 +98,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_TSC_ACTION_IN_PROGRESS, 0, 0x0212, "Action in TAOS_DEFINE_ERROR(TSDB_CODE_TSC_DISCONNECTED, 0, 0x0213, "Disconnected from service") TAOS_DEFINE_ERROR(TSDB_CODE_TSC_NO_WRITE_AUTH, 0, 0x0214, "No write permission") TAOS_DEFINE_ERROR(TSDB_CODE_TSC_CONN_KILLED, 0, 0x0215, "Connection killed") +TAOS_DEFINE_ERROR(TSDB_CODE_TSC_SQL_SYNTAX_ERROR, 0, 0x0216, "Syntax errr in SQL") // mnode TAOS_DEFINE_ERROR(TSDB_CODE_MND_MSG_NOT_PROCESSED, 0, 0x0300, "Message not processed") diff --git a/tests/script/general/http/restful_full.sim b/tests/script/general/http/restful_full.sim index 88e7dece4c939b6a935634cce40e41aff4512bb2..b7f98e49e06e6efa5f6adca372a2917c6e91ca21 100644 --- a/tests/script/general/http/restful_full.sim +++ b/tests/script/general/http/restful_full.sim @@ -119,7 +119,7 @@ endi system_content curl -H 'Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04' -d ' used1' 127.0.0.1:7111/rest/sql print 17-> $system_content -if $system_content != @{"status":"error","code":512,"desc":"invalid SQL: invalid SQL: syntax error near 'used1'"}@ then +if $system_content != @{"status":"error","code":534,"desc":"Syntax errr in SQL"}@ then return -1 endi @@ -230,4 +230,4 @@ if $system_content != @{"status":"succ","head":["ts","speed"],"data":[["2017-12- return -1 endi -system sh/exec.sh -n dnode1 -s stop -x SIGINT \ No newline at end of file +system sh/exec.sh -n dnode1 -s stop -x SIGINT