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

added command for auto-completing connection names and fixed bug in c… #455

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/sqlline/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ public Collection<CommandHandler> getCommandHandlers(SqlLine sqlLine) {
new ReflectiveCommandHandler(sqlLine, empty, "appconfig"),
new ReflectiveCommandHandler(sqlLine, empty, "rerun", "/"),
new ReflectiveCommandHandler(sqlLine, empty, "prompthandler"),
new ReflectiveCommandHandler(sqlLine, new ConnectionCompleter(sqlLine),
"namedconnect")
};
return Collections.unmodifiableList(Arrays.asList(handlers));
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/sqlline/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,17 @@ public void properties(String line, DispatchCallback callback)
}
}

public void namedConnect(String line, DispatchCallback callback) {
String example = "Usage: namedconnect <connectionname>";
String[] parts = sqlLine.split(line);
if (parts.length != 2) {
sqlLine.error(example);
return;
}
String resultline = "connect -c " + parts[1];
connect(resultline, callback);
}

public void connect(String line, DispatchCallback callback) {
String example =
"Usage: connect [-p property value]* (-(c|cn) <connectionName> | <url>) [username] [password] [driver]"
Expand Down Expand Up @@ -2186,6 +2197,10 @@ public Set<String> getAllNames() {
return allNames;
}
}

List<String> configuredConnectionNames() {
return conConfParser.getConnectionNames();
}
}

// End Commands.java
9 changes: 9 additions & 0 deletions src/main/java/sqlline/ConnectionConfigParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

Expand Down Expand Up @@ -64,6 +67,12 @@ void resetConnectionProperties() {
connections.clear();
}

List<String> getConnectionNames() {
List<String> result = new ArrayList<>(connections.keySet());
Collections.sort(result);
return result;
}

private void readFromFile(Path path) {
if (!Files.exists(path) || Files.isDirectory(path)) {
sqlLine.error(sqlLine.loc("no-file", path.toAbsolutePath()));
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/sqlline/ConnectionConfigurationNameCompleter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
// Licensed to Julian Hyde under one or more contributor license
// agreements. See the NOTICE file distributed with this work for
// additional information regarding copyright ownership.
//
// Julian Hyde licenses this file to you under the Modified BSD License
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/BSD-3-Clause
*/
package sqlline;

import java.util.List;

import org.jline.reader.Candidate;
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;
import org.jline.reader.impl.completer.StringsCompleter;

public class ConnectionConfigurationNameCompleter
implements Completer {

private SqlLine sqlline;

public ConnectionConfigurationNameCompleter(SqlLine sqlline) {
this.sqlline = sqlline;
}

@Override
public void complete(LineReader reader, ParsedLine line,
List<Candidate> candidates) {
List<String> connectionNames = sqlline.getCommands()
.configuredConnectionNames();
new StringsCompleter(connectionNames).complete(reader, line, candidates);
}
}
5 changes: 4 additions & 1 deletion src/main/java/sqlline/SqlLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class SqlLine {
private static boolean initComplete = false;

private final SqlLineSignalHandler signalHandler;
private final Completer sqlLineCommandCompleter;
private Completer sqlLineCommandCompleter;

static {
String testClass = "org.jline.reader.LineReader";
Expand Down Expand Up @@ -1971,6 +1971,9 @@ void setAppConfig(Application application) {
setDrivers(null);
this.application = application;
this.appConfig = new Config(application);
// re-create the command line completer, since new commands can be added
// via setAppConfig
this.sqlLineCommandCompleter = new SqlLineCommandCompleter(this);
}

public HighlightStyle getHighlightStyle() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/sqlline/SqlLine.properties
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ help-confirm: Require user to answer 'Are you sure?' before executing 'dangerous
help-confirmPattern: Defines 'dangerous' commands that require user to answer 'Are you sure?' before proceeding (by default, DELETE and DROP)
help-connectInteractionMode: Defines interaction mode for !connect command
help-schemas: List all the schemas in the database
help-namedconnect: Opens a new connection to the database from a config file

variables:\
\n\
\nVariables:\
Expand Down