Search in sources :

Example 1 with DatabaseConnection

use of com.j256.ormlite.support.DatabaseConnection in project ormlite-android by j256.

the class OrmLiteSqliteOpenHelper method onUpgrade.

/**
	 * Satisfies the {@link SQLiteOpenHelper#onUpgrade(SQLiteDatabase, int, int)} interface method.
	 */
@Override
public final void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    ConnectionSource cs = getConnectionSource();
    /*
		 * The method is called by Android database helper's get-database calls when Android detects that we need to
		 * create or update the database. So we have to use the database argument and save a connection to it on the
		 * AndroidConnectionSource, otherwise it will go recursive if the subclass calls getConnectionSource().
		 */
    DatabaseConnection conn = cs.getSpecialConnection(null);
    boolean clearSpecial = false;
    if (conn == null) {
        conn = new AndroidDatabaseConnection(db, true, cancelQueriesEnabled);
        try {
            cs.saveSpecialConnection(conn);
            clearSpecial = true;
        } catch (SQLException e) {
            throw new IllegalStateException("Could not save special connection", e);
        }
    }
    try {
        onUpgrade(db, cs, oldVersion, newVersion);
    } finally {
        if (clearSpecial) {
            cs.clearSpecialConnection(conn);
        }
    }
}
Also used : SQLException(java.sql.SQLException) AndroidConnectionSource(com.j256.ormlite.android.AndroidConnectionSource) ConnectionSource(com.j256.ormlite.support.ConnectionSource) AndroidDatabaseConnection(com.j256.ormlite.android.AndroidDatabaseConnection) AndroidDatabaseConnection(com.j256.ormlite.android.AndroidDatabaseConnection) DatabaseConnection(com.j256.ormlite.support.DatabaseConnection)

Example 2 with DatabaseConnection

use of com.j256.ormlite.support.DatabaseConnection in project ormlite-android by j256.

the class AndroidConnectionSource method getReadWriteConnection.

@Override
public DatabaseConnection getReadWriteConnection(String tableName) throws SQLException {
    DatabaseConnection conn = getSavedConnection();
    if (conn != null) {
        return conn;
    }
    if (connection == null) {
        SQLiteDatabase db;
        if (sqliteDatabase == null) {
            try {
                db = helper.getWritableDatabase();
            } catch (android.database.SQLException e) {
                throw SqlExceptionUtil.create("Getting a writable database from helper " + helper + " failed", e);
            }
        } else {
            db = sqliteDatabase;
        }
        connection = new AndroidDatabaseConnection(db, true, cancelQueriesEnabled);
        if (connectionProxyFactory != null) {
            connection = connectionProxyFactory.createProxy(connection);
        }
        logger.trace("created connection {} for db {}, helper {}", connection, db, helper);
    } else {
        logger.trace("{}: returning read-write connection {}, helper {}", this, connection, helper);
    }
    return connection;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DatabaseConnection(com.j256.ormlite.support.DatabaseConnection)

Example 3 with DatabaseConnection

use of com.j256.ormlite.support.DatabaseConnection in project ormlite-android by j256.

the class OrmLiteCursorLoader method loadInBackground.

@Override
public Cursor loadInBackground() {
    Cursor cursor;
    try {
        DatabaseConnection connection = dao.getConnectionSource().getReadOnlyConnection(dao.getTableName());
        AndroidCompiledStatement statement = (AndroidCompiledStatement) query.compile(connection, SELECT);
        cursor = statement.getCursor();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    // fill the cursor with results
    cursor.getCount();
    return cursor;
}
Also used : SQLException(java.sql.SQLException) DatabaseConnection(com.j256.ormlite.support.DatabaseConnection) AndroidCompiledStatement(com.j256.ormlite.android.AndroidCompiledStatement) Cursor(android.database.Cursor)

Example 4 with DatabaseConnection

use of com.j256.ormlite.support.DatabaseConnection in project ormlite-android by j256.

the class OrmLiteSqliteOpenHelper method onCreate.

/**
	 * Satisfies the {@link SQLiteOpenHelper#onCreate(SQLiteDatabase)} interface method.
	 */
@Override
public final void onCreate(SQLiteDatabase db) {
    ConnectionSource cs = getConnectionSource();
    /*
		 * The method is called by Android database helper's get-database calls when Android detects that we need to
		 * create or update the database. So we have to use the database argument and save a connection to it on the
		 * AndroidConnectionSource, otherwise it will go recursive if the subclass calls getConnectionSource().
		 */
    DatabaseConnection conn = cs.getSpecialConnection(null);
    boolean clearSpecial = false;
    if (conn == null) {
        conn = new AndroidDatabaseConnection(db, true, cancelQueriesEnabled);
        try {
            cs.saveSpecialConnection(conn);
            clearSpecial = true;
        } catch (SQLException e) {
            throw new IllegalStateException("Could not save special connection", e);
        }
    }
    try {
        onCreate(db, cs);
    } finally {
        if (clearSpecial) {
            cs.clearSpecialConnection(conn);
        }
    }
}
Also used : SQLException(java.sql.SQLException) AndroidConnectionSource(com.j256.ormlite.android.AndroidConnectionSource) ConnectionSource(com.j256.ormlite.support.ConnectionSource) AndroidDatabaseConnection(com.j256.ormlite.android.AndroidDatabaseConnection) AndroidDatabaseConnection(com.j256.ormlite.android.AndroidDatabaseConnection) DatabaseConnection(com.j256.ormlite.support.DatabaseConnection)

Example 5 with DatabaseConnection

use of com.j256.ormlite.support.DatabaseConnection in project ormlite-android by j256.

the class OrmLiteCursorLoader method loadInBackground.

@Override
public Cursor loadInBackground() {
    Cursor cursor;
    try {
        DatabaseConnection connection = dao.getConnectionSource().getReadOnlyConnection(dao.getTableName());
        AndroidCompiledStatement statement = (AndroidCompiledStatement) query.compile(connection, SELECT);
        cursor = statement.getCursor();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    // fill the cursor with results
    cursor.getCount();
    return cursor;
}
Also used : SQLException(java.sql.SQLException) DatabaseConnection(com.j256.ormlite.support.DatabaseConnection) AndroidCompiledStatement(com.j256.ormlite.android.AndroidCompiledStatement) Cursor(android.database.Cursor)

Aggregations

DatabaseConnection (com.j256.ormlite.support.DatabaseConnection)7 Cursor (android.database.Cursor)4 AndroidCompiledStatement (com.j256.ormlite.android.AndroidCompiledStatement)4 SQLException (java.sql.SQLException)4 AndroidConnectionSource (com.j256.ormlite.android.AndroidConnectionSource)2 AndroidDatabaseConnection (com.j256.ormlite.android.AndroidDatabaseConnection)2 ConnectionSource (com.j256.ormlite.support.ConnectionSource)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1