package org.hisp.dhis.android.core.common;

import android.database.sqlite.SQLiteStatement;

/**
 * Provides helper functions to handle null checks and type conversions.
 */
public class StoreUtils {

    /**
     * Handle if String argument is null and bind it using .bindNull() if so.
     * A helper function to abstract/clean up boilerplate if/else bloat..
     *
     * @param sqLiteStatement
     * @param index
     * @param arg
     */
    public static void sqLiteBind(SQLiteStatement sqLiteStatement, int index, String arg) {
        if (arg == null) {
            sqLiteStatement.bindNull(index);
        } else {
            sqLiteStatement.bindString(index, arg);
        }
    }

    /**
     * Handle if Boolean argument is null and bind it using .bindNull() if so.
     * A helper function to abstract/clean up boilerplate if/else bloat...
     * Also convet the Boolean to Long...
     *
     * @param sqLiteStatement
     * @param index
     * @param arg
     */
    public static void sqLiteBind(SQLiteStatement sqLiteStatement, int index, Boolean arg) {
        if (arg == null) {
            sqLiteStatement.bindNull(index);
        } else {
            sqLiteStatement.bindLong(index, arg ? 1 : 0);
        }
    }

    /**
     * Handle if Integer argument is null and bind it using .bindNull() if so.
     * A helper function to abstract/clean up boilerplate if/else bloat..
     *
     * @param sqLiteStatement
     * @param index
     * @param arg
     */
    public static void sqLiteBind(SQLiteStatement sqLiteStatement, int index, Integer arg) {
        if (arg == null) {
            sqLiteStatement.bindNull(index);
        } else {
            sqLiteStatement.bindLong(index, arg);
        }
    }
}

@@ -86,16 +86,40 @@
 ement;%0A%0A
+import java.util.Date;%0A%0A
 /**%0A * P
@@ -1747,10 +1747,1043 @@
 %7D%0A    %7D%0A
+%0A%0A    /**%0A     * Handle if Date argument is null and bind it using .bindNull() if so.%0A     * A helper function to abstract/clean up boilerplate if/else bloat..%0A     *%0A     * @param sqLiteStatement%0A     * @param index%0A     * @param arg%0A     */%0A    public static void sqLiteBind(SQLiteStatement sqLiteStatement, int index, Date arg) %7B%0A        if (arg == null) %7B%0A            sqLiteStatement.bindNull(index);%0A        %7D else %7B%0A            sqLiteStatement.bindString(index, BaseIdentifiableObject.DATE_FORMAT.format(arg));%0A        %7D%0A    %7D%0A%0A    /**%0A     * Handle if Enum argument is null and bind it using .bindNull() if so.%0A     * A helper function to abstract/clean up boilerplate if/else bloat..%0A     *%0A     * @param sqLiteStatement%0A     * @param index%0A     * @param arg%0A     */%0A    public static void sqLiteBind(SQLiteStatement sqLiteStatement, int index, Enum arg) %7B%0A        if (arg == null) %7B%0A            sqLiteStatement.bindNull(index);%0A        %7D else %7B%0A            sqLiteStatement.bindString(index, arg.toString());%0A        %7D%0A    %7D%0A
 %7D%0A

Support sql binding of Dates and Enums in StoreUtils