Android Open Source - SORM Query






From Project

Back to project page SORM.

License

The source code is released under:

MIT License

If you think the Android project SORM listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.annotation.core;
//ww w  .ja v  a  2 s . co m
import java.util.ArrayList;
import java.util.List;

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

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

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

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

  public <T extends Model> 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 = null;
    try {
      db = new DBHelper(context).getReadableDatabase();
      // create table if not exist
      DBUtils.createTable(context, _entity);

      Cursor cursor = db.rawQuery(sql, null);
      while (cursor.moveToNext()) {
        T entity = DBUtils.cursor2Entity(cursor, _entity);
        if (entity != null)
          result.add(entity);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (db != null)
        db.close();
    }
    return result;
  }

  public void excuteAsync(final Context context, final QueryCallback callback) {
    new Thread(new Runnable() {

      @Override
      public void run() {
        if (callback != null) {
          callback.onFinish(excute(context));
        }
      }
    }).start();
  }
}




Java Source Code List

.Creator.java
com.annotation.Column.java
com.annotation.Ignore.java
com.annotation.Index.java
com.annotation.NoNull.java
com.annotation.Table.java
com.annotation.Unique.java
com.annotation.core.Deletor.java
com.annotation.core.Droper.java
com.annotation.core.Indexer.java
com.annotation.core.Inserter.java
com.annotation.core.Model.java
com.annotation.core.Query.java
com.annotation.core.Selector.java
com.annotation.core.Updater.java
com.annotation.entity.ColumnInfo.java
com.annotation.entity.ORMcallback.java
com.annotation.entity.QueryCallback.java
com.annotation.entity.Sqlable.java
com.annotation.entity.Wherable.java
com.annotation.entity.WhereImpl.java
com.annotation.utils.DBHelper.java
com.annotation.utils.DBUtils.java
com.annotation.utils.NameBuilder.java
com.annotation.utils.ReflectionUtils.java
com.annotation.utils.SqlUtils.java
com.annotation.utils._.java