package com.annotation.core;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.annotation.utils.DBUtils;
import com.annotation.utils.DBHelper;

public class Query {
	private Class<?> _entity;
	private Selector selector;

	public Query(Selector selector) {
		this.selector = selector;
		_entity = selector.getEntity();
	}

	public <T> List<T> excute(Context context) {
		List<T> result = new ArrayList<T>();
		if (selector == null)
			throw new NullPointerException("selector Can't be null");
		String sql = selector.build();
		// 执行查询，获得cursor
		SQLiteDatabase db =  new DBHelper(context).getReadableDatabase();
		//create table if not exist
		DBUtils.createTable(db, _entity);
		
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			T entity = DBUtils.cursor2Entity(cursor, _entity);
			if (entity != null)
				result.add(entity);
		}

		return result;
	}

}

@@ -712,16 +712,39 @@
 se db = 
+null;%0D%0A%09%09try %7B%0D%0A%09%09%09db =
  new DBH
@@ -784,18 +784,20 @@
 e();%0D%0A%09%09
+%09
 //
+ 
 create t
@@ -815,16 +815,17 @@
  exist%0D%0A
+%09
 %09%09DBUtil
@@ -857,12 +857,11 @@
 );%0D%0A
-%09%09
 %0D%0A
+%09
 %09%09Cu
@@ -901,16 +901,17 @@
 ll);%0D%0A%09%09
+%09
 while (c
@@ -933,16 +933,17 @@
 t()) %7B%0D%0A
+%09
 %09%09%09T ent
@@ -992,16 +992,17 @@
 y);%0D%0A%09%09%09
+%09
 if (enti
@@ -1018,16 +1018,17 @@
 l)%0D%0A%09%09%09%09
+%09
 result.a
@@ -1044,15 +1044,119 @@
 );%0D%0A
+%09%09%09%7D%0D%0A%09%09%7D catch (Exception e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7Dfinally%7B%0D%0A%09%09%09if(db!= null)%0D%0A%09%09%09%09db.close();%0D%0A
 %09%09%7D%0D%0A
-%0D%0A
 %09%09re
@@ -1169,17 +1169,15 @@
 esult;%0D%0A
+
 %09%7D%0D%0A
-%0D%0A
 %7D%0D%0A

fix #11