planStmtTest.cpp 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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 "planTestUtil.h"
#include "planner.h"

using namespace std;

class PlanStmtTest : public PlannerTestBase {
X
Xiaoyu Wang 已提交
22
 public:
23 24 25 26
  TAOS_MULTI_BIND* createBindParams(int32_t nParams) {
    return (TAOS_MULTI_BIND*)taosMemoryCalloc(nParams, sizeof(TAOS_MULTI_BIND));
  }

27 28 29 30 31 32 33 34 35 36
  void destoryBindParams(TAOS_MULTI_BIND* pParams, int32_t nParams) {
    for (int32_t i = 0; i < nParams; ++i) {
      TAOS_MULTI_BIND* pParam = pParams + i;
      taosMemoryFree(pParam->buffer);
      taosMemoryFree(pParam->length);
      taosMemoryFree(pParam->is_null);
    }
    taosMemoryFree(pParams);
  }

37 38
  TAOS_MULTI_BIND* buildIntegerParam(TAOS_MULTI_BIND* pBindParams, int32_t index, int64_t val, int32_t type) {
    TAOS_MULTI_BIND* pBindParam = initParam(pBindParams, index, type, 0);
39

40 41
    switch (type) {
      case TSDB_DATA_TYPE_BOOL:
42
        *((bool*)pBindParam->buffer) = val;
43 44
        break;
      case TSDB_DATA_TYPE_TINYINT:
45
        *((int8_t*)pBindParam->buffer) = val;
46 47
        break;
      case TSDB_DATA_TYPE_SMALLINT:
48 49
        *((int16_t*)pBindParam->buffer) = val;
        break;
50
      case TSDB_DATA_TYPE_INT:
51 52
        *((int32_t*)pBindParam->buffer) = val;
        break;
53
      case TSDB_DATA_TYPE_BIGINT:
54 55
        *((int64_t*)pBindParam->buffer) = val;
        break;
56
      case TSDB_DATA_TYPE_TIMESTAMP:
57 58 59 60 61 62 63 64 65 66 67 68 69
        *((int64_t*)pBindParam->buffer) = val;
        break;
      default:
        break;
    }

    return pBindParam;
  }

  TAOS_MULTI_BIND* buildUIntegerParam(TAOS_MULTI_BIND* pBindParams, int32_t index, uint64_t val, int32_t type) {
    TAOS_MULTI_BIND* pBindParam = initParam(pBindParams, index, type, 0);

    switch (type) {
70
      case TSDB_DATA_TYPE_UTINYINT:
71 72
        *((uint8_t*)pBindParam->buffer) = val;
        break;
73
      case TSDB_DATA_TYPE_USMALLINT:
74 75
        *((uint16_t*)pBindParam->buffer) = val;
        break;
76
      case TSDB_DATA_TYPE_UINT:
77 78
        *((uint32_t*)pBindParam->buffer) = val;
        break;
79
      case TSDB_DATA_TYPE_UBIGINT:
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
        *((uint64_t*)pBindParam->buffer) = val;
        break;
      default:
        break;
    }
    return pBindParam;
  }

  TAOS_MULTI_BIND* buildDoubleParam(TAOS_MULTI_BIND* pBindParams, int32_t index, double val, int32_t type) {
    TAOS_MULTI_BIND* pBindParam = initParam(pBindParams, index, type, 0);

    switch (type) {
      case TSDB_DATA_TYPE_FLOAT:
        *((float*)pBindParam->buffer) = val;
        break;
      case TSDB_DATA_TYPE_DOUBLE:
        *((double*)pBindParam->buffer) = val;
        break;
      default:
        break;
    }
    return pBindParam;
  }

  TAOS_MULTI_BIND* buildStringParam(TAOS_MULTI_BIND* pBindParams, int32_t index, const char* pVal, int32_t type,
                                    int32_t bytes) {
    TAOS_MULTI_BIND* pBindParam = initParam(pBindParams, index, type, bytes);

    switch (type) {
      case TSDB_DATA_TYPE_VARCHAR:
110
      case TSDB_DATA_TYPE_VARBINARY:
D
Dingle Zhang 已提交
111
      case TSDB_DATA_TYPE_GEOMETRY:
112 113 114 115
        strncpy((char*)pBindParam->buffer, pVal, bytes);
        break;
      case TSDB_DATA_TYPE_TIMESTAMP:
      case TSDB_DATA_TYPE_NCHAR:
116 117 118
      default:
        break;
    }
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    return pBindParam;
  }

 private:
  TAOS_MULTI_BIND* initParam(TAOS_MULTI_BIND* pBindParams, int32_t index, int32_t type, int32_t bytes) {
    TAOS_MULTI_BIND* pBindParam = pBindParams + index;
    pBindParam->buffer_type = type;
    pBindParam->num = 1;
    pBindParam->buffer_length = bytes > 0 ? bytes : tDataTypes[type].bytes;
    pBindParam->buffer = taosMemoryCalloc(1, pBindParam->buffer_length);
    pBindParam->length = (int32_t*)taosMemoryCalloc(1, sizeof(int32_t));
    pBindParam->is_null = (char*)taosMemoryCalloc(1, sizeof(char));
    *(pBindParam->length) = bytes > 0 ? bytes : tDataTypes[type].bytes;
    *(pBindParam->is_null) = 0;
    return pBindParam;
134 135 136
  }
};

137 138 139 140
TEST_F(PlanStmtTest, basic) {
  useDb("root", "test");

  prepare("SELECT * FROM t1 WHERE c1 = ?");
141 142
  TAOS_MULTI_BIND* pBindParams = buildIntegerParam(createBindParams(1), 0, 10, TSDB_DATA_TYPE_INT);
  bindParams(pBindParams, 0);
143
  exec();
144
  destoryBindParams(pBindParams, 1);
145 146 147 148 149 150 151 152

  {
    prepare("SELECT * FROM t1 WHERE c1 = ? AND c2 = ?");
    TAOS_MULTI_BIND* pBindParams = createBindParams(2);
    buildIntegerParam(pBindParams, 0, 10, TSDB_DATA_TYPE_INT);
    buildStringParam(pBindParams, 1, "abc", TSDB_DATA_TYPE_VARCHAR, strlen("abc"));
    bindParams(pBindParams, -1);
    exec();
153
    destoryBindParams(pBindParams, 2);
154 155 156 157 158 159 160 161 162
  }

  {
    prepare("SELECT MAX(?), MAX(?) FROM t1");
    TAOS_MULTI_BIND* pBindParams = createBindParams(2);
    buildIntegerParam(pBindParams, 0, 10, TSDB_DATA_TYPE_TINYINT);
    buildIntegerParam(pBindParams, 1, 20, TSDB_DATA_TYPE_INT);
    bindParams(pBindParams, -1);
    exec();
163
    destoryBindParams(pBindParams, 2);
164 165 166 167
  }
}

TEST_F(PlanStmtTest, multiExec) {
168 169
  useDb("root", "test");

170
  prepare("SELECT * FROM t1 WHERE c1 = ?");
171 172
  TAOS_MULTI_BIND* pBindParams = buildIntegerParam(createBindParams(1), 0, 10, TSDB_DATA_TYPE_INT);
  bindParams(pBindParams, 0);
173
  exec();
174 175 176
  destoryBindParams(pBindParams, 1);
  pBindParams = buildIntegerParam(createBindParams(1), 0, 20, TSDB_DATA_TYPE_INT);
  bindParams(pBindParams, 0);
177
  exec();
178 179 180
  destoryBindParams(pBindParams, 1);
  pBindParams = buildIntegerParam(createBindParams(1), 0, 30, TSDB_DATA_TYPE_INT);
  bindParams(pBindParams, 0);
181
  exec();
182
  destoryBindParams(pBindParams, 1);
183
}