完成了V2X过期数据清理工作,只保留当天的数据
This commit is contained in:
@@ -49,11 +49,13 @@ open class BaseDaoFactory {
|
||||
}
|
||||
//openOrCreateDatabase 如果不存在则先创建再打开数据库,如果存在则直接打开。
|
||||
sqLiteDatabasePath =
|
||||
"${context.getDir("database", Context.MODE_APPEND).path}/$dbName"
|
||||
"${context.getDir("database", Context.MODE_APPEND).path}/$dbName"
|
||||
sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(sqLiteDatabasePath, null)
|
||||
|
||||
// 这里为了演示,添加了日志工具的叠加使用,根据需要可以自己修改
|
||||
// baseDao = BaseDaoProxyShow().bind(BaseDaoProxyLog().bind(BaseDao<T>())) as IBaseDao<T>
|
||||
// baseDao = BaseDaoProxyLog().bind(SQLBaseDao<T>()) as IBaseDao<T>
|
||||
// baseDao = BaseDao<T>()
|
||||
baseDao = BaseDaoProxyLog().bind(BaseDao<T>()) as IBaseDao<T>
|
||||
|
||||
baseDao.init(sqLiteDatabase!!, entityClass)
|
||||
|
||||
@@ -23,12 +23,12 @@ interface IBaseDao<T> {
|
||||
fun delete(where: T): Int
|
||||
|
||||
/**
|
||||
* 根据条件 [where] 进行数据更新,如果[where]==null 则代表删除所有数据
|
||||
* 根据条件 [where] 进行数据更新,如果[where]==只初始化不赋值 则代表删除所有数据
|
||||
*/
|
||||
fun update(where: T, newEntity: T): Int
|
||||
|
||||
/**
|
||||
* 根据条件 [where] 进行数据查询,如果[where]==null 则代表查询所有数据
|
||||
* 根据条件 [where] 进行数据查询,如果[where]==只初始化不赋值 则代表查询所有数据
|
||||
*/
|
||||
fun query(where: T): MutableList<T>
|
||||
|
||||
|
||||
@@ -0,0 +1,421 @@
|
||||
package com.mogo.utils.sqlite;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import com.mogo.utils.logger.Logger;
|
||||
import com.mogo.utils.sqlite.annotation.DbField;
|
||||
import com.mogo.utils.sqlite.annotation.DbTable;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 数据库操作
|
||||
*
|
||||
* @param <T>
|
||||
* @author donghongyu
|
||||
*/
|
||||
public class SQLBaseDao<T> implements IBaseDao<T> {
|
||||
private String TAG = "SQLBaseDao";
|
||||
|
||||
//数据库操作的引用
|
||||
private SQLiteDatabase sqLiteDatabase;
|
||||
//要操作的数据实体的引用
|
||||
private Class<T> entityClass;
|
||||
//要操作的数据表名称
|
||||
private String tableName;
|
||||
//记录数据表是否存在
|
||||
private boolean isInit = false;
|
||||
|
||||
//因为反射会消耗时间,这里使用缓存,进行性能优化
|
||||
//缓存空间(key-字段名,标注的自定义注解 value-成员变量)
|
||||
private HashMap<String, Field> cacheField;
|
||||
|
||||
@Override
|
||||
public boolean init(@NotNull SQLiteDatabase sqLiteDatabase, @NotNull Class<T> entityClass) {
|
||||
this.sqLiteDatabase = sqLiteDatabase;
|
||||
this.entityClass = entityClass;
|
||||
//自动建表(只创建一次)
|
||||
if (!isInit) {
|
||||
//获取表名
|
||||
tableName = entityClass.getAnnotation(DbTable.class).tableName();
|
||||
|
||||
//如果数据库没有建立连接跳出操作防止异常信息
|
||||
if (!sqLiteDatabase.isOpen()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//执行Sql进行自动建表
|
||||
String createTableSql = getCreateTableSql();
|
||||
Logger.d(TAG, "执行SQL:" + createTableSql);
|
||||
sqLiteDatabase.execSQL(createTableSql);
|
||||
|
||||
//初始化缓存空间
|
||||
cacheField = new HashMap();
|
||||
initCacheField();
|
||||
|
||||
//标记已经创建过数据表
|
||||
isInit = true;
|
||||
}
|
||||
|
||||
return isInit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据
|
||||
*/
|
||||
@Override
|
||||
public long insert(T entity) {
|
||||
//1、准备好ContentValues中的数据
|
||||
//2、设置插入的内容
|
||||
ContentValues values = getContentValuesForInsert(entity);
|
||||
//3、执行插入
|
||||
long result;
|
||||
if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) {
|
||||
result = sqLiteDatabase.insert(tableName, null, values);
|
||||
} else {
|
||||
result = -1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
*/
|
||||
@Override
|
||||
public int delete(T where) {
|
||||
Condition condition = new Condition(getContentValuesForQuery(where));
|
||||
int result;
|
||||
if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) {
|
||||
//受影响行数
|
||||
result = sqLiteDatabase.delete(
|
||||
tableName,
|
||||
condition.getWhereCause(),
|
||||
condition.getWhereArgs()
|
||||
);
|
||||
} else {
|
||||
result = -1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据
|
||||
*/
|
||||
@Override
|
||||
public int update(T where, T newEntity) {
|
||||
Condition condition = new Condition(getContentValuesForQuery(where));
|
||||
int result;
|
||||
if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) {
|
||||
//受影响行数
|
||||
result = sqLiteDatabase.update(
|
||||
tableName,
|
||||
getContentValuesForInsert(newEntity),
|
||||
condition.getWhereCause(),
|
||||
condition.getWhereArgs()
|
||||
);
|
||||
} else {
|
||||
result = -1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询数据
|
||||
*
|
||||
* @param where 查询条件对象,同时也用来初始化对象使用
|
||||
*/
|
||||
@NotNull
|
||||
@Override
|
||||
public List<T> query(T where) {
|
||||
return query(where, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询数据
|
||||
*
|
||||
* @param where 查询条件对象
|
||||
* @param orderBy 排序规则
|
||||
* @param startIndex 开始的位置
|
||||
* @param limit 限制查询得到的数据个数
|
||||
*/
|
||||
public ArrayList<T> query(T where, String orderBy, Integer startIndex, Integer limit) {
|
||||
//拼接分页语句
|
||||
String limitString = null;
|
||||
if (startIndex != null && limit != null) {
|
||||
limitString = startIndex + "," + limit;
|
||||
}
|
||||
Condition condition = new Condition(getContentValuesForQuery(where));
|
||||
Cursor cursor = null;
|
||||
//定义查询结果
|
||||
ArrayList<T> result = new ArrayList<>();
|
||||
if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) {
|
||||
try {
|
||||
//查询数据库
|
||||
cursor = sqLiteDatabase.query(
|
||||
tableName,
|
||||
null,
|
||||
condition.getWhereCause(),
|
||||
condition.getWhereArgs(),
|
||||
null,
|
||||
null,
|
||||
orderBy,
|
||||
limitString
|
||||
);
|
||||
//将查到结果添加到返回集合中
|
||||
result.addAll(getQueryResult(cursor, where));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询db结果
|
||||
*/
|
||||
private ArrayList<T> getQueryResult(Cursor cursor, T where) {
|
||||
//定义查询结果
|
||||
ArrayList<T> result = new ArrayList<>();
|
||||
//Cursor从头读到尾
|
||||
//游标从头读到尾
|
||||
cursor.moveToFirst();
|
||||
try {
|
||||
if (where != null) {
|
||||
//移动游标获取下一行数据
|
||||
while (!cursor.isAfterLast()) {
|
||||
//通过反射构建一个查询结果对象
|
||||
T item = (T) where.getClass().newInstance();
|
||||
Set<Map.Entry<String, Field>> fieldIterator = cacheField.entrySet();
|
||||
for (Map.Entry<String, Field> stringFieldEntry : fieldIterator) {
|
||||
//获取数据库字段名称
|
||||
String columnName = stringFieldEntry.getKey();
|
||||
//数据库字段名对应的数据对象的成员变量
|
||||
Field field = stringFieldEntry.getValue();
|
||||
//获取指定列名对应的索引
|
||||
int columnIndex = cursor.getColumnIndex(columnName);
|
||||
//获取成员变量数据类型
|
||||
Class fieldType = field.getType();
|
||||
if (columnIndex != -1) {
|
||||
if (fieldType == (String.class)) {
|
||||
field.set(item, cursor.getString(columnIndex));
|
||||
} else if (fieldType == (Integer.class)) {
|
||||
field.set(item, cursor.getInt(columnIndex));
|
||||
} else if (fieldType == (Long.class)) {
|
||||
field.set(item, cursor.getLong(columnIndex));
|
||||
} else if (fieldType == (Double.class)) {
|
||||
field.set(item, cursor.getDouble(columnIndex));
|
||||
} else if (fieldType == (byte[].class)) {
|
||||
field.set(item, cursor.getBlob(columnIndex));
|
||||
} else {
|
||||
//未知类型
|
||||
throw new UnsupportedOperationException("未定义的数据类型:fieldName= " + columnName + " fieldType= " + fieldType);
|
||||
}
|
||||
}
|
||||
}
|
||||
//添加到结果集
|
||||
result.add(item);
|
||||
//移动到下一个位置
|
||||
cursor.moveToNext();
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException | InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
cursor.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化字段缓存
|
||||
*/
|
||||
private void initCacheField() {
|
||||
//1.取到所有的列名(查询一个空表获取表结构,不影响性能)
|
||||
if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) {
|
||||
String sqlQuery = "select * from " + tableName + " limit 1,0";
|
||||
Cursor cursor = sqLiteDatabase.rawQuery(sqlQuery, null);
|
||||
//获取所有的列名
|
||||
String[] columnNames = cursor.getColumnNames();
|
||||
//关闭资源
|
||||
cursor.close();
|
||||
//2.取所有成员名
|
||||
Field[] columnFields = entityClass.getDeclaredFields();
|
||||
//3.通过两层循环,进行对应关系建立
|
||||
for (String columnName : columnNames) {
|
||||
for (Field columnField : columnFields) {
|
||||
if (columnName.equals(columnField.getAnnotation(DbField.class).fieldName())) {
|
||||
columnField.setAccessible(true);
|
||||
cacheField.put(columnName, columnField);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼装创建数据表的SQL语句
|
||||
*/
|
||||
private String getCreateTableSql() {
|
||||
//create table if not exists tb_name(_id integer,name varchar2(20))
|
||||
StringBuffer sqlCreateTable = new StringBuffer();
|
||||
sqlCreateTable.append("create table if not exists ");
|
||||
sqlCreateTable.append(tableName + " (");
|
||||
//反射获取所有的数据对象内的成员变量
|
||||
Field[] fields = entityClass.getDeclaredFields();
|
||||
|
||||
for (int index = 0; index < fields.length; index++) {
|
||||
//字段名称
|
||||
String columnName = fields[index].getAnnotation(DbField.class).fieldName();
|
||||
//获取成员变量数据类型
|
||||
Class fieldType = fields[index].getType();
|
||||
|
||||
if (fieldType == (String.class)) {
|
||||
sqlCreateTable.append(columnName).append(" TEXT,");
|
||||
} else if (fieldType == (Integer.class)) {
|
||||
sqlCreateTable.append(columnName).append(" INTEGER,");
|
||||
} else if (fieldType == (Long.class)) {
|
||||
sqlCreateTable.append(columnName).append(" BIGINT,");
|
||||
} else if (fieldType == (Double.class)) {
|
||||
sqlCreateTable.append(columnName).append(" DOUBLE,");
|
||||
} else if (fieldType == (byte[].class)) {
|
||||
sqlCreateTable.append(columnName).append(" BLOB,");
|
||||
} else {
|
||||
//未知类型
|
||||
throw new UnsupportedOperationException("未定义的数据类型:fieldName= " + columnName + " fieldType= " + fieldType);
|
||||
}
|
||||
|
||||
if (index == fields.length - 1) {
|
||||
if (sqlCreateTable.toString().endsWith(",")) {
|
||||
sqlCreateTable.deleteCharAt(sqlCreateTable.length() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
sqlCreateTable.append(")");
|
||||
return sqlCreateTable.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插入使用的ContentValues
|
||||
*/
|
||||
private ContentValues getContentValuesForInsert(T entity) {
|
||||
ContentValues contentValues = new ContentValues();
|
||||
Set<Map.Entry<String, Field>> fieldIterator = cacheField.entrySet();
|
||||
for (Map.Entry<String, Field> stringFieldEntry : fieldIterator) {
|
||||
try {
|
||||
//获取变量的值
|
||||
Object valueObject = stringFieldEntry.getValue().get(entity);
|
||||
//获取列名
|
||||
String columnName = stringFieldEntry.getKey();
|
||||
//获取成员变量数据类型
|
||||
Class fieldType = stringFieldEntry.getValue().getType();
|
||||
|
||||
if (fieldType == (String.class)) {
|
||||
contentValues.put(columnName, (String) valueObject);
|
||||
} else if (fieldType == (Integer.class)) {
|
||||
contentValues.put(columnName, (Integer) valueObject);
|
||||
} else if (fieldType == (Long.class)) {
|
||||
contentValues.put(columnName, (Long) valueObject);
|
||||
} else if (fieldType == (Double.class)) {
|
||||
contentValues.put(columnName, (Double) valueObject);
|
||||
} else if (fieldType == (byte[].class)) {
|
||||
contentValues.put(columnName, (byte[]) valueObject);
|
||||
} else {
|
||||
//未知类型
|
||||
throw new UnsupportedOperationException("未定义的数据类型:fieldName= " + columnName + " fieldType= " + fieldType);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Logger.d(TAG, "contentValues:" + contentValues);
|
||||
return contentValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询使用的ContentValues
|
||||
*/
|
||||
private ContentValues getContentValuesForQuery(T entity) {
|
||||
ContentValues contentValues = new ContentValues();
|
||||
try {
|
||||
if (entity != null) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessError | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return contentValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* 条件拼接
|
||||
*/
|
||||
static class Condition {
|
||||
/**
|
||||
* 条件拼接
|
||||
* _id=?&&name=?
|
||||
*/
|
||||
private String whereCause;
|
||||
|
||||
private String[] whereArgs;
|
||||
|
||||
//根据传入的contentValues转换成查询条件
|
||||
public Condition(ContentValues whereContent) {
|
||||
//记录后面填充到查询语句“?”上的数据参数
|
||||
ArrayList<String> argList = new ArrayList<>();
|
||||
//拼接查询语句
|
||||
StringBuilder whereCaseSb = new StringBuilder();
|
||||
|
||||
/*
|
||||
* 是为了链接下面的查询条件条件,也或者是替换没有查询条件的语句。
|
||||
* 比如:要把检索条件作为一个参数传递给SQL,
|
||||
* 那么,当这个检索语句不存在的话就可以给它赋值为1=1.
|
||||
* 这样就避免了SQL出错,也就可以把加条件的SQL和不加条件的SQL合二为一。
|
||||
*/
|
||||
whereCaseSb.append(" 1=1 ");
|
||||
|
||||
Set<String> keys = whereContent.keySet();
|
||||
|
||||
//因为使用了“1=1”,所以即便是这里没有任何数据拼接,也是可以正常
|
||||
for (String key : keys) {
|
||||
Object valueObject = whereContent.get(key);
|
||||
if (valueObject != null) {
|
||||
String value = (String) valueObject;
|
||||
//拼接查询条件语句
|
||||
//1:1 and _id=? and name=?
|
||||
whereCaseSb.append(" and " + key + " =?");
|
||||
//记录?对应的value
|
||||
argList.add(value);
|
||||
}
|
||||
}
|
||||
//集合转成数组
|
||||
this.whereArgs = (String[]) argList.toArray(new String[argList.size()]);
|
||||
this.whereCause = whereCaseSb.toString();
|
||||
}
|
||||
|
||||
public String getWhereCause() {
|
||||
return this.whereCause;
|
||||
}
|
||||
|
||||
public String[] getWhereArgs() {
|
||||
return this.whereArgs;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.mogo.utils.sqlite.proxy
|
||||
|
||||
import android.util.Log
|
||||
import com.mogo.utils.logger.Logger
|
||||
import java.lang.reflect.InvocationHandler
|
||||
import java.lang.reflect.Method
|
||||
import java.lang.reflect.Proxy
|
||||
@@ -24,8 +24,8 @@ class BaseDaoProxyLog : InvocationHandler {
|
||||
this.target = target
|
||||
//取得代理对象
|
||||
return Proxy.newProxyInstance(
|
||||
target.javaClass.classLoader,
|
||||
target.javaClass.interfaces, this
|
||||
target.javaClass.classLoader,
|
||||
target.javaClass.interfaces, this
|
||||
)
|
||||
}
|
||||
|
||||
@@ -40,11 +40,11 @@ class BaseDaoProxyLog : InvocationHandler {
|
||||
override fun invoke(proxy: Any, method: Method, args: Array<Any>): Any? {
|
||||
var result: Any? = null
|
||||
//反射方法前调用
|
||||
Log.i("数据库代理", "当前执行>>${method.name}")
|
||||
Logger.i("数据库代理", "当前执行>>${method.name}")
|
||||
//反射执行方法 相当于调用target.sayHelllo;
|
||||
result = method.invoke(target, *args)
|
||||
//反射方法后调用.
|
||||
Log.i("数据库代理", "执行结果>>$result")
|
||||
Logger.i("数据库代理", "执行结果>>$result")
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user