Example usage for org.apache.ibatis.jdbc ScriptRunner ScriptRunner

List of usage examples for org.apache.ibatis.jdbc ScriptRunner ScriptRunner

Introduction

In this page you can find the example usage for org.apache.ibatis.jdbc ScriptRunner ScriptRunner.

Prototype

public ScriptRunner(Connection connection) 

Source Link

Usage

From source file:com.restservice.serviceLogic.test.IntegrationTest.java

License:Open Source License

@BeforeClass
public static void resetDatabase() {
    properties = new File(System.getProperty("user.home") + "/database_test.properties");
    // Check if properties file exists. Tests don't start if it doesn't.
    if (!properties.isFile()) {
        fail("No test database properties file found at: " + properties.getPath());
    }/*from w w  w .j  av a2 s . c om*/

    FileInputStream fis = null;

    try {
        // initialize transactor and services with this file
        transactor = new Transactor(properties.getPath());
        queryService = new QueryService(properties.getPath());
        resultService = new ResultService(properties.getPath());

        Properties props = new Properties();
        fis = new FileInputStream(properties.getPath());
        props.load(fis);
        if (!props.containsKey("database.name")) {
            fail("Please add a database.name setting to your local database.properties file");
        }

        Class.forName(props.getProperty("javabase.jdbc.driver"));

        String dbUrl = props.getProperty("javabase.jdbc.url") + "?user="
                + props.getProperty("javabase.jdbc.username") + "&password="
                + props.getProperty("javabase.jdbc.password");

        Path path = Paths.get("src/test/resources/Dump.sql");
        Path newPath = Paths.get("src/test/resources/LocalDump.sql");
        Charset charset = StandardCharsets.UTF_8;

        String content = new String(Files.readAllBytes(path), charset);

        content = content.replaceAll("resttest", props.getProperty("database.name"));
        Files.write(newPath, content.getBytes(charset));

        Reader reader = Resources.getResourceAsReader("LocalDump.sql");

        Connection connection = DriverManager.getConnection(dbUrl);

        ScriptRunner sr = new ScriptRunner(connection);

        sr.setDelimiter(";");
        sr.setLogWriter(null);
        sr.setErrorLogWriter(null);
        sr.runScript(reader);

        connection.commit();
        reader.close();
    } catch (SQLException sqle) {
        sqle.printStackTrace();
        fail("fail");
    } catch (Exception e) {
        fail(e.getMessage());
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.technophobia.substeps.database.impl.SQLSubStepImplementations.java

License:Open Source License

/**
 * Executes the given sql script// w w w.  java 2s .  c  om
 * 
 * @example ExecuteScript "/myScript.sql"
 * @section Database
 * @param script
 *            the sql script which should be on the classpath
 */
@SubSteps.Step("ExecuteScript \"([^\"]*)\"")
public void executeScript(final String script) {

    LOG.debug("Executing sql script [{}]", script);

    Connection connection = null;
    InputStream scriptStream = null;

    try {

        connection = DatabaseSetupTearDown.getConnectionContext().getConnection();
        final ScriptRunner scriptRunner = new ScriptRunner(connection);

        scriptStream = SQLSubStepImplementations.class.getResourceAsStream(script);

        Assert.assertNotNull("Unable to find script: " + script, scriptStream);

        final Reader scriptReader = new InputStreamReader(scriptStream);

        scriptRunner.runScript(scriptReader);

    } finally {

        Database.close(connection);
        Database.close(scriptStream);
    }

}

From source file:com.tianjunwei.dynamic.SimpleTableAnnotatedMapperTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);//w  ww .jav a 2  s  .c  o  m
    InputStream is = getClass().getResourceAsStream("/examples/simple/CreateSimpleDB.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(SimpleTableAnnotatedMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:examples.animal.data.AnimalDataTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);//from   ww  w.j  ava 2  s  .  co  m
    InputStream is = getClass().getResourceAsStream("/examples/animal/data/CreateAnimalData.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(AnimalDataMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:examples.column.comparison.ColumnComparisonTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);/*w  ww .j  a  va 2 s  . c o  m*/
    InputStream is = getClass().getResourceAsStream("/examples/column/comparison/CreateDB.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(ColumnComparisonMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:examples.generated.always.mybatis.GeneratedAlwaysAnnotatedMapperTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);/*from   w  ww .  j ava  2 s.co  m*/
    InputStream is = getClass().getResourceAsStream("/examples/generated/always/CreateGeneratedAlwaysDB.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(GeneratedAlwaysAnnotatedMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:examples.groupby.GroupByTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);/*from w  w  w  .ja  v a2  s  . c o  m*/
    InputStream is = getClass().getResourceAsStream("/examples/groupby/CreateGroupByDB.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(GroupByMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:examples.joins.JoinMapperTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);//w  w w .  j a va 2  s . c om
    InputStream is = getClass().getResourceAsStream("/examples/joins/CreateJoinDB.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(JoinMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:examples.paging.LimitAndOffsetTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);//from   w  w  w .  j a  v a  2 s .co  m
    InputStream is = getClass().getResourceAsStream("/examples/animal/data/CreateAnimalData.sql");
    try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
        ScriptRunner sr = new ScriptRunner(connection);
        sr.setLogWriter(null);
        sr.runScript(new InputStreamReader(is));
    }

    UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
    Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
    Configuration config = new Configuration(environment);
    config.addMapper(LimitAndOffsetMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}

From source file:fsm.process.test.ProcessTest.java

/**
 * ?/*  ww  w.jav a 2  s . com*/
 * 
 * */
public static void initDataBase() throws Exception {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String url = "jdbc:mysql://localhost:3306/fsm?useUnicode=true&characterEncoding=UTF-8";
    String username = "gpps";
    String password = "111111";
    Connection conn = (Connection) DriverManager.getConnection(url, username, password);
    ScriptRunner runner = new ScriptRunner(conn);
    runner.runScript(new InputStreamReader(ProcessTest.class.getResourceAsStream("init.sql")));
    conn.close();
}