提交 ff02c010 编写于 作者: S shenglian zhou

fix bugs when composing sql string

上级 26794edd
...@@ -775,39 +775,45 @@ static int32_t applyChildTableDataPointsWithInsertSQL(TAOS* taos, char* cTableNa ...@@ -775,39 +775,45 @@ static int32_t applyChildTableDataPointsWithInsertSQL(TAOS* taos, char* cTableNa
} }
int32_t freeBytes = tsMaxSQLStringLen + 1 ; int32_t freeBytes = tsMaxSQLStringLen + 1 ;
sprintf(sql, "insert into ? using %s (", sTableName); int32_t totalLen = 0;
totalLen += sprintf(sql, "insert into %s using %s (", cTableName, sTableName);
for (int i = 0; i < numTags; ++i) { for (int i = 0; i < numTags; ++i) {
SSchema* tagSchema = taosArrayGet(tagsSchema, i); SSchema* tagSchema = taosArrayGet(tagsSchema, i);
snprintf(sql+strlen(sql), freeBytes-strlen(sql), "%s,", tagSchema->name); totalLen += snprintf(sql+totalLen, freeBytes-totalLen, "%s,", tagSchema->name);
} }
snprintf(sql + strlen(sql) - 1, freeBytes-strlen(sql)+1, ")"); --totalLen;
totalLen += snprintf(sql + totalLen, freeBytes-totalLen, ")");
snprintf(sql + strlen(sql), freeBytes-strlen(sql), " tags ("); totalLen += snprintf(sql + totalLen, freeBytes-totalLen, " tags (");
// for (int i = 0; i < numTags; ++i) { // for (int i = 0; i < numTags; ++i) {
// snprintf(sql+strlen(sql), freeBytes-strlen(sql), "?,"); // snprintf(sql+strlen(sql), freeBytes-strlen(sql), "?,");
// } // }
for (int i = 0; i < numTags; ++i) { for (int i = 0; i < numTags; ++i) {
if (tagKVs[i] == NULL) { if (tagKVs[i] == NULL) {
snprintf(sql + strlen(sql), freeBytes-strlen(sql), "NULL,"); totalLen += snprintf(sql + totalLen, freeBytes-totalLen, "NULL,");
} else { } else {
TAOS_SML_KV* kv = tagKVs[i]; TAOS_SML_KV* kv = tagKVs[i];
size_t beforeLen = totalLen;
int32_t len = 0; int32_t len = 0;
converToStr(sql+strlen(sql), kv->type, kv->value, kv->length, &len); converToStr(sql+beforeLen, kv->type, kv->value, kv->length, &len);
*(sql+strlen(sql)+len)='\0'; totalLen += len;
totalLen += snprintf(sql+totalLen, freeBytes-totalLen, ",");
} }
} }
snprintf(sql + strlen(sql) - 1, freeBytes-strlen(sql)+1, ") ("); --totalLen;
totalLen += snprintf(sql + totalLen, freeBytes-totalLen, ") (");
for (int i = 0; i < numCols; ++i) { for (int i = 0; i < numCols; ++i) {
SSchema* colSchema = taosArrayGet(colsSchema, i); SSchema* colSchema = taosArrayGet(colsSchema, i);
snprintf(sql+strlen(sql), freeBytes-strlen(sql), "%s,", colSchema->name); totalLen += snprintf(sql+totalLen, freeBytes-totalLen, "%s,", colSchema->name);
} }
snprintf(sql + strlen(sql)-1, freeBytes-strlen(sql)+1, ") values "); --totalLen;
totalLen += snprintf(sql + totalLen, freeBytes-totalLen, ") values ");
TAOS_SML_KV** colKVs = malloc(numCols*sizeof(TAOS_SML_KV*)); TAOS_SML_KV** colKVs = malloc(numCols*sizeof(TAOS_SML_KV*));
for (int r = 0; r < rows; ++r) { for (int r = 0; r < rows; ++r) {
snprintf(sql + strlen(sql)-1, freeBytes-strlen(sql)+1, "("); totalLen += snprintf(sql + totalLen, freeBytes-totalLen, "(");
memset(colKVs, 0, numCols*sizeof(TAOS_SML_KV*)); memset(colKVs, 0, numCols*sizeof(TAOS_SML_KV*));
...@@ -819,20 +825,23 @@ static int32_t applyChildTableDataPointsWithInsertSQL(TAOS* taos, char* cTableNa ...@@ -819,20 +825,23 @@ static int32_t applyChildTableDataPointsWithInsertSQL(TAOS* taos, char* cTableNa
for (int i = 0; i < numCols; ++i) { for (int i = 0; i < numCols; ++i) {
if (colKVs[i] == NULL) { if (colKVs[i] == NULL) {
snprintf(sql + strlen(sql), freeBytes-strlen(sql), "NULL,"); totalLen += snprintf(sql + totalLen, freeBytes-totalLen, "NULL,");
} else { } else {
TAOS_SML_KV* kv = colKVs[i]; TAOS_SML_KV* kv = colKVs[i];
size_t beforeLen = totalLen;
int32_t len = 0; int32_t len = 0;
converToStr(sql+strlen(sql), kv->type, kv->value, kv->length, &len); converToStr(sql+beforeLen, kv->type, kv->value, kv->length, &len);
*(sql+strlen(sql)+len)='\0'; totalLen += len;
totalLen += snprintf(sql+totalLen, freeBytes-totalLen, ",");
} }
} }
snprintf(sql + strlen(sql) - 1, freeBytes - strlen(sql) + 1, ")"); --totalLen;
totalLen += snprintf(sql+totalLen, freeBytes - totalLen, ")");
} }
free(colKVs); free(colKVs);
sql[strlen(sql)] = '\0'; sql[totalLen] = '\0';
tscDebug("SML:0x%"PRIx64" insert child table table %s of super table %s : %s", info->id, cTableName, sTableName, sql); tscInfo("SML:0x%"PRIx64" insert child table table %s of super table %s sql: %s", info->id, cTableName, sTableName, sql);
TAOS_RES* res = taos_query(taos, sql); TAOS_RES* res = taos_query(taos, sql);
code = taos_errno(res); code = taos_errno(res);
info->affectedRows = taos_affected_rows(res); info->affectedRows = taos_affected_rows(res);
...@@ -1099,9 +1108,9 @@ static int32_t applyChildTableDataPoints(TAOS* taos, char* cTableName, char* sTa ...@@ -1099,9 +1108,9 @@ static int32_t applyChildTableDataPoints(TAOS* taos, char* cTableName, char* sTa
int32_t code = TSDB_CODE_SUCCESS; int32_t code = TSDB_CODE_SUCCESS;
size_t childTableDataPoints = taosArrayGetSize(cTablePoints); size_t childTableDataPoints = taosArrayGetSize(cTablePoints);
if (childTableDataPoints < 10) { if (childTableDataPoints < 10) {
applyChildTableDataPointsWithInsertSQL(taos, cTableName, sTableName, sTableSchema, cTablePoints, rowSize, info); code = applyChildTableDataPointsWithInsertSQL(taos, cTableName, sTableName, sTableSchema, cTablePoints, rowSize, info);
} else { } else {
applyChildTableDataPointsWithStmt(taos, cTableName, sTableName, sTableSchema, cTablePoints, rowSize, info); code = applyChildTableDataPointsWithStmt(taos, cTableName, sTableName, sTableSchema, cTablePoints, rowSize, info);
} }
return code; return code;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册