// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static Object invokeGetter(Field field, Entity entity){
//     try {
//         Method method = getMethod("get",field);
//         if (method == null) return null;
//         return method.invoke(entity);
//     } catch (Exception e) {
//         L.w("invoking getter "+field.getName()+" failed in entity "+entity.getClass().getSimpleName());
//         L.e("invoking getter failed "+e.getLocalizedMessage());
//     }
//     return null;
// }


import android.content.ContentValues;
import com.basilgregory.onam.annotations.Table;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import static com.basilgregory.onam.android.DbUtil.invokeGetter;
        String rawQuery =  "SELECT :columnName FROM :table WHERE "+DB.PRIMARY_KEY_ID+" = :id Limit 1";
        return rawQuery
                .replace(":columnName",columnName)
                .replaceFirst(":table",DbUtil.getTableName(cls))
                .replaceFirst(":id",String.valueOf(id));
    }

    static String selectQuery(String tableName,String whereClause,String groupBy,String orderBy,Integer startIndex, Integer pageSize){
        if (tableName == null) return null;
        whereClause = whereClause == null? "" : " where "+ whereClause;
        groupBy = groupBy == null? "" : " group by "+ groupBy;
        orderBy = orderBy == null? "" : " order by "+orderBy;
        StringBuffer selectQuery = new StringBuffer("select * from ").append(tableName)
                .append(whereClause).append(groupBy).append(orderBy);
        if (pageSize != null)
            selectQuery.append(" LIMIT ").append(startIndex == null? 0 : startIndex).append(",").append(pageSize);
        selectQuery.append(";");
        return selectQuery.toString();
    }


    static ContentValues insertContentValues(Entity entity, Entity parentEntity){
        ContentValues contentValues = new ContentValues();
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field: fields) {
            try {
                if (Modifier.isTransient(field.getModifiers())) continue; //Transient field are already omitted from database.
                if (field.getType().getAnnotation(Table.class) != null) {
                    String columnName = DbUtil.getColumnName(field);
                    if (columnName == null) continue;
// Path: onam/src/main/java/com/basilgregory/onam/android/DbUtil.java
// static Object invokeGetter(Field field, Entity entity){
//     try {
//         Method method = getMethod("get",field);
//         if (method == null) return null;
//         return method.invoke(entity);
//     } catch (Exception e) {
//         L.w("invoking getter "+field.getName()+" failed in entity "+entity.getClass().getSimpleName());
//         L.e("invoking getter failed "+e.getLocalizedMessage());
//     }
//     return null;
// }


// Path: onam/src/main/java/com/basilgregory/onam/android/QueryBuilder.java
import android.content.ContentValues;
import com.basilgregory.onam.annotations.Table;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import static com.basilgregory.onam.android.DbUtil.invokeGetter;

        String rawQuery =  "SELECT :columnName FROM :table WHERE "+DB.PRIMARY_KEY_ID+" = :id Limit 1";
        return rawQuery
                .replace(":columnName",columnName)
                .replaceFirst(":table",DbUtil.getTableName(cls))
                .replaceFirst(":id",String.valueOf(id));
    }

    static String selectQuery(String tableName,String whereClause,String groupBy,String orderBy,Integer startIndex, Integer pageSize){
        if (tableName == null) return null;
        whereClause = whereClause == null? "" : " where "+ whereClause;
        groupBy = groupBy == null? "" : " group by "+ groupBy;
        orderBy = orderBy == null? "" : " order by "+orderBy;
        StringBuffer selectQuery = new StringBuffer("select * from ").append(tableName)
                .append(whereClause).append(groupBy).append(orderBy);
        if (pageSize != null)
            selectQuery.append(" LIMIT ").append(startIndex == null? 0 : startIndex).append(",").append(pageSize);
        selectQuery.append(";");
        return selectQuery.toString();
    }


    static ContentValues insertContentValues(Entity entity, Entity parentEntity){
        ContentValues contentValues = new ContentValues();
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field: fields) {
            try {
                if (Modifier.isTransient(field.getModifiers())) continue; //Transient field are already omitted from database.
                if (field.getType().getAnnotation(Table.class) != null) {
                    String columnName = DbUtil.getColumnName(field);
                    if (columnName == null) continue;
                    Object relatedEntity = DbUtil.invokeGetter(field,entity);