Skip to content
This repository has been archived by the owner on Aug 23, 2021. It is now read-only.

Commit

Permalink
DatabaseType now includes information about whether SQLUtil should es…
Browse files Browse the repository at this point in the history
…cape table/column names or not. This is not used by all of the loaders yet but this should make it easier to port new DBMSs
  • Loading branch information
apavlo committed Dec 1, 2016
1 parent 1d03a49 commit e0a1c4e
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 47 deletions.
56 changes: 28 additions & 28 deletions src/com/oltpbenchmark/benchmarks/tpcc/TPCCLoader.java
Expand Up @@ -42,6 +42,7 @@

import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Expand Down Expand Up @@ -101,17 +102,7 @@ public TPCCLoader(TPCCBenchmark benchmark, Connection c) {
private PreparedStatement getInsertStatement(String tableName) throws SQLException {
Table catalog_tbl = this.benchmark.getTableCatalog(tableName);
assert(catalog_tbl != null);

// if current dbType is either postgres or monetdb make insertSQL
// without escaped character.
String sql = null;

if (this.getDatabaseType() == DatabaseType.POSTGRES
|| this.getDatabaseType() == DatabaseType.MONETDB)
sql = SQLUtil.getInsertSQL(catalog_tbl, false);
else
sql = SQLUtil.getInsertSQL(catalog_tbl);

String sql = SQLUtil.getInsertSQL(catalog_tbl, this.getDatabaseType().shouldEscapeNames());
PreparedStatement stmt = this.conn.prepareStatement(sql);
return stmt;
}
Expand Down Expand Up @@ -163,6 +154,7 @@ protected int loadItem(int itemKount) {
int startORIGINAL = 0;


boolean fail = false;
try {
PreparedStatement itemPrepStmt = getInsertStatement(TPCCConstants.TABLENAME_ITEM);

Expand Down Expand Up @@ -249,11 +241,10 @@ protected int loadItem(int itemKount) {
} // end for

long tmpTime = new java.util.Date().getTime();
String etStr = " Elasped Time(ms): "
+ ((tmpTime - lastTimeMS) / 1000.000)
+ " ";
LOG.debug(etStr.substring(0, 30) + " Writing record " + k
+ " of " + t);
if (LOG.isDebugEnabled()) {
String etStr = " Elasped Time(ms): " + ((tmpTime - lastTimeMS) / 1000.000) + " ";
LOG.debug(etStr.substring(0, 30) + " Writing record " + k + " of " + t);
}
lastTimeMS = tmpTime;

if (outputFiles == false) {
Expand All @@ -262,19 +253,28 @@ protected int loadItem(int itemKount) {

transCommit();
now = new java.util.Date();
LOG.debug("End Item Load @ " + now);
if (LOG.isDebugEnabled()) {
LOG.debug("End Item Load @ " + now);
}

} catch (SQLException se) {
LOG.debug(se.getMessage());
se.printStackTrace();
SQLException next = se.getNextException();
if (next != null) {
LOG.debug(next.getMessage());
}
transRollback();
} catch (Exception e) {
e.printStackTrace();
transRollback();
} catch (BatchUpdateException ex) {
SQLException next = ex.getNextException();
LOG.error("Failed to load data for TPC-C", ex);
if (next != null) LOG.error(ex.getClass().getSimpleName() + " Cause => " + next.getMessage());
fail = true;
} catch (SQLException ex) {
SQLException next = ex.getNextException();
LOG.error("Failed to load data for TPC-C", ex);
if (next != null) LOG.error(ex.getClass().getSimpleName() + " Cause => " + next.getMessage());
fail = true;
} catch (Exception ex) {
LOG.error("Failed to load data for TPC-C", ex);
fail = true;
} finally {
if (fail) {
LOG.debug("Rolling back changes from last batch");
transRollback();
}
}

return (k);
Expand Down
105 changes: 89 additions & 16 deletions src/com/oltpbenchmark/types/DatabaseType.java
Expand Up @@ -20,26 +20,99 @@
import java.util.HashMap;
import java.util.Map;

/**
* List of the database management systems that we support
* in the framework.
* @author pavlo
*/
public enum DatabaseType {

DB2,
MYSQL,
POSTGRES,
ORACLE,
SQLSERVER,
SQLITE,
AMAZONRDS,
HSTORE,
SQLAZURE,
ASSCLOWN,
HSQLDB,
H2,
MONETDB,
NUODB,
TIMESTEN,
PELOTON
/**
* Parameters:
* (1) JDBC Driver String
* (2) Should SQLUtil.getInserSQL escape table/col names
* (3) Should SQLUtil.getInserSQL include col names
*/
DB2("com.ibm.db2.jcc.DB2Driver", true, false),
MYSQL("com.mysql.jdbc.Driver", true, false),
POSTGRES("org.postgresql.Driver", false, false),
ORACLE("oracle.jdbc.driver.OracleDriver", true, false),
SQLSERVER("com.microsoft.sqlserver.jdbc.SQLServerDriver", true, false),
SQLITE("org.sqlite.JDBC", true, false),
AMAZONRDS(null, true, false),
SQLAZURE(null, true, false),
ASSCLOWN(null, true, false),
HSQLDB("org.hsqldb.jdbcDriver", true, false),
H2("org.h2.Driver", true, false),
MONETDB("nl.cwi.monetdb.jdbc.MonetDriver", false, false),
NUODB("com.nuodb.jdbc.Driver", true, false),
TIMESTEN("com.timesten.jdbc.TimesTenDriver", true, false),
PELOTON("org.postgresql.Driver", false, false)
;

private DatabaseType(String driver, boolean escapeNames, boolean includeColNames) {
this.driver = driver;
this.escapeNames = escapeNames;
this.includeColNames = includeColNames;
}

/**
* This is the suggested driver string to use in the configuration xml
* This corresponds to the <B>'driver'</b> attribute.
*/
private final String driver;

/**
* If this flag is set to true, then the framework will escape names in
* the INSERT queries
*/
private final boolean escapeNames;

/**
* If this flag is set to true, then the framework will include the column names
* when generating INSERT queries for loading data.
*/
private final boolean includeColNames;

// ---------------------------------------------------------------
// ACCESSORS
// ----------------------------------------------------------------

/**
* Returns the suggested driver string to use for the given database type
* @return
*/
public String getSuggestedDriver() {
return (this.driver);
}

/**
* Returns true if the framework should escape the names of columns/tables when
* generating SQL to load in data for the target database type.
* @return
*/
public boolean shouldEscapeNames() {
return (this.escapeNames);
}

/**
* Returns true if the framework should include the names of columns when
* generating SQL to load in data for the target database type.
* @return
*/
public boolean shouldIncludeColumnNames() {
return (this.includeColNames);
}


// ----------------------------------------------------------------
// STATIC METHODS + MEMBERS
// ----------------------------------------------------------------

/**
* This is the database type that we will use in our unit tests.
* This should always be one of the embedded java databases
*/
public static final DatabaseType TEST_TYPE = DatabaseType.HSQLDB;

protected static final Map<Integer, DatabaseType> idx_lookup = new HashMap<Integer, DatabaseType>();
Expand Down
9 changes: 9 additions & 0 deletions src/com/oltpbenchmark/util/SQLUtil.java
Expand Up @@ -366,6 +366,15 @@ public static String getInsertSQL(Table catalog_tbl, boolean show_cols, int batc
return getInsertSQL(catalog_tbl, false, true, batchSize, exclude_columns);
}

/**
* Automatically generate the 'INSERT' SQL string for this table
* @param catalog_tbl
* @param show_cols
* @param escape_names
* @param batchSize
* @param exclude_columns
* @return
*/
public static String getInsertSQL(Table catalog_tbl, boolean show_cols, boolean escape_names, int batchSize, int...exclude_columns) {
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO ")
Expand Down
5 changes: 2 additions & 3 deletions tests/com/oltpbenchmark/api/AbstractTestCase.java
Expand Up @@ -40,8 +40,7 @@ public abstract class AbstractTestCase<T extends BenchmarkModule> extends TestCa

// HSQLDB
public static final String DB_CONNECTION = "jdbc:hsqldb:mem:";
public static final String DB_JDBC = "org.hsqldb.jdbcDriver";
public static final DatabaseType DB_TYPE = DatabaseType.HSQLDB;
public static final DatabaseType DB_TYPE = DatabaseType.TEST_TYPE;

// H2
// public static final String DB_CONNECTION = "jdbc:h2:mem:";
Expand All @@ -67,7 +66,7 @@ public abstract class AbstractTestCase<T extends BenchmarkModule> extends TestCa
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void setUp(Class<T> clazz, Class...procClasses) throws Exception {
super.setUp();
Class.forName(DB_JDBC);
Class.forName(DB_TYPE.getSuggestedDriver());

this.workConf = new WorkloadConfiguration();
TransactionTypes txnTypes = new TransactionTypes();
Expand Down

0 comments on commit e0a1c4e

Please sign in to comment.