clientJniConnector.c 50.7 KB
Newer Older
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 53 54 55 56 57
/*
 * 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 "taos.h"

#include "com_taosdata_jdbc_TSDBJNIConnector.h"
#include "jniCommon.h"

int __init = 0;

JavaVM *g_vm = NULL;

jclass    g_arrayListClass;
jmethodID g_arrayListConstructFp;
jmethodID g_arrayListAddFp;

jclass    g_metadataClass;
jmethodID g_metadataConstructFp;
jfieldID  g_metadataColtypeField;
jfieldID  g_metadataColnameField;
jfieldID  g_metadataColsizeField;
jfieldID  g_metadataColindexField;

jclass    g_rowdataClass;
jmethodID g_rowdataConstructor;
jmethodID g_rowdataClearFp;
jmethodID g_rowdataSetBooleanFp;
jmethodID g_rowdataSetByteFp;
jmethodID g_rowdataSetShortFp;
jmethodID g_rowdataSetIntFp;
jmethodID g_rowdataSetLongFp;
jmethodID g_rowdataSetFloatFp;
jmethodID g_rowdataSetDoubleFp;
jmethodID g_rowdataSetStringFp;
jmethodID g_rowdataSetTimestampFp;
jmethodID g_rowdataSetByteArrayFp;

jmethodID g_blockdataSetByteArrayFp;
jmethodID g_blockdataSetNumOfRowsFp;
jmethodID g_blockdataSetNumOfColsFp;

jclass    g_tmqClass;
jmethodID g_createConsumerErrorCallback;
jmethodID g_topicListCallback;

58
jclass g_consumerClass;
59
// deprecated
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
jmethodID g_commitCallback;

void jniGetGlobalMethod(JNIEnv *env) {
  // make sure init function executed once
  switch (atomic_val_compare_exchange_32(&__init, 0, 1)) {
    case 0:
      break;
    case 1:
      do {
        taosMsleep(0);
      } while (atomic_load_32(&__init) == 1);
    case 2:
      return;
  }

  if (g_vm == NULL) {
    (*env)->GetJavaVM(env, &g_vm);
  }

  jclass arrayListClass = (*env)->FindClass(env, "java/util/ArrayList");
  g_arrayListClass = (*env)->NewGlobalRef(env, arrayListClass);
  g_arrayListConstructFp = (*env)->GetMethodID(env, g_arrayListClass, "<init>", "()V");
  g_arrayListAddFp = (*env)->GetMethodID(env, g_arrayListClass, "add", "(Ljava/lang/Object;)Z");
  (*env)->DeleteLocalRef(env, arrayListClass);

  jclass metadataClass = (*env)->FindClass(env, "com/taosdata/jdbc/ColumnMetaData");
  g_metadataClass = (*env)->NewGlobalRef(env, metadataClass);
  g_metadataConstructFp = (*env)->GetMethodID(env, g_metadataClass, "<init>", "()V");
  g_metadataColtypeField = (*env)->GetFieldID(env, g_metadataClass, "colType", "I");
  g_metadataColnameField = (*env)->GetFieldID(env, g_metadataClass, "colName", "Ljava/lang/String;");
  g_metadataColsizeField = (*env)->GetFieldID(env, g_metadataClass, "colSize", "I");
  g_metadataColindexField = (*env)->GetFieldID(env, g_metadataClass, "colIndex", "I");
  (*env)->DeleteLocalRef(env, metadataClass);

  jclass rowdataClass = (*env)->FindClass(env, "com/taosdata/jdbc/TSDBResultSetRowData");
  g_rowdataClass = (*env)->NewGlobalRef(env, rowdataClass);
  g_rowdataConstructor = (*env)->GetMethodID(env, g_rowdataClass, "<init>", "(I)V");
  g_rowdataClearFp = (*env)->GetMethodID(env, g_rowdataClass, "clear", "()V");
  g_rowdataSetBooleanFp = (*env)->GetMethodID(env, g_rowdataClass, "setBoolean", "(IZ)V");
  g_rowdataSetByteFp = (*env)->GetMethodID(env, g_rowdataClass, "setByte", "(IB)V");
  g_rowdataSetShortFp = (*env)->GetMethodID(env, g_rowdataClass, "setShort", "(IS)V");
  g_rowdataSetIntFp = (*env)->GetMethodID(env, g_rowdataClass, "setInt", "(II)V");
  g_rowdataSetLongFp = (*env)->GetMethodID(env, g_rowdataClass, "setLong", "(IJ)V");
  g_rowdataSetFloatFp = (*env)->GetMethodID(env, g_rowdataClass, "setFloat", "(IF)V");
  g_rowdataSetDoubleFp = (*env)->GetMethodID(env, g_rowdataClass, "setDouble", "(ID)V");
  g_rowdataSetStringFp = (*env)->GetMethodID(env, g_rowdataClass, "setString", "(ILjava/lang/String;)V");
  g_rowdataSetTimestampFp = (*env)->GetMethodID(env, g_rowdataClass, "setTimestamp", "(IJI)V");
  g_rowdataSetByteArrayFp = (*env)->GetMethodID(env, g_rowdataClass, "setByteArray", "(I[B)V");
  (*env)->DeleteLocalRef(env, rowdataClass);

  jclass blockdataClass = (*env)->FindClass(env, "com/taosdata/jdbc/TSDBResultSetBlockData");
  jclass g_blockdataClass = (*env)->NewGlobalRef(env, blockdataClass);
  g_blockdataSetByteArrayFp = (*env)->GetMethodID(env, g_blockdataClass, "setByteArray", "([B)V");
  g_blockdataSetNumOfRowsFp = (*env)->GetMethodID(env, g_blockdataClass, "setNumOfRows", "(I)V");
  g_blockdataSetNumOfColsFp = (*env)->GetMethodID(env, g_blockdataClass, "setNumOfCols", "(I)V");
  (*env)->DeleteLocalRef(env, blockdataClass);

  jclass tmqClass = (*env)->FindClass(env, "com/taosdata/jdbc/tmq/TMQConnector");
  jclass g_tmqClass = (*env)->NewGlobalRef(env, tmqClass);
  g_createConsumerErrorCallback =
      (*env)->GetMethodID(env, g_tmqClass, "setCreateConsumerErrorMsg", "(Ljava/lang/String;)V");
  g_topicListCallback = (*env)->GetMethodID(env, g_tmqClass, "setTopicList", "([Ljava/lang/String;)V");
  (*env)->DeleteLocalRef(env, tmqClass);

  jclass consumerClass = (*env)->FindClass(env, "com/taosdata/jdbc/tmq/TaosConsumer");
  jclass g_consumerClass = (*env)->NewGlobalRef(env, consumerClass);
  g_commitCallback = (*env)->GetMethodID(env, g_consumerClass, "commitCallbackHandler", "(I)V");
  (*env)->DeleteLocalRef(env, consumerClass);

  atomic_store_32(&__init, 2);
  jniDebug("native method register finished");
}

int32_t check_for_params(jobject jobj, jlong conn, jlong res) {
  if ((TAOS *)conn == NULL) {
    jniError("jobj:%p, connection is closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  if ((TAOS_RES *)res == NULL) {
140
    jniError("jobj:%p, conn:%p, param res is null", jobj, (TAOS *)conn);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    return JNI_RESULT_SET_NULL;
  }

  return JNI_SUCCESS;
}

JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_initImp(JNIEnv *env, jobject jobj, jstring jconfigDir) {
  if (jconfigDir != NULL) {
    const char *confDir = (*env)->GetStringUTFChars(env, jconfigDir, NULL);
    if (confDir && strlen(confDir) != 0) {
      tstrncpy(configDir, confDir, TSDB_FILENAME_LEN);
    }
    (*env)->ReleaseStringUTFChars(env, jconfigDir, confDir);
  }

  jniGetGlobalMethod(env);
  jniDebug("jni initialized successfully, config directory: %s", configDir);
}

JNIEXPORT jobject createTSDBException(JNIEnv *env, int code, char *msg) {
  // find class
  jclass exception_clazz = (*env)->FindClass(env, "com/taosdata/jdbc/TSDBException");
  // find methods
  jmethodID init_method = (*env)->GetMethodID(env, exception_clazz, "<init>", "()V");
  jmethodID setCode_method = (*env)->GetMethodID(env, exception_clazz, "setCode", "(I)V");
  jmethodID setMessage_method = (*env)->GetMethodID(env, exception_clazz, "setMessage", "(Ljava/lang/String;)V");
  // new exception
  jobject exception_obj = (*env)->NewObject(env, exception_clazz, init_method);
  // set code
  (*env)->CallVoidMethod(env, exception_obj, setCode_method, code);
  // set message
  jstring message = (*env)->NewStringUTF(env, msg);
  (*env)->CallVoidMethod(env, exception_obj, setMessage_method, message);

  return exception_obj;
}

JNIEXPORT jobject JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setConfigImp(JNIEnv *env, jclass jobj,
                                                                               jstring config) {
  if (config == NULL) {
    char *msg = "config value is null";
    jniDebug("config value is null");
    return createTSDBException(env, -1, msg);
  }

  const char *cfg = (*env)->GetStringUTFChars(env, config, NULL);
  if (!cfg) {
    char *msg = "config value is null";
    jniDebug("config value is null");
    return createTSDBException(env, -1, msg);
  }

  setConfRet result = taos_set_config(cfg);
  int        code = result.retCode;
  char      *msg = result.retMsg;

  return createTSDBException(env, code, msg);
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setOptions(JNIEnv *env, jobject jobj, jint optionIndex,
                                                                          jstring optionValue) {
  if (optionValue == NULL) {
    jniDebug("option index:%d value is null", (int32_t)optionIndex);
    return 0;
  }

  int res = 0;

  if (optionIndex == TSDB_OPTION_LOCALE) {
    const char *locale = (*env)->GetStringUTFChars(env, optionValue, NULL);
    if (locale && strlen(locale) != 0) {
      res = taos_options(TSDB_OPTION_LOCALE, locale);
      jniDebug("set locale to %s, result:%d", locale, res);
    } else {
      jniDebug("input locale is empty");
    }
    (*env)->ReleaseStringUTFChars(env, optionValue, locale);
  } else if (optionIndex == TSDB_OPTION_CHARSET) {
    const char *charset = (*env)->GetStringUTFChars(env, optionValue, NULL);
    if (charset && strlen(charset) != 0) {
      res = taos_options(TSDB_OPTION_CHARSET, charset);
      jniDebug("set character encoding to %s, result:%d", charset, res);
    } else {
      jniDebug("input character encoding is empty");
    }
    (*env)->ReleaseStringUTFChars(env, optionValue, charset);
  } else if (optionIndex == TSDB_OPTION_TIMEZONE) {
    const char *tz1 = (*env)->GetStringUTFChars(env, optionValue, NULL);
    if (tz1 && strlen(tz1) != 0) {
      res = taos_options(TSDB_OPTION_TIMEZONE, tz1);
      jniDebug("set timezone to %s, result:%d", tz1, res);
    } else {
      jniDebug("input timezone is empty");
    }
    (*env)->ReleaseStringUTFChars(env, optionValue, tz1);
  } else {
    jniError("option index:%d is not found", (int32_t)optionIndex);
  }

  return res;
}

JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_connectImp(JNIEnv *env, jobject jobj, jstring jhost,
                                                                           jint jport, jstring jdbName, jstring juser,
                                                                           jstring jpass) {
  jlong       ret = 0;
  const char *host = NULL;
  const char *user = NULL;
  const char *pass = NULL;
  const char *dbname = NULL;

  if (jhost != NULL) {
    host = (*env)->GetStringUTFChars(env, jhost, NULL);
  }

  if (jdbName != NULL) {
    dbname = (*env)->GetStringUTFChars(env, jdbName, NULL);
  }

  if (juser != NULL) {
    user = (*env)->GetStringUTFChars(env, juser, NULL);
  }

  if (jpass != NULL) {
    pass = (*env)->GetStringUTFChars(env, jpass, NULL);
  }

  if (user == NULL) {
    jniDebug("jobj:%p, user not specified, use default user %s", jobj, TSDB_DEFAULT_USER);
  }

  if (pass == NULL) {
    jniDebug("jobj:%p, pass not specified, use default password", jobj);
  }

  ret = (jlong)taos_connect((char *)host, (char *)user, (char *)pass, (char *)dbname, (uint16_t)jport);
  if (ret == 0) {
    jniError("jobj:%p, conn:%p, connect to database failed, host=%s, user=%s, dbname=%s, port=%d", jobj, (void *)ret,
             (char *)host, (char *)user, (char *)dbname, (int32_t)jport);
  } else {
    jniDebug("jobj:%p, conn:%p, connect to database succeed, host=%s, user=%s, dbname=%s, port=%d", jobj, (void *)ret,
             (char *)host, (char *)user, (char *)dbname, (int32_t)jport);
  }

  if (host != NULL) {
    (*env)->ReleaseStringUTFChars(env, jhost, host);
  }

  if (dbname != NULL) {
    (*env)->ReleaseStringUTFChars(env, jdbName, dbname);
  }

  if (user != NULL) {
    (*env)->ReleaseStringUTFChars(env, juser, user);
  }

  if (pass != NULL) {
    (*env)->ReleaseStringUTFChars(env, jpass, pass);
  }

  return ret;
}

JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryImp(JNIEnv *env, jobject jobj,
                                                                                jbyteArray jsql, jlong con) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  if (jsql == NULL) {
    jniError("jobj:%p, conn:%p, empty sql string", jobj, tscon);
    return JNI_SQL_NULL;
  }

  jsize len = (*env)->GetArrayLength(env, jsql);

  char *str = (char *)taosMemoryCalloc(1, sizeof(char) * (len + 1));
  if (str == NULL) {
    jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tscon);
    return JNI_OUT_OF_MEMORY;
  }

  (*env)->GetByteArrayRegion(env, jsql, 0, len, (jbyte *)str);
  if ((*env)->ExceptionCheck(env)) {
    // todo handle error
  }

  TAOS_RES *tres = taos_query(tscon, str);
  int32_t   code = taos_errno(tres);

  if (code != TSDB_CODE_SUCCESS) {
334
    jniError("jobj:%p, conn:%p, code:0x%x, msg:%s", jobj, tscon, code, taos_errstr(tres));
335 336 337
  } else {
    if (taos_is_update_query(tres)) {
      int32_t affectRows = taos_affected_rows(tres);
338
      jniDebug("jobj:%p, conn:%p, code:0x%x, affect rows:%d", jobj, tscon, code, affectRows);
339
    } else {
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
      jniDebug("jobj:%p, conn:%p, code:0x%x", jobj, tscon, code);
    }
  }

  taosMemoryFreeClear(str);
  return (jlong)tres;
}

JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeQueryWithReqId(JNIEnv *env, jobject jobj,
                                                                                      jbyteArray jsql, jlong con,
                                                                                      jlong reqId) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  if (jsql == NULL) {
    jniError("jobj:%p, conn:%p, empty sql string", jobj, tscon);
    return JNI_SQL_NULL;
  }

  jsize len = (*env)->GetArrayLength(env, jsql);

  char *str = (char *)taosMemoryCalloc(1, sizeof(char) * (len + 1));
  if (str == NULL) {
    jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tscon);
    return JNI_OUT_OF_MEMORY;
  }

  (*env)->GetByteArrayRegion(env, jsql, 0, len, (jbyte *)str);
  if ((*env)->ExceptionCheck(env)) {
    // todo handle error
  }

  TAOS_RES *tres = taos_query_with_reqid(tscon, str, reqId);
  int32_t   code = taos_errno(tres);

  if (code != TSDB_CODE_SUCCESS) {
    jniError("jobj:%p, conn:%p, code:0x%x, msg:%s", jobj, tscon, code, taos_errstr(tres));
  } else {
    if (taos_is_update_query(tres)) {
      int32_t affectRows = taos_affected_rows(tres);
      jniDebug("jobj:%p, conn:%p, code:0x%x, affect rows:%d", jobj, tscon, code, affectRows);
    } else {
      jniDebug("jobj:%p, conn:%p, code:0x%x", jobj, tscon, code);
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    }
  }

  taosMemoryFreeClear(str);
  return (jlong)tres;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getErrCodeImp(JNIEnv *env, jobject jobj, jlong con,
                                                                             jlong tres) {
  int32_t code = check_for_params(jobj, con, tres);
  if (code != JNI_SUCCESS) {
    return code;
  }

  return (jint)taos_errno((TAOS_RES *)tres);
}

JNIEXPORT jstring JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getErrMsgImp(JNIEnv *env, jobject jobj, jlong tres) {
  TAOS_RES *pSql = (TAOS_RES *)tres;
  return (*env)->NewStringUTF(env, (const char *)taos_errstr(pSql));
}

JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getResultSetImp(JNIEnv *env, jobject jobj, jlong con,
                                                                                jlong tres) {
  TAOS   *tscon = (TAOS *)con;
  int32_t code = check_for_params(jobj, con, tres);
  if (code != JNI_SUCCESS) {
    return code;
  }

  if (taos_is_update_query((TAOS_RES *)tres)) {
    jniDebug("jobj:%p, conn:%p, update query, no resultset, %p", jobj, tscon, (void *)tres);
  } else {
    jniDebug("jobj:%p, conn:%p, get resultset, %p", jobj, tscon, (void *)tres);
  }

  return tres;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_isUpdateQueryImp(JNIEnv *env, jobject jobj, jlong con,
                                                                                jlong tres) {
  int32_t code = check_for_params(jobj, con, tres);
  if (code != JNI_SUCCESS) {
    return code;
  }

  return (taos_is_update_query((TAOS_RES *)tres) ? 1 : 0);
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_freeResultSetImp(JNIEnv *env, jobject jobj, jlong con,
                                                                                jlong res) {
  int32_t code = check_for_params(jobj, con, res);
  if (code != JNI_SUCCESS) {
    return code;
  }

  jniDebug("jobj:%p, conn:%p, free resultset:%p", jobj, (TAOS *)con, (void *)res);
443
  taos_free_result((void *)res);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
  return JNI_SUCCESS;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getAffectedRowsImp(JNIEnv *env, jobject jobj, jlong con,
                                                                                  jlong res) {
  TAOS   *tscon = (TAOS *)con;
  int32_t code = check_for_params(jobj, con, res);
  if (code != JNI_SUCCESS) {
    return code;
  }

  jint ret = taos_affected_rows((TAOS_RES *)res);
  jniDebug("jobj:%p, conn:%p, sql:%p, res: %p, affect rows:%d", jobj, tscon, (TAOS *)con, (TAOS_RES *)res,
           (int32_t)ret);

  return ret;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getSchemaMetaDataImp(JNIEnv *env, jobject jobj,
                                                                                    jlong con, jlong res,
                                                                                    jobject arrayListObj) {
  TAOS   *tscon = (TAOS *)con;
  int32_t code = check_for_params(jobj, con, res);
  if (code != JNI_SUCCESS) {
    return code;
  }

  TAOS_RES   *tres = (TAOS_RES *)res;
  TAOS_FIELD *fields = taos_fetch_fields(tres);

  int32_t num_fields = taos_num_fields(tres);
  if (num_fields == 0) {
    jniError("jobj:%p, conn:%p, resultset:%p, fields size is %d", jobj, tscon, tres, num_fields);
    return JNI_NUM_OF_FIELDS_0;
  } else {
    jniDebug("jobj:%p, conn:%p, resultset:%p, fields size is %d", jobj, tscon, tres, num_fields);
    for (int i = 0; i < num_fields; ++i) {
      jobject metadataObj = (*env)->NewObject(env, g_metadataClass, g_metadataConstructFp);
      (*env)->SetIntField(env, metadataObj, g_metadataColtypeField, fields[i].type);
      (*env)->SetIntField(env, metadataObj, g_metadataColsizeField, fields[i].bytes);
      (*env)->SetIntField(env, metadataObj, g_metadataColindexField, i);
      jstring metadataObjColname = (*env)->NewStringUTF(env, fields[i].name);
      (*env)->SetObjectField(env, metadataObj, g_metadataColnameField, metadataObjColname);
      (*env)->CallBooleanMethod(env, arrayListObj, g_arrayListAddFp, metadataObj);
    }
  }

  return JNI_SUCCESS;
}

/**
 *
 * @param env      vm
 * @param nchar    true multibytes data
 * @param maxBytes the maximum allowable field length
 * @return
 */
jstring jniFromNCharToByteArray(JNIEnv *env, char *nchar, int32_t maxBytes) {
  jbyteArray bytes = (*env)->NewByteArray(env, maxBytes);
  (*env)->SetByteArrayRegion(env, bytes, 0, maxBytes, (jbyte *)nchar);
  return bytes;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_fetchRowImp(JNIEnv *env, jobject jobj, jlong con,
                                                                           jlong res, jobject rowobj) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection is closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  TAOS_RES *result = (TAOS_RES *)res;
  if (result == NULL) {
    jniError("jobj:%p, conn:%p, resultset is null", jobj, tscon);
    return JNI_RESULT_SET_NULL;
  }

  TAOS_FIELD *fields = taos_fetch_fields(result);

  int32_t numOfFields = taos_num_fields(result);
  if (numOfFields == 0) {
    jniError("jobj:%p, conn:%p, resultset:%p, fields size %d", jobj, tscon, (void *)res, numOfFields);
    return JNI_NUM_OF_FIELDS_0;
  }

  TAOS_ROW row = taos_fetch_row(result);
  if (row == NULL) {
    int code = taos_errno(result);
    if (code == TSDB_CODE_SUCCESS) {
      jniDebug("jobj:%p, conn:%p, resultset:%p, fields size is %d, fetch row to the end", jobj, tscon, (void *)res,
               numOfFields);
      return JNI_FETCH_END;
    } else {
537
      jniDebug("jobj:%p, conn:%p, interrupted query. fetch row error code: 0x%x, msg:%s", jobj, tscon, code,
538
               taos_errstr(result));
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
      return JNI_RESULT_SET_NULL;
    }
  }

  int32_t *length = taos_fetch_lengths(result);

  char tmp[TSDB_MAX_BYTES_PER_ROW] = {0};

  for (int i = 0; i < numOfFields; i++) {
    if (row[i] == NULL) {
      continue;
    }

    switch (fields[i].type) {
      case TSDB_DATA_TYPE_BOOL:
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetBooleanFp, i, (jboolean)(*((char *)row[i]) == 1));
        break;
      case TSDB_DATA_TYPE_UTINYINT:
      case TSDB_DATA_TYPE_TINYINT:
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetByteFp, i, (jbyte) * ((int8_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_USMALLINT:
      case TSDB_DATA_TYPE_SMALLINT:
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetShortFp, i, (jshort) * ((int16_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_UINT:
      case TSDB_DATA_TYPE_INT:
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetIntFp, i, (jint) * (int32_t *)row[i]);
        break;
      case TSDB_DATA_TYPE_UBIGINT:
      case TSDB_DATA_TYPE_BIGINT:
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetLongFp, i, (jlong) * ((int64_t *)row[i]));
        break;
      case TSDB_DATA_TYPE_FLOAT: {
        float fv = 0;
        fv = GET_FLOAT_VAL(row[i]);
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetFloatFp, i, (jfloat)fv);
      } break;
      case TSDB_DATA_TYPE_DOUBLE: {
        double dv = 0;
        dv = GET_DOUBLE_VAL(row[i]);
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetDoubleFp, i, (jdouble)dv);
      } break;
      case TSDB_DATA_TYPE_BINARY: {
        memcpy(tmp, row[i], length[i]);  // handle the case that terminated does not exist
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetStringFp, i, (*env)->NewStringUTF(env, tmp));

        memset(tmp, 0, length[i]);
        break;
      }
      case TSDB_DATA_TYPE_NCHAR: {
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetByteArrayFp, i,
                               jniFromNCharToByteArray(env, (char *)row[i], length[i]));
        break;
      }
      case TSDB_DATA_TYPE_JSON: {
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetByteArrayFp, i,
                               jniFromNCharToByteArray(env, (char *)row[i], length[i]));
        break;
      }
      case TSDB_DATA_TYPE_TIMESTAMP: {
        int precision = taos_result_precision(result);
        (*env)->CallVoidMethod(env, rowobj, g_rowdataSetTimestampFp, i, (jlong) * ((int64_t *)row[i]), precision);
        break;
      }
      default:
        break;
    }
  }

  return JNI_SUCCESS;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_fetchBlockImp(JNIEnv *env, jobject jobj, jlong con,
                                                                             jlong res, jobject rowobj) {
  TAOS   *tscon = (TAOS *)con;
  int32_t code = check_for_params(jobj, con, res);
  if (code != JNI_SUCCESS) {
    return code;
  }

  TAOS_RES *tres = (TAOS_RES *)res;

  int32_t numOfFields = taos_num_fields(tres);
623 624 625
  if (numOfFields <= 0) {
    jniError("jobj:%p, conn:%p, query interrupted. taos_num_fields error code: 0x%x, msg:%s", jobj, tscon,
             taos_errno(tres), taos_errstr(tres));
626 627
    return JNI_RESULT_SET_NULL;
  }
628

D
dapan1121 已提交
629 630
  void   *data = NULL;
  int32_t numOfRows = 0;
631 632 633 634 635 636
  int     error_code = taos_fetch_raw_block(tres, &numOfRows, &data);
  if (numOfRows == 0) {
    if (error_code == JNI_SUCCESS) {
      jniDebug("jobj:%p, conn:%p, resultset:%p, no data to retrieve", jobj, tscon, (void *)res);
      return JNI_FETCH_END;
    } else {
637
      jniError("jobj:%p, conn:%p, query interrupted. fetch block error code: 0x%x, msg:%s", jobj, tscon, error_code,
638
               taos_errstr(tres));
639 640 641 642 643 644 645
      return JNI_RESULT_SET_NULL;
    }
  }

  (*env)->CallVoidMethod(env, rowobj, g_blockdataSetNumOfRowsFp, (jint)numOfRows);
  (*env)->CallVoidMethod(env, rowobj, g_blockdataSetNumOfColsFp, (jint)numOfFields);

H
huolibo 已提交
646
  int32_t len = *(int32_t *)(((char *)data) + 4);
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
  (*env)->CallVoidMethod(env, rowobj, g_blockdataSetByteArrayFp, jniFromNCharToByteArray(env, (char *)data, len));

  return JNI_SUCCESS;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_closeConnectionImp(JNIEnv *env, jobject jobj,
                                                                                  jlong con) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection is already closed", jobj);
    return JNI_CONNECTION_NULL;
  } else {
    jniDebug("jobj:%p, conn:%p, close connection success", jobj, tscon);
    taos_close(tscon);
    return JNI_SUCCESS;
  }
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_validateCreateTableSqlImp(JNIEnv *env, jobject jobj,
                                                                                         jlong con, jbyteArray jsql) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection is closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  if (jsql == NULL) {
    jniError("jobj:%p, conn:%p, sql is null", jobj, tscon);
    return JNI_SQL_NULL;
  }

  jsize len = (*env)->GetArrayLength(env, jsql);

  char *str = (char *)taosMemoryCalloc(1, sizeof(char) * (len + 1));
  (*env)->GetByteArrayRegion(env, jsql, 0, len, (jbyte *)str);
  if ((*env)->ExceptionCheck(env)) {
    // todo handle error
  }

  int code = taos_validate_sql(tscon, str);
687
  jniDebug("jobj:%p, conn:%p, code: 0x%x", jobj, tscon, code);
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

  taosMemoryFreeClear(str);
  return code;
}

JNIEXPORT jstring JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getTsCharset(JNIEnv *env, jobject jobj) {
  return (*env)->NewStringUTF(env, (const char *)tsCharset);
}

/**
 * Get Result Time Precision
 * @param env           vm
 * @param jobj          the TSDBJNIConnector java object
 * @param con           the c connection pointer
 * @param res           the TAOS_RES object, i.e. the SSqlObject
 * @return precision    0:ms 1:us 2:ns
 */
JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getResultTimePrecisionImp(JNIEnv *env, jobject jobj,
                                                                                         jlong con, jlong res) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection is closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  TAOS_RES *result = (TAOS_RES *)res;
  if (result == NULL) {
    jniError("jobj:%p, conn:%p, resultset is null", jobj, tscon);
    return JNI_RESULT_SET_NULL;
  }

  return taos_result_precision(result);
}

JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_prepareStmtImp(JNIEnv *env, jobject jobj,
                                                                               jbyteArray jsql, jlong con) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  if (jsql == NULL) {
    jniError("jobj:%p, conn:%p, empty sql string", jobj, tscon);
    return JNI_SQL_NULL;
  }

  jsize len = (*env)->GetArrayLength(env, jsql);

  char *str = (char *)taosMemoryCalloc(1, sizeof(char) * (len + 1));
  if (str == NULL) {
    jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tscon);
    return JNI_OUT_OF_MEMORY;
  }

  (*env)->GetByteArrayRegion(env, jsql, 0, len, (jbyte *)str);
  if ((*env)->ExceptionCheck(env)) {
    // todo handle error
  }

  TAOS_STMT *pStmt = taos_stmt_init(tscon);
  int32_t    code = taos_stmt_prepare(pStmt, str, len);
  taosMemoryFreeClear(str);
  if (code != TSDB_CODE_SUCCESS) {
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    jniError("prepareStmt jobj:%p, conn:%p, code: 0x%x", jobj, tscon, code);
    return JNI_TDENGINE_ERROR;
  }

  return (jlong)pStmt;
}

JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_prepareStmtWithReqId(JNIEnv *env, jobject jobj,
                                                                                     jbyteArray jsql, jlong con,
                                                                                     jlong reqId) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  if (jsql == NULL) {
    jniError("jobj:%p, conn:%p, empty sql string", jobj, tscon);
    return JNI_SQL_NULL;
  }

  jsize len = (*env)->GetArrayLength(env, jsql);

  char *str = (char *)taosMemoryCalloc(1, sizeof(char) * (len + 1));
  if (str == NULL) {
    jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tscon);
    return JNI_OUT_OF_MEMORY;
  }

  (*env)->GetByteArrayRegion(env, jsql, 0, len, (jbyte *)str);
  if ((*env)->ExceptionCheck(env)) {
    // todo handle error
  }

  TAOS_STMT *pStmt = taos_stmt_init_with_reqid(tscon, reqId);
  int32_t    code = taos_stmt_prepare(pStmt, str, len);
  taosMemoryFreeClear(str);
  if (code != TSDB_CODE_SUCCESS) {
    jniError("prepareStmtWithReqId jobj:%p, conn:%p, code: 0x%x", jobj, tscon, code);
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
    return JNI_TDENGINE_ERROR;
  }

  return (jlong)pStmt;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setBindTableNameImp(JNIEnv *env, jobject jobj,
                                                                                   jlong stmt, jstring jname,
                                                                                   jlong conn) {
  TAOS *tsconn = (TAOS *)conn;
  if (tsconn == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  TAOS_STMT *pStmt = (TAOS_STMT *)stmt;
  if (pStmt == NULL) {
    jniError("jobj:%p, conn:%p, invalid stmt handle", jobj, tsconn);
    return JNI_SQL_NULL;
  }

  const char *name = (*env)->GetStringUTFChars(env, jname, NULL);

  int32_t code = taos_stmt_set_tbname((void *)stmt, name);
  if (code != TSDB_CODE_SUCCESS) {
    (*env)->ReleaseStringUTFChars(env, jname, name);

818
    jniError("bindTableName jobj:%p, conn:%p, code: 0x%x", jobj, tsconn, code);
819
    return code;
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
  }

  jniDebug("jobj:%p, conn:%p, set stmt bind table name:%s", jobj, tsconn, name);
  (*env)->ReleaseStringUTFChars(env, jname, name);
  return JNI_SUCCESS;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setTableNameTagsImp(
    JNIEnv *env, jobject jobj, jlong stmt, jstring tableName, jint numOfTags, jbyteArray tags, jbyteArray typeList,
    jbyteArray lengthList, jbyteArray nullList, jlong conn) {
  TAOS *tsconn = (TAOS *)conn;
  if (tsconn == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  TAOS_STMT *pStmt = (TAOS_STMT *)stmt;
  if (pStmt == NULL) {
    jniError("jobj:%p, conn:%p, invalid stmt handle", jobj, tsconn);
    return JNI_SQL_NULL;
  }

  jsize len = (*env)->GetArrayLength(env, tags);
  char *tagsData = (char *)taosMemoryCalloc(1, len);
  (*env)->GetByteArrayRegion(env, tags, 0, len, (jbyte *)tagsData);
  if ((*env)->ExceptionCheck(env)) {
    // todo handle error
  }

  len = (*env)->GetArrayLength(env, lengthList);
  int32_t *lengthArray = (int32_t *)taosMemoryCalloc(1, len);
  (*env)->GetByteArrayRegion(env, lengthList, 0, len, (jbyte *)lengthArray);
  if ((*env)->ExceptionCheck(env)) {
  }

  len = (*env)->GetArrayLength(env, typeList);
  char *typeArray = (char *)taosMemoryCalloc(1, len);
  (*env)->GetByteArrayRegion(env, typeList, 0, len, (jbyte *)typeArray);
  if ((*env)->ExceptionCheck(env)) {
  }

  len = (*env)->GetArrayLength(env, nullList);
  char *nullArray = (char *)taosMemoryCalloc(1, len);
  (*env)->GetByteArrayRegion(env, nullList, 0, len, (jbyte *)nullArray);
  if ((*env)->ExceptionCheck(env)) {
  }

  const char *name = (*env)->GetStringUTFChars(env, tableName, NULL);
  char       *curTags = tagsData;

  TAOS_MULTI_BIND *tagsBind = taosMemoryCalloc(numOfTags, sizeof(TAOS_MULTI_BIND));
  for (int32_t i = 0; i < numOfTags; ++i) {
    tagsBind[i].buffer_type = typeArray[i];
    tagsBind[i].buffer = curTags;
    tagsBind[i].is_null = &nullArray[i];
    tagsBind[i].length = &lengthArray[i];

    curTags += lengthArray[i];
  }

  int32_t code = taos_stmt_set_tbname_tags((void *)stmt, name, tagsBind);

  int32_t nTags = (int32_t)numOfTags;
  jniDebug("jobj:%p, conn:%p, set table name:%s, numOfTags:%d", jobj, tsconn, name, nTags);

  taosMemoryFreeClear(tagsData);
  taosMemoryFreeClear(lengthArray);
  taosMemoryFreeClear(typeArray);
  taosMemoryFreeClear(nullArray);
  taosMemoryFreeClear(tagsBind);
  (*env)->ReleaseStringUTFChars(env, tableName, name);

  if (code != TSDB_CODE_SUCCESS) {
893
    jniError("tableNameTags jobj:%p, conn:%p, code: 0x%x", jobj, tsconn, code);
894
    return code;
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
  }
  return JNI_SUCCESS;
}

JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_bindColDataImp(
    JNIEnv *env, jobject jobj, jlong stmt, jbyteArray colDataList, jbyteArray lengthList, jbyteArray nullList,
    jint dataType, jint dataBytes, jint numOfRows, jint colIndex, jlong con) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  TAOS_STMT *pStmt = (TAOS_STMT *)stmt;
  if (pStmt == NULL) {
    jniError("jobj:%p, conn:%p, invalid stmt", jobj, tscon);
    return JNI_SQL_NULL;
  }

  // todo refactor
  jsize len = (*env)->GetArrayLength(env, colDataList);
  char *colBuf = (char *)taosMemoryCalloc(1, len);
  (*env)->GetByteArrayRegion(env, colDataList, 0, len, (jbyte *)colBuf);
  if ((*env)->ExceptionCheck(env)) {
    // todo handle error
  }

  len = (*env)->GetArrayLength(env, lengthList);
  char *lengthArray = (char *)taosMemoryCalloc(1, len);
  (*env)->GetByteArrayRegion(env, lengthList, 0, len, (jbyte *)lengthArray);
  if ((*env)->ExceptionCheck(env)) {
  }

  len = (*env)->GetArrayLength(env, nullList);
  char *nullArray = (char *)taosMemoryCalloc(1, len);
  (*env)->GetByteArrayRegion(env, nullList, 0, len, (jbyte *)nullArray);
  if ((*env)->ExceptionCheck(env)) {
  }

  // bind multi-rows with only one invoke.
  TAOS_MULTI_BIND *b = taosMemoryCalloc(1, sizeof(TAOS_MULTI_BIND));

  b->num = numOfRows;
  b->buffer_type = dataType;  // todo check data type
  b->buffer_length = IS_VAR_DATA_TYPE(dataType) ? dataBytes : tDataTypes[dataType].bytes;
  b->is_null = nullArray;
  b->buffer = colBuf;
  b->length = (int32_t *)lengthArray;

  // set the length and is_null array
  if (!IS_VAR_DATA_TYPE(dataType)) {
    int32_t bytes = tDataTypes[dataType].bytes;
    for (int32_t i = 0; i < numOfRows; ++i) {
      b->length[i] = bytes;
    }
  }

  int32_t code = taos_stmt_bind_single_param_batch(pStmt, b, colIndex);
  taosMemoryFreeClear(b->length);
  taosMemoryFreeClear(b->buffer);
  taosMemoryFreeClear(b->is_null);
  taosMemoryFreeClear(b);

  if (code != TSDB_CODE_SUCCESS) {
959
    jniError("bindColData jobj:%p, conn:%p, code: 0x%x", jobj, tscon, code);
960
    return code;
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
  }

  return JNI_SUCCESS;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_addBatchImp(JNIEnv *env, jobject jobj, jlong stmt,
                                                                           jlong con) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  TAOS_STMT *pStmt = (TAOS_STMT *)stmt;
  if (pStmt == NULL) {
    jniError("jobj:%p, conn:%p, invalid stmt", jobj, tscon);
    return JNI_SQL_NULL;
  }

  int32_t code = taos_stmt_add_batch(pStmt);
  if (code != TSDB_CODE_SUCCESS) {
982
    jniError("add batch jobj:%p, conn:%p, code: 0x%x", jobj, tscon, code);
983
    return code;
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
  }

  jniDebug("jobj:%p, conn:%p, stmt closed", jobj, tscon);
  return JNI_SUCCESS;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_executeBatchImp(JNIEnv *env, jobject jobj, jlong stmt,
                                                                               jlong con) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  TAOS_STMT *pStmt = (TAOS_STMT *)stmt;
  if (pStmt == NULL) {
    jniError("jobj:%p, conn:%p, invalid stmt", jobj, tscon);
    return JNI_SQL_NULL;
  }

  int32_t code = taos_stmt_execute(pStmt);
  if (code != TSDB_CODE_SUCCESS) {
1006
    jniError("excute batch jobj:%p, conn:%p, code: 0x%x", jobj, tscon, code);
1007
    return code;
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
  }

  jniDebug("jobj:%p, conn:%p, batch execute", jobj, tscon);
  return JNI_SUCCESS;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_closeStmt(JNIEnv *env, jobject jobj, jlong stmt,
                                                                         jlong con) {
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  TAOS_STMT *pStmt = (TAOS_STMT *)stmt;
  if (pStmt == NULL) {
    jniError("jobj:%p, conn:%p, invalid stmt", jobj, tscon);
    return JNI_SQL_NULL;
  }

  int32_t code = taos_stmt_close(pStmt);
  if (code != TSDB_CODE_SUCCESS) {
1030
    jniError("close stmt jobj:%p, conn:%p, code: 0x%x", jobj, tscon, code);
1031
    return code;
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
  }

  jniDebug("jobj:%p, conn:%p, stmt closed", jobj, tscon);
  return JNI_SUCCESS;
}

JNIEXPORT jstring JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_stmtErrorMsgImp(JNIEnv *env, jobject jobj, jlong stmt,
                                                                                  jlong con) {
  char  errMsg[128];
  TAOS *tscon = (TAOS *)con;
  if (tscon == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    sprintf(errMsg, "jobj:%p, connection already closed", jobj);
    return (*env)->NewStringUTF(env, errMsg);
  }

  TAOS_STMT *pStmt = (TAOS_STMT *)stmt;
  if (pStmt == NULL) {
    jniError("jobj:%p, conn:%p, invalid stmt", jobj, tscon);
    sprintf(errMsg, "jobj:%p, conn:%p, invalid stmt", jobj, tscon);
    return (*env)->NewStringUTF(env, errMsg);
  }

  return (*env)->NewStringUTF(env, taos_stmt_errstr((TAOS_STMT *)stmt));
}

TAOS_RES *schemalessInsert(JNIEnv *env, jobject jobj, jobjectArray lines, TAOS *taos, jint protocol, jint precision) {
  int    numLines = (*env)->GetArrayLength(env, lines);
  char **c_lines = taosMemoryCalloc(numLines, sizeof(char *));
  if (c_lines == NULL) {
    jniError("c_lines:%p, alloc memory failed", c_lines);
    return NULL;
  }
  for (int i = 0; i < numLines; ++i) {
    jstring line = (jstring)((*env)->GetObjectArrayElement(env, lines, i));
    c_lines[i] = (char *)(*env)->GetStringUTFChars(env, line, 0);
  }

  TAOS_RES *tres = taos_schemaless_insert(taos, c_lines, numLines, protocol, precision);

  for (int i = 0; i < numLines; ++i) {
    jstring line = (jstring)((*env)->GetObjectArrayElement(env, lines, i));
    (*env)->ReleaseStringUTFChars(env, line, c_lines[i]);
  }

  taosMemoryFreeClear(c_lines);
  return tres;
}

JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_insertLinesImp(JNIEnv *env, jobject jobj,
                                                                              jobjectArray lines, jlong conn,
                                                                              jint protocol, jint precision) {
  TAOS *taos = (TAOS *)conn;
  if (taos == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  TAOS_RES *tres = schemalessInsert(env, jobj, lines, taos, protocol, precision);

  int code = taos_errno(tres);
  if (code != TSDB_CODE_SUCCESS) {
1094
    jniError("jobj:%p, conn:%p, code: 0x%x, msg:%s", jobj, taos, code, taos_errstr(tres));
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
    taos_free_result(tres);
    return JNI_TDENGINE_ERROR;
  }
  taos_free_result(tres);

  return JNI_SUCCESS;
}

JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_schemalessInsertImp(JNIEnv *env, jobject jobj,
                                                                                    jobjectArray lines, jlong conn,
                                                                                    jint protocol, jint precision) {
  TAOS *taos = (TAOS *)conn;
  if (taos == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  TAOS_RES *tres = schemalessInsert(env, jobj, lines, taos, protocol, precision);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162

  return (jlong)tres;
}

JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_schemalessInsertWithReqId(
    JNIEnv *env, jobject jobj, jlong conn, jobjectArray lines, jint protocol, jint precision, jlong reqId) {
  TAOS *taos = (TAOS *)conn;
  if (taos == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  int    numLines = (*env)->GetArrayLength(env, lines);
  char **c_lines = taosMemoryCalloc(numLines, sizeof(char *));
  if (c_lines == NULL) {
    jniError("c_lines:%p, alloc memory failed", c_lines);
    return JNI_OUT_OF_MEMORY;
  }

  for (int i = 0; i < numLines; ++i) {
    jstring line = (jstring)((*env)->GetObjectArrayElement(env, lines, i));
    c_lines[i] = (char *)(*env)->GetStringUTFChars(env, line, 0);
  }

  TAOS_RES *tres = taos_schemaless_insert_with_reqid(taos, c_lines, numLines, protocol, precision, reqId);

  for (int i = 0; i < numLines; ++i) {
    jstring line = (jstring)((*env)->GetObjectArrayElement(env, lines, i));
    (*env)->ReleaseStringUTFChars(env, line, c_lines[i]);
  }

  taosMemoryFreeClear(c_lines);

  return (jlong)tres;
}

JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_schemalessInsertWithTtl(JNIEnv *env, jobject jobj,
                                                                                        jlong conn, jobjectArray lines,
                                                                                        jint protocol, jint precision,
                                                                                        jint ttl) {
  TAOS *taos = (TAOS *)conn;
  if (taos == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  int    numLines = (*env)->GetArrayLength(env, lines);
  char **c_lines = taosMemoryCalloc(numLines, sizeof(char *));
  if (c_lines == NULL) {
    jniError("c_lines:%p, alloc memory failed", c_lines);
1163 1164
    return JNI_OUT_OF_MEMORY;
  }
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179

  for (int i = 0; i < numLines; ++i) {
    jstring line = (jstring)((*env)->GetObjectArrayElement(env, lines, i));
    c_lines[i] = (char *)(*env)->GetStringUTFChars(env, line, 0);
  }

  TAOS_RES *tres = taos_schemaless_insert_ttl(taos, c_lines, numLines, protocol, precision, ttl);

  for (int i = 0; i < numLines; ++i) {
    jstring line = (jstring)((*env)->GetObjectArrayElement(env, lines, i));
    (*env)->ReleaseStringUTFChars(env, line, c_lines[i]);
  }

  taosMemoryFreeClear(c_lines);

1180
  return (jlong)tres;
1181
}
1182

1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_schemalessInsertWithTtlAndReqId(
    JNIEnv *env, jobject jobj, jlong conn, jobjectArray lines, jint protocol, jint precision, jint ttl, jlong reqId) {
  TAOS *taos = (TAOS *)conn;
  if (taos == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    return JNI_CONNECTION_NULL;
  }

  int    numLines = (*env)->GetArrayLength(env, lines);
  char **c_lines = taosMemoryCalloc(numLines, sizeof(char *));
  if (c_lines == NULL) {
    jniError("c_lines:%p, alloc memory failed", c_lines);
    return JNI_OUT_OF_MEMORY;
  }

  for (int i = 0; i < numLines; ++i) {
    jstring line = (jstring)((*env)->GetObjectArrayElement(env, lines, i));
    c_lines[i] = (char *)(*env)->GetStringUTFChars(env, line, 0);
  }

  TAOS_RES *tres = taos_schemaless_insert_ttl_with_reqid(taos, c_lines, numLines, protocol, precision, ttl, reqId);

  for (int i = 0; i < numLines; ++i) {
    jstring line = (jstring)((*env)->GetObjectArrayElement(env, lines, i));
    (*env)->ReleaseStringUTFChars(env, line, c_lines[i]);
  }

  taosMemoryFreeClear(c_lines);

  return (jlong)tres;
}

JNIEXPORT jobject createSchemalessResp(JNIEnv *env, int totalRows, int code, const char *msg) {
  // find class
  jclass schemaless_clazz = (*env)->FindClass(env, "com/taosdata/jdbc/SchemalessResp");
  // find methods
  jmethodID init_method = (*env)->GetMethodID(env, schemaless_clazz, "<init>", "()V");
  jmethodID setCode_method = (*env)->GetMethodID(env, schemaless_clazz, "setCode", "(I)V");
  jmethodID setMsg_method = (*env)->GetMethodID(env, schemaless_clazz, "setMsg", "(Ljava/lang/String;)V");
  jmethodID setTotalRows_method = (*env)->GetMethodID(env, schemaless_clazz, "setTotalRows", "(I)V");
  // new schemaless
  jobject schemaless_obj = (*env)->NewObject(env, schemaless_clazz, init_method);
  // set code
  (*env)->CallVoidMethod(env, schemaless_obj, setCode_method, code);
  // set totalRows
  (*env)->CallVoidMethod(env, schemaless_obj, setTotalRows_method, totalRows);
  // set message
  jstring message = (*env)->NewStringUTF(env, msg);
  (*env)->CallVoidMethod(env, schemaless_obj, setMsg_method, message);

  return schemaless_obj;
}

JNIEXPORT jobject JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_schemalessInsertRaw(JNIEnv *env, jobject jobj,
                                                                                      jlong conn, jstring data,
                                                                                      jint protocol, jint precision) {
  TAOS *taos = (TAOS *)conn;
  if (taos == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    char *msg = "JNI connection is NULL";
    return createSchemalessResp(env, 0, JNI_CONNECTION_NULL, msg);
  }

  char     *line = (char *)(*env)->GetStringUTFChars(env, data, NULL);
  jint      len = (*env)->GetStringUTFLength(env, data);
  int32_t   totalRows;
  TAOS_RES *tres = taos_schemaless_insert_raw(taos, line, len, &totalRows, protocol, precision);

  (*env)->ReleaseStringUTFChars(env, data, line);

  // if (tres == NULL) {
  //   jniError("jobj:%p, schemaless raw insert failed", jobj);
  //   char *msg = "JNI schemaless raw insert return null";
  //   return createSchemalessResp(env, 0, JNI_TDENGINE_ERROR, msg);
  // }

  int code = taos_errno(tres);
  if (code != TSDB_CODE_SUCCESS) {
    jniError("jobj:%p, conn:%p, code: 0x%x, msg:%s", jobj, taos, code, taos_errstr(tres));
    taos_free_result(tres);
    return createSchemalessResp(env, 0, code, taos_errstr(tres));
  }
  taos_free_result(tres);

  return createSchemalessResp(env, totalRows, JNI_SUCCESS, NULL);
}

JNIEXPORT jobject JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_schemalessInsertRawWithReqId(
    JNIEnv *env, jobject jobj, jlong conn, jstring data, jint protocol, jint precision, jlong reqId) {
  TAOS *taos = (TAOS *)conn;
  if (taos == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    char *msg = "JNI connection is NULL";
    return createSchemalessResp(env, 0, JNI_CONNECTION_NULL, msg);
  }

  char     *line = (char *)(*env)->GetStringUTFChars(env, data, NULL);
  jint      len = (*env)->GetStringUTFLength(env, data);
  int32_t   totalRows;
  TAOS_RES *tres = taos_schemaless_insert_raw_with_reqid(taos, line, len, &totalRows, protocol, precision, reqId);

  (*env)->ReleaseStringUTFChars(env, data, line);

  int code = taos_errno(tres);
  if (code != TSDB_CODE_SUCCESS) {
    jniError("jobj:%p, conn:%p, code: 0x%x, msg:%s", jobj, taos, code, taos_errstr(tres));
    taos_free_result(tres);
    return createSchemalessResp(env, 0, code, taos_errstr(tres));
  }
  taos_free_result(tres);

  return createSchemalessResp(env, totalRows, JNI_SUCCESS, NULL);
}

JNIEXPORT jobject JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_schemalessInsertRawWithTtl(JNIEnv *env, jobject jobj,
                                                                                             jlong conn, jstring data,
                                                                                             jint protocol,
                                                                                             jint precision, jint ttl) {
  TAOS *taos = (TAOS *)conn;
  if (taos == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    char *msg = "JNI connection is NULL";
    return createSchemalessResp(env, 0, JNI_CONNECTION_NULL, msg);
  }

  char     *line = (char *)(*env)->GetStringUTFChars(env, data, NULL);
  jint      len = (*env)->GetStringUTFLength(env, data);
  int32_t   totalRows;
  TAOS_RES *tres = taos_schemaless_insert_raw_ttl(taos, line, len, &totalRows, protocol, precision, ttl);

  (*env)->ReleaseStringUTFChars(env, data, line);

  int code = taos_errno(tres);
  if (code != TSDB_CODE_SUCCESS) {
    jniError("jobj:%p, conn:%p, code: 0x%x, msg:%s", jobj, taos, code, taos_errstr(tres));
    taos_free_result(tres);
    return createSchemalessResp(env, 0, code, taos_errstr(tres));
  }
  taos_free_result(tres);

  return createSchemalessResp(env, totalRows, JNI_SUCCESS, NULL);
}

JNIEXPORT jobject JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_schemalessInsertRawWithTtlAndReqId(
    JNIEnv *env, jobject jobj, jlong conn, jstring data, jint protocol, jint precision, jint ttl, jlong reqId) {
  TAOS *taos = (TAOS *)conn;
  if (taos == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    char *msg = "JNI connection is NULL";
    return createSchemalessResp(env, 0, JNI_CONNECTION_NULL, msg);
  }

  char     *line = (char *)(*env)->GetStringUTFChars(env, data, NULL);
  jint      len = (*env)->GetStringUTFLength(env, data);
  int32_t   totalRows;
  TAOS_RES *tres =
      taos_schemaless_insert_raw_ttl_with_reqid(taos, line, len, &totalRows, protocol, precision, ttl, reqId);

  (*env)->ReleaseStringUTFChars(env, data, line);

  int code = taos_errno(tres);
  if (code != TSDB_CODE_SUCCESS) {
    jniError("jobj:%p, conn:%p, code: 0x%x, msg:%s", jobj, taos, code, taos_errstr(tres));
    taos_free_result(tres);
    return createSchemalessResp(env, 0, code, taos_errstr(tres));
  }
  taos_free_result(tres);

  return createSchemalessResp(env, totalRows, JNI_SUCCESS, NULL);
}

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
// TABLE_VG_ID_FID_CACHE cache resp object for getTableVgID
typedef struct TABLE_VG_ID_FIELD_CACHE {
  int      cached;
  jclass   clazz;
  jfieldID codeField;
  jfieldID vgIDField;
} TABLE_VG_ID_FIELD_CACHE;

TABLE_VG_ID_FIELD_CACHE tableVgIdFieldCache;

void cacheTableVgIDField(JNIEnv *env, jobject jobj) {
  if (tableVgIdFieldCache.cached) {
    return;
  }

  tableVgIdFieldCache.clazz = (*env)->GetObjectClass(env, jobj);
  tableVgIdFieldCache.codeField = (*env)->GetFieldID(env, tableVgIdFieldCache.clazz, "code", "I");
  tableVgIdFieldCache.vgIDField = (*env)->GetFieldID(env, tableVgIdFieldCache.clazz, "vgID", "I");
  tableVgIdFieldCache.cached = 1;
}

JNIEXPORT jobject JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_getTableVgID(JNIEnv *env, jobject jobj, jlong conn,
                                                                               jstring jdb, jstring jtable,
                                                                               jobject resp) {
  if (!tableVgIdFieldCache.cached) {
    cacheTableVgIDField(env, resp);
  }

  TAOS *taos = (TAOS *)conn;
  if (taos == NULL) {
    jniError("jobj:%p, connection already closed", jobj);
    (*env)->SetIntField(env, resp, tableVgIdFieldCache.codeField, JNI_CONNECTION_NULL);
    return resp;
  }

  const char *db = NULL;
  const char *table = NULL;
  int         vgID = 0;

  if (jdb != NULL) {
    db = (*env)->GetStringUTFChars(env, jdb, NULL);
  }
  if (jtable != NULL) {
    table = (*env)->GetStringUTFChars(env, jtable, NULL);
  }

  int code = taos_get_table_vgId(taos, db, table, &vgID);
  if (db != NULL) {
    (*env)->ReleaseStringUTFChars(env, jdb, db);
  }
  if (table != NULL) {
    (*env)->ReleaseStringUTFChars(env, jtable, table);
  }

  (*env)->SetIntField(env, resp, tableVgIdFieldCache.codeField, code);
  (*env)->SetIntField(env, resp, tableVgIdFieldCache.vgIDField, vgID);
  return resp;
}