Skip to content

Instantly share code, notes, and snippets.

@SebastianEngel
Created March 14, 2014 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SebastianEngel/9550841 to your computer and use it in GitHub Desktop.
Save SebastianEngel/9550841 to your computer and use it in GitHub Desktop.
diff --git a/core/src/main/java/novoda/lib/sqliteprovider/provider/action/InsertHelper.java b/core/src/main/java/novoda/lib/sqliteprovider/provider/action/InsertHelper.java
index 864fb45..555da5f 100644
--- a/core/src/main/java/novoda/lib/sqliteprovider/provider/action/InsertHelper.java
+++ b/core/src/main/java/novoda/lib/sqliteprovider/provider/action/InsertHelper.java
@@ -6,6 +6,8 @@ import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
+import java.util.List;
+
import novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteOpenHelper;
import novoda.lib.sqliteprovider.util.Log;
import novoda.lib.sqliteprovider.util.UriUtils;
@@ -27,19 +29,25 @@ public class InsertHelper {
public long insert(Uri uri, ContentValues values) {
ContentValues insertValues = (values != null) ? new ContentValues(values) : new ContentValues();
final String table = UriUtils.getItemDirID(uri);
- final String firstConstrain = dbHelper.getFirstConstrain(table, insertValues);
appendParentReference(uri, insertValues);
long rowId = -1;
- if (firstConstrain != null) {
- rowId = tryUpdateWithConstrain(table, firstConstrain, insertValues);
- } else {
+
+ List<String> uniqueConstrains = dbHelper.getUniqueConstrains(table);
+
+ if (uniqueConstrains.size() == 1) {
+ rowId = tryUpdateWithConstrain(table, uniqueConstrains.get(0), insertValues);
+ } else if (uniqueConstrains.size() == 0) {
if (Log.Provider.warningLoggingEnabled()) {
Log.Provider.w("No constrain against URI: " + uri);
}
}
+
+ // No constraint or multiple constraints. In case of multiple constraints,
+ // just try the insert and trust on the "ON CONFLICT" clause defined in the table's SQL.
if (rowId <= 0) {
rowId = dbHelper.getWritableDatabase().insert(table, null, insertValues);
}
+
if (rowId > 0) {
return rowId;
}
diff --git a/core/src/main/java/novoda/lib/sqliteprovider/util/DBUtils.java b/core/src/main/java/novoda/lib/sqliteprovider/util/DBUtils.java
index 1ff4417..82027d7 100644
--- a/core/src/main/java/novoda/lib/sqliteprovider/util/DBUtils.java
+++ b/core/src/main/java/novoda/lib/sqliteprovider/util/DBUtils.java
@@ -121,7 +121,7 @@ public final class DBUtils {
if (isUnique == 1) {
String name = pragmas.getString(1);
final Cursor pragmaInfo = db.rawQuery(String.format(PRGAMA_INDEX_INFO, name), null);
- if (pragmaInfo.moveToFirst()) {
+ while (pragmaInfo.moveToNext()) {
constrains.add(pragmaInfo.getString(2));
}
pragmaInfo.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment