Bug 738347: Null value check for ContentValues in SQLiteBridge. [r=rnewman]
authorSriram Ramasubramanian <sriram@mozilla.com>
Mon, 09 Apr 2012 10:48:51 -0700
changeset 94579 36adc9ed1d0ed7170e67163d144db2fe67cb439a
parent 94578 308fedc536c1675a4dbe1404d14e5c037ed982a5
child 94580 490b6732369e10c0326d5e29e56ec23de59046b9
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs738347
milestone14.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 738347: Null value check for ContentValues in SQLiteBridge. [r=rnewman]
mobile/android/base/sqlite/SQLiteBridge.java
--- a/mobile/android/base/sqlite/SQLiteBridge.java
+++ b/mobile/android/base/sqlite/SQLiteBridge.java
@@ -131,27 +131,33 @@ public class SQLiteBridge {
     /* This method is referenced by Robocop via reflection. */
     public Cursor rawQuery(String sql, String[] selectionArgs)
         throws SQLiteBridgeException {
         return internalQuery(sql, selectionArgs);
     }
 
     public long insert(String table, String nullColumnHack, ContentValues values)
                throws SQLiteBridgeException {
-        Set<Entry<String, Object>> valueSet = values.valueSet();
-        Iterator<Entry<String, Object>> valueIterator = valueSet.iterator();
+        if (values == null)
+            return 0;
+
         ArrayList<String> valueNames = new ArrayList<String>();
         ArrayList<String> valueBinds = new ArrayList<String>();
         ArrayList<String> keyNames = new ArrayList<String>();
 
-        while(valueIterator.hasNext()) {
-            Entry<String, Object> value = valueIterator.next();
+        for (Entry<String, Object> value : values.valueSet()) {
             keyNames.add(value.getKey());
-            valueNames.add("?");
-            valueBinds.add(value.getValue().toString());
+
+            Object val = value.getValue();
+            if (val == null) {
+                valueNames.add("NULL");
+            } else {
+                valueNames.add("?");
+                valueBinds.add(val.toString());
+            }
         }
 
         StringBuilder sb = new StringBuilder("INSERT into ");
         sb.append(table);
 
         sb.append(" (");
         sb.append(TextUtils.join(", ", keyNames));
         sb.append(")");
@@ -164,35 +170,45 @@ public class SQLiteBridge {
         String[] binds = new String[valueBinds.size()];
         valueBinds.toArray(binds);
         internalQuery(sb.toString(), binds);
         return mQueryResults[RESULT_INSERT_ROW_ID];
     }
 
     public int update(String table, ContentValues values, String whereClause, String[] whereArgs)
                throws SQLiteBridgeException {
-        Set<Entry<String, Object>> valueSet = values.valueSet();
-        Iterator<Entry<String, Object>> valueIterator = valueSet.iterator();
+        if (values == null)
+            return 0;
+
         ArrayList<String> valueNames = new ArrayList<String>();
 
         StringBuilder sb = new StringBuilder("UPDATE ");
         sb.append(table);
+        sb.append(" SET ");
 
-        sb.append(" SET ");
-        while(valueIterator.hasNext()) {
-            Entry<String, Object> value = valueIterator.next();
-            sb.append(value.getKey() + " = ?");
-            valueNames.add(value.getValue().toString());
-            if (valueIterator.hasNext())
+        boolean isFirst = true;
+
+        for (Entry<String, Object> value : values.valueSet()) {
+            if (isFirst)
+                isFirst = false;
+            else
                 sb.append(", ");
-            else
-                sb.append(" ");
+
+            sb.append(value.getKey());
+
+            Object val = value.getValue();
+            if (val == null) {
+                sb.append(" = NULL");
+            } else {
+                sb.append(" = ?");
+                valueNames.add(val.toString());
+            }
         }
 
-        if (whereClause != null) {
+        if (!TextUtils.isEmpty(whereClause)) {
             sb.append(" WHERE ");
             sb.append(whereClause);
             for (int i = 0; i < whereArgs.length; i++) {
                 valueNames.add(whereArgs[i]);
             }
         }
 
         String[] binds = new String[valueNames.size()];