Android Open Source - spades Query






From Project

Back to project page spades.

License

The source code is released under:

Apache License

If you think the Android project spades 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

/**
 * Copyright (C) 2013 Pau Picas Sans <pau.picas@gmail.com>
 *//from   w  w w  .j  a v  a  2 s .c o m
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package cat.picas.spades.query;

import static android.text.TextUtils.join;

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

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cat.picas.spades.Column;
import cat.picas.spades.CursorInfo;
import cat.picas.spades.CursorInfoBuilder;
import cat.picas.spades.SqlHelper;
import cat.picas.spades.Table;

public class Query {

  private NameMapper mMapper = new NameMapper();
  private ColumnSelector mSelector = new ColumnSelector();
  private ArrayList<String> mFromClauses = new ArrayList<String>();
  private ArrayList<String> mWhereClauses = new ArrayList<String>();
  private ArrayList<String> mWhereArgs = new ArrayList<String>();
  private ArrayList<String> mOrderBy = new ArrayList<String>();
  private ArrayList<String> mGroupByClauses = new ArrayList<String>();
  private String mLimit;
  private boolean mDistinct;

  public Query(Table fromTable) {
    mSelector.setAutoEntitiesId(true);
    mSelector.setAutoRowsId(true);
    from(fromTable);
  }

  public Query select(Table table) {
    for (Column column : table.getColumns()) {
      mSelector.selectColumn(column);
    }
    return this;
  }

  public Query select(Column... cols) {
    for (Column column : cols) {
      mSelector.selectColumn(column);
    }
    return this;
  }

  public Query select(String expr, Column... cols) {
    mSelector.selectColumn(expr, cols);
    return this;
  }

  public Query from(Table table) {
    if (mSelector.hasTables()) {
      throw new IllegalArgumentException("From clause already defined");
    }
    mSelector.addTable(table);
    mFromClauses.add(table.name + " AS " + mMapper.alias(table));
    return this;
  }

  public Query leftJoin(Table table, String onExpr, Column... onExprCols) {
    customJoin("LEFT", table, onExpr, onExprCols);
    return this;
  }

  public Query innerJoin(Table table, String onExpr, Column... onExprCols) {
    customJoin("INNER", table, onExpr, onExprCols);
    return this;
  }

  public Query where(Column col, String expr) {
    mWhereClauses.add(SqlHelper.expr("%s " + expr, col));
    return this;
  }

  public Query where(String expr, Column... cols) {
    mWhereClauses.add(SqlHelper.expr(expr, cols));
    return this;
  }

  public Query params(String... params) {
    mWhereArgs.addAll(Arrays.asList(params));
    return this;
  }

  public Query groupBy(Column... cols) {
    for (Column col : cols) {
      mGroupByClauses.add(mMapper.ref(col));
    }
    return this;
  }

  public Query groupBy(String expr, Column... cols) {
    mGroupByClauses.add(SqlHelper.expr(expr, cols));
    return this;
  }

  public Query orderBy(Column col, boolean asc) {
    mOrderBy.add(mMapper.ref(col) + " " + (asc ? "ASC" : "DESC"));
    return this;
  }

  public Query limit(int limit) {
    return limit(limit, 0);
  }

  public Query limit(int limit, int offset) {
    if (limit == 0) {
      mLimit = null;
    } else {
      mLimit = (offset > 0 ? limit + " OFFSET " + offset : String.valueOf(limit));
    }
    return this;
  }

  public Query distinct() {
    mDistinct = true;
    return this;
  }

  public Query setDistinct(boolean state) {
    mDistinct = state;
    return this;
  }

  public Query setAutoEntitiesId(boolean state) {
    mSelector.setAutoEntitiesId(state);
    return this;
  }

  public Query setAutoRowsId(boolean state) {
    mSelector.setAutoRowsId(state);
    return this;
  }

  public boolean isDistinct() {
    return mDistinct;
  }

  public boolean hasAutoEntitiesId() {
    return mSelector.isAutoEntitiesId();
  }

  public boolean hasAutoRowsId() {
    return mSelector.isAutoRowsId();
  }

  public boolean hasTable(Table table) {
    return mSelector.hasTable(table);
  }

  public CursorInfo getCursorInfo() {
    CursorInfoBuilder builder = new CursorInfoBuilder();
    for (SelectedColumn selected : mSelector.getSelectedColumns()) {
      if (!selected.isCustom()) {
        builder.add(selected.getColumn());
      } else {
        builder.addOffset();
      }
    }
    return builder.build();
  }

  public Cursor execute(SQLiteDatabase db) {
    checkFromClause();

    String tables= join(" ", mFromClauses);
    String[] columns = prepareColumnsStrings(false);
    String selection = mWhereClauses.isEmpty() ? null : join(" AND ", mWhereClauses);
    String[] selectionArgs = mWhereArgs.isEmpty() ? null
        : (String[]) mWhereArgs.toArray(new String[mWhereArgs.size()]);
    String groupBy = mGroupByClauses.isEmpty() ? null : join(",", mGroupByClauses);
    String orderBy = mOrderBy.isEmpty() ? null : join(",", mOrderBy);

    return db.query(mDistinct, tables, columns, selection, selectionArgs, groupBy, null, orderBy, mLimit);
  }

  public int count(SQLiteDatabase db) {
    checkFromClause();

    String tables= join(" ", mFromClauses);
    String[] columns = prepareColumnsStrings(true);
    String selection = mWhereClauses.isEmpty() ? null : join(" AND ", mWhereClauses);
    String[] selectionArgs = mWhereArgs.isEmpty() ? null
        : (String[]) mWhereArgs.toArray(new String[mWhereArgs.size()]);
    String groupBy = mGroupByClauses.isEmpty() ? null : join(",", mGroupByClauses);

    int res = 0;
    Cursor cursor = db.query(tables, columns, selection, selectionArgs, groupBy, null, null);
    if (cursor.moveToFirst()) {
      res = cursor.getInt(0);
    }
    cursor.close();

    return res;
  }

  protected void customJoin(String type, Table table, String onExpr, Column... onExprCols) {
    checkFromClause();
    mSelector.addTable(table);
    mFromClauses.add(type + " JOIN " + table.name + " AS " + mMapper.alias(table) + " ON "
        + SqlHelper.expr(onExpr, onExprCols));
  }

  protected void checkFromClause() {
    if (!mSelector.hasTables()) {
      throw new IllegalArgumentException("Missing from clause");
    }
  }

  protected String[] prepareColumnsStrings(boolean forCount) {
    List<SelectedColumn> selectedCols;
    if (forCount) {
      selectedCols = mSelector.getSelectedColumnsForCount();
    } else {
      selectedCols = mSelector.getSelectedColumns();
    }
    int i = 0;
    String[] columns = new String[selectedCols.size()];
    for (SelectedColumn selected : selectedCols) {
      columns[i++] = selected.format();
    }
    return columns;
  }

}




Java Source Code List

cat.picas.spades.AbstractEntity.java
cat.picas.spades.AutoEntityMapper.java
cat.picas.spades.ColumnBuilder.java
cat.picas.spades.Column.java
cat.picas.spades.CursorInfoBuilder.java
cat.picas.spades.CursorInfo.java
cat.picas.spades.Dao.java
cat.picas.spades.EntityMapper.java
cat.picas.spades.Entity.java
cat.picas.spades.RelatedChild.java
cat.picas.spades.RelatedList.java
cat.picas.spades.RelatedParent.java
cat.picas.spades.SqlHelper.java
cat.picas.spades.Table.java
cat.picas.spades.Tables.java
cat.picas.spades.fetch.ArrayListFetchStrategy.java
cat.picas.spades.fetch.FetchStrategy.java
cat.picas.spades.fetch.HashMapFetchStrategy.java
cat.picas.spades.map.BooleanMapper.java
cat.picas.spades.map.DateMapper.java
cat.picas.spades.map.DoubleMapper.java
cat.picas.spades.map.EnumMapper.java
cat.picas.spades.map.IntegerMapper.java
cat.picas.spades.map.LongMapper.java
cat.picas.spades.map.MappedFieldFactory.java
cat.picas.spades.map.MappedField.java
cat.picas.spades.map.RelatedParentMapper.java
cat.picas.spades.map.StringMapper.java
cat.picas.spades.map.ValueMapper.java
cat.picas.spades.query.ColumnSelector.java
cat.picas.spades.query.NameMapper.java
cat.picas.spades.query.Query.java
cat.picas.spades.query.SelectedColumn.java
cat.picas.spades.util.ReflectionUtils.java
cat.picas.spadessamples.AliasTableActivity.java
cat.picas.spadessamples.ArrayListActivity.java
cat.picas.spadessamples.CursorListActivity.java
cat.picas.spadessamples.MainActivity.java
cat.picas.spadessamples.PersonDetailActivity.java
cat.picas.spadessamples.adapter.CouplesCursorAdapter.java
cat.picas.spadessamples.adapter.PersonArrayAdapter.java
cat.picas.spadessamples.adapter.PersonCursorAdapter.java
cat.picas.spadessamples.model.ContactPointDao.java
cat.picas.spadessamples.model.ContactPoint.java
cat.picas.spadessamples.model.DatabaseHelper.java
cat.picas.spadessamples.model.FixturesHelper.java
cat.picas.spadessamples.model.PersonDao.java
cat.picas.spadessamples.model.Person.java
cat.picas.spadessamples.model.inheritance.HotelDao.java
cat.picas.spadessamples.model.inheritance.Hotel.java
cat.picas.spadessamples.model.inheritance.PlaceDao.java
cat.picas.spadessamples.model.inheritance.Place.java
cat.picas.spadessamples.model.inheritance.RestaurantDao.java
cat.picas.spadessamples.model.inheritance.Restaurant.java