SQLiteLintConfig config =new SQLiteLintConfig(SQLiteLint.SqlExecutionCallbackMode.HOOK);
SQLiteLintPlugin sqLiteLintPlugin = new SQLiteLintPlugin(config);
builder.plugin(sqLiteLintPlugin);
Matrix.init(builder.build());
sqLiteLintPlugin.start();
SQLiteLintPlugin plugin = (SQLiteLintPlugin) Matrix.with().getPluginByClass(SQLiteLintPlugin.class);
if (plugin == null) {
return;
}
if (!plugin.isPluginStarted()) {
plugin.start();
}
plugin.addConcernedDB(new SQLiteLintConfig.ConcernDb(TestDBHelper.get().getWritableDatabase())
//.setWhiteListXml(R.xml.sqlite_lint_whitelist)//disable white list by default
.enableAllCheckers());
实际测试使用
这里结合Matrix 官方例子来看。
publicclassTestDBHelperextendsSQLiteOpenHelper{
privatestaticfinalint DB_VERSION = 1;
privatestaticfinal String DB_NAME = "sqliteLintTest.db";
publicstaticfinal String TABLE_NAME = "testTable";
publicstaticfinal String TABLE_NAME_AUTO_INCREMENT = "testTableAutoIncrement";
publicstaticfinal String TABLE_NAME_WITHOUT_ROWID_BETTER = "testTableWithoutRowid";
publicstaticfinal String TABLE_NAME_Redundant_index = "testTableRedundantIndex";
publicstaticfinal String TABLE_NAME_CONTACT = "contact";
privatestatic TestDBHelper mHelper = null;
...
@OverridepublicvoidonCreate(SQLiteDatabase sqLiteDatabase){
String sql = "create table if not exists " + TABLE_NAME + " (Id integer primary key, name text, age integer)";
sqLiteDatabase.execSQL(sql);
String sqlAutoIncrement = "create table if not exists " + TABLE_NAME_AUTO_INCREMENT + " (Id integer primary key AUTOINCREMENT, name text, age integer)";
sqLiteDatabase.execSQL(sqlAutoIncrement);
String sqlWithoutRowId = "create table if not exists " + TABLE_NAME_WITHOUT_ROWID_BETTER + " (Id text primary key, name integer, age integer)";
sqLiteDatabase.execSQL(sqlWithoutRowId);
String sqlRedundantIndex = "create table if not exists " + TABLE_NAME_Redundant_index + " (Id text, name text, age integer, gender integer)";
sqLiteDatabase.execSQL(sqlRedundantIndex);
String indexSql = "create index if not exists index_age on " + TABLE_NAME_Redundant_index + "(age);";
String indexSql2 = "create index if not exists index_age_name on " + TABLE_NAME_Redundant_index + "(age, name);";
String indexSql3 = "create index if not exists index_name_age on " + TABLE_NAME_Redundant_index + "(name,age);";
String indexSql4 = "create index if not exists index_id on " + TABLE_NAME_Redundant_index + "(Id);";
sqLiteDatabase.execSQL(indexSql);
sqLiteDatabase.execSQL(indexSql2);
sqLiteDatabase.execSQL(indexSql3);
sqLiteDatabase.execSQL(indexSql4);
String contact = "create table if not exists " + TABLE_NAME_CONTACT + " (Id integer primary key, name text, age integer, gender integer, status integer)";
sqLiteDatabase.execSQL(contact);
String contactIndex = "create index if not exists index_age_name_status on " + TABLE_NAME_CONTACT + "(age, name, status);";
String contactIndex2 = "create index if not exists index_name_age_status on " + TABLE_NAME_CONTACT + "(name, age, status);";
String contactStatusIndex = "create index if not exists index_status on " + TABLE_NAME_CONTACT + "(status);";
sqLiteDatabase.execSQL(contactIndex);
sqLiteDatabase.execSQL(contactIndex2);
sqLiteDatabase.execSQL(contactStatusIndex);
}
...
}
这里新建了几个数据库表:
testTable:正常的表,integer 类型的id作为主键
testTableAutoIncrement:id 作为主键,且是自增属性
testTableWithoutRowid:设置了withoutRowid属性
testTableRedundantIndex:设置多个索引
contact:主要用于多条件查询
以下为测试的sql语句:
publicstatic String[] getTestSqlList() {
String[] list = new String[]{
"select * from testTable",//select *"select name from testTable where age>10",//no index"select name from testTableRedundantIndex where age&2 != 0",//not use index"select name from testTableRedundantIndex where name like 'j%'",//not use index"select name from testTableRedundantIndex where name = 'jack' and age > 20",
"select testTable.name from testTable, testTableAutoIncrement where testTableAutoIncrement.age=testTable.age",
"select Id from testTable where age = 10 union select Id from testTableRedundantIndex where age > 10",//union"select name from testTable order by age",//use tmp tree"select name from testTableRedundantIndex where gender=1 and age=5",//bigger index"select name, case when age>=18 then 'Adult' else 'child' end LifeStage from testTableRedundantIndex where age > 20 order by age,name,gender",
"select name,age,gender from testTableRedundantIndex where age > 10 and age < 20 or id between 30 and 40 or id = 1000 ORDER BY name,age,gender desc limit 10 offset 2;",
"select * from (select * from testTable where age = 18 order by age limit 10) as tb where age = 18 " +
"UNION select m.* from testTable AS m, testTableRedundantIndex AS c where m.age = c.age;",
"SELECT name FROM testTable WHERE name not LIKE 'rt%' OR name LIKE 'rc%' AND age > 20 GROUP BY name ORDER BY age;",
"SELECT id AS id_alias FROM testTable AS test_alias WHERE id_alias = 1 or id = 2",
"SELECT name FROM testTable WHERE id = (SELECT id FROM testTableRedundantIndex WHERE name = 'hello world')",
"SELECT * FROM testTable where name = 'rc' UNION SELECT * FROM testTableWithoutRowid UNION SELECT * FROM testTableAutoIncrement",
"SELECT name FROM testTable WHERE AGE GLOB '2*';",
"SELECT DISTINCT name FROM testTable GROUP BY name HAVING count(name) < 2;",
"SELECT name FROM contact WHERE status = 2;",
"select rowid from contact where name = 'rr' or age > 12",
"select t1.name ,t2.age from testTable as t1,testTableRedundantIndex as t2 where t1.id = t2.id and (t1.age=23 or t2.age=12);",
"select t1.name ,t2.age from testTable as t1,testTableRedundantIndex as t2 where t1.id = t2.id and (t1.age=23 and t2.age=12);",
"select name,age from contact where name like 'w%' and age > 12",
"select name,age from contact where name >= 'rc' and age&2=1",
"select name,age from contact where name = 'r' or age > 12 or status = 1",
};
return list;
}
publicSQLiteLintConfig(SQLiteLint.SqlExecutionCallbackMode sqlExecutionCallbackMode){
SQLiteLint.setSqlExecutionCallbackMode(sqlExecutionCallbackMode);
sConcernDbList = new ArrayList<>();
}
//com.tencent.sqlitelint.SQLiteLint#setSqlExecutionCallbackModepublicstaticvoidsetSqlExecutionCallbackMode(SqlExecutionCallbackMode sqlExecutionCallbackMode){
if (sSqlExecutionCallbackMode != null) {
return;
}
sSqlExecutionCallbackMode = sqlExecutionCallbackMode;
if (sSqlExecutionCallbackMode == SqlExecutionCallbackMode.HOOK) {
// hook must called before open the database
SQLite3ProfileHooker.hook();
}
}