Android Open Source - sqlite-provider Modular S Q Lite Open Helper






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  w  w. ja v a2 s  .co  m*/
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import novoda.lib.sqliteprovider.util.DatabaseUtils;

public class ModularSQLiteOpenHelper extends SQLiteOpenHelper {

    private static final String TAG = ModularSQLiteOpenHelper.class.getSimpleName();
    private static final String SELECT_TABLES_NAME = "SELECT name FROM sqlite_master WHERE type='table';";
    private static final int ALWAYS_UPGRADE = 99;

    private static int dbVersion = 0;

    private final List<String> createdTable;
    private final Map<String, SQLiteTableCreator> createStatements;

    public ModularSQLiteOpenHelper(Context context) {
        this(context, null);
    }

    public ModularSQLiteOpenHelper(Context context, SQLiteDatabase.CursorFactory factory) {
        super(context, new StringBuilder(context.getApplicationInfo().packageName).append(".db").toString(), factory, dbVersion);
        createdTable = new ArrayList<String>();
        createStatements = new HashMap<String, SQLiteTableCreator>();
        init();
    }

    private void init() {
        final Cursor cur = getReadableDatabase().rawQuery(SELECT_TABLES_NAME, null);
        while (cur.moveToNext()) {
            createdTable.add(cur.getString(0));
        }
        cur.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.v(TAG, "upgrading database from version " + oldVersion + " to " + newVersion);
        for (Entry<String, SQLiteTableCreator> entry : createStatements.entrySet()) {
            if (createdTable.contains(entry.getKey())) {
                Log.v(TAG, "Table " + entry.getKey() + " already in DB.");
            } else {
                Log.v(TAG, "Creating table: " + entry.getKey());
                SQLiteTableCreator creator = entry.getValue();
                db.execSQL(DatabaseUtils.getCreateStatement(creator));
                if (creator.isOneToMany()) {
                    for (String trigger : creator.getTriggers()) {
                        db.execSQL(trigger);
                    }
                }
                createdTable.add(entry.getKey());
            }
        }
    }

    public synchronized void createTable(SQLiteTableCreator creator) {
        if (createdTable.contains(creator.getTableName())) {
            Log.v(TAG, "Table " + creator.getTableName() + " already in DB.");
        } else {
            Log.v(TAG, "Will create table " + creator.getTableName());
            createStatements.put(creator.getTableName(), creator);
            getWritableDatabase().needUpgrade(++dbVersion);
            onUpgrade(getWritableDatabase(), 0, ALWAYS_UPGRADE);
        }
    }

    /**
     * Method to return the columns and type for a specific table. This only
     * supports value defined in SQLType
     * @param table , the table name against which we want the columns
     * @return a map containing all columns and their type
     */
    public synchronized Map<String, SQLiteType> getColumnsForTable(final String table) {
        final Cursor cur = getReadableDatabase().rawQuery(
                new StringBuilder("PRAGMA table_info('").append(table).append("')").toString(),
                null);
        final Map<String, SQLiteType> ret = new HashMap<String, SQLiteType>(cur.getCount());
        while (cur.moveToNext()) {
            ret.put(cur.getString(cur.getColumnIndexOrThrow("name")),
                    SQLiteType.valueOf(cur.getString(cur.getColumnIndexOrThrow("type"))));
        }
        cur.close();
        return ret;
    }

    /**
     * Utility method to check if a table has been created in the database or
     * not.
     * @param tableName , the table to check if created or not
     * @return true if the table has been created. false otherwise
     */
    public synchronized boolean isTableCreated(final String tableName) {
        return createdTable.contains(tableName);
    }

}




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