|
|
@ -21,6 +21,7 @@ import android.database.Cursor; |
|
|
|
import android.database.sqlite.SQLiteDatabase; |
|
|
|
import android.database.sqlite.SQLiteDatabase; |
|
|
|
import android.database.sqlite.SQLiteOpenHelper; |
|
|
|
import android.database.sqlite.SQLiteOpenHelper; |
|
|
|
import android.support.annotation.NonNull; |
|
|
|
import android.support.annotation.NonNull; |
|
|
|
|
|
|
|
import android.support.v4.util.LruCache; |
|
|
|
import android.text.TextUtils; |
|
|
|
import android.text.TextUtils; |
|
|
|
import com.arialyy.aria.core.AriaManager; |
|
|
|
import com.arialyy.aria.core.AriaManager; |
|
|
|
import com.arialyy.aria.util.ALog; |
|
|
|
import com.arialyy.aria.util.ALog; |
|
|
@ -49,6 +50,7 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
private static final int DEL_DATA = 6; |
|
|
|
private static final int DEL_DATA = 6; |
|
|
|
|
|
|
|
|
|
|
|
private static volatile SqlHelper INSTANCE = null; |
|
|
|
private static volatile SqlHelper INSTANCE = null; |
|
|
|
|
|
|
|
private static LruCache<Integer, DbEntity> mDataCache = new LruCache<>(1024); |
|
|
|
|
|
|
|
|
|
|
|
static SqlHelper init(Context context) { |
|
|
|
static SqlHelper init(Context context) { |
|
|
|
if (INSTANCE == null) { |
|
|
|
if (INSTANCE == null) { |
|
|
@ -162,27 +164,27 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
return count; |
|
|
|
return count; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
///**
|
|
|
|
* 关键字模糊检索全文 |
|
|
|
// * 关键字模糊检索全文
|
|
|
|
* |
|
|
|
// *
|
|
|
|
* @param column 需要查找的列 |
|
|
|
// * @param column 需要查找的列
|
|
|
|
* @param mathSql 关键字语法,exsimple “white OR green”、“blue AND red”、“white NOT green” |
|
|
|
// * @param mathSql 关键字语法,exsimple “white OR green”、“blue AND red”、“white NOT green”
|
|
|
|
*/ |
|
|
|
// */
|
|
|
|
public static <T extends DbEntity> List<T> searchData(SQLiteDatabase db, Class<T> clazz, |
|
|
|
//public static <T extends DbEntity> List<T> searchData(SQLiteDatabase db, Class<T> clazz,
|
|
|
|
String column, String mathSql) { |
|
|
|
// String column, String mathSql) {
|
|
|
|
String sql = "SELECT * FROM " |
|
|
|
// String sql = "SELECT * FROM "
|
|
|
|
+ CommonUtil.getClassName(clazz) |
|
|
|
// + CommonUtil.getClassName(clazz)
|
|
|
|
+ " WHERE " |
|
|
|
// + " WHERE "
|
|
|
|
+ column |
|
|
|
// + column
|
|
|
|
+ " MATCH '" |
|
|
|
// + " MATCH '"
|
|
|
|
+ mathSql |
|
|
|
// + mathSql
|
|
|
|
+ "'"; |
|
|
|
// + "'";
|
|
|
|
|
|
|
|
//
|
|
|
|
Cursor cursor = db.rawQuery(sql, null); |
|
|
|
// Cursor cursor = db.rawQuery(sql, null);
|
|
|
|
List<T> data = cursor.getCount() > 0 ? newInstanceEntity(db, clazz, cursor) : null; |
|
|
|
// List<T> data = cursor.getCount() > 0 ? newInstanceEntity(db, clazz, cursor) : null;
|
|
|
|
closeCursor(cursor); |
|
|
|
// closeCursor(cursor);
|
|
|
|
return data; |
|
|
|
// return data;
|
|
|
|
} |
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 检查某个字段的值是否存在 |
|
|
|
* 检查某个字段的值是否存在 |
|
|
@ -328,19 +330,27 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
db = checkDb(db); |
|
|
|
db = checkDb(db); |
|
|
|
Class<?> clazz = dbEntity.getClass(); |
|
|
|
Class<?> clazz = dbEntity.getClass(); |
|
|
|
List<Field> fields = CommonUtil.getAllFields(clazz); |
|
|
|
List<Field> fields = CommonUtil.getAllFields(clazz); |
|
|
|
|
|
|
|
DbEntity cacheEntity = mDataCache.get(dbEntity.hashCode()); |
|
|
|
if (fields != null && fields.size() > 0) { |
|
|
|
if (fields != null && fields.size() > 0) { |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
StringBuilder sql = new StringBuilder(); |
|
|
|
sb.append("UPDATE ").append(CommonUtil.getClassName(dbEntity)).append(" SET "); |
|
|
|
StringBuilder prams = new StringBuilder(); |
|
|
|
|
|
|
|
sql.append("UPDATE ").append(CommonUtil.getClassName(dbEntity)).append(" SET "); |
|
|
|
int i = 0; |
|
|
|
int i = 0; |
|
|
|
for (Field field : fields) { |
|
|
|
for (Field field : fields) { |
|
|
|
field.setAccessible(true); |
|
|
|
field.setAccessible(true); |
|
|
|
if (SqlUtil.ignoreField(field)) { |
|
|
|
if (SqlUtil.ignoreField(field)) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
sb.append(i > 0 ? ", " : ""); |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
if (cacheEntity != null && field.get(dbEntity) == field.get(cacheEntity)) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//sb.append(i > 0 ? ", " : "");
|
|
|
|
|
|
|
|
//sb.append(field.getName()).append("='");
|
|
|
|
String value; |
|
|
|
String value; |
|
|
|
sb.append(field.getName()).append("='"); |
|
|
|
prams.append(i > 0 ? ", " : ""); |
|
|
|
|
|
|
|
prams.append(field.getName()).append("='"); |
|
|
|
Type type = field.getType(); |
|
|
|
Type type = field.getType(); |
|
|
|
if (type == Map.class) { |
|
|
|
if (type == Map.class) { |
|
|
|
value = SqlUtil.map2Str((Map<String, String>) field.get(dbEntity)); |
|
|
|
value = SqlUtil.map2Str((Map<String, String>) field.get(dbEntity)); |
|
|
@ -357,16 +367,21 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
value = obj == null ? "" : obj.toString(); |
|
|
|
value = obj == null ? "" : obj.toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sb.append(value == null ? "" : value); |
|
|
|
//sb.append(value == null ? "" : value);
|
|
|
|
sb.append("'"); |
|
|
|
//sb.append("'");
|
|
|
|
|
|
|
|
prams.append(TextUtils.isEmpty(value) ? "" : value); |
|
|
|
|
|
|
|
prams.append("'"); |
|
|
|
} catch (IllegalAccessException e) { |
|
|
|
} catch (IllegalAccessException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
i++; |
|
|
|
i++; |
|
|
|
} |
|
|
|
} |
|
|
|
sb.append(" where rowid=").append(dbEntity.rowID); |
|
|
|
if (!TextUtils.isEmpty(prams.toString())) { |
|
|
|
print(MODIFY_DATA, sb.toString()); |
|
|
|
sql.append(prams.toString()); |
|
|
|
db.execSQL(sb.toString()); |
|
|
|
sql.append(" where rowid=").append(dbEntity.rowID); |
|
|
|
|
|
|
|
print(MODIFY_DATA, sql.toString()); |
|
|
|
|
|
|
|
db.execSQL(sql.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
close(db); |
|
|
|
close(db); |
|
|
|
} |
|
|
|
} |
|
|
@ -477,9 +492,11 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
//外键Map,在Sqlite3中foreign修饰的字段必须放在最后
|
|
|
|
//外键Map,在Sqlite3中foreign修饰的字段必须放在最后
|
|
|
|
final List<Field> foreignArray = new ArrayList<>(); |
|
|
|
final List<Field> foreignArray = new ArrayList<>(); |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
sb.append("create VIRTUAL table ") |
|
|
|
//sb.append("create VIRTUAL table ")
|
|
|
|
|
|
|
|
sb.append("create table ") |
|
|
|
.append(TextUtils.isEmpty(tableName) ? CommonUtil.getClassName(clazz) : tableName) |
|
|
|
.append(TextUtils.isEmpty(tableName) ? CommonUtil.getClassName(clazz) : tableName) |
|
|
|
.append(" USING fts4("); |
|
|
|
//.append(" USING fts4(");
|
|
|
|
|
|
|
|
.append(" ("); |
|
|
|
for (Field field : fields) { |
|
|
|
for (Field field : fields) { |
|
|
|
field.setAccessible(true); |
|
|
|
field.setAccessible(true); |
|
|
|
if (SqlUtil.ignoreField(field)) { |
|
|
|
if (SqlUtil.ignoreField(field)) { |
|
|
@ -656,6 +673,7 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid")); |
|
|
|
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid")); |
|
|
|
|
|
|
|
mDataCache.put(entity.hashCode(), entity); |
|
|
|
entitys.add(entity); |
|
|
|
entitys.add(entity); |
|
|
|
} |
|
|
|
} |
|
|
|
closeCursor(cursor); |
|
|
|
closeCursor(cursor); |
|
|
|