diff --git a/src/modules/http/inc/httpJson.h b/src/modules/http/inc/httpJson.h index 3ac2669da92799f455faacd1f50a6322db33d3f8..91c39a1ac20d48856460a369ad57ec3fb3a15f5b 100644 --- a/src/modules/http/inc/httpJson.h +++ b/src/modules/http/inc/httpJson.h @@ -64,6 +64,7 @@ void httpJsonOriginString(JsonBuf* buf, char* sVal, int len); void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int maxLen); void httpJsonInt64(JsonBuf* buf, int64_t num); void httpJsonTimestamp(JsonBuf* buf, int64_t t); +void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t); void httpJsonInt(JsonBuf* buf, int num); void httpJsonFloat(JsonBuf* buf, float num); void httpJsonDouble(JsonBuf* buf, double num); diff --git a/src/modules/http/inc/restJson.h b/src/modules/http/inc/restJson.h index 109e9a33bfc29b4a5376ce14c2f00cf2f57cec82..e16c148d7ce36be76637f9c214b1e114b98fd338 100644 --- a/src/modules/http/inc/restJson.h +++ b/src/modules/http/inc/restJson.h @@ -39,11 +39,16 @@ #define REST_JSON_AFFECT_ROWS "affected_rows" #define REST_JSON_AFFECT_ROWS_LEN 13 +#define REST_TIMESTAMP_FMT_LOCAL_STRING 0 +#define REST_TIMESTAMP_FMT_TIMESTAMP 1 +#define REST_TIMESTAMP_FMT_UTC_STRING 2 + void restBuildSqlAffectRowsJson(HttpContext *pContext, HttpSqlCmd *cmd, int affect_rows); void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result); -bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows); -bool restBuildSqlTimeJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows); +bool restBuildSqlTimestampJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows); +bool restBuildSqlLocalTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows); +bool restBuildSqlUtcTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows); void restStopSqlJson(HttpContext *pContext, HttpSqlCmd *cmd); #endif \ No newline at end of file diff --git a/src/modules/http/src/httpJson.c b/src/modules/http/src/httpJson.c index f507da8acc17d570585e92e416015b8c990ba04c..189d6c8f304ef689961df1f674a8487f71b51c3a 100644 --- a/src/modules/http/src/httpJson.c +++ b/src/modules/http/src/httpJson.c @@ -266,11 +266,24 @@ void httpJsonTimestamp(JsonBuf* buf, int64_t t) { struct tm* ptm; time_t tt = t / 1000; ptm = localtime(&tt); - int length = (int)strftime(ts, 30, "%Y-%m-%dT%H:%M:%S", ptm); + int length = (int)strftime(ts, 30, "%Y-%m-%d %H:%M:%S", ptm); - snprintf(ts+length, MAX_NUM_STR_SZ, ".%03ldZ", t % 1000); + snprintf(ts+length, MAX_NUM_STR_SZ, ".%03ld", t % 1000); - httpJsonString(buf, ts, length + 5); + httpJsonString(buf, ts, length + 4); +} + +void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t) { + char ts[35] = {0}; + + struct tm *ptm; + time_t tt = t / 1000; + ptm = localtime(&tt); + int length = (int) strftime(ts, 35, "%Y-%m-%dT%H:%M:%S", ptm); + length += snprintf(ts + length, MAX_NUM_STR_SZ, ".%03ld", t % 1000); + length += (int) strftime(ts + length, 35 - length, "%z", ptm); + + httpJsonString(buf, ts, length + 4); } void httpJsonInt(JsonBuf* buf, int num) { diff --git a/src/modules/http/src/restHandle.c b/src/modules/http/src/restHandle.c index fd4318983774c4fa9e2ed8dc01d92bc9e71346ac..58509e693d8a76279c85c865825661e9a972efcd 100644 --- a/src/modules/http/src/restHandle.c +++ b/src/modules/http/src/restHandle.c @@ -18,10 +18,12 @@ static HttpDecodeMethod restDecodeMethod = {"rest", restProcessRequest}; static HttpDecodeMethod restDecodeMethod2 = {"restful", restProcessRequest}; -static HttpEncodeMethod restEncodeSqlMethod = { - restStartSqlJson, restStopSqlJson, restBuildSqlJson, restBuildSqlAffectRowsJson, NULL, NULL, NULL, NULL}; -static HttpEncodeMethod restEncodeSqlTimeMethod = { - restStartSqlJson, restStopSqlJson, restBuildSqlTimeJson, restBuildSqlAffectRowsJson, NULL, NULL, NULL, NULL}; +static HttpEncodeMethod restEncodeSqlTimestampMethod = { + restStartSqlJson, restStopSqlJson, restBuildSqlTimestampJson, restBuildSqlAffectRowsJson, NULL, NULL, NULL, NULL}; +static HttpEncodeMethod restEncodeSqlLocalTimeStringMethod = { + restStartSqlJson, restStopSqlJson, restBuildSqlLocalTimeStringJson, restBuildSqlAffectRowsJson, NULL, NULL, NULL, NULL}; +static HttpEncodeMethod restEncodeSqlUtcTimeStringMethod = { + restStartSqlJson, restStopSqlJson, restBuildSqlUtcTimeStringJson, restBuildSqlAffectRowsJson, NULL, NULL, NULL, NULL}; void restInitHandle(HttpServer* pServer) { httpAddMethod(pServer, &restDecodeMethod); @@ -55,7 +57,7 @@ bool restProcessLoginRequest(HttpContext* pContext) { return true; } -bool restProcessSqlRequest(HttpContext* pContext, int isSqlT) { +bool restProcessSqlRequest(HttpContext* pContext, int timestampFmt) { httpTrace("context:%p, fd:%d, ip:%s, user:%s, process restful sql msg", pContext, pContext->fd, pContext->ipstr, pContext->user); @@ -74,10 +76,13 @@ bool restProcessSqlRequest(HttpContext* pContext, int isSqlT) { cmd->nativSql = sql; pContext->reqType = HTTP_REQTYPE_SINGLE_SQL; - if (!isSqlT) - pContext->encodeMethod = &restEncodeSqlMethod; - else - pContext->encodeMethod = &restEncodeSqlTimeMethod; + if (timestampFmt == REST_TIMESTAMP_FMT_LOCAL_STRING) { + pContext->encodeMethod = &restEncodeSqlLocalTimeStringMethod; + } else if (timestampFmt == REST_TIMESTAMP_FMT_TIMESTAMP) { + pContext->encodeMethod = &restEncodeSqlTimestampMethod; + } else if (timestampFmt == REST_TIMESTAMP_FMT_UTC_STRING) { + pContext->encodeMethod = &restEncodeSqlUtcTimeStringMethod; + } return true; } @@ -94,9 +99,11 @@ bool restProcessRequest(struct HttpContext* pContext) { } if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "sql")) { - return restProcessSqlRequest(pContext, 0); + return restProcessSqlRequest(pContext, REST_TIMESTAMP_FMT_LOCAL_STRING); } else if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "sqlt")) { - return restProcessSqlRequest(pContext, 1); + return restProcessSqlRequest(pContext, REST_TIMESTAMP_FMT_TIMESTAMP); + } else if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "sqlutc")) { + return restProcessSqlRequest(pContext, REST_TIMESTAMP_FMT_UTC_STRING); } else if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "login")) { return restProcessLoginRequest(pContext); } else { diff --git a/src/modules/http/src/restJson.c b/src/modules/http/src/restJson.c index 65d9b3456ec418969aba4dee3ad4ca1b32f0220c..e9315fa32b2eca9ee18a43c28446d4ba221c5428 100644 --- a/src/modules/http/src/restJson.c +++ b/src/modules/http/src/restJson.c @@ -85,7 +85,7 @@ void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result) httpJsonToken(jsonBuf, JsonArrStt); } -bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows) { +bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows, int timestampFormat) { JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); if (jsonBuf == NULL) return false; @@ -134,10 +134,13 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, httpJsonStringForTransMean(jsonBuf, row[i], fields[i].bytes); break; case TSDB_DATA_TYPE_TIMESTAMP: - // httpJsonInt64(jsonBuf, *((int64_t *)row[i])); - // httpTimeToString(*((int64_t *)row[i]), timeBuf, 32); - // httpJsonString(jsonBuf, timeBuf, strlen(timeBuf)); - httpJsonTimestamp(jsonBuf, *((int64_t *)row[i])); + if (timestampFormat == REST_TIMESTAMP_FMT_LOCAL_STRING) { + httpJsonTimestamp(jsonBuf, *((int64_t *)row[i])); + } else if (timestampFormat == REST_TIMESTAMP_FMT_TIMESTAMP) { + httpJsonInt64(jsonBuf, *((int64_t *)row[i])); + } else { + httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i])); + } break; default: break; @@ -167,70 +170,16 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, } } -bool restBuildSqlTimeJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return false; - - cmd->numOfRows += numOfRows; - - int num_fields = taos_num_fields(result); - TAOS_FIELD *fields = taos_fetch_fields(result); - - for (int i = 0; i < numOfRows; ++i) { - TAOS_ROW row = taos_fetch_row(result); - - // data row array begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); - - for (int i = 0; i < num_fields; i++) { - httpJsonItemToken(jsonBuf); - - if (row[i] == NULL) { - httpJsonOriginString(jsonBuf, "null", 4); - continue; - } - - switch (fields[i].type) { - case TSDB_DATA_TYPE_BOOL: - case TSDB_DATA_TYPE_TINYINT: - httpJsonInt(jsonBuf, *((int8_t *)row[i])); - break; - case TSDB_DATA_TYPE_SMALLINT: - httpJsonInt(jsonBuf, *((int16_t *)row[i])); - break; - case TSDB_DATA_TYPE_INT: - httpJsonInt(jsonBuf, *((int32_t *)row[i])); - break; - case TSDB_DATA_TYPE_BIGINT: - httpJsonInt64(jsonBuf, *((int64_t *)row[i])); - break; - case TSDB_DATA_TYPE_FLOAT: - httpJsonFloat(jsonBuf, *((float *)row[i])); - break; - case TSDB_DATA_TYPE_DOUBLE: - httpJsonDouble(jsonBuf, *((double *)row[i])); - break; - case TSDB_DATA_TYPE_BINARY: - case TSDB_DATA_TYPE_NCHAR: - httpJsonStringForTransMean(jsonBuf, row[i], fields[i].bytes); - break; - case TSDB_DATA_TYPE_TIMESTAMP: - httpJsonInt64(jsonBuf, *((int64_t *)row[i])); - // httpTimeToString(*((int64_t *)row[i]), timeBuf, 32); - // httpJsonString(jsonBuf, timeBuf, strlen(timeBuf)); - // httpJsonTimestamp(jsonBuf, *((int64_t *)row[i])); - break; - default: - break; - } - } +bool restBuildSqlTimestampJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows) { + return restBuildSqlJson(pContext,cmd, result, numOfRows, REST_TIMESTAMP_FMT_TIMESTAMP); +} - // data row array end - httpJsonToken(jsonBuf, JsonArrEnd); - } +bool restBuildSqlLocalTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows) { + return restBuildSqlJson(pContext,cmd, result, numOfRows, REST_TIMESTAMP_FMT_LOCAL_STRING); +} - return true; +bool restBuildSqlUtcTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows) { + return restBuildSqlJson(pContext,cmd, result, numOfRows, REST_TIMESTAMP_FMT_UTC_STRING); } void restStopSqlJson(HttpContext *pContext, HttpSqlCmd *cmd) {