parser.c 5.6 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
H
Haojun Liao 已提交
14 15
 */

X
Xiaoyu Wang 已提交
16
#include "parser.h"
X
Xiaoyu Wang 已提交
17
#include "os.h"
X
Xiaoyu Wang 已提交
18

X
Xiaoyu Wang 已提交
19 20
#include "parInt.h"
#include "parToken.h"
X
Xiaoyu Wang 已提交
21

D
stmt  
dapan1121 已提交
22 23 24 25
bool isInsertSql(const char* pStr, size_t length) {
  if (NULL == pStr) {
    return false;
  }
X
Xiaoyu Wang 已提交
26

X
Xiaoyu Wang 已提交
27 28 29
  int32_t index = 0;

  do {
X
Xiaoyu Wang 已提交
30
    SToken t0 = tStrGetToken((char*)pStr, &index, false);
31
    if (t0.type != TK_NK_LP) {
X
Xiaoyu Wang 已提交
32 33 34 35 36
      return t0.type == TK_INSERT || t0.type == TK_IMPORT;
    }
  } while (1);
}

X
Xiaoyu Wang 已提交
37
static int32_t parseSqlIntoAst(SParseContext* pCxt, SQuery** pQuery) {
X
Xiaoyu Wang 已提交
38
  int32_t code = parse(pCxt, pQuery);
X
Xiaoyu Wang 已提交
39
  if (TSDB_CODE_SUCCESS == code) {
40 41
    code = authenticate(pCxt, *pQuery);
  }
42 43 44 45 46 47 48

  if (TSDB_CODE_SUCCESS == code && (*pQuery)->placeholderNum > 0) {
    // TSWAP((*pQuery)->pContainPlaceholderRoot, (*pQuery)->pRoot);
    return TSDB_CODE_SUCCESS;
  }

  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
49 50
    code = translate(pCxt, *pQuery);
  }
51
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
52
    code = calculateConstant(pCxt, *pQuery);
X
Xiaoyu Wang 已提交
53 54 55 56
  }
  return code;
}

X
Xiaoyu Wang 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
static int32_t setValueByBindParam(SValueNode* pVal, TAOS_MULTI_BIND* pParam) {
  if (pParam->is_null && 1 == *(pParam->is_null)) {
    pVal->node.resType.type = TSDB_DATA_TYPE_NULL;
    pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
    return TSDB_CODE_SUCCESS;
  }
  int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
  pVal->node.resType.type = pParam->buffer_type;
  pVal->node.resType.bytes = inputSize;
  switch (pParam->buffer_type) {
    case TSDB_DATA_TYPE_BOOL:
      pVal->datum.b = *((bool*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_TINYINT:
      pVal->datum.i = *((int8_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      pVal->datum.i = *((int16_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_INT:
      pVal->datum.i = *((int32_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_BIGINT:
      pVal->datum.i = *((int64_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_FLOAT:
      pVal->datum.d = *((float*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      pVal->datum.d = *((double*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
      if (NULL == pVal->datum.p) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
      break;
    case TSDB_DATA_TYPE_NCHAR: {
      pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
      if (NULL == pVal->datum.p) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }

      int32_t output = 0;
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
                         &output)) {
        return errno;
      }
      varDataSetLen(pVal->datum.p, output);
      pVal->node.resType.bytes = output;
      break;
    }
    case TSDB_DATA_TYPE_TIMESTAMP:
      pVal->datum.i = *((int64_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_UTINYINT:
      pVal->datum.u = *((uint8_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      pVal->datum.u = *((uint16_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_UINT:
      pVal->datum.u = *((uint32_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_UBIGINT:
      pVal->datum.u = *((uint64_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_JSON:
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
    case TSDB_DATA_TYPE_MEDIUMBLOB:
      // todo
    default:
      break;
  }
  pVal->translate = true;
  return TSDB_CODE_SUCCESS;
}

int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
X
Xiaoyu Wang 已提交
141
  int32_t code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
142
  if (isInsertSql(pCxt->pSql, pCxt->sqlLen)) {
X
Xiaoyu Wang 已提交
143
    code = parseInsertSql(pCxt, pQuery);
X
Xiaoyu Wang 已提交
144
  } else {
X
Xiaoyu Wang 已提交
145
    code = parseSqlIntoAst(pCxt, pQuery);
X
Xiaoyu Wang 已提交
146
  }
X
Xiaoyu Wang 已提交
147 148
  terrno = code;
  return code;
X
Xiaoyu Wang 已提交
149 150
}

151
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode(pQueryNode); }
152 153 154

int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
  return extractResultSchema(pRoot, numOfCols, pSchema);
D
stmt  
dapan1121 已提交
155
}
X
Xiaoyu Wang 已提交
156

157
int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx) {
X
Xiaoyu Wang 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  int32_t code = TSDB_CODE_SUCCESS;

  if (colIdx < 0) {
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
    for (int32_t i = 0; i < size; ++i) {
      code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i);
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
    }
  } else {
    code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams);
  }

  return code;
}

int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery) {
  int32_t code = translate(pCxt, pQuery);
  if (TSDB_CODE_SUCCESS == code) {
    code = calculateConstant(pCxt, pQuery);
  }
180

X
Xiaoyu Wang 已提交
181 182
  return code;
}