Search in sources :

Example 1 with H2Database

use of org.h2.android.H2Database in project h2database by h2database.

the class H2Database method openDatabase.

/**
 * Open a connection to the given database.
 *
 * @param path the database file name
 * @param factory the cursor factory
 * @param flags 0, or a combination of OPEN_READONLY and CREATE_IF_NECESSARY
 * @return a connection to this database
 */
public static H2Database openDatabase(String path, H2Database.CursorFactory factory, int flags) {
    ConnectionInfo ci = new ConnectionInfo(path);
    if ((flags & OPEN_READWRITE) != 0) {
    // TODO readonly connections
    }
    if ((flags & CREATE_IF_NECESSARY) == 0) {
        ci.setProperty("IFEXISTS", "TRUE");
    }
    ci.setProperty("FILE_LOCK", "FS");
    Database db = new Database(ci, null);
    Session s = db.getSystemSession();
    return new H2Database(s, factory);
}
Also used : Database(org.h2.engine.Database) ConnectionInfo(org.h2.engine.ConnectionInfo) Session(org.h2.engine.Session)

Example 2 with H2Database

use of org.h2.android.H2Database in project h2database by h2database.

the class H2Database method create.

/**
 * Create a new in-memory database.
 *
 * @param factory the cursor factory
 * @return a connection to this database
 */
public static H2Database create(H2Database.CursorFactory factory) {
    ConnectionInfo ci = new ConnectionInfo("mem:");
    Database db = new Database(ci, null);
    Session s = db.getSystemSession();
    return new H2Database(s, factory);
}
Also used : Database(org.h2.engine.Database) ConnectionInfo(org.h2.engine.ConnectionInfo) Session(org.h2.engine.Session)

Example 3 with H2Database

use of org.h2.android.H2Database in project h2database by h2database.

the class Test method main.

public static void main(String... args) throws Exception {
    H2Database db = H2Utils.openOrCreateDatabase("helloWorld.db", MODE_PRIVATE, null);
    log("opened ps=" + db.getPageSize());
    try {
        // db.execSQL("DROP TABLE IF EXISTS test");
        // log("dropped");
        db.execSQL("CREATE TABLE if not exists test(ID INTEGER PRIMARY KEY, NAME VARCHAR)");
        log("created");
        for (int j = 0; j < 10; j++) {
            Cursor c = db.rawQuery("select * from test", new String[0]);
            int count = c.getCount();
            for (int i = 0; i < count; i++) {
                c.move(1);
                c.getInt(0);
                c.getString(1);
            }
            c.close();
        }
        // log("select " + count);
        db.execSQL("delete from test");
        log("delete");
        db.beginTransaction();
        for (int i = 0; i < 1000; i++) {
            db.execSQL("INSERT INTO TEST VALUES(?, 'Hello')", new Object[] { i });
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        log("inserted");
        for (int i = 0; i < 10; i++) {
            Cursor c = db.rawQuery("select * from test where id=?", new String[] { "" + i });
            int count = c.getCount();
            if (count > 0) {
                c.move(1);
                c.getInt(0);
                c.getString(1);
            }
            c.close();
        }
        log("select");
    } finally {
        db.close();
        log("closed");
    }
}
Also used : Cursor(android.database.Cursor) H2Database(org.h2.android.H2Database)

Example 4 with H2Database

use of org.h2.android.H2Database in project sandbox by irof.

the class FirstTest method setup.

@Before
public void setup() throws Exception {
    // H2DatabaseのDataSourceを用意
    dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:mem:temp;DB_CLOSE_DELAY=-1");
    dataSource.setUser("sa");
    // Flywayでテーブル作成
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.migrate();
}
Also used : Flyway(org.flywaydb.core.Flyway) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Before(org.junit.Before)

Example 5 with H2Database

use of org.h2.android.H2Database in project h2database by h2database.

the class UploadBuild method main.

/**
 * This method is called when executing this application from the command
 * line.
 *
 * @param args the command line parameters
 */
public static void main(String... args) throws Exception {
    System.setProperty("h2.socketConnectTimeout", "30000");
    String password = System.getProperty("h2.ftpPassword");
    if (password == null) {
        return;
    }
    FtpClient ftp = FtpClient.open("h2database.com");
    ftp.login("h2database", password);
    ftp.changeWorkingDirectory("/httpdocs");
    boolean coverage = new File("coverage/index.html").exists();
    boolean coverageFailed;
    if (coverage) {
        byte[] data = IOUtils.readBytesAndClose(new FileInputStream("coverage/index.html"), -1);
        String index = new String(data, StandardCharsets.ISO_8859_1);
        coverageFailed = index.contains("CLASS=\"h\"");
        while (true) {
            int idx = index.indexOf("<A HREF=\"");
            if (idx < 0) {
                break;
            }
            int end = index.indexOf('>', idx) + 1;
            index = index.substring(0, idx) + index.substring(end);
            idx = index.indexOf("</A>");
            index = index.substring(0, idx) + index.substring(idx + "</A>".length());
        }
        index = StringUtils.replaceAll(index, "[all", "");
        index = StringUtils.replaceAll(index, "classes]", "");
        FileOutputStream out = new FileOutputStream("coverage/overview.html");
        out.write(index.getBytes(StandardCharsets.ISO_8859_1));
        out.close();
        new File("details").mkdir();
        zip("details/coverage_files.zip", "coverage", true);
        zip("coverage.zip", "details", false);
        FileUtils.delete("coverage.txt");
        FileUtils.delete("details/coverage_files.zip");
        FileUtils.delete("details");
        if (ftp.exists("/httpdocs", "coverage")) {
            ftp.removeDirectoryRecursive("/httpdocs/coverage");
        }
        ftp.makeDirectory("/httpdocs/coverage");
    } else {
        coverageFailed = true;
    }
    String testOutput;
    boolean error;
    if (new File("docs/html/testOutput.html").exists()) {
        testOutput = IOUtils.readStringAndClose(new FileReader("docs/html/testOutput.html"), -1);
        error = testOutput.contains(OutputCatcher.START_ERROR);
    } else if (new File("log.txt").exists()) {
        testOutput = IOUtils.readStringAndClose(new FileReader("log.txt"), -1);
        testOutput = testOutput.replaceAll("\n", "<br />");
        error = true;
    } else {
        testOutput = "No log.txt";
        error = true;
    }
    if (!ftp.exists("/httpdocs", "automated")) {
        ftp.makeDirectory("/httpdocs/automated");
    }
    String buildSql;
    if (ftp.exists("/httpdocs/automated", "history.sql")) {
        buildSql = new String(ftp.retrieve("/httpdocs/automated/history.sql"));
    } else {
        buildSql = "create table item(id identity, title varchar, " + "issued timestamp, desc varchar);\n";
    }
    String ts = new java.sql.Timestamp(System.currentTimeMillis()).toString();
    String now = ts.substring(0, 16);
    int idx = testOutput.indexOf("Statements per second: ");
    if (idx >= 0) {
        int end = testOutput.indexOf("<br />", idx);
        if (end >= 0) {
            String result = testOutput.substring(idx + "Statements per second: ".length(), end);
            now += " " + result + " op/s";
        }
    }
    String sql = "insert into item(title, issued, desc) values('Build " + now + (error ? " FAILED" : "") + (coverageFailed ? " COVERAGE" : "") + "', '" + ts + "', '<a href=\"http://www.h2database.com/" + "html/testOutput.html\">Output</a>" + " - <a href=\"http://www.h2database.com/" + "coverage/overview.html\">Coverage</a>" + " - <a href=\"http://www.h2database.com/" + "automated/h2-latest.jar\">Jar</a>');\n";
    buildSql += sql;
    Connection conn;
    try {
        Class.forName("org.h2.Driver");
        conn = DriverManager.getConnection("jdbc:h2:mem:");
    } catch (Exception e) {
        Class.forName("org.h2.upgrade.v1_1.Driver");
        conn = DriverManager.getConnection("jdbc:h2v1_1:mem:");
    }
    conn.createStatement().execute(buildSql);
    String newsfeed = IOUtils.readStringAndClose(new FileReader("src/tools/org/h2/build/doc/buildNewsfeed.sql"), -1);
    ScriptReader r = new ScriptReader(new StringReader(newsfeed));
    Statement stat = conn.createStatement();
    ResultSet rs = null;
    while (true) {
        String s = r.readStatement();
        if (s == null) {
            break;
        }
        if (stat.execute(s)) {
            rs = stat.getResultSet();
        }
    }
    rs.next();
    String content = rs.getString("content");
    conn.close();
    ftp.store("/httpdocs/automated/history.sql", new ByteArrayInputStream(buildSql.getBytes()));
    ftp.store("/httpdocs/automated/news.xml", new ByteArrayInputStream(content.getBytes()));
    ftp.store("/httpdocs/html/testOutput.html", new ByteArrayInputStream(testOutput.getBytes()));
    String jarFileName = "bin/h2-" + Constants.getVersion() + ".jar";
    if (FileUtils.exists(jarFileName)) {
        ftp.store("/httpdocs/automated/h2-latest.jar", new FileInputStream(jarFileName));
    }
    if (coverage) {
        ftp.store("/httpdocs/coverage/overview.html", new FileInputStream("coverage/overview.html"));
        ftp.store("/httpdocs/coverage/coverage.zip", new FileInputStream("coverage.zip"));
        FileUtils.delete("coverage.zip");
    }
    String mavenRepoDir = System.getProperty("user.home") + "/.m2/repository/";
    boolean mavenSnapshot = new File(mavenRepoDir + "com/h2database/h2/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.jar").exists();
    if (mavenSnapshot) {
        if (!ftp.exists("/httpdocs", "m2-repo")) {
            ftp.makeDirectory("/httpdocs/m2-repo");
        }
        if (!ftp.exists("/httpdocs/m2-repo", "com")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com");
        }
        if (!ftp.exists("/httpdocs/m2-repo/com", "h2database")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com/h2database");
        }
        if (!ftp.exists("/httpdocs/m2-repo/com/h2database", "h2")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2");
        }
        if (!ftp.exists("/httpdocs/m2-repo/com/h2database/h2", "1.0-SNAPSHOT")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2/1.0-SNAPSHOT");
        }
        if (!ftp.exists("/httpdocs/m2-repo/com/h2database", "h2-mvstore")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2-mvstore");
        }
        if (!ftp.exists("/httpdocs/m2-repo/com/h2database/h2-mvstore", "1.0-SNAPSHOT")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2-mvstore/1.0-SNAPSHOT");
        }
        ftp.store("/httpdocs/m2-repo/com/h2database/h2" + "/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.pom", new FileInputStream(mavenRepoDir + "com/h2database/h2/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.pom"));
        ftp.store("/httpdocs/m2-repo/com/h2database/h2" + "/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.jar", new FileInputStream(mavenRepoDir + "com/h2database/h2/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.jar"));
        ftp.store("/httpdocs/m2-repo/com/h2database/h2-mvstore" + "/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.pom", new FileInputStream(mavenRepoDir + "com/h2database/h2-mvstore/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.pom"));
        ftp.store("/httpdocs/m2-repo/com/h2database/h2-mvstore" + "/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.jar", new FileInputStream(mavenRepoDir + "com/h2database/h2-mvstore/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.jar"));
    }
    ftp.close();
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) StringReader(java.io.StringReader) ResultSet(java.sql.ResultSet) FileReader(java.io.FileReader) FtpClient(org.h2.dev.ftp.FtpClient) File(java.io.File) ScriptReader(org.h2.util.ScriptReader)

Aggregations

ConnectionInfo (org.h2.engine.ConnectionInfo)2 Database (org.h2.engine.Database)2 Session (org.h2.engine.Session)2 Cursor (android.database.Cursor)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Flyway (org.flywaydb.core.Flyway)1 H2Database (org.h2.android.H2Database)1 FtpClient (org.h2.dev.ftp.FtpClient)1 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)1 ScriptReader (org.h2.util.ScriptReader)1 Before (org.junit.Before)1