package com.jiang.android.rxjavaapp.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import java.util.ArrayList;
import java.util.List;

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


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

    public static final String TABLENAME = "ALLOPERATORS";

    /**
     * Properties of entity alloperators.<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 Name = new Property(1, String.class, "name", false, "NAME");
        public final static Property Thread = new Property(2, String.class, "thread", false, "THREAD");
        public final static Property Desc = new Property(3, String.class, "desc", false, "DESC");
        public final static Property Img = new Property(4, String.class, "img", false, "IMG");
        public final static Property Url = new Property(5, String.class, "url", false, "URL");
        public final static Property Operators_id = new Property(6, Long.class, "operators_id", false, "OPERATORS_ID");
        public final static Property Outer_id = new Property(7, Long.class, "outer_id", false, "OUTER_ID");
    };

    private DaoSession daoSession;

    private Query<alloperators> operators_AlloperatorsListQuery;

    public alloperatorsDao(DaoConfig config) {
        super(config);
    }
    
    public alloperatorsDao(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 + "\"ALLOPERATORS\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"NAME\" TEXT NOT NULL ," + // 1: name
                "\"THREAD\" TEXT NOT NULL ," + // 2: thread
                "\"DESC\" TEXT NOT NULL ," + // 3: desc
                "\"IMG\" TEXT NOT NULL ," + // 4: img
                "\"URL\" TEXT NOT NULL ," + // 5: url
                "\"OPERATORS_ID\" INTEGER," + // 6: operators_id
                "\"OUTER_ID\" INTEGER);"); // 7: outer_id
    }

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

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, alloperators entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getName());
        stmt.bindString(3, entity.getThread());
        stmt.bindString(4, entity.getDesc());
        stmt.bindString(5, entity.getImg());
        stmt.bindString(6, entity.getUrl());
 
        Long operators_id = entity.getOperators_id();
        if (operators_id != null) {
            stmt.bindLong(7, operators_id);
        }
    }

    @Override
    protected void attachEntity(alloperators 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 alloperators readEntity(Cursor cursor, int offset) {
        alloperators entity = new alloperators( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getString(offset + 1), // name
            cursor.getString(offset + 2), // thread
            cursor.getString(offset + 3), // desc
            cursor.getString(offset + 4), // img
            cursor.getString(offset + 5), // url
            cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6) // operators_id
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, alloperators entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setName(cursor.getString(offset + 1));
        entity.setThread(cursor.getString(offset + 2));
        entity.setDesc(cursor.getString(offset + 3));
        entity.setImg(cursor.getString(offset + 4));
        entity.setUrl(cursor.getString(offset + 5));
        entity.setOperators_id(cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(alloperators entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(alloperators entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "alloperatorsList" to-many relationship of operators. */
    public List<alloperators> _queryOperators_AlloperatorsList(Long outer_id) {
        synchronized (this) {
            if (operators_AlloperatorsListQuery == null) {
                QueryBuilder<alloperators> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Outer_id.eq(null));
                operators_AlloperatorsListQuery = queryBuilder.build();
            }
        }
        Query<alloperators> query = operators_AlloperatorsListQuery.forCurrentThread();
        query.setParameter(0, outer_id);
        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.getOperatorsDao().getAllColumns());
            builder.append(" FROM ALLOPERATORS T");
            builder.append(" LEFT JOIN OPERATORS T0 ON T.\"OPERATORS_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected alloperators loadCurrentDeep(Cursor cursor, boolean lock) {
        alloperators entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        operators operators = loadCurrentOther(daoSession.getOperatorsDao(), cursor, offset);
        entity.setOperators(operators);

        return entity;    
    }

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