Skip to content

Commit

Permalink
Merge pull request microsoft#386 from v-xiangs/newdev-populate-Table-…
Browse files Browse the repository at this point in the history
…With-PreparedStatement

populate table with prepared statement
  • Loading branch information
xiangyushawn authored Sep 25, 2017
2 parents e618ade + 7cdbf33 commit 9f2ef21
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.DBConnection;
import com.microsoft.sqlserver.testframework.DBPreparedStatement;
import com.microsoft.sqlserver.testframework.DBStatement;
import com.microsoft.sqlserver.testframework.DBTable;;

Expand All @@ -37,7 +38,8 @@ static void setUpSourceTable() {
stmt = con.createStatement();
sourceTable = new DBTable(true);
stmt.createTable(sourceTable);
stmt.populateTable(sourceTable);
DBPreparedStatement pstmt = new DBPreparedStatement(con);
pstmt.populateTable(sourceTable);
}
finally {
con.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public abstract class AbstractSQLGenerator {// implements ISQLGenerator {
protected static final String PRIMARY_KEY = "PRIMARY KEY";
protected static final String DEFAULT = "DEFAULT";
protected static final String COMMA = ",";
protected static final String QUESTION_MARK = "?";

// FIXME: Find good word for '. Better replaced by wrapIdentifier.
protected static final String TICK = "'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public DBPreparedStatement(DBConnection dbconnection) {
}

/**
* set up internal PreparedStatement with query
*
* @param query
* @return
* @throws SQLException
*
*/
Expand Down Expand Up @@ -84,4 +88,22 @@ public DBResultSet executeQuery() throws SQLException {
return dbresultSet;
}

/**
* populate table with values using prepared statement
*
* @param table
* @return <code>true</code> if table is populated
*/
public boolean populateTable(DBTable table) {
return table.populateTableWithPreparedStatement(this);
}

/**
*
* @return
* @throws SQLException
*/
public boolean execute() throws SQLException {
return pstmt.execute();
}
}
57 changes: 54 additions & 3 deletions src/test/java/com/microsoft/sqlserver/testframework/DBTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.junit.jupiter.api.Assertions.fail;

import java.sql.JDBCType;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -143,7 +144,7 @@ private void addColumns(boolean unicode) {
public String getTableName() {
return tableName;
}

public List<DBColumn> getColumns() {
return this.columns;
}
Expand All @@ -156,7 +157,7 @@ public List<DBColumn> getColumns() {
public String getEscapedTableName() {
return escapedTableName;
}

public String getDefinitionOfColumns() {
return tableDefinition;
}
Expand Down Expand Up @@ -234,7 +235,7 @@ else if (VariableLengthType.ScaleOnly == column.getSqlType().getVariableLengthTy
tableDefinition = tableDefinition.substring(0, indexOfLastComma);

sb.add(tableDefinition);

sb.add(CLOSE_BRACKET);
return sb.toString();
}
Expand All @@ -257,6 +258,56 @@ boolean populateTable(DBStatement dbstatement) {
return false;
}

/**
* using prepared statement to populate table with values
*
* @param dbstatement
* @return
*/
boolean populateTableWithPreparedStatement(DBPreparedStatement dbPStmt) {
try {
populateValues();

// create the insertion query
StringJoiner sb = new StringJoiner(SPACE_CHAR);
sb.add("INSERT");
sb.add("INTO");
sb.add(escapedTableName);
sb.add("VALUES");
sb.add(OPEN_BRACKET);
for (int colNum = 0; colNum < totalColumns; colNum++) {
sb.add(QUESTION_MARK);

if (colNum < totalColumns - 1) {
sb.add(COMMA);
}
}
sb.add(CLOSE_BRACKET);
String sql = sb.toString();

dbPStmt.prepareStatement(sql);

// insert data
for (int i = 0; i < totalRows; i++) {
for (int colNum = 0; colNum < totalColumns; colNum++) {
if (passDataAsHex(colNum)) {
((PreparedStatement) dbPStmt.product()).setBytes(colNum + 1, ((byte[]) (getColumn(colNum).getRowValue(i))));
}
else {
dbPStmt.setObject(colNum + 1, String.valueOf(getColumn(colNum).getRowValue(i)));
}
}
dbPStmt.execute();
}

return true;
}
catch (SQLException ex) {
fail(ex.getMessage());
}
return false;
}

private void populateValues() {
// generate values for all columns
for (int i = 0; i < totalColumns; i++) {
Expand Down

0 comments on commit 9f2ef21

Please sign in to comment.