提交 3e2897c4 authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 7db34fd8
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.test.TestBase;
public class TestAutoRecompile extends TestBase {
public void test() throws Exception {
deleteDb("autoRecompile");
Connection conn=getConnection("autoRecompile");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY)");
PreparedStatement prep = conn.prepareStatement("SELECT * FROM TEST");
check(prep.executeQuery().getMetaData().getColumnCount(), 1);
stat.execute("ALTER TABLE TEST ADD COLUMN NAME VARCHAR(255)");
check(prep.executeQuery().getMetaData().getColumnCount(), 2);
stat.execute("DROP TABLE TEST");
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, X INT, Y INT)");
check(prep.executeQuery().getMetaData().getColumnCount(), 3);
// TODO test auto-recompile with insert..select, views and so on
prep = conn.prepareStatement("INSERT INTO TEST VALUES(1, 2, 3)");
stat.execute("ALTER TABLE TEST ADD COLUMN Z INT");
try {
prep.execute();
error("must fail");
} catch(SQLException e) {
checkNotGeneralException(e);
}
try {
prep.execute();
error("must fail");
} catch(SQLException e) {
checkNotGeneralException(e);
}
conn.close();
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import org.h2.test.TestBase;
import org.h2.util.MemoryUtils;
public class TestBigDb extends TestBase {
public void test() throws Exception {
if(config.memory) {
return;
}
if(config.networked && config.big) {
return;
}
testLargeTable();
testInsert();
testLeftSummary();
}
private void testLargeTable() throws Exception {
deleteDb("bigDb");
Connection conn = getConnection("bigDb");
Statement stat = conn.createStatement();
stat.execute(
"CREATE CACHED TABLE PAB_ARTLEV("+
"MUTATIECODE CHAR(1) DEFAULT CAST(RAND()*9 AS INT),"+
"PRDCODE CHAR(20) DEFAULT SECURE_RAND(10),"+
"ORGCODESUPPLIER CHAR(13) DEFAULT SECURE_RAND(6),"+
"PRDCODEGTIN CHAR(14) DEFAULT SECURE_RAND(7),"+
"PRDCODEMF CHAR(20) DEFAULT SECURE_RAND(10),"+
"ORGCODEMF CHAR(13) DEFAULT SECURE_RAND(6),"+
"SUBSTITUTEDBY CHAR(20) DEFAULT SECURE_RAND(10),"+
"SUBSTITUTEDBYGTIN CHAR(14) DEFAULT SECURE_RAND(7),"+
"SUBSTITUTIONFOR CHAR(20) DEFAULT SECURE_RAND(10),"+
"SUBSTITUTIONFORGTIN CHAR(14) DEFAULT SECURE_RAND(7),"+
"VERWERKBAAR CHAR(2) DEFAULT SECURE_RAND(1),"+
"BESTELBAAR CHAR(2) DEFAULT SECURE_RAND(1),"+
"AANTALGEBRUIKSEENHEDEN DECIMAL(7,2) DEFAULT RAND(),"+
"PRIMARYUNITCODE CHAR(3) DEFAULT SECURE_RAND(1),"+
"RATEPRICEORDERUNIT DECIMAL(9,3) DEFAULT RAND(),"+
"ORDERUNITCODE CHAR(3) DEFAULT SECURE_RAND(1),"+
"ORDERQTYMIN DECIMAL(6,1) DEFAULT RAND(),"+
"ORDERQTYLOTSIZE DECIMAL(6,1) DEFAULT RAND(),"+
"ORDERUNITCODE2 CHAR(3) DEFAULT SECURE_RAND(1),"+
"PRICEGROUP CHAR(20) DEFAULT SECURE_RAND(10),"+
"LEADTIME INTEGER DEFAULT RAND(),"+
"LEADTIMEUNITCODE CHAR(3) DEFAULT SECURE_RAND(1),"+
"PRDGROUP CHAR(10) DEFAULT SECURE_RAND(5),"+
"WEIGHTGROSS DECIMAL(7,3) DEFAULT RAND(),"+
"WEIGHTUNITCODE CHAR(3) DEFAULT SECURE_RAND(1),"+
"PACKUNITCODE CHAR(3) DEFAULT SECURE_RAND(1),"+
"PACKLENGTH DECIMAL(7,3) DEFAULT RAND(),"+
"PACKWIDTH DECIMAL(7,3) DEFAULT RAND(),"+
"PACKHEIGHT DECIMAL(7,3) DEFAULT RAND(),"+
"SIZEUNITCODE CHAR(3) DEFAULT SECURE_RAND(1),"+
"STATUCCODE CHAR(3) DEFAULT SECURE_RAND(1),"+
"INTRASTATCODE CHAR(12) DEFAULT SECURE_RAND(6),"+
"PRDTITLE CHAR(50) DEFAULT SECURE_RAND(25),"+
"VALIDFROM DATE DEFAULT NOW(),"+
"MUTATIEDATUM DATE DEFAULT NOW())");
int len = getSize(10, 50000);
try {
PreparedStatement prep = conn.prepareStatement("INSERT INTO PAB_ARTLEV(PRDCODE) VALUES('abc' || ?)");
long time = System.currentTimeMillis();
for(int i=0; i<len; i++) {
if((i % 1000) == 0) {
long t = System.currentTimeMillis();
if(t-time > 1000) {
time = t;
int free = MemoryUtils.getMemoryFree();
System.out.println("i: " + i + " free: " + free + " used: " + MemoryUtils.getMemoryUsed());
}
}
prep.setInt(1, i);
prep.execute();
}
stat.execute("CREATE INDEX IDX_PAB_ARTLEV_PRDCODE ON PAB_ARTLEV(PRDCODE)");
ResultSet rs = stat.executeQuery("SELECT * FROM PAB_ARTLEV");
int columns = rs.getMetaData().getColumnCount();
while(rs.next()) {
for(int i=0; i<columns; i++) {
rs.getString(i+1);
}
}
} catch(OutOfMemoryError e) {
e.printStackTrace();
conn.close();
throw e;
}
conn.close();
}
private void testLeftSummary() throws Exception {
deleteDb("bigDb");
Connection conn = getConnection("bigDb");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT, NEG INT AS -ID, NAME VARCHAR, PRIMARY KEY(ID, NAME))");
stat.execute("CREATE INDEX IDXNEG ON TEST(NEG, NAME)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(ID, NAME) VALUES(?, '1234567890')");
int len = getSize(10, 1000);
int block = getSize(3, 10);
int left, x = 0;
for(int i=0; i<len; i++) {
left = x+block/2;
for(int j=0; j<block; j++) {
prep.setInt(1, x++);
prep.execute();
}
stat.execute("DELETE FROM TEST WHERE ID>" + left);
ResultSet rs = stat.executeQuery("SELECT COUNT(*) FROM TEST");
rs.next();
int count = rs.getInt(1);
trace("count: " + count);
}
conn.close();
}
private void testInsert() throws Exception {
deleteDb("bigDb");
Connection conn = getConnection("bigDb");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(NAME) VALUES('Hello World')");
int len = getSize(1000, 10000);
long time = System.currentTimeMillis();
for(int i=0; i<len; i++) {
if(i % 1000 == 0) {
long t = System.currentTimeMillis();
time = t;
trace("rows:" + i + " time:" + (t-time));
Thread.yield();
}
prep.execute();
}
conn.close();
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.*;
import org.h2.test.TestBase;
public class TestBigResult extends TestBase {
public void test() throws Exception {
if(config.memory) {
return;
}
testLimitBufferedResult();
testOrderGroup();
}
private void testLimitBufferedResult() throws Exception {
deleteDb("bigResult");
Connection conn = getConnection("bigResult");
Statement stat = conn.createStatement();
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(ID INT)");
for(int i=0; i<200; i++) {
stat.execute("INSERT INTO TEST(ID) VALUES("+i+")");
}
stat.execute("SET MAX_MEMORY_ROWS 100");
ResultSet rs;
rs = stat.executeQuery("select id from test order by id limit 10 offset 85");
for(int i=85; rs.next(); i++) {
check(i, rs.getInt(1));
}
rs = stat.executeQuery("select id from test order by id limit 10 offset 95");
for(int i=95; rs.next(); i++) {
check(i, rs.getInt(1));
}
rs = stat.executeQuery("select id from test order by id limit 10 offset 105");
for(int i=105; rs.next(); i++) {
check(i, rs.getInt(1));
}
conn.close();
}
private void testOrderGroup() throws Exception {
deleteDb("bigResult");
Connection conn = getConnection("bigResult");
Statement stat = conn.createStatement();
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(" + "ID INT PRIMARY KEY, "
+ "Name VARCHAR(255), "
+ "FirstName VARCHAR(255), "
+ "Points INT,"
+ "LicenseID INT)");
int len = getSize(10, 5000);
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?, ?, ?, ?)");
for(int i=0; i<len; i++) {
prep.setInt(1, i);
prep.setString(2, "Name " + i);
prep.setString(3, "First Name " + i);
prep.setInt(4, i*10);
prep.setInt(5, i*i);
prep.execute();
}
conn.close();
conn = getConnection("bigResult");
stat = conn.createStatement();
stat.setMaxRows(len + 1);
ResultSet rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
for(int i=0; i<len; i++) {
rs.next();
check(i, rs.getInt(1));
check("Name " + i, rs.getString(2));
check("First Name " + i, rs.getString(3));
check(i*10, rs.getInt(4));
check(i*i, rs.getInt(5));
}
stat.setMaxRows(len + 1);
rs = stat.executeQuery("SELECT * FROM TEST WHERE ID >= 1000 ORDER BY ID");
for(int i=1000; i<len; i++) {
rs.next();
check(i, rs.getInt(1));
check("Name " + i, rs.getString(2));
check("First Name " + i, rs.getString(3));
check(i*10, rs.getInt(4));
check(i*i, rs.getInt(5));
}
stat.execute("SET MAX_MEMORY_ROWS 2");
rs = stat.executeQuery("SELECT Name, SUM(ID) FROM TEST GROUP BY NAME");
while(rs.next()) {
rs.getString(1);
rs.getInt(2);
}
conn.setAutoCommit(false);
stat.setMaxRows(0);
stat.execute("SET MAX_MEMORY_ROWS 0");
stat.execute("CREATE TABLE DATA(ID INT, NAME VARCHAR_IGNORECASE(255))");
prep = conn.prepareStatement("INSERT INTO DATA VALUES(?, ?)");
for(int i=0; i<len; i++) {
prep.setInt(1, i);
prep.setString(2, ""+i/200);
prep.execute();
}
Statement s2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = s2.executeQuery("SELECT NAME FROM DATA");
rs.last();
conn.setAutoCommit(true);
rs = s2.executeQuery("SELECT NAME FROM DATA ORDER BY ID");
while(rs.next()) {
// do nothing
}
conn.close();
}
}
差异被折叠。
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.*;
import org.h2.test.TestBase;
public class TestCheckpoint extends TestBase {
public void test() throws Exception {
// TODO test checkpoint with rollback, not only just run the command
deleteDb("checkpoint");
Connection c0 = getConnection("checkpoint");
Statement s0 = c0.createStatement();
Connection c1 = getConnection("checkpoint");
Statement s1 = c1.createStatement();
s1.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
s1.execute("INSERT INTO TEST VALUES(1, 'Hello')");
s0.execute("CHECKPOINT");
s1.execute("INSERT INTO TEST VALUES(2, 'World')");
c1.setAutoCommit(false);
s1.execute("INSERT INTO TEST VALUES(3, 'Maybe')");
s0.execute("CHECKPOINT");
s1.execute("INSERT INTO TEST VALUES(4, 'Or not')");
s0.execute("CHECKPOINT");
s1.execute("INSERT INTO TEST VALUES(5, 'ok yes')");
s1.execute("COMMIT");
s0.execute("CHECKPOINT");
c0.close();
c1.close();
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.test.TestBase;
import org.h2.tools.CreateCluster;
import org.h2.tools.DeleteDbFiles;
import org.h2.tools.Server;
public class TestCluster extends TestBase {
public void test() throws Exception {
if(config.memory || config.networked) {
return;
}
DeleteDbFiles.main(new String[]{"-dir", BASE_DIR + "/node1", "-quiet"});
DeleteDbFiles.main(new String[]{"-dir", BASE_DIR + "/node2", "-quiet"});
// create the master database
Connection conn;
Class.forName("org.h2.Driver");
conn = DriverManager.getConnection("jdbc:h2:file:" + BASE_DIR + "/node1/test", "sa", "");
Statement stat;
stat = conn.createStatement();
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
int len = getSize(10, 1000);
for(int i=0; i<len; i++) {
prep.setInt(1, i);
prep.setString(2, "Data" + i);
prep.executeUpdate();
}
conn.close();
CreateCluster.main(new String[]{
"-urlSource", "jdbc:h2:file:"+ BASE_DIR + "/node1/test",
"-urlTarget", "jdbc:h2:file:"+BASE_DIR + "/node2/test",
"-user", "sa",
"-serverlist", "localhost:9091,localhost:9092"
});
Server n1 = org.h2.tools.Server.createTcpServer(new String[]{"-tcpPort", "9091", "-baseDir", BASE_DIR + "/node1"}).start();
Server n2 = org.h2.tools.Server.createTcpServer(new String[]{"-tcpPort", "9092", "-baseDir", BASE_DIR + "/node2"}).start();
try {
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9091/test", "sa", "");
error("should not be able to connect in standalone mode");
} catch(SQLException e) {
checkNotGeneralException(e);
}
try {
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9092/test", "sa", "");
error("should not be able to connect in standalone mode");
} catch(SQLException e) {
checkNotGeneralException(e);
}
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9091,localhost:9092/test", "sa", "");
stat = conn.createStatement();
check(conn, len);
conn.close();
// n1.stop();
// n2.stop();
// n1 = org.h2.tools.Server.startTcpServer(new String[]{"-tcpPort", "9091", "-baseDir", BASE_DIR + "/node1"});
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9091/test;CLUSTER=''", "sa", "");
check(conn, len);
conn.close();
n1.stop();
// n2 = org.h2.tools.Server.startTcpServer(new String[]{"-tcpPort", "9092", "-baseDir", BASE_DIR + "/node2"});
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9092/test;CLUSTER=''", "sa", "");
check(conn, len);
conn.close();
n2.stop();
}
void check(Connection conn, int len) throws Exception {
PreparedStatement prep = conn.prepareStatement("SELECT * FROM TEST WHERE ID=?");
for(int i=0; i<len; i++) {
prep.setInt(1, i);
ResultSet rs = prep.executeQuery();
rs.next();
check(rs.getString(2), "Data"+i);
checkFalse(rs.next());
}
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import org.h2.test.TestBase;
public class TestCompatibility extends TestBase {
private Connection conn;
public void test() throws Exception {
deleteDb("compatibility");
conn = getConnection("compatibility");
testHsqlDb();
testMySQL();
conn.close();
}
private void testHsqlDb() throws Exception {
Statement stat = conn.createStatement();
stat.execute("DROP TABLE TEST IF EXISTS; CREATE TABLE TEST(ID INT PRIMARY KEY); ");
stat.execute("CALL CURRENT_TIME");
stat.execute("CALL CURRENT_TIMESTAMP");
stat.execute("CALL CURRENT_DATE");
stat.execute("CALL SYSDATE");
stat.execute("CALL TODAY");
stat.execute("DROP TABLE TEST IF EXISTS");
stat.execute("CREATE TABLE TEST(ID INT)");
stat.execute("INSERT INTO TEST VALUES(1)");
PreparedStatement prep = conn.prepareStatement(
"SELECT LIMIT ? 1 ID FROM TEST"
);
prep.setInt(1, 2);
prep.executeQuery();
stat.execute("DROP TABLE TEST IF EXISTS");
}
private void testMySQL() throws Exception {
Statement stat = conn.createStatement();
stat.execute("SELECT 1");
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(ID INT, NAME VARCHAR)");
stat.execute("INSERT INTO TEST VALUES(1, 'Hello'), (2, 'World')");
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.io.File;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import org.h2.test.TestBase;
import org.h2.tools.Csv;
public class TestCsv extends TestBase {
public void test() throws Exception {
testAsTable();
testWriteRead();
testRead();
}
private void testAsTable() throws Exception {
deleteDb("csv");
Connection conn = getConnection("csv");
Statement stat = conn.createStatement();
stat.execute("call csvwrite('test.csv', 'select 1 id, ''Hello'' name')");
ResultSet rs = stat.executeQuery("select name from csvread('test.csv')");
check(rs.next());
check(rs.getString(1), "Hello");
checkFalse(rs.next());
rs = stat.executeQuery("call csvread('test.csv')");
check(rs.next());
check(rs.getInt(1), 1);
check(rs.getString(2), "Hello");
checkFalse(rs.next());
new File("test.csv").delete();
conn.close();
}
public void testRead() throws Exception {
File f = new File(BASE_DIR + "/test.csv");
f.delete();
RandomAccessFile file = new RandomAccessFile(f, "rw");
file.write("a,b,c,d\n201,-2,0,18\n, \"abc\"\"\" ,,\"\"\n 1 ,2 , 3, 4 \n5, 6, 7, 8".getBytes());
file.close();
ResultSet rs = Csv.getInstance().read(BASE_DIR + "/test.csv", null, "UTF8");
ResultSetMetaData meta = rs.getMetaData();
check(meta.getColumnCount(), 4);
check(meta.getColumnLabel(1), "a");
check(meta.getColumnLabel(2), "b");
check(meta.getColumnLabel(3), "c");
check(meta.getColumnLabel(4), "d");
check(rs.next());
check(rs.getString(1), "201");
check(rs.getString(2), "-2");
check(rs.getString(3), "0");
check(rs.getString(4), "18");
check(rs.next());
check(rs.getString(1), null);
check(rs.getString(2), "abc\"");
check(rs.getString(3), null);
check(rs.getString(4), "");
check(rs.next());
check(rs.getString(1), "1");
check(rs.getString(2), "2");
check(rs.getString(3), "3");
check(rs.getString(4), "4");
check(rs.next());
check(rs.getString(1), "5");
check(rs.getString(2), "6");
check(rs.getString(3), "7");
check(rs.getString(4), "8");
checkFalse(rs.next());
// a,b,c,d
// 201,-2,0,18
// 201,2,0,18
// 201,2,0,18
// 201,2,0,18
// 201,2,0,18
// 201,2,0,18
}
public void testWriteRead() throws Exception {
deleteDb("csv");
Connection conn = getConnection("csv");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)");
int len = 100;
for(int i=0; i<len; i++) {
stat.execute("INSERT INTO TEST(NAME) VALUES('Ruebezahl')");
}
Csv.getInstance().write(conn, BASE_DIR + "/test.csv", "SELECT * FROM TEST", "UTF8");
ResultSet rs = Csv.getInstance().read(BASE_DIR + "/test.csv", null, "UTF8");
// stat.execute("CREATE ALIAS CSVREAD FOR \"org.h2.tools.Csv.read\"");
ResultSetMetaData meta = rs.getMetaData();
check(2, meta.getColumnCount());
for(int i=0; i<len; i++) {
rs.next();
check(rs.getString("ID"), "" + (i+1));
check(rs.getString("NAME"), "Ruebezahl");
}
checkFalse(rs.next());
conn.close();
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import org.h2.test.TestBase;
import org.h2.tools.SimpleResultSet;
public class TestFunctions extends TestBase {
Statement stat;
public void test() throws Exception {
deleteDb("functions");
Connection conn = getConnection("functions");
stat = conn.createStatement();
test("abs(null)", null);
test("abs(1)", "1");
test("abs(1)", "1");
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
stat.execute("CREATE ALIAS ADDROW FOR \"" + getClass().getName() + ".addRow\"");
ResultSet rs;
rs = stat.executeQuery("CALL ADDROW(1, 'Hello')");
rs.next();
check(rs.getInt(1), 1);
rs = stat.executeQuery("SELECT * FROM TEST");
rs.next();
check(rs.getInt(1), 1);
check(rs.getString(2), "Hello");
checkFalse(rs.next());
rs = stat.executeQuery("CALL ADDROW(2, 'World')");
stat.execute("CREATE ALIAS SEL FOR \"" + getClass().getName() + ".select\"");
rs = stat.executeQuery("CALL SEL('SELECT * FROM TEST ORDER BY ID')");
check(rs.getMetaData().getColumnCount(), 2);
rs.next();
check(rs.getInt(1), 1);
check(rs.getString(2), "Hello");
rs.next();
check(rs.getInt(1), 2);
check(rs.getString(2), "World");
checkFalse(rs.next());
rs = stat.executeQuery("SELECT NAME FROM SEL('SELECT * FROM TEST ORDER BY NAME') ORDER BY NAME DESC");
check(rs.getMetaData().getColumnCount(), 1);
rs.next();
check(rs.getString(1), "World");
rs.next();
check(rs.getString(1), "Hello");
checkFalse(rs.next());
rs = stat.executeQuery("SELECT SEL('SELECT * FROM TEST WHERE ID=' || ID) FROM TEST ORDER BY ID");
check(rs.getMetaData().getColumnCount(), 1);
rs.next();
check("((1, Hello))", rs.getString(1));
rs.next();
check("((2, World))", rs.getString(1));
checkFalse(rs.next());
rs = stat.executeQuery("SELECT SEL('SELECT * FROM TEST ORDER BY ID') FROM DUAL");
check(rs.getMetaData().getColumnCount(), 1);
rs.next();
check("((1, Hello), (2, World))", rs.getString(1));
checkFalse(rs.next());
try {
rs = stat.executeQuery("CALL SEL('ERROR')");
error("expected error");
} catch (SQLException e) {
check("42001", e.getSQLState());
}
stat.execute("CREATE ALIAS SIMPLE FOR \"" + getClass().getName() + ".simpleResultSet\"");
rs = stat.executeQuery("CALL SIMPLE(2, 1,1,1,1,1,1,1)");
check(rs.getMetaData().getColumnCount(), 2);
rs.next();
check(rs.getInt(1), 0);
check(rs.getString(2), "Hello");
rs.next();
check(rs.getInt(1), 1);
check(rs.getString(2), "World");
checkFalse(rs.next());
rs = stat.executeQuery("SELECT * FROM SIMPLE(1, 1,1,1,1,1,1,1)");
check(rs.getMetaData().getColumnCount(), 2);
rs.next();
check(rs.getInt(1), 0);
check(rs.getString(2), "Hello");
checkFalse(rs.next());
stat.execute("CREATE ALIAS ARRAY FOR \"" + getClass().getName() + ".getArray\"");
rs = stat.executeQuery("CALL ARRAY()");
check(rs.getMetaData().getColumnCount(), 2);
rs.next();
check(rs.getInt(1), 0);
check(rs.getString(2), "Hello");
checkFalse(rs.next());
stat.execute("CREATE ALIAS ROOT FOR \"" + getClass().getName() + ".root\"");
rs = stat.executeQuery("CALL ROOT(9)");
rs.next();
check(rs.getInt(1), 3);
checkFalse(rs.next());
stat.execute("CREATE ALIAS MAXID FOR \"" + getClass().getName() + ".selectMaxId\"");
rs = stat.executeQuery("CALL MAXID()");
rs.next();
check(rs.getInt(1), 2);
checkFalse(rs.next());
rs = stat.executeQuery("SELECT * FROM MAXID()");
rs.next();
check(rs.getInt(1), 2);
checkFalse(rs.next());
rs = stat.executeQuery("CALL CASE WHEN -9 < 0 THEN 0 ELSE ROOT(-9) END");
rs.next();
check(rs.getInt(1), 0);
checkFalse(rs.next());
stat.execute("CREATE ALIAS blob2stream FOR \"" + getClass().getName() + ".blob2stream\"");
stat.execute("CREATE ALIAS stream2stream FOR \"" + getClass().getName() + ".stream2stream\"");
stat.execute("CREATE TABLE TEST_BLOB(ID INT PRIMARY KEY, VALUE BLOB)");
stat.execute("INSERT INTO TEST_BLOB VALUES(0, null)");
stat.execute("INSERT INTO TEST_BLOB VALUES(1, 'edd1f011edd1f011edd1f011')");
rs = stat.executeQuery("SELECT blob2stream(VALUE) FROM TEST_BLOB");
while(rs.next()) {
}
rs.close();
rs = stat.executeQuery("SELECT stream2stream(VALUE) FROM TEST_BLOB");
while(rs.next()) {
// ignore
}
stat.execute("CREATE ALIAS NULLRESULT FOR \"" + getClass().getName() + ".nullResultSet\"");
rs = stat.executeQuery("CALL NULLRESULT()");
check(rs.getMetaData().getColumnCount(), 1);
rs.next();
check(rs.getString(1), null);
checkFalse(rs.next());
conn.close();
}
void test(String sql, String value) throws Exception {
ResultSet rs = stat.executeQuery("CALL " + sql);
rs.next();
String s = rs.getString(1);
check(value, s);
}
public static BufferedInputStream blob2stream(Blob value) throws SQLException {
if(value == null) {
return null;
}
BufferedInputStream bufferedInStream = new BufferedInputStream(value.getBinaryStream());
return bufferedInStream;
}
public static BufferedInputStream stream2stream(InputStream value) throws SQLException {
if(value == null) {
return null;
}
BufferedInputStream bufferedInStream = new BufferedInputStream(value);
return bufferedInStream;
}
public static int addRow(Connection conn, int id, String name) throws SQLException {
conn.createStatement().execute("INSERT INTO TEST VALUES(" + id + ", '" + name + "')");
ResultSet rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM TEST");
rs.next();
int result = rs.getInt(1);
rs.close();
return result;
}
public static ResultSet select(Connection conn, String sql) throws SQLException {
return conn.createStatement().executeQuery(sql);
}
public static ResultSet selectMaxId(Connection conn) throws SQLException {
return conn.createStatement().executeQuery("SELECT MAX(ID) FROM TEST");
}
public static Object[] getArray() {
return new Object[] { new Integer(0), "Hello" };
}
public static ResultSet nullResultSet(Connection conn) throws SQLException {
PreparedStatement statement = conn.prepareStatement("select null from system_range(1,1)");
return statement.executeQuery();
}
public static ResultSet simpleResultSet(Integer count, int ip, boolean bp, float fp, double dp, long lp, byte byp, short sp) throws SQLException {
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("ID", Types.INTEGER, 10, 0);
rs.addColumn("NAME", Types.VARCHAR, 255, 0);
if (count == null) {
if (ip != 0 || bp || fp != 0.0 || dp != 0.0 || sp != 0 || lp != 0 || byp != 0) {
throw new Error("params not 0/false");
}
}
if (count != null) {
if (ip != 1 || !bp || fp != 1.0 || dp != 1.0 || sp != 1 || lp != 1 || byp != 1) {
throw new Error("params not 1/true");
}
if (count.intValue() >= 1) {
rs.addRow(new Object[] { new Integer(0), "Hello" });
}
if (count.intValue() >= 2) {
rs.addRow(new Object[] { new Integer(1), "World" });
}
}
return rs;
}
public static int root(int value) {
if (value < 0) {
new Exception("function called but should not").printStackTrace();
}
return (int) Math.sqrt(value);
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.*;
import java.util.Random;
import org.h2.test.TestBase;
public class TestIndex extends TestBase {
Connection conn;
Statement stat;
Random random = new Random();
private void reconnect() throws Exception {
if(conn != null) {
conn.close();
conn = null;
}
conn = getConnection("index");
stat = conn.createStatement();
}
public void test() throws Exception {
if(config.networked && config.big) {
return;
}
random.setSeed(100);
deleteDb("index");
testWideIndex(147);
testWideIndex(313);
testWideIndex(979);
testWideIndex(1200);
testWideIndex(2400);
if(config.big && config.logMode == 2) {
for(int i=0; i<2000; i++) {
if((i%100)==0) {
System.out.println("width: " + i);
}
testWideIndex(i);
}
}
testLike();
reconnect();
testConstraint();
testLargeIndex();
testMultiColumnIndex();
//long time;
//time = System.currentTimeMillis();
testHashIndex(true, false);
testHashIndex(false, false);
//System.out.println("btree="+(System.currentTimeMillis()-time));
//time = System.currentTimeMillis();
testHashIndex(true, true);
testHashIndex(false, true);
//System.out.println("hash="+(System.currentTimeMillis()-time));
testMultiColumnHashIndex();
conn.close();
}
String getRandomString(int len) {
StringBuffer buff = new StringBuffer();
for(int i = 0; i<len; i++) {
buff.append((char)('a' + random.nextInt(26)));
}
return buff.toString();
}
void testWideIndex(int length) throws Exception {
reconnect();
stat.execute("CREATE TABLE TEST(ID INT, NAME VARCHAR)");
stat.execute("CREATE INDEX IDXNAME ON TEST(NAME)");
for(int i=0; i<100; i++) {
stat.execute("INSERT INTO TEST VALUES("+i+", SPACE("+length+") || "+i+" )");
}
ResultSet rs = stat.executeQuery("SELECT * FROM TEST ORDER BY NAME");
while(rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("NAME");
check(""+id, name.trim());
}
if(!config.memory) {
reconnect();
rs = stat.executeQuery("SELECT * FROM TEST ORDER BY NAME");
while(rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("NAME");
check(""+id, name.trim());
}
}
stat.execute("DROP TABLE TEST");
}
void testLike() throws Exception {
reconnect();
stat.execute("CREATE TABLE ABC(ID INT, NAME VARCHAR)");
stat.execute("INSERT INTO ABC VALUES(1, 'Hello')");
PreparedStatement prep = conn.prepareStatement("SELECT * FROM ABC WHERE NAME LIKE CAST(? AS VARCHAR)");
prep.setString(1, "Hi%");
prep.execute();
stat.execute("DROP TABLE ABC");
}
void testConstraint() throws Exception {
if(config.memory) {
return;
}
stat.execute("CREATE TABLE PARENT(ID INT PRIMARY KEY)");
stat.execute("CREATE TABLE CHILD(ID INT PRIMARY KEY, PID INT, FOREIGN KEY(PID) REFERENCES PARENT(ID))");
reconnect();
stat.execute("DROP TABLE PARENT");
stat.execute("DROP TABLE CHILD");
}
void testLargeIndex() throws Exception {
random.setSeed(10);
for(int i=1; i<100; i += getSize(1000, 3)) {
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(NAME VARCHAR("+i+"))");
stat.execute("CREATE INDEX IDXNAME ON TEST(NAME)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?)");
for(int j=0; j<getSize(2, 5); j++) {
prep.setString(1, getRandomString(i));
prep.execute();
}
if(!config.memory) {
conn.close();
conn = getConnection("index");
stat = conn.createStatement();
}
ResultSet rs = stat.executeQuery("SELECT COUNT(*) FROM TEST WHERE NAME > 'mdd'");
rs.next();
int count = rs.getInt(1);
trace(i+" count="+count);
}
stat.execute("DROP TABLE IF EXISTS TEST");
}
void testHashIndex(boolean primaryKey, boolean hash) throws Exception {
if(config.memory) {
return;
}
reconnect();
stat.execute("DROP TABLE IF EXISTS TEST");
if(primaryKey) {
stat.execute("CREATE TABLE TEST(A INT PRIMARY KEY "+(hash?"HASH":"")+", B INT)");
} else {
stat.execute("CREATE TABLE TEST(A INT, B INT)");
stat.execute("CREATE UNIQUE "+(hash?"HASH":"")+" INDEX ON TEST(A)");
}
PreparedStatement prep;
prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
int len = getSize(5, 1000);
for(int a=0; a<len; a++) {
prep.setInt(1, a);
prep.setInt(2, a);
prep.execute();
check(1, getValue(stat, "SELECT COUNT(*) FROM TEST WHERE A=" + a));
check(0, getValue(stat, "SELECT COUNT(*) FROM TEST WHERE A=-1-" + a));
}
reconnect();
prep = conn.prepareStatement("DELETE FROM TEST WHERE A=?");
for(int a=0; a<len; a++) {
if(getValue(stat, "SELECT COUNT(*) FROM TEST WHERE A=" + a) != 1) {
check(1, getValue(stat, "SELECT COUNT(*) FROM TEST WHERE A=" + a));
}
prep.setInt(1, a);
check(1, prep.executeUpdate());
}
check(0, getValue(stat, "SELECT COUNT(*) FROM TEST"));
}
void testMultiColumnIndex() throws Exception {
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(A INT, B INT)");
PreparedStatement prep;
prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
int len = getSize(3, 260);
for(int a=0; a<len; a++) {
prep.setInt(1, a);
prep.setInt(2, a);
prep.execute();
}
stat.execute("INSERT INTO TEST SELECT A, B FROM TEST");
stat.execute("CREATE INDEX ON TEST(A, B)");
prep = conn.prepareStatement("DELETE FROM TEST WHERE A=?");
for(int a=0; a<len; a++) {
log(stat, "SELECT * FROM TEST");
check(2, getValue(stat, "SELECT COUNT(*) FROM TEST WHERE A=" + (len-a-1)));
check((len-a)*2, getValue(stat, "SELECT COUNT(*) FROM TEST"));
prep.setInt(1, (len-a-1));
prep.execute();
}
check(0, getValue(stat, "SELECT COUNT(*) FROM TEST"));
}
void testMultiColumnHashIndex() throws Exception {
if(config.memory) {
return;
}
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(A INT, B INT, DATA VARCHAR(255))");
stat.execute("CREATE UNIQUE HASH INDEX IDXAB ON TEST(A, B)");
PreparedStatement prep;
prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?, ?)");
// speed is quadratic (len*len)
int len = getSize(2, 14);
for(int a=0; a<len; a++) {
for(int b=0; b<len; b+=2) {
prep.setInt(1, a);
prep.setInt(2, b);
prep.setString(3, "i("+a+","+b+")");
prep.execute();
}
}
reconnect();
prep = conn.prepareStatement("UPDATE TEST SET DATA=DATA||? WHERE A=? AND B=?");
for(int a=0; a<len; a++) {
for(int b=0; b<len; b+=2) {
prep.setString(1, "u("+a+","+b+")");
prep.setInt(2, a);
prep.setInt(3, b);
prep.execute();
}
}
reconnect();
ResultSet rs = stat.executeQuery("SELECT * FROM TEST WHERE DATA <> 'i('||a||','||b||')u('||a||','||b||')'");
checkFalse(rs.next());
check(len*(len/2), getValue(stat, "SELECT COUNT(*) FROM TEST"));
stat.execute("DROP TABLE TEST");
}
int getValue(Statement stat, String sql) throws Exception {
ResultSet rs = stat.executeQuery(sql);
rs.next();
return rs.getInt(1);
}
void log(Statement stat, String sql) throws Exception {
trace(sql);
ResultSet rs = stat.executeQuery(sql);
int cols = rs.getMetaData().getColumnCount();
while(rs.next()) {
for(int i=0; i<cols; i++) {
trace("["+i+"]="+rs.getString(i+1));
}
}
trace("---done---");
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.math.BigDecimal;
import java.sql.*;
import org.h2.test.TestBase;
public class TestLinkedTable extends TestBase {
public void test() throws Exception {
deleteDb("linked1");
deleteDb("linked2");
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:"+BASE_DIR+"/linked1", "sa1", "abc");
Statement stat = conn.createStatement();
stat.execute("CREATE TEMP TABLE TEST_TEMP(ID INT PRIMARY KEY)");
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255), XT TINYINT, XD DECIMAL(10,2), XTS TIMESTAMP, XBY BINARY(255), XBO BIT, XSM SMALLINT, XBI BIGINT, XBL BLOB, XDA DATE, XTI TIME, XCL CLOB, XDO DOUBLE)");
stat.execute("CREATE INDEX IDXNAME ON TEST(NAME)");
stat.execute("INSERT INTO TEST VALUES(0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)");
stat.execute("INSERT INTO TEST VALUES(1, 'Hello', -1, 10.30, '2001-02-03 11:22:33.4455', X'FF0102', TRUE, 3000, 1234567890123456789, X'1122AA', DATE '0002-01-01', TIME '00:00:00', 'J\u00fcrg', 2.25)");
testRow(stat, "TEST");
stat.execute("INSERT INTO TEST VALUES(2, 'World', 30, 100.05, '2005-12-31 12:34:56.789', X'FFEECC33', FALSE, 1, -1234567890123456789, X'4455FF', DATE '9999-12-31', TIME '23:59:59', 'George', -2.5)");
testRow(stat, "TEST");
stat.execute("SELECT * FROM TEST_TEMP");
conn.close();
conn = DriverManager.getConnection("jdbc:h2:"+BASE_DIR+"/linked1", "sa1", "abc");
stat = conn.createStatement();
testRow(stat, "TEST");
try {
stat.execute("SELECT * FROM TEST_TEMP");
error("temp table must not be persistent");
} catch(SQLException e) {
checkNotGeneralException(e);
}
conn.close();
conn = DriverManager.getConnection("jdbc:h2:"+BASE_DIR+"/linked2", "sa2", "def");
stat = conn.createStatement();
stat.execute("CREATE LINKED TABLE IF NOT EXISTS LINK_TEST('org.h2.Driver', 'jdbc:h2:"+BASE_DIR+"/linked1', 'sa1', 'abc', 'TEST')");
stat.execute("CREATE LINKED TABLE IF NOT EXISTS LINK_TEST('org.h2.Driver', 'jdbc:h2:"+BASE_DIR+"/linked1', 'sa1', 'abc', 'TEST')");
testRow(stat, "LINK_TEST");
conn.close();
conn = DriverManager.getConnection("jdbc:h2:"+BASE_DIR+"/linked2", "sa2", "def");
stat = conn.createStatement();
stat.execute("INSERT INTO LINK_TEST VALUES(3, 'Link Test', 30, 100.05, '2005-12-31 12:34:56.789', X'FFEECC33', FALSE, 1, -1234567890123456789, X'4455FF', DATE '9999-12-31', TIME '23:59:59', 'George', -2.5)");
ResultSet rs = stat.executeQuery("SELECT COUNT(*) FROM LINK_TEST");
rs.next();
check(rs.getInt(1), 4);
rs = stat.executeQuery("SELECT COUNT(*) FROM LINK_TEST WHERE NAME='Link Test'");
rs.next();
check(rs.getInt(1), 1);
int uc = stat.executeUpdate("DELETE FROM LINK_TEST WHERE ID=3");
check(uc, 1);
rs = stat.executeQuery("SELECT COUNT(*) FROM LINK_TEST");
rs.next();
check(rs.getInt(1), 3);
rs = stat.executeQuery("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='LINK_TEST'");
rs.next();
check(rs.getString("TABLE_TYPE"), "TABLE LINK");
rs.next();
rs = stat.executeQuery("SELECT * FROM LINK_TEST WHERE ID=0");
rs.next();
check(rs.getString("NAME")==null && rs.wasNull());
check(rs.getString("XT")==null && rs.wasNull());
check(rs.getInt("ID")==0 && !rs.wasNull());
check(rs.getBigDecimal("XD")==null && rs.wasNull());
check(rs.getTimestamp("XTS")==null && rs.wasNull());
check(rs.getBytes("XBY")==null && rs.wasNull());
check(rs.getBoolean("XBO")==false && rs.wasNull());
check(rs.getShort("XSM")==0 && rs.wasNull());
check(rs.getLong("XBI")==0 && rs.wasNull());
check(rs.getString("XBL")==null && rs.wasNull());
check(rs.getString("XDA")==null && rs.wasNull());
check(rs.getString("XTI")==null && rs.wasNull());
check(rs.getString("XCL")==null && rs.wasNull());
check(rs.getString("XDO")==null && rs.wasNull());
checkFalse(rs.next());
stat.execute("DROP TABLE LINK_TEST");
stat.execute("CREATE LINKED TABLE LINK_TEST('org.h2.Driver', 'jdbc:h2:"+BASE_DIR+"/linked1', 'sa1', 'abc', '(SELECT COUNT(*) FROM TEST)')");
rs = stat.executeQuery("SELECT * FROM LINK_TEST");
rs.next();
check(rs.getInt(1), 3);
checkFalse(rs.next());
conn.close();
deleteDb("linked1");
deleteDb("linked2");
}
void testRow(Statement stat, String name) throws Exception {
ResultSet rs = stat.executeQuery("SELECT * FROM "+name+" WHERE ID=1");
rs.next();
check(rs.getString("NAME"), "Hello");
check(rs.getByte("XT"), -1);
BigDecimal bd = rs.getBigDecimal("XD");
check(bd.equals(new BigDecimal("10.30")));
Timestamp ts = rs.getTimestamp("XTS");
String s = ts.toString();
check(s, "2001-02-03 11:22:33.4455");
check(ts.equals(Timestamp.valueOf("2001-02-03 11:22:33.4455")));
check(rs.getBytes("XBY"), new byte[]{(byte)255, (byte)1, (byte)2});
check(rs.getBoolean("XBO"));
check(rs.getShort("XSM"), 3000);
check(rs.getLong("XBI"), 1234567890123456789L);
check(rs.getString("XBL"), "1122aa");
check(rs.getString("XDA"), "0002-01-01");
check(rs.getString("XTI"), "00:00:00");
check(rs.getString("XCL"), "J\u00fcrg");
check(rs.getString("XDO"), "2.25");
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.api.DatabaseEventListener;
import org.h2.test.TestBase;
public class TestListener extends TestBase implements DatabaseEventListener {
private long last, start;
public TestListener() {
start = last = System.currentTimeMillis();
}
public void test() throws Exception {
if(config.networked) {
return;
}
deleteDb("listener");
Connection conn;
conn = getConnection("listener");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, 'Test' || SPACE(100))");
int len = getSize(100, 100000);
for(int i=0; i<len; i++) {
prep.setInt(1, i);
prep.execute();
}
crash(conn);
conn = getConnection("listener;database_event_listener='" + getClass().getName() + "'");
conn.close();
}
public void diskSpaceIsLow(long stillAvailable) throws SQLException {
System.out.println("diskSpaceIsLow stillAvailable="+stillAvailable);
}
public void exceptionThrown(SQLException e) {
e.printStackTrace();
}
public void setProgress(int state, String name, int current, int max) {
long time = System.currentTimeMillis();
if(time < last+1000) {
return;
}
last = time;
String stateName;
switch(state) {
case STATE_SCAN_FILE:
stateName = "Scan " + name;
break;
case STATE_CREATE_INDEX:
stateName = "Create Index " + name;
break;
case STATE_RECOVER:
stateName = "Recover";
break;
default:
new Error("unknownn state: " + state).printStackTrace();
stateName = "? " + name;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
System.out.println("state: " + stateName + " " + (100*current/max) + " " + (time-start));
}
public void closingDatabase() {
}
public void init(String url) {
}
}
差异被折叠。
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.io.Serializable;
class TestLobObject implements Serializable {
private static final long serialVersionUID = 904356179316518715L;
String data;
TestLobObject(String data) {
this.data = data;
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import org.h2.test.TestBase;
import org.h2.tools.FileBase;
public class TestLogFile extends TestBase {
private Connection conn;
private static final int MAX_LOG_SIZE = 1;
private long reconnect(int maxFiles) throws Exception {
if(conn != null) {
conn.close();
}
long length = 0;
ArrayList files = FileBase.getDatabaseFiles(BASE_DIR, "logfile", false);
checkSmaller(files.size(), maxFiles+2);
for(int i=0; i<files.size(); i++) {
String fileName = (String) files.get(i);
long len = new File(fileName).length();
length += len;
}
conn = getConnection("logfile");
return length;
}
public void test() throws Exception {
if(config.memory) {
return;
}
deleteDb("logfile");
reconnect(0);
insert();
int maxFiles = 3; // data, index, log
for(int i=0; i<3; i++) {
long length = reconnect(maxFiles);
insert();
long l2 = reconnect(maxFiles);
trace("l2="+ l2);
check(l2 <= length * 2);
}
conn.close();
}
private void checkLogSize() throws Exception {
String[] files = new File(".").list();
for(int j=0; j<files.length; j++) {
String name = files[j];
if(name.startsWith("logfile") && name.endsWith(".log.db")) {
long length = new File(name).length();
checkSmaller(length, MAX_LOG_SIZE * 1024 * 1024 * 2);
}
}
}
void insert() throws Exception {
Statement stat = conn.createStatement();
stat.execute("SET LOGSIZE 200");
stat.execute("SET MAX_LOG_SIZE " + MAX_LOG_SIZE);
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, 'Hello' || ?)");
int len = getSize(1, 10000);
for(int i=0; i<len; i++) {
prep.setInt(1, i);
prep.setInt(2, i);
prep.execute();
if(i>0 && (i % 2000) == 0) {
checkLogSize();
}
}
checkLogSize();
}
}
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import org.h2.test.TestBase;
public class TestMemoryUsage extends TestBase {
private Connection conn;
private void reconnect() throws Exception {
if(conn != null) {
conn.close();
}
// Class.forName("org.hsqldb.jdbcDriver");
// conn = DriverManager.getConnection("jdbc:hsqldb:test", "sa", "");
conn = getConnection("memoryUsage");
}
public void test() throws Exception {
deleteDb("memoryUsage");
reconnect();
insertUpdateSelectDelete();
reconnect();
insertUpdateSelectDelete();
conn.close();
}
void insertUpdateSelectDelete() throws Exception {
Statement stat = conn.createStatement();
long time;
int len = getSize(1, 2000);
// insert
time = System.currentTimeMillis();
stat.execute("DROP TABLE IF EXISTS TEST");
trace("drop=" + (System.currentTimeMillis()-time));
stat.execute("CREATE CACHED TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, 'Hello World')");
printTimeMemory("start", 0);
time = System.currentTimeMillis();
for(int i=0; i<len; i++) {
prep.setInt(1, i);
prep.execute();
if(i % 50000 == 0) {
trace(" " + (100*i/len) + "%");
}
}
printTimeMemory("insert", System.currentTimeMillis()-time);
// update
time = System.currentTimeMillis();
prep = conn.prepareStatement("UPDATE TEST SET NAME='Hallo Welt' WHERE ID = ?");
for(int i=0; i<len; i++) {
prep.setInt(1, i);
prep.execute();
if(i % 50000 == 0) {
trace(" " + (100*i/len) + "%");
}
}
printTimeMemory("update", System.currentTimeMillis()-time);
// select
time = System.currentTimeMillis();
prep = conn.prepareStatement("SELECT * FROM TEST WHERE ID = ?");
for(int i=0; i<len; i++) {
prep.setInt(1, i);
ResultSet rs = prep.executeQuery();
rs.next();
if(rs.next()) {
error("one row expected, got more");
}
if(i % 50000 == 0) {
trace(" " + (100*i/len) + "%");
}
}
printTimeMemory("select", System.currentTimeMillis()-time);
// select randomized
Random random = new Random(1);
time = System.currentTimeMillis();
prep = conn.prepareStatement("SELECT * FROM TEST WHERE ID = ?");
for(int i=0; i<len; i++) {
prep.setInt(1, random.nextInt(len));
ResultSet rs = prep.executeQuery();
rs.next();
if(rs.next()) {
error("one row expected, got more");
}
if(i % 50000 == 0) {
trace(" " + (100*i/len) + "%");
}
}
printTimeMemory("select randomized", System.currentTimeMillis()-time);
// delete
time = System.currentTimeMillis();
prep = conn.prepareStatement("DELETE FROM TEST WHERE ID = ?");
for(int i=0; i<len; i++) {
prep.setInt(1, random.nextInt(len));
prep.executeUpdate();
if(i % 50000 == 0) {
trace(" " + (100*i/len) + "%");
}
}
printTimeMemory("delete", System.currentTimeMillis()-time);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论