Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors database code #987

Merged
merged 29 commits into from
Apr 18, 2016
Merged

Refactors database code #987

merged 29 commits into from
Apr 18, 2016

Conversation

simonharrer
Copy link
Contributor

Refactoring of the database SQL stuff.

  • Favor composition over inheritance
  • Make a lot of stuff more explicit, e.g., DBStringsPreferences are the values which are stored in the preferences (a subset of the DBStrings basically), and DatabaseType defines the types
  • MySQL and PostgreSQL specific code is now in the MySQL and the PostgreSQL class and can be accessed, both implement the Database interface which defines all DB specific code.

There is still a lot to do in that package, but this should be a good start, I think.

@simonharrer simonharrer added cleanup-ops status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers labels Mar 17, 2016
output(Localization.lang("No entries or multiple entries selected."));
return;
}
JabRefExecutorService.INSTANCE.execute((Runnable) () -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using IntelliJ(?), right? I'm wondering if the settings there and Eclipse's are the same? I think I formatted that complete block when I introduced the lambda... But not fully sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am an IntelliJ addict. Formatting seems to differ. I reverted it.

@oscargus
Copy link
Contributor

Looks good!

this.database = database;
}

public String getServerType() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to change the return type to DatabaseType instead of the string value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

public String getCreateTableSQL(Table table) {
switch (table) {

case JABREF_DATABASE:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point I would suggest better creating a method for each case
e.g. createhabRefDatabase
That would better follow the principle of a single purpose method and additionally reduces the length of the method. Furthermore better readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, to me, the method is still quite simple despite being long in lines as it comes with almost no complexity. Hence, I would not gain that much by splitting it up. From my perspective, I would have the hassle of maintaining 9 methods, which have to be put in the interface, and I cannot iterate over these 9 methods in the caller (which is currently done as I can iterate over the Table enum to call the method for each table).

@tobiasdiez
Copy link
Member

In principle LGTM

# Conflicts:
#	src/main/java/net/sf/jabref/exporter/MySQLExport.java
#	src/main/java/net/sf/jabref/exporter/PostgreSQLExport.java
#	src/main/java/net/sf/jabref/sql/exporter/DatabaseExporter.java
@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 26.494% when pulling 7adae91 on refactor-database-code into 70a65a6 on master.

Connection conn = connect(url, dbStrings.getDbPreferences().getUsername(), dbStrings.getPassword());

SQLUtil.processQuery(conn, "CREATE DATABASE IF NOT EXISTS `" + dbStrings.getDbPreferences().getDatabase() + '`');
conn.setCatalog(dbStrings.getDbPreferences().getDatabase());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one thing I got in mind while looking through an old PR I created a while ago:
There is a potential resource leak in here:
If the Create Database query fails and throws an SQLException, the connection does not get closed and remains open.
That was one thing I fixed in my old PR #910 because it was discovered from coverity amalysis.
My solution was to catch the exception and close the conn. Then re throw the ex.
https://github.com/Siedlerchr/jabref/blob/reworkedCoverityFixes/src/main/java/net/sf/jabref/sql/exporter/MySQLExporter.java

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, you are talking about managing the connection itself. I am not sure if we do handle the connection resource at all, mostly only the statements and result sets.

@stefan-kolb
Copy link
Member

Can you merge this in?

@simonharrer
Copy link
Contributor Author

Hm, I am not completely sure this introduces some bugs. Should I wait for directly after the release?

@simonharrer simonharrer added this to the v3.4 milestone Apr 5, 2016
@simonharrer simonharrer merged commit 5993f3d into master Apr 18, 2016
@simonharrer simonharrer deleted the refactor-database-code branch April 18, 2016 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup-ops status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants