Example usage for android.database.sqlite SQLiteDatabase CONFLICT_FAIL

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

Introduction

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

Prototype

int CONFLICT_FAIL

To view the source code for android.database.sqlite SQLiteDatabase CONFLICT_FAIL.

Click Source Link

Document

When a constraint violation occurs, the command aborts with a return code SQLITE_CONSTRAINT.

Usage

From source file:ru.gkpromtech.exhibition.db.DbHelper.java

public void applyUpdates(SQLiteDatabase db, JsonNode updates, boolean isStatic) throws Exception {

    JsonNode nodeRev = updates.get("revision");
    if (nodeRev == null)
        return;//from   w w w .j a  va  2s .  c  o  m

    final String synchronous = getPragma(db, "synchronous");
    final String journalMode = getPragma(db, "journal_mode");

    db.rawQuery("PRAGMA synchronous = OFF", null);
    db.rawQuery("PRAGMA journal_mode = MEMORY", null);
    //  FK,  ?   ???    add-update
    db.execSQL("PRAGMA foreign_keys = OFF");

    SharedPreferences prefs = getPrefs();

    int langId = Profile.getInstance(mContext).getLangId();
    int currentRevision = prefs.getInt("revision", 0);
    ObjectMapper mapper = new ObjectMapper();
    int revision = nodeRev.asInt();
    ArrayNode nodeChanges = (ArrayNode) updates.get("changes");

    TypeReference<List<Change>> typeRef = new TypeReference<List<Change>>() {
    };
    List<Change> changes = mapper.readValue(nodeChanges.traverse(), typeRef);

    Map<Table, List<Integer>> deletedTableRowIds = new HashMap<>();

    try {
        db.beginTransaction();

        for (Change change : changes) {
            if (currentRevision > change.id) {
                Log.w("PPDB", "Skipping old change #" + change.id);
                continue;
            }

            boolean tr = change.entity.endsWith("_tr");
            String entityName = !tr ? change.entity : change.entity.substring(0, change.entity.length() - 3);

            Class<? extends Entity> entity = getEntityForTableName(entityName);
            if (entity == null) {
                Log.e("PPDB", "Cannot find entity for " + entityName);
                continue;
            }

            Table<? extends Entity> table = getTableFor(entity);
            if (table == null) {
                Log.e("PPDB", "Cannot find table for entity " + entityName);
                continue;
            }

            if (!tr) {
                if (change.data != null) {
                    switch (change.changetype) {
                    case Change.ADDED:
                        table.insert(db, change.data, SQLiteDatabase.CONFLICT_FAIL);
                        break;
                    case Change.UPDATED:
                        change.data.remove("id");
                        table.partialUpdate(db, change.rowid, change.data, SQLiteDatabase.CONFLICT_FAIL);
                        break;
                    }
                } else {
                    if (change.changetype == Change.DELETED) {
                        List<Integer> ids = deletedTableRowIds.get(table);
                        if (ids == null) {
                            ids = new ArrayList<>();
                            deletedTableRowIds.put(table, ids);
                        }
                        ids.add(change.rowid);
                    }
                }
            } else if (change.data != null) {
                int changeLangId = change.data.get("languageid").asInt();
                if (changeLangId != langId)
                    continue;
                change.data.remove("languageid");
                switch (change.changetype) {
                case Change.ADDED:
                case Change.UPDATED:
                    TableRef annotation = entity.getAnnotation(TableRef.class);
                    if (annotation == null) {
                        Log.e("PPDB", "Cannot get trid field for entity " + entityName);
                        continue;
                    }
                    String trIdName = annotation.trid();
                    JsonNode nodeTrId = change.data.get(trIdName);
                    if (nodeTrId == null) {
                        Log.e("PPDB", "Change data don't have a field [" + trIdName + "]: " + entityName);
                        continue;
                    }
                    int id = nodeTrId.asInt();
                    change.data.remove("id");
                    change.data.remove(trIdName);
                    table.partialUpdate(db, id, change.data, SQLiteDatabase.CONFLICT_FAIL);
                    break;
                }
            }
        }

        db.setTransactionSuccessful();

    } finally {
        db.endTransaction();
    }

    //   ??? ?, ? FK
    db.execSQL("PRAGMA foreign_keys = ON");

    try {
        db.beginTransaction();

        for (Map.Entry<Table, List<Integer>> entry : deletedTableRowIds.entrySet()) {
            Table table = entry.getKey();
            List<Integer> ids = entry.getValue();
            for (Integer id : ids)
                table.delete(db, id);
        }

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

    if (synchronous != null)
        db.rawQuery("PRAGMA synchronous = " + synchronous, null);
    if (journalMode != null)
        db.rawQuery("PRAGMA journal_mode = " + journalMode, null);

    if (revision > currentRevision)
        prefs.edit().putInt("revision", revision).apply();

    if (isStatic)
        prefs.edit().putInt("jsonRevision", revision).apply();
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void insert(T item) {
    insert(item, SQLiteDatabase.CONFLICT_FAIL);
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void insert(List<T> items) {
    insert(items, SQLiteDatabase.CONFLICT_FAIL);
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void update(T item) {
    update(item, SQLiteDatabase.CONFLICT_FAIL);
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void update(List<T> items) {
    update(items, SQLiteDatabase.CONFLICT_FAIL);
}