/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/test/ybcmd.cc
Line | Count | Source (jump to first uncovered line) |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // This module is to help testing Ybcmd Parser manually. This application reads a statement from |
15 | | // stdin, parses it, and returns result or reports errors. |
16 | | // |
17 | | // To connect to an existing cluster at given masters' addresses: |
18 | | // |
19 | | // $ ybcmd --ybcmd_run=1 --ybcmd_master_addresses 127.0.0.1:7101,127.0.0.1:7102,127.0.0.1:7103 |
20 | | // ybcmd > create keyspace test; |
21 | | // ybcmd > use test; |
22 | | // ybcmd > create table t (c int primary key); |
23 | | // ybcmd > insert into t (c) values (1); |
24 | | // ybcmd > select * from t where c = 1; |
25 | | // ybcmd > exit; |
26 | | // |
27 | | // To start a simulated cluster of its own: |
28 | | // |
29 | | // $ ybcmd --ybcmd_run=1 |
30 | | // ... |
31 | | // ybcmd > ... |
32 | | // |
33 | | //-------------------------------------------------------------------------------------------------- |
34 | | |
35 | | #include <cstddef> |
36 | | |
37 | | #include "yb/client/client.h" |
38 | | #include "yb/client/meta_data_cache.h" |
39 | | |
40 | | #include "yb/util/result.h" |
41 | | #include "yb/util/status_log.h" |
42 | | |
43 | | #include "yb/yql/cql/ql/test/ql-test-base.h" |
44 | | |
45 | | using std::cout; |
46 | | using std::cin; |
47 | | using std::endl; |
48 | | using std::make_shared; |
49 | | using std::string; |
50 | | using yb::client::YBClientBuilder; |
51 | | |
52 | | DEFINE_bool(ybcmd_run, false, "Not to run this test unless instructed"); |
53 | | |
54 | | DEFINE_string(ybcmd_master_addresses, "", |
55 | | "Comma-separated addresses of the existing masters ybcmd to connect to. If unset, " |
56 | | "ybcmd will start a simulated cluster instead."); |
57 | | |
58 | | namespace yb { |
59 | | namespace ql { |
60 | | |
61 | | class TestQLCmd : public QLTestBase { |
62 | | public: |
63 | 0 | TestQLCmd() : QLTestBase() { |
64 | 0 | } |
65 | | |
66 | 0 | void ConnectCluster(const string& master_addresses) { |
67 | 0 | YBClientBuilder builder; |
68 | 0 | builder.add_master_server_addr(master_addresses); |
69 | 0 | builder.default_rpc_timeout(MonoDelta::FromSeconds(30)); |
70 | 0 | client_ = CHECK_RESULT(builder.Build()); |
71 | 0 | metadata_cache_ = std::make_shared<client::YBMetaDataCache>(client_.get(), false); |
72 | 0 | } |
73 | | }; |
74 | | |
75 | 0 | TEST_F(TestQLCmd, TestQLCmd) { |
76 | 0 | if (!FLAGS_ybcmd_run) { |
77 | 0 | return; |
78 | 0 | } |
79 | | |
80 | 0 | if (!FLAGS_ybcmd_master_addresses.empty()) { |
81 | | // Connect to external cluster. |
82 | 0 | ConnectCluster(FLAGS_ybcmd_master_addresses); |
83 | 0 | } else { |
84 | | // Init the simulated cluster. |
85 | 0 | ASSERT_NO_FATALS(CreateSimulatedCluster()); |
86 | 0 | } |
87 | | |
88 | | // Get a processor. |
89 | 0 | TestQLProcessor *processor = GetQLProcessor(); |
90 | |
|
91 | 0 | const string exit_cmd = "exit"; |
92 | 0 | while (!cin.eof()) { |
93 | | // Read the statement. |
94 | 0 | string stmt; |
95 | 0 | while (!cin.eof()) { |
96 | 0 | cout << endl << "\033[1;33mybcmd > \033[0m"; |
97 | |
|
98 | 0 | string sub_stmt; |
99 | 0 | getline(cin, sub_stmt); |
100 | 0 | stmt += sub_stmt; |
101 | |
|
102 | 0 | if (stmt.substr(0, 4) == exit_cmd && |
103 | 0 | (stmt[4] == '\0' || isspace(stmt[4]) || stmt[4] == ';')) { |
104 | 0 | return; |
105 | 0 | } |
106 | | |
107 | 0 | if (sub_stmt.find_first_of(";") != string::npos) { |
108 | 0 | break; |
109 | 0 | } |
110 | | |
111 | 0 | if (stmt.size() != 0) { |
112 | 0 | stmt += "\n"; |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | 0 | if (stmt.empty()) { |
117 | 0 | continue; |
118 | 0 | } |
119 | | |
120 | | // Execute. |
121 | 0 | cout << "\033[1;34mExecute statement: " << stmt << "\033[0m" << endl; |
122 | 0 | StatementParameters params; |
123 | 0 | do { |
124 | 0 | Status s = processor->Run(stmt, params); |
125 | 0 | if (!s.ok()) { |
126 | 0 | cout << s.ToString(false); |
127 | 0 | } else { |
128 | 0 | const ExecutedResult::SharedPtr& result = processor->result(); |
129 | 0 | if (result != nullptr) { |
130 | | // Check result. |
131 | 0 | switch (result->type()) { |
132 | 0 | case ExecutedResult::Type::SET_KEYSPACE: |
133 | 0 | cout << "Keyspace set to " |
134 | 0 | << static_cast<SetKeyspaceResult*>(result.get())->keyspace(); |
135 | 0 | break; |
136 | 0 | case ExecutedResult::Type::ROWS: { |
137 | 0 | RowsResult* rows_result = static_cast<RowsResult*>(result.get()); |
138 | 0 | std::unique_ptr<QLRowBlock> row_block(rows_result->GetRowBlock()); |
139 | 0 | cout << row_block->ToString(); |
140 | | // Extract the paging state from the result (if present) and populate it in the |
141 | | // statement parameters to retrieve the next set of rows until the end is reached |
142 | | // when there is no more table id in the paging state (below). |
143 | 0 | CHECK_OK(params.SetPagingState(rows_result->paging_state())); |
144 | 0 | break; |
145 | 0 | } |
146 | 0 | case ExecutedResult::Type::SCHEMA_CHANGE: |
147 | 0 | break; |
148 | 0 | } |
149 | 0 | } |
150 | 0 | } |
151 | 0 | } while (!params.table_id().empty()); |
152 | 0 | } |
153 | 0 | } |
154 | | |
155 | | } // namespace ql |
156 | | } // namespace yb |