• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #10013

06 Sep 2023 12:26PM CUT coverage: 47.691% (+0.04%) from 47.654%
#10013

push

travis_ci

web-flow
[To rel/1.2] Change display nums to fixed 1000 (#11039)

1 of 1 new or added line in 1 file covered. (100.0%)

80214 of 168196 relevant lines covered (47.69%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/iotdb-client/cli/src/main/java/org/apache/iotdb/cli/AbstractCli.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.iotdb.cli;
21

22
import org.apache.iotdb.cli.utils.IoTPrinter;
23
import org.apache.iotdb.exception.ArgsErrorException;
24
import org.apache.iotdb.jdbc.IoTDBConnection;
25
import org.apache.iotdb.jdbc.IoTDBJDBCResultSet;
26
import org.apache.iotdb.rpc.IoTDBConnectionException;
27
import org.apache.iotdb.rpc.RpcUtils;
28
import org.apache.iotdb.service.rpc.thrift.ServerProperties;
29
import org.apache.iotdb.tool.ImportCsv;
30

31
import org.apache.commons.cli.CommandLine;
32
import org.apache.commons.cli.Option;
33
import org.apache.commons.cli.Options;
34
import org.apache.commons.lang3.ArrayUtils;
35

36
import java.io.BufferedReader;
37
import java.io.IOException;
38
import java.io.InputStreamReader;
39
import java.sql.ResultSet;
40
import java.sql.ResultSetMetaData;
41
import java.sql.SQLException;
42
import java.sql.Statement;
43
import java.time.ZoneId;
44
import java.util.ArrayList;
45
import java.util.Arrays;
46
import java.util.HashSet;
47
import java.util.List;
48
import java.util.Set;
49

50
import static org.apache.iotdb.cli.utils.IoTPrinter.computeHANCount;
51
import static org.apache.iotdb.cli.utils.IoTPrinter.printBlockLine;
52
import static org.apache.iotdb.cli.utils.IoTPrinter.printCount;
53
import static org.apache.iotdb.cli.utils.IoTPrinter.printRow;
54
import static org.apache.iotdb.cli.utils.IoTPrinter.println;
55

56
public abstract class AbstractCli {
×
57

58
  static final String HOST_ARGS = "h";
59
  static final String HOST_NAME = "host";
60

61
  static final String HELP_ARGS = "help";
62

63
  static final String PORT_ARGS = "p";
64
  static final String PORT_NAME = "port";
65

66
  static final String PW_ARGS = "pw";
67
  private static final String PW_NAME = "password";
68

69
  static final String USERNAME_ARGS = "u";
70
  static final String USERNAME_NAME = "username";
71

72
  private static final String EXECUTE_ARGS = "e";
73
  private static final String EXECUTE_NAME = "execute";
74
  private static final String NULL = "null";
75

76
  static final int CODE_OK = 0;
77
  static final int CODE_ERROR = 1;
78

79
  static final String ISO8601_ARGS = "disableISO8601";
80
  static final List<String> AGGREGRATE_TIME_LIST = new ArrayList<>();
×
81
  static final String RPC_COMPRESS_ARGS = "c";
82
  private static final String RPC_COMPRESS_NAME = "rpcCompressed";
83
  static final String TIMEOUT_ARGS = "timeout";
84
  private static final String TIMEOUT_NAME = "queryTimeout";
85
  static final String SET_TIMESTAMP_DISPLAY = "set time_display_type";
86
  static final String SHOW_TIMESTAMP_DISPLAY = "show time_display_type";
87
  static final String SET_TIME_ZONE = "set time_zone";
88
  static final String SHOW_TIMEZONE = "show time_zone";
89
  static final String SET_FETCH_SIZE = "set fetch_size";
90
  static final String SHOW_FETCH_SIZE = "show fetch_size";
91
  private static final String HELP = "help";
92
  static final String IOTDB_CLI_PREFIX = "IoTDB";
93
  static final String SCRIPT_HINT = "./start-cli.sh(start-cli.bat if Windows)";
94
  static final String QUIT_COMMAND = "quit";
95
  static final String EXIT_COMMAND = "exit";
96
  private static final String SHOW_METADATA_COMMAND = "show timeseries";
97
  static final int MAX_HELP_CONSOLE_WIDTH = 88;
98
  static final String TIMESTAMP_STR = "Time";
99
  private static final String IMPORT_CMD = "import";
100
  static int maxPrintRowCount = 1000;
×
101
  private static int fetchSize = 1000;
×
102
  static int queryTimeout = 0;
×
103
  static String timestampPrecision = "ms";
×
104
  static String timeFormat = RpcUtils.DEFAULT_TIME_FORMAT;
×
105
  private static boolean continuePrint = false;
×
106

107
  private static int lineCount = 0;
×
108
  private static final String SUCCESS_MESSAGE = "The statement is executed successfully.";
109

110
  private static boolean isReachEnd = false;
×
111

112
  static String host = "127.0.0.1";
×
113
  static String port = "6667";
×
114
  static String username;
115
  static String password;
116
  static String execute;
117
  static boolean hasExecuteSQL = false;
×
118

119
  static Set<String> keywordSet = new HashSet<>();
×
120

121
  static ServerProperties properties = null;
×
122

123
  private static boolean cursorBeforeFirst = true;
×
124

125
  static int lastProcessStatus = CODE_OK;
×
126

127
  static void init() {
128
    keywordSet.add("-" + HOST_ARGS);
×
129
    keywordSet.add("-" + HELP_ARGS);
×
130
    keywordSet.add("-" + PORT_ARGS);
×
131
    keywordSet.add("-" + PW_ARGS);
×
132
    keywordSet.add("-" + USERNAME_ARGS);
×
133
    keywordSet.add("-" + EXECUTE_ARGS);
×
134
    keywordSet.add("-" + ISO8601_ARGS);
×
135
    keywordSet.add("-" + RPC_COMPRESS_ARGS);
×
136
  }
×
137

138
  static Options createOptions() {
139
    Options options = new Options();
×
140
    Option help = new Option(HELP_ARGS, false, "Display help information(optional)");
×
141
    help.setRequired(false);
×
142
    options.addOption(help);
×
143

144
    Option timeFormat = new Option(ISO8601_ARGS, false, "Display timestamp in number(optional)");
×
145
    timeFormat.setRequired(false);
×
146
    options.addOption(timeFormat);
×
147

148
    Option host =
×
149
        Option.builder(HOST_ARGS)
×
150
            .argName(HOST_NAME)
×
151
            .hasArg()
×
152
            .desc("Host Name (optional, default 127.0.0.1)")
×
153
            .build();
×
154
    options.addOption(host);
×
155

156
    Option port =
×
157
        Option.builder(PORT_ARGS)
×
158
            .argName(PORT_NAME)
×
159
            .hasArg()
×
160
            .desc("Port (optional, default 6667)")
×
161
            .build();
×
162
    options.addOption(port);
×
163

164
    Option username =
×
165
        Option.builder(USERNAME_ARGS)
×
166
            .argName(USERNAME_NAME)
×
167
            .hasArg()
×
168
            .desc("User name (required)")
×
169
            .required()
×
170
            .build();
×
171
    options.addOption(username);
×
172

173
    Option password =
×
174
        Option.builder(PW_ARGS).argName(PW_NAME).hasArg().desc("password (optional)").build();
×
175
    options.addOption(password);
×
176

177
    Option execute =
×
178
        Option.builder(EXECUTE_ARGS)
×
179
            .argName(EXECUTE_NAME)
×
180
            .hasArg()
×
181
            .desc("execute statement (optional)")
×
182
            .build();
×
183
    options.addOption(execute);
×
184

185
    Option isRpcCompressed =
×
186
        Option.builder(RPC_COMPRESS_ARGS)
×
187
            .argName(RPC_COMPRESS_NAME)
×
188
            .desc("Rpc Compression enabled or not")
×
189
            .build();
×
190
    options.addOption(isRpcCompressed);
×
191

192
    Option queryTimeout =
×
193
        Option.builder(TIMEOUT_ARGS)
×
194
            .argName(TIMEOUT_NAME)
×
195
            .hasArg()
×
196
            .desc(
×
197
                "The timeout in second. "
198
                    + "Using the configuration of server if it's not set (optional)")
199
            .build();
×
200
    options.addOption(queryTimeout);
×
201
    return options;
×
202
  }
203

204
  static String checkRequiredArg(
205
      String arg, String name, CommandLine commandLine, boolean isRequired, String defaultValue)
206
      throws ArgsErrorException {
207
    String str = commandLine.getOptionValue(arg);
×
208
    if (str == null) {
×
209
      if (isRequired) {
×
210
        String msg =
×
211
            String.format(
×
212
                "%s: Required values for option '%s' not provided", IOTDB_CLI_PREFIX, name);
213
        println(msg);
×
214
        println("Use -help for more information");
×
215
        throw new ArgsErrorException(msg);
×
216
      } else if (defaultValue == null) {
×
217
        String msg =
×
218
            String.format("%s: Required values for option '%s' is null.", IOTDB_CLI_PREFIX, name);
×
219
        throw new ArgsErrorException(msg);
×
220
      } else {
221
        return defaultValue;
×
222
      }
223
    }
224
    return str;
×
225
  }
226

227
  private static void setFetchSize(String fetchSizeString) {
228
    long tmp = Long.parseLong(fetchSizeString.trim());
×
229
    if (tmp > Integer.MAX_VALUE || tmp < 0) {
×
230
      fetchSize = Integer.MAX_VALUE;
×
231
    } else {
232
      fetchSize = Integer.parseInt(fetchSizeString.trim());
×
233
    }
234
  }
×
235

236
  private static int setFetchSize(String specialCmd, String cmd) {
237
    String[] values = specialCmd.split("=");
×
238
    if (values.length != 2) {
×
239
      println(String.format("Fetch size format error, please input like %s=10000", SET_FETCH_SIZE));
×
240
      return CODE_ERROR;
×
241
    }
242
    try {
243
      setFetchSize(cmd.split("=")[1]);
×
244
    } catch (Exception e) {
×
245
      println(String.format("Fetch size format error, %s", e.getMessage()));
×
246
      return CODE_ERROR;
×
247
    }
×
248
    println("Fetch size has set to " + values[1].trim());
×
249
    return CODE_OK;
×
250
  }
251

252
  static void setQueryTimeout(String timeoutString) {
253
    long timeout = Long.parseLong(timeoutString.trim());
×
254
    if (timeout > Integer.MAX_VALUE || timeout < 0) {
×
255
      queryTimeout = 0;
×
256
    } else {
257
      queryTimeout = Integer.parseInt(timeoutString.trim());
×
258
    }
259
  }
×
260

261
  static String[] removePasswordArgs(String[] args) {
262
    int index = -1;
×
263
    for (int i = 0; i < args.length; i++) {
×
264
      if (args[i].equals("-" + PW_ARGS)) {
×
265
        index = i;
×
266
        break;
×
267
      }
268
    }
269
    if (index >= 0
×
270
        && ((index + 1 >= args.length)
271
            || (index + 1 < args.length && keywordSet.contains(args[index + 1])))) {
×
272
      return ArrayUtils.remove(args, index);
×
273
    }
274
    return args;
×
275
  }
276

277
  @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning
278
  static String[] processExecuteArgs(String[] args) {
279
    int index = -1;
×
280
    for (int i = 0; i < args.length; i++) {
×
281
      if (args[i].equals("-" + EXECUTE_ARGS)) {
×
282
        index = i;
×
283
        break;
×
284
      }
285
    }
286
    if (index >= 0
×
287
        && ((index + 1 >= args.length)
288
            || (index + 1 < args.length && keywordSet.contains(args[index + 1])))) {
×
289
      return ArrayUtils.remove(args, index);
×
290
    } else if (index == -1) {
×
291
      return args;
×
292
    } else {
293
      StringBuilder executeCommand = new StringBuilder();
×
294
      for (int j = index + 1; j < args.length; j++) {
×
295
        executeCommand.append(args[j]).append(" ");
×
296
      }
297
      // remove last space
298
      executeCommand.deleteCharAt(executeCommand.length() - 1);
×
299
      // some bashes may not remove quotes of parameters automatically, remove them in that case
300
      if (executeCommand.charAt(0) == '\'' || executeCommand.charAt(0) == '\"') {
×
301
        executeCommand.deleteCharAt(0);
×
302
        if (executeCommand.charAt(executeCommand.length() - 1) == '\''
×
303
            || executeCommand.charAt(executeCommand.length() - 1) == '\"') {
×
304
          executeCommand.deleteCharAt(executeCommand.length() - 1);
×
305
        }
306
      }
307

308
      execute = executeCommand.toString();
×
309
      hasExecuteSQL = true;
×
310
      // When execute sql in CLI with -e mode, we should print all results by setting continuePrint
311
      // is true.
312
      continuePrint = true;
×
313
      args = Arrays.copyOfRange(args, 0, index);
×
314
      return args;
×
315
    }
316
  }
317

318
  static void displayLogo(String logo, String version, String buildInfo) {
319
    println(
×
320
        (logo != null
×
321
                ? logo
×
322
                : (" _____       _________  ______   ______    \n"
×
323
                    + "|_   _|     |  _   _  ||_   _ `.|_   _ \\   \n"
324
                    + "  | |   .--.|_/ | | \\_|  | | `. \\ | |_) |  \n"
325
                    + "  | | / .'`\\ \\  | |      | |  | | |  __'.  \n"
326
                    + " _| |_| \\__. | _| |_    _| |_.' /_| |__) | \n"
327
                    + "|_____|'.__.' |_____|  |______.'|_______/  "))
328
            + "version "
329
            + version
330
            + " (Build: "
331
            + (buildInfo != null ? buildInfo : "UNKNOWN")
×
332
            + ")"
333
            + "\n"
334
            + "                                           \n");
335
  }
×
336

337
  static void echoStarting() {
338
    println("---------------------");
×
339
    println("Starting IoTDB Cli");
×
340
    println("---------------------");
×
341
  }
×
342

343
  static OperationResult handleInputCmd(String cmd, IoTDBConnection connection) {
344
    lastProcessStatus = CODE_OK;
×
345
    String specialCmd = cmd.toLowerCase().trim();
×
346

347
    if (QUIT_COMMAND.equals(specialCmd) || EXIT_COMMAND.equals(specialCmd)) {
×
348
      return OperationResult.STOP_OPER;
×
349
    }
350
    if (HELP.equals(specialCmd)) {
×
351
      showHelp();
×
352
      return OperationResult.CONTINUE_OPER;
×
353
    }
354
    if (specialCmd.startsWith(SET_TIMESTAMP_DISPLAY)) {
×
355
      lastProcessStatus = setTimestampDisplay(specialCmd, cmd);
×
356
      return OperationResult.CONTINUE_OPER;
×
357
    }
358

359
    if (specialCmd.startsWith(SET_TIME_ZONE)) {
×
360
      lastProcessStatus = setTimeZone(specialCmd, cmd, connection);
×
361
      return OperationResult.CONTINUE_OPER;
×
362
    }
363

364
    if (specialCmd.startsWith(SET_FETCH_SIZE)) {
×
365
      lastProcessStatus = setFetchSize(specialCmd, cmd);
×
366
      return OperationResult.CONTINUE_OPER;
×
367
    }
368

369
    if (specialCmd.startsWith(SHOW_TIMEZONE)) {
×
370
      lastProcessStatus = showTimeZone(connection);
×
371
      return OperationResult.CONTINUE_OPER;
×
372
    }
373
    if (specialCmd.startsWith(SHOW_TIMESTAMP_DISPLAY)) {
×
374
      println("Current time format: " + timeFormat);
×
375
      return OperationResult.CONTINUE_OPER;
×
376
    }
377
    if (specialCmd.startsWith(SHOW_FETCH_SIZE)) {
×
378
      println("Current fetch size: " + fetchSize);
×
379
      return OperationResult.CONTINUE_OPER;
×
380
    }
381

382
    if (specialCmd.startsWith(IMPORT_CMD)) {
×
383
      lastProcessStatus = importCmd(specialCmd, cmd, connection);
×
384
      return OperationResult.CONTINUE_OPER;
×
385
    }
386

387
    lastProcessStatus = executeQuery(connection, cmd);
×
388
    return OperationResult.NO_OPER;
×
389
  }
390

391
  private static void showHelp() {
392
    println("    <your-sql>\t\t\t execute your sql statment");
×
393
    println(
×
394
        String.format("    %s\t\t show how many timeseries are in iotdb", SHOW_METADATA_COMMAND));
×
395
    println(
×
396
        String.format(
×
397
            "    %s=xxx\t eg. long, default, ISO8601, yyyy-MM-dd HH:mm:ss.",
398
            SET_TIMESTAMP_DISPLAY));
399
    println(String.format("    %s\t show time display type", SHOW_TIMESTAMP_DISPLAY));
×
400
    println(String.format("    %s=xxx\t\t eg. +08:00, Asia/Shanghai.", SET_TIME_ZONE));
×
401
    println(String.format("    %s\t\t show cli time zone", SHOW_TIMEZONE));
×
402
    println(
×
403
        String.format(
×
404
            "    %s=xxx\t\t set fetch size when querying data from server.", SET_FETCH_SIZE));
405
    println(String.format("    %s\t\t show fetch size", SHOW_FETCH_SIZE));
×
406
  }
×
407

408
  private static int setTimestampDisplay(String specialCmd, String cmd) {
409
    String[] values = specialCmd.split("=");
×
410
    if (values.length != 2) {
×
411
      println(
×
412
          String.format(
×
413
              "Time display format error, please input like %s=ISO8601", SET_TIMESTAMP_DISPLAY));
414
      return CODE_ERROR;
×
415
    }
416
    try {
417
      timeFormat = RpcUtils.setTimeFormat(cmd.split("=")[1]);
×
418
    } catch (Exception e) {
×
419
      println(String.format("time display format error, %s", e.getMessage()));
×
420
      return CODE_ERROR;
×
421
    }
×
422
    println("Time display type has set to " + cmd.split("=")[1].trim());
×
423
    return CODE_OK;
×
424
  }
425

426
  /**
427
   * if cli has not specified a zoneId, it will be set to cli's system timezone by default otherwise
428
   * for insert and query accuracy cli should set timezone the same for all sessions.
429
   *
430
   * @param specialCmd
431
   * @param cmd
432
   * @param connection
433
   * @return execute result code
434
   */
435
  private static int setTimeZone(String specialCmd, String cmd, IoTDBConnection connection) {
436
    String[] values = specialCmd.split("=");
×
437
    if (values.length != 2) {
×
438
      println(String.format("Time zone format error, please input like %s=+08:00", SET_TIME_ZONE));
×
439
      return CODE_ERROR;
×
440
    }
441
    try {
442
      connection.setTimeZone(cmd.split("=")[1].trim());
×
443
    } catch (Exception e) {
×
444
      println(String.format("Time zone format error: %s", e.getMessage()));
×
445
      return CODE_ERROR;
×
446
    }
×
447
    println("Time zone has set to " + values[1].trim());
×
448
    return CODE_OK;
×
449
  }
450

451
  private static int showTimeZone(IoTDBConnection connection) {
452
    try {
453
      println("Current time zone: " + connection.getTimeZone());
×
454
    } catch (Exception e) {
×
455
      println("Cannot get time zone from server side because: " + e.getMessage());
×
456
      return CODE_ERROR;
×
457
    }
×
458
    return CODE_OK;
×
459
  }
460

461
  private static int importCmd(String specialCmd, String cmd, IoTDBConnection connection) {
462
    String[] values = specialCmd.split(" ");
×
463
    if (values.length != 2) {
×
464
      println(
×
465
          "Please input like: import /User/myfile. "
466
              + "Noted that your file path cannot contain any space character)");
467
      return CODE_ERROR;
×
468
    }
469
    println(cmd.split(" ")[1]);
×
470
    try {
471
      return ImportCsv.importFromTargetPath(
×
472
          host,
473
          Integer.parseInt(port),
×
474
          username,
475
          password,
476
          cmd.split(" ")[1],
×
477
          connection.getTimeZone());
×
478
    } catch (IoTDBConnectionException e) {
×
479
      IoTPrinter.printException(e);
×
480
      return CODE_ERROR;
×
481
    }
482
  }
483

484
  @SuppressWarnings({"squid:S3776"}) // Suppress high Cognitive Complexity warning
485
  private static int executeQuery(IoTDBConnection connection, String cmd) {
486
    int executeStatus = CODE_OK;
×
487
    long startTime = System.currentTimeMillis();
×
488
    try (Statement statement = connection.createStatement()) {
×
489
      ZoneId zoneId = ZoneId.of(connection.getTimeZone());
×
490
      statement.setFetchSize(fetchSize);
×
491
      boolean hasResultSet = statement.execute(cmd.trim());
×
492
      if (hasResultSet) {
×
493
        // print the result
494
        try (ResultSet resultSet = statement.getResultSet()) {
×
495
          ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
×
496
          int columnLength = resultSetMetaData.getColumnCount();
×
497
          List<Integer> maxSizeList = new ArrayList<>(columnLength);
×
498
          List<List<String>> lists =
×
499
              cacheResult(resultSet, maxSizeList, columnLength, resultSetMetaData, zoneId);
×
500
          output(lists, maxSizeList);
×
501
          long costTime = System.currentTimeMillis() - startTime;
×
502
          println(String.format("It costs %.3fs", costTime / 1000.0));
×
503
          while (!isReachEnd) {
×
504
            if (continuePrint) {
×
505
              maxSizeList = new ArrayList<>(columnLength);
×
506
              lists = cacheResult(resultSet, maxSizeList, columnLength, resultSetMetaData, zoneId);
×
507
              output(lists, maxSizeList);
×
508
              continue;
×
509
            }
510
            println("This display 1000 rows. Press ENTER to show more, input 'q' to quit.");
×
511
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
×
512
            try {
513
              if ("".equals(br.readLine())) {
×
514
                maxSizeList = new ArrayList<>(columnLength);
×
515
                lists =
×
516
                    cacheResult(resultSet, maxSizeList, columnLength, resultSetMetaData, zoneId);
×
517
                output(lists, maxSizeList);
×
518
              } else {
519
                break;
×
520
              }
521
            } catch (IOException e) {
×
522
              IoTPrinter.printException(e);
×
523
              executeStatus = CODE_ERROR;
×
524
            }
×
525
          }
×
526
          // output tracing activity
527
          if (((IoTDBJDBCResultSet) resultSet).isSetTracingInfo()) {
×
528
            maxSizeList = new ArrayList<>(2);
×
529
            lists = cacheTracingInfo(resultSet, maxSizeList);
×
530
            outputTracingInfo(lists, maxSizeList);
×
531
          }
532
        }
533
      } else {
534
        println("Msg: " + SUCCESS_MESSAGE);
×
535
      }
536
    } catch (Exception e) {
×
537
      println("Msg: " + e.getMessage());
×
538
      executeStatus = CODE_ERROR;
×
539
    } finally {
540
      resetArgs();
×
541
    }
542
    return executeStatus;
×
543
  }
544

545
  /**
546
   * cache all results.
547
   *
548
   * @param resultSet jdbc resultSet
549
   * @param maxSizeList the longest result of every column
550
   * @param columnCount the number of column
551
   * @param resultSetMetaData jdbc resultSetMetaData
552
   * @param zoneId your time zone
553
   * @return {@literal List<List<String>> result}
554
   * @throws SQLException throw exception
555
   */
556
  @SuppressWarnings({"squid:S6541", "squid:S3776"}) // Suppress high Cognitive Complexity warning
557
  // Methods should not perform too many tasks (aka Brain method)
558
  private static List<List<String>> cacheResult(
559
      ResultSet resultSet,
560
      List<Integer> maxSizeList,
561
      int columnCount,
562
      ResultSetMetaData resultSetMetaData,
563
      ZoneId zoneId)
564
      throws SQLException {
565

566
    int j = 0;
×
567
    if (cursorBeforeFirst) {
×
568
      isReachEnd = !resultSet.next();
×
569
      cursorBeforeFirst = false;
×
570
    }
571

572
    List<List<String>> lists = new ArrayList<>(columnCount);
×
573
    int endTimeIndex = -1;
×
574
    if (resultSet instanceof IoTDBJDBCResultSet) {
×
575
      for (int i = 1; i <= columnCount; i++) {
×
576
        List<String> list = new ArrayList<>(maxPrintRowCount + 1);
×
577
        String columnLabel = resultSetMetaData.getColumnLabel(i);
×
578
        if (columnLabel.equalsIgnoreCase("__endTime")) {
×
579
          endTimeIndex = i;
×
580
        }
581
        list.add(columnLabel);
×
582
        lists.add(list);
×
583
        int count = computeHANCount(columnLabel);
×
584
        maxSizeList.add(columnLabel.length() + count);
×
585
      }
586

587
      boolean printTimestamp = !((IoTDBJDBCResultSet) resultSet).isIgnoreTimeStamp();
×
588
      while (j < maxPrintRowCount && !isReachEnd) {
×
589
        for (int i = 1; i <= columnCount; i++) {
×
590
          String tmp;
591
          if (printTimestamp && i == 1) {
×
592
            tmp =
×
593
                RpcUtils.formatDatetime(
×
594
                    timeFormat, timestampPrecision, resultSet.getLong(TIMESTAMP_STR), zoneId);
×
595
          } else if (endTimeIndex == i) {
×
596
            tmp =
×
597
                RpcUtils.formatDatetime(
×
598
                    timeFormat, timestampPrecision, resultSet.getLong(i), zoneId);
×
599
          } else {
600
            tmp = resultSet.getString(i);
×
601
          }
602
          if (tmp == null) {
×
603
            tmp = NULL;
×
604
          }
605
          lists.get(i - 1).add(tmp);
×
606
          int count = computeHANCount(tmp);
×
607
          int realLength = tmp.length() + count;
×
608
          if (maxSizeList.get(i - 1) < realLength) {
×
609
            maxSizeList.set(i - 1, realLength);
×
610
          }
611
        }
612
        j++;
×
613
        isReachEnd = !resultSet.next();
×
614
      }
615
      return lists;
×
616
    }
617

618
    for (int i = 1; i <= columnCount; i += 2) {
×
619
      List<String> timeList = new ArrayList<>(maxPrintRowCount + 1);
×
620
      timeList.add(resultSetMetaData.getColumnLabel(i).substring(0, TIMESTAMP_STR.length()));
×
621
      lists.add(timeList);
×
622
      List<String> valueList = new ArrayList<>(maxPrintRowCount + 1);
×
623
      valueList.add(resultSetMetaData.getColumnLabel(i + 1));
×
624
      lists.add(valueList);
×
625
      maxSizeList.add(TIMESTAMP_STR.length());
×
626
      maxSizeList.add(resultSetMetaData.getColumnLabel(i + 1).length());
×
627
    }
628

629
    while (j < maxPrintRowCount && !isReachEnd) {
×
630
      for (int i = 1; i <= columnCount; i++) {
×
631
        String tmp = resultSet.getString(i);
×
632
        if (tmp == null) {
×
633
          tmp = NULL;
×
634
        }
635
        if (i % 2 != 0 && !tmp.equals(NULL)) {
×
636
          tmp =
×
637
              RpcUtils.formatDatetime(timeFormat, timestampPrecision, Long.parseLong(tmp), zoneId);
×
638
        }
639
        lists.get(i - 1).add(tmp);
×
640
        if (maxSizeList.get(i - 1) < tmp.length()) {
×
641
          maxSizeList.set(i - 1, tmp.length());
×
642
        }
643
      }
644
      j++;
×
645
      isReachEnd = !resultSet.next();
×
646
    }
647
    return lists;
×
648
  }
649

650
  private static List<List<String>> cacheTracingInfo(ResultSet resultSet, List<Integer> maxSizeList)
651
      throws Exception {
652
    List<List<String>> lists = new ArrayList<>(2);
×
653
    lists.add(0, new ArrayList<>());
×
654
    lists.add(1, new ArrayList<>());
×
655

656
    String activityStr = "Activity";
×
657
    String elapsedTimeStr = "Elapsed Time";
×
658
    lists.get(0).add(activityStr);
×
659
    lists.get(1).add(elapsedTimeStr);
×
660
    maxSizeList.add(0, activityStr.length());
×
661
    maxSizeList.add(1, elapsedTimeStr.length());
×
662

663
    List<String> activityList = ((IoTDBJDBCResultSet) resultSet).getActivityList();
×
664
    List<Long> elapsedTimeList = ((IoTDBJDBCResultSet) resultSet).getElapsedTimeList();
×
665
    String[] statisticsInfoList = {
×
666
      "seriesPathNum", "seqFileNum", "unSeqFileNum", "seqChunkInfo", "unSeqChunkInfo", "pageNumInfo"
667
    };
668

669
    for (int i = 0; i < activityList.size(); i++) {
×
670

671
      if (i == activityList.size() - 1) {
×
672
        // cache Statistics
673
        for (String infoName : statisticsInfoList) {
×
674
          String info = ((IoTDBJDBCResultSet) resultSet).getStatisticsInfoByName(infoName);
×
675
          lists.get(0).add(info);
×
676
          lists.get(1).add("");
×
677
          if (info.length() > maxSizeList.get(0)) {
×
678
            maxSizeList.set(0, info.length());
×
679
          }
680
        }
681
      }
682

683
      String activity = activityList.get(i);
×
684
      String elapsedTime = elapsedTimeList.get(i).toString();
×
685
      if (activity.length() > maxSizeList.get(0)) {
×
686
        maxSizeList.set(0, activity.length());
×
687
      }
688
      if (elapsedTime.length() > maxSizeList.get(1)) {
×
689
        maxSizeList.set(1, elapsedTime.length());
×
690
      }
691
      lists.get(0).add(activity);
×
692
      lists.get(1).add(elapsedTime);
×
693
    }
694

695
    return lists;
×
696
  }
697

698
  private static void output(List<List<String>> lists, List<Integer> maxSizeList) {
699
    printBlockLine(maxSizeList);
×
700
    printRow(lists, 0, maxSizeList);
×
701
    printBlockLine(maxSizeList);
×
702
    for (int i = 1; i < lists.get(0).size(); i++) {
×
703
      printRow(lists, i, maxSizeList);
×
704
    }
705
    printBlockLine(maxSizeList);
×
706
    if (isReachEnd) {
×
707
      lineCount += lists.get(0).size() - 1;
×
708
      printCount(lineCount);
×
709
    } else {
710
      lineCount += maxPrintRowCount;
×
711
    }
712
  }
×
713

714
  private static void outputTracingInfo(List<List<String>> lists, List<Integer> maxSizeList) {
715
    println();
×
716
    println("Tracing Activties:");
×
717
    printBlockLine(maxSizeList);
×
718
    printRow(lists, 0, maxSizeList);
×
719
    printBlockLine(maxSizeList);
×
720
    for (int i = 1; i < lists.get(0).size(); i++) {
×
721
      printRow(lists, i, maxSizeList);
×
722
    }
723
    printBlockLine(maxSizeList);
×
724
  }
×
725

726
  private static void resetArgs() {
727
    lineCount = 0;
×
728
    cursorBeforeFirst = true;
×
729
    isReachEnd = false;
×
730
  }
×
731

732
  enum OperationResult {
×
733
    STOP_OPER,
×
734
    CONTINUE_OPER,
×
735
    NO_OPER
×
736
  }
737

738
  static boolean processCommand(String s, IoTDBConnection connection) {
739
    if (s == null) {
×
740
      return true;
×
741
    }
742
    String[] cmds = s.trim().split(";");
×
743
    for (String cmd : cmds) {
×
744
      if (cmd != null && !"".equals(cmd.trim())) {
×
745
        OperationResult result = handleInputCmd(cmd, connection);
×
746
        switch (result) {
×
747
          case STOP_OPER:
748
            return false;
×
749
          case CONTINUE_OPER:
750
            continue;
×
751
          default:
752
            break;
753
        }
754
      }
755
    }
756
    return true;
×
757
  }
758
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc