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.UserThreadLink;

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

    public static final String TABLENAME = "USER_THREAD_LINK";

    /**
     * Properties of entity UserThreadLink.<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 BUserDaoId = new Property(1, Long.class, "BUserDaoId", false, "BUSER_DAO_ID");
        public final static Property BThreadDaoId = new Property(2, Long.class, "BThreadDaoId", false, "BTHREAD_DAO_ID");
    };

    private DaoSession daoSession;

    private Query<UserThreadLink> bUser_UserThreadLinksQuery;
    private Query<UserThreadLink> bThread_UserThreadLinksQuery;

    public UserThreadLinkDao(DaoConfig config) {
        super(config);
    }
    
    public UserThreadLinkDao(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 + "'USER_THREAD_LINK' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'BUSER_DAO_ID' INTEGER," + // 1: BUserDaoId
                "'BTHREAD_DAO_ID' INTEGER);"); // 2: BThreadDaoId
    }

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

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, UserThreadLink entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long BUserDaoId = entity.getBUserDaoId();
        if (BUserDaoId != null) {
            stmt.bindLong(2, BUserDaoId);
        }
 
        Long BThreadDaoId = entity.getBThreadDaoId();
        if (BThreadDaoId != null) {
            stmt.bindLong(3, BThreadDaoId);
        }
    }

    @Override
    protected void attachEntity(UserThreadLink 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 UserThreadLink readEntity(Cursor cursor, int offset) {
        UserThreadLink entity = new UserThreadLink( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // BUserDaoId
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2) // BThreadDaoId
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, UserThreadLink entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setBUserDaoId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setBThreadDaoId(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(UserThreadLink entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(UserThreadLink entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "userThreadLinks" to-many relationship of BUser. */
    public List<UserThreadLink> _queryBUser_UserThreadLinks(Long BUserDaoId) {
        synchronized (this) {
            if (bUser_UserThreadLinksQuery == null) {
                QueryBuilder<UserThreadLink> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.BUserDaoId.eq(null));
                bUser_UserThreadLinksQuery = queryBuilder.build();
            }
        }
        Query<UserThreadLink> query = bUser_UserThreadLinksQuery.forCurrentThread();
        query.setParameter(0, BUserDaoId);
        return query.list();
    }

    /** Internal query to resolve the "userThreadLinks" to-many relationship of BThread. */
    public List<UserThreadLink> _queryBThread_UserThreadLinks(Long BThreadDaoId) {
        synchronized (this) {
            if (bThread_UserThreadLinksQuery == null) {
                QueryBuilder<UserThreadLink> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.BThreadDaoId.eq(null));
                bThread_UserThreadLinksQuery = queryBuilder.build();
            }
        }
        Query<UserThreadLink> query = bThread_UserThreadLinksQuery.forCurrentThread();
        query.setParameter(0, BThreadDaoId);
        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.getBThreadDao().getAllColumns());
            builder.append(" FROM USER_THREAD_LINK T");
            builder.append(" LEFT JOIN BUSER T0 ON T.'BUSER_DAO_ID'=T0.'_id'");
            builder.append(" LEFT JOIN BTHREAD T1 ON T.'BTHREAD_DAO_ID'=T1.'_id'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected UserThreadLink loadCurrentDeep(Cursor cursor, boolean lock) {
        UserThreadLink entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

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

        BThread BThread = loadCurrentOther(daoSession.getBThreadDao(), cursor, offset);
        entity.setBThread(BThread);

        return entity;    
    }

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