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






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

package novoda.lib.sqliteprovider.provider;
/*from   w  w w .  j  a va  2s .c o  m*/
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import novoda.lib.sqliteprovider.provider.action.InsertHelper;
import novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteOpenHelper;
import novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteQueryBuilder;
import novoda.lib.sqliteprovider.util.Log;
import novoda.lib.sqliteprovider.util.UriUtils;

public class SQLiteContentProviderImpl extends SQLiteContentProvider {

    protected static final String ID = "_id";
    private static final String GROUP_BY = "groupBy";
    private static final String HAVING = "having";
    private static final String LIMIT = "limit";
    private static final String EXPAND = "expand";
    private static final String DISTINCT = "distinct";

    private InsertHelper helper;
    private final ImplLogger logger;

    public SQLiteContentProviderImpl() {
        logger = new ImplLogger();
    }

    @Override
    public boolean onCreate() {
        super.onCreate();
        helper = new InsertHelper((ExtendedSQLiteOpenHelper) getDatabaseHelper());
        return true;
    }

    protected SQLiteDatabase getWritableDatabase() {
        return getDatabaseHelper().getWritableDatabase();
    }

    protected SQLiteDatabase getReadableDatabase() {
        return getDatabaseHelper().getReadableDatabase();
    }

    @Override
    protected SQLiteOpenHelper getDatabaseHelper(Context context) {
        try {
            return new ExtendedSQLiteOpenHelper(context, getCursorFactory());
        } catch (IOException e) {
            Log.Provider.e(e);
            throw new IllegalStateException(e.getMessage());
        }
    }

    @Override
    protected Uri insertInTransaction(Uri uri, ContentValues values) {
        long rowId = helper.insert(uri, values);
    Uri newUri = ContentUris.withAppendedId(uri, rowId);
    notifyUriChange(newUri);
    return newUri;
    }

    @Override
    protected int updateInTransaction(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        ContentValues insertValues = (values != null) ? new ContentValues(values) : new ContentValues();

        int rowId = getWritableDatabase().update(UriUtils.getItemDirID(uri), insertValues, selection, selectionArgs);

        if (rowId > 0) {
            Uri insertUri = ContentUris.withAppendedId(uri, rowId);
            notifyUriChange(insertUri);
            return rowId;
        }
        throw new SQLException("Failed to update row into " + uri + " because it does not exists.");
    }

    @Override
    protected int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase database = getWritableDatabase();
        int count = database.delete(UriUtils.getItemDirID(uri), selection, selectionArgs);
        notifyUriChange(uri);
        return count;
    }

    @Override
    protected void notifyChange() {

    }

    public void notifyUriChange(Uri uri) {
        getContext().getContentResolver().notifyChange(uri, null, getNotificationSyncToNetwork());
    }

    public boolean getNotificationSyncToNetwork() {
        return false;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        logger.logStart(uri);

        final ExtendedSQLiteQueryBuilder builder = getSQLiteQueryBuilder();

        final List<String> expands = uri.getQueryParameters(EXPAND);
        final String groupBy = uri.getQueryParameter(GROUP_BY);
        final String having = uri.getQueryParameter(HAVING);
        final String limit = uri.getQueryParameter(LIMIT);

        builder.setDistinct("true".equals(uri.getQueryParameter(DISTINCT)));

        final StringBuilder tableName = new StringBuilder(UriUtils.getItemDirID(uri));
        builder.setTables(tableName.toString());
        Map<String, String> autoproj = null;

        if (expands.size() > 0) {
            builder.addInnerJoin(expands.toArray(new String[] {}));
            ExtendedSQLiteOpenHelper extendedHelper = (ExtendedSQLiteOpenHelper) getDatabaseHelper();
            autoproj = extendedHelper.getProjectionMap(tableName.toString(), expands.toArray(new String[] {}));
            builder.setProjectionMap(autoproj);
        }

        if (UriUtils.isItem(uri)) {
            String where = ID + "=" + uri.getLastPathSegment();
            logger.logAppendWhere(where);
            builder.appendWhere(where);
        } else if (UriUtils.hasParent(uri)) {
            StringBuilder escapedWhere = new StringBuilder();
            DatabaseUtils.appendEscapedSQLString(escapedWhere, UriUtils.getParentId(uri));
            String where = UriUtils.getParentColumnName(uri) + ID + "=" + escapedWhere.toString();
            logger.logAppendWhere(where);
            builder.appendWhere(where);
        }

        logger.logEnd(projection, selection, selectionArgs, sortOrder, builder, groupBy, having, limit, autoproj);

        Cursor cursor = builder.query(getReadableDatabase(), projection, selection, selectionArgs, groupBy, having, sortOrder, limit);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    protected ExtendedSQLiteQueryBuilder getSQLiteQueryBuilder() {
        return new ExtendedSQLiteQueryBuilder();
    }

    @Override
    protected SQLiteDatabase.CursorFactory getCursorFactory() {
        return null;
    }
}




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