「优化」
增加了SQLite查询接口没可以排序
This commit is contained in:
@@ -71,6 +71,9 @@ public class SQLBaseDao<T> implements SQLIDao<T> {
|
||||
|
||||
/**
|
||||
* 插入数据
|
||||
*
|
||||
* @param entity 要插入数据的数据对象爱你那个
|
||||
* @return 插入结果ID,如果发生错误则为-1
|
||||
*/
|
||||
@Override
|
||||
public long insert(T entity) {
|
||||
@@ -89,9 +92,13 @@ public class SQLBaseDao<T> implements SQLIDao<T> {
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
*
|
||||
* @param where 要删除的对象,对象的字段如果赋值,则代表查询条件
|
||||
* @return 受影响行数
|
||||
*/
|
||||
@Override
|
||||
public int delete(T where) {
|
||||
// 拼接查询条件
|
||||
Condition condition = new Condition(getContentValuesForQuery(where));
|
||||
int result;
|
||||
if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) {
|
||||
@@ -109,6 +116,10 @@ public class SQLBaseDao<T> implements SQLIDao<T> {
|
||||
|
||||
/**
|
||||
* 更新数据
|
||||
*
|
||||
* @param where 要更新的对象,对象的字段如果赋值,则代表查询条件
|
||||
* @param newEntity 新的数据内容
|
||||
* @return 受影响行数
|
||||
*/
|
||||
@Override
|
||||
public int update(T where, T newEntity) {
|
||||
@@ -151,6 +162,20 @@ public class SQLBaseDao<T> implements SQLIDao<T> {
|
||||
return query(where, orderBy, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param where 根据条件 [where] 进行数据查询,如果[where]==只初始化不赋值 则代表查询所有数据
|
||||
* @param orderBy 排序字段
|
||||
* @param isOrderDESC 是否倒序 true-倒序,false-正序
|
||||
* @return 结果集合
|
||||
*/
|
||||
@Override
|
||||
public List<T> query(T where, String orderBy, boolean isOrderDESC) {
|
||||
if (isOrderDESC) {
|
||||
orderBy = orderBy + " DESC";
|
||||
}
|
||||
return query(where, orderBy, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询数据
|
||||
*
|
||||
@@ -356,7 +381,7 @@ public class SQLBaseDao<T> implements SQLIDao<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询使用的ContentValues
|
||||
* 获取查询使用的 条件值对象
|
||||
*/
|
||||
private ContentValues getContentValuesForQuery(T entity) {
|
||||
ContentValues contentValues = new ContentValues();
|
||||
@@ -365,7 +390,8 @@ public class SQLBaseDao<T> implements SQLIDao<T> {
|
||||
Set<Map.Entry<String, Field>> fieldIterator = cacheField.entrySet();
|
||||
for (Map.Entry<String, Field> stringFieldEntry : fieldIterator) {
|
||||
if (stringFieldEntry.getValue().get(entity) != null) {
|
||||
contentValues.put(stringFieldEntry.getKey(), stringFieldEntry.getValue().get(entity).toString());
|
||||
contentValues.put(stringFieldEntry.getKey(),
|
||||
stringFieldEntry.getValue().get(entity).toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -377,15 +403,18 @@ public class SQLBaseDao<T> implements SQLIDao<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 条件拼接
|
||||
* 条件拼接对象
|
||||
*/
|
||||
static class Condition {
|
||||
/**
|
||||
* 条件拼接
|
||||
* 条件key拼接
|
||||
* _id=?&&name=?
|
||||
*/
|
||||
private String whereCause;
|
||||
|
||||
/**
|
||||
* 条件值,与 whereCause 顺序一致
|
||||
*/
|
||||
private String[] whereArgs;
|
||||
|
||||
//根据传入的contentValues转换成查询条件
|
||||
@@ -418,7 +447,7 @@ public class SQLBaseDao<T> implements SQLIDao<T> {
|
||||
}
|
||||
}
|
||||
//集合转成数组
|
||||
this.whereArgs = (String[]) argList.toArray(new String[argList.size()]);
|
||||
this.whereArgs = argList.toArray(new String[argList.size()]);
|
||||
this.whereCause = whereCaseSb.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public interface SQLIDao<T> {
|
||||
long insert(T entity);
|
||||
|
||||
/**
|
||||
* 根据条件 [where] 进行数据删除
|
||||
* 根据条件 [where] 进行数据删除,如果[where]==只初始化不赋值 则代表删除所有数据
|
||||
*/
|
||||
int delete(T where);
|
||||
|
||||
@@ -31,12 +31,23 @@ public interface SQLIDao<T> {
|
||||
int update(T where, T newEntity);
|
||||
|
||||
/**
|
||||
* 根据条件 [where] 进行数据查询,如果[where]==只初始化不赋值 则代表查询所有数据
|
||||
* @param where 根据条件 [where] 进行数据查询,如果[where]==只初始化不赋值 则代表查询所有数据
|
||||
* @return 结果集合
|
||||
*/
|
||||
List<T> query(T where);
|
||||
|
||||
/**
|
||||
* 根据条件 [where] 进行数据查询,如果[where]==只初始化不赋值 则代表查询所有数据
|
||||
* @param where 根据条件 [where] 进行数据查询,如果[where]==只初始化不赋值 则代表查询所有数据
|
||||
* @param orderBy 排序字段
|
||||
* @return 结果集合
|
||||
*/
|
||||
List<T> query(T where, String orderBy);
|
||||
|
||||
/**
|
||||
* @param where 根据条件 [where] 进行数据查询,如果[where]==只初始化不赋值 则代表查询所有数据
|
||||
* @param orderBy 排序字段
|
||||
* @param isOrderDESC 是否倒序
|
||||
* @return 结果集合
|
||||
*/
|
||||
List<T> query(T where, String orderBy, boolean isOrderDESC);
|
||||
}
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
package com.mogo.utils.sqlite.proxy
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.util.Log
|
||||
import com.mogo.utils.sqlite.SQLIDao
|
||||
|
||||
/**
|
||||
* <p>静态代理数据库操作类,用来记录日志</P>
|
||||
* Created by donghongyu on 2019/9/6.
|
||||
*/
|
||||
class BaseDaoProxy<T>(var iBaseDao: SQLIDao<T>) : SQLIDao<T> by iBaseDao {
|
||||
override fun init(sqLiteDatabase: SQLiteDatabase, entityClass: Class<T>): Boolean {
|
||||
Log.i("数据库代理", "初始化数据库连接……")
|
||||
val isInitSuccess = iBaseDao.init(sqLiteDatabase, entityClass)
|
||||
if (isInitSuccess) {
|
||||
Log.i("数据库代理", "数据库连接成功……")
|
||||
} else {
|
||||
Log.e("数据库代理", "数据库连接失败……")
|
||||
}
|
||||
return isInitSuccess
|
||||
}
|
||||
|
||||
override fun insert(entity: T): Long {
|
||||
Log.i("数据库代理", "开始插入数据……")
|
||||
val result = iBaseDao.insert(entity)
|
||||
Log.i("数据库代理", "插入数据索引位置> $result <……")
|
||||
return result
|
||||
}
|
||||
|
||||
override fun delete(where: T): Int {
|
||||
Log.i("数据库代理", "开始删除数据……")
|
||||
val result = iBaseDao.delete(where)
|
||||
Log.i("数据库代理", "删除了> $result <条数据……")
|
||||
return result
|
||||
}
|
||||
|
||||
override fun update(where: T, newEntity: T): Int {
|
||||
Log.i("数据库代理", "开始更新数据……")
|
||||
val result = iBaseDao.update(where, newEntity)
|
||||
Log.i("数据库代理", "更新了> $result <条数据……")
|
||||
return result
|
||||
}
|
||||
|
||||
override fun query(where: T): MutableList<T> {
|
||||
Log.i("数据库代理", "开始查询数据……")
|
||||
val result = iBaseDao.query(where)
|
||||
Log.i("数据库代理", "查询到> ${result.size} <条数据……")
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.mogo.utils.sqlite.proxy
|
||||
|
||||
import com.mogo.utils.logger.Logger
|
||||
import com.mogo.utils.network.utils.GsonUtil
|
||||
import java.lang.reflect.InvocationHandler
|
||||
import java.lang.reflect.Method
|
||||
import java.lang.reflect.Proxy
|
||||
@@ -40,11 +41,11 @@ class BaseDaoProxyLog : InvocationHandler {
|
||||
override fun invoke(proxy: Any, method: Method, args: Array<Any>): Any? {
|
||||
var result: Any? = null
|
||||
//反射方法前调用
|
||||
Logger.i("数据库代理", "当前执行>>${method.name}")
|
||||
Logger.i("SQL数据库管理", "当前执行>>${method.name}>>")
|
||||
//反射执行方法 相当于调用target.sayHelllo;
|
||||
result = method.invoke(target, *args)
|
||||
//反射方法后调用.
|
||||
Logger.i("数据库代理", "执行结果>>$result")
|
||||
Logger.i("SQL数据库管理", "执行结果>>$result")
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.mogo.utils.sqlite.proxy
|
||||
|
||||
import android.util.Log
|
||||
import java.lang.reflect.InvocationHandler
|
||||
import java.lang.reflect.Method
|
||||
import java.lang.reflect.Proxy
|
||||
|
||||
/**
|
||||
* 使用java实现动态代理数据库操作类,加入更多额外的功能,来测试代理类的叠加功能
|
||||
* Created by donghongyu on 2019/9/6.
|
||||
*/
|
||||
class BaseDaoProxyShow : InvocationHandler {
|
||||
|
||||
private var target: Any? = null
|
||||
|
||||
/**
|
||||
* 绑定委托对象并返回一个【代理占位】
|
||||
*
|
||||
* @param target 真实对象
|
||||
* @return 代理对象【占位】
|
||||
*/
|
||||
fun bind(target: Any): Any {
|
||||
this.target = target
|
||||
//取得代理对象
|
||||
return Proxy.newProxyInstance(
|
||||
target.javaClass.classLoader,
|
||||
target.javaClass.interfaces, this
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 同过代理对象调用方法首先进入这个方法.
|
||||
*
|
||||
* @param proxy --代理对象
|
||||
* @param method -- 方法,被调用方法.
|
||||
* @param args -- 方法的参数
|
||||
*/
|
||||
@Throws(Throwable::class)
|
||||
override fun invoke(proxy: Any, method: Method, args: Array<Any>): Any? {
|
||||
var result: Any? = null
|
||||
//反射方法前调用
|
||||
Log.i("数据库代理", "显示执行>>${method.name}")
|
||||
//反射执行方法 相当于调用target.sayHelllo;
|
||||
result = method.invoke(target, *args)
|
||||
//反射方法后调用.
|
||||
Log.i("数据库代理", "执行结果>>$result")
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ public class V2XSQLiteUtils {
|
||||
* 查询当天的V2X数据
|
||||
*/
|
||||
public static List<V2XHistoryScenarioData> getScenarioHistoryData() {
|
||||
return getScenarioHistoryDao().query(new V2XHistoryScenarioData(), "triggerTime DESC");
|
||||
return getScenarioHistoryDao().query(new V2XHistoryScenarioData(), "triggerTime", true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user