I have a class named ReadTableConnectionInfo
which contains details about the table such as tableName
and it's columns
. I have written a multithreaded code in which I am supposed to generate a random select sql by using the column values from table.getColumns()
.
Suppose for table1 it has 8 columns such as-
ID
CREATION
DATE
ACCOUNT
ADVERTISE
SELLERS
GEOGRAPHIC
DEMOGRAPHIC
Below is my code from which I am trying generate random select sql using the columns. Here table
is the ReadTableConnectionInfo
object.
@Override
public void run() {
while (System.currentTimeMillis() <= 30 minutes) {
final String selectSql = generateRandomSQL(table);
preparedStatement = entry.getValue().prepareCall(selectSql);
preparedStatement.setString(id);
rs = preparedStatement.executeQuery();
}
}
/**
* A simple method that will construct the SQL
* and return back
* @param columns
* @return sql
*/
private String generateRandomSQL(ReadTableConnectionInfo table) {
/* generate random columns from
* `table.getColumns`, not sure how to make that
*/
final String sql = "SELECT ID, CREATION_DATE, LAST_MODIFIED_DATE, from "+table.getTableName+" where id = ?";
return sql;
}
Below is my ReadTableConncectionInfo class
which will hold tableName and columns list
public class ReadTableConnectionInfo {
public String tableName;
public ArrayList<String> columns;
public ArrayList<String> getColumns() {
return columns;
}
public void setColumns(ArrayList<String> columns) {
this.columns = columns;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
}
Now I need to generate random select SQL in generateRandomSQL
method from the table.getColumns()
list. In all my SELECT sql, I will always have ID, CREATION and DATE
and apart from that, it should be random
Sample SQL Example-
`SELECT ID, CREATION, DATE, ACCOUNT from table1 where id = ?`
`SELECT ID, CREATION, DATE, ACCOUNT, ADVERTISE from table1 where id = ?`
`SELECT ID, CREATION, DATE, ACCOUNT, SELLERS from table1 where id = ?`
`SELECT ID, CREATION, DATE, GEOGRAPHIC, DEMOGRAPHIC, ADVERTISE from table1 where id = ?`
`Other possible permutations`
Can anyone help me in building random SELECT sql with ID, CREATION and DATE always in there? Any suggestions will be of great help.