Java Code Examples for org.javalite.activejdbc.DB

The following examples show how to use org.javalite.activejdbc.DB. 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
public void init()
{
    this.sql = Sponge.getServiceManager().provideUnchecked(SqlService.class);
    try // TODO: the logger is annoying, so disable it anyway (although it is a dirty way)
    {
        Field field = DB.class.getDeclaredField("LOGGER");
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        int m = field.getModifiers() & ~Modifier.FINAL;
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, m);

        NOPLogger l = NOPLogger.NOP_LOGGER;
        field.set(null, l);
    }
    catch (ReflectiveOperationException e)
    {
        throw new IllegalStateException(e);
    }
}
 
Example 2
Source Project: javalite   Source File: DBConnectionFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void before() {

    if(Configuration.isTesting())
        return;



    if (connectionConfigs.isEmpty()) {
        throw new InitException("There are no connection specs in '" + AppConfig.activeEnv() + "' environment");
    }

    for (ConnectionConfig connectionConfig : connectionConfigs) {
        DB db = new DB(connectionConfig.getDbName());
        db.open(connectionConfig);
        if(manageTransaction){
            db.openTransaction();
        }
    }
}
 
Example 3
Source Project: javalite   Source File: DBConnectionFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void after() {
    if(Configuration.isTesting())
        return;

    if (connectionConfigs != null && !connectionConfigs.isEmpty()) {
        for (ConnectionConfig connectionConfig : connectionConfigs) {
            DB db = new DB(connectionConfig.getDbName());
            if(db.hasConnection()){
                if(manageTransaction){
                    if (RequestContext.exceptionHappened()) {
                        logDebug("Skip commit transaction because already rolled back.");
                    } else {
                        db.commitTransaction();
                    }
                }
                db.close();
            }
        }
    }
}
 
Example 4
Source Project: javalite   Source File: DBConnectionFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onException(Exception e) {        
    if(Configuration.isTesting())
        return;

    if (connectionConfigs != null && !connectionConfigs.isEmpty()) {
        for (ConnectionConfig connectionConfig : connectionConfigs) {
            DB db = new DB(connectionConfig.getDbName());
            if (db.hasConnection()) {
                if (manageTransaction) {
                    db.rollbackTransaction();
                    logDebug("Rolling back transaction due to exception: " + e);
                }
                db.close();
            }
        }
    }
}
 
Example 5
Source Project: javalite   Source File: DBSpec.java    License: Apache License 2.0 6 votes vote down vote up
@Before @BeforeEach
public final void openTestConnections() {

    if(!suppressDb){
        loadConfiguration("/database.properties");
        testConnectionConfigs = getTestConnectionConfigs();
        if (testConnectionConfigs.isEmpty()) {
            LOGGER.warn("no DB connections are configured, none opened");
        }else {
            testConnectionConfigs.forEach(connectionConfig -> {
                DB db = new DB(connectionConfig.getDbName());
                db.open(connectionConfig);
                if (rollback) {
                    db.openTransaction();
                }
            });
        }
    }
}
 
Example 6
public void recordOpen(UUID uuid, String menuName, Player p)
{
    if (!this.databaseUrl.isEmpty())
    {
        try (DB db = new DB())
        {
            db.open(this.dataSource);
            new VirtualChestOpenRecord(uuid, menuName, p).insert();
        }
    }
}
 
Example 7
public void recordClose(UUID uuid, String menuName, Player p)
{
    if (!this.databaseUrl.isEmpty())
    {
        try (DB db = new DB())
        {
            db.open(this.dataSource);
            new VirtualChestCloseRecord(uuid, menuName, p).insert();
        }
    }
}
 
Example 8
public void recordSlotClick(UUID uuid, String menuName, int menuSlot, VirtualChestInventory.ClickStatus s, Player p)
{
    if (!this.databaseUrl.isEmpty())
    {
        try (DB db = new DB())
        {
            db.open(this.dataSource);
            new VirtualChestClickSlotRecord(uuid, menuName, menuSlot, s, p).insert();
        }
    }
}
 
Example 9
public void recordExecution(UUID uuid, int order, String executionPrefix, String executionSuffix)
{
    if (!this.databaseUrl.isEmpty())
    {
        try (DB db = new DB())
        {
            db.open(this.dataSource);
            new VirtualChestActionExecutionRecord(uuid, order, executionPrefix, executionSuffix).insert();
        }
    }
}
 
Example 10
Source Project: torrenttunes-client   Source File: Tools.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static final void dbInit() {
	try {
		new DB("ttc").open("org.sqlite.JDBC", "jdbc:sqlite:" + DataSources.DB_FILE(), "root", "[email protected]");
	} catch (DBException e) {
		e.printStackTrace();
		dbClose();
		dbInit();
	}

}
 
Example 11
Source Project: javalite   Source File: DBSpecHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static void openTestConnections(){
    List<ConnectionConfig> connectionConfigs = getTestConnectionConfigs();
    if(connectionConfigs.isEmpty()){
        LOGGER.warn("no DB connections are configured, none opened");
        return;
    }

    for (ConnectionConfig connectionConfig : connectionConfigs) {
        DB db = new DB(connectionConfig.getDbName());
        db.open(connectionConfig);
        if (Configuration.rollback())
            db.openTransaction();            
    }
}
 
Example 12
Source Project: javalite   Source File: DBSpecHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static void closeTestConnections() {
    List<ConnectionConfig> connectionConfigs = getTestConnectionConfigs();
    for (ConnectionConfig connectionConfig : connectionConfigs) {
        String dbName = connectionConfig.getDbName();
        DB db = new DB(dbName);
        if (Configuration.rollback()) {
            db.rollbackTransaction();
        }
        db.close();

    }
}
 
Example 13
Source Project: javalite   Source File: UniquenessValidator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void validate(Model model) {
    MetaModel metaModel = metaModelOf(model.getClass());
    if (new DB(metaModel.getDbName()).count(metaModel.getTableName(), attribute + " = ?", model.get(attribute)) > 0) {
        model.addValidator(this, attribute);
    }
}
 
Example 14
Source Project: javalite   Source File: DBSpec.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Set to true in order  to rollback a transaction at the end of the test.
 * <p>
 *     <em>
 *     WARNING: if you set this value to false inside your test, the framework will not
 *     clean any remaining data you insert into your test database. Basically, this is a
 *     "manual mode" where you are responsible for cleaning after yourself.
 *     </em>
 * </p>
 *
 * @param rollback true to rollback transactions at the end of the test, false to not rollback.
 */
public void setRollback(boolean rollback) {
    if(!suppressDb){
        this.rollback = rollback;
        Map<String, Connection> connections = DB.connections();
        for(String name: connections.keySet()){
            try {
                boolean autocommit = !rollback;
                connections.get(name).setAutoCommit(autocommit);
            } catch (SQLException e) {
                throw new InitException(e);
            }
        }
    }
}
 
Example 15
Source Project: javalite   Source File: DBSpec.java    License: Apache License 2.0 5 votes vote down vote up
@After @AfterEach
public final void closeTestConnections() {
    if(!suppressDb){

        for (ConnectionConfig connectionConfig : testConnectionConfigs) {
            String dbName = connectionConfig.getDbName();
            DB db = new DB(dbName);
            if (rollback) {
                db.rollbackTransaction();
            }
            db.close();
        }
        clearConnectionConfigs();
    }
}
 
Example 16
Source Project: torrenttunes-client   Source File: Tools.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static final void dbClose() {
	new DB("ttc").close();
}
 
Example 17
Source Project: javalite   Source File: DBConnectionFilter.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * This constructor is used to open all configured connections for a current environment.
 */
public DBConnectionFilter() {
    this.connectionConfigs = DBConfiguration.getConnectionConfigsExceptTesting(DB.DEFAULT_NAME);
}