package xw.cn.daoexample.db;

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

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import xw.cn.daoexample.bean.RepositoryNote;

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

    public static final String TABLENAME = "REPOSITORY_NOTE";

    /**
     * Properties of entity RepositoryNote.<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 Username = new Property(2, String.class, "username", false, "USERNAME");
        public final static Property Repos = new Property(3, int.class, "repos", false, "REPOS");
    }


    public RepositoryNoteDao(DaoConfig config) {
        super(config);
    }
    
    public RepositoryNoteDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"REPOSITORY_NOTE\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ," + // 0: id
                "\"NAME\" TEXT UNIQUE ," + // 1: name
                "\"USERNAME\" TEXT," + // 2: username
                "\"REPOS\" INTEGER NOT NULL );"); // 3: repos
    }

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

    @Override
    protected final void bindValues(DatabaseStatement stmt, RepositoryNote entity) {
        stmt.clearBindings();
        stmt.bindLong(1, entity.getId());
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(2, name);
        }
 
        String username = entity.getUsername();
        if (username != null) {
            stmt.bindString(3, username);
        }
        stmt.bindLong(4, entity.getRepos());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, RepositoryNote entity) {
        stmt.clearBindings();
        stmt.bindLong(1, entity.getId());
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(2, name);
        }
 
        String username = entity.getUsername();
        if (username != null) {
            stmt.bindString(3, username);
        }
        stmt.bindLong(4, entity.getRepos());
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.getLong(offset + 0);
    }    

    @Override
    public RepositoryNote readEntity(Cursor cursor, int offset) {
        RepositoryNote entity = new RepositoryNote( //
            cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // name
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // username
            cursor.getInt(offset + 3) // repos
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, RepositoryNote entity, int offset) {
        entity.setId(cursor.getLong(offset + 0));
        entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setUsername(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setRepos(cursor.getInt(offset + 3));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(RepositoryNote entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(RepositoryNote entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(RepositoryNote entity) {
        throw new UnsupportedOperationException("Unsupported for entities with a non-null key");
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
}