From 5c15ba41dc0fb27847fa66f74bbf3785f3e3dfa0 Mon Sep 17 00:00:00 2001 From: Bitter <534429149@qq.com> Date: Tue, 20 Nov 2018 16:58:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A8=A1=E7=B3=8A=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E4=B8=8B=E8=BD=BD=E4=BB=BB=E5=8A=A1=E7=9A=84=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/arialyy/aria/orm/DbEntity.java | 13 ++++++++++++ .../com/arialyy/aria/orm/DelegateFind.java | 20 +++++++++++++++++++ .../com/arialyy/aria/orm/DelegateWrapper.java | 7 +++++++ 3 files changed, 40 insertions(+) diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java index f6856acc..15b71db8 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java @@ -102,6 +102,19 @@ public abstract class DbEntity { return util.findData(clazz, expression); } + /** + * 模糊查询一组数据 + * + * DownloadEntity.findDatas(DownloadEntity.class, "downloadUrl like http://"); + * + * + * @return 没有数据返回null + */ + public static List findDataByFuzzy(Class clazz, String conditions) { + DelegateWrapper util = DelegateWrapper.getInstance(); + return util.findDataByFuzzy(clazz, conditions); + } + /** * 查询一行数据 * diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java index e292a67f..2eb8fff0 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java @@ -355,6 +355,26 @@ class DelegateFind extends AbsDelegate { return data; } + /** + * 模糊查寻数据 + */ + List findDataByFuzzy(SQLiteDatabase db, Class clazz, String conditions) { + db = checkDb(db); + if(TextUtils.isEmpty(conditions)){ + throw new IllegalArgumentException("sql语句表达式不能为null或\"\""); + } + if(!conditions.toUpperCase().contains("LIKE")){ + throw new IllegalArgumentException("sql语句表达式未包含LIEK"); + } + String sql = + "SELECT rowid, * FROM " + CommonUtil.getClassName(clazz) + " WHERE "+conditions; + print(FIND_DATA, sql); + Cursor cursor = db.rawQuery(sql, null); + List data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; + closeCursor(cursor); + close(db); + return data; + } /** * 查找表的所有数据 */ diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java b/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java index e87c5b9d..faa8fc25 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java @@ -118,6 +118,13 @@ public class DelegateWrapper { return mDManager.getDelegate(DelegateFind.class).findData(mDb, clazz, expression); } + /** + * 模糊查寻数据 + */ + List findDataByFuzzy(Class clazz, String conditions) { + return mDManager.getDelegate(DelegateFind.class).findDataByFuzzy(mDb, clazz, conditions); + } + /** * 通过rowId判断数据是否存在 */