nodesToSQLFuncs.c 5.5 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
/*
 * 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"

X
Xiaoyu Wang 已提交
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
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"};
D
dapan1121 已提交
53 54 55 56 57 58 59
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;
      if (colNode->dbName[0]) {
D
dapan1121 已提交
60
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->dbName);
D
dapan1121 已提交
61
      }
X
Xiaoyu Wang 已提交
62

D
dapan1121 已提交
63
      if (colNode->tableAlias[0]) {
D
dapan1121 已提交
64
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias);
D
dapan1121 已提交
65
      } else if (colNode->tableName[0]) {
D
dapan1121 已提交
66
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName);
D
dapan1121 已提交
67
      }
X
Xiaoyu Wang 已提交
68

D
dapan1121 已提交
69 70 71 72 73
      if (colNode->tableAlias[0]) {
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->colName);
      } else {
        *len += snprintf(buf + *len, bufSize - *len, "%s", colNode->colName);
      }
X
Xiaoyu Wang 已提交
74

D
dapan1121 已提交
75 76
      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
77
    case QUERY_NODE_VALUE: {
D
dapan1121 已提交
78
      SValueNode *colNode = (SValueNode *)pNode;
X
Xiaoyu Wang 已提交
79
      char       *t = nodesGetStrValueFromNode(colNode);
D
dapan1121 已提交
80 81
      if (NULL == t) {
        nodesError("fail to get str value from valueNode");
D
dapan1121 已提交
82
        NODES_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
83
      }
X
Xiaoyu Wang 已提交
84

D
dapan1121 已提交
85
      *len += snprintf(buf + *len, bufSize - *len, "%s", t);
D
dapan1121 已提交
86
      taosMemoryFree(t);
X
Xiaoyu Wang 已提交
87

D
dapan1121 已提交
88 89 90
      return TSDB_CODE_SUCCESS;
    }
    case QUERY_NODE_OPERATOR: {
X
Xiaoyu Wang 已提交
91
      SOperatorNode *pOpNode = (SOperatorNode *)pNode;
D
dapan1121 已提交
92
      *len += snprintf(buf + *len, bufSize - *len, "(");
D
dapan1121 已提交
93
      if (pOpNode->pLeft) {
D
dapan1121 已提交
94
        NODES_ERR_RET(nodesNodeToSQL(pOpNode->pLeft, buf, bufSize, len));
D
dapan1121 已提交
95 96 97 98
      }

      if (pOpNode->opType >= (sizeof(gOperatorStr) / sizeof(gOperatorStr[0]))) {
        nodesError("unknown operation type:%d", pOpNode->opType);
D
dapan1121 已提交
99
        NODES_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
100
      }
X
Xiaoyu Wang 已提交
101

D
dapan1121 已提交
102
      *len += snprintf(buf + *len, bufSize - *len, " %s ", gOperatorStr[pOpNode->opType]);
D
dapan1121 已提交
103 104

      if (pOpNode->pRight) {
D
dapan1121 已提交
105
        NODES_ERR_RET(nodesNodeToSQL(pOpNode->pRight, buf, bufSize, len));
X
Xiaoyu Wang 已提交
106
      }
D
dapan1121 已提交
107

D
dapan1121 已提交
108
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
109 110 111

      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
112 113 114 115 116
    case QUERY_NODE_LOGIC_CONDITION: {
      SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
      SNode               *node = NULL;
      bool                 first = true;

D
dapan1121 已提交
117
      *len += snprintf(buf + *len, bufSize - *len, "(");
X
Xiaoyu Wang 已提交
118

D
dapan1121 已提交
119 120
      FOREACH(node, pLogicNode->pParameterList) {
        if (!first) {
D
dapan1121 已提交
121
          *len += snprintf(buf + *len, bufSize - *len, " %s ", gLogicConditionStr[pLogicNode->condType]);
D
dapan1121 已提交
122
        }
D
dapan1121 已提交
123
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
D
dapan1121 已提交
124 125 126
        first = false;
      }

D
dapan1121 已提交
127
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
128 129 130

      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
131 132 133 134 135
    case QUERY_NODE_FUNCTION: {
      SFunctionNode *pFuncNode = (SFunctionNode *)pNode;
      SNode         *node = NULL;
      bool           first = true;

D
dapan1121 已提交
136
      *len += snprintf(buf + *len, bufSize - *len, "%s(", pFuncNode->functionName);
X
Xiaoyu Wang 已提交
137

D
dapan1121 已提交
138 139
      FOREACH(node, pFuncNode->pParameterList) {
        if (!first) {
D
dapan1121 已提交
140
          *len += snprintf(buf + *len, bufSize - *len, ", ");
D
dapan1121 已提交
141
        }
D
dapan1121 已提交
142
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
D
dapan1121 已提交
143 144 145
        first = false;
      }

D
dapan1121 已提交
146
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
147 148 149

      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
150 151 152 153 154
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *pListNode = (SNodeListNode *)pNode;
      SNode         *node = NULL;
      bool           first = true;

D
dapan1121 已提交
155
      *len += snprintf(buf + *len, bufSize - *len, "(");
X
Xiaoyu Wang 已提交
156

D
dapan1121 已提交
157 158
      FOREACH(node, pListNode->pNodeList) {
        if (!first) {
D
dapan1121 已提交
159
          *len += snprintf(buf + *len, bufSize - *len, ", ");
D
dapan1121 已提交
160 161 162 163
        }
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
        first = false;
      }
X
Xiaoyu Wang 已提交
164

D
dapan1121 已提交
165
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
166 167 168 169 170 171

      return TSDB_CODE_SUCCESS;
    }
    default:
      break;
  }
X
Xiaoyu Wang 已提交
172

D
dapan1121 已提交
173
  nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type));
D
dapan1121 已提交
174
  NODES_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
175
}