66import android .database .SQLException ;
77import android .net .Uri ;
88
9+ import java .util .Arrays ;
10+ import java .util .List ;
11+
912import novoda .lib .sqliteprovider .sqlite .ExtendedSQLiteOpenHelper ;
13+ import novoda .lib .sqliteprovider .util .Constraint ;
1014import novoda .lib .sqliteprovider .util .Log ;
1115import novoda .lib .sqliteprovider .util .UriUtils ;
1216
@@ -27,11 +31,11 @@ public InsertHelper(ExtendedSQLiteOpenHelper helper) {
2731 public long insert (Uri uri , ContentValues values ) {
2832 ContentValues insertValues = (values != null ) ? new ContentValues (values ) : new ContentValues ();
2933 final String table = UriUtils .getItemDirID (uri );
30- final String firstConstrain = dbHelper .getFirstConstrain (table , insertValues );
34+ final Constraint constraint = dbHelper .getFirstConstraint (table , insertValues );
3135 appendParentReference (uri , insertValues );
3236 long rowId = -1 ;
33- if (firstConstrain != null ) {
34- rowId = tryUpdateWithConstrain (table , firstConstrain , insertValues );
37+ if (constraint != null ) {
38+ rowId = tryUpdateWithConstraint (table , constraint , insertValues );
3539 } else {
3640 if (Log .Provider .warningLoggingEnabled ()) {
3741 Log .Provider .w ("No constrain against URI: " + uri );
@@ -48,36 +52,77 @@ public long insert(Uri uri, ContentValues values) {
4852 throw new SQLException ("Failed to insert row into " + uri );
4953 }
5054
55+ /**
56+ * Use {@link #tryUpdateWithConstraint(String, Constraint, ContentValues)}
57+ */
58+ @ Deprecated
5159 protected long tryUpdateWithConstrain (String table , String constrain , ContentValues values ) {
5260 long rowId = -1 ;
5361 int update = dbHelper .getWritableDatabase ().update (table , values , constrain + "=?" ,
54- new String [] {
55- values .getAsString (constrain )
56- });
62+ new String []{
63+ values .getAsString (constrain )
64+ });
5765
5866 if (Log .Provider .verboseLoggingEnabled ()) {
5967 Log .Provider .v ("Constrain " + constrain + " yield " + update );
6068 }
6169 if (update > 0 ) {
62- rowId = getRowIdForUpdate (table , constrain , values );
70+ rowId = getRowIdForUpdate (table , new Constraint ( Arrays . asList ( constrain )) , values );
6371 }
6472 return rowId ;
6573 }
6674
75+ protected long tryUpdateWithConstraint (String table , Constraint constraint , ContentValues values ) {
76+ long rowId = -1 ;
77+ String whereClause = getWhereClause (constraint );
78+ String [] whereArgs = getWhereArguments (constraint , values );
79+ int update = dbHelper .getWritableDatabase ().update (table , values , whereClause , whereArgs );
80+
81+ if (Log .Provider .verboseLoggingEnabled ()) {
82+ Log .Provider .v ("Constrain " + constraint + " yield " + update );
83+ }
84+ if (update > 0 ) {
85+ rowId = getRowIdForUpdate (table , constraint , values );
86+ }
87+ return rowId ;
88+ }
89+
90+ private String getWhereClause (Constraint constraint ) {
91+ List <String > columns = constraint .getColumns ();
92+ String whereClause = "" ;
93+ for (int i = 0 ; i < columns .size (); i ++) {
94+ String column = columns .get (i );
95+ if (i > 0 ) {
96+ whereClause += " AND " ;
97+ }
98+ whereClause += column + "=?" ;
99+ }
100+ return whereClause ;
101+ }
102+
103+ private String [] getWhereArguments (Constraint constraint , ContentValues values ) {
104+ List <String > columns = constraint .getColumns ();
105+ int columnCount = columns .size ();
106+ String [] whereArgs = new String [columnCount ];
107+ for (int i = 0 ; i < columnCount ; i ++) {
108+ String column = columns .get (i );
109+ whereArgs [i ] = values .getAsString (column );
110+ }
111+ return whereArgs ;
112+ }
113+
67114 /**
68115 * Will get the Row id from the latest update.
69116 *
70117 * @param table
71- * @param constrain
118+ * @param constraint
72119 * @param values
73120 * @return
74121 */
75- private long getRowIdForUpdate (String table , String constrain , ContentValues values ) {
76- final Cursor cur = dbHelper .getReadableDatabase ().query (table , new String [] {
122+ private long getRowIdForUpdate (String table , Constraint constraint , ContentValues values ) {
123+ final Cursor cur = dbHelper .getReadableDatabase ().query (table , new String []{
77124 "rowid"
78- }, constrain + "=?" , new String [] {
79- values .getAsString (constrain )
80- }, null , null , null );
125+ }, getWhereClause (constraint ), getWhereArguments (constraint , values ), null , null , null );
81126 if (!cur .moveToFirst ()) {
82127 return -1 ;
83128 }
0 commit comments