提交 0da8e9ad 编写于 作者: S slguan

revise issue #124

上级 0e57b621
...@@ -64,6 +64,7 @@ void httpJsonOriginString(JsonBuf* buf, char* sVal, int len); ...@@ -64,6 +64,7 @@ void httpJsonOriginString(JsonBuf* buf, char* sVal, int len);
void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int maxLen); void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int maxLen);
void httpJsonInt64(JsonBuf* buf, int64_t num); void httpJsonInt64(JsonBuf* buf, int64_t num);
void httpJsonTimestamp(JsonBuf* buf, int64_t t); void httpJsonTimestamp(JsonBuf* buf, int64_t t);
void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t);
void httpJsonInt(JsonBuf* buf, int num); void httpJsonInt(JsonBuf* buf, int num);
void httpJsonFloat(JsonBuf* buf, float num); void httpJsonFloat(JsonBuf* buf, float num);
void httpJsonDouble(JsonBuf* buf, double num); void httpJsonDouble(JsonBuf* buf, double num);
......
...@@ -39,11 +39,16 @@ ...@@ -39,11 +39,16 @@
#define REST_JSON_AFFECT_ROWS "affected_rows" #define REST_JSON_AFFECT_ROWS "affected_rows"
#define REST_JSON_AFFECT_ROWS_LEN 13 #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 restBuildSqlAffectRowsJson(HttpContext *pContext, HttpSqlCmd *cmd, int affect_rows);
void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result); void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result);
bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows); bool restBuildSqlTimestampJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows);
bool restBuildSqlTimeJson(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); void restStopSqlJson(HttpContext *pContext, HttpSqlCmd *cmd);
#endif #endif
\ No newline at end of file
...@@ -266,11 +266,24 @@ void httpJsonTimestamp(JsonBuf* buf, int64_t t) { ...@@ -266,11 +266,24 @@ void httpJsonTimestamp(JsonBuf* buf, int64_t t) {
struct tm* ptm; struct tm* ptm;
time_t tt = t / 1000; time_t tt = t / 1000;
ptm = localtime(&tt); 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) { void httpJsonInt(JsonBuf* buf, int num) {
......
...@@ -18,10 +18,12 @@ ...@@ -18,10 +18,12 @@
static HttpDecodeMethod restDecodeMethod = {"rest", restProcessRequest}; static HttpDecodeMethod restDecodeMethod = {"rest", restProcessRequest};
static HttpDecodeMethod restDecodeMethod2 = {"restful", restProcessRequest}; static HttpDecodeMethod restDecodeMethod2 = {"restful", restProcessRequest};
static HttpEncodeMethod restEncodeSqlMethod = { static HttpEncodeMethod restEncodeSqlTimestampMethod = {
restStartSqlJson, restStopSqlJson, restBuildSqlJson, restBuildSqlAffectRowsJson, NULL, NULL, NULL, NULL}; restStartSqlJson, restStopSqlJson, restBuildSqlTimestampJson, restBuildSqlAffectRowsJson, NULL, NULL, NULL, NULL};
static HttpEncodeMethod restEncodeSqlTimeMethod = { static HttpEncodeMethod restEncodeSqlLocalTimeStringMethod = {
restStartSqlJson, restStopSqlJson, restBuildSqlTimeJson, restBuildSqlAffectRowsJson, NULL, NULL, NULL, NULL}; restStartSqlJson, restStopSqlJson, restBuildSqlLocalTimeStringJson, restBuildSqlAffectRowsJson, NULL, NULL, NULL, NULL};
static HttpEncodeMethod restEncodeSqlUtcTimeStringMethod = {
restStartSqlJson, restStopSqlJson, restBuildSqlUtcTimeStringJson, restBuildSqlAffectRowsJson, NULL, NULL, NULL, NULL};
void restInitHandle(HttpServer* pServer) { void restInitHandle(HttpServer* pServer) {
httpAddMethod(pServer, &restDecodeMethod); httpAddMethod(pServer, &restDecodeMethod);
...@@ -55,7 +57,7 @@ bool restProcessLoginRequest(HttpContext* pContext) { ...@@ -55,7 +57,7 @@ bool restProcessLoginRequest(HttpContext* pContext) {
return true; 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, httpTrace("context:%p, fd:%d, ip:%s, user:%s, process restful sql msg", pContext, pContext->fd, pContext->ipstr,
pContext->user); pContext->user);
...@@ -74,10 +76,13 @@ bool restProcessSqlRequest(HttpContext* pContext, int isSqlT) { ...@@ -74,10 +76,13 @@ bool restProcessSqlRequest(HttpContext* pContext, int isSqlT) {
cmd->nativSql = sql; cmd->nativSql = sql;
pContext->reqType = HTTP_REQTYPE_SINGLE_SQL; pContext->reqType = HTTP_REQTYPE_SINGLE_SQL;
if (!isSqlT) if (timestampFmt == REST_TIMESTAMP_FMT_LOCAL_STRING) {
pContext->encodeMethod = &restEncodeSqlMethod; pContext->encodeMethod = &restEncodeSqlLocalTimeStringMethod;
else } else if (timestampFmt == REST_TIMESTAMP_FMT_TIMESTAMP) {
pContext->encodeMethod = &restEncodeSqlTimeMethod; pContext->encodeMethod = &restEncodeSqlTimestampMethod;
} else if (timestampFmt == REST_TIMESTAMP_FMT_UTC_STRING) {
pContext->encodeMethod = &restEncodeSqlUtcTimeStringMethod;
}
return true; return true;
} }
...@@ -94,9 +99,11 @@ bool restProcessRequest(struct HttpContext* pContext) { ...@@ -94,9 +99,11 @@ bool restProcessRequest(struct HttpContext* pContext) {
} }
if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "sql")) { 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")) { } 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")) { } else if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "login")) {
return restProcessLoginRequest(pContext); return restProcessLoginRequest(pContext);
} else { } else {
......
...@@ -85,7 +85,7 @@ void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result) ...@@ -85,7 +85,7 @@ void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result)
httpJsonToken(jsonBuf, JsonArrStt); 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); JsonBuf *jsonBuf = httpMallocJsonBuf(pContext);
if (jsonBuf == NULL) return false; if (jsonBuf == NULL) return false;
...@@ -134,10 +134,13 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, ...@@ -134,10 +134,13 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
httpJsonStringForTransMean(jsonBuf, row[i], fields[i].bytes); httpJsonStringForTransMean(jsonBuf, row[i], fields[i].bytes);
break; break;
case TSDB_DATA_TYPE_TIMESTAMP: case TSDB_DATA_TYPE_TIMESTAMP:
// httpJsonInt64(jsonBuf, *((int64_t *)row[i])); if (timestampFormat == REST_TIMESTAMP_FMT_LOCAL_STRING) {
// httpTimeToString(*((int64_t *)row[i]), timeBuf, 32); httpJsonTimestamp(jsonBuf, *((int64_t *)row[i]));
// httpJsonString(jsonBuf, timeBuf, strlen(timeBuf)); } else if (timestampFormat == REST_TIMESTAMP_FMT_TIMESTAMP) {
httpJsonTimestamp(jsonBuf, *((int64_t *)row[i])); httpJsonInt64(jsonBuf, *((int64_t *)row[i]));
} else {
httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i]));
}
break; break;
default: default:
break; break;
...@@ -167,70 +170,16 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, ...@@ -167,70 +170,16 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result,
} }
} }
bool restBuildSqlTimeJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows) { bool restBuildSqlTimestampJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows) {
JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); return restBuildSqlJson(pContext,cmd, result, numOfRows, REST_TIMESTAMP_FMT_TIMESTAMP);
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;
}
}
// data row array end bool restBuildSqlLocalTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows) {
httpJsonToken(jsonBuf, JsonArrEnd); 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) { void restStopSqlJson(HttpContext *pContext, HttpSqlCmd *cmd) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册