-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: support multiline SQL input in obclient #<573> #604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,6 +89,14 @@ int init_tcp_sock(const char *server_host, int server_port) | |
| } | ||
| return sockfd; | ||
| } | ||
| static void replace_all(std::string &s) { | ||
| for (auto &ch : s) { | ||
| if (ch == '\n' || ch == '\r') { | ||
| ch = ' '; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| const char *startup_tips = R"( | ||
| Welcome to the OceanBase database implementation course. | ||
|
|
@@ -135,8 +143,14 @@ int main(int argc, char *argv[]) | |
| std::string input_command = ""; | ||
| MiniobLineReader::instance().init(LINE_HISTORY_FILE); | ||
|
|
||
| const char *const_prompt = " -> "; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to use a meaningful name, such as multi_line_prompt, in_command_prompt. |
||
|
|
||
| std::string sql_buffer; | ||
|
|
||
|
|
||
| while (true) { | ||
| input_command = MiniobLineReader::instance().my_readline(prompt_str); | ||
| const char *prompt = sql_buffer.empty() ? prompt_str : const_prompt; | ||
| input_command = MiniobLineReader::instance().my_readline(prompt); | ||
|
|
||
| if (input_command.empty() || common::is_blank(input_command.c_str())) { | ||
| continue; | ||
|
|
@@ -145,38 +159,47 @@ int main(int argc, char *argv[]) | |
| if (MiniobLineReader::instance().is_exit_command(input_command)) { | ||
| break; | ||
| } | ||
|
|
||
| sql_buffer += input_command + ' '; | ||
|
|
||
| if ((send_bytes = write(sockfd, input_command.c_str(), input_command.length() + 1)) == -1) { // TODO writen | ||
| fprintf(stderr, "send error: %d:%s \n", errno, strerror(errno)); | ||
| exit(1); | ||
| } | ||
|
|
||
| memset(send_buf, 0, sizeof(send_buf)); | ||
| std::string trimmed = sql_buffer; | ||
| replace_all(trimmed); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| int len = 0; | ||
| while ((len = recv(sockfd, send_buf, MAX_MEM_BUFFER_SIZE, 0)) > 0) { | ||
| bool msg_end = false; | ||
| for (int i = 0; i < len; i++) { | ||
| if (0 == send_buf[i]) { | ||
| msg_end = true; | ||
| if (!trimmed.empty() && trimmed.find(';') != std::string::npos) { | ||
| if ((send_bytes = write(sockfd, trimmed.c_str(), trimmed.length() + 1)) == -1) { // TODO writen | ||
| fprintf(stderr, "send error: %d:%s \n", errno, strerror(errno)); | ||
| exit(1); | ||
| } | ||
| sql_buffer.clear(); | ||
|
|
||
| memset(send_buf, 0, sizeof(send_buf)); | ||
|
|
||
| int len = 0; | ||
| while ((len = recv(sockfd, send_buf, MAX_MEM_BUFFER_SIZE, 0)) > 0) { | ||
| bool msg_end = false; | ||
| for (int i = 0; i < len; i++) { | ||
| if (0 == send_buf[i]) { | ||
| msg_end = true; | ||
| break; | ||
| } | ||
| printf("%c", send_buf[i]); | ||
| } | ||
| if (msg_end) { | ||
| break; | ||
| } | ||
| printf("%c", send_buf[i]); | ||
| memset(send_buf, 0, MAX_MEM_BUFFER_SIZE); | ||
| } | ||
| if (msg_end) { | ||
|
|
||
| if (len < 0) { | ||
| fprintf(stderr, "Connection was broken: %s\n", strerror(errno)); | ||
| break; | ||
| } | ||
| if (0 == len) { | ||
| printf("Connection has been closed\n"); | ||
| break; | ||
| } | ||
| memset(send_buf, 0, MAX_MEM_BUFFER_SIZE); | ||
| } | ||
|
|
||
| if (len < 0) { | ||
| fprintf(stderr, "Connection was broken: %s\n", strerror(errno)); | ||
| break; | ||
| } | ||
| if (0 == len) { | ||
| printf("Connection has been closed\n"); | ||
| break; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| close(sockfd); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
stringinstead ofstd::string.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace_allis not a good name as it just replacenew linecharacters.