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

revise issue #124

上级 0e57b621
......@@ -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);
......
......@@ -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
......@@ -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) {
......
......@@ -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 {
......
......@@ -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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册