Skip to content

Commit

Permalink
net-sf-ucanaccess-fork: Strict and transparent handling of driver pro…
Browse files Browse the repository at this point in the history
…perties, introduce enum Metadata.Property for driver properties
  • Loading branch information
spannm committed Nov 17, 2023
1 parent 082483f commit 67e3688
Show file tree
Hide file tree
Showing 69 changed files with 764 additions and 570 deletions.
59 changes: 24 additions & 35 deletions src/main/java/net/ucanaccess/console/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import net.ucanaccess.converters.Metadata.Property;
import net.ucanaccess.log.Logger;
import net.ucanaccess.util.Try;
import net.ucanaccess.util.UcanaccessRuntimeException;

import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
private static final String EXPORT_USAGE = "export [--help] [--bom] [-d <delimiter>] [-t <table>] "
Expand All @@ -32,29 +30,16 @@ public Main(Connection _conn, BufferedReader _input) {

}

private static boolean hasPassword(File fl) throws IOException {
try (Database db = Try.catching(() -> DatabaseBuilder.open(fl))
private static boolean hasPassword(File _f) throws IOException {
try (Database db = Try.catching(() -> DatabaseBuilder.open(_f))
.orElseGet(() -> Try.catching(() -> new DatabaseBuilder()
.setReadOnly(true)
.setFile(fl).open()).orThrow())) {
.setFile(_f).open()).orThrow())) {
return db.getDatabasePassword() != null;
}
}

private static void lcProperties(Properties pr) {
Properties nb = new Properties();

for (Entry<Object, Object> entry : pr.entrySet()) {
String key = (String) entry.getKey();
if (key != null) {
nb.put(key.toLowerCase(), entry.getValue());
}
}
pr.clear();
pr.putAll(nb);
}

public static void main(String[] args) throws Exception {
public static void main(String[] _args) throws Exception {
Logger.setLogPrintWriter(new PrintWriter(System.out));
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
// password properties info
Expand All @@ -63,25 +48,29 @@ public static void main(String[] args) throws Exception {
long size = 0;
String passwordEntry = "";
String[] commands = null;
if (args.length > 0) {
String file = args[0];
if (_args.length > 0) {
String file = _args[0];
if (file.endsWith(".properties")) {
File pfl = new File(args[0]);
File pfl = new File(_args[0]);
if (pfl.exists()) {
try (FileInputStream fis = new FileInputStream(pfl)) {
props.load(fis);
}
lcProperties(props);
// convert keys to enum name or lower-case
props = props.stringPropertyNames().stream()
.collect(Collectors.toMap(
k -> Optional.ofNullable(Property.parse(k)).map(Property::name).orElse(k.toLowerCase()),
props::getProperty, (v1, v2) -> v1, Properties::new));
}
} else if (file.endsWith(".accdb") || file.endsWith(".mdb")) {
fl = new File(file);
size = fl.length();
if (args.length > 1) {
if (_args.length > 1) {
int arg = 1;
if (hasPassword(fl)) {
passwordEntry = args[arg++];
passwordEntry = _args[arg++];
} else {
commands = Arrays.copyOfRange(args, arg++, args.length);
commands = Arrays.copyOfRange(_args, arg++, _args.length);
}
}
}
Expand All @@ -91,8 +80,8 @@ public static void main(String[] args) throws Exception {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
} catch (ClassNotFoundException e) {

System.out.println(e.getMessage());
System.out.println("Check your classpath! ");
System.err.println(e.getMessage());
System.err.println("Check your classpath!");
System.exit(1);
}
while (fl == null || !fl.exists()) {
Expand All @@ -114,13 +103,13 @@ public static void main(String[] args) throws Exception {
Connection conn = null;
try {
String noMem = "";
if (passwordEntry.isEmpty() && (props.containsKey("jackcessopener") || hasPassword(fl))) {
if (passwordEntry.isEmpty() && (props.containsKey(Property.jackcessOpener.name()) || hasPassword(fl))) {
System.out.print("Please, enter password: ");
passwordEntry = ";password=" + input.readLine().trim();
}

if (!props.containsKey("jackcessopener")) {
noMem = size > 30000000 ? ";memory=false" : "";
if (!props.containsKey(Property.jackcessOpener.name())) {
noMem = size > 30000000 ? ";" + Property.memory + "=false" : "";
}

conn = DriverManager.getConnection("jdbc:ucanaccess://" + fl.getAbsolutePath() + passwordEntry + noMem, props);
Expand All @@ -132,7 +121,7 @@ public static void main(String[] args) throws Exception {
}
} catch (Exception _ex) {
_ex.printStackTrace();
System.out.println(_ex.getMessage());
System.err.println(_ex.getMessage());
System.exit(1);
}
Main main = new Main(conn, input);
Expand Down
Loading

0 comments on commit 67e3688

Please sign in to comment.