|
|
@ -162,14 +162,35 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
return count; |
|
|
|
return count; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 关键字模糊检索全文 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param column 需要查找的列 |
|
|
|
|
|
|
|
* @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, |
|
|
|
|
|
|
|
String column, String mathSql) { |
|
|
|
|
|
|
|
String sql = "SELECT * FROM " |
|
|
|
|
|
|
|
+ CommonUtil.getClassName(clazz) |
|
|
|
|
|
|
|
+ " WHERE " |
|
|
|
|
|
|
|
+ column |
|
|
|
|
|
|
|
+ " MATCH '" |
|
|
|
|
|
|
|
+ mathSql |
|
|
|
|
|
|
|
+ "'"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Cursor cursor = db.rawQuery(sql, null); |
|
|
|
|
|
|
|
List<T> data = cursor.getCount() > 0 ? newInstanceEntity(db, clazz, cursor) : null; |
|
|
|
|
|
|
|
closeCursor(cursor); |
|
|
|
|
|
|
|
return data; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 检查某个字段的值是否存在 |
|
|
|
* 检查某个字段的值是否存在 |
|
|
|
* |
|
|
|
* |
|
|
|
* @param expression 字段和值"url=xxx" |
|
|
|
* @param expression 字段和值"url=xxx" |
|
|
|
* @return {@code true}该字段的对应的value已存在 |
|
|
|
* @return {@code true}该字段的对应的value已存在 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
static synchronized boolean checkDataExist(SQLiteDatabase db, Class clazz, |
|
|
|
static synchronized boolean checkDataExist(SQLiteDatabase db, Class clazz, String... expression) { |
|
|
|
String... expression) { |
|
|
|
|
|
|
|
db = checkDb(db); |
|
|
|
db = checkDb(db); |
|
|
|
CheckUtil.checkSqlExpression(expression); |
|
|
|
CheckUtil.checkSqlExpression(expression); |
|
|
|
String sql = |
|
|
|
String sql = |
|
|
@ -414,12 +435,16 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
* @return true,该数据库实体对应的表存在;false,不存在 |
|
|
|
* @return true,该数据库实体对应的表存在;false,不存在 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
static synchronized boolean tableExists(SQLiteDatabase db, Class clazz) { |
|
|
|
static synchronized boolean tableExists(SQLiteDatabase db, Class clazz) { |
|
|
|
|
|
|
|
return tableExists(db, CommonUtil.getClassName(clazz)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static synchronized boolean tableExists(SQLiteDatabase db, String tableName) { |
|
|
|
db = checkDb(db); |
|
|
|
db = checkDb(db); |
|
|
|
Cursor cursor = null; |
|
|
|
Cursor cursor = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
sb.append("SELECT COUNT(*) AS c FROM sqlite_master WHERE type='table' AND name='"); |
|
|
|
sb.append("SELECT COUNT(*) AS c FROM sqlite_master WHERE type='table' AND name='"); |
|
|
|
sb.append(CommonUtil.getClassName(clazz)); |
|
|
|
sb.append(tableName); |
|
|
|
sb.append("'"); |
|
|
|
sb.append("'"); |
|
|
|
print(TABLE_EXISTS, sb.toString()); |
|
|
|
print(TABLE_EXISTS, sb.toString()); |
|
|
|
cursor = db.rawQuery(sb.toString(), null); |
|
|
|
cursor = db.rawQuery(sb.toString(), null); |
|
|
@ -446,14 +471,15 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
static synchronized void createTable(SQLiteDatabase db, Class clazz, String tableName) { |
|
|
|
static synchronized void createTable(SQLiteDatabase db, Class clazz, String tableName) { |
|
|
|
db = checkDb(db); |
|
|
|
db = checkDb(db); |
|
|
|
|
|
|
|
createFTSTable(db); |
|
|
|
List<Field> fields = CommonUtil.getAllFields(clazz); |
|
|
|
List<Field> fields = CommonUtil.getAllFields(clazz); |
|
|
|
if (fields != null && fields.size() > 0) { |
|
|
|
if (fields != null && fields.size() > 0) { |
|
|
|
//外键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 table ") |
|
|
|
sb.append("create VIRTUAL table ") |
|
|
|
.append(TextUtils.isEmpty(tableName) ? CommonUtil.getClassName(clazz) : tableName) |
|
|
|
.append(TextUtils.isEmpty(tableName) ? CommonUtil.getClassName(clazz) : tableName) |
|
|
|
.append("("); |
|
|
|
.append(" USING fts4("); |
|
|
|
for (Field field : fields) { |
|
|
|
for (Field field : fields) { |
|
|
|
field.setAccessible(true); |
|
|
|
field.setAccessible(true); |
|
|
|
if (SqlUtil.ignoreField(field)) { |
|
|
|
if (SqlUtil.ignoreField(field)) { |
|
|
@ -461,8 +487,7 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
} |
|
|
|
} |
|
|
|
Class<?> type = field.getType(); |
|
|
|
Class<?> type = field.getType(); |
|
|
|
sb.append(field.getName()); |
|
|
|
sb.append(field.getName()); |
|
|
|
if (type == String.class || type == Map.class || type == List.class || SqlUtil.isOneToOne( |
|
|
|
if (type == String.class || SqlUtil.isOneToOne(field) || type.isEnum()) { |
|
|
|
field) || type.isEnum()) { |
|
|
|
|
|
|
|
sb.append(" varchar"); |
|
|
|
sb.append(" varchar"); |
|
|
|
} else if (type == int.class || type == Integer.class) { |
|
|
|
} else if (type == int.class || type == Integer.class) { |
|
|
|
sb.append(" interger"); |
|
|
|
sb.append(" interger"); |
|
|
@ -478,6 +503,8 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
sb.append(" data"); |
|
|
|
sb.append(" data"); |
|
|
|
} else if (type == byte.class || type == Byte.class) { |
|
|
|
} else if (type == byte.class || type == Byte.class) { |
|
|
|
sb.append(" blob"); |
|
|
|
sb.append(" blob"); |
|
|
|
|
|
|
|
} else if (type == Map.class || type == List.class) { |
|
|
|
|
|
|
|
sb.append(" text"); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
@ -486,7 +513,7 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (SqlUtil.isForeign(field)) { |
|
|
|
if (SqlUtil.isForeign(field)) { |
|
|
|
foreignArray.add(field); |
|
|
|
//foreignArray.add(field);
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (SqlUtil.isNoNull(field)) { |
|
|
|
if (SqlUtil.isNoNull(field)) { |
|
|
@ -514,6 +541,17 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
close(db); |
|
|
|
close(db); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 创建分词表 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private static void createFTSTable(SQLiteDatabase db) { |
|
|
|
|
|
|
|
String tableName = "ariaFts"; |
|
|
|
|
|
|
|
String sql = "CREATE VIRTUAL TABLE " + tableName + " USING fts4(tokenize= porter)"; |
|
|
|
|
|
|
|
if (!tableExists(db, tableName)) { |
|
|
|
|
|
|
|
db.execSQL(sql); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 打印数据库日志 |
|
|
|
* 打印数据库日志 |
|
|
|
* |
|
|
|
* |
|
|
|