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

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

Introduction

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

Prototype

public void setDelimiter(String delimiter) 

Source Link

Usage

From source file:com.daemon.SearchTermTest.java

License:Open Source License

@BeforeClass
public static void prepareDatabase() throws SQLException {
    try {/*w w w  . ja v  a  2  s  . com*/
        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 {//from  www  .  j a v 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);/*from   w ww. j  av a2 s. c o m*/

    // 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   w  w  w  .  ja va 2s.  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:org.craftercms.studio.impl.v1.dal.DataSourceInitializerImpl.java

License:Open Source License

@Override
public void initDataSource() {
    if (isEnabled()) {
        String scriptPath = getScriptPath();
        SqlSession session = sqlSessionFactory.openSession();
        Connection conn = session.getConnection();

        ScriptRunner sr = new ScriptRunner(conn);

        sr.setDelimiter(delimiter);
        InputStream is = getClass().getClassLoader().getResourceAsStream(scriptPath);
        Reader reader = new InputStreamReader(is);
        sr.runScript(reader);//from   w ww.  j  av a 2  s.  c  o m
        try {
            conn.close();
        } catch (SQLException e) {
            logger.error("Error while closing connection with database", e);
        }
    }
}

From source file:org.sonar.core.persistence.DdlUtils.java

License:Open Source License

private static ScriptRunner newScriptRunner(Connection connection) {
    ScriptRunner scriptRunner = new ScriptRunner(connection);
    scriptRunner.setDelimiter(";");
    scriptRunner.setStopOnError(true);//from  ww w .  j a va  2  s. co m
    scriptRunner.setLogWriter(new PrintWriter(new NullWriter()));
    return scriptRunner;
}