Android Open Source - sqlite-provider Uri Table Creator






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.rest.database;
/*w ww  . jav  a2  s.c  om*/
import android.net.Uri;

import java.util.*;

public class UriTableCreator implements SQLiteTableCreator {

    private static final String COL_PRIMARY_KEY = "_id";

    private Uri uri;

    private List<String> pathSegments;

    protected UriTableCreator() {
    }

    protected UriTableCreator(final Uri uri) {
        this.setUri(uri);
    }

    @Override
    public String getParentColumnName() {
        if (isOneToMany()) {
            return new StringBuffer(pathSegments.get(pathSegments.size() - 3)).append(COL_PRIMARY_KEY).toString();
        }
        return null;
    }

    @Override
    public String getParentTableName() {
        if (isOneToMany()) {
            return pathSegments.get(pathSegments.size() - 3);
        }
        return null;
    }

    @Override
    public SQLiteType getParentType() {
        return SQLiteType.INTEGER;
    }

    @Override
    public String getPrimaryKey() {
        return COL_PRIMARY_KEY;
    }

    @Override
    public String getTableName() {
        return pathSegments.get(pathSegments.size() - 1);
    }

    /**
     * By default, we will delete all relationship on INSERT, UPDATE, DELETE
     * as we would expect the parent node to contain the one to many relationship and
     * reinsert the children into the database. This is a safe approach rather then updating or appending values.
     */
    @Override
    public String[] getTriggers() {
        if (isOneToMany()) {
            return SQLiteUtil.getTriggers(getParentTableName(), getParentPrimaryKey(), getTableName(), getParentColumnName());
        }
        return null;
    }

    @Override
    public String getParentPrimaryKey() {
        return COL_PRIMARY_KEY;
    }

    @Override
    public SQLiteType getType(String field) {
        if (field.equals(COL_PRIMARY_KEY)) {
            return SQLiteType.INTEGER;
        }
        return SQLiteType.TEXT;
    }

    @Override
    public boolean isNullAllowed(String field) {
        return true;
    }

    @Override
    public boolean isOneToMany() {
        return (pathSegments.size() > 2);
    }

    @Override
    public boolean isUnique(String field) {
        if (field.equals(getPrimaryKey())) {
            return true;
        }
        return false;
    }

    @Override
    public SQLiteConflictClause onConflict(String field) {
        return SQLiteConflictClause.REPLACE;
    }

    @Override
    public boolean shouldIndex(String field) {
        if (field.equals(getPrimaryKey())) {
            return true;
        }
        return false;
    }

    @Override
    public boolean shouldPKAutoIncrement() {
        if (getPrimaryKey() == null || getPrimaryKey().equals(COL_PRIMARY_KEY)) {
            return true;
        }
        return false;
    }

    public final void setUri(Uri uri) {
        // just to overcome an issue with # sign and getPath would only result
        // in the path after #
        this.uri = Uri.parse(uri.toString().replace('#', '1'));
        pathSegments = new ArrayList<String>(Arrays.asList(this.uri.getPath().split("/")));
        pathSegments.remove(0);
    }

    public Uri getUri() {
        return uri;
    }

    public static SQLiteTableCreator fromUri(final Uri uri) {
        UriTableCreator utc = new UriTableCreator(uri) {
        };
        return utc;
    }

    @Override
    public String[] getTableFields() {
        return new String[] {getPrimaryKey()};
    }
}




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