nodesToSQLFuncs.c 4.7 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
 * 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/>.
 */

#include "cmdnodes.h"
#include "nodesUtil.h"
#include "plannodes.h"
#include "querynodes.h"
#include "taos.h"
#include "taoserror.h"
#include "thash.h"

char *gOperatorStr[] = {NULL, "+", "-", "*", "/", "%", "&", "|", ">", ">=", "<", "<=", "=", "<>", 
                        "IN", "NOT IN", "LIKE", "NOT LIKE", "MATCH", "NMATCH", "IS NULL", "IS NOT NULL",
                        "IS TRUE", "IS FALSE", "IS UNKNOWN", "IS NOT TRUE", "IS NOT FALSE", "IS NOT UNKNOWN"};
char *gLogicConditionStr[] = {"AND", "OR", "NOT"};

int32_t nodesNodeToSQL(SNode *pNode, char *buf, int32_t bufSize, int32_t *len) {
  switch (pNode->type) {
    case QUERY_NODE_COLUMN: {
      SColumnNode *colNode = (SColumnNode *)pNode;
      *len = 0;
      if (colNode->dbName[0]) {
        *len += snprintf(buf, bufSize - *len, "`%s`.", colNode->dbName);
      }
      
      if (colNode->tableAlias[0]) {
        *len += snprintf(buf, bufSize - *len, "`%s`.", colNode->tableAlias);
      } else if (colNode->tableName[0]) {
        *len += snprintf(buf, bufSize - *len, "`%s`.", colNode->tableName);
      }

      *len += snprintf(buf, bufSize - *len, "`%s`", colNode->colName);
      
      return TSDB_CODE_SUCCESS;
    }
    case QUERY_NODE_VALUE:{
      SValueNode *colNode = (SValueNode *)pNode;
      char *t = nodesGetStrValueFromNode(colNode);
      if (NULL == t) {
        nodesError("fail to get str value from valueNode");
D
dapan1121 已提交
53
        NODES_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
54 55 56 57 58 59 60 61 62 63 64
      }
      
      *len += snprintf(buf, bufSize - *len, "%s", t);
      taosMemoryFree(t);
      
      return TSDB_CODE_SUCCESS;
    }
    case QUERY_NODE_OPERATOR: {
      SOperatorNode* pOpNode = (SOperatorNode*)pNode;
      *len += snprintf(buf, bufSize - *len, "(");
      if (pOpNode->pLeft) {
D
dapan1121 已提交
65
        NODES_ERR_RET(nodesNodeToSQL(pOpNode->pLeft, buf, bufSize, len));
D
dapan1121 已提交
66 67 68 69
      }

      if (pOpNode->opType >= (sizeof(gOperatorStr) / sizeof(gOperatorStr[0]))) {
        nodesError("unknown operation type:%d", pOpNode->opType);
D
dapan1121 已提交
70
        NODES_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
71 72 73 74 75
      }
      
      *len += snprintf(buf, bufSize - *len, " %s ", gOperatorStr[pOpNode->opType]);

      if (pOpNode->pRight) {
D
dapan1121 已提交
76
        NODES_ERR_RET(nodesNodeToSQL(pOpNode->pRight, buf, bufSize, len));
D
dapan1121 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
      }    

      *len += snprintf(buf, bufSize - *len, ")");

      return TSDB_CODE_SUCCESS;
    }
    case QUERY_NODE_LOGIC_CONDITION:{
      SLogicConditionNode* pLogicNode = (SLogicConditionNode*)pNode;
      SNode* node = NULL;
      bool first = true;
      
      *len += snprintf(buf, bufSize - *len, "(");
      
      FOREACH(node, pLogicNode->pParameterList) {
        if (!first) {
          *len += snprintf(buf, bufSize - *len, " %s ", gLogicConditionStr[pLogicNode->condType]);
        }
D
dapan1121 已提交
94
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
D
dapan1121 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        first = false;
      }

      *len += snprintf(buf, bufSize - *len, ")");

      return TSDB_CODE_SUCCESS;
    }
    case QUERY_NODE_FUNCTION:{
      SFunctionNode* pFuncNode = (SFunctionNode*)pNode;
      SNode* node = NULL;
      bool first = true;
      
      *len += snprintf(buf, bufSize - *len, "%s(", pFuncNode->functionName);
      
      FOREACH(node, pFuncNode->pParameterList) {
        if (!first) {
          *len += snprintf(buf, bufSize - *len, ", ");
        }
D
dapan1121 已提交
113
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
D
dapan1121 已提交
114 115 116 117 118 119 120 121 122
        first = false;
      }

      *len += snprintf(buf, bufSize - *len, ")");

      return TSDB_CODE_SUCCESS;
    }
    case QUERY_NODE_NODE_LIST:{
      SNodeListNode* pListNode = (SNodeListNode *)pNode;
D
dapan1121 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136
      SNode* node = NULL;
      bool first = true;
      
      *len += snprintf(buf, bufSize - *len, "(");
      
      FOREACH(node, pListNode->pNodeList) {
        if (!first) {
          *len += snprintf(buf, bufSize - *len, ", ");
        }
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
        first = false;
      }
      
      *len += snprintf(buf, bufSize - *len, ")");
D
dapan1121 已提交
137 138 139 140 141 142 143 144

      return TSDB_CODE_SUCCESS;
    }
    default:
      break;
  }
  
  nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type));
D
dapan1121 已提交
145
  NODES_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
146
}