Android Open Source - SORM Selector






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;
//  w  w w.j av a  2  s .  com
import java.lang.reflect.Field;

import com.annotation.entity.WhereImpl;
import com.annotation.utils.ReflectionUtils;

public class Selector extends WhereImpl<Selector>{
  Class<?> _entity;
  String _table;
  int __limit, __offset;
  boolean _distinct, _all;
  StringBuffer  _groupBy, _orderBy, _resultColumn;

  public Selector(String... resultColumn) {
    super();
    _resultColumn = new StringBuffer();
    _groupBy = new StringBuffer();
    _orderBy = new StringBuffer();
    __limit = -1;
    __offset = -1;
    all();
    for (String c : resultColumn) {
      _resultColumn.append(c).append(",");
    }
    if (_resultColumn.length() > 0) {
      _resultColumn.deleteCharAt(_resultColumn.length() - 1);
    }
  }

  public Selector from(Class<?> cls) {
    _table = ReflectionUtils.getTableName(cls);
    _entity = cls;
    return this;
  }

  public Selector distinct() {
    _distinct = true;
    _all = false;
    return this;
  }

  public Selector all() {
    _distinct = false;
    _all = true;
    return this;
  }

  public Selector groupBy(String... columns) {
    for (String c : columns) {
      _groupBy.append(c).append(",");
    }
    if (_groupBy.length() > 0) {
      _groupBy.deleteCharAt(_groupBy.length() - 1).append(" ");
    }
    return this;
  }

  public Selector orderBy(String... columns) {
    for (String c : columns) {
      _orderBy.append(c).append(",");
    }
    if (_orderBy.length() > 0) {
      _orderBy.deleteCharAt(_orderBy.length() - 1).append(" ");
    }
    return this;
  }

  public Selector limit(int _limit) {
    __limit = _limit;
    return this;
  }

  public Selector offset(int _offset) {
    __offset = _offset;
    return this;
  }

  @Override
  public String build() {
    StringBuffer builder = new StringBuffer();
    builder.append("Select").append(" ");
    if (_distinct) {
      builder.append("Distinct").append(" ");
    } else {
      builder.append("All").append(" ");
    }
    
    if (_resultColumn.length() == 0) {
      for (Field f : ReflectionUtils.getColumnFields(_entity)) {
        _resultColumn.append(f.getName()).append(",");
      }
      if (_resultColumn.length() > 0) {
        _resultColumn.deleteCharAt(_resultColumn.length() - 1);
      }
    }
    builder.append(_resultColumn).append(" ");

    builder.append("From").append(" ").append(_table).append(" ");
    if(hasWhere())
      builder.append("Where").append(" ").append(getWhere()).append(" ");

    if (_groupBy.length() > 0)
      builder.append("Group By").append(" ").append(_groupBy).append(" ");

    if (_orderBy.length() > 0)
      builder.append("Order By").append(" ").append(_orderBy).append(" ");

    if (__limit > 0)
      builder.append("Limit").append(" ").append(__limit).append(" ");

    if (__offset > 0)
      builder.append("Offset").append(" ").append(__offset).append(" ");
    return builder.toString();
  }

  public Class<?> getEntity() {
    return _entity;
  }

}




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