-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from sfu-cl-lab/new-database-framework
Initial changes required to change our code to use the new database framework and some cleanup.
- Loading branch information
Showing
6 changed files
with
144 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
code/factorbase/src/main/java/ca/sfu/cs/factorbase/database/FactorBaseDataBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ca.sfu.cs.factorbase.database; | ||
|
||
import ca.sfu.cs.factorbase.exception.DataBaseException; | ||
|
||
/** | ||
* Methods expected to be implemented to enable the extraction of data from a database for FactorBase. | ||
*/ | ||
public interface FactorBaseDataBase { | ||
/** | ||
* This method should setup all the extra tables required for FactorBase to learn a Bayesian | ||
* network for the provided database. | ||
* | ||
* @throws DataBaseException if an error occurs when attempting to access the database. | ||
*/ | ||
void setupDatabase() throws DataBaseException; | ||
} |
53 changes: 53 additions & 0 deletions
53
code/factorbase/src/main/java/ca/sfu/cs/factorbase/database/MySQLFactorBaseDataBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ca.sfu.cs.factorbase.database; | ||
|
||
import java.io.IOException; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.text.MessageFormat; | ||
|
||
import ca.sfu.cs.common.Configuration.Config; | ||
import ca.sfu.cs.factorbase.exception.DataBaseException; | ||
import ca.sfu.cs.factorbase.util.BZScriptRunner; | ||
|
||
import com.mysql.jdbc.Connection; | ||
|
||
public class MySQLFactorBaseDataBase implements FactorBaseDataBase { | ||
|
||
private static final String CONNECTION_STRING = "jdbc:{0}/{1}"; | ||
private String baseDatabaseName; | ||
private Connection baseConnection; | ||
|
||
|
||
/** | ||
* Create connections to the databases required by FactorBase to learn a Bayesian Network. | ||
* | ||
* @param dbaddress - the address of the MySQL database to connect to. e.g. mysql://127.0.0.1 | ||
* @param dbname - the name of the database with the original data. e.g. unielwin | ||
* @param username - the username to use when accessing the database. | ||
* @param password - the password to use when accessing the database. | ||
* @throws SQLException if there is a problem connecting to the required databases. | ||
*/ | ||
public MySQLFactorBaseDataBase(String dbaddress, String dbname, String username, String password) throws DataBaseException { | ||
this.baseDatabaseName = dbname; | ||
String baseConnectionString = MessageFormat.format(CONNECTION_STRING, dbaddress, dbname); | ||
|
||
try { | ||
this.baseConnection = (Connection) DriverManager.getConnection(baseConnectionString, username, password); | ||
} catch (SQLException e) { | ||
throw new DataBaseException("Unable to connect to the provided database.", e); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void setupDatabase() throws DataBaseException { | ||
BZScriptRunner bzsr = new BZScriptRunner(this.baseDatabaseName, this.baseConnection); | ||
try { | ||
bzsr.runScript(Config.SCRIPTS_DIRECTORY + "setup.sql"); | ||
bzsr.createSP(Config.SCRIPTS_DIRECTORY + "storedprocs.sql"); | ||
bzsr.callSP("find_values"); | ||
} catch (SQLException | IOException e) { | ||
throw new DataBaseException("An error occurred when attempting to setup the database for FactorBase.", e); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
code/factorbase/src/main/java/ca/sfu/cs/factorbase/exception/DataBaseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ca.sfu.cs.factorbase.exception; | ||
|
||
|
||
/** | ||
* Custom exception to wrap and throw the specific cause of an error. Should be thrown when an | ||
* error occurs when attempting to access a database. | ||
*/ | ||
public class DataBaseException extends Exception { | ||
|
||
|
||
/** | ||
* Serial version - auto-generated by Eclipse. | ||
*/ | ||
private static final long serialVersionUID = -6623897407612813075L; | ||
|
||
|
||
/** | ||
* Used to wrap the root cause of an exception when attempting to access a database. | ||
* | ||
* @param message - useful message telling the user what has potentially gone wrong. | ||
* @param cause - the exception that caused the error to occur when accessing a database. | ||
*/ | ||
public DataBaseException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters