0

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.

4

2 回答 2

0
private String generateRandomSQL(ReadTableConnectionInfo table) {
    Random r = new Random(table.getColumns.size());
    int rNumber = r.nextInt();

    List<String> suffledColumns = new ArrayList<String>(table.getColumns());
    Collections.shuffle(suffledColumns);

    String columnsList = "";

    for(int i = 0 ; i < rNumber ; i ++){
        columnsList += ("," + shuffledColumns.get(i));
    }

    final String sql = "SELECT ID, CREATION_DATE, LAST_MODIFIED_DATE" + columnsList +"  from " + table.getTableName() + " where id = ?";

        return sql;
}
于 2013-02-24T07:54:11.090 回答
0

Something like that :

   String colPart="SELECT ID, CREATION_DATE, LAST_MODIFIED_DATE"
   Random rNo=new Random();
   int noOfCols=columns.size();
   int colCount=rNo.nextInt(noOfCols-3);
   for(int i=0;i<colCount;i++){
   string colToAdd=columns.get(3+rNo.nextInt(noOfCols-3));
   if(!colPart.contains(colToAdd))    
       colPart+=","+colToAdd;
   }        
    final String sql =colPart+" from "+table.getTableName+" where id = ?";
于 2013-02-24T08:02:49.587 回答