use of io.questdb.std.Rnd in project questdb by bluestreak01.
the class InsertTest method testBindVariableInsert.
private void testBindVariableInsert(int partitionBy, TimestampFunction timestampFunction, boolean initBindVariables, boolean columnSet) throws Exception {
assertMemoryLeak(() -> {
CairoTestUtils.createAllTableWithNewTypes(configuration, partitionBy);
// this is BLOB
byte[] blob = new byte[500];
TestBinarySequence bs = new TestBinarySequence();
bs.of(blob);
Rnd rnd = new Rnd();
if (initBindVariables) {
// this is type declaration to have query compile correctly
bindVariableService.setInt(0, 0);
bindVariableService.setShort(1, (short) 10);
bindVariableService.setByte(2, (byte) 91);
bindVariableService.setDouble(3, 9.2);
bindVariableService.setFloat(4, 5.6f);
bindVariableService.setLong(5, 99901);
bindVariableService.setStr(6, "hello kitty");
bindVariableService.setStr(7, "sym?");
bindVariableService.setBoolean(8, true);
bindVariableService.setBin(9, bs);
bindVariableService.setDate(10, 1234L);
bindVariableService.setLong256(11, 1, 2, 3, 4);
bindVariableService.setChar(12, 'A');
bindVariableService.setTimestamp(13, timestampFunction.getTimestamp());
}
final String sql;
if (columnSet) {
sql = "insert into all2 (" + "int, " + "short, " + "byte, " + "double, " + "float, " + "long, " + "str, " + "sym, " + "bool, " + "bin, " + "date, " + "long256, " + "chr, " + "timestamp" + ") values (" + "$1, " + "$2, " + "$3, " + "$4, " + "$5, " + "$6, " + "$7, " + "$8, " + "$9, " + "$10, " + "$11, " + "$12, " + "$13, " + "$14)";
} else {
sql = "insert into all2 values (" + "$1, " + "$2, " + "$3, " + "$4, " + "$5, " + "$6, " + "$7, " + "$8, " + "$9, " + "$10, " + "$11, " + "$12, " + "$13, " + "$14)";
}
final CompiledQuery cq = compiler.compile(sql, sqlExecutionContext);
Assert.assertEquals(CompiledQuery.INSERT, cq.getType());
InsertStatement insert = cq.getInsertStatement();
try (InsertMethod method = insert.createMethod(sqlExecutionContext)) {
for (int i = 0; i < 10_000; i++) {
bindVariableService.setInt(0, rnd.nextInt());
bindVariableService.setShort(1, rnd.nextShort());
bindVariableService.setByte(2, rnd.nextByte());
bindVariableService.setDouble(3, rnd.nextDouble());
bindVariableService.setFloat(4, rnd.nextFloat());
bindVariableService.setLong(5, rnd.nextLong());
bindVariableService.setStr(6, rnd.nextChars(6));
bindVariableService.setStr(7, rnd.nextChars(1));
bindVariableService.setBoolean(8, rnd.nextBoolean());
rnd.nextBytes(blob);
bindVariableService.setBin(9, bs);
bindVariableService.setDate(10, rnd.nextLong());
bindVariableService.setLong256(11, rnd.nextLong(), rnd.nextLong(), rnd.nextLong(), rnd.nextLong());
bindVariableService.setChar(12, rnd.nextChar());
bindVariableService.setTimestamp(13, timestampFunction.getTimestamp());
method.execute();
}
method.commit();
}
rnd.reset();
try (TableReader reader = engine.getReader(sqlExecutionContext.getCairoSecurityContext(), "all2")) {
final TableReaderRecordCursor cursor = reader.getCursor();
final Record record = cursor.getRecord();
while (cursor.hasNext()) {
Assert.assertEquals(rnd.nextInt(), record.getInt(0));
Assert.assertEquals(rnd.nextShort(), record.getShort(1));
Assert.assertEquals(rnd.nextByte(), record.getByte(2));
Assert.assertEquals(rnd.nextDouble(), record.getDouble(3), 0.0001);
Assert.assertEquals(rnd.nextFloat(), record.getFloat(4), 0.000001);
Assert.assertEquals(rnd.nextLong(), record.getLong(5));
TestUtils.assertEquals(rnd.nextChars(6), record.getStr(6));
TestUtils.assertEquals(rnd.nextChars(1), record.getSym(7));
Assert.assertEquals(rnd.nextBoolean(), record.getBool(8));
rnd.nextBytes(blob);
BinarySequence binarySequence = record.getBin(9);
Assert.assertEquals(blob.length, binarySequence.length());
for (int j = 0, m = blob.length; j < m; j++) {
Assert.assertEquals(blob[j], binarySequence.byteAt(j));
}
Assert.assertEquals(rnd.nextLong(), record.getDate(10));
Long256 long256 = record.getLong256A(11);
Assert.assertEquals(rnd.nextLong(), long256.getLong0());
Assert.assertEquals(rnd.nextLong(), long256.getLong1());
Assert.assertEquals(rnd.nextLong(), long256.getLong2());
Assert.assertEquals(rnd.nextLong(), long256.getLong3());
Assert.assertEquals(rnd.nextChar(), record.getChar(12));
}
}
});
}
Aggregations