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:org.mybatis.cdi.ManagerProducers.java

License:Apache License

private SqlSessionFactory createSessionManagerJTA() throws IOException {
    Reader reader = Resources.getResourceAsReader("org/mybatis/cdi/mybatis-config_jta.xml");
    SqlSessionFactory manager = new SqlSessionFactoryBuilder().build(reader);
    reader.close();// ww  w .ja  va2s . co m

    SqlSession session = manager.openSession();
    Connection conn = session.getConnection();
    reader = Resources.getResourceAsReader("org/mybatis/cdi/CreateDB_JTA.sql");
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.runScript(reader);
    reader.close();
    session.close();

    return manager;
}

From source file:org.mybatis.guice.AbstractGuiceTestExtension.java

License:Apache License

public AbstractGuiceTestExtension() throws SQLException {
    final Contact contact = new Contact();
    contact.setFirstName("John");
    contact.setLastName("Doe");
    contact.setCreated(new CustomType(currentTimeMillis()));
    contact.setAddress(null);/* ww w. ja v a  2  s  .com*/

    final Contact contactWithAddress = new Contact();
    contactWithAddress.setFirstName("John");
    contactWithAddress.setLastName("Doe");
    contactWithAddress.setCreated(new CustomType(currentTimeMillis()));

    final Address address = new Address();
    address.setNumber(1234);
    address.setStreet("Elm street");
    contactWithAddress.setAddress(address);

    final Counter counter = new Counter();

    // bindings
    final List<Module> modules = this.createMyBatisModule();
    modules.add(new Module() {
        @Override
        public void configure(Binder binder) {
            bindProperties(binder, createTestProperties());
            binder.bind(Contact.class).toInstance(contact);
            binder.bind(Contact.class).annotatedWith(named("contactWithAddress"))
                    .toInstance(contactWithAddress);
            binder.bind(Counter.class).toInstance(counter);
        }
    });
    this.injector = createInjector(modules);

    // prepare the test db
    final Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration()
            .getEnvironment();
    final DataSource dataSource = environment.getDataSource();
    final ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
    runner.setAutoCommit(true);
    runner.setStopOnError(true);
    runner.runScript(new StringReader("DROP TABLE IF EXISTS contact;"
            + "CREATE TABLE contact (id int GENERATED BY DEFAULT AS IDENTITY (START WITH 1), "
            + "first_name VARCHAR(20) NOT NULL, " + "last_name VARCHAR(20) NOT NULL, " + "created TIMESTAMP, "
            + "address VARCHAR(100) DEFAULT NULL) ;"));
    runner.closeConnection();
}

From source file:org.mybatis.guice.AbstractGuiceTestRunner.java

License:Apache License

public AbstractGuiceTestRunner(Class<?> klass) throws InitializationError {
    super(klass);

    try {/*from ww w .ja  v a 2 s.  co  m*/
        final Contact contact = new Contact();
        contact.setFirstName("John");
        contact.setLastName("Doe");
        contact.setCreated(new CustomType(currentTimeMillis()));
        contact.setAddress(null);

        final Contact contactWithAddress = new Contact();
        contactWithAddress.setFirstName("John");
        contactWithAddress.setLastName("Doe");
        contactWithAddress.setCreated(new CustomType(currentTimeMillis()));

        Address address = new Address();
        address.setNumber(1234);
        address.setStreet("Elm street");
        contactWithAddress.setAddress(address);

        final Counter counter = new Counter();

        // bindings
        List<Module> modules = this.createMyBatisModule();
        modules.add(new Module() {
            public void configure(Binder binder) {
                bindProperties(binder, createTestProperties());
                binder.bind(Contact.class).toInstance(contact);
                binder.bind(Contact.class).annotatedWith(named("contactWithAddress"))
                        .toInstance(contactWithAddress);
                binder.bind(Counter.class).toInstance(counter);
            }
        });
        this.injector = createInjector(modules);

        // prepare the test db
        Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration()
                .getEnvironment();
        DataSource dataSource = environment.getDataSource();
        ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
        runner.setAutoCommit(true);
        runner.setStopOnError(true);
        runner.runScript(new StringReader("DROP TABLE IF EXISTS contact;"
                + "CREATE TABLE contact (id int GENERATED BY DEFAULT AS IDENTITY (START WITH 1), "
                + "first_name VARCHAR(20) NOT NULL, " + "last_name VARCHAR(20) NOT NULL, "
                + "created TIMESTAMP, " + "address VARCHAR(100) DEFAULT NULL) ;"));
        runner.closeConnection();
    } catch (Exception e) {
        throw new InitializationError(e);
    }
}

From source file:org.mybatis.guice.CleanDatabaseRule.java

License:Apache License

public void evaluate() throws Exception {
    ScriptRunner runner = new ScriptRunner(
            sqlSession.getConfiguration().getEnvironment().getDataSource().getConnection());
    try {/* w w  w .ja  v a  2  s .  c o m*/
        runner.setAutoCommit(true);
        runner.setStopOnError(true);
        runner.runScript(new StringReader(
                "DELETE FROM contact; " + "INSERT INTO contact (id, first_name, last_name, created) "
                        + "VALUES (1, '" + contact.getFirstName() + "', '" + contact.getLastName() + "', '"
                        + new Timestamp(contactWithAddress.getCreated().getValue()) + "'); "
                        + "INSERT INTO contact (id, first_name, last_name, created, address) " + "VALUES (2, '"
                        + contactWithAddress.getFirstName() + "', '" + contactWithAddress.getLastName() + "', '"
                        + new Timestamp(contactWithAddress.getCreated().getValue()) + "', '"
                        + addressConverter.convert(contactWithAddress.getAddress()) + "'); "));
        contact.setId(1);
        contactWithAddress.setId(2);
    } finally {
        runner.closeConnection();
    }
}

From source file:org.mybatis.guice.nestedtx.NestedTxTest.java

License:Apache License

@BeforeEach
public void setup() throws Exception {
    injector = Guice.createInjector(new MyBatisModule() {
        @Override//from www .  jav  a 2 s .co m
        protected void initialize() {
            bindDataSourceProviderType(PooledDataSourceProvider.class);
            bindTransactionFactoryType(JdbcTransactionFactory.class);

            install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED);

            Properties connectionProps = new Properties();
            connectionProps.setProperty("mybatis.environment.id", "jdbc");
            connectionProps.setProperty("JDBC.username", "sa");
            connectionProps.setProperty("JDBC.password", "");
            connectionProps.setProperty("JDBC.autoCommit", "false");

            Names.bindProperties(binder(), connectionProps);

            addMapperClass(NestedTxMapper.class);
            bind(NestedTxService.class);
        }
    });

    // prepare the test db
    Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration()
            .getEnvironment();
    DataSource dataSource = environment.getDataSource();
    ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
    runner.setAutoCommit(true);
    runner.setStopOnError(true);
    runner.runScript(getResourceAsReader("org/mybatis/guice/nestedtx/setupdb.sql"));
    runner.closeConnection();

    service = injector.getInstance(NestedTxService.class);
}

From source file:org.mybatis.guice.sample.SampleBasicTest.java

License:Apache License

@BeforeEach
public void setupMyBatisGuice() throws Exception {

    // bindings/* ww  w  .  j  av  a 2 s .c  o m*/
    this.injector = createInjector(new MyBatisModule() {

        @Override
        protected void initialize() {
            install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED);

            bindDataSourceProviderType(PooledDataSourceProvider.class);
            bindTransactionFactoryType(JdbcTransactionFactory.class);
            addMapperClass(UserMapper.class);

            bindProperties(binder(), createTestProperties());
            bind(FooService.class).to(FooServiceMapperImpl.class);
        }

    });

    // prepare the test db
    Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration()
            .getEnvironment();
    DataSource dataSource = environment.getDataSource();
    ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
    runner.setAutoCommit(true);
    runner.setStopOnError(true);
    runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-schema.sql"));
    runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-test-data.sql"));
    runner.closeConnection();

    this.fooService = this.injector.getInstance(FooService.class);
}

From source file:org.mybatis.guice.sample.SampleSqlSessionTest.java

License:Apache License

@BeforeEach
public void setupMyBatisGuice() throws Exception {

    // bindings//from  ww  w  . ja v  a  2 s.  c o m
    List<Module> modules = this.createMyBatisModule();
    this.injector = Guice.createInjector(modules);

    // prepare the test db
    Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration()
            .getEnvironment();
    DataSource dataSource = environment.getDataSource();
    ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
    runner.setAutoCommit(true);
    runner.setStopOnError(true);
    runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-schema.sql"));
    runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-test-data.sql"));
    runner.closeConnection();

    this.fooService = this.injector.getInstance(FooService.class);
}

From source file:org.mybatis.guice.sample.SampleTestBase.java

License:Apache License

private void createTestDb(Injector injector) throws IOException, SQLException {
    // prepare the test db
    Environment environment = injector.getInstance(SqlSessionFactory.class).getConfiguration().getEnvironment();
    DataSource dataSource = environment.getDataSource();

    ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
    runner.setAutoCommit(true);// w  ww  .j ava  2 s .co m
    runner.setStopOnError(true);
    runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-schema.sql"));
    runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-test-data.sql"));
    runner.closeConnection();
}

From source file:org.mybatis.scripting.freemarker.CustomizedDataContextTest.java

License:Apache License

@BeforeAll
public static void setUp() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");

    JDBCDataSource dataSource = new JDBCDataSource();
    dataSource.setUrl("jdbc:hsqldb:mem:db4");
    dataSource.setUser("sa");
    dataSource.setPassword("");

    try (Connection conn = dataSource.getConnection()) {
        try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
            ScriptRunner runner = new ScriptRunner(conn);
            runner.setLogWriter(null);/*  w  w w  .  j  a v a  2s .  co m*/
            runner.setErrorLogWriter(null);
            runner.runScript(reader);
            conn.commit();
        }
    }

    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);

    // You can call configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class)
    // after this to use FreeMarker driver by default.
    Configuration configuration = new Configuration(environment);

    configuration.addMapper(CustomizedDataContextMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}

From source file:org.mybatis.scripting.freemarker.FreeMarkerInAnnotationsTest.java

License:Apache License

@BeforeAll
public static void setUp() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");

    JDBCDataSource dataSource = new JDBCDataSource();
    dataSource.setUrl("jdbc:hsqldb:mem:db1");
    dataSource.setUser("sa");
    dataSource.setPassword("");

    try (Connection conn = dataSource.getConnection()) {
        try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
            ScriptRunner runner = new ScriptRunner(conn);
            runner.setLogWriter(null);//from  w w  w  .  j av  a  2 s .  com
            runner.setErrorLogWriter(null);
            runner.runScript(reader);
            conn.commit();
        }
    }

    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("development", transactionFactory, dataSource);

    // You can call configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class)
    // after this to use FreeMarker driver by default.
    Configuration configuration = new Configuration(environment);

    configuration.addMapper(NameMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}