-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SQLLINE-386] support working with saved connections
1. Path to file with connections could be specified in connectionConfig property 2. Connections in a file have names, these names could be used as connection nicknames 3. Connections allowed for both: input args and !connect command 4. !rereadconfconnections to reset connections loaded from file or use another file 5. !showconfconnections to read current connection file content
- Loading branch information
Showing
10 changed files
with
418 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
// 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.io.BufferedReader; | ||
import java.io.FileInputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
public class ConnectionConfigParser { | ||
private static final String SEPARATOR = ": "; | ||
private static final char COMMENT_START = '#'; | ||
private final SqlLine sqlLine; | ||
private final Map<String, Properties> connections = new HashMap<>(); | ||
|
||
public ConnectionConfigParser(SqlLine sqlLine) { | ||
this.sqlLine = sqlLine; | ||
} | ||
|
||
Properties getConnectionProperties(String connectionName) { | ||
if (connections.isEmpty()) { | ||
final String connectionConfig = sqlLine.getOpts().getConnectionConfig(); | ||
if (connectionConfig == null || connectionConfig.isEmpty()) { | ||
sqlLine.error("Connection config is not set"); | ||
return null; | ||
} | ||
readFromFile(Paths.get(connectionConfig)); | ||
} | ||
return connections.get(connectionName); | ||
} | ||
|
||
void resetConnectionProperties() { | ||
connections.clear(); | ||
} | ||
|
||
private void readFromFile(Path path) { | ||
if (!Files.exists(path) || Files.isDirectory(path)) { | ||
sqlLine.error("File '" + path.toAbsolutePath() | ||
+ "' not exists or a directory"); | ||
return; | ||
} | ||
int minOffset = Integer.MAX_VALUE; | ||
int offset; | ||
String connectionName = null; | ||
try (BufferedReader br = new BufferedReader( | ||
new InputStreamReader( | ||
new FileInputStream(path.toFile()), StandardCharsets.UTF_8))) { | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
if (line.isEmpty()) { | ||
continue; | ||
} | ||
String name; | ||
String value; | ||
offset = 0; | ||
while (Character.isWhitespace(line.charAt(offset))) { | ||
offset++; | ||
} | ||
if (line.charAt(offset) == COMMENT_START | ||
|| line.charAt(offset) == ':' || !line.contains(SEPARATOR)) { | ||
// skip commented line, line started with ':', | ||
// line not containing SEPARATOR at all | ||
continue; | ||
} | ||
final int sepIndex = line.indexOf(SEPARATOR); | ||
name = line.substring(0, sepIndex).trim(); | ||
if (minOffset >= offset) { | ||
connectionName = name; | ||
connections.put(connectionName, new Properties()); | ||
minOffset = offset; | ||
} else { | ||
value = line.substring(sepIndex + SEPARATOR.length()).trim(); | ||
if (!value.isEmpty() && value.charAt(0) == COMMENT_START) { | ||
value = ""; | ||
} | ||
connections.get(connectionName).setProperty(name, value); | ||
} | ||
} | ||
} catch (Exception e) { | ||
sqlLine.error(e); | ||
} | ||
} | ||
} | ||
|
||
// ConnectionConfigParser.java |
Oops, something went wrong.