Skip to content

Commit

Permalink
Adding database input to driver session (#27)
Browse files Browse the repository at this point in the history
Co-authored-by: carolinavelasquez <[email protected]>
  • Loading branch information
carovel and carovel-lg authored Dec 9, 2022
1 parent 8a03992 commit 7f190da
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Neo4jBoltConfiguration {
public static final String AUTH_TYPE = "authType";
public static final String USER = "user";
public static final String PASSWORD = "password";
public static final String DATABASE = "database";

private final Map<String, String> configuration;

Expand All @@ -32,6 +33,7 @@ public Integer getPort() {
return Integer.valueOf(configuration.getOrDefault(PORT, "7687"));
}

public String getDatabase() { return configuration.get(DATABASE); }
public String getUser() {
return configuration.get(USER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.exceptions.ClientException;

import java.nio.channels.UnresolvedAddressException;
Expand All @@ -30,6 +31,7 @@ public class Neo4jBoltDatabase implements GraphDatabaseApi {

private final String url;
private final AuthToken auth;
private final SessionConfig dbConfig;

public Neo4jBoltDatabase(Map<String, String> configuration) {
this(new Neo4jBoltConfiguration(configuration));
Expand Down Expand Up @@ -59,6 +61,12 @@ public Neo4jBoltDatabase(Neo4jBoltConfiguration configuration) {
} else {
auth = AuthTokens.none();
}

if (configuration.getDatabase() != null) {
dbConfig = SessionConfig.forDatabase(configuration.getDatabase());
} else {
dbConfig = SessionConfig.defaultConfig();
}
}

@Override
Expand All @@ -72,7 +80,7 @@ public GraphQueryResult execute(String query, Map<String, Object> statementParam
// TODO: driver can be instantiated when this object is constructed. No need to create a new driver every time a query is executed.
Driver driver = GraphDatabase.driver(url, auth);
try {
try (Session session = driver.session()) {
try (Session session = driver.session(dbConfig)) {

Neo4jBoltBuffer buffer = new Neo4jBoltBuffer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="ab71c" layout-manager="GridLayoutManager" row-count="7" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="ab71c" layout-manager="GridLayoutManager" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="General"/>
Expand All @@ -63,15 +63,15 @@
</component>
<component id="8ff6" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="User"/>
</properties>
</component>
<component id="43517" class="javax.swing.JLabel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Password"/>
Expand All @@ -87,19 +87,19 @@
</component>
<component id="38ac4" class="com.intellij.ui.components.JBTextField" binding="userField">
<constraints>
<grid row="4" column="1" row-span="1" col-span="2" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="5" column="1" row-span="1" col-span="2" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="bb1eb" class="com.intellij.ui.components.JBPasswordField" binding="passwordField">
<constraints>
<grid row="5" column="1" row-span="1" col-span="2" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="6" column="1" row-span="1" col-span="2" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="c6d40" class="javax.swing.JButton" binding="testConnectionButton" default-binding="true">
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Test connection"/>
Expand All @@ -124,7 +124,7 @@
<grid id="d111d" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="6" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="7" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -156,7 +156,7 @@
</grid>
<component id="681df" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Authentication"/>
Expand Down Expand Up @@ -188,7 +188,7 @@
</component>
<component id="f5648" class="javax.swing.JComboBox" binding="authTypeComboBox" default-binding="true">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
Expand All @@ -197,6 +197,22 @@
</model>
</properties>
</component>
<component id="f550a" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Database"/>
</properties>
</component>
<component id="1ee31" class="com.intellij.ui.components.JBTextField" binding="databaseField">
<constraints>
<grid row="3" column="1" row-span="1" col-span="2" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
</children>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Neo4jBoltDataSourceDialog extends DataSourceDialog {
private JBTextField hostField;
private JBTextField portField;
private JBTextField userField;
private JBTextField databaseField;
private JBPasswordField passwordField;
private JButton testConnectionButton;
private JPanel loadingPanel;
Expand Down Expand Up @@ -95,6 +96,7 @@ protected JComponent createCenterPanel() {
String host = conf.get(Neo4jBoltConfiguration.HOST);
String port = conf.get(Neo4jBoltConfiguration.PORT);
String authType = conf.get(Neo4jBoltConfiguration.AUTH_TYPE);
String database = conf.get(Neo4jBoltConfiguration.DATABASE);
String user = conf.get(Neo4jBoltConfiguration.USER);
String password = conf.get(Neo4jBoltConfiguration.PASSWORD);

Expand All @@ -116,6 +118,7 @@ protected JComponent createCenterPanel() {

hostField.setText(host);
portField.setText(port);
databaseField.setText(database);
userField.setText(user);
passwordField.setText(password);
}
Expand Down Expand Up @@ -146,6 +149,7 @@ public DataSourceApi constructDataSource() {
configuration.put(Neo4jBoltConfiguration.PORT, data.port);
configuration.put(Neo4jBoltConfiguration.USER, data.user);
configuration.put(Neo4jBoltConfiguration.PASSWORD, data.password);
configuration.put(Neo4jBoltConfiguration.DATABASE, data.database);

return dataSourcesComponent.getDataSourceContainer().createDataSource(
dataSourceToEdit,
Expand Down Expand Up @@ -176,6 +180,7 @@ private Data extractData() {
hostField.getText(),
portField.getText(),
authTypeComboBox.getItemAt(authTypeComboBox.getSelectedIndex()),
databaseField.getText(),
userField.getText(),
String.valueOf(passwordField.getPassword()) // TODO: use password API?
);
Expand All @@ -192,15 +197,17 @@ private static final class Data {
private final String host;
private final String port;
private final String authType;
private final String database;
private final String user;
private final String password;

public Data(String dataSourceName, String protocol, String host, String port, String authType, String user, String password) {
public Data(String dataSourceName, String protocol, String host, String port, String authType, String database, String user, String password) {
this.dataSourceName = dataSourceName;
this.protocol = protocol;
this.host = host;
this.port = port;
this.authType = authType;
this.database = database;
this.user = user;
this.password = password;
}
Expand Down

0 comments on commit 7f190da

Please sign in to comment.