Skip to content
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

fix: restore current database while restoring connection #21887

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 51 additions & 1 deletion tools/shell/src/shellWebsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <taosws.h>
#include <shellInt.h>

// save current database name
char curDBName[128] = ""; // TDB_MAX_DBNAME_LEN is 24, put large
DuanKuanJun marked this conversation as resolved.
Show resolved Hide resolved

int shell_conn_ws_server(bool first) {
char cuttedDsn[SHELL_WS_DSN_BUFF] = {0};
int dsnLen = strlen(shell.args.dsn);
Expand Down Expand Up @@ -59,6 +62,14 @@ int shell_conn_ws_server(bool first) {
fprintf(stdout, "successfully connected to cloud service\n");
}
fflush(stdout);

// switch to current database if have
if(curDBName[0] !=0) {
char command[256];
sprintf(command, "use %s;", curDBName);
DuanKuanJun marked this conversation as resolved.
Show resolved Hide resolved
shellRunSingleCommandWebsocketImp(command);
}

return 0;
}

Expand Down Expand Up @@ -290,7 +301,46 @@ void shellRunSingleCommandWebsocketImp(char *command) {

if (shellRegexMatch(command, "^\\s*use\\s+[a-zA-Z0-9_]+\\s*;\\s*$",
REG_EXTENDED | REG_ICASE)) {
fprintf(stdout, "Database changed.\r\n\r\n");

gccgdb1234 marked this conversation as resolved.
Show resolved Hide resolved
// copy dbname to curDBName
char *p = command;
bool firstStart = false;
bool firstEnd = false;
int i = 0;
while (*p != 0) {
if (*p != ' ') {
// not blank
if (!firstStart) {
firstStart = true;
} else if (firstEnd) {
if(*p == ';' && *p != '\\') {
break;
}
// database name
curDBName[i++] = *p;
if(i + 4 > sizeof(curDBName)) {
// DBName is too long, reset zero and break
i = 0;
break;
}
}
} else {
// blank
if(firstStart == true && firstEnd == false){
firstEnd = true;
}
if(firstStart && firstEnd && i > 0){
// blank after database name
break;
}
}
// move next
p++;
}
// append end
curDBName[i] = 0;

fprintf(stdout, "Database changed to %s.\r\n\r\n", curDBName);
fflush(stdout);
ws_free_result(res);
return;
Expand Down