【发布时间】:2013-02-27 02:45:53
【问题描述】:
我有一个名为TableConnectionInfo 的类,其中包含有关表的详细信息,例如tableName,它是columns。我编写了一个方法,我应该使用每个表的table.getColumns() 方法中的列值来生成随机选择 sql。
假设 table1 有 10 列,例如-
col1
col2
col3
col4
col5
col6
col7
col8
col9
col10
下面是我尝试使用table.getColumns() 生成随机选择sql 的方法。这里table 是TableConnectionInfo 对象。
private static Random random = new SecureRandom();
private String generateRandomSQL(TableConnectionInfo table) {
int rNumber = random.nextInt(table.getColumns().size());
List<String> shuffledColumns = new ArrayList<String>(table.getColumns());
Collections.shuffle(shuffledColumns);
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;
}
下面是我的TableConncectionInfo 类,它将包含tableName,它是columns 列表
public class TableConnectionInfo {
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;
}
}
问题陈述:-
所以上述方法的问题是,它会随机打乱所有列,我会得到这样的结果-
`SELECT ID, CREATION_DATE, LAST_MODIFIED_DATE, col3, col1, col5 from table1 where id = ?`
`SELECT ID, CREATION_DATE, LAST_MODIFIED_DATE, col7, col2, col3, col1 from table1 where id = ?`
`SELECT ID, CREATION_DATE, LAST_MODIFIED_DATE, col8, col1, col2, col6 from table1 where id = ?`
... Other possible combinations
我正在寻找这样的东西。在 ArrayList 中插入列的方式- 假设这是顺序-
col1, col2, col3, col4, col5, col6, col7, col8, col9, col10
那么我的SELECT SQL 中的列应该是相同的顺序。意义-
`SELECT ID, CREATION_DATE, LAST_MODIFIED_DATE, col1, col3, col5 from table1 where id = ?`
`SELECT ID, CREATION_DATE, LAST_MODIFIED_DATE, col7, col8, col9, col10 from table1 where id = ?`
`SELECT ID, CREATION_DATE, LAST_MODIFIED_DATE, col1, col4, col5, col6, col10 from table1 where id = ?`
它不应该是随机的,比如col1,然后是col5,然后是col2(这是错误的)。应该是-
col5, col7, col10
col3, col6, col8, col10
other options
基本上,我希望在我的 SELECT sql 中以插入 ArrayList 的方式但以随机顺序插入列。 Like- 按插入顺序选择任意 4 个或按插入顺序选择任意 6 个或按插入顺序选择任意 7 个
【问题讨论】:
-
你想要它们在你的数组列表中还是随机顺序?
-
如果您希望列集合按定义的顺序排列,为什么要对它们进行洗牌?
-
@MattBusche,我想要它们,因为它们在我的数组列表中,但顺序是随机的。假设
col1、col2、col3、col4是我的 ArrayList 中的顺序。然后我需要这样的东西 -col1, col4或col2, col3但不是col4, col1或col3 , col1。 -
@Perception,我这样做是因为我正在进行 LnP 测试,因此我们正在尝试复制我们即将预期的生产场景。所以这就是我打算这样做的原因。
-
恕我直言,您需要执行以下步骤,1. 创建一个新列表 p,其中包含列数组中的项目。 2. 随机播放 p。 3. 从 p 中创建一个 rNumber 项的子列表 4. 使用 columns 数组的排序顺序对其进行排序。