Skip to content

Commit

Permalink
Enhance connection builder
Browse files Browse the repository at this point in the history
- Fix withoutUserPass (wrong property)
- Strictly typed parameters
- Allow null in dbPath and newDatabaseVersion
  • Loading branch information
spannm committed Mar 2, 2024
1 parent 7945810 commit adaea3f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
24 changes: 21 additions & 3 deletions src/main/java/net/ucanaccess/jdbc/UcanaccessConnectionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import static net.ucanaccess.converters.Metadata.Property.*;

import io.github.spannm.jackcess.Database.FileFormat;
import net.ucanaccess.converters.Metadata;
import net.ucanaccess.converters.Metadata.Property;
import net.ucanaccess.exception.UcanaccessRuntimeException;
import net.ucanaccess.type.ColumnOrder;
import net.ucanaccess.util.Try;

import java.io.File;
import java.sql.DriverManager;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -28,6 +30,11 @@ public UcanaccessConnectionBuilder withDbPath(String _dbPath) {
return this;
}

public UcanaccessConnectionBuilder withDbPath(File _dbPath) {
dbPath = Optional.ofNullable(_dbPath).map(File::getAbsolutePath).orElse(null);
return this;
}

public UcanaccessConnectionBuilder withUser(String _user) {
return withProp(user, _user);
}
Expand All @@ -37,7 +44,7 @@ public UcanaccessConnectionBuilder withPassword(String _pass) {
}

public UcanaccessConnectionBuilder withoutUserPass() {
props.remove(ignoreCase);
props.remove(user);
props.remove(password);
return this;
}
Expand Down Expand Up @@ -70,8 +77,19 @@ public UcanaccessConnectionBuilder withMemory() {
return withProp(memory, true);
}

public UcanaccessConnectionBuilder withNewDatabaseVersionProp(String _name) {
return withProp(newDatabaseVersion, _name);
public UcanaccessConnectionBuilder withNewDatabaseVersion(FileFormat _version) {
return withProp(newDatabaseVersion, _version == null ? null : _version.name());
}

public UcanaccessConnectionBuilder withNewDatabaseVersion(String _version) {
FileFormat version = null;
if (_version != null) {
version = FileFormat.parse(_version);
if (version == null) {
throw new UcanaccessRuntimeException("Version required");
}
}
return withProp(newDatabaseVersion, version);
}

public UcanaccessConnectionBuilder withProp(Metadata.Property _prop, Object _value) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/ucanaccess/jdbc/CreateDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void testNewDatabase(AccessVersion _accessVersion) throws Exception {
.withDbPath(fileMdb.getAbsolutePath())
.withoutUserPass()
.withImmediatelyReleaseResources()
.withNewDatabaseVersionProp(getFileFormat().name());
.withNewDatabaseVersion(getFileFormat());
getLogger().log(Level.DEBUG, "Database url: {0}", bldr.getUrl());
UcanaccessConnection conn = bldr.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void testReloadMirror(AccessVersion _accessVersion) throws Exception {
.withDbPath(dbFile.getAbsolutePath())
.withoutUserPass()
.withMemory()
.withNewDatabaseVersionProp(getFileFormat().name())
.withNewDatabaseVersion(getFileFormat())
.build();
UcanaccessStatement stCreate = conn.createStatement()) {
stCreate.execute("CREATE TABLE Table1 (ID COUNTER PRIMARY KEY, TextField TEXT(50))");
Expand Down

0 comments on commit adaea3f

Please sign in to comment.