package net.yasite.dao;
import java.util.List;
import net.yasite.entity.UserEntity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import com.google.gson.Gson;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;
public class UserDao extends AbstractDao< UserEntity , Long> {
public static final String TABLENAME = "USER";
public UserDao(DaoConfig config) {
super(config);
}
public UserDao(DaoConfig config, DaoSession daoSession) {
super(config, daoSession);
}
public static class Properties {
public final static Property Id = new Property(0,long.class,"id",true,"ID");
public final static Property Username = new Property(1,String.class,"username",false,"USERNAME");
public final static Property Logined_at = new Property(2,String.class,"logined_at",false,"LOGINED_AT");
public final static Property Avatar = new Property(3,String.class,"avatar",false,"AVATAR");
public final static Property Weibo_binded = new Property(4,String.class,"weibo_binded",false,"WEIBO_BINDED");
public final static Property Qq_binded = new Property(5,String.class,"qq_binded",false,"QQ_BINDED");
public final static Property Logined = new Property(6,String.class,"logined",false,"LOGINED");
}
public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
String constraint = ifNotExists ? "IF NOT EXISTS " : "";
StringBuffer sql = new StringBuffer(0);
sql.append("CREATE TABLE " + constraint + "'" + TABLENAME + "' ( ")
.append("ID INTEGER AUTO_INCREMENT PRIMARY KEY,")
.append("USERNAME TEXT ,")
.append("LOGINED_AT TEXT ,")
.append("AVATAR TEXT ,")
.append("WEIBO_BINDED TEXT ,")
.append("QQ_BINDED TEXT ,")
.append("LOGINED TEXT );");
db.execSQL(sql.toString());
}
public static void dropTable(SQLiteDatabase db, boolean ifExists) {
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'" + TABLENAME + "'";
db.execSQL(sql);
}
@Override
protected void bindValues(SQLiteStatement stmt, UserEntity entity) {
stmt.bindLong(1, entity.getId());
if(entity.getUsername() != null){
stmt.bindString(2, entity.getUsername());
}
if(entity.getLogined_at() != null){
stmt.bindString(3, entity.getLogined_at());
}
if(entity.getAvatar() != null){
stmt.bindString(4, entity.getAvatar());
}else{
stmt.bindString(4, "");
}
if(entity.getLogined() != null){
stmt.bindString(7, entity.getLogined());
}
}
@Override
protected Long getKey(UserEntity entity) {
if (entity != null) {
return entity.getId();
} else {
return null;
}
}
@Override
protected boolean isEntityUpdateable() {
return true;
}
@Override
protected Long readKey(Cursor cursor, int offset) {
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
}
@Override
protected Long updateKeyAfterInsert(UserEntity entity, long rowId) {
entity.setId(rowId);
return rowId;
}
private UserEntity setEntity(Cursor cursor, UserEntity entity, int offset){
Gson gson = new Gson();
entity.setId(cursor.getLong(offset + 0));
entity.setUsername(cursor.getString(offset + 1));
entity.setLogined_at(cursor.getString(offset + 2));
entity.setAvatar(cursor.getString(offset + 3));
entity.setLogined(cursor.getString(offset + 6));
return entity;
}
@Override
protected UserEntity readEntity(Cursor cursor, int offset) {
UserEntity entity = new UserEntity();
return setEntity(cursor,entity,offset);
}
@Override
protected void readEntity(Cursor cursor, UserEntity entity, int offset) {
entity = setEntity(cursor,entity,offset);
}
public UserEntity getloggedUser(){
List<UserEntity> list = queryBuilder().list();
if(list != null && list.size() > 0){
return list.get(0);
}else{
return null;
}
}
}