Skip to content

Commit

Permalink
Fixes the export of more than one entry through the connection option J…
Browse files Browse the repository at this point in the history
…abRef#685

Added ability to add server specific connection parameters in dbStrings, because MySQL and PostreSQL parameter names differ.

Using setCatalog instead of use database (recommended in mysql doc see: https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html , Section  "Initial Database for Connection" )
  • Loading branch information
Siedlerchr committed Feb 20, 2016
1 parent d86ce49 commit 8bb6272
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by
- Fixed [#803](https://github.com/JabRef/jabref/issues/803): Fixed dynamically group, free-form search
- Fixed [#743](https://github.com/JabRef/jabref/issues/743): Logger not configured when JAR is started
- Fixed [#822](https://github.com/JabRef/jabref/issues/822):OSX - Exception when adding the icon to the dock
- Fixed [#685](https://github.com/JabRef/jabref/issues/685): Fixed MySQL exporting for more than one entry



### Removed
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/sf/jabref/sql/DBStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class DBStrings {
private String database;
private String username;
private String password;
private String dbParameters = "";

private List<String> serverTypes;
private boolean isInitialized;
Expand Down Expand Up @@ -123,6 +124,24 @@ public void isConfigValid(boolean confValid) {
this.configValid = confValid;
}

/**
* Returns the database parameters set
* @return dbParameters: The concatenated parameters
*/
public String getDbParameters() {
return dbParameters;
}

/**
* Add server specific database parameter(s) <br>
* Multiple parameters must be concatenated in the format <br>
* {@code ?Parameter1=value&parameter2=value2}
* @param dbParameter The concatendated parameter
*/
public void setDbParameters(String dbParameters) {
this.dbParameters = dbParameters;
}

/**
* Store these db strings into JabRef preferences.
*/
Expand All @@ -132,4 +151,5 @@ public void storeToPreferences() {
Globals.prefs.put(JabRefPreferences.DB_CONNECT_DATABASE, getDatabase());
Globals.prefs.put(JabRefPreferences.DB_CONNECT_USERNAME, getUsername());
}

}
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/sql/SQLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static AutoCloseable processQueryWithResults(Object out, String query) th
public static String createJDBCurl(DBStrings dbStrings, boolean withDBName) {
String url;
url = "jdbc:" + dbStrings.getServerType().toLowerCase() + "://" + dbStrings.getServerHostname()
+ (withDBName ? '/' + dbStrings.getDatabase() : "");
+ (withDBName ? '/' + dbStrings.getDatabase() : "") + dbStrings.getDbParameters();
return url;
}

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/net/sf/jabref/sql/exporter/MySQLExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
final public class MySQLExporter extends DBExporter {

private static MySQLExporter instance;
private static final String OPT_ALLOW_MULTI_QUERIES = "?allowMultiQueries=true";


private MySQLExporter() {
Expand All @@ -55,15 +56,19 @@ public static MySQLExporter getInstance() {
@Override
public Connection connectToDB(DBStrings dbstrings) throws Exception {
this.dbStrings = dbstrings;

dbStrings.setDbParameters(OPT_ALLOW_MULTI_QUERIES);
String url = SQLUtil.createJDBCurl(dbstrings, false);
String drv = "com.mysql.jdbc.Driver";


Class.forName(drv).newInstance();
Connection conn = DriverManager.getConnection(url,
dbstrings.getUsername(), dbstrings.getPassword());
SQLUtil.processQuery(conn, "CREATE DATABASE IF NOT EXISTS `"
+ dbStrings.getDatabase() + '`');
SQLUtil.processQuery(conn, "USE `" + dbStrings.getDatabase() + '`');

conn.setCatalog(dbStrings.getDatabase());
return conn;
}

Expand All @@ -89,7 +94,7 @@ protected void createTables(Object out) throws SQLException {
out,
"CREATE TABLE IF NOT EXISTS entry_types ( \n"
+ "entry_types_id INT UNSIGNED NOT NULL AUTO_INCREMENT, \n"
+ "label TEXT, \n"
+ "label TEXT, \n"
+ SQLUtil.fieldsAsCols(SQLUtil.getAllFields(),
" VARCHAR(3) DEFAULT NULL") + ", \n"
+ "PRIMARY KEY (entry_types_id) \n" + ");");
Expand Down

0 comments on commit 8bb6272

Please sign in to comment.