• 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/Cli.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.JlineUtils;
23
import org.apache.iotdb.exception.ArgsErrorException;
24
import org.apache.iotdb.jdbc.Config;
25
import org.apache.iotdb.jdbc.IoTDBConnection;
26
import org.apache.iotdb.rpc.RpcUtils;
27

28
import org.apache.commons.cli.CommandLine;
29
import org.apache.commons.cli.CommandLineParser;
30
import org.apache.commons.cli.DefaultParser;
31
import org.apache.commons.cli.HelpFormatter;
32
import org.apache.commons.cli.Options;
33
import org.apache.commons.cli.ParseException;
34
import org.apache.thrift.TException;
35
import org.jline.reader.EndOfFileException;
36
import org.jline.reader.LineReader;
37
import org.jline.reader.UserInterruptException;
38

39
import java.io.IOException;
40
import java.sql.DriverManager;
41
import java.sql.SQLException;
42

43
import static org.apache.iotdb.cli.utils.IoTPrinter.println;
44
import static org.apache.iotdb.jdbc.Config.IOTDB_ERROR_PREFIX;
45

46
/** args[]: -h 127.0.0.1 -p 6667 -u root -pw root */
47
public class Cli extends AbstractCli {
×
48

49
  private static CommandLine commandLine;
50
  private static LineReader lineReader;
51

52
  /**
53
   * IoTDB Client main function.
54
   *
55
   * @param args launch arguments
56
   * @throws ClassNotFoundException ClassNotFoundException
57
   */
58
  public static void main(String[] args) throws ClassNotFoundException, IOException {
59
    Class.forName(Config.JDBC_DRIVER_NAME);
×
60
    Options options = createOptions();
×
61
    HelpFormatter hf = new HelpFormatter();
×
62
    hf.setWidth(MAX_HELP_CONSOLE_WIDTH);
×
63
    commandLine = null;
×
64

65
    if (args == null || args.length == 0) {
×
66
      println(
×
67
          "Require more params input, eg. ./start-cli.sh(start-cli.bat if Windows) "
68
              + "-h xxx.xxx.xxx.xxx -p xxxx -u xxx.");
69
      println("For more information, please check the following hint.");
×
70
      hf.printHelp(SCRIPT_HINT, options, true);
×
71
      System.exit(CODE_ERROR);
×
72
    }
73
    init();
×
74
    String[] newArgs = removePasswordArgs(args);
×
75
    String[] newArgs2 = processExecuteArgs(newArgs);
×
76
    boolean continues = parseCommandLine(options, newArgs2, hf);
×
77
    if (!continues) {
×
78
      System.exit(CODE_ERROR);
×
79
    }
80

81
    try {
82
      host = checkRequiredArg(HOST_ARGS, HOST_NAME, commandLine, false, host);
×
83
      port = checkRequiredArg(PORT_ARGS, PORT_NAME, commandLine, false, port);
×
84
      username = checkRequiredArg(USERNAME_ARGS, USERNAME_NAME, commandLine, true, null);
×
85
    } catch (ArgsErrorException e) {
×
86
      println(IOTDB_ERROR_PREFIX + "Input params error because" + e.getMessage());
×
87
      System.exit(CODE_ERROR);
×
88
    } catch (Exception e) {
×
89
      println(IOTDB_ERROR_PREFIX + "Exit cli with error " + e.getMessage());
×
90
      System.exit(CODE_ERROR);
×
91
    }
×
92

93
    lineReader = JlineUtils.getLineReader(username, host, port);
×
94
    serve();
×
95
  }
×
96

97
  private static boolean parseCommandLine(Options options, String[] newArgs, HelpFormatter hf) {
98
    try {
99
      CommandLineParser parser = new DefaultParser();
×
100
      commandLine = parser.parse(options, newArgs);
×
101
      if (commandLine.hasOption(HELP_ARGS)) {
×
102
        hf.printHelp(SCRIPT_HINT, options, true);
×
103
        return false;
×
104
      }
105
      if (commandLine.hasOption(RPC_COMPRESS_ARGS)) {
×
106
        Config.rpcThriftCompressionEnable = true;
×
107
      }
108
      if (commandLine.hasOption(ISO8601_ARGS)) {
×
109
        timeFormat = RpcUtils.setTimeFormat("long");
×
110
      }
111
      if (commandLine.hasOption(TIMEOUT_ARGS)) {
×
112
        setQueryTimeout(commandLine.getOptionValue(TIMEOUT_ARGS));
×
113
      }
114
    } catch (ParseException e) {
×
115
      println(
×
116
          "Require more params input, eg. ./start-cli.sh(start-cli.bat if Windows) "
117
              + "-h xxx.xxx.xxx.xxx -p xxxx -u xxx.");
118
      println("For more information, please check the following hint.");
×
119
      hf.printHelp(IOTDB_CLI_PREFIX, options, true);
×
120
      return false;
×
121
    } catch (NumberFormatException e) {
×
122
      println(
×
123
          IOTDB_ERROR_PREFIX
124
              + ": error format of max print row count, it should be an integer number");
125
      return false;
×
126
    }
×
127
    return true;
×
128
  }
129

130
  private static void serve() {
131
    try {
132
      password = commandLine.getOptionValue(PW_ARGS);
×
133
      if (hasExecuteSQL && password != null) {
×
134
        executeSql();
×
135
      }
136
      if (password == null) {
×
137
        password = lineReader.readLine("please input your password:", '\0');
×
138
      }
139
      receiveCommands(lineReader);
×
140
    } catch (Exception e) {
×
141
      println(IOTDB_ERROR_PREFIX + ": Exit cli with error: " + e.getMessage());
×
142
      System.exit(CODE_ERROR);
×
143
    }
×
144
  }
×
145

146
  private static void executeSql() throws TException {
147
    try (IoTDBConnection connection =
×
148
        (IoTDBConnection)
149
            DriverManager.getConnection(
×
150
                Config.IOTDB_URL_PREFIX + host + ":" + port + "/", username, password)) {
151
      connection.setQueryTimeout(queryTimeout);
×
152
      properties = connection.getServerProperties();
×
153
      timestampPrecision = properties.getTimestampPrecision();
×
154
      AGGREGRATE_TIME_LIST.addAll(properties.getSupportedTimeAggregationOperations());
×
155
      processCommand(execute, connection);
×
156
      System.exit(lastProcessStatus);
×
157
    } catch (SQLException e) {
×
158
      println(IOTDB_ERROR_PREFIX + "Can't execute sql because" + e.getMessage());
×
159
      System.exit(CODE_ERROR);
×
160
    }
×
161
  }
×
162

163
  private static void receiveCommands(LineReader reader) throws TException {
164
    try (IoTDBConnection connection =
×
165
        (IoTDBConnection)
166
            DriverManager.getConnection(
×
167
                Config.IOTDB_URL_PREFIX + host + ":" + port + "/", username, password)) {
168
      connection.setQueryTimeout(queryTimeout);
×
169
      properties = connection.getServerProperties();
×
170
      AGGREGRATE_TIME_LIST.addAll(properties.getSupportedTimeAggregationOperations());
×
171
      timestampPrecision = properties.getTimestampPrecision();
×
172

173
      echoStarting();
×
174
      displayLogo(properties.getLogo(), properties.getVersion(), properties.getBuildInfo());
×
175
      println(String.format("Successfully login at %s:%s", host, port));
×
176
      while (true) {
177
        boolean readLine = readerReadLine(reader, connection);
×
178
        if (readLine) {
×
179
          break;
×
180
        }
181
      }
×
182
    } catch (SQLException e) {
×
183
      println(
×
184
          String.format(
×
185
              "%s: %s Host is %s, port is %s.", IOTDB_ERROR_PREFIX, e.getMessage(), host, port));
×
186
      System.exit(CODE_ERROR);
×
187
    }
×
188
  }
×
189

190
  private static boolean readerReadLine(LineReader reader, IoTDBConnection connection) {
191
    String s;
192
    try {
193
      s = reader.readLine(IOTDB_CLI_PREFIX + "> ", null);
×
194
      boolean continues = processCommand(s, connection);
×
195
      if (!continues) {
×
196
        return true;
×
197
      }
198
    } catch (UserInterruptException e) {
×
199
      // Exit on signal INT requires confirmation.
200
      readLine(reader);
×
201
    } catch (EndOfFileException e) {
×
202
      // Exit on EOF (usually by pressing CTRL+D).
203
      System.exit(CODE_OK);
×
204
    }
×
205
    return false;
×
206
  }
207

208
  private static void readLine(LineReader reader) {
209
    try {
210
      reader.readLine("Press CTRL+C again to exit, or press ENTER to continue", '\0');
×
211
    } catch (UserInterruptException | EndOfFileException e2) {
×
212
      System.exit(CODE_OK);
×
213
    }
×
214
  }
×
215
}
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