package com.braunster.chatsdk.dao;

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

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.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.braunster.chatsdk.dao.FollowerLink;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table FOLLOWER_LINK.
*/
public class FollowerLinkDao extends AbstractDao<FollowerLink, Long> {

    public static final String TABLENAME = "FOLLOWER_LINK";

    /**
     * Properties of entity FollowerLink.<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 Type = new Property(1, Integer.class, "type", false, "TYPE");
        public final static Property BUserDaoId = new Property(2, Long.class, "BUserDaoId", false, "BUSER_DAO_ID");
        public final static Property LinkOwnerBUserDaoId = new Property(3, Long.class, "linkOwnerBUserDaoId", false, "LINK_OWNER_BUSER_DAO_ID");
    };

    private DaoSession daoSession;

    private Query<FollowerLink> bUser_FollowerLinksQuery;

    public FollowerLinkDao(DaoConfig config) {
        super(config);
    }
    
    public FollowerLinkDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'FOLLOWER_LINK' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'TYPE' INTEGER," + // 1: type
                "'BUSER_DAO_ID' INTEGER," + // 2: BUserDaoId
                "'LINK_OWNER_BUSER_DAO_ID' INTEGER);"); // 3: linkOwnerBUserDaoId
    }

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

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, FollowerLink entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Integer type = entity.getType();
        if (type != null) {
            stmt.bindLong(2, type);
        }
 
        Long BUserDaoId = entity.getBUserDaoId();
        if (BUserDaoId != null) {
            stmt.bindLong(3, BUserDaoId);
        }
 
        Long linkOwnerBUserDaoId = entity.getLinkOwnerBUserDaoId();
        if (linkOwnerBUserDaoId != null) {
            stmt.bindLong(4, linkOwnerBUserDaoId);
        }
    }

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

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

    /** @inheritdoc */
    @Override
    public FollowerLink readEntity(Cursor cursor, int offset) {
        FollowerLink entity = new FollowerLink( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1), // type
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // BUserDaoId
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3) // linkOwnerBUserDaoId
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, FollowerLink entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setType(cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1));
        entity.setBUserDaoId(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setLinkOwnerBUserDaoId(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(FollowerLink entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(FollowerLink entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "followerLinks" to-many relationship of BUser. */
    public List<FollowerLink> _queryBUser_FollowerLinks(Long BUserDaoId) {
        synchronized (this) {
            if (bUser_FollowerLinksQuery == null) {
                QueryBuilder<FollowerLink> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.BUserDaoId.eq(null));
                bUser_FollowerLinksQuery = queryBuilder.build();
            }
        }
        Query<FollowerLink> query = bUser_FollowerLinksQuery.forCurrentThread();
        query.setParameter(0, BUserDaoId);
        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.getBUserDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getBUserDao().getAllColumns());
            builder.append(" FROM FOLLOWER_LINK T");
            builder.append(" LEFT JOIN BUSER T0 ON T.'BUSER_DAO_ID'=T0.'_id'");
            builder.append(" LEFT JOIN BUSER T1 ON T.'LINK_OWNER_BUSER_DAO_ID'=T1.'_id'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected FollowerLink loadCurrentDeep(Cursor cursor, boolean lock) {
        FollowerLink entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        BUser BUser = loadCurrentOther(daoSession.getBUserDao(), cursor, offset);
        entity.setBUser(BUser);
        offset += daoSession.getBUserDao().getAllColumns().length;

        BUser linkOwnerBUser = loadCurrentOther(daoSession.getBUserDao(), cursor, offset);
        entity.setLinkOwnerBUser(linkOwnerBUser);

        return entity;    
    }

    public FollowerLink 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<FollowerLink> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<FollowerLink> list = new ArrayList<FollowerLink>(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<FollowerLink> 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<FollowerLink> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}