Search in sources :

Example 71 with Db

use of org.h2.test.db.Db in project h2database by h2database.

the class ModelsTest method testModelGeneration.

private void testModelGeneration() {
    DbInspector inspector = new DbInspector(db);
    List<String> models = inspector.generateModel(null, "SupportedTypes", "org.h2.test.jaqu", true, true);
    assertEquals(1, models.size());
    // a poor test, but a start
    assertEquals(1364, models.get(0).length());
}
Also used : DbInspector(org.h2.jaqu.DbInspector)

Example 72 with Db

use of org.h2.test.db.Db in project h2database by h2database.

the class AliasMapTest method test.

@Override
public void test() throws Exception {
    Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
    db.insertAll(Product.getList());
    Product p = new Product();
    List<Product> products = db.from(p).where(p.unitsInStock).is(9).orderBy(p.productId).select();
    assertEquals("[]", products.toString());
    db.close();
}
Also used : Db(org.h2.jaqu.Db)

Example 73 with Db

use of org.h2.test.db.Db in project h2database by h2database.

the class AnnotationsTest method testCreateTableIfRequiredAnnotation.

private void testCreateTableIfRequiredAnnotation() {
    // tests JQTable.createTableIfRequired=false
    Db noCreateDb = null;
    try {
        noCreateDb = Db.open("jdbc:h2:mem:", "sa", "sa");
        noCreateDb.insertAll(ProductNoCreateTable.getList());
        noCreateDb.close();
    } catch (RuntimeException r) {
        SQLException s = (SQLException) r.getCause();
        assertEquals(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s.getErrorCode());
    }
    if (noCreateDb != null) {
        JdbcUtils.closeSilently(noCreateDb.getConnection());
    }
}
Also used : SQLException(java.sql.SQLException) Db(org.h2.jaqu.Db)

Example 74 with Db

use of org.h2.test.db.Db in project h2database by h2database.

the class TestLob method testLobCompression.

private void testLobCompression(boolean compress) throws Exception {
    deleteDb("lob");
    Connection conn;
    conn = reconnect(null);
    if (compress) {
        conn.createStatement().execute("SET COMPRESS_LOB LZF");
    } else {
        conn.createStatement().execute("SET COMPRESS_LOB NO");
    }
    conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, C CLOB)");
    PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
    long time = System.nanoTime();
    int len = getSize(10, 40);
    if (config.networked && config.big) {
        len = 5;
    }
    StringBuilder buff = new StringBuilder();
    for (int i = 0; i < 1000; i++) {
        buff.append(StringUtils.xmlNode("content", null, "This is a test " + i));
    }
    String xml = buff.toString();
    for (int i = 0; i < len; i++) {
        prep.setInt(1, i);
        prep.setString(2, xml + i);
        prep.execute();
    }
    for (int i = 0; i < len; i++) {
        ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST");
        while (rs.next()) {
            if (i == 0) {
                assertEquals(xml + rs.getInt(1), rs.getString(2));
            } else {
                Reader r = rs.getCharacterStream(2);
                String result = IOUtils.readStringAndClose(r, -1);
                assertEquals(xml + rs.getInt(1), result);
            }
        }
    }
    time = System.nanoTime() - time;
    trace("time: " + TimeUnit.NANOSECONDS.toMillis(time) + " compress: " + compress);
    conn.close();
    if (!config.memory) {
        long length = new File(getBaseDir() + "/lob.h2.db").length();
        trace("len: " + length + " compress: " + compress);
    }
}
Also used : Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) StringReader(java.io.StringReader) PreparedStatement(java.sql.PreparedStatement) File(java.io.File) Savepoint(java.sql.Savepoint)

Example 75 with Db

use of org.h2.test.db.Db in project h2database by h2database.

the class TestMultiThread method testConcurrentLobAdd.

private void testConcurrentLobAdd() throws Exception {
    String db = getTestName();
    deleteDb(db);
    final String url = getURL(db + ";MULTI_THREADED=1", true);
    try (Connection conn = getConnection(url)) {
        Statement stat = conn.createStatement();
        stat.execute("create table test(id identity, data clob)");
        Task[] tasks = new Task[2];
        for (int i = 0; i < tasks.length; i++) {
            Task t = new Task() {

                @Override
                public void call() throws Exception {
                    try (Connection c2 = getConnection(url)) {
                        PreparedStatement p2 = c2.prepareStatement("insert into test(data) values(?)");
                        while (!stop) {
                            p2.setCharacterStream(1, new StringReader(new String(new char[10 * 1024])));
                            p2.execute();
                        }
                    }
                }
            };
            tasks[i] = t;
            t.execute();
        }
        Thread.sleep(500);
        for (Task t : tasks) {
            t.get();
        }
    }
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) StringReader(java.io.StringReader) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Database (org.h2.engine.Database)70 Connection (java.sql.Connection)31 Statement (java.sql.Statement)20 Table (org.h2.table.Table)19 PreparedStatement (java.sql.PreparedStatement)18 ResultSet (java.sql.ResultSet)13 SQLException (java.sql.SQLException)13 Column (org.h2.table.Column)12 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)9 StatementBuilder (org.h2.util.StatementBuilder)9 DbObject (org.h2.engine.DbObject)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 DbException (org.h2.message.DbException)7 Schema (org.h2.schema.Schema)7 Before (org.junit.Before)7 InputStream (java.io.InputStream)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 JdbcConnection (org.h2.jdbc.JdbcConnection)6