package com.zhangke.shizhong.db;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "RATION_RECORD".
*/
public class RationRecordDao extends AbstractDao<RationRecord, Long> {

    public static final String TABLENAME = "RATION_RECORD";

    /**
     * Properties of entity RationRecord.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property ParentPlanId = new Property(1, Long.class, "parentPlanId", false, "PARENT_PLAN_ID");
        public final static Property Name = new Property(2, String.class, "name", false, "NAME");
        public final static Property Value = new Property(3, double.class, "value", false, "VALUE");
        public final static Property Date = new Property(4, String.class, "date", false, "DATE");
    }

    private Query<RationRecord> rationPlan_ClockRecordsQuery;

    public RationRecordDao(DaoConfig config) {
        super(config);
    }
    
    public RationRecordDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"RATION_RECORD\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"PARENT_PLAN_ID\" INTEGER," + // 1: parentPlanId
                "\"NAME\" TEXT," + // 2: name
                "\"VALUE\" REAL NOT NULL ," + // 3: value
                "\"DATE\" TEXT);"); // 4: date
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"RATION_RECORD\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, RationRecord entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long parentPlanId = entity.getParentPlanId();
        if (parentPlanId != null) {
            stmt.bindLong(2, parentPlanId);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
        stmt.bindDouble(4, entity.getValue());
 
        String date = entity.getDate();
        if (date != null) {
            stmt.bindString(5, date);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, RationRecord entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long parentPlanId = entity.getParentPlanId();
        if (parentPlanId != null) {
            stmt.bindLong(2, parentPlanId);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
        stmt.bindDouble(4, entity.getValue());
 
        String date = entity.getDate();
        if (date != null) {
            stmt.bindString(5, date);
        }
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public RationRecord readEntity(Cursor cursor, int offset) {
        RationRecord entity = new RationRecord( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // parentPlanId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // name
            cursor.getDouble(offset + 3), // value
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4) // date
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, RationRecord entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setParentPlanId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setValue(cursor.getDouble(offset + 3));
        entity.setDate(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(RationRecord entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(RationRecord entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(RationRecord entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "clockRecords" to-many relationship of RationPlan. */
    public List<RationRecord> _queryRationPlan_ClockRecords(Long parentPlanId) {
        synchronized (this) {
            if (rationPlan_ClockRecordsQuery == null) {
                QueryBuilder<RationRecord> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ParentPlanId.eq(null));
                rationPlan_ClockRecordsQuery = queryBuilder.build();
            }
        }
        Query<RationRecord> query = rationPlan_ClockRecordsQuery.forCurrentThread();
        query.setParameter(0, parentPlanId);
        return query.list();
    }

}
