Skip to content

Commit

Permalink
Make insert keyword to be used depending upon Database type while pre…
Browse files Browse the repository at this point in the history
…paring SQL queries to load data. oltpbenchmark#364
  • Loading branch information
chrajeshbabu committed Jun 24, 2021
1 parent b61f29d commit 44db87e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private void loadUsers(Connection conn, int lo, int hi) throws SQLException {
String sql = SQLUtil.getInsertSQL(catalog_tbl, this.getDatabaseType());
if(this.getDatabaseType() == DatabaseType.ORACLE) {
// Oracle handles quoted object identifiers differently, do not escape names
sql = SQLUtil.getInsertSQL(catalog_tbl, false);
sql = SQLUtil.getInsertSQL(catalog_tbl, false, DatabaseType.ORACLE.getInsertKeyword());
}
PreparedStatement userInsert = conn.prepareStatement(sql);

Expand Down Expand Up @@ -268,7 +268,7 @@ private void loadPages(Connection conn, int lo, int hi) throws SQLException {
String sql = SQLUtil.getInsertSQL(catalog_tbl, this.getDatabaseType());
if (this.getDatabaseType() == DatabaseType.ORACLE) {
// Oracle handles quoted object identifiers differently, do not escape names
sql = SQLUtil.getInsertSQL(catalog_tbl, false);
sql = SQLUtil.getInsertSQL(catalog_tbl, false, DatabaseType.ORACLE.getInsertKeyword());
}
PreparedStatement pageInsert = conn.prepareStatement(sql);

Expand Down Expand Up @@ -427,7 +427,7 @@ private void loadRevision(Connection conn) throws SQLException {
String textSQL = SQLUtil.getInsertSQL(textTable, this.getDatabaseType());
if (this.getDatabaseType() == DatabaseType.ORACLE) {
// Oracle handles quoted object identifiers differently, do not escape names
textSQL = SQLUtil.getInsertSQL(textTable, false);
textSQL = SQLUtil.getInsertSQL(textTable, false, DatabaseType.ORACLE.getInsertKeyword());
}
PreparedStatement textInsert = conn.prepareStatement(textSQL);

Expand All @@ -436,7 +436,7 @@ private void loadRevision(Connection conn) throws SQLException {
String revSQL = SQLUtil.getInsertSQL(revTable, this.getDatabaseType());
if (this.getDatabaseType() == DatabaseType.ORACLE) {
// Oracle handles quoted object identifiers differently, do not escape names
revSQL = SQLUtil.getInsertSQL(revTable, false);
revSQL = SQLUtil.getInsertSQL(revTable, false, DatabaseType.ORACLE.getInsertKeyword());
}
PreparedStatement revisionInsert = conn.prepareStatement(revSQL);

Expand Down
50 changes: 31 additions & 19 deletions src/com/oltpbenchmark/types/DatabaseType.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,37 @@ public enum DatabaseType {
* (3) Should SQLUtil.getInsertSQL include col names
* (4) Does this DBMS support "real" transactions?
*/
DB2("com.ibm.db2.jcc.DB2Driver", true, false, true),
MYSQL("com.mysql.jdbc.Driver", true, false, true),
MYROCKS("com.mysql.jdbc.Driver", true, false, true),
POSTGRES("org.postgresql.Driver", false, false, true),
ORACLE("oracle.jdbc.driver.OracleDriver", true, false, true),
SQLSERVER("com.microsoft.sqlserver.jdbc.SQLServerDriver", true, false, true),
SQLITE("org.sqlite.JDBC", true, false, true),
AMAZONRDS(null, true, false, true),
SQLAZURE(null, true, false, true),
ASSCLOWN(null, true, false, true),
HSQLDB("org.hsqldb.jdbcDriver", false, false, true),
H2("org.h2.Driver", true, false, true),
MONETDB("nl.cwi.monetdb.jdbc.MonetDriver", false, false, true),
NUODB("com.nuodb.jdbc.Driver", true, false, true),
TIMESTEN("com.timesten.jdbc.TimesTenDriver", true, false, true),
CASSANDRA("com.github.adejanovski.cassandra.jdbc.CassandraDriver", true, true, false),
MEMSQL("com.mysql.jdbc.Driver", true, false, false),
NOISEPAGE("org.postgresql.Driver", false, false, true),
DB2("com.ibm.db2.jcc.DB2Driver", true, false, true, "INSERT"),
MYSQL("com.mysql.jdbc.Driver", true, false, true, "INSERT"),
MYROCKS("com.mysql.jdbc.Driver", true, false, true, "INSERT"),
POSTGRES("org.postgresql.Driver", false, false, true, "INSERT"),
ORACLE("oracle.jdbc.driver.OracleDriver", true, false, true, "INSERT"),
SQLSERVER("com.microsoft.sqlserver.jdbc.SQLServerDriver", true, false, true, "INSERT"),
SQLITE("org.sqlite.JDBC", true, false, true, "INSERT"),
AMAZONRDS(null, true, false, true, "INSERT" ),
SQLAZURE(null, true, false, true, "INSERT"),
ASSCLOWN(null, true, false, true, "INSERT"),
HSQLDB("org.hsqldb.jdbcDriver", false, false, true, "INSERT"),
H2("org.h2.Driver", true, false, true, "INSERT"),
MONETDB("nl.cwi.monetdb.jdbc.MonetDriver", false, false, true, "INSERT"),
NUODB("com.nuodb.jdbc.Driver", true, false, true, "INSERT"),
TIMESTEN("com.timesten.jdbc.TimesTenDriver", true, false, true, "INSERT"),
CASSANDRA("com.github.adejanovski.cassandra.jdbc.CassandraDriver", true, true, false, "INSERT"),
MEMSQL("com.mysql.jdbc.Driver", true, false, false, "INSERT"),
NOISEPAGE("org.postgresql.Driver", false, false, true, "INSERT")
;

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

}

/**
Expand Down Expand Up @@ -90,6 +93,13 @@ private DatabaseType(String driver,
* when the framework tries to set the isolation level.
*/
private boolean supportTxns;

/**
* Most of the databases use "INSERT" key word in insert statements to write data to database
* but some databases like Apache Phoenix uses "UPSERT" to combine the semantics of insert
* and update together in same query. This helps to define the insert key word used in DB.
*/
private String insertKeyword;

// ---------------------------------------------------------------
// ACCESSORS
Expand Down Expand Up @@ -129,6 +139,8 @@ public boolean shouldIncludeColumnNames() {
public boolean shouldUseTransactions() {
return (this.supportTxns);
}

public String getInsertKeyword() { return (this.insertKeyword); }

// ----------------------------------------------------------------
// STATIC METHODS + MEMBERS
Expand Down
17 changes: 10 additions & 7 deletions src/com/oltpbenchmark/util/SQLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public static String getCountSQL(DatabaseType dbType, Table catalog_tbl, String
* @return
*/
public static String getInsertSQL(DatabaseType dbType, Table catalog_tbl, int...exclude_columns) {
return getInsertSQL(catalog_tbl, false, dbType.shouldEscapeNames(), 1, exclude_columns);
return getInsertSQL(catalog_tbl, false, dbType.shouldEscapeNames(), 1, dbType.getInsertKeyword(), exclude_columns);
}

/**
Expand All @@ -347,10 +347,11 @@ public static String getInsertSQL(DatabaseType dbType, Table catalog_tbl, int...
*
* @param catalog_tbl Table affected
* @param escape_names Flag to escape object names
* @param insertKeyword
* @return
*/
public static String getInsertSQL(Table catalog_tbl, boolean escape_names, int...exclude_columns) {
return getInsertSQL(catalog_tbl, false, escape_names, 1, exclude_columns);
public static String getInsertSQL(Table catalog_tbl, boolean escape_names, String insertKeyword, int...exclude_columns) {
return getInsertSQL(catalog_tbl, false, escape_names, 1, insertKeyword, exclude_columns);
}

/**
Expand All @@ -363,7 +364,7 @@ public static String getInsertSQL(Table catalog_tbl, boolean escape_names, int..
* @return
*/
public static String getInsertSQL(Table catalog_tbl, DatabaseType db_type, int... exclude_columns) {
return SQLUtil.getInsertSQL(catalog_tbl, false, false, 1, exclude_columns);
return SQLUtil.getInsertSQL(catalog_tbl, false, false, 1, db_type.getInsertKeyword(), exclude_columns);
}

/**
Expand All @@ -376,7 +377,7 @@ public static String getInsertSQL(Table catalog_tbl, DatabaseType db_type, int..
*/
@Deprecated
public static String getInsertSQL(Table catalog_tbl, boolean show_cols, int batchSize, int...exclude_columns) {
return getInsertSQL(catalog_tbl, false, true, batchSize, exclude_columns);
return getInsertSQL(catalog_tbl, false, true, batchSize, "INSERT", exclude_columns);
}

/**
Expand All @@ -388,11 +389,13 @@ public static String getInsertSQL(Table catalog_tbl, boolean show_cols, int batc
* the number of sets of parameters
* that should be included in the insert
* @param exclude_columns
* @param insertKeyword
* @return
*/
public static String getInsertSQL(Table catalog_tbl, boolean show_cols, boolean escape_names, int batchSize, int... exclude_columns) {
public static String getInsertSQL(Table catalog_tbl, boolean show_cols, boolean escape_names, int batchSize, String insertKeyword, int... exclude_columns) {
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO ")
sb.append(insertKeyword);
sb.append(" INTO ")
.append(escape_names ? catalog_tbl.getEscapedName() : catalog_tbl.getName());

StringBuilder values = new StringBuilder();
Expand Down

0 comments on commit 44db87e

Please sign in to comment.