package com.jishuli.Moco.BusinessLogicLayer.dao;

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

import com.jishuli.Moco.PersistenceLayer.County;

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

    public static final String TABLENAME = "COUNTY";

    /**
     * Properties of entity County.<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 Identifier = new Property(1, Long.class, "identifier", false, "IDENTIFIER");
        public final static Property Name = new Property(2, String.class, "name", false, "NAME");
        public final static Property CityID = new Property(3, long.class, "cityID", false, "CITY_ID");
    };

    private Query<County> city_CountiesQuery;

    public CountyDao(DaoConfig config) {
        super(config);
    }
    
    public CountyDao(DaoConfig config, DaoSession daoSession) {
        super(config, 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 + "\"COUNTY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"IDENTIFIER\" INTEGER," + // 1: identifier
                "\"NAME\" TEXT," + // 2: name
                "\"CITY_ID\" INTEGER NOT NULL );"); // 3: cityID
    }

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

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, County entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long identifier = entity.getIdentifier();
        if (identifier != null) {
            stmt.bindLong(2, identifier);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
        stmt.bindLong(4, entity.getCityID());
    }

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

    /** @inheritdoc */
    @Override
    public County readEntity(Cursor cursor, int offset) {
        County entity = new County( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // identifier
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // name
            cursor.getLong(offset + 3) // cityID
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, County entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setIdentifier(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setCityID(cursor.getLong(offset + 3));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(County entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(County entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "counties" to-many relationship of City. */
    public List<County> _queryCity_Counties(long cityID) {
        synchronized (this) {
            if (city_CountiesQuery == null) {
                QueryBuilder<County> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CityID.eq(null));
                city_CountiesQuery = queryBuilder.build();
            }
        }
        Query<County> query = city_CountiesQuery.forCurrentThread();
        query.setParameter(0, cityID);
        return query.list();
    }

}
