-
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.
Created new database framework and moved the database setup logic int…
…o it. - Initial code for the new database framework that is an attempt to abstract the database logic from the Bayesian Network learning logic. - Created a new FactorBaseDataBase interface so that other databases can be supported in the future more easily. - Created a custom DataBaseException that can be thrown by any database technology that implements the FactorBaseDataBase interface. - All the logic found in MakeSetup.java was moved into MySQLFactorBaseDataBase.java since the logic makes more sense to be in this file, i.e. it manipulates the given MySQL database, and MakeSetup.java was moved into a new subdirectory (called "refactored") of the obsolete directory. - Updated RunBB.java to use the new database framework for the database setup logic portion of the process.
- Loading branch information
Showing
5 changed files
with
105 additions
and
2 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.