Back to project page android-sqlite-helper.
The source code is released under:
Apache License
If you think the Android project android-sqlite-helper listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package net.ichigotake.sqlitehelper.dml; /* w ww.j av a2 s . co m*/ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.text.TextUtils; import net.ichigotake.sqlitehelper.schema.Table; import java.util.ArrayList; import java.util.List; public class Select { private final SQLiteDatabase database; private final Table table; private final Where where; private final List<String> orderBy; public Select(SQLiteDatabase database, Table from) { this.database = database; this.table = from; this.where = new Where(); this.orderBy = new ArrayList<>(); } public Select where(Where where) { if (!where.isEmpty()) { this.where.and(where); } return this; } public Select orderBy(Order field) { String seq = field.getSequence() == Order.Sequence.DESC ? "DESC" : "ASC"; orderBy.add(field.getFieldName() + " " + seq); return this; } public Cursor execute() { return database.rawQuery(buildQuery(), getArguments()); } /** visible for testing */ String buildQuery() { String query = "SELECT * FROM " + table.getTableName(); if (!where.isEmpty()) { query += " WHERE " + where.getQuery(); } if (!orderBy.isEmpty()) { query += " ORDER BY " + TextUtils.join(",", orderBy); } return query; } /** visible for testing */ String[] getArguments() { return where.getArguments(); } }