Example usage for android.database.sqlite SQLiteDatabase beginTransaction

List of usage examples for android.database.sqlite SQLiteDatabase beginTransaction

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase beginTransaction.

Prototype

public void beginTransaction() 

Source Link

Document

Begins a transaction in EXCLUSIVE mode.

Usage

From source file:org.ttrssreader.controllers.DBHelper.java

void insertFeeds(Set<Feed> set) {
    if (!isDBAvailable() || set == null)
        return;/*from  w w  w.  jav a2 s .c  om*/

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        for (Feed f : set) {
            insertFeed(f.id, f.categoryId, f.title, f.url, f.unread);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

void insertArticles(Collection<Article> articles) {
    if (!isDBAvailable() || articles == null || articles.isEmpty())
        return;/*from  w w w. j  a v a2s  .  c  om*/

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        for (Article a : articles) {
            insertArticleIntern(a);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * insert given remote files into DB and link them with given article
 *
 * @param articleId "parent" article/*from  w  w  w.j a v a 2 s.co m*/
 * @param fileUrls  array of remote file URLs
 */
public void insertArticleFiles(int articleId, String[] fileUrls) {
    if (!isDBAvailable())
        return;

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        for (String url : fileUrls) {
            long remotefileId = insertRemoteFile(url);
            if (remotefileId != 0)
                insertRemoteFile2Article(remotefileId, articleId);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * mark remote files with given IDs as non cached (cached=0)
 *
 * @param rfIds IDs of remote files to be marked as non-cached
 *///  w  ww  .j av  a2 s .co  m
public void markRemoteFilesNonCached(Collection<Integer> rfIds) {
    if (!isDBAvailable())
        return;

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        ContentValues cv = new ContentValues(1);
        cv.put("cached", 0);
        for (String ids : StringSupport.convertListToString(rfIds, 1000)) {
            db.update(TABLE_REMOTEFILES, cv, "id in (" + ids + ")", null);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

void markUnsynchronizedStates(Collection<Integer> ids, String mark, int state) {
    if (!isDBAvailable())
        return;//from   w w w . j ava2  s  .  c  o  m

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        for (Integer id : ids) {
            // First update, then insert. If row exists it gets updated and second call ignores it, else the second
            // call inserts it.
            db.execSQL(String.format("UPDATE %s SET %s=%s WHERE id=%s", TABLE_MARK, mark, state, id));
            db.execSQL(String.format("INSERT OR IGNORE INTO %s (id, %s) VALUES (%s, %s)", TABLE_MARK, mark, id,
                    state));
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * mark given remote file as cached/uncached and optionally specify it's file size
 *
 * @param url    remote file URL/*from w w  w  .  j  a v a2 s . c  o m*/
 * @param cached the cached flag
 * @param size   file size may be {@code null}, if so, then it will not be updated in DB
 */
public void markRemoteFileCached(String url, boolean cached, Long size) {
    if (!isDBAvailable())
        return;

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        ContentValues cv = new ContentValues(2);
        cv.put("cached", cached);
        if (size != null) {
            cv.put("length", size);
        }
        db.update(TABLE_REMOTEFILES, cv, "url=?", new String[] { url });
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

void markUnsynchronizedNotes(Map<Integer, String> ids) {
    if (!isDBAvailable())
        return;//from w w w  . j a  va 2 s  .c  o  m

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        for (Integer id : ids.keySet()) {
            String note = ids.get(id);
            if (note == null || note.equals(""))
                continue;

            ContentValues cv = new ContentValues(1);
            cv.put(MARK_NOTE, note);
            db.update(TABLE_MARK, cv, "id=" + id, null);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * mark given property of given articles with given state
 *
 * @param idList set of article IDs, which should be processed
 * @param mark   mark to be set/*from  w  w  w  . j  a  va  2 s. c om*/
 * @param state  value for the mark
 */
public void markArticles(Set<Integer> idList, String mark, int state) {
    if (!isDBAvailable())
        return;

    if (idList != null && !idList.isEmpty()) {
        SQLiteDatabase db = getOpenHelper().getWritableDatabase();
        writeLock(true);
        db.beginTransaction();
        try {
            for (String ids : StringSupport.convertListToString(idList, 400)) {
                markArticles(ids, mark, state);
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
            writeLock(false);
        }
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * remove specified mark in the temporary mark table for specified
 * articles and then cleanup this table/*from  ww  w  . j a  v a2  s .c o  m*/
 *
 * @param ids  article IDs, which mark should be reseted
 * @param mark article mark to be reseted
 */
void setMarked(Map<Integer, String> ids, String mark) {
    if (!isDBAvailable())
        return;

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        ContentValues cv = new ContentValues(1);
        for (String idList : StringSupport.convertListToString(ids.keySet(), 1000)) {
            cv.putNull(mark);
            db.update(TABLE_MARK, cv, "id IN(" + idList + ")", null);
            db.delete(TABLE_MARK, "isUnread IS null AND isStarred IS null AND isPublished IS null", null);
        }

        // Insert notes afterwards and only if given note is not null
        cv = new ContentValues(1);
        for (Integer id : ids.keySet()) {
            String note = ids.get(id);
            if (note == null || note.equals(""))
                continue;

            cv.put(MARK_NOTE, note);
            db.update(TABLE_MARK, cv, "id=" + id, null);
        }

        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

/**
 * delete articles and all its resources (e.g. remote files, labels etc.)
 *
 * @param whereClause the optional WHERE clause to apply when deleting.
 *                    Passing null will delete all rows.
 * @param whereArgs   You may include ?s in the where clause, which
 *                    will be replaced by the values from whereArgs. The values
 *                    will be bound as Strings.
 * @return the number of rows affected if a whereClause is passed in, 0
 * otherwise. To remove all rows and get a count pass "1" as the
 * whereClause./*  w  w  w.  j  a va2 s  .  c o m*/
 */
private int safelyDeleteArticles(String whereClause, String[] whereArgs) {
    int deletedCount = 0;

    Collection<RemoteFile> rfs = getRemoteFilesForArticles(whereClause, whereArgs, true);
    if (!rfs.isEmpty()) {
        Set<Integer> rfIds = new HashSet<>(rfs.size());
        for (RemoteFile rf : rfs) {
            rfIds.add(rf.id);
            Controller.getInstance().getImageCache().getCacheFile(rf.url).delete();
        }
        deleteRemoteFiles(rfIds);
    }

    // @formatter:off
    StringBuilder query = new StringBuilder();
    query.append(" articleId IN (").append("     SELECT _id").append("       FROM ").append(TABLE_ARTICLES)
            .append("       WHERE ").append(whereClause).append(" )");
    // @formatter:on

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        // first, delete article referencies from linking table to preserve foreign key constraint on the next step
        db.delete(TABLE_REMOTEFILE2ARTICLE, query.toString(), whereArgs);

        // TODO Foreign-key constraint failed from purgeOrphanedArticles() and safelyDeleteArticles()
        deletedCount = db.delete(TABLE_ARTICLES, whereClause, whereArgs);
        purgeLabels();
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }

    return deletedCount;
}