package com.jingpu.android.apersistance.greendao.model;

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 com.jingpu.android.apersistance.greendao.model.CItem;

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

    public static final String TABLENAME = "CITEM";

    /**
     * Properties of entity CItem.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property CId = new Property(0, Long.class, "cId", false, "C_ID");
        public final static Property Id = new Property(1, Long.class, "id", true, "_id");
        public final static Property IImId = new Property(2, int.class, "iImId", false, "I_IM_ID");
        public final static Property IName = new Property(3, String.class, "iName", false, "I_NAME");
        public final static Property IPrice = new Property(4, float.class, "iPrice", false, "I_PRICE");
        public final static Property IData = new Property(5, String.class, "iData", false, "I_DATA");
    };

    private DaoSession daoSession;


    public CItemDao(DaoConfig config) {
        super(config);
    }
    
    public CItemDao(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 + "\"CITEM\" (" + //
                "\"C_ID\" INTEGER," + // 0: cId
                "\"_id\" INTEGER PRIMARY KEY ," + // 1: id
                "\"I_IM_ID\" INTEGER NOT NULL ," + // 2: iImId
                "\"I_NAME\" TEXT NOT NULL ," + // 3: iName
                "\"I_PRICE\" REAL NOT NULL ," + // 4: iPrice
                "\"I_DATA\" TEXT NOT NULL );"); // 5: iData
    }

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

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, CItem entity) {
        stmt.clearBindings();
 
        Long cId = entity.getCId();
        if (cId != null) {
            stmt.bindLong(1, cId);
        }
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(2, id);
        }
        stmt.bindLong(3, entity.getIImId());
        stmt.bindString(4, entity.getIName());
        stmt.bindDouble(5, entity.getIPrice());
        stmt.bindString(6, entity.getIData());
    }

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

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

    /** @inheritdoc */
    @Override
    public CItem readEntity(Cursor cursor, int offset) {
        CItem entity = new CItem( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // cId
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // id
            cursor.getInt(offset + 2), // iImId
            cursor.getString(offset + 3), // iName
            cursor.getFloat(offset + 4), // iPrice
            cursor.getString(offset + 5) // iData
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, CItem entity, int offset) {
        entity.setCId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setIImId(cursor.getInt(offset + 2));
        entity.setIName(cursor.getString(offset + 3));
        entity.setIPrice(cursor.getFloat(offset + 4));
        entity.setIData(cursor.getString(offset + 5));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(CItem entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(CItem entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    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.getCategoryDao().getAllColumns());
            builder.append(" FROM CITEM T");
            builder.append(" LEFT JOIN CATEGORY T0 ON T.\"C_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected CItem loadCurrentDeep(Cursor cursor, boolean lock) {
        CItem entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Category category = loadCurrentOther(daoSession.getCategoryDao(), cursor, offset);
        entity.setCategory(category);

        return entity;    
    }

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