/** * Create a new random index. * * @return the index */ Index newRandomIndex() { String indexName = "I_" + config.randomIdentifier(); int len = config.random().getLog(getColumnCount() - 1) + 1; boolean unique = config.random().getBoolean(50); Column[] cols = getRandomColumns(len); Index index = new Index(this, indexName, cols, unique); return index; }
public static void mainFunction() throws Exception { TestSynth bot = new TestSynth(); bot.connect("irc.freenode.net"); bot.joinChannel("#csf"); }
private static Object randomInt(TestSynth config) { int value; if (config.is(TestSynth.POSTGRESQL)) { value = config.random().getInt(1000000); } else { value = config.random().getRandomInt(); } return value; }
private static BigDecimal randomDecimal(TestSynth config, int precision, int scale) { int len = config.random().getLog(precision - scale) + scale; if (len == 0) { len++; } StringBuilder buff = new StringBuilder(); for (int i = 0; i < len; i++) { buff.append((char) ('0' + config.random().getInt(10))); } buff.insert(len - scale, '.'); if (config.random().getBoolean(20)) { buff.insert(0, '-'); } return new BigDecimal(buff.toString()); }
private String getTimestampSQL(Timestamp ts) { String s = "'" + ts.toString() + "'"; if (config.getMode() != TestSynth.HSQLDB) { s = "TIMESTAMP " + s; } return s; }
private String getTimeSQL(Time time) { String s = "'" + time.toString() + "'"; if (config.getMode() != TestSynth.HSQLDB) { s = "TIME " + s; } return s; }
private String getDateSQL(Date date) { String s = "'" + date.toString() + "'"; if (config.getMode() != TestSynth.HSQLDB) { s = "DATE " + s; } return s; }
private Connection getConnection() throws Exception { log("(getConnection to " + url + ");"); if (driver == null) { return config.getConnection("synth"); } Class.forName(driver); return DriverManager.getConnection(url, user, password); }
/** * Generate a random value. * * @param config the configuration * @param type the value type * @param precision the precision * @param scale the scale * @param mayBeNull if the value may be null or not * @return the value */ static Value getRandom(TestSynth config, int type, int precision, int scale, boolean mayBeNull) { Object data; if (mayBeNull && config.random().getBoolean(20)) { return new Value(config, type, null); } switch (type) { case Types.BIGINT: data = randomLong(config); break; case Types.DOUBLE: data = randomDouble(config); break; case Types.DECIMAL: data = randomDecimal(config, precision, scale); break; case Types.VARBINARY: case Types.BINARY: case Types.BLOB: data = randomBytes(config, precision); break; case Types.CLOB: case Types.VARCHAR: data = config.random().randomString(config.random().getInt(precision)); break; case Types.DATE: data = randomDate(config); break; case Types.TIME: data = randomTime(config); break; case Types.TIMESTAMP: data = randomTimestamp(config); break; case Types.INTEGER: data = randomInt(config); break; case Types.BOOLEAN: case Types.BIT: data = config.random().getBoolean(50) ? "TRUE" : "FALSE"; break; default: throw new AssertionError("type=" + type); } return new Value(config, type, data); }
/** * Get a random column of the specified type. * * @param type the type * @return the column or null if no such column was found */ Column getRandomColumnOfType(int type) { ArrayList<Column> list = New.arrayList(); for (Column col : columns) { if (col.getType() == type) { list.add(col); } } if (list.size() == 0) { return null; } return list.get(config.random().getInt(list.size())); }
/** * Get a random column that can be used in a condition. * * @return the column */ Column getRandomConditionColumn() { ArrayList<Column> list = New.arrayList(); for (Column col : columns) { if (Column.isConditionType(config, col.getType())) { list.add(col); } } if (list.size() == 0) { return null; } return list.get(config.random().getInt(list.size())); }
/** * Create a new random table. * * @param config the configuration * @return the table */ static Table newRandomTable(TestSynth config) { Table table = new Table(config); table.name = "T_" + config.randomIdentifier(); // there is a difference between local temp tables for persistent and // in-memory mode // table.temporary = config.random().getBoolean(10); // if(table.temporary) { // if(config.getMode() == TestSynth.H2_MEM) { // table.globalTemporary = false; // } else { // table.globalTemporary = config.random().getBoolean(50); // } // } int len = config.random().getLog(10) + 1; table.columns = new Column[len]; for (int i = 0; i < len; i++) { Column col = Column.getRandomColumn(config); table.columns[i] = col; } if (config.random().getBoolean(90)) { int pkLen = config.random().getLog(len); table.primaryKeys = new Column[pkLen]; for (int i = 0; i < pkLen; i++) { Column pk = null; do { pk = table.columns[config.random().getInt(len)]; } while (pk.getPrimaryKey()); table.primaryKeys[i] = pk; pk.setPrimaryKey(true); pk.setNullable(false); } } return table; }
/** * Get a number of random column from this table. * * @param len the column count * @return the columns */ Column[] getRandomColumns(int len) { int[] index = new int[columns.length]; for (int i = 0; i < columns.length; i++) { index[i] = i; } for (int i = 0; i < columns.length; i++) { int temp = index[i]; int r = index[config.random().getInt(columns.length)]; index[i] = index[r]; index[r] = temp; } Column[] c = new Column[len]; for (int i = 0; i < len; i++) { c[i] = columns[index[i]]; } return c; }
private static Timestamp randomTimestamp(TestSynth config) { return config.random().randomTimestamp(); }
private static Long randomLong(TestSynth config) { return Long.valueOf(config.random().getInt(1000)); }
private static Double randomDouble(TestSynth config) { return config.random().getInt(100) / 10.; }
private static Date randomDate(TestSynth config) { return config.random().randomDate(); }
private static byte[] randomBytes(TestSynth config, int max) { int len = config.random().getLog(max); byte[] data = new byte[len]; config.random().getBytes(data); return data; }
private void log(String s) { config.log(id, s); }
Column getRandomColumn() { return columns[config.random().getInt(columns.length)]; }