Search in sources :

Example 31 with ScriptRunner

use of org.apache.ibatis.jdbc.ScriptRunner in project mybatis-3 by mybatis.

the class CustomCollectionHandlingTest method initDb.

private static void initDb(Connection conn) throws IOException, SQLException {
    try {
        Reader scriptReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/custom_collection_handling/CreateDB.sql");
        ScriptRunner runner = new ScriptRunner(conn);
        runner.setLogWriter(null);
        runner.setErrorLogWriter(new PrintWriter(System.err));
        runner.runScript(scriptReader);
        conn.commit();
        scriptReader.close();
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : Reader(java.io.Reader) ScriptRunner(org.apache.ibatis.jdbc.ScriptRunner) PrintWriter(java.io.PrintWriter)

Example 32 with ScriptRunner

use of org.apache.ibatis.jdbc.ScriptRunner in project mybatis-3 by mybatis.

the class EncodingTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    Connection conn = null;
    // save charset
    Charset charset = Resources.getCharset();
    try {
        Class.forName("org.hsqldb.jdbcDriver");
        conn = DriverManager.getConnection("jdbc:hsqldb:mem:encoding", "sa", "");
        // make sure that the SQL file has been saved in UTF-8!
        Resources.setCharset(Charset.forName("utf-8"));
        Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/encoding/CreateDB.sql");
        ScriptRunner runner = new ScriptRunner(conn);
        runner.setLogWriter(null);
        runner.setErrorLogWriter(null);
        runner.runScript(reader);
        conn.commit();
        reader.close();
        reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/encoding/EncodingConfig.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        reader.close();
    } finally {
        // restore charset
        Resources.setCharset(charset);
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : Connection(java.sql.Connection) Charset(java.nio.charset.Charset) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) ScriptRunner(org.apache.ibatis.jdbc.ScriptRunner) BeforeClass(org.junit.BeforeClass)

Example 33 with ScriptRunner

use of org.apache.ibatis.jdbc.ScriptRunner in project mybatis-3 by mybatis.

the class EnumTypeHandlerUsingAnnotationTest method initDatabase.

@BeforeClass
public static void initDatabase() throws Exception {
    Connection conn = null;
    try {
        Class.forName("org.hsqldb.jdbcDriver");
        conn = DriverManager.getConnection("jdbc:hsqldb:mem:enumtypehandler_on_annotation", "sa", "");
        Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/enumtypehandler_on_annotation/CreateDB.sql");
        ScriptRunner runner = new ScriptRunner(conn);
        runner.setLogWriter(null);
        runner.setErrorLogWriter(null);
        runner.runScript(reader);
        conn.commit();
        reader.close();
        reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/enumtypehandler_on_annotation/mybatis-config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        sqlSessionFactory.getConfiguration().getMapperRegistry().addMapper(PersonMapper.class);
        reader.close();
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : Connection(java.sql.Connection) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) ScriptRunner(org.apache.ibatis.jdbc.ScriptRunner) BeforeClass(org.junit.BeforeClass)

Example 34 with ScriptRunner

use of org.apache.ibatis.jdbc.ScriptRunner in project mybatis-3 by mybatis.

the class ExtendTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    Connection conn = null;
    try {
        Class.forName("org.hsqldb.jdbcDriver");
        conn = DriverManager.getConnection("jdbc:hsqldb:mem:extend", "sa", "");
        Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/extend/CreateDB.sql");
        ScriptRunner runner = new ScriptRunner(conn);
        runner.setLogWriter(null);
        runner.setErrorLogWriter(null);
        runner.runScript(reader);
        conn.commit();
        reader.close();
        reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/extend/ExtendConfig.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        reader.close();
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : Connection(java.sql.Connection) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) ScriptRunner(org.apache.ibatis.jdbc.ScriptRunner) BeforeClass(org.junit.BeforeClass)

Example 35 with ScriptRunner

use of org.apache.ibatis.jdbc.ScriptRunner in project mybatis-3 by mybatis.

the class ExtendResultMapTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    // create a SqlSessionFactory
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/extendresultmap/mybatis-config.xml");
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    reader.close();
    // populate in-memory database
    SqlSession session = sqlSessionFactory.openSession();
    Connection conn = session.getConnection();
    reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/extendresultmap/CreateDB.sql");
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.runScript(reader);
    reader.close();
    session.close();
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) Connection(java.sql.Connection) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) ScriptRunner(org.apache.ibatis.jdbc.ScriptRunner) BeforeClass(org.junit.BeforeClass)

Aggregations

ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)130 Reader (java.io.Reader)126 Connection (java.sql.Connection)114 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)109 BeforeClass (org.junit.BeforeClass)91 SqlSession (org.apache.ibatis.session.SqlSession)69 Before (org.junit.Before)17 PrintWriter (java.io.PrintWriter)8 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)4 Configuration (org.apache.ibatis.session.Configuration)2 StringReader (java.io.StringReader)1 Charset (java.nio.charset.Charset)1 Properties (java.util.Properties)1 NullWriter (org.apache.commons.io.output.NullWriter)1 UnpooledDataSource (org.apache.ibatis.datasource.unpooled.UnpooledDataSource)1 Environment (org.apache.ibatis.mapping.Environment)1 JdbcTransactionFactory (org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory)1 Test (org.junit.Test)1