Android Open Source - sqlite-provider S Q Lite Content Provider






From Project

Back to project page sqlite-provider.

License

The source code is released under:

Apache License

If you think the Android project sqlite-provider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2009 The Android Open Source Project
 *//from  w  ww. j av a 2 s. c o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package novoda.lib.sqliteprovider.provider;

import android.content.*;
import android.database.sqlite.*;
import android.net.Uri;

import java.util.ArrayList;

/**
 * General purpose {@link ContentProvider} base class that uses SQLiteDatabase
 * for storage.
 */
public abstract class SQLiteContentProvider extends ContentProvider implements SQLiteTransactionListener {

    private static final int SLEEP_AFTER_YIELD_DELAY = 4000;

    private final ThreadLocal<Boolean> mApplyingBatch = new ThreadLocal<Boolean>();
    private SQLiteOpenHelper mOpenHelper;

    private volatile boolean mNotifyChange;

    @Override
    public boolean onCreate() {
        mOpenHelper = getDatabaseHelper(getContext());
        return true;
    }

    protected abstract SQLiteOpenHelper getDatabaseHelper(Context context);

    protected abstract SQLiteDatabase.CursorFactory getCursorFactory();

    /**
     * The equivalent of the {@link #insert} method, but invoked within a
     * transaction.
     */
    protected abstract Uri insertInTransaction(Uri uri, ContentValues values);

    /**
     * The equivalent of the {@link #update} method, but invoked within a
     * transaction.
     */
    protected abstract int updateInTransaction(Uri uri, ContentValues values, String selection,
            String[] selectionArgs);

    /**
     * The equivalent of the {@link #delete} method, but invoked within a
     * transaction.
     */
    protected abstract int deleteInTransaction(Uri uri, String selection, String[] selectionArgs);

    protected abstract void notifyChange();

    protected SQLiteOpenHelper getDatabaseHelper() {
        return mOpenHelper;
    }

    private boolean applyingBatch() {
        return mApplyingBatch.get() != null && mApplyingBatch.get();
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Uri result = null;
        boolean applyingBatch = applyingBatch();
        if (!applyingBatch) {
            SQLiteDatabase mDb = mOpenHelper.getWritableDatabase();
            mDb.beginTransactionWithListener(this);
            try {
                result = insertInTransaction(uri, values);
                if (result != null) {
                    mNotifyChange = true;
                }
                mDb.setTransactionSuccessful();
            } finally {
                mDb.endTransaction();
            }

            onEndTransaction();
        } else {
            result = insertInTransaction(uri, values);
            if (result != null) {
                mNotifyChange = true;
            }
        }
        return result;
    }

    @Override
    public int bulkInsert(Uri uri, ContentValues[] values) {
        int numValues = values.length;
        SQLiteDatabase mDb = mOpenHelper.getWritableDatabase();
        mDb.beginTransactionWithListener(this);
        try {
            for (int i = 0; i < numValues; i++) {
                Uri result = insertInTransaction(uri, values[i]);
                if (result != null) {
                    mNotifyChange = true;
                }
                mDb.yieldIfContendedSafely();
            }
            mDb.setTransactionSuccessful();
        } finally {
            mDb.endTransaction();
        }

        onEndTransaction();
        return numValues;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int count = 0;
        boolean applyingBatch = applyingBatch();
        if (!applyingBatch) {
            SQLiteDatabase mDb = mOpenHelper.getWritableDatabase();
            mDb.beginTransactionWithListener(this);
            try {
                count = updateInTransaction(uri, values, selection, selectionArgs);
                if (count > 0) {
                    mNotifyChange = true;
                }
                mDb.setTransactionSuccessful();
            } finally {
                mDb.endTransaction();
            }

            onEndTransaction();
        } else {
            count = updateInTransaction(uri, values, selection, selectionArgs);
            if (count > 0) {
                mNotifyChange = true;
            }
        }

        return count;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int count = 0;
        boolean applyingBatch = applyingBatch();
        if (!applyingBatch) {
            SQLiteDatabase mDb = mOpenHelper.getWritableDatabase();
            mDb.beginTransactionWithListener(this);
            try {
                count = deleteInTransaction(uri, selection, selectionArgs);
                if (count > 0) {
                    mNotifyChange = true;
                }
                mDb.setTransactionSuccessful();
            } finally {
                mDb.endTransaction();
            }

            onEndTransaction();
        } else {
            count = deleteInTransaction(uri, selection, selectionArgs);
            if (count > 0) {
                mNotifyChange = true;
            }
        }
        return count;
    }

    @Override
    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
            throws OperationApplicationException {
        SQLiteDatabase mDb = mOpenHelper.getWritableDatabase();
        mDb.beginTransactionWithListener(this);
        try {
            mApplyingBatch.set(true);
            final int numOperations = operations.size();
            final ContentProviderResult[] results = new ContentProviderResult[numOperations];
            for (int i = 0; i < numOperations; i++) {
                final ContentProviderOperation operation = operations.get(i);
                if (i > 0 && operation.isYieldAllowed()) {
                    mDb.yieldIfContendedSafely(SLEEP_AFTER_YIELD_DELAY);
                }
                results[i] = operation.apply(this, results, i);
            }
            mDb.setTransactionSuccessful();
            return results;
        } finally {
            mApplyingBatch.set(false);
            mDb.endTransaction();
            onEndTransaction();
        }
    }

    @Override
    public void onBegin() {
        onBeginTransaction();
    }

    @Override
    public void onCommit() {
        beforeTransactionCommit();
    }

    @Override
    public void onRollback() {
        // not used
    }

    protected void onBeginTransaction() {
    }

    protected void beforeTransactionCommit() {
    }

    protected void onEndTransaction() {
        if (mNotifyChange) {
            mNotifyChange = false;
            notifyChange();
        }
    }
}




Java Source Code List

com.novoda.sqliteprovider.demo.NovodaApplication.java
com.novoda.sqliteprovider.demo.domain.Firework.java
com.novoda.sqliteprovider.demo.domain.Groups.java
com.novoda.sqliteprovider.demo.domain.Shop.java
com.novoda.sqliteprovider.demo.domain.UseCaseFactory.java
com.novoda.sqliteprovider.demo.domain.UseCaseInfo.java
com.novoda.sqliteprovider.demo.loader.FireworkLoader.java
com.novoda.sqliteprovider.demo.loader.FireworkSaver.java
com.novoda.sqliteprovider.demo.persistance.DatabaseConstants.java
com.novoda.sqliteprovider.demo.persistance.DatabaseReader.java
com.novoda.sqliteprovider.demo.persistance.DatabaseWriter.java
com.novoda.sqliteprovider.demo.persistance.FireworkReader.java
com.novoda.sqliteprovider.demo.persistance.FireworkWriter.java
com.novoda.sqliteprovider.demo.provider.FireworkProvider.java
com.novoda.sqliteprovider.demo.provider.FireworkUriConstants.java
com.novoda.sqliteprovider.demo.simple.provider.FireworkProvider.java
com.novoda.sqliteprovider.demo.simple.ui.MainActivity.java
com.novoda.sqliteprovider.demo.simple.ui.MainFragment.java
com.novoda.sqliteprovider.demo.ui.AddFireworkActivity.java
com.novoda.sqliteprovider.demo.ui.FindDistinctFireworksActivity.java
com.novoda.sqliteprovider.demo.ui.FindFireworkWithPkActivity.java
com.novoda.sqliteprovider.demo.ui.FindFireworksFromOneShopActivity.java
com.novoda.sqliteprovider.demo.ui.FindThreeFireworksActivity.java
com.novoda.sqliteprovider.demo.ui.FireworkActivity.java
com.novoda.sqliteprovider.demo.ui.MainActivity.java
com.novoda.sqliteprovider.demo.ui.ViewAllFireworksActivity.java
com.novoda.sqliteprovider.demo.ui.ViewShopActivity.java
com.novoda.sqliteprovider.demo.ui.WellStockedShopActivity.java
com.novoda.sqliteprovider.demo.ui.adapter.FireworkAdapter.java
com.novoda.sqliteprovider.demo.ui.base.NovodaActivity.java
com.novoda.sqliteprovider.demo.ui.fragment.AddFireworkFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.FindFireworkWithPkFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.FindFireworksFromOneShopFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.ShopFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.UriSqlFragment.java
com.novoda.sqliteprovider.demo.ui.input.DemoMenu.java
com.novoda.sqliteprovider.demo.ui.input.OnPrimaryKeyInputError.java
com.novoda.sqliteprovider.demo.ui.util.FromXML.java
com.novoda.sqliteprovider.demo.ui.widget.UriSqlView.java
com.novoda.sqliteprovider.demo.util.Log.java
novoda.lib.sqliteprovider.ContentProviderTest.java
novoda.lib.sqliteprovider.cursor.EmptyCursor.java
novoda.lib.sqliteprovider.migration.Migrations.java
novoda.lib.sqliteprovider.provider.ImplLogger.java
novoda.lib.sqliteprovider.provider.LoggedContentProvider.java
novoda.lib.sqliteprovider.provider.SQLiteContentProviderImpl.java
novoda.lib.sqliteprovider.provider.SQLiteContentProvider.java
novoda.lib.sqliteprovider.provider.action.InsertHelper.java
novoda.lib.sqliteprovider.sqlite.AssetHelperTest.java
novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteOpenHelperTest.java
novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteOpenHelper.java
novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteQueryBuilder.java
novoda.lib.sqliteprovider.sqlite.IDatabaseMetaInfo.java
novoda.lib.sqliteprovider.sqlite.InsertHelperTest.java
novoda.lib.sqliteprovider.sqlite.SQLiteUri.java
novoda.lib.sqliteprovider.testproviders.TestProvider.java
novoda.lib.sqliteprovider.util.DBUtilsTest.java
novoda.lib.sqliteprovider.util.DBUtils.java
novoda.lib.sqliteprovider.util.DatabaseUtils.java
novoda.lib.sqliteprovider.util.Log.java
novoda.lib.sqliteprovider.util.SQLFile.java
novoda.lib.sqliteprovider.util.UriInspector.java
novoda.lib.sqliteprovider.util.UriToSqlAttributes.java
novoda.lib.sqliteprovider.util.UriUtils.java
novoda.rest.database.ModularSQLiteOpenHelper.java
novoda.rest.database.Persister.java
novoda.rest.database.SQLiteConflictClause.java
novoda.rest.database.SQLiteFileParser.java
novoda.rest.database.SQLiteInserter.java
novoda.rest.database.SQLiteTableCreatorWrapper.java
novoda.rest.database.SQLiteTableCreator.java
novoda.rest.database.SQLiteType.java
novoda.rest.database.SQLiteUtil.java
novoda.rest.database.UriQueryBuilder.java
novoda.rest.database.UriTableCreator.java
novoda.rest.database.listener.NodeInsertListener.java
novoda.rest.database.listener.SQLiteInsertListener.java
novoda.rest.database.listener.SQLiteTableCreateListener.java