Java Code Examples for org.javalite.activejdbc.Base

The following examples show how to use org.javalite.activejdbc.Base. These examples are extracted from open source projects. 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 Project: javalite   Source File: OracleDBReset.java    License: 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 2
Source Project: javalite   Source File: CheckMojo.java    License: 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 3
Source Project: javalite   Source File: ValidateMojo.java    License: 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 Project: bluima   Source File: PubmedDatabaseAE.java    License: 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 5
Source Project: bluima   Source File: PubmedDatabaseAE.java    License: 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 6
Source Project: bluima   Source File: PubmedArticleEntity.java    License: 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 7
Source Project: tutorials   Source File: ProductUnitTest.java    License: 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 8
Source Project: tutorials   Source File: ActiveJDBCApp.java    License: 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 9
Source Project: javalite   Source File: OracleDBReset.java    License: 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 10
Source Project: javalite   Source File: OracleDBReset.java    License: 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 11
Source Project: javalite   Source File: OracleDBReset.java    License: 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 12
Source Project: javalite   Source File: Db2DBReset.java    License: 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 Project: javalite   Source File: DBSpecTest.java    License: 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 14
Source Project: javalite   Source File: DefaultDBReset.java    License: 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 15
Source Project: javalite   Source File: DefaultDBReset.java    License: 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 16
Source Project: javalite   Source File: MigrateMojo.java    License: 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 17
Source Project: javalite   Source File: DropMojo.java    License: 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 18
Source Project: javalite   Source File: DbUtils.java    License: 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 Project: javalite   Source File: VersionStrategy.java    License: 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 20
Source Project: javalite   Source File: MySQLMigrationSpec.java    License: 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 21
Source Project: javalite   Source File: MySQLMigrationSpec.java    License: 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 22
Source Project: flowchat   Source File: Tools.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static final void dbInit() {
    Base.open(hikariDataSource); // get connection from pool
}
 
Example 23
Source Project: flowchat   Source File: Tools.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static final void dbClose() {
    Base.close();
}
 
Example 24
Source Project: bluima   Source File: PubmedArticleEntity.java    License: Apache License 2.0 4 votes vote down vote up
public static void connect(String[] db_connection) {
    Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://" + db_connection[0]
            + "/" + db_connection[1] + "", db_connection[2],
            db_connection[3]);
}
 
Example 25
Source Project: javalite   Source File: Main.java    License: Apache License 2.0 4 votes vote down vote up
public static void main(String[] args){
    DBConfiguration.loadConfiguration("/database.properties");
    Base.open();
}
 
Example 26
Source Project: javalite   Source File: DBSpecTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void a_clean(){
    setRollback(false);
    Base.exec("delete from patients");
}
 
Example 27
Source Project: javalite   Source File: DBSpecTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void b_shouldHaveConnectionToTestDB(){
    a(Base.connection()).shouldNotBeNull();
}
 
Example 28
Source Project: javalite   Source File: DBSpecTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void c_shouldRollBackTransactionByDefault(){
    Base.exec("insert into patients (first_name, last_name) values ('Billie', 'Holiday')");
}
 
Example 29
Source Project: javalite   Source File: DBSpecTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void d_shouldNotFindRecordsFromRolledBackTransaction(){
    a(Base.count("patients")).shouldBeEqual(0);
}
 
Example 30
Source Project: javalite   Source File: DBSpecTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void e_should_NOT_RollBackTransaction(){
    setRollback(false); //  here, we are leaving data in DB for the next test to find (no rollback at the end of test)
    Base.exec("insert into patients (first_name, last_name) values ('Billie', 'Holiday')");
}