package com.adins.mss.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.database.Database;
import de.greenrobot.dao.database.DatabaseStatement;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.adins.mss.dao.Kompetisi;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "MS_KOMPETISI".
*/
public class KompetisiDao extends AbstractDao<Kompetisi, String> {

    public static final String TABLENAME = "MS_KOMPETISI";

    /**
     * Properties of entity Kompetisi.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Uuid_kompetisi = new Property(0, String.class, "uuid_kompetisi", true, "UUID_KOMPETISI");
        public final static Property MembershipProgramCode = new Property(1, String.class, "membershipProgramCode", false, "MEMBERSHIP_PROGRAM_CODE");
        public final static Property MembershipProgramName = new Property(2, String.class, "membershipProgramName", false, "MEMBERSHIP_PROGRAM_NAME");
        public final static Property MembershipProgramPriorityCode = new Property(3, String.class, "membershipProgramPriorityCode", false, "MEMBERSHIP_PROGRAM_PRIORITY_CODE");
        public final static Property MembershipProgramStatus = new Property(4, String.class, "membershipProgramStatus", false, "MEMBERSHIP_PROGRAM_STATUS");
        public final static Property MembershipProgramExpiredDate = new Property(5, String.class, "membershipProgramExpiredDate", false, "MEMBERSHIP_PROGRAM_EXPIRED_DATE");
        public final static Property MembershipProgramStartDate = new Property(6, String.class, "membershipProgramStartDate", false, "MEMBERSHIP_PROGRAM_START_DATE");
        public final static Property GracePeriode = new Property(7, String.class, "gracePeriode", false, "GRACE_PERIODE");
        public final static Property MembershipProgramLogo = new Property(8, String.class, "membershipProgramLogo", false, "MEMBERSHIP_PROGRAM_LOGO");
        public final static Property Uuid_user = new Property(9, String.class, "uuid_user", false, "UUID_USER");
    };

    private DaoSession daoSession;

    private Query<Kompetisi> user_KompetisiListQuery;

    public KompetisiDao(DaoConfig config) {
        super(config);
    }
    
    public KompetisiDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = 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 + "\"MS_KOMPETISI\" (" + //
                "\"UUID_KOMPETISI\" TEXT PRIMARY KEY NOT NULL ," + // 0: uuid_kompetisi
                "\"MEMBERSHIP_PROGRAM_CODE\" TEXT," + // 1: membershipProgramCode
                "\"MEMBERSHIP_PROGRAM_NAME\" TEXT," + // 2: membershipProgramName
                "\"MEMBERSHIP_PROGRAM_PRIORITY_CODE\" TEXT," + // 3: membershipProgramPriorityCode
                "\"MEMBERSHIP_PROGRAM_STATUS\" TEXT," + // 4: membershipProgramStatus
                "\"MEMBERSHIP_PROGRAM_EXPIRED_DATE\" TEXT," + // 5: membershipProgramExpiredDate
                "\"MEMBERSHIP_PROGRAM_START_DATE\" TEXT," + // 6: membershipProgramStartDate
                "\"GRACE_PERIODE\" TEXT," + // 7: gracePeriode
                "\"MEMBERSHIP_PROGRAM_LOGO\" TEXT," + // 8: membershipProgramLogo
                "\"UUID_USER\" TEXT);"); // 9: uuid_user
    }

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

    /** @inheritdoc */
    @Override
    protected void bindValues(DatabaseStatement stmt, Kompetisi entity) {
        stmt.clearBindings();
        stmt.bindString(1, entity.getUuid_kompetisi());
 
        String membershipProgramCode = entity.getMembershipProgramCode();
        if (membershipProgramCode != null) {
            stmt.bindString(2, membershipProgramCode);
        }
 
        String membershipProgramName = entity.getMembershipProgramName();
        if (membershipProgramName != null) {
            stmt.bindString(3, membershipProgramName);
        }
 
        String membershipProgramPriorityCode = entity.getMembershipProgramPriorityCode();
        if (membershipProgramPriorityCode != null) {
            stmt.bindString(4, membershipProgramPriorityCode);
        }
 
        String membershipProgramStatus = entity.getMembershipProgramStatus();
        if (membershipProgramStatus != null) {
            stmt.bindString(5, membershipProgramStatus);
        }
 
        String membershipProgramExpiredDate = entity.getMembershipProgramExpiredDate();
        if (membershipProgramExpiredDate != null) {
            stmt.bindString(6, membershipProgramExpiredDate);
        }
 
        String membershipProgramStartDate = entity.getMembershipProgramStartDate();
        if (membershipProgramStartDate != null) {
            stmt.bindString(7, membershipProgramStartDate);
        }
 
        String gracePeriode = entity.getGracePeriode();
        if (gracePeriode != null) {
            stmt.bindString(8, gracePeriode);
        }
 
        String membershipProgramLogo = entity.getMembershipProgramLogo();
        if (membershipProgramLogo != null) {
            stmt.bindString(9, membershipProgramLogo);
        }
 
        String uuid_user = entity.getUuid_user();
        if (uuid_user != null) {
            stmt.bindString(10, uuid_user);
        }
    }

    @Override
    protected void attachEntity(Kompetisi entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.getString(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Kompetisi readEntity(Cursor cursor, int offset) {
        Kompetisi entity = new Kompetisi( //
            cursor.getString(offset + 0), // uuid_kompetisi
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // membershipProgramCode
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // membershipProgramName
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // membershipProgramPriorityCode
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // membershipProgramStatus
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // membershipProgramExpiredDate
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // membershipProgramStartDate
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // gracePeriode
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // membershipProgramLogo
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9) // uuid_user
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Kompetisi entity, int offset) {
        entity.setUuid_kompetisi(cursor.getString(offset + 0));
        entity.setMembershipProgramCode(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setMembershipProgramName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setMembershipProgramPriorityCode(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setMembershipProgramStatus(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setMembershipProgramExpiredDate(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setMembershipProgramStartDate(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setGracePeriode(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setMembershipProgramLogo(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setUuid_user(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
     }
    
    /** @inheritdoc */
    @Override
    protected String updateKeyAfterInsert(Kompetisi entity, long rowId) {
        return entity.getUuid_kompetisi();
    }
    
    /** @inheritdoc */
    @Override
    public String getKey(Kompetisi entity) {
        if(entity != null) {
            return entity.getUuid_kompetisi();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "kompetisiList" to-many relationship of User. */
    public List<Kompetisi> _queryUser_KompetisiList(String uuid_user) {
        synchronized (this) {
            if (user_KompetisiListQuery == null) {
                QueryBuilder<Kompetisi> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Uuid_user.eq(null));
                user_KompetisiListQuery = queryBuilder.build();
            }
        }
        Query<Kompetisi> query = user_KompetisiListQuery.forCurrentThread();
        query.setParameter(0, uuid_user);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getUserDao().getAllColumns());
            builder.append(" FROM MS_KOMPETISI T");
            builder.append(" LEFT JOIN MS_USER T0 ON T.\"UUID_USER\"=T0.\"UUID_USER\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Kompetisi loadCurrentDeep(Cursor cursor, boolean lock) {
        Kompetisi entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        User user = loadCurrentOther(daoSession.getUserDao(), cursor, offset);
        entity.setUser(user);

        return entity;    
    }

    public Kompetisi loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Kompetisi> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Kompetisi> list = new ArrayList<Kompetisi>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Kompetisi> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Kompetisi> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
