org.javalite.activejdbc.Base Java Examples

The following examples show how to use org.javalite.activejdbc.Base. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CheckMojo.java    From javalite with Apache License 2.0 6 votes vote down vote up
public void executeMojo() throws MojoExecutionException {
    getLog().info("Checking " + getUrl() + " using migrations from " + getMigrationsPath());

    List<Migration> pendingMigrations;
    try {
        openConnection();
        MigrationManager manager = new MigrationManager(getMigrationsPath());
        pendingMigrations = manager.getPendingMigrations();
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to check " + getUrl(), e);
    }finally {
        Base.close();
    }

    if (pendingMigrations.isEmpty()) return;

    getLog().warn("Pending migration(s): ");
    for (Migration migration : pendingMigrations)
        getLog().warn("Migration: " + migration.getName());

    getLog().warn("Run db-migrator:migrate to apply pending migrations.");
    throw new MojoExecutionException("Pending migration(s) exist, migrate your db and try again.");
}
 
Example #2
Source File: OracleDBReset.java    From javalite with Apache License 2.0 6 votes vote down vote up
static void resetOracle(String[] statements) throws SQLException {
        dropTriggers();
        dropSequences();
        dropTables();

        for (String statement : statements) {
            if (!Util.blank(statement)) {
                Statement st = null;
                try {
                    st = Base.connection().createStatement();
                    st.executeUpdate(statement);
                } catch(SQLException e) {
                    System.out.println("Problem statement: " + statement);
                    throw e;
                } finally {
                    closeQuietly(st);
                }
            }
        }
}
 
Example #3
Source File: ValidateMojo.java    From javalite with Apache License 2.0 6 votes vote down vote up
public void executeMojo() throws MojoExecutionException {
    getLog().info("Validating " + getUrl() + " using migrations from " + getMigrationsPath());
    try {
        openConnection();
        MigrationManager manager = new MigrationManager(getMigrationsPath());
        List<Migration> pendingMigrations = manager.getPendingMigrations();

        getLog().info("Database: " + getUrl());
        getLog().info("Up-to-date: " + pendingMigrations.isEmpty());
        if (!pendingMigrations.isEmpty()) {
            getLog().info("Pending Migrations: ");
            for (Migration migration : pendingMigrations) {
                getLog().info(migration.getName());
            }
        }else{
            getLog().info("No pending migrations found");
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to validate " + getUrl(), e);
    }finally {
        Base.close();
    }
}
 
Example #4
Source File: OracleDBReset.java    From javalite with Apache License 2.0 5 votes vote down vote up
private static void dropSequences() throws SQLException {

        List<Map> sequences = Base.findAll("select sequence_name from user_sequences");
        for (Map sequence : sequences) {
            Statement s = Base.connection().createStatement();
            s.execute("drop sequence " + sequence.get("sequence_name"));
            System.out.println("Dropping sequence: " + sequence.get("sequence_name"));
            s.close();
        }
    }
 
Example #5
Source File: DefaultDBReset.java    From javalite with Apache License 2.0 5 votes vote down vote up
private static void resetSchema(String[] statements) throws SQLException {
    Connection connection = Base.connection();
    for (String statement : statements) {
        if (!Util.blank(statement)) {
            Statement st = connection.createStatement();
            st.executeUpdate(statement);
            st.close();
        }
    }
}
 
Example #6
Source File: DefaultDBReset.java    From javalite with Apache License 2.0 5 votes vote down vote up
public static void after() {

        try {
            Base.connection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Base.close();
    }
 
Example #7
Source File: MySQLMigrationSpec.java    From javalite with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() throws Exception {
    try {
        exec("drop database mysql_migration_test");
    } catch (Exception e) {/*ignore*/}
    Base.close();
}
 
Example #8
Source File: DBSpecTest.java    From javalite with Apache License 2.0 5 votes vote down vote up
@Test
public void f_shouldFindRecordsFromPrevious_NOT_Rolledback_Transaction(){
    a(Base.count("patients")).shouldBeEqual(1);

    //lets cleanup after ourselves just in case
    setRollback(false);
    Base.exec("delete from patients");
}
 
Example #9
Source File: MySQLMigrationSpec.java    From javalite with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {

    Base.open(driver(), url(), user(), password());
    try {
        exec("drop database mysql_migration_test");
    } catch (Exception e) {/*ignore*/}
    exec("create database mysql_migration_test");
    Base.close();
    Base.open(driver(), "jdbc:mysql://localhost/mysql_migration_test", user(), password());
    migrationManager = new MigrationManager("src/test/resources/test_migrations/mysql/");
}
 
Example #10
Source File: VersionStrategy.java    From javalite with Apache License 2.0 5 votes vote down vote up
public void recordMigration(String version, Date startTime, long duration) {

        try{
            Base.exec("insert into " + VERSION_TABLE + " values (?, ?, ?)", version, new Timestamp(startTime.getTime()), duration);
        }catch(Exception e){
            throw  new MigrationException(e);
        }
    }
 
Example #11
Source File: MigrateMojo.java    From javalite with Apache License 2.0 5 votes vote down vote up
public void executeMojo() throws MojoExecutionException {
    getLog().info("Migrating " + getUrl() + " using migrations at " + getMigrationsPath());
    try {
        openConnection();
        new MigrationManager(getMigrationsPath()).migrate(getLog(), getEncoding());
    } catch(SQLException e){
        throw new MojoExecutionException("Failed to migrate database " + getUrl(), e);
    } finally {
        Base.close();
    }
}
 
Example #12
Source File: Db2DBReset.java    From javalite with Apache License 2.0 5 votes vote down vote up
static void resetSchema(String[] statements) throws SQLException {
    Connection connection = Base.connection();

    createDropTableProcedure(connection);

    for (String statement: statements) {
        if(Util.blank(statement)) continue;
        Statement st = connection.createStatement();
        st.executeUpdate(statement);
        st.close();
    }
}
 
Example #13
Source File: OracleDBReset.java    From javalite with Apache License 2.0 5 votes vote down vote up
private static void dropTables() throws SQLException {
    List<Map> tables = Base.findAll("select table_name from user_tables");
    for (Map table : tables) {
        Statement s = Base.connection().createStatement();
        s.execute("drop table " + table.get("table_name") + " cascade constraints");
        System.out.println("Dropping table: " + table.get("table_name"));
        s.close();
    }
}
 
Example #14
Source File: OracleDBReset.java    From javalite with Apache License 2.0 5 votes vote down vote up
private static void dropTriggers() throws SQLException {

        List<Map> triggers = Base.findAll("select trigger_name from user_triggers");
        for (Map trigger : triggers) {
            if(trigger.get("trigger_name").toString().contains("$")){
                continue;   
            }
            Statement s = Base.connection().createStatement();
            String sql = "drop trigger " + trigger.get("trigger_name");
            System.out.println("Dropping trigger: " + trigger.get("trigger_name"));
            s.execute(sql);
            s.close();
        }
    }
 
Example #15
Source File: DropMojo.java    From javalite with Apache License 2.0 5 votes vote down vote up
public void executeMojo() throws MojoExecutionException {
    try {
        String dropSql = blank(getDropSql()) ? "drop database %s" : getDropSql();
        openConnection(true);
        exec(format(dropSql, DbUtils.extractDatabaseName(getUrl())));
        getLog().info("Dropped database " + getUrl());
    } finally {
        Base.close();
    }
}
 
Example #16
Source File: ActiveJDBCApp.java    From tutorials with MIT License 5 votes vote down vote up
public static void main( String[] args )
{
    try {
        Base.open();
        ActiveJDBCApp app = new ActiveJDBCApp();
        app.create();
        app.update();
        app.delete();
        app.deleteCascade();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        Base.close();
    }
}
 
Example #17
Source File: ProductUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
public void givenSavedProduct_WhenFindFirst_ThenSavedProductIsReturned() {
    //Open DB connection
    Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password");

    //Create a product and save it
    Product toSaveProduct = new Product();
    toSaveProduct.set("name", "Bread");
    toSaveProduct.saveIt();

    //Find product
    Product savedProduct = Product.findFirst("name = ?", "Bread");

    Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name"));
}
 
Example #18
Source File: DbUtils.java    From javalite with Apache License 2.0 5 votes vote down vote up
public static int exec(String statement){
    try {
        LOGGER.info("Executing: " + statement);
        return Base.exec(statement);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #19
Source File: PubmedArticleEntity.java    From bluima with Apache License 2.0 5 votes vote down vote up
public static void reConnect(String[] db_connection) {
    try {
        Base.close();
    } catch (Exception e) {// nope
    }
    connect(db_connection);
}
 
Example #20
Source File: PubmedDatabaseAE.java    From bluima with Apache License 2.0 5 votes vote down vote up
@Override
public void destroy() {
    try {
        Base.close();
    } catch (Exception e) {
        LOG.warn("could not close conn", e);
    }
}
 
Example #21
Source File: PubmedDatabaseAE.java    From bluima with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(UimaContext context)
        throws ResourceInitializationException {
    super.initialize(context);
    Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://" + db_connection[0]
            + "/" + db_connection[1] + "", db_connection[2],
            db_connection[3]);
    processed = 0;
    inserted = 0;
}
 
Example #22
Source File: H2MigrationSpec.java    From javalite with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown(){
    Base.close();
}
 
Example #23
Source File: VersionStrategy.java    From javalite with Apache License 2.0 4 votes vote down vote up
public List<String> getAppliedMigrations() {
    return Base.firstColumn("select " + VERSION_COLUMN + " from " + VERSION_TABLE);
}
 
Example #24
Source File: MigrationSpec.java    From javalite with Apache License 2.0 4 votes vote down vote up
@Before
public void before() {
    connection = new MockConnection();
    Base.attach(connection);
}
 
Example #25
Source File: MigrationSpec.java    From javalite with Apache License 2.0 4 votes vote down vote up
@After
public void after(){
    Base.detach();
}
 
Example #26
Source File: H2MigrationSpec.java    From javalite with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws Exception {
    Base.open("org.h2.Driver", "jdbc:h2:mem:h2-migration-test;DB_CLOSE_DELAY=-1", "sa", "");
    migrationManager = new MigrationManager("src/test/resources/test_migrations/h2/");
}
 
Example #27
Source File: HSQLMigrationSpec.java    From javalite with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() throws Exception {
    Base.close();
}
 
Example #28
Source File: SimpleVersionStrategySpec.java    From javalite with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
    strategy = new VersionStrategy();
    Base.open("org.h2.Driver", "jdbc:h2:mem:h2-migration-test;DB_CLOSE_DELAY=-1", "sa", "");
    try {exec("drop table " + VersionStrategy.VERSION_TABLE);} catch (Exception e) {/* ignore*/}
}
 
Example #29
Source File: SimpleVersionStrategySpec.java    From javalite with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    Base.close();
}
 
Example #30
Source File: HSQLMigrationSpec.java    From javalite with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws Exception {
    Base.open("org.hsqldb.jdbcDriver", "jdbc:hsqldb:file:./target/tmp/hsql-migration-test", "sa", "");
    migrationManager = new MigrationManager("src/test/resources/test_migrations/hsql/");
}