use of org.h2.result.Row in project h2database by h2database.
the class TestShell method test.
@Override
public void test() throws Exception {
Shell shell = new Shell();
ByteArrayOutputStream buff = new ByteArrayOutputStream();
shell.setOut(new PrintStream(buff));
shell.runTool("-url", "jdbc:h2:mem:", "-driver", "org.h2.Driver", "-user", "sa", "-password", "sa", "-properties", "null", "-sql", "select 'Hello ' || 'World' as hi");
String s = new String(buff.toByteArray());
assertContains(s, "HI");
assertContains(s, "Hello World");
assertContains(s, "(1 row, ");
shell = new Shell();
buff = new ByteArrayOutputStream();
shell.setOut(new PrintStream(buff));
shell.runTool("-help");
s = new String(buff.toByteArray());
assertContains(s, "Interactive command line tool to access a database using JDBC.");
test(true);
test(false);
}
use of org.h2.result.Row in project h2database by h2database.
the class TestShell method test.
private void test(final boolean commandLineArgs) throws IOException {
PipedInputStream testIn = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(testIn);
toolOut = new PrintStream(out, true);
out = new PipedOutputStream();
PrintStream testOut = new PrintStream(out, true);
toolIn = new PipedInputStream(out);
Task task = new Task() {
@Override
public void call() throws Exception {
try {
Shell shell = new Shell();
shell.setIn(toolIn);
shell.setOut(toolOut);
shell.setErr(toolOut);
if (commandLineArgs) {
shell.runTool("-url", "jdbc:h2:mem:", "-user", "sa", "-password", "sa");
} else {
shell.runTool();
}
} finally {
toolOut.close();
}
}
};
task.execute();
InputStreamReader reader = new InputStreamReader(testIn);
lineReader = new LineNumberReader(reader);
read("");
read("Welcome to H2 Shell");
read("Exit with");
if (!commandLineArgs) {
read("[Enter]");
testOut.println("jdbc:h2:mem:");
read("URL");
testOut.println("");
read("Driver");
testOut.println("sa");
read("User");
testOut.println("sa");
read("Password");
}
read("Commands are case insensitive");
read("help or ?");
read("list");
read("maxwidth");
read("autocommit");
read("history");
read("quit or exit");
read("");
testOut.println("history");
read("sql> No history");
testOut.println("1");
read("sql> Not found");
testOut.println("select 1 a;");
read("sql> A");
read("1");
read("(1 row,");
testOut.println("history");
read("sql> #1: select 1 a");
read("To re-run a statement, type the number and press and enter");
testOut.println("1");
read("sql> select 1 a");
read("A");
read("1");
read("(1 row,");
testOut.println("select 'x' || space(1000) large, 'y' small;");
read("sql> LARGE");
read("x");
read("(data is partially truncated)");
read("(1 row,");
testOut.println("select x, 's' s from system_range(0, 10001);");
read("sql> X | S");
for (int i = 0; i < 10000; i++) {
read((i + " ").substring(0, 4) + " | s");
}
for (int i = 10000; i <= 10001; i++) {
read((i + " ").substring(0, 5) + " | s");
}
read("(10002 rows,");
testOut.println("select error;");
read("sql> Error:");
if (read("").startsWith("Column \"ERROR\" not found")) {
read("");
}
testOut.println("create table test(id int primary key, name varchar)\n;");
read("sql> ...>");
testOut.println("insert into test values(1, 'Hello');");
read("sql>");
testOut.println("select null n, * from test;");
read("sql> N | ID | NAME");
read("null | 1 | Hello");
read("(1 row,");
// test history
for (int i = 0; i < 30; i++) {
testOut.println("select " + i + " ID from test;");
read("sql> ID");
read("" + i);
read("(1 row,");
}
testOut.println("20");
read("sql> select 10 ID from test");
read("ID");
read("10");
read("(1 row,");
testOut.println("maxwidth");
read("sql> Usage: maxwidth <integer value>");
read("Maximum column width is now 100");
testOut.println("maxwidth 80");
read("sql> Maximum column width is now 80");
testOut.println("autocommit");
read("sql> Usage: autocommit [true|false]");
read("Autocommit is now true");
testOut.println("autocommit false");
read("sql> Autocommit is now false");
testOut.println("autocommit true");
read("sql> Autocommit is now true");
testOut.println("list");
read("sql> Result list mode is now on");
testOut.println("select 1 first, 2 second;");
read("sql> FIRST : 1");
read("SECOND: 2");
read("(1 row, ");
testOut.println("select x from system_range(1, 3);");
read("sql> X: 1");
read("");
read("X: 2");
read("");
read("X: 3");
read("(3 rows, ");
testOut.println("select x, 2 as y from system_range(1, 3) where 1 = 0;");
read("sql> X");
read("Y");
read("(0 rows, ");
testOut.println("list");
read("sql> Result list mode is now off");
testOut.println("help");
read("sql> Commands are case insensitive");
read("help or ?");
read("list");
read("maxwidth");
read("autocommit");
read("history");
read("quit or exit");
read("");
testOut.println("exit");
read("sql>");
task.get();
}
use of org.h2.result.Row in project h2database by h2database.
the class TestFileLockSerialized method testConcurrentReadWrite.
private void testConcurrentReadWrite() throws Exception {
deleteDb("fileLockSerialized");
String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
// ;TRACE_LEVEL_SYSTEM_OUT=3
// String readUrl = writeUrl + ";ACCESS_MODE_DATA=R";
trace(" create database");
Class.forName("org.h2.Driver");
Connection conn = getConnection(writeUrl, "sa", "sa");
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key)");
Connection conn3 = getConnection(writeUrl, "sa", "sa");
PreparedStatement prep3 = conn3.prepareStatement("insert into test values(?)");
Connection conn2 = getConnection(writeUrl, "sa", "sa");
Statement stat2 = conn2.createStatement();
printResult(stat2, "select * from test");
stat2.execute("create local temporary table temp(name varchar) not persistent");
printResult(stat2, "select * from temp");
trace(" insert row 1");
stat.execute("insert into test values(1)");
trace(" insert row 2");
prep3.setInt(1, 2);
prep3.execute();
printResult(stat2, "select * from test");
printResult(stat2, "select * from temp");
conn.close();
conn2.close();
conn3.close();
}
use of org.h2.result.Row in project h2database by h2database.
the class MemoryFootprint method main.
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String... a) {
// System.getProperties().store(System.out, "");
print("Object", new Object());
print("Timestamp", new java.sql.Timestamp(0));
print("Date", new java.sql.Date(0));
print("Time", new java.sql.Time(0));
print("BigDecimal", new BigDecimal("0"));
print("BigInteger", new BigInteger("0"));
print("String", new String("Hello"));
print("Data", Data.create(null, 10));
print("Row", new RowImpl(new Value[0], 0));
System.out.println();
for (int i = 1; i < 128; i += i) {
System.out.println(getArraySize(1, i) + " bytes per p1[]");
print("boolean[" + i + "]", new boolean[i]);
System.out.println(getArraySize(2, i) + " bytes per p2[]");
print("char[" + i + "]", new char[i]);
print("short[" + i + "]", new short[i]);
System.out.println(getArraySize(4, i) + " bytes per p4[]");
print("int[" + i + "]", new int[i]);
print("float[" + i + "]", new float[i]);
System.out.println(getArraySize(8, i) + " bytes per p8[]");
print("long[" + i + "]", new long[i]);
print("double[" + i + "]", new double[i]);
System.out.println(getArraySize(Constants.MEMORY_POINTER, i) + " bytes per obj[]");
print("Object[" + i + "]", new Object[i]);
System.out.println();
}
}
use of org.h2.result.Row in project h2database by h2database.
the class TableFilter method getBestPlanItem.
/**
* Get the best plan item (index, cost) to use use for the current join
* order.
*
* @param s the session
* @param filters all joined table filters
* @param filter the current table filter index
* @param allColumnsSet the set of all columns
* @return the best plan item
*/
public PlanItem getBestPlanItem(Session s, TableFilter[] filters, int filter, HashSet<Column> allColumnsSet) {
PlanItem item1 = null;
SortOrder sortOrder = null;
if (select != null) {
sortOrder = select.getSortOrder();
}
if (indexConditions.isEmpty()) {
item1 = new PlanItem();
item1.setIndex(table.getScanIndex(s, null, filters, filter, sortOrder, allColumnsSet));
item1.cost = item1.getIndex().getCost(s, null, filters, filter, sortOrder, allColumnsSet);
}
int len = table.getColumns().length;
int[] masks = new int[len];
for (IndexCondition condition : indexConditions) {
if (condition.isEvaluatable()) {
if (condition.isAlwaysFalse()) {
masks = null;
break;
}
int id = condition.getColumn().getColumnId();
if (id >= 0) {
masks[id] |= condition.getMask(indexConditions);
}
}
}
PlanItem item = table.getBestPlanItem(s, masks, filters, filter, sortOrder, allColumnsSet);
item.setMasks(masks);
// The more index conditions, the earlier the table.
// This is to ensure joins without indexes run quickly:
// x (x.a=10); y (x.b=y.b) - see issue 113
item.cost -= item.cost * indexConditions.size() / 100 / (filter + 1);
if (item1 != null && item1.cost < item.cost) {
item = item1;
}
if (nestedJoin != null) {
setEvaluatable(true);
item.setNestedJoinPlan(nestedJoin.getBestPlanItem(s, filters, filter, allColumnsSet));
// TODO optimizer: calculate cost of a join: should use separate
// expected row number and lookup cost
item.cost += item.cost * item.getNestedJoinPlan().cost;
}
if (join != null) {
setEvaluatable(true);
do {
filter++;
} while (filters[filter] != join);
item.setJoinPlan(join.getBestPlanItem(s, filters, filter, allColumnsSet));
// TODO optimizer: calculate cost of a join: should use separate
// expected row number and lookup cost
item.cost += item.cost * item.getJoinPlan().cost;
}
return item;
}
Aggregations