package interdroid.util;

import java.util.Map.Entry;

import android.content.ContentValues;

public final class DbUtil {

    /**
     * No construction.
     */
    private DbUtil() { }

    /**
     * Returns a quoted column name if the column is not already quoted.
     * @param columnName the name of the column to quote
     * @return the quoted column name
     */
    public static String quoteColumnName(final String columnName) {
        if (columnName.charAt(0) != '\'') {
            return "'" + columnName + "'";
        }
        return columnName;
    }


    /**
     * Android does not quote identifiers so we have to handle that.
     * This returns a new ContentValues where the columns have all been
     * quoted properly.
     *
     * @param values the values to be quoted
     * @return a sanitized version of the values with quoted column names
     */
    public static ContentValues quoteColumnNames(final ContentValues values) {
        ContentValues cleanValues = new ContentValues();
        for (Entry<String, Object> val : values.valueSet()) {
            Object value = val.getValue();
            String cleanName = val.getKey();
            cleanName = quoteColumnName(cleanName);
            // This really sucks. There is no generic put an object....
            if (value == null) {
                cleanValues.putNull(cleanName);
            } else if (value instanceof Boolean) {
                cleanValues.put(cleanName, (Boolean) value);
            } else if (value instanceof Byte) {
                cleanValues.put(cleanName, (Byte) value);
            } else if (value instanceof byte[]) {
                cleanValues.put(cleanName, (byte[]) value);
            } else if (value instanceof Double) {
                cleanValues.put(cleanName, (Double) value);
            } else if (value instanceof Float) {
                cleanValues.put(cleanName, (Float) value);
            } else if (value instanceof Integer) {
                cleanValues.put(cleanName, (Integer) value);
            } else if (value instanceof Long) {
                cleanValues.put(cleanName, (Long) value);
            } else if (value instanceof Short) {
                cleanValues.put(cleanName, (Short) value);
            } else if (value instanceof String) {
                cleanValues.put(cleanName, (String) value);
            } else {
                throw new RuntimeException(
                        "Don't know how to add value of type: "
                                + value.getClass().getCanonicalName());
            }
        }
        return cleanValues;
    }
}

@@ -59,67 +59,275 @@
 ort 
-android.content.ContentValues;%0A%0Apublic final class DbUtil %7B
+org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport android.content.ContentValues;%0Aimport android.database.Cursor;%0A%0Apublic final class DbUtil %7B%0A    /**%0A     * Access to Logger.%0A     */%0A    private static final Logger LOG = LoggerFactory.getLogger(DbUtil.class);
 %0A%0A  
@@ -680,17 +680,17 @@
 0) != '%5C
-'
+%22
 ') %7B%0A   
@@ -706,17 +706,18 @@
 return %22
-'
+%5C%22
 %22 + colu
@@ -726,17 +726,18 @@
 Name + %22
-'
+%5C%22
 %22;%0A     
@@ -775,16 +775,1122 @@
 %0A    %7D%0A%0A
+    /**%0A     * Returns an array of quoted column names%0A     * @param columnNames the names of the columns to quote.%0A     * @return an array of quoted column names%0A     */%0A    public static String%5B%5D quoteColumnNames(final String%5B%5D columnNames) %7B%0A        String%5B%5D ret = null;%0A        if (columnNames != null) %7B%0A            ret = new String%5BcolumnNames.length%5D;%0A            for (int i = 0; i %3C columnNames.length; i++) %7B%0A                ret%5Bi%5D = quoteColumnName(columnNames%5Bi%5D);%0A            %7D%0A        %7D%0A        return ret;%0A    %7D%0A%0A    /**%0A     * Returns the index for a field.%0A     * @param c the cursor to check in%0A     * @param columnName the column name to look for%0A     * @return the index, or -1%0A     */%0A    public static int getFieldIndex(Cursor c, String columnName) %7B%0A        int index = c.getColumnIndex(columnName);%0A        if (index == -1) %7B%0A            index = c.getColumnIndex(quoteColumnName(columnName));%0A        %7D%0A        if (index == -1) %7B%0A            LOG.warn(%22Unable to find column index: %7B%7D in %7B%7D%22,%0A                    columnName, c.getColumnNames());%0A        %7D%0A        return index;%0A    %7D%0A
 %0A    /**

Add column quoting utilities.