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

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

Introduction

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

Prototype

public void setLogWriter(PrintWriter logWriter) 

Source Link

Usage

From source file:com.collective.messages.persistence.dao.BaseDataTestCase.java

License:Apache License

public static void runScript(DataSource ds, String resource) throws IOException, SQLException {
    Connection connection = ds.getConnection();
    try {//  w ww. jav a2  s.c o  m
        ScriptRunner runner = new ScriptRunner(connection);
        runner.setAutoCommit(true);
        runner.setStopOnError(false);
        runner.setLogWriter(null);
        runner.setErrorLogWriter(null);
        runScript(runner, resource);
    } finally {
        connection.close();
    }
}

From source file:com.daemon.SearchTermTest.java

License:Open Source License

@BeforeClass
public static void prepareDatabase() throws SQLException {
    try {//from w w w . jav  a 2  s .c o  m
        transactor = new Transactor();
        connection = DriverManager.getConnection(transactor.getDbUrl());

        // Prepare database
        Reader reader = Resources.getResourceAsReader("DaemonDump.sql");

        ScriptRunner sr = new ScriptRunner(connection);

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

        connection.commit();
        reader.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        connection.close();
    }
}

From source file:com.daemon.TransactorTest.java

License:Open Source License

@BeforeClass
public static void beforeClass() {
    try {//  w  ww .j  av a 2  s. c o  m
        _dbPath = Transactor.DATABASE_PROPERTY_PATH;
        URL url = Resources.getResourceURL("database.properties");
        Transactor.DATABASE_PROPERTY_PATH = URLDecoder.decode(url.getFile(), "UTF-8");
        _trans = new Transactor();
        _trans.connect();

        // Prepare database
        Reader reader = Resources.getResourceAsReader("DaemonDump.sql");

        ScriptRunner sr = new ScriptRunner(_trans.getConnection());

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

        _trans.getConnection().commit();
        reader.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.persinity.common.db.SimpleRelDb.java

License:Apache License

private void configure(final ScriptRunner scriptRunner) {
    // All statements are separated by "/" (put on new line as is the Oracle script style)
    scriptRunner.setDelimiter("/");
    scriptRunner.setFullLineDelimiter(true);

    // Abort on error, as once deviated, subsequent stmts may lead the DB to unpredictable state
    scriptRunner.setStopOnError(true);/*  w w  w. j ava 2s .  c  om*/

    // Do not auto commit, leave that to the RelDb user
    scriptRunner.setAutoCommit(false);

    // Log script feedback
    // Aways set log writer otherwise it will dump on System.out
    scriptRunner.setLogWriter(new PrintWriter(new Log4jOutputStream(log.getLogger(), Level.DEBUG)));
    scriptRunner.setErrorLogWriter(new PrintWriter(new Log4jOutputStream(log.getLogger(), Level.ERROR)));
}

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 ww  w  .  j ava 2s  .  co m

    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.tianjunwei.dynamic.SimpleTableAnnotatedMapperTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    Class.forName(JDBC_DRIVER);/*from   w w  w .  j a v a 2s  .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 w w  w .j  av a2  s .com*/
    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);// www. j a  v  a2  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);/*w  w  w .j  a  v a  2s.  com*/
    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);//  w  w w. j  av  a 2 s.co  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);
}