org.h2.tools.RunScript Java Examples

The following examples show how to use org.h2.tools.RunScript. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Oauth2RefreshTokenGetHandlerTest.java    From light-oauth2 with Apache License 2.0 7 votes vote down vote up
@BeforeClass
public static void runOnceBeforeClass() {
    System.out.println("@BeforeClass - runOnceBeforeClass");
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = Oauth2RefreshTokenGetHandlerTest.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #2
Source File: H2CacheStoreStrategy.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws IgniteCheckedException If failed.
 */
public H2CacheStoreStrategy() throws IgniteCheckedException {
    Server srv = null;

    try {
        srv = Server.createTcpServer().start();

        port = srv.getPort();

        dataSrc = H2CacheStoreSessionListenerFactory.createDataSource(port);

        try (Connection conn = connection()) {
            RunScript.execute(conn, new StringReader(CREATE_CACHE_TABLE));
            RunScript.execute(conn, new StringReader(CREATE_STATS_TABLES));
            RunScript.execute(conn, new StringReader(POPULATE_STATS_TABLE));
        }
    }
    catch (SQLException e) {
        throw new IgniteCheckedException("Failed to set up cache store strategy" +
            (srv == null ? "" : ": " + srv.getStatus()), e);
    }
}
 
Example #3
Source File: AbstractSQLTest.java    From sharding-jdbc-1.5.1 with Apache License 2.0 6 votes vote down vote up
private static void createSchema(final DatabaseType dbType) {
    try {
        Connection conn;
        for (int i = 0; i < 10; i++) {
            for (String database : Arrays.asList("tbl", "db", "dbtbl", "nullable", "master", "slave", "jdbc")) {
                if ("tbl".equals(database)) {
                    if (i == 0) {
                        conn = initialConnection(database, dbType);
                        RunScript.execute(conn, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("integrate/schema/table/tbl.sql")));
                        conn.close();
                    }
                } else {
                    if ("jdbc".equals(database) && i >= 2) {
                        continue;
                    }
                    conn = initialConnection(database + "_" + i, dbType);
                    RunScript.execute(conn, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("integrate/schema/table/" + database + ".sql")));
                    conn.close();
                }
            }
        }
    } catch (final SQLException ex) {
        ex.printStackTrace();
    }
}
 
Example #4
Source File: H2ConnectionManager.java    From reladomo with Apache License 2.0 6 votes vote down vote up
public void prepareTables() throws Exception
{
    Path ddlPath = Paths.get(ClassLoader.getSystemResource("sql").toURI());

    try (Connection conn = xaConnectionManager.getConnection();)
    {
        Files.walk(ddlPath, 1)
                .filter(path -> !Files.isDirectory(path)
                        && (path.toString().endsWith("ddl")
                        || path.toString().endsWith("idx")))
                .forEach(path -> {
            try
            {
                RunScript.execute(conn, Files.newBufferedReader(path));
            }
            catch (Exception e)
            {
                throw new RuntimeException("Exception at table initialization", e);
            }
        });
    }
}
 
Example #5
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #6
Source File: CacheStartupHookProviderTest.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void runOnceBeforeClass() {
    System.out.println("@BeforeClass - runOnceBeforeClass");
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = CacheStartupHookProviderTest.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #7
Source File: AuditHandlerTest.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void runOnceBeforeClass() {
    System.out.println("@BeforeClass - runOnceBeforeClass");
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = AuditHandlerTest.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #8
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #9
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #10
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example #11
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        // Runscript doesn't work need to execute batch here.
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example #12
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #13
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #14
Source File: Oauth2RefreshTokenRefreshTokenGetHandlerTest.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void runOnceBeforeClass() {
    System.out.println("@BeforeClass - runOnceBeforeClass");
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = Oauth2RefreshTokenGetHandlerTest.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #15
Source File: Oauth2RefreshTokenRefreshTokenDeleteHandlerTest.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void runOnceBeforeClass() {
    System.out.println("@BeforeClass - runOnceBeforeClass");
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = Oauth2RefreshTokenGetHandlerTest.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #16
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example #17
Source File: JdbcStorageUpgradeTest.java    From apicurio-studio with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a datasource from a DB file found in the test resources.
 * @throws SQLException
 */
private static BasicDataSource createDatasourceFrom(String sqlFileName) throws Exception {
    // Step 1 - copy the db file from the classpath to a temp location
    URL sqlUrl = JdbcStorageUpgradeTest.class.getResource(sqlFileName);
    Assert.assertNotNull(sqlUrl);
    File tempDbFile = File.createTempFile("_apicurio_junit", ".mv.db");
    tempDbFile.deleteOnExit();

    String jdbcUrl = "jdbc:h2:file:" + tempDbFile.getAbsolutePath().replaceAll(".mv.db", "");
    
    // Step 2 - create a datasource from the file path
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa");
    ds.setPassword("sa");
    ds.setUrl(jdbcUrl);
    
    try (Connection connection = ds.getConnection(); Reader reader = new InputStreamReader(sqlUrl.openStream())) {
        RunScript.execute(connection, reader);
    }
    
    return ds;
}
 
Example #18
Source File: AbstractShardingSphereDataSourceForEncryptTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Before
public void initTable() {
    try (ShardingSphereConnection connection = encryptDataSource.getConnection()) {
        RunScript.execute(connection, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("encrypt_data.sql")));
    } catch (final SQLException ex) {
        ex.printStackTrace();
    }
}
 
Example #19
Source File: AbstractShardingSphereDataSourceForShardingTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Before
public void initTable() {
    try {
        ShardingSphereConnection conn = shardingSphereDataSource.getConnection();
        RunScript.execute(conn, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("jdbc_data.sql")));
        conn.close();
    } catch (final SQLException ex) {
        ex.printStackTrace();
    }
}
 
Example #20
Source File: AbstractSQLTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private static void createSchema(final String dbName, final DatabaseType databaseType) {
    try {
        Connection conn = databaseTypeMap.get(databaseType).get(dbName).getConnection();
        RunScript.execute(conn, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("jdbc_init.sql")));
        conn.close();
    } catch (final SQLException ex) {
        ex.printStackTrace();
    }
}
 
Example #21
Source File: H2CacheStoreStrategy.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public void resetStore() {
    try (Connection conn = connection()) {
        RunScript.execute(conn, new StringReader("delete from CACHE;"));
        RunScript.execute(conn, new StringReader(POPULATE_STATS_TABLE));
    }
    catch (SQLException e) {
        throw new IgniteException(e);
    }
}
 
Example #22
Source File: AbstractSpringJUnitTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Before
public void createSchema() throws SQLException {
    for (String each : getSchemaFiles()) {
        RunScript.execute(createDataSource(each).getConnection(), new InputStreamReader(classLoader.getResourceAsStream(each)));
    }
    reInitMetaData();
}
 
Example #23
Source File: SchemaEnvironmentManager.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Create database.
 *
 * @param ruleType rule type
 * @throws IOException IO exception
 * @throws JAXBException JAXB exception
 * @throws SQLException SQL exception
 */
public static void createDatabase(final String ruleType) throws IOException, JAXBException, SQLException {
    SchemaEnvironment databaseInitialization = unmarshal(EnvironmentPath.getDatabaseEnvironmentResourceFile(ruleType));
    for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) {
        DataSource dataSource = DataSourceUtil.createDataSource(each, null);
        try (
                Connection connection = dataSource.getConnection();
                StringReader stringReader = new StringReader(Joiner.on(";\n").skipNulls().join(generateCreateDatabaseSQLs(each, databaseInitialization.getDatabases())))) {
            RunScript.execute(connection, stringReader);
        }
    }
}
 
Example #24
Source File: SchemaEnvironmentManager.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
/**
 * Drop database.
 *
 * @param ruleType rule type
 * @throws IOException IO exception
 * @throws JAXBException JAXB exception
 */
public static void dropDatabase(final String ruleType) throws IOException, JAXBException {
    SchemaEnvironment databaseInitialization = unmarshal(EnvironmentPath.getDatabaseEnvironmentResourceFile(ruleType));
    for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) {
        DataSource dataSource = DataSourceUtil.createDataSource(each, null);
        try (
                Connection connection = dataSource.getConnection();
                StringReader stringReader = new StringReader(Joiner.on(";\n").skipNulls().join(generateDropDatabaseSQLs(each, databaseInitialization.getDatabases())))) {
            RunScript.execute(connection, stringReader);
        } catch (final SQLException ex) {
            // TODO database maybe not exist
        }
    }
}
 
Example #25
Source File: H2JpaCleaner.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void clean() {
  super.clean();
  if (server != null) {
    server.shutdown();
  } else {
    try (Connection conn = dataSource.getConnection()) {
      RunScript.execute(conn, new StringReader("SHUTDOWN"));
    } catch (SQLException x) {
      throw new RuntimeException(x.getMessage(), x);
    }
  }
}
 
Example #26
Source File: H2DBTestServer.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void shutdown() {
  try (Connection conn = getDataSource().getConnection()) {
    RunScript.execute(conn, new StringReader("SHUTDOWN"));
  } catch (SQLException x) {
    throw new RuntimeException(x);
  }
}
 
Example #27
Source File: H2DBTestServer.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void start() {
  final JdbcDataSource dataSource = new JdbcDataSource();
  dataSource.setUrl(getUrl() + ";DB_CLOSE_DELAY=-1");
  try (Connection conn = dataSource.getConnection()) {
    RunScript.execute(conn, new StringReader("SELECT 1"));
  } catch (SQLException x) {
    throw new RuntimeException(x);
  }
}
 
Example #28
Source File: DbH2ServerStartup.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Populate sample database.
 *
 * @throws SQLException if
 */
public static void populateDatabase() throws SQLException {
    // Try to connect to database TCP server.
    JdbcConnectionPool dataSrc = JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", "");

    // Create Person table in database.
    RunScript.execute(dataSrc.getConnection(), new StringReader(CREATE_PERSON_TABLE));

    // Populates Person table with sample data in database.
    RunScript.execute(dataSrc.getConnection(), new StringReader(POPULATE_PERSON_TABLE));
}
 
Example #29
Source File: FlywaySchemaInitializerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private Set<TestEntity> queryEntities() throws SQLException {
  final Set<TestEntity> entities = new HashSet<>();
  try (Connection conn = dataSource.getConnection()) {
    final ResultSet result = RunScript.execute(conn, new StringReader("SELECT * FROM test"));
    while (result.next()) {
      entities.add(new TestEntity(result.getLong("id"), result.getString("text")));
    }
  }
  return entities;
}
 
Example #30
Source File: FlywaySchemaInitializerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@AfterMethod
public void cleanup() throws SQLException, URISyntaxException {
  try (Connection conn = dataSource.getConnection()) {
    RunScript.execute(conn, new StringReader("SHUTDOWN"));
  }
  IoUtil.deleteRecursive(targetDir().resolve(Paths.get(SCRIPTS_ROOT)).toFile());
}