日期:2014-05-16  浏览次数:20661 次

android数据库操作(二)
package cn.madfinger.android.core;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;
import cn.madfinger.android.core.util.DateUtils;
import cn.madfinger.android.core.util.PageList;
import cn.madfinger.android.core.util.StrUtils;
import cn.madfinger.android.core.util.impl.PageListImpl;

/**
 * 数据库操作基类
 * 
 * @author wiley
 *
 * @param <T>
 */

public abstract class AbstractBaseDao<T extends AbstractBaseModel> {
	protected static String TAG = AbstractBaseDao.class.getSimpleName();
	private static Map<String, Integer> TYPES = new HashMap<String, Integer>();
	public static final int TYPE_STRING=1;
	public static final int TYPE_INTEGER=2;
	public static final int TYPE_LONG=3;
	public static final int TYPE_SHORT=4;
	public static final int TYPE_FLOAT=5;
	public static final int TYPE_DOUBLE=6;
	
	static {
		TYPES.put("date", TYPE_STRING);
		TYPES.put("string", TYPE_STRING);
		TYPES.put("integer", TYPE_INTEGER);
		TYPES.put("int", TYPE_INTEGER);
		TYPES.put("long", TYPE_LONG);
		TYPES.put("short", TYPE_SHORT);
		TYPES.put("float", TYPE_FLOAT);
		TYPES.put("double", TYPE_DOUBLE);
	}
	//protected SQLiteDatabase dbHandler;
	protected SqliteHelper sqliteHelper;

	public AbstractBaseDao() {

	}

	public void setSqliteHelper(SqliteHelper sqliteHelper) {
		this.sqliteHelper = sqliteHelper;
	}
	
	
	public long getCount(){
		return getCount(null, null, null, null);
	}
	
	public long getCount(String selection, String[] selectionArgs){
		return getCount(selection, selectionArgs, null, null);
	}
	
	public long getCount(String selection, String[] selectionArgs,String groupBy, String having){
		String sqlString="SELECT COUNT(*) AS NUM FROM "+this.getEntityClass().getSimpleName();
		if(!StrUtils.isEmpty(selection)){
			sqlString+=" "+selection;
		}
		if(!StrUtils.isEmpty(groupBy)){
			sqlString+=" "+groupBy;
			if(!StrUtils.isEmpty(having)){sqlString+=having;}
		}
		Cursor cursor=this.execSql(sqlString,selectionArgs);
		cursor.moveToFirst();
		return cursor.getLong(0);
	}

	public PageList<Map<String, Object>> queryPageList(String[] columns,int pageNum, int maxPerPage){
		return queryPageList(columns, null, null, null, null, null, pageNum, maxPerPage);
	}
	
	public PageList<Map<String, Object>> queryPageList(String[] columns,String selection, String[] selectionArgs,int pageNum, int maxPerPage){
		return queryPageList(columns, selection, selectionArgs, null, null, null, pageNum, maxPerPage);
	}
	
	public PageList<Map<String, Object>> queryPageList(String[] columns,String selection, String[] selectionArgs,String orderBy,int pageNum, int maxPerPage){
		return queryPageList(columns, selection, selectionArgs, null, null, orderBy, pageNum, maxPerPage);
	}
	
	public PageList<Map<String, Object>> queryPageList(String[] columns,String selection, String[] selectionArgs,String groupBy, String having, String orderBy,int pageNum, int maxPerPage){
		long recordCount=getCount(selection,selectionArgs,groupBy,having);
		PageListImpl<Map<String, Object>> page = new PageListImpl<Map<String, Object>>();
		page.calculatePageInfo(pageNum, maxPerPage, recordCount);
		Cursor cursor=sqliteHelper.getReaderHandler().query(this.getEntityClass().getSimpleName(), columns, selection, selectionArgs, groupBy, having, orderBy,maxPerPage+","+page.getStartRecordNum());
		List<Map<String, Object>> list=cursor2List(cursor,columns);
		page.setDataList(list);
		return page;
	}
	
	public PageList